0
//a.c
#include<stdio.h>
int main()
{
    printf("%d \n",sizeof'A');   //the result is 4
    return 0;
}

why the result of sizeof'A' is 4 in c? I run it in windows 7 X64,vs2010.

//a.cpp
#include<stdio.h>
int main()
{
    printf("%d \n",sizeof'A');   //the result is 1
    return 0;
}

in a .cpp file the rusult is 1.why?

  • `'A'` is not a type, its a number. The number 65 to be precise. I suppose it is of type `char`, but as you saw yourself this interpretion may depend from a compiler to another. – Havenard Nov 21 '13 at 01:54
  • 1
    @Havenard: `'A'` is a character constant. Its value is 65 in ASCII, but C doesn't specify the ASCII character set; it will be different on an IBM mainframe that uses EBCDIC. The type of a C character constant is always `int`. In C++, it's of type `char`. This depends on the language, not on the compiler. – Keith Thompson Nov 21 '13 at 01:55
  • See this link inside stackoverflow itself. [why sizeof('a') is 4 in C?][1] [1]: http://stackoverflow.com/questions/8654837/why-sizeofa-is-4-in-c?rq=1 –  Nov 21 '13 at 01:58

0 Answers0