Here is a program
#include <stdio.h>
main()
{ unsigned char i=0x80;
printf("i=%d",i<<1);
}
The output it is giving is 256.
I am not clear with what does
unsigned char i=0x80; <-- i is not int it is char so what will it store?
I know bitshift and hexadecimal things.
How is the value of i being stored and how does it gets changed to 256?
UPDATE
Why did overflow not occurred when the bit shift operation happened?
In C, a
charis an integer type used to store character data, typically 1 byte.The value stored in
iis0x80a hexidecimal constant that is equal to128.An arithmetic operation on two integer types (such as
i << 1) will promote to the wider type, in this case toint, since1is an int constant. In any case, integer function arguments are promoted to int.Then you send the result to
printf, with a%dformat specifier, which mean “print an integer”.