考慮下列 C 語言程式的宣告 union allType { char character; int integer; float real; double doubleNum; }; 如果 sizeof(char) 是 1, sizeof(int) 是 4, sizeof(float) 是 4, sizeof(double) 是 8, sizeof(allType) 是多少?
A8正確答案
B16
C17
D20
答案與詳解
正解。union 所有成員共用同一塊記憶體空間,其大小為最大成員所佔的位元組數。本題中 double 最大為 8 bytes,故 sizeof(allType)=8。
