Like Tree1Likes
  • 1 Post By qny

pre and post increment

This is a discussion on pre and post increment within the C Programming forums, part of the General Programming Boards category; int a = 5; printf("%d %d %d \n",a++,a++,++a); it prints 7 6 8 im using gcc compiler, how it evaluates.......

  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
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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;
    rags_to_riches likes this.

  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
    21,313
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    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 05: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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 10:02 PM
  2. Post Increment an Pre Increment operators in c++
    By anil_ in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2011, 07: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, 04: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


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