printf confusion

This is a discussion on printf confusion within the C Programming forums, part of the General Programming Boards category; Hi, Here is the code. Code: int main() { int i=10; printf(" %d %d \n",++i,i++); i=10; printf(" %d %d \n",i++,++i); ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Question printf confusion

    Hi,
    Here is the code.

    Code:
    int main()
    {
       int i=10;
       printf(" %d  %d \n",++i,i++);
    
       i=10;
       printf(" %d  %d \n",i++,++i);
      return 0
    }
    The o/p im getting is

    12 10
    11 12

    i have not been ablie to explain the o/p.
    Please help me find an answer as how i is evaluated and how the values r pushed and popped
    out of printf function call to get the o/p im getting.

    Thanks all

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I'm fairly certain that is undefined behavior. (This means that the output of the above code is unpredictable - it could vary between computers, compilers, or local weather predictions.)

    When using ++i or i++, try to use only one in a single expression. When the actual increment occurs is undefined. Break it up into two lines, and store intermediate results if need be.

    See this FAQ page. Questions 3.1, 3.2, 3.3

    The relevant standard quoting:
    Quote Originally Posted by my c89 draft, 3.3.2.2 'function calls'
    The order of evaluation of the function designator, the arguments, and subexpressions within the arguments is unspecified, but there is a sequence point before the actual call.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Definitely undefined.
    If some teacher/book is telling you otherwise, they're wrong.
    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. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 08:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 05:54 AM
  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