条件表达式作为printf的参数

时间:2021-09-04 01:09:03

So i came across the following code in C Language

所以我在C语言中遇到了以下代码

foo() {                     
    int v=10;
    printf("%d %d %d\n", v==10, v=25, v > 20);
}

and it returns 0 25 0 can anybody explain me how and why

并且它返回0 25 0可以任何人解释我如何以及为什么

4 个解决方案

#1


3  

printf("%d %d %d\n", v==10, v=25, v > 20);

What you see is undefined behavior becuase the order of evalutaion within printf() is not defined.

你看到的是未定义的行为,因为printf()中的evalutaion的顺序没有定义。

The output can be explained as(Right to left evaluation)

输出可以解释为(从右到左的评估)

v = 10 and hence v>20 is false so last `%d` prints 0
v = 25 Now v is 25 so second `printf()` prints out 25

Then you have

那你有

v ==10 which is false because v is 25 now. This is not a defined order of evaluation and might vary so this is UB

v == 10这是假的,因为v现在是25。这不是一个定义的评估顺序,可能会有所不同,所以这是UB

#2


0  

Your compiler seems to evaluate the function parameters from right to left.

您的编译器似乎从右到左评估函数参数。

So, v > 20 is evaluated first, then v=25 and then v==10.

因此,首先评估v> 20,然后v = 25,然后v == 10。

Therefore you get the output 0 25 0

因此,您得到输出0 25 0

#3


0  

Your code is subject to undefined behavior. Looks like in your platform,

您的代码受到未定义的行为的影响。在你的平台上看起来像

v > 20 got evaluated first, followed by
v=25, followed by
v==10.

首先评估v> 20,然后是v = 25,接着是v == 10。

Which is perfectly standards compliant behavior.

这是完全符合标准的行为。

Remember that those expressions could have been evaluated in any order and it would still be standards compliant behavior.

请记住,这些表达式可以按任何顺序进行评估,它仍然是符合标准的行为。

#4


0  

It is evaluated from right to left...

它从右到左进行评估......

First it evaluates v > 20 its false so it prints 0

首先,它评估v> 20为false,因此它打印0

Next it sets v=25 an prints it

然后它设置v = 25打印它

Next it check if v is 10. Its false so it prints 0 (the value of v is changed in the above step)

接下来检查v是否为10.其为false,因此它打印0(在上面的步骤中更改了v的值)

EDIT

This is the way your compiler evaluates it but the order of evalaution generally is undefined

这是编译器评估它的方式,但evalaution的顺序通常是未定义的

#1


3  

printf("%d %d %d\n", v==10, v=25, v > 20);

What you see is undefined behavior becuase the order of evalutaion within printf() is not defined.

你看到的是未定义的行为,因为printf()中的evalutaion的顺序没有定义。

The output can be explained as(Right to left evaluation)

输出可以解释为(从右到左的评估)

v = 10 and hence v>20 is false so last `%d` prints 0
v = 25 Now v is 25 so second `printf()` prints out 25

Then you have

那你有

v ==10 which is false because v is 25 now. This is not a defined order of evaluation and might vary so this is UB

v == 10这是假的,因为v现在是25。这不是一个定义的评估顺序,可能会有所不同,所以这是UB

#2


0  

Your compiler seems to evaluate the function parameters from right to left.

您的编译器似乎从右到左评估函数参数。

So, v > 20 is evaluated first, then v=25 and then v==10.

因此,首先评估v> 20,然后v = 25,然后v == 10。

Therefore you get the output 0 25 0

因此,您得到输出0 25 0

#3


0  

Your code is subject to undefined behavior. Looks like in your platform,

您的代码受到未定义的行为的影响。在你的平台上看起来像

v > 20 got evaluated first, followed by
v=25, followed by
v==10.

首先评估v> 20,然后是v = 25,接着是v == 10。

Which is perfectly standards compliant behavior.

这是完全符合标准的行为。

Remember that those expressions could have been evaluated in any order and it would still be standards compliant behavior.

请记住,这些表达式可以按任何顺序进行评估,它仍然是符合标准的行为。

#4


0  

It is evaluated from right to left...

它从右到左进行评估......

First it evaluates v > 20 its false so it prints 0

首先,它评估v> 20为false,因此它打印0

Next it sets v=25 an prints it

然后它设置v = 25打印它

Next it check if v is 10. Its false so it prints 0 (the value of v is changed in the above step)

接下来检查v是否为10.其为false,因此它打印0(在上面的步骤中更改了v的值)

EDIT

This is the way your compiler evaluates it but the order of evalaution generally is undefined

这是编译器评估它的方式,但evalaution的顺序通常是未定义的