Thread: question about increment operator

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    75

    question about increment operator

    Code:
    void main(){
    	int a;
    	a=10;
    	printf("%d\t %d",++a,a++);
    }
    output : 12 10

    i search on google but dont find anything.i thought output would be 11 11 but its not help me guys

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The behaviour is undefined, so the output could be anything. Read the FAQ answers under this C FAQ section on Expressions. Note that question 3.7 would normally apply ("the order of evaluation of the arguments to a function call is unspecified"), but here it really is undefined, not merely unspecified, because the arguments to the function are evaluated within consecutive sequence points (or you can they are unsequenced relative to each other).
    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

  3. #3
    Registered User
    Join Date
    Apr 2015
    Location
    Bangalore, Karnataka, India
    Posts
    34
    Quote Originally Posted by san12345 View Post
    Code:
    void main(){
        int a;
        a=10;
        printf("%d\t %d",++a,a++);
    }
    output : 12 10

    i search on google but don't find anything.i thought output would be 11 11 but its not help me guys

    I had tried this many times and observed different outputs on different kinds of compilers.
    But I am quite familiar with the GCC output and can explain what exactly has happened. See below points:
    - a++ : first store the value in the variable and then increment the value of the variable
    - ++a : First increment the value and then store it in the variable

    Now when ( ++a, a++ ) is encountered inside printf and as per right to left precedence first a++ will be executed. So, first this value will be stored as 10 and won't be incremented immediately. (But will be incremented before doing the next operation)
    While doing ++a, a contains 11 (because it is already incremented in the last operation) and the value will be 12 because this pre-increment operator will be executed on 11.

    So printf prints 12, 10.

    Now try with the following code:
    Code:
    void main(){
        int a;
        a=10;
        printf("%t %d\t %d", ++a, ++a, a++);
    }
    I have not tried this yet but as per my experience I believe you will get the following output:
    13, 13, 10 (in a GCC compiler)

    The reason is, all the post increment variables updated at last, no matter wherever they are encountered.
    Last edited by www.infysim.org; 02-26-2016 at 11:26 AM.

  4. #4
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    thx man thats the answer i was looking for.u clearly explained my queries thx again

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by www.infysim.org
    Now when ( ++a, a++ ) is encountered inside printf and as per right to left precedence first a++ will be executed.
    Again, the order of evaluation of function arguments is unspecified, so there is no "right to left precedence" to speak of.

    Quote Originally Posted by san12345
    thx man thats the answer i was looking for.u clearly explained my queries thx again
    Um, www.infysim.org's answer is specific to a particular compiler with particular compile options. The behaviour is undefined, hence the observation that "I had tried this many times and observed different outputs on different kinds of compilers".
    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

  6. #6
    Registered User
    Join Date
    Apr 2015
    Location
    Bangalore, Karnataka, India
    Posts
    34
    Quote Originally Posted by san12345 View Post
    thx man thats the answer i was looking for.u clearly explained my queries thx again
    I thought it will be difficult to make someone understand especially in a forum where you have to choose your words carefully to convey what you really want to explain.
    I am glad that my explanation helped you.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > I had tried this many times and observed different outputs on different kinds of compilers.
    There is no mystery once you understand what
    - a sequence point is
    - that an object may be modified only once between sequence points.

    When you know these things, you can see that analysing anything which modifies an object more than once is inherently broken, and analysing it is just a waste of time. Nothing you could learn from the analysis would be of lasting benefit.

    Quote Originally Posted by standard_c99
    6.5 Expressions

    2 Between the previous and next sequence point an object shall have its stored value
    modified at most once by the evaluation of an expression. Furthermore, the prior value
    shall be accessed only to determine the value to be stored.
    See also Question 3.1 and related questions.

    And for a practical demonstration of many different compilers and compiler flags, see
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-26-2015, 08:00 AM
  2. increment operator
    By Rohit88 in forum C Programming
    Replies: 5
    Last Post: 07-29-2013, 01:39 PM
  3. pre-increment operator..
    By narendrav in forum C Programming
    Replies: 6
    Last Post: 05-02-2013, 01:57 AM
  4. Question on increment operator
    By noodles in forum C Programming
    Replies: 1
    Last Post: 09-05-2006, 11:40 PM
  5. increment operator Question from a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:23 AM