c - 为什么 printf ("%d",1/0.0) 的输出是 0?

标签 c floating-point printf undefined-behavior divide-by-zero

我使用代码块

当代码为:

printf("%d",1/0);

程序无法运行,出现错误。但是当我写这个时:

printf("%d",1/0.0);

程序可以运行,输出为0。我想知道为什么。

最佳答案

1/01/0.0 都是未定义的行为:

C11 §6.5.5 Multiplicative operators

The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.

关于c - 为什么 printf ("%d",1/0.0) 的输出是 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20342357/

相关文章:

c - 循环内的 strcat() 与 sprintf()

c - 如何使用clone()让父进程和子进程同时运行?

c# - C# 是否像 Objective-C 和 C++ 一样是 C 的超集?

c - C 中的四舍五入 float

java - 如何在保留小数点精度的同时将 float 转换为 double?

c - 返回 char *str 类型的 float 的 printf 值

c - 什么是 __attribute__((unused)) 静态的?

c - `out' 、 'in' 的操作数类型不匹配

c - 用户输入读取文件并测试其是否有效?在C中

使用 snprintf 复制数组中的字符