"C Primer Plus" (sixth edition) answers (2.12)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43219615/article/details/99868104

To use the "C Primer Plus" C beginners learning to prepare.

  1. Write a program called once printf () function, put your name printed on a line. Once again calls printf () function, your name were printed in two lines. Then, two calls to printf () function, put your name printed on a line. It should be output as shown in FIG.
    Export
#include <stdio.h>
int main(int argc, char const **argv) {
	/*调用一次printf函数,把名字打印在一行 \n是换行,也就是从另一行输出*/
	printf("Gustav Mahler\n");
	
	/*调用一次printf函数,把你的名字打印在两行*/
	printf("Gustav\nMahler\n");
	
	/*调用两次printf函数 把名字打印在一行 第二个printf输出的内容紧挨着第一行输出*/
	printf("Gustav ");
	printf("Mahler");
	
	return 0; 
} 
  1. Write a program to print your name and address.
#include <stdio.h>
int main(int argc, char const **argv) {
	/*打印地址和姓名*/
	printf("My name is Xinghai Liao, from Changsha, China."); 
	
	return 0; 
} 
  1. Write a program that converts your age is the number of days, and show these two values. Here do not consider the problem a leap year.
#include <stdio.h>
//函数声明
int ageConvertDay(int age);
int main(int argc, char const **argv) {
	int age;
	//输入年龄 
	printf("Please enter your age:");
	scanf("%d", &age);
	
	//ageConvertDay函数计算结果 
	int result = ageConvertDay(age);
	//输出结果 
	printf("Your age is %d, the result is %d.", age, result);
	
	return 0;
} 
/*不考虑闰年计算年龄的函数定义*/
int ageConvertDay(int age) {
	return age*365;
}
  1. Write a program that generates at the output. In addition to the main function, the program also two custom function calls: a called Jolly (), for 3 message before printing, printing a first call; deny another named function (), the last message print.
    image
#include <stdio.h>

/*函数声明*/
void jolly();
void deny(); 

int main(int argc, char const **argv) {
	/*调用三次jolly*/
	jolly();
	jolly();
	jolly();
	
	/*调用deny*/ 
	deny();
} 

/*函数定义*/
void jolly() {
	printf("For he's a jolly good fellow!\n");	
} 
void deny() {
	printf("Which nobody can deny!\n");
}
  1. Write a program that produces the following output.
    Title picture
#include <stdio.h>

/*函数声明*/
void br(void);
void ic(void);

int main(int argc, char const **argv) {
	/*打印第一句话*/
	br();
	printf(", ");//输出逗号 
	ic();
	printf("\n");//输出换行
	
	/*打印第二句话*/
	ic();
	printf(", \n");//输出逗号和换行 
	
	/*打印第三句话*/
	br();
} 

/*函数定义*/
void br(void) {
	printf("Brazil, Russia");	
} 
void ic(void) {
	printf("India, China");
}
  1. Write a program to create an integer variable toes, and set toes 10. The program also calculates the square twice and toes of the toes. The program should print three values, and are described to show distinction.
#include <stdio.h>
int main(int argc, char const **argv) {
	/*声明整型变量toes*/ 
	int toes;
	/*给toes变量赋值10*/
	toes = 10;
	
	/*计算toes的两倍 并赋值给doubleToes*/
	int doubleToes = toes * 2;
	/*计算toes的平方 并赋值给squareToes*/ 
	int squareToes = toes * toes;
	
	/*输出*/
	printf("toes:%d    doubleToes:%d    squareToes:%d", toes, doubleToes, squareToes);
	return 0; 
} 
  1. Write a program that generates output in the following format:
    Title picture
#include <stdio.h>
int main(int argc, char const **argv) {
	printf("Smile!Smile!Smile!\n");
	printf("Smile!Smile!\n");
	printf("Smile!");
	return 0; 
} 
  1. Write a program that calls a function named one_three () of. In the first line of the function prints the word "one", and then call the second function two (), and then in another line prints the word "Three". two () function displays the word "two" in a row. main () function before calling one_three () function to print "starting now:", and in print after the call is complete "done!". Output should look like.
    Export
#include <stdio.h>

/*声明函数*/
void two();
void one_three();

int main(int argc, char const **argv) {
	printf("starting now:\n");
	one_three();
	printf("done!");
} 

/*定义函数*/ 
void two() {
	printf("two\n");
}
void one_three() {
	printf("one\n");
	two();//在这个函数调用two()函数 有些老的编译器声明时不把two()函数放在前面声明可能会报错 
	printf("three\n");
}

Guess you like

Origin blog.csdn.net/weixin_43219615/article/details/99868104