I have the following program:
#include<stdio.h>
int main()
{
int i =257;
int *iptr =&i;
printf("%d%d",*((char*)iptr),*((char*)iptr+1));
return 0;
}
The output is:
1 1
I am not able to understand why the second value is 1.
Please explain.
Same reason the first value is coming out 1. You're accessing a single byte at a time from an int. Since 257 is 0x0101, the two least significant bytes each contain the value 1.
Probably your int is 4 bytes long and stored little-endian, although I suppose it could be 2 bytes long with either endian-ness.
Because 257 in binary is 00000001 00000001: So both the first and the second bytes representing it are set to 1.
(char*)iptr is the char (thus 1 byte) pointed by iptr, and (char*)iptr+1 is the next byte.
0x00,0x00,0x01,0x01 and you would get 0 0 as the output.