Increment Operator

This is a discussion on Increment Operator within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main() { int x=8; x = --x + x-- + x--; printf("%d\n",x); return 0; } Output is ...

  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
    Posts
    23,001
    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
    Posts
    23,001
    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, 08:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 07:54 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM
  5. increment operator Question from a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21