我迷糊之间的真假..没有一个真值代表非零和假值代表零? [英] I am confused between True and False .. does a True value stands for Non-Zero and False value stands for Zero?

查看:173
本文介绍了我迷糊之间的真假..没有一个真值代表非零和假值代表零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的程序让我比较操作完成后,相反的答案?

Why does the below program gives me the opposite answer after comparison operations are done?

main()
{
    int k=35;
    printf("%d\n%d\n%d",k==35,k=50,k<40); 
}

输出

0
50
1

0 50 1

推荐答案

这方案是不是一个有效的C程序按照C标准。结果
没有与此计划相关的问题2

This program is not a valid C program as per the C standard.
There are 2 problems associated with this program.

问题1:未指定的行为

参数评测给函数的顺序是的未指定 [参考1]

The order of evaluation of arguments to a function is Unspecified[Ref 1].


  • 它可以从左到右或

  • 这可能是从右到左或

  • 任何其他神奇的秩序

问题2:未定义行为

这是未定义行为 [参考文献2] ,因为变量应该不是没有干预序列点被修改多次。需要注意的是在函数参数不引入序列点。因此, K 没有干预序列点被修改,导致未定义的行为。

This has undefined behavior[Ref 2] because a variable should not be modified more than once without a intervening sequence point. Note that , in the function arguments does not introduce a sequence point. Thus k gets modified without a intervening sequence point and causes Undefined Behavior.

所以,你不能依赖行为要具体在这种情况下,任何东西。该方案是不是一个有效的C程序。

So you cannot rely on the behavior to be anything specific in this case. The program is not a valid C program.

[参考1] 结果
C99标准6.5.2.2.10:

功能指示符,实际的论点评估的秩序,
  实际的参数内SUBEX pressions是不确定的,但有一个序列点
  前实际调用。

The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.

[参考文献2] 结果
C99标准6.5.2:

在previous和下一个序列点的对象应具有其存储的值之间
  由前pression的评价最多一次修改。此外,现有的值
  应仅被访问,以确定被存储的值。

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.

需要注意的是不确定的,并且未定义行为是由标准定义的术语:


Note that Unspecified and Undefined Behavior are terms defined by the standard as:

C99标准3.19不确定的行为:

行为,其中本国际标准提供了两个或更多的可能性和
  并没有规定在其上选择在任何情况下要求

behavior where this International Standard provides two or more possibilities and imposes no requirements on which is chosen in any instance

C99标准3.18未定义行为:

行为,使用时的非便携的或错误的程序构建体,错误的数据,或
  的indeterminately价值对象,这本国际标准并没有规定
  要求

behavior, upon use of a nonportable or erroneous program construct, of erroneous data, or of indeterminately valued objects, for which this International Standard imposes no requirements

这篇关于我迷糊之间的真假..没有一个真值代表非零和假值代表零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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