热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C中赋值表达式的结果是什么?-WhatistheresultofanassignmentexpressioninC?

Inthefollowingcode:在以下代码中:intc;while((c10)>0)Whatdoesc10evaluateto?Isit1wh

In the following code:

在以下代码中:

int c;
while((c=10)>0)

What does c = 10 evaluate to? Is it 1 which indicates that the value 10 is assigned to variable c successfully, or is it 10? Why?

c = 10评估为什么?是1表示值10成功分配给变量c,还是10?为什么?

5 个解决方案

#1


9  

c = 10 is an expression returning 10 which also assigns 10 to c.

c = 10是表达式返回10,其也将10分配给c。

#2


1  

while((c=10)>0)

c = 10 should return 10.

c = 10应该返回10。

Now, for while(10>0) 10>0, the > operator returns 1 (non-zero value).

现在,对于while(10> 0)10> 0,>运算符返回1(非零值)。

#3


1  

Assignment returns with the assigned value. In case c=10 is 10. Since 10!=0, in c it means also true so this is an infinite loop.

赋值返回指定的值。在c = 10的情况下是10.由于10!= 0,在c中它也意味着是,因此这是无限循环。

It is like you would write

就像你会写的那样

while(10)

Plus You've made the assignment.

加上你完成了作业。

If You follow this logic, You can see, that

如果你遵循这个逻辑,你可以看到,那

while(c=0)

would be a loop that never executes its statement or block.

将是一个永远不会执行其语句或块的循环。

#4


1  

This is an infinite loop. It first assign 10 to c, then compare it with c > 0, then again loop starts, assign 10 to c, compare it with c>0 and so on. Loop never ends. This is equivalent to the following:

这是一个无限循环。首先将10分配给c,然后将其与c> 0进行比较,然后再次循环开始,将10分配给c,将其与c> 0进行比较,依此类推。循环永不结束。这相当于以下内容:

while(c=10);

/* Because c assign a garbage value, but not true for all cases maybe it assign 0 */
while(c); 

Edit: It will not return 10 because compiler return only true or false value, so it return true or 1 instead of 10.

编辑:它不会返回10因为编译器只返回true或false值,所以它返回true或1而不是10。

#5


0  

It is said in C99 6.5.16

在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.

推荐阅读
author-avatar
尹洪2502855427
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有