在上面的程序中if语句总是返回true,即使我将变量“a”的数据类型从“int”更改为“char”。
if语句中的含义是什么?
#include <stdio.h> int main(){ int b = 10,a; if (a = 5){ printf("%d",b); } }In the above program if statement always returns true even if i change the data type of the variable "a" from "int" to "char".
What does = mean in the if statement??
最满意答案
=是C中的assignment operator 。根据C99 6.5.16:
赋值运算符将值存储在左操作数指定的对象中。 赋值表达式在赋值后具有左操作数的值,但不是左值。
这意味着表达式a = 5将返回5 ,因此if块中的指令将被执行。 相反,如果用a = 0替换它,那么0将由赋值表达式和内部指令返回, if不执行的话。
= is an assignment operator in C. According to C99 6.5.16:
An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue.
It means that expression a = 5 will return 5 and therefore instructions inside if block will be executed. In contrary, if you replaced it with a = 0 then 0 would be returned by assignment expression and instructions inside if would not be executed.
更多推荐
发布评论