9,The output for this program is: (a) i=4 j=2 (b) i=3 j=2 (c) i=3 j=4 (d) i=3 j=6
int main() {
int i = 3 ;
int j;
j = sizeof ( ++ i + ++ i);
int main() {
int i = 3 ;
int j;
j = sizeof ( ++ i + ++ i);
printf(
"
i=%d j=%d
"
, i ,j);
return 0 ;
}
return 0 ;
}
分析:因为sizeof是运算符,不是函数。我们很多时候都将sizeof当成了函数,这是不对的。sizeof只对数据类型进行判定而不是去计算表达式。所以i的值不会改变。关于sizeof我们可以看看MSDN上如何对sizeof进行定义的:
sizeof Operatorsizeof expression The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t. The expression is either an identifier or a type-cast expression (a type specifier enclosed in parentheses). When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment. When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.
答案 :(c) i=3 j=4
10,The output for this program is: (a) 5 5 5 5 (b) 3 5 3 5 (c) 5 3 5 3 (d) 3 3 3 3
void f1( int * , int );
void f2( int * , int );
void ( * p[ 2 ]) ( int * , int );
int main() {
int a;
int b;
p[ 0 ] = f1;
p[ 1 ] = f2;
a = 3 ;
b = 5 ;
p[ 0 ]( & a , b);
printf( " %d\t %d\t " , a ,b);
p[ 1 ]( & a , b);
printf( " %d\t %d\t " , a ,b);
return 0 ;
}
void f1( int * p , int q) {
int tmp;
tmp =* p;
* p = q;
q = tmp;
}
void f2( int * p , int q) {
int tmp;
tmp =* p;
* p = q;
q = tmp;
}
void f1( int * , int );
void f2( int * , int );
void ( * p[ 2 ]) ( int * , int );
int main() {
int a;
int b;
p[ 0 ] = f1;
p[ 1 ] = f2;
a = 3 ;
b = 5 ;
p[ 0 ]( & a , b);
printf( " %d\t %d\t " , a ,b);
p[ 1 ]( & a , b);
printf( " %d\t %d\t " , a ,b);
return 0 ;
}
void f1( int * p , int q) {
int tmp;
tmp =* p;
* p = q;
q = tmp;
}
void f2( int * p , int q) {
int tmp;
tmp =* p;
* p = q;
q = tmp;
}
分析:考察函数参数的传递方式,值传递和地址传递。值传递在函数调用后不改变实参,而地址传递在函数调用后改变实参。只要知道这一点,本题就很好解答。f1和f2中的两个参数第一个都是地址传递,第二个都是值传递。
答案: (a) 5 5 5 5
11,The output for this program is: (a) 0 1 2 0 (b) 0 1 2 1 (c) 1 2 0 1 (d) 0 2 1 1
void e( int );
void e( int );
int
main() {
int a;
a = 3 ;
e(a);
return 0 ;
}
void e( int n) {
if (n > 0 ) {
e( -- n);
printf( " %d " , n);
e( -- n);
}
}
int a;
a = 3 ;
e(a);
return 0 ;
}
void e( int n) {
if (n > 0 ) {
e( -- n);
printf( " %d " , n);
e( -- n);
}
}
分析:考察递归函数的调用,调用深度,调用后一级一级的返回。
e(3) e(2) e(1)
e(3): e(2): e(1):
e(2) e(1) e(0)
printf("%d" , n); //2 printf("%d" , n); //1 printf("%d" , n); //0
e(1) e(0) e(-1)
答案: (a) 0 1 2 0
答案: (a) 0 1 2 0
12,type of tmp is
(a) Pointer to function of having two arguments that is pointer to float
(b) int
(c) Pointer to function having two argument that is pointer to float and return int
(d) None of the above
typedef int ( * test) ( float * , float * )
test tmp;
(a) Pointer to function of having two arguments that is pointer to float
(b) int
(c) Pointer to function having two argument that is pointer to float and return int
(d) None of the above
typedef int ( * test) ( float * , float * )
test tmp;
分析:考察函数指针和typedef。函数指针的形式是:数据类型标志符 (指针变量名) (形参列表); 例如:int (*f) (int x); /* 声明一个函数指针 */
typedef为C语言的关键字,作用是为一种数据类型定义一个新名字。在编程中使用typedef目的一般有两个,一个是给变量一个易记且意义明确的新名字,另一个是简化一些比较复杂的类型声明。 typedef int (*test) ( float* , float*) 中test是一个函数指针,经过typedef定以后成为一个函数指针的数据类型。所以test tmp中temp是一个指向函数的指针,这个函数返回整型值
答案:(c) Pointer to function having two argument that is pointer to float and return int
13,The output for this program is: (a) 5 (b) 6 (c) 9 (d) None of the above
int main() {
char * p;
char buf[ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 9 , 8 };
p = & ((buf + 1 )[ 5 ]);
13,The output for this program is: (a) 5 (b) 6 (c) 9 (d) None of the above
int main() {
char * p;
char buf[ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 9 , 8 };
p = & ((buf + 1 )[ 5 ]);
printf(
"
%d
"
,
*
p);
return 0 ;
}
return 0 ;
}
分析:考察数组地址的变化。buf是数组的起始地址,也即第一个元素的地址&buf[0],buf+1就是数组的第二个元素的地址&buf[1],现在要以buf+1作为起始地址,(buf+1)[5] 相当于 buf+1+5 即buf[6]
答案:(c) 9
14,The output for this program is: (a) ab (b) cd (c) ef (d) gh
Void f( char ** );
int main() {
char * argv[] = { " ab " , " cd " , " ef " , " gh " , " ij " , " kl " };
f( argv );
return 0 ;
}
void f( char ** p ) {
char * t;
t = (p += sizeof ( int ))[ - 1 ];
printf( " %s " , t);
}
Void f( char ** );
int main() {
char * argv[] = { " ab " , " cd " , " ef " , " gh " , " ij " , " kl " };
f( argv );
return 0 ;
}
void f( char ** p ) {
char * t;
t = (p += sizeof ( int ))[ - 1 ];
printf( " %s " , t);
}
分析:数组中存放可以分高地址和低地址两种顺序存放。在调用函数f中 p+=sizeof(int) 相当于 p+=4 此时p指向第五个元素,再取p[-1]就相当于p-=1;此时指向了第四个元素。返回给t即 t =“gh”
答案:(d) gh
15,The output for this program is: (a) 7 (b) 6 (c) 5 (d) 3
#include
<
stdarg.h
>
int ripple ( int , ...);
int main(){
int num;
int ripple ( int , ...);
int main(){
int num;
num
=
ripple (
3
,
5
,
7
); printf(
"
%d
"
, num);
return 0 ;
}
int ripple ( int n, ...) {
int i , j;
int k;
va_list p; //参数列表
k = 0 ;
j = 1 ;
va_start( p , n); //获取第一个参数
for (; j < n; ++ j) {
i = va_arg( p , int ); //获取以一个参数
for (; i; i &= i - 1 )
++ k;
}
return k;
}
return 0 ;
}
int ripple ( int n, ...) {
int i , j;
int k;
va_list p; //参数列表
k = 0 ;
j = 1 ;
va_start( p , n); //获取第一个参数
for (; j < n; ++ j) {
i = va_arg( p , int ); //获取以一个参数
for (; i; i &= i - 1 )
++ k;
}
return k;
}
分析:考察不定参数函数的知识点。
答案:(c) 5
16, The value of j at the end of the execution of the this program is:
(a) 10 (b) 15 (c) 6 (d) 7
int counter ( int i) {
(a) 10 (b) 15 (c) 6 (d) 7
int counter ( int i) {
static
int
count
=
0
;
count = count + i;
return (count );
}
int main() {
int i , j;
for (i = 0 ; i <= 5 ; i ++ )
j = counter(i);
return 0 ;
}
count = count + i;
return (count );
}
int main() {
int i , j;
for (i = 0 ; i <= 5 ; i ++ )
j = counter(i);
return 0 ;
}
分析:主要考察静态变量static,静态变量的生存期是在整个程序结束的时候才结束。内存中的位置:静态存储区(静态存储区在整个程序运行期间都存在)。
答案:(b) 15
1706

被折叠的 条评论
为什么被折叠?



