PO_*_*PO_ 3 c precision divide-by-zero format-specifiers
最初我将变量 x 和 y 声明为 int 类型:
#include<stdio.h>
int main(){
int x, y = 0 ;
x = 1 / y;
printf("%d", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
程序崩溃(原因很明显)。
现在我将变量 x 和 y 声明为 double:
#include<stdio.h>
int main(){
double x, y = 0 ;
x = 1 / y;
printf("%d", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是输出:0。(为什么?)
然后我在 printf 中将 %d 更改为 %f:
#include<stdio.h>
int main(){
double x, y = 0 ;
x = 1 / y;
printf("%f", x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:1.#INF00
我不明白这里发生了什么。
请解释我上面的情况。