Miscellaneous Company Exam
Company
Programming
int a;
printf("%un", (int)(&a+1)-(int)(&a));
it prints sizeof(a)=4;
but i can't understand ,pls can any one explain this.
Read Solution (Total 4)
-
- sizeof(a) ll be 2 only...
- 10 years agoHelpfull: Yes(12) No(0)
Assume the address assigned to "a" is 2000... So it l take 2 bytes as its an "integer" variable... So "a" ll hold 2000 nd 2001...
printf("%un", (int)(&a+1)-(int)(&a));
The above line ll print "2"... Since &a=2000... When u increment an address variable, it ll increment as many bytes as its holding... So it ll increment 2 bytes [ ie 2002 nd thats is converted to int (int)] ... Next (int) (&a) = 2000
So 2002 - 2000 = 2...
Hence its printing 2...
If u print sizeof(a), again it ll print 2 only... Bcozzz sizeof integer variable is always "2" only...The increment of address never affects the sizeof the variable...
- 10 years agoHelpfull: Yes(2) No(0)
- explain which is main thing .......
- 10 years agoHelpfull: Yes(0) No(0)
- this output depends upon compiler in ANSI C it will print 4 .
- 10 years agoHelpfull: Yes(0) No(0)
Miscellaneous Company Exam Other Question