这是合法的C吗? [英] Is this legal C?

查看:59
本文介绍了这是合法的C吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在C测试中被问到的。以下代码是否合法C?

在C90中是否合法? C99?


#define main()

int main

#define mainbody(){return 0; }

主体

解决方案

Max写道:


这是在C测试中被问到的。以下代码是否合法C?

在C90中是否合法? C99?



是的。是的。


#define main()

int main

#define mainbody(){return 0; }

主体



这是合法的


int main(){返回0;}


是。

-

pete


< blockquote> pete< pf ***** @ mindspring.comwrites:


Max写道:


这是在C测试中被问到的。以下代码是否合法C?

在C90中是否合法? C99?



是的。是的。


#define main()

int main

#define mainbody(){return 0; }

主体



这是合法的


int main(){返回0;}


是。



有一个相当微妙的论点是int main()实际上并不需要支持
,但是出于所有实际目的,它是可以的。

" int main(void)"但是更好。


请注意主要被定义为类似函数的宏,并且mainbody

被定义为类似对象的宏。


编译代码和/或检查预处理器输出(假设你的编译器支持这个
)肯定没有明确回答这个问题,

但它会提供一些很好的提示。

-

Keith Thompson(The_Other_Keith) ks***@mib.org < ; http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


Keith Thompson说:


< snip>


有一个相当微妙的论点,即int main()实际上并不需要支持



我记得之前已经提出过这个问题。我当时并不完全相信

,而且我现在还不完全相信。


没有参数列表的函数定义不带参数因此,
在语义上完全等同于int main(void)。 (注意

,我们不是在谈论这里的定义。)


由于标准只需要main,它等同于int

main(void)或int main(int argc,char ** argv),因为int main()是

相当于int main(void),在我看来那个int main()是完全可以接受的 - 对于编译器来说,至少,即使造型师可能会对b
对象。


< snip>


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


This was asked in a C test. Is the following code legal C?
Is it legal in C90? C99?

#define main()
int main
#define mainbody () { return 0; }
mainbody

解决方案

Max wrote:

This was asked in a C test. Is the following code legal C?
Is it legal in C90? C99?

Yes. Yes.

#define main()
int main
#define mainbody () { return 0; }
mainbody

It''s as legal as

int main(){return 0;}

is.
--
pete


pete <pf*****@mindspring.comwrites:

Max wrote:

This was asked in a C test. Is the following code legal C?
Is it legal in C90? C99?


Yes. Yes.

#define main()
int main
#define mainbody () { return 0; }
mainbody


It''s as legal as

int main(){return 0;}

is.

There''s a fairly subtle argument that "int main()" is not actually
required to be supported, but for all practical purposes it''s ok.
"int main(void)" is better, though.

Note that "main" is defined as a function-like macro, and "mainbody"
is defined as an object-like macro.

Compiling the code and/or examining the preprocessor output (assuming
your compiler supports this) won''t definitively answer the question,
but it will give some good hints.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


Keith Thompson said:

<snip>

There''s a fairly subtle argument that "int main()" is not actually
required to be supported,

I remember that this has come up before. I wasn''t completely convinced
then, and I''m not completely convinced now.

A function definition that has no parameter list takes no parameters, and
is thus precisely equivalent to int main(void) in semantic terms. (Note
that we''re not talking about mere definitions here.)

Since the Standard only requires of main that it be equivalent to int
main(void) or int main(int argc, char **argv), and since int main() is
equivalent to int main(void), it seems to me that int main() is perfectly
acceptable - to the compiler, at least, even though the stylist might
object.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于这是合法的C吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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