简体   繁体   English

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

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

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 

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 else path. 这将迫使else路径。

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

These messages are precisely backwards. 这些消息恰好是向后的。 The statement after the if is executed if the condition isn't 0, and the statement after the else is executed if the condition is 0, so this should be 会后声明中if如果条件不为 0,且如果条件 0,则执行ELSE后面的语句,所以这应该是执行

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. 如预期的那样。

If () function accepts true or false value as an argument. If()函数接受true或false值作为参数。

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. 的语句部分将不会执行,系统将直接跳转到其他部分。

In the case you mentioned you assigned 0 to your variable and passed it as an argument to if(). 在您提到的情况下,您为变量分配了0,并将其作为参数传递给if()。 Note that if() only accepts 0 or non 0 value. 请注意,if()仅接受0或非0值。 So, it doesn't matter what assignment you made. 因此,进行什么作业都没有关系。 if() will recieve the value of your variable 'a' as an argument and act accordingly. 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。

if(a=0) is assignement of 0 in variable a . if(a=0)是变量a0赋值。 if you want to compare a against zero you need to write like below if(a==0) 如果要比较a与零,则需要编写如下if(a==0)

your condition is simple assignment which is making a as zero so condition getting false and you are getting prints from else part. 您的条件是简单赋值,将a设为零,因此条件变为假,并且您从else部分获得打印件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当分配给另一个变量时,C 中的结构中的 malloced 数据会发生什么情况? - What happens to malloced data in a struct in C when it is assigned to another variable? 当 fork() 处于某个条件时会发生什么? - What happens when fork() in a condition? 声明变量时会发生什么? - What happens when declaring a variable? 在初始化期间为指针指定字符串值时究竟会发生什么? - What exactly happens when pointers are assigned with strings values during initialization? 如果为char分配的值太大而无法放入字节,会发生什么? - What happens when a char is assigned a value too large to fit in a byte? 当main()函数没有返回零时会发生什么 - What Happens when main() function does not return zero 当变量超出范围时会发生什么? - What happens when a variable goes out of scope? 如果发出了pthread条件变量但互斥锁已锁定的情况,会发生什么情况? - What happens if a pthread condition variable is signaled but the mutex is locked? 当逗号分隔的for循环的条件部分中有多个表达式时会发生什么? - What happens when there is multiple expressions in the condition part of a for loop seperated by commas? break语句后分配的值会怎样? - What happens to assigned values after break statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM