Little question about postfix and prefix

This is a discussion on Little question about postfix and prefix within the C Programming forums, part of the General Programming Boards category; I thought this code: Code: { int myInt; myInt = 5; printf( "myInt++ ---> %d\n\n++myInt ---> %d", myInt++, ++myInt); } ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    11

    Unhappy Little question about postfix and prefix

    I thought this code:
    Code:
    {           int	myInt;
                 myInt = 5;
                 printf( "myInt++ ---> %d\n\n++myInt ---> %d", myInt++, ++myInt);
    }
    will yield:

    "myInt++ ---> 5

    ++myInt ---> 7"

    but it actually gave:

    "myInt++ ---> 6

    ++myInt ---> 7"

    with a warning: operation on 'myInt' may be undefined.

    Can some one help me out? I'm so confused by the result and the warning message.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,621
    Read this C FAQ on expressions.
    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
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Changing a variable more than once in a single statement is undefined.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    11
    Thank you guys very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is prefix faster than postfix?
    By dwks in forum C Programming
    Replies: 6
    Last Post: 07-28-2005, 11:51 AM
  2. stacks postfix aww fooey
    By drdodirty2002 in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2004, 10:00 AM
  3. postfix and prefix
    By drdodirty2002 in forum C++ Programming
    Replies: 8
    Last Post: 09-24-2004, 06:26 AM
  4. postfix and prefix...???
    By matheo917 in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2002, 01:19 PM
  5. Data Structures / prefix and postfix
    By rickc77 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-08-2001, 11:48 AM

Tags for this Thread


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