我的程序中有以下代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int print_numbers_above(double x, float y, int z)
{
printf("x=%lf, y=%f, z=%d\n", x, y, z);
}
int main()
{
double x = 1.25;
float y = 1.25;
int z = 3;
print_numbers_below(x, y, z);
print_numbers_above(x, y, z);
return 0;
}
int print_numbers_below(double x, float y, int z)
{
printf("x=%lf, y=%f, z=%d\n", x, y, z);
}
输出:
x = 1.25,y = 0.000000,z =垃圾
x = 1.250000,y = 1.250000,z = 3
现在,我知道函数print_numbers_below()应该先声明(或者应该在main之前定义)。 但它并没有引发错误。它假定它是一个extern函数(因为它返回int,所以没有任何返回类型的冲突)。 现在,我无法理解为什么我不能正确传递超过1个参数?
(我使用的是Visual Studio 2013)
答案 0 :(得分:6)
合理的编译器会给你以下错误:
foo.c:12:5: warning: implicit declaration of function 'print_numbers_below' is invalid in C99 [-Wimplicit-function-declaration]
print_numbers_below(x, y, z);
^
foo.c:16:5: error: conflicting types for 'print_numbers_below'
int print_numbers_below(double x, float y, int z)
^
foo.c:12:5: note: previous implicit declaration is here
print_numbers_below(x, y, z);
^
答案 1 :(得分:3)
当你调用函数编译器没有看到原型时,它会将default argument promotions应用于所有参数。 Chars和短裤被提升为整体,并且花车被提升为双打。当print_numbers_below
实际上想要浮动时,这会中断。
如果尚未定义该功能,请始终使用原型。