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.
Since int in an integer type variable. So, the sizeof(int) simply implies the value of size of an integer. Whether it is a 32-bit Machine or 64-bit machine, sizeof(int) will always return a value 4 as the size of an integer.
C requires int be at least as many bits as char . Therefore, int can store the same values as char (allowing for signed/unsigned differences). In most cases, int is a lot larger than char .
A character can be a single letter, number, symbol, or whitespace. The char data type is an integral type, meaning the underlying value is stored as an integer.
First, per ANSI/IEC 9899:1999(E) §6.4.4.4:
10. An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. [...]
§6.5.3.4:
2. The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. [...]
3. When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. [...]
As you can see, since the type of a character constant is int
, for sizeof('a')
we get sizeof(int)
, which is 4 on your platform. However, for sizeof(c)
, we get the size of a char
, which is defined to be 1.
So why can we assign 'a'
to a char
?
§6.5.16.1:
2. In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.
So, the int
that is 'a'
is implicitly converted to a char
. There's an example in there as well, showing explicitly that int
s can be implicitly converted to char
.
The compiler implicitly converts the int
to char
.
int i = 42;
char c = i * 2 - 4;
That last line is interpreted by the compiler as:
char c = (char)(i * 2 - 4);
These implicit type conversions are handled by the compiler - there's no "buffer overflow". The (char)
is handled internally (by the machine itself, most likely, for simple types like int
). It appropriately cuts down on the extra bytes and preserves the "signedness" (+
/-
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With