精選 12 道高頻 C 語言面試題,涵蓋核心知識點,附詳細解析與陷阱提示。
int a[5]={1,2,3,4,5}; int *p=a; printf("%d", *(p+3)-*p); 輸出?struct {char c; int i;} s; sizeof(s)=? 32/64 位差異?f(n){if(n>0) f(n-1);} 最大深度受何限制?struct {int a:3; int b:4;}; 記憶體佈局與跨平台風險?volatile?編譯器優化行為如何變化?fork() 返回值在父/子進程?vfork() 差異?malloc/free 配對原則?i = i++ + ++i; 結果?為何 UB?const int *p vs int *const p vs const int *const p 區別?struct {int len; char data[];} 用途與 malloc 計算?p+3 指向 a[3]=4,*p=1,4-1=3。char 1B + 3B padding + int 4B = 8B。結構體對齊到最大成員 (4)。fork():父進程返回子 PID,子進程返回 0。vfork():子進程共享父進程位址空間,子進程先執行,調用 exec 或 _exit 前父進程阻塞。free(NULL) 安全,重複 free 崩潰。const int *p:指向常量整數 (值不可變);int *const p:常量指標 (指標不可變);const int *const p:雙重常量。int (*(*fp)(int))(void); 或 typedef int (*FuncPtr)(void); FuncPtr (*fp)(int);。union {int i; char c[4];} u = {.i=0x01020304}; return u.c[0]==1 ? "little" : "big";。malloc(sizeof(Struct) + len) 一次分配,提高局部性、減少碎片。volatile/restrict 的影響。__builtin_*、static_assert、C11 _Atomic 現代特性。