in Programming in C
607 views
2 votes
2 votes
main() {

int i = 511;

char *ptr = (char *)&i;

printf("%d", *ptr); }
in Programming in C
607 views

1 Answer

1 vote
1 vote

i=

511
address 1000

 511 = 00000001 11111111

11111111 00000001
Lower byte (1000-1001) upper byte (1002-1003)

ptr =1000 {char pointer}

11111111 which is in 2's complement o/p: -1

edited by

4 Comments

@srestha

{ char *ptr = (char *)&i; } type casting is done here

0
0

we can also anlyse it other way ...just think char can range from (-128 to 127)..if there are any value like 

char c=128..then (printf("%d",c);  it will print -128....bocz  ...it will behave like circular queue...(-128,-127,....-1,0,1....,127)again (-128...)...

..solution ((511-128)-256)-128) no we would be at ... -1

0
0
@hs_yadav

solution ((511-128)-256)-128) no we would be at ... -1

Why you are subtracting with 256 and again 128... Although I understood why you are subtracting first time with 128.

Please suggest your approach
0
0

Related questions

0 votes
0 votes
0 answers
3