c - C99 标准中的哪里说有符号整数溢出是未定义的行为?

标签 c c99 undefined-behavior

C99 标准中的哪里说有符号整数溢出是未定义的行为?

我在 6.2.5 节看到关于无符号整数溢出的评论是明确定义的(参见 Why is unsigned integer overflow defined behavior but signed integer overflow isn't? ):

A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.



但我在附录 J 中查看未定义行为,我只在列表中看到这些类似的项目:

An expression having signed promoted type is left-shifted and either the value of the expression is negative or the result of shifting would be not be representable in the promoted type





The value of the result of an integer arithmetic or conversion function cannot be represented



(注意这是指“整数算术函数”,而不是整数算术本身

最佳答案

我没有 C99 的副本,但在 C11 标准中,此文本出现在第 6.5 节第 5 段中:

If an exceptional condition occurs during the evaluation of an expression (that is, if the result is not mathematically defined or not in the range of representable values for its type), the behavior is undefined.



对于任何溢出来说,这似乎是一个包罗万象的东西;然后关于无符号整数的文本成为 6.5 ¶ 5 以上的特例。

关于c - C99 标准中的哪里说有符号整数溢出是未定义的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36342460/

相关文章:

c - 当不支持重载函数时,C 中的 math.h 函数如何接受多种类型的参数?

c++ - 三元运算符是否以定义的方式短路

c - 判断一个函数传递了一个void指针(char int ore Failure)

c - 零(!0)编译器的逻辑否定是否依赖于 C?

pass-by-reference - 在 C99 中通过引用传递

c++ - std::map - 递减迭代器给出奇怪的结果?

c++ - strcpy 中的段错误

mysql - 以编程方式登录mysql

c - 从代表不同值的不同文件访问 C 中的相同宏

在 C : trying to change the size of the allocation first 中编码 realloc