Check out this program
#include<stdio.h>
int main (){
char c='a';
printf("%d %d", sizeof(c),sizeof('a'));
}
the output is 1 4
I know when we write a statement char c=’a’;
then how does it happen that in space of 1 byte (char c) some thing of 4 bytes (ASCII code) is stored why is there no overflow etc.
First, per ANSI/IEC 9899:1999(E) §6.4.4.4:
§6.5.3.4:
As you can see, since the type of a character constant is
int, forsizeof('a')we getsizeof(int), which is 4 on your platform. However, forsizeof(c), we get the size of achar, which is defined to be 1.So why can we assign
'a'to achar?§6.5.16.1:
So, the
intthat is'a'is implicitly converted to achar. There’s an example in there as well, showing explicitly thatints can be implicitly converted tochar.