调用带有过多参数的函数时的GCC行为 [英] GCC behaviour when calling a function with too many arguments

查看:92
本文介绍了调用带有过多参数的函数时的GCC行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现GCC的行为对我来说似乎很奇怪(未经其他编译器检查).

I just noticed a behaviour with GCC that seems strange to me (not checked with other compilers).

如果我编译此代码:

#include <stdio.h>

void foo(int i)
{
  printf("Hello %d\n",i);
}

int main(){
  foo(1, 2);
  return 0;
}

我会得到一个编译器错误:

I will get a compiler error :

test.c:9:5: error: too many arguments to function ‘foo’

但是如果我编译这段代码:

But if I compile this code :

#include <stdio.h>

void foo()
{
  printf("Hello\n");
}

int main(){
  foo(1, 2);
  return 0;
}

我没有收到任何错误或警告.

I get no errors or warnings.

有人可以解释一下为什么吗?

Could someone explain me why ?

我用gcc 4.6.3和arm-none-eabi-gcc 4.8.3测试了

I tested this with gcc 4.6.3 and arm-none-eabi-gcc 4.8.3

我编译所有警告:gcc -Wall test.c

推荐答案

在C语言中,编写void foo()表示foo接受未指定数量的参数.

In C, writing void foo() means that foo takes an unspecified number of arguments.

要指示函数foo()不应包含任何参数,应编写void foo(void)

To indicate that the function foo() should take no arguments, you should write void foo(void)

因此,您还应该使用签名int main(void).

For this reason you should also use the signature int main(void).

这篇关于调用带有过多参数的函数时的GCC行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆