in Programming in C
498 views
4 votes
4 votes

Answer given is D "gh" . how??

in Programming in C
by
498 views

4 Comments

@atul_21, I will be sticking to my solution until someone gives better explanation.
0
0
@harish thanks for pointing out . now corrected.

formerly i assumed default value of int
0
0
If we take size of int as 2 then answer is cd

If we take size of int as 4 the answer is gh
0
0

1 Answer

2 votes
2 votes
Best answer

old compilers the size of integer is 2bytes and new compilers the size of integer is 4B.

 If nothing is mention in the question about size of a integer then consider size of int is 4B.if they mention we have to consider that value.

Here size of int is 2B.

char *t;

t=(p+=sizeof(int))[-1] 

t=(p+2)[-1]   // p+2 it skips 2 positions from start.and p is pointing to array position 2.

p[-1] again it goes back then p points to p[1].

print all characters in that string and output is cd.

So option should be B.

Suppose if we execute this programme on new compilers it gives 'gh' as outpot.

selected by

Related questions