if语句中的赋值语句如何作为条件? [英] How the assignment statement in an if statement serves as a condition?

查看:651
本文介绍了if语句中的赋值语句如何作为条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if ((vnd = (struct diam_vnd_t *)g_hash_table_lookup(vendors,vend))) {...}

您能告诉我为什么它是赋值而不是方括号内的布尔表达式吗?在什么情况下可以将此分配视为真"或假"?

Can you tell me why it is an assignment but not a boolean expression in the brackets ? And in what situation this assignment can be considered "true" or "false" ?

推荐答案

引用C11,第6.5.16章,赋值运算符(强调我的)

Quoting C11, chapter §6.5.16, Assignment operators (emphasis mine)

赋值运算符将一个值存储在由左操作数指定的对象中. 一个 赋值表达式具有赋值后的左操作数的值, 111),但没有 左值.

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,111) but is not an lvalue.

因此,首先将发生分配,然后,已分配的值将用作if中的条件语句.

So, first the assignment will happen, and then, the value that has been assigned will be used as the conditional statement in if.

所以,如果是

if (p = 0 )

将评估为FALSE,并且

will evaluate to FALSE and

if (p = 5)

将为TRUE.

这篇关于if语句中的赋值语句如何作为条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆