■
昨日は寝てしまった。
まあ、週に一回くらいは10時間くらい寝たいところである。
■
今年になってから100回くらい「C言語むずい」って言ってる気がするな
以下のコードは、tcc, faucc, sdcc, bcc(Boarland ではないほう), clang で通らないが、Cとして正しいプログラム。
int *b = (int*)(struct xxyyzz { struct aabb { int a; } n; struct nenum { int x; } f; }*)0; int func(struct xxyyzz {int funarg; struct aabb {int funarg;}; } x) { struct xxyyzz xxyyzz; struct aabb z; int a; xxyyzz.funarg; { struct xxyyzz { int aa; struct aabb y; union aabb { int x; } n; union aabb z; }; union aabb a; a.x = 0; { int *a = (int*)(struct xxyyzz{union aabb{int n3;}f; struct nenum{int a;}ne;}*)0; union aabb z; z.n3 = 8; } } return z.funarg; } struct aabb x; int main() { struct aabb z; z.a = 0; return x.a; }
C の難しいところのひとつとして、式と宣言と型定義が結びついているというのがある。
C99 の sizeof なんかは、コンパイル時定数のように見えて、型定義も副作用も書けるという立派な複雑すぎる要素のひとつである。
#include <stdio.h> int main() { sizeof(struct a { int a;}); /* 型定義ができる */ struct a aa; sizeof(int [puts("a")]); /* 副作用が書ける */ sizeof(int [((struct b {int b;}*)0, puts("b"))]); /* もちろん両方書ける */ struct b bb; sizeof(sizeof(int [puts("c")])); /* これは副作用無い */ }
C言語カルトクイズ
CUDAも飽きたのでC言語にしよう。
以下、コンパイルが通る/通らないを答えなさい
Q1
struct xx { struct yy {int a;}; }; struct yy yy; int main(){}
Q2
struct xx { struct xx {int a;}; }; int main(){}
Q3
struct xx { int a; }; int main(){ struct xx {int b;}; }
Q4
struct xx { int a[sizeof(struct xx{int a;})]; }; int main(){}
Q5
struct xx { int (*a)(struct xx {int a;} x); }; int main(){}
Q6
struct xx { int global; }; int main(){ struct xx *x = (struct xx {int local;}*)0; struct xx *y; return x->global + y->local; }
Q7
struct xx { int global; }; int main(){ struct xx; struct xx *x = (struct xx {int local;}*)0; struct xx *y; return x->global + y->local; }
あれ…理解できない…これ…よく見たら、Q6ってGCCの結果おかしいのじゃないか…
Q8
struct xx { int global; }; int main(){ struct xx a; struct xx *x = (struct xx {int local;}*)0; struct xx *y; return x->global + y->local; }
宣言子無しと有りは型指定子の意味が変わってるのか…
Q9
struct xx { int global; }; int main(){ struct xx x = *(struct xx*)(struct xx {int local;}*)0, y = *(struct xx*)0; }
Q10
struct xx { int global; }; int main(){ struct yy { union xx { int local; }; }; struct xx xx; }