你可能學了個假c語言系列
阿新 • • 發佈:2018-12-28
前言
最近朋友給我發了些反人類的程式,有點慌,感覺自己學的是假的c語言,所以找了最近遇到的幾個c語言細節(坑爹)的地方分享給大家。答案知識點會在後面給上。
反猿猴題目==0x1
輸出結果是什麼??
typedef struct test
{
int t;
}test;
int main()
{
int buff[sizeof((test *)0)->t];
printf("%d", sizeof(buff));
system("pause");
}
反猿猴題目==0x2
int main()
{
int a = 10;
int *b = & (int&)a;
printf("%d", *b);
system("pause");
}
反猿猴題目==0x3
int main()
{
int a = 1, b = 2, c = 2, t;
while (a < b < c) { t = a; a = b; b = t; c--; }
printf("/n%d,%d,%d", a, b, c);
system("pause");
}
反猿猴題目==0x4
union MyUnion
{
char b;
char (*p)[10];
};
int main()
{
printf("%d\n" , sizeof(MyUnion));
system("pause");
}
0x1解答
看到這個,我想這玩意能編譯通過??
其實一直忽略了,sizeof也是一個c的運算子。而且它的操作不止sizeof(),所以按照運算子的優先順序,int buff[sizeof((test *)0)->t]相當於int buff[int];答案為16;
sizeof(object);//sizeof(物件);
sizeof(type_name);//sizeof(型別);
sizeof object;//sizeof物件;
ox2解答
(int&)a就是*(int *)a,繞來繞去花裡胡哨。答案10;
0x3解答
<運算子是從左往右進行比較比較結果是bool值,再和第三個比較。一共迴圈兩次,答案1,2,0;
0x4解答
聯合體的大小是成員中最大成員的大小,也就是指向一維陣列的指標,答案4;
最後
由於是熬夜作業,博主解答的比較簡單,自己多多體會,博主睡了,小命要緊。