Thread: Increment Operator

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Increment Operator

    Code:
    #include<stdio.h>
    
    int main() 
    {
      int x=8;
    
      x = --x + x-- + x--;
    
      printf("%d\n",x);
    
      return 0;
    }
    Output is 19..Can anyone explain how?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The behavior is undefined, simply put. It can become anything.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Thanks but why undefined??? Can you explain please?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because you are modifying the same variable multiple times in one statement.
    Break them into separate rows and it will no longer be undefined.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anirban View Post
    Thanks but why undefined??? Can you explain please?
    Because the C standard says that a variable should only be changed once within a sequence point (basically, within one statement, although that's a simplification). This is so that the compiler doesn't HAVE to do these type of things in a particular order, which in turn allows the compiler to use more efficient instructions in some special cases [1]. And this is why the value from such an expression will vary from one compiler to another (or between different versions of compilers, different compiler settings, etc).

    [1] For example, 68K and PDP-11/VAX-11 have "autoincrement" and "autodecrement" instruction modes that automatically update the pointer of a memory location, as well as store or retrieve the data in memory. Using *ptr++ can then be implemented as a autoincrement type operation. But if the compiler had to take into account the entire line of execution before using such operations, it may not be able to use them very often.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 06:54 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. increment operator Question from a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:23 AM