今天的主要练习~
1.编写一个程序,调用一次 printf()函数,把你的名字和姓打印在一行。在调用一次 printf()函数,把你的名和姓分别打印在两行。然后,在调用两次printf() 函数,把你的名和姓打印在一行。输出应如下所示:
Gustav Mahler -----第1次打印内容
Gustav -----第2次打印内容
Mahler -----仍是第2次打印的内容
Gustav Mahler -----第3次和第4次打印的内容
#include <stdio.h>
int main()
{
printf("Gustav Mahler\n");
printf("Gustav\nMahler\n");
printf("Gustav");
printf("Mahler\n");
return 0;
}
2.编写一个程序,打印你的姓名和地址。
#include <stdio.h>
int main()
{
printf("My name is HuWence\n");
printf("I live in xi'an,shaanxi province\n");
return 0;
}
3.编写一个程序把你的年龄转换成天数,并显示这两个值。这里不用考虑闰年的问题。
#include <stdio.h>
int main()
{
int age=22, days;
days = age * 365;
printf("I am %d years old,i lived for %d days\n",age,days);
return 0;
}
4.编写一个程序,生成以下输出:
For he’s a jolly good fellow!
For he’s a jolly good fellow!
For he’s a jolly good fellow!
Which nobody can deny!
除了main函数以外,该程序还要调用两个自定义函数:一个名为jolly(),用于打印前3条消息,调用一次打印一条;另一个函数名为deny(),打印最后一条消息。
#include <stdio.h>
void jolly(void);
void deny(void);
int main()
{
printf("For he's a ");
jolly();
printf("good fellow\n");
printf("For he's a ");
jolly();
printf("good fellow\n");
printf("For he's a ");
jolly();
printf("good fellow\n");
printf("Which nobody can");
deny();
printf("\n");
return 0;
}
void jolly(void)
{
printf("jolly");
}
void deny(void)
{
printf("deny");
}
\\上面的代码是我第一反应写出来的觉得太繁琐,而下面的代码是经过改良后的
#include <stdio.h>
void jolly(void);
void deny(void);
int main()
{
jolly();
jolly();
jolly();
deny();
}
void jolly(void)
{
printf("For he's a jolly good fellow!\n");
}
void deny(void)
{
printf("Which nobody can deny!\n");
}
5.编写一个程序,生成以下输出:
Brazil, Russia, India, China
India, China,
Brazil, Russia
除了main()以外,该程序还要调用两个自定义函数:一个名为br(), 调用一次打印一次“Brazil,Russia";另一个名为ic(),调用一次打印一次“India, China”。其他内容在main()函数中完成。
#include <stdio.h>
void br(void);
void ic(void);
int main()
{
br();
ic();
ic();
br();
printf("\n");
return 0;
}
void br(void)
{
printf("Brazil, Russia, ");
}
void ic(void)
{
printf("India, China\n");
}
6.编写一个程序,创建一个整型变量toes,并将toes设置为10。程序中还要计算toes的两倍和toes的平方。该程序应打印3个值,并分别描述以示区分。
#include <stdio.h>
int main(void)
{
int toes;
toes = 10;
printf("toes = %d\n",toes);
printf("two toes = %d\n" , 2*toes);
printf("toes squared = %d\n", toes * toes);
return 0;
}
7.许多研究表明,微笑益处多多。编写一个程序,生成以下格式的输出:
Smile!Smile!Smile!
Smile!Smile!
Smile!
该程序要定义一个函数,该函数被调用一次打印一次“Smile!”,根据程序的需要使用该函数。
#include <stdio.h>
void t(void);
int main()
{
t();t();t();
printf("\n");
t();t();
printf("\n");
t();
printf("\n");
return 0;
}
void t(void)
{
printf("Smile!");
}
8.在C语言中,函数可以调用另一个函数。编写一个程序,调用一个名为one_three()的函数。该函数在一行打印单词“one”,在调用第2个函数two(),然后再另一行打印单词“three”。two()函数在一行现实单词“two”。main()函数在调用one_three()函数前要打印短语“starting now:”,并在调用完毕后显示短语“done!”。因此,该程序的输出应如下所示:
starting now:
one
two
three
done!
#include <stdio.h>
void one_three(void);
void two(void);
int main()
{
printf("staring now:\n");
one_three();
printf("done!\n");
return 0;
}
void one_three()
{
printf("one\n");
two();
printf("three\n");
}
void two()
{
printf("two\n");
}
—— —— —— —— —— —— —— ——— ——分割线
以下是今天随手练习的:
//把2英寻转换成英尺
#include <stdio.h>
int main()
{
int feet,fathoms;
fathoms = 2;
feet = 6 * fathoms;
printf("There are %d feet in %d fathoms!\n", feet , fathoms);
printf("Yes, i said %d feet!\n", 6 * fathoms);
return 0;
}
//一个文件中包含两个函数
#include <stdio.h>
void butler(void);//ANSI/ISO 函数原型
int main(void)
{
printf("I will summon the butler function.\n");
butler();
printf("Yes.Bring me some tea and writeable DVDs.\n");
return 0;
}
void butler(void) //函数定义开始
{
printf("You rang, sir?\n");
}
#include <stdio.h>
int main()
{
printf("Gustav Mahler\n");
printf("Gustav\nMahler\n");
printf("Gustav");
printf("Mahler\n");
return 0;
}