Thread: Famous C Question :)

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    8

    Famous C Question :)

    Hi guys

    I can bet that a lot of C experienced programmers have seen this before

    int i=0;
    printf("%d %d %d", i, i++, ++i);


    gives:

    2 1 1

    I know that it has something to do with the "stack", but I don't really get it , so any help is appreciated

    another thing guys, if you please

    I really need a C book that concentrates on things going under
    the hood, I mean, every C book I read is all about loops, pointers, array,..etc

    I need a book that explains what happens in the memory while executing the program not just the syntax

    Thanks guys




  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Actually, I get different output than you do.

    I get : 2 1 2 which makes more sense, tbh.

    So basically, I think the ++ operator takes an address and increments it that way. Before a print happens, the calculations happen so we have the first i++ which yields 1 then the second ++i which yields 2. This all changes the value of i as well from 0 to 1 to 2.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    8
    I found this ia a book.
    I am using Netbeans with Cygwin plugin and gcc.

    You may get one of many results from such a statement. The commas serve only to separate arguments of the
    printf(), and do not generate the left-to-right sequence that they would otherwise when they aren't used in functions. With the statement shown here, you are not ensured of any order. The postfix i++ might possibly be performed before the prefix ++i, even though the precedence table does not explain this. Here, the order of evaluation depends on how your compiler sends these arguments to the printf() function.

    What compiler are you using ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,856
    Quote Originally Posted by HighTechPro
    I can bet that a lot of C experienced programmers have seen this before
    Yes, good bet. This one (not the exact code, but the ideas involved) is covered by this FAQ on expressions.

    Quote Originally Posted by MutantJohn
    Actually, I get different output than you do.
    The behaviour is undefined. There is a sequence point after all the arguments are evaluated (and the order of evaluation itself is unspecified to begin with), but the problem is that i is modified more than once before this sequence point.

    EDIT:
    Quote Originally Posted by Mysterious Book
    The commas serve only to separate arguments of the printf(), and do not generate the left-to-right sequence that they would otherwise when they aren't used in functions
    This is correct, i.e., the order of evaluation of arguments in a function call is unspecified.

    Quote Originally Posted by Mysterious Book
    You may get one of many results from such a statement. (...) With the statement shown here, you are not ensured of any order. The postfix i++ might possibly be performed before the prefix ++i, even though the precedence table does not explain this. Here, the order of evaluation depends on how your compiler sends these arguments to the printf() function.
    This is inaccurate. Because of the fact that the same variable is modified twice between consecutive sequence points, the behaviour is undefined. This technical difference means that a compiler can validly choose to say, do what the book suggests, or it could reject the code, or it could do something entirely different, e.g., print "hello world".
    Last edited by laserlight; 08-29-2013 at 12:20 PM.
    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
    Aug 2013
    Posts
    8
    Thanks for your help and guidance,
    I will be more careful next time.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    We should made this even more undefined and start using multiple threads acting on i without ever using a mutex.

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    8
    Quote Originally Posted by MutantJohn View Post
    Actually, I get different output than you do.

    I get : 2 1 2 which makes more sense, tbh.

    So basically, I think the ++ operator takes an address and increments it that way. Before a print happens, the calculations happen so we have the first i++ which yields 1 then the second ++i which yields 2. This all changes the value of i as well from 0 to 1 to 2.

    What compiler are you using ?

  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Just gcc.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,121
    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.

  10. #10
    Registered User
    Join Date
    Aug 2013
    Posts
    8
    We are using the same compilers
    How come we got different results !!
    This is confusing

    Can you check the expression in your program again, if you please ?

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    different versions of the same compiler may produce different results, and even the same version between different levels of optimization.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,856
    Quote Originally Posted by HighTechPro
    We are using the same compilers
    How come we got different results !!
    Different compiler version, different compiler option, different surrounding code, etc. Besides, because undefined behaviour is involved, the compiler could theoretically produce different code each time you compiled it, with everything else the same other than the time of compilation.

    EDIT:
    In other words, understand why this is undefined behaviour, then move on to write correct code.
    Last edited by laserlight; 08-29-2013 at 01:26 PM.
    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

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,121
    > We are using the same compilers
    Just forget about trying to analyse or rationalise the code.
    The only thing you need to know is that it is BROKEN and you should walk away from it, and find something you can actually use.
    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.

  14. #14
    Registered User
    Join Date
    Aug 2013
    Posts
    8
    Quote Originally Posted by Salem View Post
    > We are using the same compilers
    Just forget about trying to analyse or rationalise the code.
    The only thing you need to know is that it is BROKEN and you should walk away from it, and find something you can actually use.
    I know it is BROKEN and I will never use it,
    I am trying to understand why

    thanks all,
    I got it

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,674
    I found this ia a book.
    Let me guess...is it "Let Us C"?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  3. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM