C语言 在“if”条件下将变量赋值为零时会发生什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17540692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:52:53  来源:igfitidea点击:

What happens when a variable is assigned to zero in an `if` condition?

c

提问by Ani

It would be helpful if anybody can explain this.

如果有人可以解释这一点,那将会很有帮助。

int main()
{
 int a=0;
 if(a=0)
       printf("a is zero\t");
 else
       printf("a is not zero\t");
 printf("Value of a is %d\n",a);
 return 0;
}

output of this is

这个的输出是

a is not zero   Value of a is 0 

回答by trojanfoe

The result of the assignment is the value of the expression.

赋值的结果是表达式的值

Therefore:

所以:

if (a = 0)

is the same as:

是相同的:

if (0)

which is the same as:

这与:

if (false)

which will force the elsepath.

这将强制else路径。

回答by Jim Balter

if(a=0)
       printf("a is zero\t");
 else
       printf("a is not zero\t");

These messages are precisely backwards. The statement after the ifis executed if the condition isn't0, and the statement after the else is executed if the condition is0, so this should be

这些消息恰恰是倒退的。后声明if如果条件执行为0,如果条件执行否则会后声明0,所以这应该是

if(a=0)
       printf("a is not zero\t");
 else
       printf("a is zero\t");

Or, equivalently but more clearly,

或者,等效但更清楚地,

a = 0;
if(a)
       printf("a is not zero\t");
 else
       printf("a is zero\t");

Which, together with

其中,连同

printf("Value of a is %d\n",a);

would print

会打印

a is zero   Value of a is 0 

as expected.

正如预期的那样。

回答by Chetan Bhasin

If () function accepts true or false value as an argument.

if() 函数接受真或假值作为参数。

So whatever you put inside the bracket has no significance to if() function but of the fact what value it has.

因此,无论您在括号内放入什么,对 if() 函数都没有意义,但实际上它具有什么价值。

'0' in any case is considered as false value, so when you pass 0 as an argument like:

'0' 在任何情况下都被视为假值,因此当您将 0 作为参数传递时,例如:

if(0)
{
  ---statments---
}

The statement part of will not get executed, and the system will directly jump to else part.

的语句部分不会被执行,系统会直接跳转到else部分。

In the case you mentioned you assigned 0 to your variable and passed it as an argument to if(). Note that if() only accepts 0 or non 0 value. So, it doesn't matter what assignment you made. if() will recieve the value of your variable 'a' as an argument and act accordingly.

在您提到的情况下,您将 0 分配给您的变量并将其作为参数传递给 if()。请注意, if() 仅接受 0 或非 0 值。所以,你做了什么任务并不重要。if() 将接收变量 'a' 的值作为参数并相应地采取行动。

In this case, since the value of a is 0, the if part will not get executed and the system will jump to else.

在这种情况下,由于 a 的值为 0,因此不会执行 if 部分,系统将跳转到 else。

回答by Dayal rai

if(a=0)is assignement of 0in variable a. if you want to compare aagainst zero you need to write like below if(a==0)

if(a=0)0in 变量的赋值a。如果你想a与零进行比较,你需要像下面这样写 if(a==0)

your condition is simple assignment which is making aas zero so condition getting false and you are getting prints from elsepart.

您的条件是简单的分配,它a为零,因此条件变得错误,并且您正在从else零件中获得打印件。