[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [LCP]query regarding pointers in C




#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@yahoo.com>
Sent by: linuxcprogramming-admin@lists.linux.org.au

15/04/2004 06:46 PM

Please respond to
linuxcprogramming

To
linuxcprogramming@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