2017-09-25 88 views
1

我有以下代碼:C爲什麼sizeof('a')= 4且sizeof(char)= 1?

#include <stdio.h> 
#include <stdlib.h> 

int main(int argc, char * argv[]){ 
    printf("size of tab = %d\n", sizeof('\t')); 
    printf("size of a = %d\n", sizeof('a')); 
    printf("size of char = %d\n", sizeof(char)); 
} 

輸出:

size of tab = 4 
size of a = 4 
size of char = 1 

爲什麼是'a'的大小和char不同的尺寸。是不是'a' a char

+1

'的sizeof( '\ T')'是實際計算的大小的'int'其中'int'是' '\ t' 內的ASCII值''sizeof('a')的情況類似' –

+0

'''是'int'。 C中的所有常量至少是'int'。 – chux

+0

6.4.4.4字符常量p2 _An整數字符常量是一個或多個多字節字符的序列,用單引號括起來,如'x'._和p10中所示_An整數字符常量的類型爲int._ 6.5.3.4 sizeof ...運算符p4 _當sizeof應用於具有char,unsigned char或 signed char的操作數(或其合格版本)時,結果爲1._ – BLUEPIXY

回答

1

'a是包含字符a的int。 看看這些表達式的值:

sizeof((char) 'a'); 
char a = 'a'; 
sizeof(a); 
相關問題