printf output

This is a discussion on printf output within the C Programming forums, part of the General Programming Boards category; The below code gives following output: 5 6 3 6 Code: #include<stdio.h> void main() { int i = 3; printf ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    Question printf output

    The below code gives following output:
    5 6 3
    6
    Code:
    #include<stdio.h>
    
    void main()
    {
    	int i = 3;
    	printf ("\n%d %d %d\n", i++, ++i, i++);
    	printf("%d\n", i);
    }
    can someone explain me how?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    That is improper use of prefix and postfix operators on the same variable in the same expression. In other words, you are getting exactly what you are asking for: undefined behavior.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    If you want to do a bit more reading on this, check these links from the comp.lang.c FAQ:

    http://c-faq.com/expr/seqpoints.html
    http://c-faq.com/expr/evalorder4.html
    http://c-faq.com/expr/experiment.html
    http://c-faq.com/expr/evalorder2.html
    http://c-faq.com/expr/ieqiplusplus.html

    and so on..

    Make sure you read the 'see also' references as well.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    Thanks guys...that was very enlightening!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  2. I have some questions :(
    By geekrockergal in forum C Programming
    Replies: 19
    Last Post: 02-01-2009, 08:44 AM
  3. menu problem!!! need U R G E N T help!!!
    By catcat28 in forum C Programming
    Replies: 16
    Last Post: 11-19-2007, 12:32 PM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

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