1 #include <stdio.h> 2 3 int main(void) /* 一个简单的 C 程序 */ 4 { 5 int num; /* 定义一个名为 num 的变量 */ 6 num = 1; /* 为 num 赋一个值 */ 7 8 printf("I am a simple "); /* 使用 printf() 函数 */ 9 printf("computer.\n"); 10 printf("My favorite number is %d because it is first.\n", num); 11 // favorite 喜爱的 first 第一 12 13 getchar(); // 暂停等待用户输入 14 return 0; 15 16 }
1 /* 2 希望能运行。 3 */ 4 x = 100; 5 y = 200; 6 /* 其他内容已省略 */
接下来,假设你决定删除第 4 行,但不小心把第 3 行删除了(*/)。代码如下:
1 /* 2 希望能运行 3 y = 200; 4 /* 其他内容已省略 */
1 int main() // 旧规矩 2 { 3 int doors; 4 int dogs; 5 doors = 5; 6 dogs = 3; 7 // 其他语句 8 }
C99 和 C11 遵循 C++ 的惯例,可以把声明放在块中的任何位置。尽管如此,首次使用变量之前一定要先声明它。因此,如果编译器支持这一新特性,可以这样编写上面的代码:
1 int main() // 目前的 C 规则 2 { 3 // 一些语句 4 int doors; 5 doors = 5; // 第 1 次 使用 doors 6 // 其他语句 7 int dogs; 8 dogs = 3; // 第 1 次 使用 dogs 9 // 其他语句 10 11 }
有效的名称 | 无效的名称 |
wiggles | $Z]** |
cat2 | 2cat |
Hot_Tub | Hot-Tub |
taxRate | tax rate |
_kcab | don‘t |
1 #include <stdio.h> 2 { 3 语句 4 return 0; 5 } 6 7 (大部分语句都以分号结尾)
1 int main(void){ int four; four 2 = 3 4 4 ; 5 printf( 6 "%d\n", 7 four);return 0;}
1 // fathm_ft.c -- 把 2 音寻转换为英寸。 2 3 #include <stdio.h> 4 5 int main(void) 6 { 7 int feet, fathoms; // feet 英尺 fathoms 音寻 8 9 fathoms = 2; 10 feet = 6 * fathoms; 11 printf("There are %d feet in %d fathoms!\n", feet, fathoms); 12 printf("Yes, I said %d feet!\n", 6 * fathoms); 13 14 getchar(); // 暂停屏幕 等待用户输入 15 return 0; 16 } 17 18 /************************************ 19 输出结果: 20 ------------------------------------- 21 There are 12 feet in 2 fathoms! 22 Yes, I said 12 feet! 23 ------------------------------------- 24 *************************************/
1 // * two_func.c -- 一个文件中包含两个函数 2 3 #include <stdio.h> 4 5 void butler(void); /* ANSI/ISO C 函数原型 */ 6 7 int main(void) 8 { 9 printf("I will summon the butler function.\n"); 10 butler(); 11 printf("Yes. Bring me some tea and writeable DVDs.\n"); 12 13 getchar(); 14 return 0; 15 } 16 17 void butler(void) /* 函数定义开始 */ 18 { 19 20 printf("You rang, sir?\n"); 21 22 } 23 24 /******************************************* 25 输出结果: 26 -------------------------------------------- 27 I will summon the butler function. 28 You rang, sir? 29 Yes. Bring me some tea and writeable DVDs. 30 -------------------------------------------- 31 32 ********************************************/
1 /* nogood.c -- 有错误的程序 */ 2 3 #include <stdio.h> 4 5 int main(void) 6 ( 7 int n, int n2, int n3; 8 /* 该程序有多处错误 9 n = 5; 10 n2 = n * n; 11 n3 = n2 * n2; 12 printf("n = %d, n squared = %d, n cubed = %d\n", n, n2, n3) 13 14 return 0; 15 ) 16 17 /************************************************************ 18 编译不通过 19 语法错误: 20 1) main() 函数的 函数体错用了 () 圆括号,应该改用 {} 花括号 21 2)定义变量格式出错,应该改为: 22 int n, n2, n3; 23 或者 24 int n; 25 int n2; 26 int n3; 27 3) 注释没有成对出现 少了后面的 * / 28 4) printf() 函数最后的少了分号 ; 29 30 语义错误: 31 程序中的 n3 原意是代表 n 的立方,但是却写成了 n2 * n2,由于 32 n2 里的值是 n 的平方,也就是 25 ,所以 n3 = n2 * n2; 33 等价于 n3 = 25 * 25; 因此得出的结果与程序原意不符 34 应将其修改为 n3 = n2 * n; 或者 n3 = n * n * n; 35 *************************************************************/
auto | extern | short | while |
break | float | signed | _Alignas |
case | for | sizeof | _Alignof |
char | goto | static | _Atomic |
const | if | struct | _Bool |
continue | inline | switch | _Complex |
default | int | typedef | _Generic |
do | long | union | _Imaginary |
double | register | unsigned | _Noreturn |
else | restrict | void | _Static_assert |
enum | return | volatile | _Thread_local |
1 include studio.h 2 int main{void} /* 该程序打印一年有多少周 /* 3 ( 4 int s 5 s := 56; 6 printf(There are s weeks in a year.); 7 return 0; 8 9 10 11 ------------------ 以下为修改程序 ------------------------------ 12 13 #include <stdio.h> 14 int main(void) /* 该程序打印一年有多少周 */ 15 { 16 int s; 17 18 s = 56; 19 printf("There are %d weeks in a year.", s); 20 21 return 0; 22 } 23 24 /*********************************************************** 25 输出结果: 26 ------------------------------------------------------------ 27 There are 56 weeks in a year. 28 ------------------------------------------------------------ 29 ************************************************************/
5. 假设下面的 4 个例子都是完整程序中的一部分,它们都输出什么结果?
1 a. printf("Baa Baa Black Sheep."); 2 printf("Have you any wool?\n"); 3 /**************************************************************** 4 输出结果: 5 ----------------------------------------------------------------- 6 Baa Baa Black Sheep.Have you any wool? 7 8 ----------------------------------------------------------------- 9 ****************************************************************/ 10 11 b. printf("Begone!\nO Creature of lard!\n"); 12 /**************************************************************** 13 输出结果: 14 ----------------------------------------------------------------- 15 Begone! 16 O Creature of lard! 17 18 ----------------------------------------------------------------- 19 ****************************************************************/ 20 21 c. printf("What?\nNo/nfish?\n"); 22 /**************************************************************** 23 输出结果: 24 ----------------------------------------------------------------- 25 What? 26 NO/nfish? 27 28 ----------------------------------------------------------------- 29 ****************************************************************/ 30 31 d. int num; 32 num = 2; 33 printf("%d + %d = %d", num, num, num + num); 34 /**************************************************************** 35 输出结果: 36 ----------------------------------------------------------------- 37 2 + 2 = 4 38 ----------------------------------------------------------------- 39 ****************************************************************/
1 #include <stdio.h> 2 int main(void) 3 { 4 int a, b; 5 6 a = 5; 7 b = 2; /* 第 7 行 */ // a = 5, b = 2; 8 b = a; /* 第 8 行 */ // a = 5, b = 5; 9 a = b; /* 第 9 行 */ // a = 5, b = 5; 10 printf("%d %d\n", b, a); 11 return 0; 12 } 13 14 // 请问,在执行完第 7 、第 8 、第 9 行后,程序的状态分别是什么? 15 16 /**************************************************** 17 输出结果: 18 ----------------------------------------------------- 19 5 5 20 ----------------------------------------------------- 21 *****************************************************/
9. 考虑下面的程序:
1 #include <stdio.h> 2 int main(void) 3 { 4 int x, y; 5 6 x = 10; 7 y = 5; /* 第 7 行 */ // x = 10, y = 5; 8 y = x + y; /* 第 8 行 */ // x = 10, y = 15; 9 x = x * y; /* 第 9 行 */ // x = 150, y = 15; 10 printf("%d %d\n", x, y); // 输出为:150 15 11 return 0; 12 } 13 14 // 请问,在执行完第 7 、第 8 、第 9 行后,程序的状态分别是什么? 15 16 /**************************************************** 17 输出结果: 18 ----------------------------------------------------- 19 150 15 20 21 ----------------------------------------------------- 22 *****************************************************/
2.12 编程练习
1 /* 2 1. 编写一个程序,调用一次 printf() 函数,把你的姓名打印在一行。 3 再调用一次 printf() 函数,把你的姓名分别打印在两行。 4 然后,再调用两次 printf()函数,把你的姓名打印在一行。输出应 5 如下所示 6 -------------------------------------------------------------- 7 Gustav Mahler <- 第 1 次打印的内容 8 Gustav <- 第 2 次打印的内容 9 Mahler <- 仍是第 2 次打印的内容 10 Gustav Mahler <- 第 3 次和第 4 次打印的内容 11 -------------------------------------------------------------- 12 13 */ 14 15 16 #include <stdio.h> 17 18 int main(void) 19 { 20 printf("Gustav Mahler\n"); // Gustav Mahler 21 22 printf("Gustav\nMahler\n"); // Gustav 23 // Mahler 24 25 printf("Gustav "); 26 printf("Mahler\n"); 27 // Gustav Mahler 28 29 30 getchar(); 31 return 0; 32 } 33 34 /*************************************************** 35 36 输出结果: 37 ---------------------------------------------------- 38 Gustav Mahler 39 Gustav 40 Mahler 41 Gustav Mahler 42 ---------------------------------------------------- 43 44 ***************************************************/
1 /************************************************* 2 2. 编写一个程序,打印你的姓名和地址。 3 **************************************************/ 4 5 #include <stdio.h> 6 7 int main(void) 8 { 9 10 printf("Name: M0usek.\n"); 11 printf("Address: China.\n"); 12 13 14 getchar(); 15 return 0; 16 } 17 18 /*************************************************** 19 输出结果: 20 ---------------------------------------------------- 21 Name: M0usek. 22 Address: China. 23 24 ---------------------------------------------------- 25 26 ****************************************************/
1 /*********************************************************** 2 3. 编写一个程序把你的年龄转换成天数,并显示这两个值。 3 这里不用考虑闰年的问题。 4 5 年龄转换天数简单实现版: 6 该程序只是简单的将用户输入的年龄转换成天数,如 用户输入 10 岁 7 则将 10 * 365 = 3650 天,只是一个大概数 并不精准。 8 9 *************************************************************/ 10 11 #include <stdio.h> 12 13 int main(void) 14 { 15 int nAges = 0; // 年龄 16 int nDayCount = 0; // 天数 17 18 19 printf("Please input your ages: "); 20 scanf_s("%d", &nAges); 21 22 nDayCount = nAges * 365; // 年龄转天数 23 24 printf("%d ages = %d days\n", nAges, nDayCount); 25 26 getchar(); 27 return 0; 28 } 29 30 /********************************************************** 31 输出结果: 32 ----------------------------------------------------------- 33 Please input your ages: 10 34 10 ages = 3650 days 35 ----------------------------------------------------------- 36 **********************************************************/
1 /******************************************************** 2 4. 编写一个程序,生成以下输出: 3 4 For he‘s a jolly good fellow! 5 For he‘s a jolly good fellow! 6 For he‘s a jolly good fellow! 7 Which nobody can deny! 8 9 除了 main() 函数以外,该程序还要调用两个自定义函数:一个名为 10 jolly(),用于打印前 3 条消息,调用一次打印一条;另一个函数名 11 为 deny() ,打印最后一条消息。 12 13 *********************************************************/ 14 15 #include <stdio.h> 16 17 void jolly(void); // jolly() 函数声明 18 void deny(void); // deny() 函数声明 19 20 21 int main(void) 22 { 23 jolly(); // 调用 jolly() 函数 24 jolly(); 25 jolly(); 26 deny(); // 调用 deny() 函数 27 28 getchar(); 29 return 0; 30 } 31 32 // jolly() 函数定义 33 void jolly() 34 { 35 printf("For he‘s a jolly good fellow!\n"); 36 } 37 38 // deny() 函数定义 39 void deny() 40 { 41 printf("Which nobody can deny!\n"); 42 } 43 44 /****************************************************** 45 输出结果: 46 ------------------------------------------------------- 47 For he‘s a jolly good fellow! 48 For he‘s a jolly good fellow! 49 For he‘s a jolly good fellow! 50 Which nobody can deny! 51 ------------------------------------------------------- 52 *******************************************************/
1 /************************************************* 2 5. 编写一个程序,生成以下输出: 3 4 Brazil, Russia, India, China 5 India, China, 6 Brazil, Russia 7 8 除了 main()以外,该程序还要调用两个自定义函数: 9 一个名为 br(),调用一次打印“Brazil, Russia”; 10 另一个名为 ic(),调用一次打印一次 "India, China"。 11 其他内容在 main() 函数中完成。 12 **************************************************/ 13 14 15 #include <stdio.h> 16 17 void br(); // 函数声明 18 void ic(); // 函数声明 19 20 int main(void) 21 { 22 br(); 23 printf(", "); 24 ic(); 25 printf("\n"); 26 ic(); 27 printf(",\n"); 28 br(); 29 printf("\n"); 30 31 getchar(); 32 return 0; 33 } 34 35 void br() 36 { 37 printf("Brazil, Russia"); 38 } 39 40 void ic() 41 { 42 printf("India, China"); 43 } 44 45 /********************************************************* 46 输出结果: 47 ---------------------------------------------------------- 48 Brazil, Russia, India, China 49 India, China, 50 Brazil, Russia 51 52 ---------------------------------------------------------- 53 **********************************************************/
1 /***************************************** 2 6. 编写一个程序,创建一个整型变量toes,并将 3 toes 设置为 10 。程序中还要计算 toes 的两倍 4 和 toes 的平方。该程序应打印 3 个值,并分别 5 描述以示区分 6 ******************************************/ 7 8 #include <stdio.h> 9 10 int main(void) 11 { 12 int toes = 10; 13 14 printf("toes = %d\n", toes); 15 printf("toes * 2 = %d\n", toes * 2); 16 printf("toes squared = %d\n", toes * toes); 17 18 19 getchar(); 20 return 0; 21 } 22 23 24 25 /********************************************************* 26 输出结果: 27 ---------------------------------------------------------- 28 toes = 10 29 toes * 2 = 20 30 toes squared = 100 31 ---------------------------------------------------------- 32 **********************************************************/
1 /********************************************************* 2 7. 许多研究表明,微笑益处多多。编写一个程序,生成以下格式的输出 3 4 Smile!Smile!Smile! 5 Smile!Smile! 6 Smile! 7 8 **********************************************************/ 9 10 #include <stdio.h> 11 12 void Smile(); 13 14 15 int main(void) 16 { 17 // Smile(); 18 // Smile(); 19 // Smile(); 20 // printf("\n"); 21 // 22 // Smile(); 23 // Smile(); 24 // printf("\n"); 25 // 26 // Smile(); 27 // printf("\n"); 28 29 ///*--------------------------------- 30 for(int i = 0; i<3; i++) 31 { 32 for(int j = 3; j>i; j--) 33 { 34 Smile(); 35 } 36 printf("\n"); 37 } 38 //-----------------------------------*/ 39 40 getchar(); 41 return 0; 42 } 43 44 void Smile() 45 { 46 printf("Smile!"); 47 } 48 49 /******************************************************** 50 输出结果: 51 ---------------------------------------------------------- 52 Smile!Smile!Smile! 53 Smile!Smile! 54 Smile! 55 ---------------------------------------------------------- 56 *********************************************************/
1 /****************************************** 2 8. 在 C 语言中,函数可以调用另一个函数。编写一 3 个程序,调用一个名为 one_three() 的函数。 4 该函数在一行打印单词“one”,再调用第 2 个 函数 5 two(),然后在另一行打印单词 “three”。two() 函数 6 在一行显示单词"two"。main()函数在调用 one_three() 7 函数前要打印短语"starting now:", 并在调用完毕后显示 8 短语"done!"因此,该程序的输出应如下所示: 9 10 starting now: 11 one 12 two 13 three 14 done! 15 16 ******************************************/ 17 18 19 #include <stdio.h> 20 21 void one_three(); 22 void two(); 23 24 25 int main(void) 26 { 27 printf("starting now:\n"); 28 one_three(); 29 printf("done!\n"); 30 31 getchar(); 32 return 0; 33 } 34 35 void one_three() 36 { 37 printf("one\n"); 38 two(); 39 printf("three\n"); 40 } 41 42 void two() 43 { 44 printf("two\n"); 45 } 46 47 /********************************************************* 48 输出结果: 49 --------------------------------------------------------- 50 starting now: 51 one 52 two 53 three 54 done! 55 --------------------------------------------------------- 56 **********************************************************/
C Primer Plus (第6版) 读书笔记_Chapter 2
原文:https://www.cnblogs.com/Micek/p/9153225.html