Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

c - How exactly is the program running?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


This is not the same as the order in which they are passed to the function.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...