Thread: printf output

  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
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    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, 10:25 AM
  2. I have some questions :(
    By geekrockergal in forum C Programming
    Replies: 19
    Last Post: 02-01-2009, 09: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, 01: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, 09:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM