-1

I am a beginner in C and I came across a code which went like this:

#include<stdio.h>
int main()
{
    int k=35;
    printf("%d %d %d",k==35,k=50,k>40);
    return 0;
}

I tried to find the output without writing the code in my computer and 'actually' running it first. So here's how I figured out what the output might be:

In the precedence table, among assignment (==), equality (=) and greater than (>) operators, the greater than operator will be implemented first. So, initially, k=35 and thus k>40 is false (the integer thus will be 0). Next up is the equality operator (==). Now, k==35 is true initially. So this will return an integer 1. Finally, the assignment operator (=) will do its job, set the value of k to 50(and ultimately, returning 50) and the program will exit. Thus, based on this logic, I 'guessed' that final output will be like:

1 50 0

Now I ran the code in my IDE (I'm coming to my IDE a bit later) and the result it gave was:

0 50 0

So, my confusion is, in which order are the operators being implemented?

Any help or hints are very much welcome.

NOTE: I noticed a bit closer and found that the only way possible is: First the > operator does its job, then the = operator and finally, the == operator.

I changed my printf line a bit and wrote:

printf("%d %d %d",k==35,k>40,k=50);

Here, I found that the output was:

0 1 50

which, again was not according to what I 'guessed' initially with my logic.

I tried all the possible ordering of the syntax inside the printf() function. After looking at all those outputs, I doubt: Is the program being implemented in a right-to-left order here irrespective of the ordering in the precedence table?

NOTE: Regarding my IDE, I use Dev C++ 5.11. I use it as our instructor has advised us to use it for our course. As this IDE is not so much well-received among many others, I tried an online compiler, but the results are the same.

DeBARtha
  • 460
  • 6
  • 17

2 Answers2

3

The behavior is undefined - the result is not guaranteed to be predictable or repeatable.

First, precedence only controls which operators are grouped with which operands - it does not control the order in which expressions are evaluated.

Second, the order in which function arguments are evaluated is unspecified - they can be evaluated right-to-left, left-to-right, or any other order1.

Finally, you are updating k (k = 50) and attempting to use it in a value computation (k == 35, k > 40) without an intervening sequence point, which is explicitly called out by the language definition as undefined behavior.

So the result can literally be anything, even what you expect it to be. You can’t rely on it to be consistent or predictable.


  1. This is not the same as the order in which they are passed to the function.
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Thank you. I understand it now. I got this code chunk from a book which asked what would be its output. I initially thought that it was undefined but I couldn't believe that it'll be in a question. – DeBARtha Dec 30 '20 at 11:39
  • 2
    In addition to right-to-left, left-to-right, or “any other order,” argument evaluation may be interleaved, such as evaluating some subexpressions of one argument before subexpressions of another argument in arbitrary orders and combinations. That includes a possibility that writing individual bytes to `k` in `k=50` may be interleaved with uses of the value of `k`, which is why the overall behavior is undefined. – Eric Postpischil Dec 30 '20 at 11:39
  • 2
    @DebarthaPaul: Unfortunately, there are a lot of bad C textbooks and tutorials, written by people who don’t understand the language as well as they think they do. This is a common misconception in many of them. – John Bode Dec 30 '20 at 12:06
0

printf("%d %d %d",k==35,k=50,k>40);

This is undefined behavior. The order of evaluation of the arguments is simply unspecified.

See here and here for further explanation.

Order of evaluation of the operands of any C operator, including the order of evaluation of function arguments in a function-call expression, and the order of evaluation of the subexpressions within any expression is unspecified[...]. The compiler will evaluate them in any order, and may choose another order when the same expression is evaluated again.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
JCWasmx86
  • 3,473
  • 2
  • 11
  • 29