Thread: Problem understanding simple increment operation

  1. #1
    Registered User
    Join Date
    Feb 2011

    Problem understanding simple increment operation

            int i=5,j;
            j=++i + ++i + ++i;
    	 printf("%d",j); //22
            j=i++ + i++ + i++;
            printf("%d",j); //19
    Shall not it give 21 and 18 respectively?????
    Last edited by Avenger625; 01-27-2013 at 01:18 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Nice, France
    Switching on the warnings can help.

    This results to undefined behavior and should never be followed by any programmer. But I guess, you found that while learning. (so do I one year ago (or two probably)).

    linux05:/home/users/std10093>gcc -Wall px.c -o px
    px.c: In function 'main':
    px.c:4: warning: operation on 'i' may be undefined
    px.c:4: warning: operation on 'i' may be undefined
    px.c:7: warning: operation on 'i' may be undefined
    px.c:7: warning: operation on 'i' may be undefined
    I got 22 and 15 :P
    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

  3. #3
    Registered User
    Join Date
    Nov 2012
    The important thing to remember with expressions is that the order of evaluation is not specified. So for example if you write

    j = EXPRA + EXPRB + EXPRC;
    Then the order in which EXPRA, EXPRB and EXPRC is evaluated is not fixed. It is arbtirary. Therefore, you should not use any expressions here where you really are depending on their side effects. A side effect is something that happens as the result of an expression being evaluated (like incrementing a variable, for example).

    So basically, there is not any correct answer for what will happen for the expressions you gave. Usually the best idea is to use increments either all by themselves, or to use them in very restricted situations, for example

    myArray[i++] = foo;
    is just fine. It puts foo into myArray at position i, and then conveniently increments i for you afterwards.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    The example you gave contains several lines of code that result in undefined behavior. There are lots of similar examples with discussion in the following section of the C-FAQ: Expressions.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Check my example here -> Post and Pre Increment
    Make careful note of the wonderful variety of answers you can get from undefined behaviour.
    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. Simple ternary operation baffling me
    By alter.ego in forum C Programming
    Replies: 4
    Last Post: 02-20-2012, 08:26 AM
  2. how to change this simple opeation into power operation..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 12-20-2008, 03:17 PM
  3. Increment simple question
    By SurferIX in forum C Programming
    Replies: 6
    Last Post: 06-01-2008, 02:49 PM
  4. Replies: 11
    Last Post: 08-30-2004, 03:56 PM
  5. Replies: 5
    Last Post: 12-17-2001, 11:59 AM