为什么哦为什么这不会给出编译错误? [英] Why oh why does this NOT give a compile error?

查看:54
本文介绍了为什么哦为什么这不会给出编译错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么以下代码在gcc 4.0.2上没有

错误编译?


void f()

{

}


void t()

{

f(1,2 ,3);

f(" 1");

}


我预计至少会有一些警告,但不会即使。这是最新C语言或其他东西的

功能,你可以为没有参数的函数提供
任意参数吗?


Paul


是的,我正在编写正确的文件:


12:58 | paul @ tabu:/ tmpgcc -v

使用内置规格。

目标:i386-redhat-linux

配置:../ configure --prefix = / usr --mandir = / usr / share / man -

infodir = / usr / share / info --enable-shared --enable-threads = posix -

enable-checking = release --with-system-zlib --enable -__ cxa_atexit -

disable-libunwind-exceptions --enable-libgcj-multifile --enable-

languages = c,c ++,objc,java,f95,ada --enable-java-awt = gtk --with-java-

home = / usr / lib / jvm / java- 1.4.2-gcj-1.4.2.0 / jre --host = i386-redhat-linux

线程模型:posix

gcc版本4.0.2 20051125(Red Hat 4.0) .2-8)

12:58 | paul @ tabu:/ tmpcat tc

void f()

{

}


void t()

{

f(1,2,3);

f( 1);

}


int

main()

{

t();


返回0;

}

12:58 | paul @ tabu:/ tmpgcc -ot -W -Wall tc

12:58 | paul @ tabu:/ tmpls -lt

-rwxrwxr-x 1 paul paul 4659 11月5日12:58 t

Can someone explain to me why the following code compiles without
errors on gcc 4.0.2?

void f()
{
}

void t()
{
f(1,2,3);
f("1");
}

I would expect at least some warning, but not even that. Is this a
feature of the newest C dialect or something, that you can provide
arbitrary arguments to a function having no arguments?

Paul

And yes, I''m compiling the right file:

12:58|paul@tabu:/tmpgcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
enable-checking=release --with-system-zlib --enable-__cxa_atexit --
disable-libunwind-exceptions --enable-libgcj-multifile --enable-
languages=c,c++,objc,java,f95,ada --enable-java-awt=gtk --with-java-
home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --host=i386-redhat-linux
Thread model: posix
gcc version 4.0.2 20051125 (Red Hat 4.0.2-8)
12:58|paul@tabu:/tmpcat t.c
void f()
{
}

void t()
{
f(1,2,3);
f("1");
}

int
main()
{
t();

return 0;
}
12:58|paul@tabu:/tmpgcc -o t -W -Wall t.c
12:58|paul@tabu:/tmpls -l t
-rwxrwxr-x 1 paul paul 4659 Nov 5 12:58 t

推荐答案

2007年11月5日星期一下午5:32 Paul Melis< pa ******** @ gmail。写了

文章< 11 ********************** @ 22g2000hsm.googlegroups。 com>:
On Monday 05 Nov 2007 5:32 pm Paul Melis <pa********@gmail.comwrote in
article <11**********************@22g2000hsm.googlegroups. com>:

有人可以向我解释为什么以下代码在gcc 4.0.2上没有

错误编译?


void f()

{

}


void t()

{

f(1,2,3);

f(" 1");

}


我预计至少会有一些警告,但即便如此。这是最新C语言或其他东西的一个

功能,你可以为没有参数的函数提供
任意参数吗?
Can someone explain to me why the following code compiles without
errors on gcc 4.0.2?

void f()
{
}

void t()
{
f(1,2,3);
f("1");
}

I would expect at least some warning, but not even that. Is this a
feature of the newest C dialect or something, that you can provide
arbitrary arguments to a function having no arguments?



这是K& R的一个特征。 C,即C的预标准方言。对于

向后兼容性,它仍然是合法的,但不推荐。


函数中的空参数列表声明告诉编译器

该函数采用一个未指定的数字和类型的参数和

转换参数交叉检查对

的调用声明。


要指定函数不接受任何参数,请在参数列表之间放置void关键字




void f(void){/ * ... * /}

< snip>

It''s a feature of "K&R" C, i.e., a pre-Standard dialect of C. For
backwards compatibility it is still legal though not recommended.

An empty parameter list in a function declaration tells the compiler
that the function takes an unspecified number and type of arguments and
turns of parameter cross-checking of invocations against the
declaration.

To specify that a function accepts no arguments put the void keyword
between the parameter list.

void f(void) { /* ... */ }

<snip>


"桑托什" < sa ********* @ gmail.comschrieb im Newsbeitrag

news:fg ********** @ registered.motzarella.org ...
"santosh" <sa*********@gmail.comschrieb im Newsbeitrag
news:fg**********@registered.motzarella.org...

2007年11月5日星期一下午5:32 Paul Melis< pa ******** @ gmail.comwrote

文章< 11 ********************** @ 22g2000hsm.googlegroups。 com>:
On Monday 05 Nov 2007 5:32 pm Paul Melis <pa********@gmail.comwrote in
article <11**********************@22g2000hsm.googlegroups. com>:

>有人可以向我解释为什么以下代码在gcc 4.0.2上没有
错误编译?
void f()
{
}

void t()
{
f(1,2,3);
f (1);
}

我希望至少有一些警告,但即便如此。这是最新C语言的特征还是什么,你可以为没有参数的函数提供任意参数吗?
>Can someone explain to me why the following code compiles without
errors on gcc 4.0.2?

void f()
{
}

void t()
{
f(1,2,3);
f("1");
}

I would expect at least some warning, but not even that. Is this a
feature of the newest C dialect or something, that you can provide
arbitrary arguments to a function having no arguments?



这是K& R的一个特征。 C,即C的预标准方言。对于

向后兼容性,它仍然是合法的,但不推荐。


It''s a feature of "K&R" C, i.e., a pre-Standard dialect of C. For
backwards compatibility it is still legal though not recommended.



因此我至少会发出警告。在我的编译器上我得到一个


警告(229):函数调用中的参数太多


再见,Jojo

And therefore I''d expect at least a warning. And on my compiler I get one

warning(229): too many arguments in function call

Bye, Jojo


2007年11月5日星期一下午5:45 Joachim Schmitz< no ************ @ hp.com>

写道在文章< fg ********** @ usenet01.boi.hp.com>:
On Monday 05 Nov 2007 5:45 pm Joachim Schmitz <no************@hp.com>
wrote in article <fg**********@usenet01.boi.hp.com>:

" santosh" < sa ********* @ gmail.comschrieb im Newsbeitrag

news:fg ********** @ registered.motzarella.org ...
"santosh" <sa*********@gmail.comschrieb im Newsbeitrag
news:fg**********@registered.motzarella.org...

> 2007年11月5日星期一下午5:32 Paul Melis< pa ******** @ gmail.comwrote
in article< 11**********************@22g2000hsm.googlegroups。 com>:
>On Monday 05 Nov 2007 5:32 pm Paul Melis <pa********@gmail.comwrote
in article <11**********************@22g2000hsm.googlegroups. com>:

>>有人可以向我解释为什么以下代码在gcc 4.0.2上没有
错误编译?

void f()
{
}

void t()
{
f(1,2,3);
f(1);
}

我希望至少有一些警告,但即便如此。这是最新C语言的特征还是什么,你可以为没有参数的函数提供任意参数吗?
>>Can someone explain to me why the following code compiles without
errors on gcc 4.0.2?

void f()
{
}

void t()
{
f(1,2,3);
f("1");
}

I would expect at least some warning, but not even that. Is this a
feature of the newest C dialect or something, that you can provide
arbitrary arguments to a function having no arguments?


这是K& R的一个特征。 C,即C的标准前方言。对于
向后兼容性,它仍然是合法的,但不推荐。


It''s a feature of "K&R" C, i.e., a pre-Standard dialect of C. For
backwards compatibility it is still legal though not recommended.



因此我至少会发出警告。在我的编译器上我得到

一个


警告(229):函数调用中的参数太多

And therefore I''d expect at least a warning. And on my compiler I get
one

warning(229): too many arguments in function call



gcc需要''-Wmissing-prototypes''或''-Wold-style-definition''

切换以产生诊断。这些都不包括在''-Wall''和''-W'中的


gcc needs either ''-Wmissing-prototypes'' or ''-Wold-style-definition''
switches to produce a diagnostic. These are not included even
in ''-Wall'' and ''-W''.


这篇关于为什么哦为什么这不会给出编译错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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