2011-12-28 2 views
9

가능한 중복 : 여기
Why are C character literals ints instead of chars?sizeof ('a')가 C에서 4 인 이유는 무엇입니까?

#include<stdio.h> 
int main(void) 
{ 
    char b = 'c'; 
    printf("here size is %zu\n",sizeof('a')); 
    printf("here size is %zu",sizeof(b)); 
} 

가 출력 (라이브 데모 here를 참조하십시오.)되어

here size is 4 
here size is 1 

sizeof('a')이 4 왜 점점 아니에요?

+0

페이지에 해당 [대신 문자의 C 문자 리터럴의 int를 왜?] (http://stackoverflow.com/questions/433895/why-are-c- 문자 - 리터럴 - 문자 대신 - ints) –

답변

10

'a'와 같은 C 문자 상수는 int입니다. 그래서 ('A'는)는 sizeof (int)를 (이것은 또 다른 영역은 비록 를 sizeof이며,

는 놀랍게도, C에서 문자 상수는 int 형의 위치 :

이 suject에 대한 C FAQ있다 여기서 C++ 은 다릅니다.

3

'a'는 기본적으로 정수이며, 이로 인해 시스템의 정수가 4 바이트가됩니다.

char은 1 바이트이며, 이로 인해 1 바이트를 얻습니다.

10

다음은 유명한 C 책의 유명한 줄 - The C programming Language에 의해 Kernighan & Ritchie 작은 따옴표 사이에 쓰여진 문자에 관한 것입니다.

A character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set.

그래서 sizeof('a')sizeof(int)