C语言复习问题总结


1.字段结构的成员的地址

改错题:

1
2
3
4
struct _half_byte {
unsigned short hb0: 4, hb1: 4, hb2: 4, hb3: 4;
} m;
scanf("%hu %hu %hu %hu", &m.hb0, &m.hb1, &m.hb2, &m.hb3);

这里需要注意到字段结构的成员是没有地址的,因此scanf("%hu %hu %hu %hu", &m.hb0, &m.hb1, &m.hb2, &m.hb3);是错误的,正确的代码可以类似:

1
2
3
unsigned short tmp[4];
scanf(“%hu %hu %hu %hu”, &tmp[0] , &tmp[1] , &tmp[2] , &tmp[3]);
m.hb0 = tmp[0], m.hb1 = tmp[1] , m.hb2 = tmp[2] , m.hb3 = tmp[3];

2.&&和||的编译器优化

如果开启了编译器优化,那么若&&的第一个条件为假或者||的第一个条件为真,那么编译器将不会去看&&或||的第二个条件,因此:

Declare int x = 0, y = 1; , which values of the following expressions are nonzero?

A. x++ + y--

B. y-x||x++, x

C. x*y&&y--, y

D. y-- ? y++ : y

这道题目的B和C为例,B中y-x已经为1,所以编译器将忽略x++,所以x仍然为0,所以B错误。而C中x*y为0所以编译器将忽略y–,所以y为1,C正确。

3.注意标识符和关键字的区别

例如:main是一个合法的标识符,而非关键字。

再例如:Case是一个合法的标识符,而非关键字。(注意C语言区分大小写)

4.词法分析

How many tokens are there in the statement of *p+++=12.L;?

A. 5

B. 6

C. 7

D. 10

记号共有5类:标识符、关键字、常量(含字符串常量)、运算符、标点符号。

*p+++=12.L;可以被分解为 * p ++ += 12.L ;共计6个记号,所以,答案为B。

5.转义序列

根据文档:

Escape sequences are used to represent certain special characters within string literals and character constants.

在C中可以由 \(反斜杠)+ 字符来进行转义。

6.类型转换

long x, y; x = -6L; y = 5UL; Which expression is equal to 1?

A. x<y && -6L<5UL

B. x<y && -6L>5UL

C. x>y && -6L<5UL

D. x>y && -6L>5UL

x,y的类型为long,因此5UL将会被转换为5L,所以x<y。而在进行比较运算的时候-6L会被提升至Unsigned Long,此时-6L>5UL,所以答案选B。

7.后缀自增自减运算符的计算延迟

后缀式++/–在遇到&& || ?: , 运算符后、或者完整表达式结束后自增/自减。

8.注意作用域、链接和存储时期

作用域描述程序中可以访问一个标识符的一个或多个区域;链接包括外部、内部和空链接,具有空链接的变量被代码块或函数原型所私有,外部链接的变量可以在多文件程序的任何地方使用,内部链接的变量可以在一个文件的任何地方使用;静态存储时期的变量将在程序执行期间一直存在,并且只能初始化一次。

具体可以参考C Primer Plus第十二章《存储类、链接和内存管理》或是课本。以下列出5个存储类的组合(来自C Primer Plus):
















































存储类 时期 作用域 链接 声明方式
自动 自动 代码块 代码块内
寄存器 自动 代码块 代码块内,使用关键字register
具有外部链接的静态 静态 文件 外部 所有函数之外
具有内部链接的静态 静态 文件 内部 所有函数之外,使用关键字static
空链接的静态 静态 代码块 代码块内,使用关键字static

In the following descriptions about the using of static, which ones are correct?

A. The static declaration can be used to variables and functions.

B. The external static declaration limits the scope of that object.

C. Internal static variables are local to a particular function just as auto variables are.

D. Internal static variables provide permanent storage and are initialized only once.

A正确,B external static具有内部链接,因此被限制了只能被当前文件引用,正确,C内部定义的静态变量具有代码块作用域,正确,D正确。

同时在另外一个文件中引用具有外部链接的变量时需要使用关键字 extern,因此,下面一道题选D。

Suppose that file_a.c and file_b.c can be compiled independently. and they share the following global variables

extern int x; char ch;

which are declared in file_a.c. The allowed global variables declared in file_b.c are

A、extern int x; char ch;

B、extern int x; extern char ch;

C、int x; char ch;

D、int x; extern char ch;

9.改错题注意悬挂指针

例如:

1
2
3
4
char *a[5];
int i = 0;
for (i=0; i<5; i++)
scanf("%s", a[i]);

10.Switch中的break

若switch中的case不加break,将会将该case后的所有语句全部执行完。

11.字段成员的内存分配

字段成员的内存分配是由低位到高位的。例如下面的题目,答案为C:

Suppose declared:

1
2
3
4
5
6
7
struct direction {
unsigned short int east:4,south:4,west:4,north:4; //east为最低位4,north为最高位1
};
union ud{
unsigned short int all;
struct direction d;
} a={0x1234};

the output of printf("%d\n",a.d.south); is

A、1

B、2

C、3

D、4

12.细心审题

C语言考试审题还是很重要的,稍微一不小心看错了一个字母,有可能题目就错了,务必仔细审题。