search
Log In
2 votes
387 views
int main() {
    int i,n;
    int x = 123456789;
    void *a = &x;
    unsigned char *c1 = (unsigned char*)a;
    for(i=0;i< sizeof a;i++) {
        printf("%u  ",*(c1+i));
    }
    char *c2 = (char*)a;
    printf("\n %d", *(c2+1));
    char *c3 = (char*)a;
    printf("\n %u \n", *(c3+1));
}

Output ?

in Programming 387 views

1 Answer

2 votes
 
Best answer
 int x = 123456789;

See how data will be stored ,

00010101 11001101 01011011 00000111

a is pointing to address of int x.

unsigned char *c1 = (unsigned char*)a;

Now char c1 is pointing to the location of a.

Next statement is printing consequtive 4 bytes which are unsigned in nature.

So value will be:21   205   91    7 (See from above table)

 char *c2 = (char*)a;

Char c2 contains address of a which is signed .Now it is printing second byte value at pointed by a.

And correspoinding value will be -51(which will fall inside signed range ).

 char *c3 = (char*)a;

Char c3 contains address of a which is also signed .Now it is also printing second byte value pointed by a. But it is signed .

PS: *(c3+1) is promoted inside printf to a 32 bit integer , preserving sign. ( padding ones because, initialy *c3 was a signed char having 1 in the sign bit, and it is itself in 2's complement form). After that we read using %u specifier which simply tells how to interpret the promoted data. %u nothing to do with promotion.
So, in the last printf %u reads the promoted data 11111111111111111111111111001101 as 4294967245.

Out:4294967245


selected by
1

Thanks !!. I would like to add: *(c3+1) is promoted inside printf to a 32 bit integer , preserving sign. ( padding ones because, initialy *c3 was a signed char having 1 in the sign bit, and it is itself in 2's complement form). After that we read using %u specifier which simply tells how to interpret the promoted data. %u nothing to do with promotion. So, in the last printf %u reads the promoted data 11111111111111111111111111001101 as 4294967245.

0
thank you
0
@Manoj how data are storing ? I am not getting

int is 4 B rt?

then 1=0001

and 2=0010 rt?

how r u getting 0101?
2
$(123456789)_{10} = 00000111 \ \ 01011011 \ \ 11001101 \ \ 00010101$
They stored as in little endian system. Lower byte in lower address.
1
ok got it :)

Related questions

0 votes
1 answer
1
331 views
main() { unsigned int i= 255; char *p= &i; int j= *p; printf("%d\n", j); unsigned int k= *p; printf("%d", k); } Both the outputs are -1. I have even tried with - int i = 255(3rd line). Still the output is -1. I don't understand how it is -1.
asked Jan 12, 2019 in Programming Psnjit 331 views
0 votes
1 answer
2
337 views
#include<stdio.h> void fun(int *p,int *q) { p=q; *p=q; } int i=0,j=1; int main() { fun(&i,&j); printf("%d%d",i,j); } What will be the output of i and j in 16-bit C Compiler?
asked Nov 3, 2018 in Programming Mr khan 3 337 views
0 votes
1 answer
3
204 views
What is the interpretation of following C declaration: char *(*(**foo[][8])())[]; (A) foo is array of 8 pointer to pointer to function returning pointer to array of pointer to char. (B) foo is array of array of 8 pointer to pointer to function returning pointer to ... of char. (C) foo is array of array of 8 pointer to pointer to function returning pointer to array of pointer to char. (D) None
asked Sep 30, 2018 in Programming sushmita 204 views
0 votes
0 answers
4
279 views
Please explain the output for the following program: #include<stdio.h> int main() { int i = 100; int *a = &i; float *f = (float *)a; (*f)++; printf("%d", *a); //getting some garbage value }
asked Sep 2, 2018 in Programming garvit_vijai 279 views
...