1

I was reading a C programming book, and I have just passed by a paragraph who states the following: "character constants, like 'F', are ints, not chars, which explains why sizeof 'F' is equal to sizeof (int)".

But I didn't believed this, so I decided to test for my self and the results where:

printf("%ld", sizeof('F')); //output 4;

But if we do:

char a = 'F';
printf("%ld", sizeof(a)); //output 1;

So my question is, what's going on under the hood? Why is 'F' handled by the compiler as an int, while it could help be handled as a char, which occupied less memory?

I have a guess, since 'F' is a constant and is typically stored in a register, it would be irrelevant to handle it has a char|int or long... Can somebody confirm my assumption or explain me why this happens?

João Pinho
  • 3,725
  • 1
  • 19
  • 29
  • 1
    The constant value `'F'` is type `int`. It's not seen as a type `char`. So `sizeof('F')` is the size of an `int`. If you did `sizeof((char)'F')` then you'd get `1`. – lurker Feb 07 '14 at 13:50
  • Thanks for answering my question, I'm clarified! Happy Coding. – João Pinho Feb 07 '14 at 13:54

0 Answers0