C Primer Plus Fifth Edition 笔记chapter2
Summary
A C program consists of one or more C functions. Every C program must contain a function called main() because it is the function called when the program starts up. A simple function consists of a header followed by an opening brace, followed by the statements constituting the function body, followed by a terminating, or closing, brace.
Each C statement is an instruction to the computer and is marked by a terminating semicolon. A declaration statement creates a name for a variable and identifies the type of data to be stored in the variable. The name of a variable is an example of an identifier. An assignment statement assigns a value to a variable or, more generally, to a storage area. A function call statement causes the named function to be executed. When the called function is done, the program returns to the next statement after the function call.
The printf() function can be used to print phrases and the values of variables.
The syntax of a language is the set of rules that governs the way in which valid statements in that language are put together. The semantics of a statement is its meaning. The compiler helps you detect syntax errors, but semantic errors show up in a program's behavior only after it is compiled. Detecting semantic errors may involve tracing the program state—that is, the values of all variables—after each program step.
Finally, keywords are the vocabulary of the C language.
Review Questions:
1. What are the basic modules of a C program called?
我的答案:函数
标准答案:They are called functions.
2.What is a syntax error? Give an example of one in English and one in C.
我的答案:syntax error就是违反了C标准所犯的错误。比如漏掉分号
标准答案:A syntax error is a violation of the rules governing how sentences or programs are put together. Here's an example in English: "Me speak English good." Here's an example in C:
printf"Where are the parentheses?";.
3. What is a semantic error? Give an example of one in English and one in C.
我的答案:semantic error就是符合C标准,但是和实际想达到的结果不一致。
标准答案:A semantic error is one of meaning. Here's an example in English: "This sentence is excellent Italian." Here's a C example:
thrice_n = 3 + n;
4. Indiana Sloth has prepared the following program and brought it to you for approval. Please help him out.
include studio.h
int main{void} /* this prints the number of weeks in a year /*
(
int s
s := 56;
print(There are s weeks in a year.);
return 0;
我的答案:
#include <stdio.h>
int main(void) /* this prints the number of weeks in a year */
{
int s;
s = 56;
printf("There are %d weeks in a year.",s);
getch();
return 0;
}
标准答案:
#include <stdio.h>
int main(void) /* this prints the number of weeks in a year */
{
int s;
s = 52;
printf("There are %d weeks in a year./n", s);
return 0;
}
5. Assuming that each of the following examples is part of a complete program, what will each one print?
a. printf("Baa Baa Black Sheep.");
printf("Have you any wool?/n");
b. printf("Begone!/nO creature of lard!");
c.printf("What?/nNo/nBonzo?/n");
d.int num;
num = 2;
printf("%d + %d = %d", num, num, num + num);
我的答案:
标准答案:
6. Which of the following are C keywords? main, int, function, char, =
我的答案:int char
标准答案:
7. How would you print the values of words and lines in the form There were 3020 words and 350 lines.? Here, 3020 and 350 represent values for the two variables.
我的答案:printf("There were %d words and %d lines./n", words, lines);
标准答案:
8. Consider the following program:
#include <stdio.h>
int main(void)
{
int a, b;
a = 5;
b = 2; /* line 7 */
b = a; /* line 8 */
a = b; /* line 9 */
printf("%d %d/n", b, a);
return 0;
}
What is the program state after line 7? Line 8? Line 9?
我的答案:5 5