第2章 C語言概述
復習題
1、如何稱呼C程序的基本模塊?
答:它們被稱為函數。
2、什么是語法錯誤?給出它的一個英語例子和C語言例子。
答:C的語法錯誤是指把正確的C符號放在了錯誤的位置。這是英語中的一個例子:"Me speak English good.";這是C語言的一個例子:
printf "Where are the parentheses?";
3、什么是語義錯誤?給出它的一個英語例子和C語言例子。
答:語義錯誤就是在意思上的錯誤。這是英語中的一個例子:"The sentence is excellent Italian.";這是C語言中的一個例子:
thrice_n = 3 + n;
4、Indiana Sloth 已經編好了下面的程序,并想征求你的意見。請幫助他評定。
1 include studio.h
2 int main{void} /*該程序可顯示出一年中有多少周/*
3 (
4 int s
5
6 s: = 56;
7 print(There are s weeks in a year.);
8 return 0;
答:第1行:以一個#開始,拼寫出文件名stdio.h,然后把文件名放在一對尖括號中。
第2行:使用(),而不是使用{};使用*/來結束注釋,而不是使用/*。
第3行:使用{,而不是(。
第4行:使用分號來結束語句。
第5行:Indiana使這一行(空白行)正確!
第6行:使用=,而不是使用:=進行賦值(顯然,Indiana 了解一些Pascal)。每年有52周而不是56周。
第7行:應該是
printf("There are %d weeks in a year.\n",s); 第9行:源程序沒有第9行,但是應該有,它應該包含一個結束花括號}。
在進行這些修改之后,代碼如下:
1 #include<stdio.h>
2 int main(void) /*該程序可顯示出一年中有多少周/*
3 {
4 int s;
5
6 s = 52;
7 printf("There are %d weeks in a year.\n",s);
8 return 0;
9 }
5、假設下面的每一個例子都是某個完整程序的一部分,它們每個將輸出什么結果?
a.printf("Baa Baa Black Sheep.");
printf("Have you any wool?\n");
b.printf("Begone!\n0 creature of lard!");
c.printf("What?\nNo/nBonzo?\n");
d.int num;
num = 2;
printf("%d + %d = %d",num,num,num + num);
答:a.Baa Baa Black Sheep.Have you any wool?(注意:在句號之后沒有空格。)
b.Begone!
0 creature of lard!(注意光標仍留在第2行結束處。)
c.What?
No/nBonzo?(注意斜線符號”/“沒有反斜線符號”\“所具有的作用,它只是簡單地作為斜線符號被打印出來。)
d.2 + 2 = 4
6、下面哪幾個是C的關鍵字?main,int,function,char,=
答:int,char(注意
main只是一個函數名,函數是C中的一個技術術語。=是一個運算符)
7、如何以下面的格式輸出words和lines的值:"There were 3020 words and 350 lines"?這里,3020和350代表兩個變量的值。
答:
1 #include <stdio.h>
2 int main(void)
3 {
4 int num1,num2;
5 num1 = 3020;
6 num2 = 350;
7
8 printf("There were %d words and %d lines\n",num1,num2);
9 return 0;
10 }
8、考慮下面的程序:
1 #include <stdio.h>
2 int main(void)
3 {
4 int a, b;
5
6 a = 5;
7 b = 2; /*第7行*/
8 b = a; /*第8行*/
9 a = b; /*第9行*/
10 printf("%d %d\n", b, a);
11 return 0;
12 }
請問在第7行、第8行和第9行之后程序的狀態分別是什么?
答:第7行之后,a為5,b為2。第8行之后,a為5,b為5。第9行之后,a為5,b為5。
編程練習(新手初學,僅供參考!!!)1、
1 #include <stdio.h>
2 int main(void)
3 {
4 printf("Li Ayun\n");
5 printf("Li\nAyun\n");
6 printf("Li ");
7 printf("Ayun\n");
8 return 0;
9 }
2、
1 #include <stdio.h>
2 int main(void)
3 {
4 printf("My name is Li Ayun!\n");
5 printf("I live in BeiJing now!\n");
6 return 0;
7 }
3、
1 #include <stdio.h>
2 int main(void)
3 {
4 int age;
5 age = 23;
6 printf("age = %d, days = %d\n", age, age * 365);
7 return 0;
8 }
4、
1 #include <stdio.h>
2 void show(void);
3 void end(void);
4 int main(void)
5 {
6 show();
7 show();
8 show();
9 end();
10 return 0;
11 }
12 void show(void)
13 {
14 printf("For he's a jolly good fellow!\n");
15 }
16 void end(void)
17 {
18 printf("Which nobody can deny!\n");
19 }
5、
1 #include <stdio.h>
2 int main(void)
3 {
4 int toes;
5 toes = 10;
6
7 printf("toes = %d\ntoes + toes = %d\ntoes * toes = %d", toes, toes + toes, toes * toes);
8 return 0;
9 }
6、
1 #include <stdio.h>
2 void showSmile(void);
3 int main(void)
4 {
5 showSmile();
6 showSmile();
7 showSmile();
8 printf("\n");
9 showSmile();
10 showSmile();
11 printf("\n");
12 showSmile();
13 return 0;
14 }
15 void showSmile(void)
16 {
17 printf("Smile!");
18 }
7、
1 #include <stdio.h>
2 void one_three(void);
3 void two(void);
4 int main(void)
5 {
6 printf("starting now:\n");
7 one_three();
8 two();
9 printf("done!");
10 return 0;
11 }
12 void one_three(void)
13 {
14 printf("one\n");
15 }
16 void two(void)
17 {
18 printf("two\nthree\n");
19 }