#include<stdio.h>
int main()
{
char ch = 'A';
printf("%d\n",'ag');
printf("%d\n",'a');
printf("%d, %d, %d, %d", sizeof(ch), sizeof('a'), sizeof('Ag'), sizeof(3.14f));
return 0;
}
I used to have many doubts on the output of this question while running on g++ and gcc.
But I have cleared almost all the doubts by referring these links:
I still need to understand one thing about the output of this question.
Can someone please explain the output of printf("%d\n",'ag'); mentioned above in the program. How is it actually stored in the memory?
The output for the program on the Linux/GCC platform is:
24935
97
1, 4, 4, 4
The type of a single-quoted literal is
int. So the size is typically large enough for more than one character’s worth of bits. The exact way the characters are interpreted is, as far as I know, implementation-dependent.In your case, you’re getting a little-endian ordering:
'a'is 97 (0x61)'g'is 103 (0x67)Your value is 24935 = 0x6167, so you’re getting the
'a'in the higher byte and the'g'in the lower.