Here is one program
#include<stdio.h>
#include<stdlib.h>
int main()
{
unsigned char a=0x80;
printf("%d\n",a<<1);
}
The output of above is 256
Now here is one more version of above program
#include<stdio.h>
#include<stdlib.h>
int main()
{
unsigned char a=0x80;
a=a<<1;
printf("%d\n",a);
}
The output of above is
0
As far as my understanding is I am not able to see any difference between the two?
i.e. why is output coming 256 in first one and 0 in second program what is the difference in statements in both?
The expression
a << 1is of typeintaccording to the C language’s type-promotion rules. In the first program, you are taking thisint, which now has the value0x100, and passing it directly toprintf(), which works as expected.In the second program, your
intis assigned to anunsigned char, which results in truncation of0x100to0x00.