#include <stdio.h>
float diff_abs(float,float);
int main() {
float x;
float y;
scanf("%f", &x);
scanf("%f", &y);
printf("%f\n", diff_abs(x,y));
return 0;
}
float diff_abs(float a, float b) {
float *pa = &a;
float *pb = &b;
float tmp = a;
a = a-b;
b = *pb-tmp;
printf("%.2f\n", a);
printf("%.2f\n", b);
}
大家好,我正在编写一个 C 程序,它应该保存在变量 a-b 中,在 b 中保存为变量 b-a。 一切正常,但如果我在输出结束时运行我的代码,编译器会向我显示此消息:
3.14
-2.71
5.85
-5.85
1.#QNAN0
1.#QNANO 是什么意思?
最佳答案
在这段代码 printf("%f\n", diff_abs(x,y))
中,您告诉编译器打印一个 float
类型的变量这应该是函数 diff_abs
的返回值。但是在您的函数 diff_abs
中,您没有返回任何值。
所以等待float
的%f
不会得到任何值,它会打印#QNAN0
,这意味着不是数字
。所以你可以改变你的代码如下:
在你的主要:
//printf("%f\n", diff_abs(x,y)); //comment this line
diff_abs(x,y); //just call the function
在函数中:
void diff_abs(float a, float b) { //change the return value to void
//float *pa = &a; //you are not using this variable
float *pb = &b;
float tmp = a;
a = a-b;
b = *pb-tmp;
printf("%.2f\n", a);
printf("%.2f\n", b);
return;
}
关于c - 1.#QNANO在C语言输出中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40789455/