[LCP]query regarding pointers in C

Murali Vaddagiri murali.vad at in.ibm.com
Thu Apr 15 21:40:01 UTC 2004


#include<stdio.h> 
    main()
   {
     int a=555,*ptr=&a,b=*ptr;
     printf("%d %d %d",++a,--b,(*ptr)++);
}
Answer:
for printf, arguments are evaluated "right to left"

so, (*ptr)++ increments the value of a by 1. the current value of a will 
be 556, but (*ptr) still outputs as 555
--b = 554 , as expected
++a = preincrementing a = hence a will be 556 + 1 =557
hence the output!!

--------------------------------------
#include<stdio.h> 
    main()
   {
     int a=555,*ptr=&a,b=*ptr;
     printf("%d %d %d",++a,--b,*ptr++);
}

Answer:
*ptr++ = postincrementing the pointer ptr, hence the output will be 555
--b = 554 as expected.
++a= 556 , as expected!!

Thanks,
Murali.




"sowmya.k" <sowmi99 at yahoo.com> 
Sent by: linuxcprogramming-admin at lists.linux.org.au
15/04/2004 06:46 PM
Please respond to
linuxcprogramming


To
linuxcprogramming at lists.linux.org.au
cc

Subject
[LCP]query regarding pointers in C






pls tell me how these 2 pgms work
 
1. #include<stdio.h> 
    main()
   {
     int a=555,*ptr=&a,b=*ptr;
     printf("%d %d %d",++a,--b,(*ptr)++);
}
 
for this the output is 557 554 555
 
2. #include<stdio.h> 
    main()
   {
     int a=555,*ptr=&a,b=*ptr;
     printf("%d %d %d",++a,--b,*ptr++);
}
 
for this i got the output as 556 554 555
Could any one pls explain me the reson behind this output
Thanking you,
Sowmya
Do you Yahoo!?
Yahoo! Tax Center - File online by April 15th
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.linux.org.au/pipermail/linuxcprogramming/attachments/20040415/8b37f10e/attachment.htm 


More information about the linuxCprogramming mailing list