我有一个 c 函数有一个不正确的行为,我从来没有见过这样的事情也许有人可以告诉我这个完整的谜团
所以在test.c中,函数是:
float wtf(int c)
{
return 0.0;
}
在主程序中
float a=wtf(0);
float b=wtf(2);
float c=wtf(3);
float d=wtf(4);
printf("%f %f %f %f\n",a,b,c,d);
结果:
0.000000 2.000000 3.000000 4.000000
现在为什么应该输出 0 的函数以某种方式输出参数?
这是我在linux下的编译方式
cc -std=c99 -I"/usr/include/pd" -DPD -DVERSION='"0.0"' -fPIC -O6 -funroll-loops -fomit-frame-pointer -Wall -W -g -o "main.o" -c "main.c" -lpng -lbcm2835 -Wno-unused-parameter -Wno-implicit-function-declaration -Wno-unused-variable -Wno-unused-but-set-variable -Wno-comment -lc
有人可以帮帮我吗? 问候
最佳答案
看起来您缺少编译器的 -Wall
参数。有了它你会看到这样的消息:
main.c:3:1: warning: implicit declaration of function ‘wtf’ [-Wimplicit-function-declaration]
哪种方式告诉您问题所在——如果您在尝试调用函数之前没有在编译单元中声明它,编译器将假定它返回 int
——并且在这种情况下,它看起来像 int
返回值恰好存储在与第一个参数相同的位置(可能是某个特定的整数寄存器)。因此,除了获取函数实际尝试返回的返回值(可能在浮点寄存器中),它会将 int
转换为 float,如您所见。
关于c - 在 linux 下,c 函数有疯狂的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36680780/