top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

Pre and Post Increment in C/C++

+3 votes
126 views

Here is my code

#include<stdio.h>
void main()
{
int a=6;
printf ("%d %d %d %d",a,a++,a++,++a);
}

I believe pre-increment added one to the existing the value and use the new values where as post-increment would increase the existing value by 1 but still use the old value.

By using this logic output should be 6 6 7 9 but I am getting 9 8 7 9.

Can someone please explain?

posted Oct 28, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
I am also interested to know the explanation as mine is also coming 9879 for the above program.

2 Answers

+4 votes

Pre-increment operator:
(1) Increments the value of the variable by 1
(2) Returns the variable itself.
Post-increment operator:
(1) Saves the current value in a temp variable.
(2) Increments the value of the variable by 1
(3) Returns the value saved in the temp variable

printf ("%d %d %d %d",a,a++,a++,++a);
In the above printf() statement, execution happens from right to left.
"++a" is executed first, so value of "a" becomes 7 and returns "a"
"a++" is executed next, so value of "a" becomes 8 and returns "7"
"a++" is executed next, so value of "a" becomes 9 and returns "8"
latest value of "a" is 9
so after the above steps printf becomes printf("%d %d %d %d", a, 8, 7, a);
that's why output becomes 9 8 7 9

answer Oct 28, 2014 by Pradeep Kumar Nalla
Very nice explanation and deserves a recommendation. Was not knowing the concept of return and temp variable.
I have to admit it beside the of concept of sequence point and its undefined behaviour, your explanation is working exactly the say. I also don't know the concept of temp variable in case of post increment.

I still have doubt on temp variable as we follow the -Wall option says the undefined behaviour and sequence point.
It will be very helpful if have any source, link, textbook, which says the same.

any ways kudos (y).
Hi Pradeep,
I understand your explanation but I have query, When we look into the above code in perspective of sequence point concept post increment has to happen after sequence point i.e when it encounters ; or && or || or ',' operator. But here there is no such operator in printf statement but still it gets incremented. Could you please tell me the reason?
Plz refer to the below link, which is having similar explanation about the pre/post-increment operators:
http://alexkalicki.com/blog/programming/pre-vs-post-incrementation/
+1 vote

It is due the concept of sequence point, here is the wiki link on sequence point
Sequence point

There is no defined order of evaluation for the arguments of a function call, and the results might vary for each compiler.

If you compile your code using

 gcc program_name -Wall     // "-Wall" option to generate warning  

you will get warning telling that undefined behaviour on a like this :

 warning: operation on ‘a’ may be undefined [-Wsequence-point]
     printf ("%d %d %d %d\n",a , a++, a++, ++a);
                                       ^
answer Oct 28, 2014 by Arshad Khan
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...