C
Programming and Technical
Programming
Program
On a machine where pointers are 4 bytes long, what happens when the following code is
executed.
main()
{
int x=0,*p=0;
x++; p++;
printf ("%d and %d\n",x,p);
}
a) 1 and 1 is printed
b) 1 and 4 is printed
c) 4 and 4 is printed
d) causes an exception
CHOOSE THE CORRECT OPTION
Read Solution (Total 10)
-
- Answer is B
x value is 1 because it is an integer variable having one increment and p is a integer pointer, integer having 4 bytes then one increment is equal to 4 bytes memory. so Answer is B - 10 years agoHelpfull: Yes(3) No(0)
- b is right
- 10 years agoHelpfull: Yes(2) No(0)
- but the %d used in the question wont cause any prob ???
i mean address are in bytes.. and also, you dont know the base address of this pointer, how can u say that it will give 4, according to me D is right... - 10 years agoHelpfull: Yes(2) No(0)
- it doesn't cause any exception i think it is (b) because when we increment a pointer it increased by 4 byte in 32 bit plateform. if initially it is 1200 then after p++ it would be 1204.
- 10 years agoHelpfull: Yes(1) No(0)
- causes an exception. Because p points to NULL, when increasing p it causes an exception.
- 10 years agoHelpfull: Yes(1) No(0)
- Ans :b
x++ => 1
p => gives the address of the 0 ,
p++ => adress increments ...pointers are 4 bytes long so . p++ gives 4 - 10 years agoHelpfull: Yes(1) No(0)
- B is Answer because P++ indicate next memory location which is 4.
- 10 years agoHelpfull: Yes(0) No(0)
- option b
b is right
- 10 years agoHelpfull: Yes(0) No(0)
- it will print..
1 and 4
with a warring message ::: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat] - 10 years agoHelpfull: Yes(0) No(0)
- as pointer is of 4 bytes so it will give.......1 and 4 as o/p
- 10 years agoHelpfull: Yes(0) No(0)
C Other Question