Thread: pre and post increment

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    33

    pre and post increment

    int a = 5;

    printf("%d %d %d \n",a++,a++,++a);



    it prints 7 6 8

    im using gcc compiler,
    how it evaluates....

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    The order in which the program executes the parameters to a function is unspecified.

    Relying on a specific order is Undefined Behaviour.

    Don't rely on specific order.
    Don't write code with Undefined Behaviour.

    Code:
    int a = 5;
    printf("%d %d %d\n", a + 1, a + 2, a + 3);
    a = 8;

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    Code:
    int a = 5; printf("%d %d %d\n", a + 1, a + 2, a + 3);
    it prints 7 6 8

    im using gcc compiler,
    how it evaluates....

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... you probably read the output wrongly. In that second code snippet, no matter what order of evaluation is used, the result should still be 6 7 8
    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

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    yes,the correct one is
    printf("%d %d %d \n",a++,a++,++a);

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by prathiksa View Post
    printf("%d %d %d \n",a++,a++,++a);
    It could print 7 6 8
    it could print yellow
    it could format your hard drive
    it could send money from your bank account to mine

    Don't write Undefined Behaviour!

    To see what your compiler, with your options, did: try seeing the assembler output (hint: gcc -S).
    Note the output may be different if you compile in the afternoon, or change the optimization level, or your teacher is looking, or ... ... ...
    Last edited by qny; 12-07-2012 at 06:00 AM.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    in my machine it prints 7 6 8

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's a regular topic.

    Post and Pre Increment
    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. Post and Pre Increment
    By Raj 89 in forum C Programming
    Replies: 7
    Last Post: 11-22-2012, 11:02 PM
  2. Post Increment an Pre Increment operators in c++
    By anil_ in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2011, 08:27 PM
  3. pre & post increment need help
    By zing_foru in forum C Programming
    Replies: 6
    Last Post: 10-09-2009, 12:14 PM
  4. post vs pre increment
    By xddxogm3 in forum C Programming
    Replies: 13
    Last Post: 03-19-2004, 05:07 PM
  5. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM

Tags for this Thread