Using expressions as arguments of printf function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mamunur rashid
    New Member
    • Nov 2010
    • 6

    Using expressions as arguments of printf function

    Hello, please explain the output of following simple C program:
    Code:
    int main( ) 
    { 
        int x = 15; 
        printf("\n%d %d %d", x != 15, x = 20, x > 30);
        return 0; 
    }
    Output: 1 20 0

    I understand the output 20, its just being assigned to x. But am confused about the return value of other two expressions; i.e. x!=15 and x>30. When do we get a 0 and when a 1?
    Last edited by Rabbit; Jan 21 '14, 04:38 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • stdq
    New Member
    • Apr 2013
    • 94

    #2
    Hi! x != 15 is false, since x equals 15 (instead of being different than that), so the returned value in that case is 0 (false). The same goes for x > 30. On the other hand, when the expression is true, then you a get a 1.

    Comment

    • mamunur rashid
      New Member
      • Nov 2010
      • 6

      #3
      Hi stdq, please notice x!=15 didn't return a 0, it returned a 1 (true?). That's why I am confused.

      Comment

      • stdq
        New Member
        • Apr 2013
        • 94

        #4
        I understand...I don't know the reason, though. I would expect a 0. Maybe someone will provide us with the answer. Sorry I can't be of more help.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          From the C99 Standard
          6.5 Expressions
          ...
          2 Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
          Your code violates this rule by modifying x once and reading it, not once, but twice in order to evaluate the two Boolean expressions -- leading to undefined behavior. Undefined behavior is unpredictable and need not be consistent. From the point of view of the C Standard, it would be just as legitimate for this program to reformat your hard drive as to print the output you're seeing.
          Avoid undefined behavior!

          Comment

          • mamunur rashid
            New Member
            • Nov 2010
            • 6

            #6
            Friends, I have found a convincing answer.
            In C, we follow standard calling convention which is based on 2 things-
            1. the order in which arguments are passed to a function
            2. the cleanup of stack elements former to store formal arguments and local variable.
            Using standard calling convention, arguments are passed in right to left order. So initially we have x=15.
            Now,(starting from right) x>30 is false, so it returns 0.
            Then, x becomes 20, 20 is printed.
            Lastly, x(20)!=15, is true and returns non zero value 1.
            And these are printed in order 1 20 0

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              It is tempting to think you can figure out how your particular compiler handles certain cases of undefined behavior. However, you can never be sure. The behavior may vary with optimization level or with the instructions surrounding the snippet in question. The behavior will certainly vary from compiler to compiler.

              I have 35+ years of experience with programming. I cannot emphasize enough that the correct answer to your original question is...
              "The program in question exhibits undefined behavior; so nothing it does should come as a surprise."

              Don't give in to the temptation to try and predict what the undefined behavior will be. From there, it is a short hop to writing programs that take advantage of your predictions. The result is nonportable code that fails intermittently.

              I have given similar advice here and elsewhere.

              Comment

              Working...