无法理解为什么会产生这样的输出

时间:2022-03-28 22:25:23
main()
{
    int a = 10, b = 5, c = 5;
    int d;
    d = a == (b + c);
    printf("%d", d);
}

OUTPUT: 1

Can anyone please explain how this value is assigned to d??

任何人都可以解释这个值如何分配给d ??

3 个解决方案

#1


2  

== has a higher precedence than =, so

==的优先级高于=,所以

d = a == (b + c);

is equivalent to:

相当于:

d = (a == (b + c));

it tests if a is equal to b + c, 1 if true, and 0 if false.

它测试a是否等于b + c,如果为真则为1,如果为假则为0。

#2


2  

a == (b + c) is true, true is represented by a 1 from your compiler, that's why d becomes 1.

a ==(b + c)为真,true由编译器中的1表示,这就是d变为1的原因。

if the sum of b + c was not equal to 10 it would have printed 0

如果b + c的总和不等于10则会打印0

Remmeber in C false is represented by 0, any other value means true.

C false中的Remmeber由0表示,任何其他值表示true。

Thus

if(-1)
{
    printf("true");
}

prints true

#3


1  

== returns 1 if its operands are equal and 0 if they're not.

==如果操作数相等则返回1,如果不操作则返回0。

#1


2  

== has a higher precedence than =, so

==的优先级高于=,所以

d = a == (b + c);

is equivalent to:

相当于:

d = (a == (b + c));

it tests if a is equal to b + c, 1 if true, and 0 if false.

它测试a是否等于b + c,如果为真则为1,如果为假则为0。

#2


2  

a == (b + c) is true, true is represented by a 1 from your compiler, that's why d becomes 1.

a ==(b + c)为真,true由编译器中的1表示,这就是d变为1的原因。

if the sum of b + c was not equal to 10 it would have printed 0

如果b + c的总和不等于10则会打印0

Remmeber in C false is represented by 0, any other value means true.

C false中的Remmeber由0表示,任何其他值表示true。

Thus

if(-1)
{
    printf("true");
}

prints true

#3


1  

== returns 1 if its operands are equal and 0 if they're not.

==如果操作数相等则返回1,如果不操作则返回0。