参考教材:《C Primer Plus(第六版)》Stephen Prata 著 姜佑 译
1.编写一个程序,调用一次printf()函数,把你的名和姓打印在一行。再调用一次printf()函数,把你的名和姓分别打印在两行。然后,再调用两次printf()函数,把你的名和姓打印在一行。
#include <stdio.h>
int main (void)
{
printf("Jerry Lin\n");
printf("Jerry\nLin\n");
printf("Jerry ");
printf("Lin\n");
return 0;
}
2.编写一个程序,打印你的姓名和地址。
#include <stdio.h>
int main(void)
{
printf("JerryLin\nAnhui,Wuhu\n");
return 0;
}
3.编写一个程序,把你的年龄转换成天数,并显示这两个值。这里不用考虑闰年的问题。
#include <stdio.h>
int main (void)
{
int age,day;
printf("Please enter your age\n");
scanf("%d",&age);
day = 365 * age;
printf("%d",day);
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(void)
{
jolly();
jolly();
jolly();
deny();
return 0;
}
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,Rus-
sia"; 另一个名为ic(),调用一次打印一次“India,China”。其他内容在main()函数中完成。
#include <stdio.h>
void br(void);
void ic(void);/*函数原型*/
int main(void)
{
br();
printf(",");
ic();
printf("\n");
ic();
printf("\n");
br();
printf("\n");
return 0;
}
void br(void)/*定义函数*/
{
printf("Brazil,Russia");
}
void ic(void)
{
printf("India,China");
}
6.编写一个程序,创建一个整型变量toes,并将toes设置成10。程序中还要计算toes的两倍和toes的平方。该程序应打印3个值,并分别描述以示区分。
#include <stdio.h>
int main(void)
{
int toes,toes2,toes3;
toes = 10 ;
toes2 = 2 * toes;
toes3 = toes * toes;
printf("toes=%d\n",toes);
printf("toes*2=%d\n",toes2);
printf("toes*toes=%d\n",toes3);
return 0;
}
7.许多研究表明,微笑益处多多。编写一个程序,生成以下格式的输出:
Smile!Smile!Smile!
Smile!Smile!
Smile!
该程序要定义一个函数,该函数被调用一次答应一次“Smile!”,根据程序的需要使用该函数。
#include <stdio.h>
void hahaha(void);/*函数原型*/
int main(void)
{
hahaha();
hahaha();
hahaha();
printf("\n");
hahaha();
hahaha();
printf("\n");
hahaha();
printf("\n");
return 0;
}
void hahaha(void)/*定义一个函数*/
{
printf("Smiles!");
}
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(void)
{
printf("starting now:\n");
one_three();
printf("done!\n");
return 0;
}
void one_three(void)/*定义一个函数*/
{
printf("one\n");
two();
printf("three\n");
}
void two(void)
{
printf("two\n");
}
(个人答案,如有错误或更正建议欢迎联系作者)