C语言不强调函数前向声明导致的错误

u012410874 2014-06-25 09:53:09
由于C语言并不强制要求函数使用前需声明,所以下面程序在VS2010是没有报错,但在linux下报错,
求各位大神指导下!!!
还有 C语言不进行函数参数检查吗?

#include<stdio.h>
int main()
{
printf("%f\n",add(3.0,4.0));
printf("%d\n",plus(3,4));
return 0;
}
float add(float a,float b)
{
return 3.0;
}
int plus(int a,int b)
{
return a+b;
}

VS2010结果:

ubuntu结果:
gcc -c func.c
func.c: In function ‘main’:
func.c:12:3: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat]
func.c: At top level:
func.c:16:7: error: conflicting types for ‘add’
func.c:12:17: note: previous implicit declaration of ‘add’ was here
注释掉printf("%f\n",add(3.0,4.0));结果正确为7
...全文
326 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
FrankHB1989 2014-06-26
  • 打赏
  • 举报
回复
引用 10 楼 grf123 的回复:
#include<stdio.h>
int add(float,float);    // 这样就叫显示声明了。
int main()
{
	printf("%d\n",add(3.2,4.8));   // C是从上到下编译的,声明之后,编译器会在下面找。前面不声明,它就要默认为int 了。
	return  0;
}

// 这里,叫定义,不叫声明。区分声明和定义。define and declare

int add(float a,float b)
{
	printf("%f\n",a);//结果-0.000000
	printf("%f\n",b);//结果2.150000
	return 3;
}
是定义就不叫声明?扯。
FrankHB1989 2014-06-26
  • 打赏
  • 举报
回复
引用 7 楼 mymtom 的回复:
楼主没有搞清楚状况吧! 谁说C语言不要求前导声明啊,那叫隐式声明。 C99就不说了,二十多年前的C89就说的清清楚楚: If the expression that precedes the parenthesized argument list in a function call consists solely of an identifier, and if no declaration is visible for this identifier, the identifier is implicitly declared exactly as if, in the innermost block containing the function call, the declaration extern int identifier();
什么叫不说了,obselete还无视了?
schlafenhamster 2014-06-25
  • 打赏
  • 举报
回复
是 printf("%f\n",add(3.0,4.0)); int printf( const char* format, ...); 后面 。。。 是 不知道 类型 的
zybjtu 2014-06-25
  • 打赏
  • 举报
回复
需要前置声明的
brookmill 2014-06-25
  • 打赏
  • 举报
回复
这里说的很明显,编译器认为传给%f的是int,也就是默认声明的add的返回值类型 func.c:12:3: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat]
brookmill 2014-06-25
  • 打赏
  • 举报
回复
因为你没有事先声明,c编译器在main里面第一次见到add的时候会采取默认声明,把add作为一个返回int的函数。但是接下来定义的add是返回float,所以就冲突了 func.c:16:7: error: conflicting types for ‘add’ // 这里说的就是类型冲突,16行定义的float的add func.c:12:17: note: previous implicit declaration of ‘add’ was here // 12行默认声明的int的add 虽然c不强制声明,还是都声明了比较稳妥,没必要跟自己跟编译器较劲
baichi4141 2014-06-25
  • 打赏
  • 举报
回复
C语言强制要求函数使用前需声明
Explorerlxz 2014-06-25
  • 打赏
  • 举报
回复
使用函数前都要申明一下!C语言应该是会进行参数检查的,我的看法!
grf123 2014-06-25
  • 打赏
  • 举报
回复
#include<stdio.h>
int add(float,float);    // 这样就叫显示声明了。
int main()
{
	printf("%d\n",add(3.2,4.8));   // C是从上到下编译的,声明之后,编译器会在下面找。前面不声明,它就要默认为int 了。
	return  0;
}

// 这里,叫定义,不叫声明。区分声明和定义。define and declare

int add(float a,float b)
{
	printf("%f\n",a);//结果-0.000000
	printf("%f\n",b);//结果2.150000
	return 3;
}
u012410874 2014-06-25
  • 打赏
  • 举报
回复
长知识了! 返回值默认为int,那隐式声明对函数的参数列表是怎么假设的?如下例子 传入的参数为什么是错误的?难道也默认为int吗?求指导!!
#include<stdio.h>
int main()
{
	printf("%d\n",add(3.2,4.8));
	return  0;
}
int add(float a,float b)
{
	printf("%f\n",a);//结果-0.000000
	printf("%f\n",b);//结果2.150000
	return 3;
}
mymtom 2014-06-25
  • 打赏
  • 举报
回复
楼主没有搞清楚状况吧! 谁说C语言不要求前导声明啊,那叫隐式声明。 C99就不说了,二十多年前的C89就说的清清楚楚: If the expression that precedes the parenthesized argument list in a function call consists solely of an identifier, and if no declaration is visible for this identifier, the identifier is implicitly declared exactly as if, in the innermost block containing the function call, the declaration extern int identifier();

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧