Thread: increment operator

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    14

    increment operator

    Code:
    #include<stdio.h>
    void main()
    {
        int i=5;
        printf("%d\n",i++ + ++i);
    }
    this code prints the value as 12 because for 1st i will be taken as 5 and then it is incremented to 6 which is again incremented to have value 7 for 2nd i......but if we change the printf statement to printf("%d\n",i++ + i++); it gives 10 as output and also if printf("%d\n",i++ + i++ + ++i); gives 16 as output....
    Please help me thru this ??
    xyz88

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    This is undefined behavior and should be avoided
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    First things first: We use to write
    Code:
    int main(void)
    {
          ....
          return 0;
    }
    Also, try to keep your text outside the code tags

    Increment operators can lead to undefined behavior if they are not used as they ought to be.
    I have gathered examples from around here and you can take a look at this link.

    I think what you have there is an example of undefined behavior.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Definitely undefined behavior: Expressions. Your code is closes to 3.2, but many of the examples on that page apply.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Over 18 months ago, you asked basically the same question.
    calculation
    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.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    [SPAM]
    One thing is for sure people, Salem has a great memory!
    [/SPAM]
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pre-increment operator..
    By narendrav in forum C Programming
    Replies: 6
    Last Post: 05-02-2013, 01:57 AM
  2. Increment Operator
    By theunderdog118 in forum C Programming
    Replies: 4
    Last Post: 05-31-2010, 11:00 AM
  3. Increment Operator
    By anirban in forum C Programming
    Replies: 5
    Last Post: 11-25-2008, 08:25 AM
  4. Help with increment operator
    By capvirgo in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 11:06 PM
  5. Help with increment operator, please
    By capvirgo in forum C Programming
    Replies: 4
    Last Post: 02-18-2008, 07:06 PM