Why does sizeof ('3') == 4 use the GCC compiler?

Why is the conclusion for the next program 4?

#include <stdio.h>

int main()
{
    printf("%d\n", sizeof('3'));
    return 0;
}
+4
source share
2 answers

Because the type of character constant int, not char(and the size inton your platform is four).

The draft C99 says:

An integer character constant is of type int.

This may seem strange, but remember that you can do this:

const uint32_t png_IHDR = 'IHDR';

In other words, one character constant can consist of more than one actual character (four, above). This means that the type cannot have the resulting value char, because then it will immediately overflow and be meaningless.

: , , -, , .:)

+10

int.

C . "3" - int.

sizeof(character_constant)==sizeof(int)==> In your case sizeof(int)==4

++ char

, C ++.

memset(&i, 'a', sizeof('a'));   // Questionable code 
+5

All Articles