c语言高级面试题
整个测试遵循以下的约定:
??????? 假定在所有的程序中必须的头文件都已经被正确包含。
考虑如下的数据类型:
??????? char 为1个字节
??????? int 为4个字节
??????? long int 为4个字节
??????? float 为4个字节
??????? double 为个8字节
??????? long double 为 8个字节
??????? 指针为4个字节
?
?
1. Consider the following program:
#include
static jmp_buf buf;
?
main()
{
volatile int b;
b =3;
?
if(setjmp(buf)!=0)
{
printf("%d ", b);
exit(0);
}
b=5;
longjmp(buf , 1);
}
The output for this program is: (a) 3(b) 5(c) 0(d) None of the above
2. Consider the following program:
main()
{
struct node
{
int a;
int b;
int c;
};
struct node s= { 3, 5,6 };
struct node *pt = &s;
printf("%d" , *(int*)pt);
}
The output for this program is: (a) 3(b) 5(c) 6(d) 7
3. Consider the following code segment:
int foo ( int x , int n)
{
int val;
val =1;
if (n>0)
{
if (n%2 == 1) val = val *x;
val = val * foo(x*x , n/2);
}
return val;
}
What function of x and n is compute by this code segment?
(a) x^n(b) x*n(c) n^x(d) None of the above
4. Consider the following program:
main()
{
int a[5] = ;
int *ptr = (int*)(&a+1);
?
printf("%d %d" , *(a+1), *(ptr-1) );
?
}
The output for this program is: (a) 2 2(b) 2 1(c) 2 5(d) None of the above
5. Consider the following program:
void foo(int [][3] );
?
main()
{
int a [3][3]= { { 1,2,3} , { 4,5,6},};
foo(a);
printf("%d" , a[2][1]);
}
?
void foo( int b[][3])
{
++ b;
b[1][1] =9;
}
The output for this program is: (a) 8(b) 9(c) 7(d) None of the above
6. Consider the following program:
main()
{
int a, b,c, d;
a=3;
b=5;
c=a,b;
d=(a,b);
?
printf("c=%d" ,c);
printf("d=%d" ,d);
?
}
The output for this program is: (a) c=3 d=3(b) c=5 d=3(c) c=3 d=5(d) c=5 d=5
7. Consider the following program:
main()
{
int a[][3] = { 1,2,3 ,4,5,6};
int (*ptr)[3] =a;
?
printf("%d %d " ,(*ptr)[1], (*ptr)[2] );
?
++ptr;
printf("%d %d" ,(*ptr)[1], (*ptr)[2] );
}
The output for this program is: