当前位置: 代码迷 >> 综合 >> some basic interview question
  详细解决方案

some basic interview question

热度:40   发布时间:2023-12-08 09:18:25.0

(the header is omitted,sorry)
1.about the pointer and the ++ ,–

int main()
{int x = 4;int *p = &x;int *k = p++;int r = p - k;printf("%d", r);
}

>
explain:
C本身无法防止非法的指针减法运算,它无法为你提出任何警告或提示。
for more information
(http://blog.csdn.net/gdmmhym/article/details/6451554)
2.

int main()
{
unsigned int i = 23;
signed char c = -23;
if (i > c)
printf("Yes\n");
else if (i < c)
printf("No\n");
}

answer :No
for more information
(http://blog.csdn.net/ljianhui/article/details/10367703)
3.

int main()
{int x = -2;x = x >> 1;printf("%d\n", x);
}

answer:-1
it is simple,but i just want to write it here.
4.8. What is the output of this C code?

int main()
{int y = 1;if (y & (y = 2))printf("true %d\n", y);elseprintf("false %d\n", y);}

a) true 2
b) false 2
c) Either option a or option b
d) true 1
View Answer

Answer:c

5.
void main()
{
char a = ‘a’;
int x = (a % 10)++;
printf(“%d\n”, x);
}
answer:compile error
please note that :
[Error] lvalue required as increment operand
6.concentrate on the detail
example 1:
void main()
{
1 < 2 ? return 1: return 2;
}

answer:compile error
note that the ‘return 1’ is not a expression
example 2:

int main()
{
int x = 1, y = 0;
x &&= y;
printf("%d\n", x);
}

please note that ‘&&= ’ does not exist
+=、-=、*=、/=、%=、<<=、>>=、&=、^=、|=
here
example 3

int main()
{
int y = 1, x = 0;
int l = (y++, x++) ? y : x;
printf(“%d\n”, l);
}
a) 1
b) 2
c) Compile time error
d) Undefined behaviour
View Answer

Answer:a

7.some thing i want to figure out ,

int main()
{int x = 1;short int i = 2;float f = 3;if (sizeof((x == 2) ? f : i) == sizeof(float))printf("float\n");else if (sizeof((x == 2) ? f : i) == sizeof(short int))printf("short int\n");
}

  相关解决方案