谈谈C语言中函数声明/定义冲突时的处理(VS2010、VC6)

首先,本文中讨论的例子采用C语言,而非C++语言。

使用示例分析这个问题:

例子1

#include <stdio.h>

void hello(int a);
void hello(int a, int b);

void hello(int a)
{
	printf("hello, %d\n", a);
}

void main()
{
	hello(1);
}


在这个例子中,hello()函数声明了两次,定义了一次。

VS2010的编译结果为:

xx.c(4): warning C4031: second formal parameter list longer than the first list
xx.c(7): warning C4029: declared formal parameter list different from definition

VC6的编译结果为:

xx.c(4) : warning C4031: second formal parameter list longer than the first list
xx.c(7) : warning C4029: declared formal parameter list different from definition
xx.c(13) : error C2198: 'hello' : too few actual parameters


例子2

#include <stdio.h>

void hello(int a);
void hello(int a, int b);

void hello(int a)
{
	printf("hello, %d\n", a);
}

void hello(int a, int b);

void main()
{
	hello(1);
}

例子2比例子1多了一个函数声明(红色部分)。

在这个例子中,hello()函数声明了三次,定义了一次。

VS2010的编译结果为:

xx.c(4): warning C4031: second formal parameter list longer than the first list
xxc(7): warning C4029: declared formal parameter list different from definition
xx.c(11): warning C4031: second formal parameter list longer than the first list
xx.c(15): error C2198: 'hello' : too few arguments for call

VC6的编译结果为:

<

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值