Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int is 4 bytes but still it can be stored in char why is there no overflow

Tags:

c

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.

like image 264
Registered User Avatar asked Jun 19 '11 05:06

Registered User


People also ask

Is int always 4 bytes in C?

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.

Why can ints and chars store each other?

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 .

Can char hold an integer?

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.


2 Answers

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 ints can be implicitly converted to char.

like image 131
bdonlan Avatar answered Sep 30 '22 07:09

bdonlan


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" (+/-).

like image 45
Mateen Ulhaq Avatar answered Sep 30 '22 08:09

Mateen Ulhaq