gpt4 book ai didi

c - 如果/或 % 运算符的一个或两个操作数是负数,结果是否将由实现定义?

转载 作者:太空狗 更新时间:2023-10-29 15:26:16 24 4
gpt4 key购买 nike

例如这些表达式的值是什么?它们依赖于编译器吗?

-7/3   -7%3
7/-3 7%-3
-7/-3 -7%-3

最佳答案

在 C89 中,除法 / 的结果可以被截断为负操作数。

在 C99 或更高版本中,结果将被截断为零。

% 运算符取决于所有标准中 / 的行为。


引用资料:

C89 § 3.3.5

If either operand is negative, whether the result of the / operator is the largest integer less than or equal to the algebraic quotient or the smallest integer greater than or equal to the algebraic quotient is implementation-defined, as is the sign of the result of the % operator. If the quotient a/b is representable. the expression (a/b) * b + a%b shall equal a.

C11 § 6.5.5

When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded

带脚注:

This is often called "truncation toward zero"

关于c - 如果/或 % 运算符的一个或两个操作数是负数,结果是否将由实现定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30255238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com