#include <stdio.h>
void main() {
extern int fun(float);
int a=fun(3.5);
printf("%d",a);
}
int fun(aa)
float aa;
{
return ((int)aa);
}上面提到的代码块在我的Visual 8编译器上编译得很好,尽管输出是一个垃圾值。但是,当我在gcc-4.3.4上编译相同的代码时,我得到了以下编译错误:
prog.c:2:警告:返回‘main’类型不是‘int’ prog.c:8:错误:为“乐趣”而冲突的类型 prog.c:3:错误:之前的“乐趣”声明就在这里
当它具有下列属性时,它将如何工作:
发布于 2011-12-10 07:30:19
该函数是用K&R风格编写的,您的原型是不正确的。事实上还有其他的问题..。
#include <stdio.h>
void main() {
extern int fun(float);
int a=fun(3.5);
printf("%d",a);
}
int fun(aa)
float aa;
{
return ((int)aa);
}main()的返回类型是int,至少在标准C中是这样的。您的print语句应该包含一个换行符。
如果fun()函数是用原型编写的,那么原型就可以了:
int fun(float aa) { ... }然而,该函数是用K&R风格编写的,因此该函数期望被传递一个double,并将其转换为float。
int fun(double aa_arg) { float aa = aa_arg; ... }在K&R C中,所有float值都以double形式传递。这就是为什么您要得到垃圾;您正在(可能是无意中)欺骗您的编译器,并且它通过对您执行GIGO来得到它自己的回报。
FWIW: GCC 4.6.1拒绝编译您的代码(即使没有任何警告设置)。它抱怨说:
f1.c: In function ‘main’:
f1.c:2: warning: return type of ‘main’ is not ‘int’
f1.c: At top level:
f1.c:9: error: conflicting types for ‘fun’
f1.c:3: error: previous declaration of ‘fun’ was here您可以通过多种不同的方式解决这个问题:
#include <stdio.h>
int main(void)
{
extern int fun(float);
int a = fun(3.5);
printf("%d\n", a);
return(0);
}
int fun(float aa)
{
return ((int)aa);
}或者:
#include <stdio.h>
int main(void)
{
extern int fun(double);
int a = fun(3.5);
printf("%d\n", a);
return(0);
}
int fun(double aa)
{
return ((int)aa);
}或者:
#include <stdio.h>
int main(void)
{
extern int fun(double);
int a = fun(3.5);
printf("%d\n", a);
return(0);
}
int fun(aa)
double aa;
{
return ((int)aa);
}或者:
#include <stdio.h>
int main(void)
{
extern int fun(double);
int a = fun(3.5);
printf("%d\n", a);
return(0);
}
int fun(aa)
float aa;
{
return ((int)aa);
}或者:
#include <stdio.h>
int main(void)
{
extern int fun();
int a = fun(3.5);
printf("%d\n", a);
return(0);
}
int fun(aa)
float aa;
{
return ((int)aa);
}我相信这些都是正确的,它们都应该在没有警告的情况下编译(除非您要求编译器抱怨旧风格(K&R)函数定义等等)。
GCC变得相当挑剔,我得到了警告:
/usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition f2.c -o f2
f2.c:12: warning: no previous prototype for ‘fun’
/usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition f3.c -o f3
f3.c:12: warning: no previous prototype for ‘fun’
/usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition f4.c -o f4
f4.c:12: warning: function declaration isn’t a prototype
f4.c: In function ‘fun’:
f4.c:13: warning: old-style function definition
/usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition f5.c -o f5
f5.c:12: warning: function declaration isn’t a prototype
f5.c: In function ‘fun’:
f5.c:13: warning: old-style function definition
/usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition f6.c -o f6
f6.c: In function ‘main’:
f6.c:5: warning: function declaration isn’t a prototype
f6.c: At top level:
f6.c:12: warning: function declaration isn’t a prototype
f6.c: In function ‘fun’:
f6.c:13: warning: old-style function definition发布于 2011-12-10 07:10:30
这是函数定义的K&R风格。这是不符合标准的,因此在gcc(这是由Ideone使用)编译器错误。
它在visual studio中适用于您,因为visual studio允许您更改默认的编码样式。
https://stackoverflow.com/questions/8455045
复制相似问题