C Primer Plus 第6版 第2章 编程练习

第2章主要是练习打印 printf的使用,自定义函数。

1.使用printf打印你的姓名

#include<stdio.h>
int main(void)
{
	printf("Gustav Mathlr\n");
	printf("Gustav\n");
	printf("Mahler\n");
	printf("Gustav Mathlr\n");

	getchar(); //在VS中让窗口停留
	return 0;
}

2.打印你的姓名和地址

#include<stdio.h>
int main(void)
{
	printf("小明\n");
	printf("阿拉德大陆\n");


	getchar(); //在VS中让窗口停留
	return 0;
}

3.将年龄转换成天数,并且显示这两个值。(不考虑闰年)

#include<stdio.h>
int main(void)
{
	int age;
	int day;
	printf("请输入您的年龄\n");
	scanf_s("%d", &age);
	day = age * 365;
	printf("%d 岁相当于 %d天", age,day);


	getchar(); getchar(); //在VS中让窗口停留
	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!

四句话。要求自定义2个函数,分别打印第1个语句和第4个语句。

#include<stdio.h>
void jolly()
{
	printf("For he's a jolly good fellow!\n");
}
void deny()
{
	printf("Which nobody can deny!\n");
}
int main(void)
{

	jolly();
	jolly();
	jolly();
	deny();
	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

 5.

打印:
Brazil, Russia, India, China
India, China
Brazil, Russia

要求自定义函数br()打印 Brazil, Russia  

ic() 打印 India, China

#include<stdio.h>
void br()
{
	printf("Brazil, Russia");
}
void ic()
{
	printf("India, China");
}
int main(void)
{
	br();
	printf(",");
	ic();
	printf("\n");
	ic();
	printf("\n");
	br();
	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

6. 创建一个整型变量toes将toes设置为10,计算它的2倍和平方。并打印这3个值。

#include<stdio.h>
int main(void)
{
	int toes = 10;
	printf("toes = %d, tose * 2 = %d, toes * tose = %d", toes, toes * 2, toes * toes);
	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

7.定义函数打印smile

#include<stdio.h>
void smile()
{
	printf("Smile!");
}
int main(void)
{
	smile(); smile(); smile(); printf("\n");
	smile(); smile(); printf("\n");
	smile(); printf("\n");
	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

8. main函数中调用 one_three(),one_three打印one然后调用two()然后打印three,其中two()打印two

#include<stdio.h>
void two()
{
	printf("two\n");
}
void one_three()
{
	printf("one\n");
	two();
	printf("three\n");
}
int main(void)
{
	printf("start now:\n");
	one_three();
	printf("done!");
	getchar(); getchar(); //在VS中让窗口停留
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41068877/article/details/83268166