Thread: Help with Pre and Post Increment !

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    51

    Help with Pre and Post Increment !

    Hey,
    I came across a seemingly simple Pre and Post increment question in C. bu when i tried i was stunned to see the output.
    Code:
    #include<stdio.h>
    void main()
    {
    int I=6;
    printf ("%d %d %d %d",I,I++,I++,++I);
    }
    As far as i knew, I believed the the pre-increment added one to the existing the value and use the new values.

    But post-increment would increase the existing value by 1 but still use the old value.

    By using this logic, i thought that the output of the code snipped would be 6 6 7 9

    But when i ran the code i got the output as 9 8 7 9. Can someone explain this behaviour?

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by thebenman View Post
    I came across a seemingly simple Pre and Post increment question in C.
    Where did you see that example? It's probably only useful as an example of undefined behaviour. Basically, C does not guarantee what order all of your arguments will be executed. See this thread for more information: c - Output of multiple post and pre increments in one statement - Stack Overflow

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    10
    hope this programm will clear your problems
    Code:
    /* Function Calling Conventions */
    main ()
    {
         int b = 1;
    /* Calling convention indicates the order or the way the value is been called
    in another function. Here b is been called by function fun.*/
         fun (b);
         }
    fun (int a)
    {
         printf ("%d %d %d", a, ++a, a++);
        }
    /* What will be the output of the programme ? no its not 1 2 3 its 3 3 1, why?
    Note that the calling convention of C language is from right to left. Thus first
    'a' is passed with value 1, and then it gets incremented to 2 via the a++
    condtion, and then the result of ++a passed that is 3. Now the latest value of a
    is 3 thus it get passed. Result of all is 1 3 3 and printf collects it and
    printf prints the right to left that is 3 3 1.
    Note that knowing calling convention is important for programmer to trace how
    to call the arguments.*/
    Last edited by raviwagh; 10-27-2014 at 11:14 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    raviwagh's answer is wrong in all important respects.

    The properties of pre and post increment have NOTHING at all to do with calling conventions.

    Post and Pre Increment
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    hope this programm will clear your problems
    Why will your code clear any problems. It looks like you also need to read the link provided in post 2. Your program also exhibits undefined behavior. Just because your compiler produced output that seems reasonable to you doesn't solve the problem of undefined behavior.

    Jim

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    10
    Yes i agree my programm may does not solve his problem but my anwser is sufficient for how the output is come in such manner. And for the post and pre increment operators i want to add that
    1. when post increment operator is used the variable incremented after value is used like if n = 5
    Code:
    n = 5;
    x = n++;
    set x to 5
    2. while when pre increment operator is used the variable incremented before value is used where
    Code:
     x = ++n;
    will set x to 6
    Last edited by raviwagh; 10-28-2014 at 11:13 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by raviwagh
    Yes i agree my programm does may does not solve his answers but my anser is sufficient for how the output is in such manner.
    Your answer is simply wrong: "Note that the calling convention of C language is from right to left." That statement is not true since the C standard leaves the order of evaluation of function arguments unspecified. Of course, you could have stated that right to left evaluation could have produced that result, but that would be misleading unless you also clarified that not only is this something that cannot be relied upon, but in fact the behaviour is undefined because the same variable, I, is modified more than once between consecutive sequence points.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2014
    Posts
    10
    oh sorry for the description in above answer. the programm above is from my c learning test folder and it is un-linked here because that program originally relate to the C language function arguments calling conventions. Thus by saying "Calling convention of C language" i mean "arguments calling conventions of C language".

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by raviwagh
    the programm above is from my c learning test folder and it is un-linked here because that program originally relate to the C language function arguments calling conventions. Thus by saying "Calling convention of C language" i mean "arguments calling conventions of C language".
    In that case, you should be aware that whoever prepared your learning material was wrong on that point, or perhaps meant it as an example rather than as a rule.

    EDIT:
    Wait, no, your program from post #3? That has undefined behaviour for the same reasons as has been described. If your learning material failed to explain the undefined behaviour when presenting that program, then your learning material is defective.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    10
    Quote Originally Posted by laserlight View Post
    In that case, you should be aware that whoever prepared your learning material was wrong on that point, or perhaps meant it as an example rather than as a rule.

    EDIT:
    Wait, no, your program from post #3? That has undefined behaviour for the same reasons as has been described. If your learning material failed to explain the undefined behaviour when presenting that program, then your learning material is defective.
    i think then Dennis ritchie would be very unhappy

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by raviwagh
    i think then Denniss ritchie would be very unhappy
    The name dropping makes no difference: defective learning material is defective. Of course, it could be that the undefined behaviour is explained, but you failed to read it properly, or that the material is sufficiently old such that what it states no longer applies, in which case it is defective other than as a historical artifact.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2014
    Posts
    10
    Quote Originally Posted by laserlight View Post
    The name dropping makes no difference: defective learning material is defective. Of course, it could be that the undefined behaviour is explained, but you failed to read it properly, or that the material is sufficiently old such that what it states no longer applies, in which case it is defective other than as a historical artifact.
    there is no undefined behavior, so let me prove my point.
    Thus c arguments have right from left convention following programm
    Code:
    main()
    {
    int i = 5;
    printf ("%d %d %d %d %d", i, i++, i++, i++, i++);
    }
    will give output as 9 8 7 6 5 so here my points prove that the arguments convention is from right to left
    now the pre increment behaviour
    if we add a pre-increment operator to variable it first increments the variable value whether the variable is used or not so like as following programm
    Code:
    main()
    {
    int i = 5;
    printf ("%d %d %d %d %d", i, ++i, ++i, ++i, ++i);
    }
    will give output as 9 9 9 9 9

    so as in the thebenman's programm the first the variable value is passed to the printf function which is 6, but pre-increment operator increment variable value and by doing that the value is get incremented to 9 because there is two more post operators are in the processing so pre-increment operator changes the value of variable i to its final increment. Thus i become 9 in first, now post increments prints their values which is get incremented after the value is been used the first post increment operator increments the value of i is to 7, while second will increment to 8, and the last is the preincrement it self has value which is needed to be print so last the value is passed is 9 thus we got results
    9 7 8 9 and thus c arguments passing convention is from right to left the output is get printed 9 8 7 9.
    Try above programm's and i bet they will give same output in any compiler.
    Last edited by raviwagh; 10-28-2014 at 08:58 PM.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    there is no undefined behavior, so let me prove my point.
    It appears that my compiler disagrees with you:

    main.c||In function ‘main’:|
    main.c|6|warning: operation on ‘i’ may be undefined [-Wsequence-point]|
    main.c|6|warning: operation on ‘i’ may be undefined [-Wsequence-point]|
    main.c|6|warning: operation on ‘i’ may be undefined [-Wsequence-point]|
    main.c|6|warning: operation on ‘i’ may be undefined [-Wsequence-point]|
    As you can see my compiler is telling you that you are invoking undefined behavior.

    Jim

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by raviwagh
    there is no undefined behavior, so let me prove my point.
    Thus c arguments have right from left convention following programm
    Refer to the C standard:
    Quote Originally Posted by C11 Clause 6.5.2.2 Paragraph 10
    There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call. Every evaluation in the calling function (including other function calls) that is not otherwise specifically sequenced before or after the execution of the body of the called function is indeterminately sequenced with respect to the execution of the called function.
    This means that the order of evaluation of the arguments is unspecified ("indeterminately sequenced"), so your claim of "right from left convention" does not necessarily hold true. But there's more:
    Quote Originally Posted by C11 Clause 6.5 Paragraph 2
    If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.
    In the code from post #1, the two instances of I++ and the ++I result in side effects on I, which is the same scalar object. These side effects are unsequenced relative to each other since the sequence point comes "after the evaluations of the function designator and the actual arguments but before the actual call". Therefore, the behaviour is undefined. Likewise for a++ and ++a in the example you posted in post #3.

    Quote Originally Posted by raviwagh
    Try above programm's and i bet they will give same output in any compiler.
    How much would you like to bet?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Sep 2014
    Posts
    10
    Quote Originally Posted by jimblumberg View Post
    It appears that my compiler disagrees with you:



    As you can see my compiler is telling you that you are invoking undefined behavior.

    Jim
    which compiler are you using??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pre and post increment
    By prathiksa in forum C Programming
    Replies: 7
    Last Post: 12-07-2012, 06:03 AM
  2. Post Increment an Pre Increment operators in c++
    By anil_ in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2011, 08:27 PM
  3. pre & post increment need help
    By zing_foru in forum C Programming
    Replies: 6
    Last Post: 10-09-2009, 12:14 PM
  4. pre/Post Increment
    By ganesh bala in forum C Programming
    Replies: 1
    Last Post: 02-12-2009, 04:17 AM
  5. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM