是一个没有main()标准的C或C ++程序吗?

时间:2022-12-05 20:49:27

I am involved in a discussion with a colleague of mine who says it is possible to "run a program" in C and C++ without main and that too in a hosted environment. I said that's completely incorrect as per the Standards. He then asked me to see this link which mentions

我参与了与我的一位同事的讨论,他说可以在没有main的情况下在C和C ++中“运行程序”,在托管环境中也是如此。我说根据标准,这是完全错误的。然后他让我看到这个提到的链接

In several years, an entry was submitted that was so patently absurd that it required a new definition of some of the rules for the next year. This is regarded as a high honor. An example is the world's shortest self-reproducing program. The entry was a program zero bytes in length that if run printed zero bytes to the screen (simply an empty file).

几年来,提交的条目非常荒谬,需要对明年的一些规则作出新的定义。这被视为一种崇​​高的荣誉。一个例子是世界上最短的自我复制计划。该条目是一个零字节长度的程序,如果运行打印零字节到屏幕(只是一个空文件)。

I argued that the solution would not be correct as per C and C++ Standards. What do you guys think about this?

我认为根据C和C ++标准,解决方案不正确。你们怎么看待这个?

Also check out this link.

另请查看此链接。

3 个解决方案

#1


11  

Supposedly there have been some compilers that accepted a program without main() and supplied their own no-op fallback main() from a library. However, such programs are not conforming.

据说有些编译器接受了一个没有main()的程序,并从库中提供了自己的无操作回退main()。但是,这些程序并不符合要求。

The incidence you're referring to was the smr entry of the 1994 IOCCC. However, as published by the contest, it didn't use a C compiler at all! The Makefile stanza for it contained:

您所指的事件是1994年IOCCC的smr入境。但是,正如竞赛所公布的那样,它根本没有使用C编译器!它的Makefile节包含:

smr: smr.c
    @${RM} -rf smr
    ${CP} smr.c smr
    ${CHMOD} +x smr

So it didn't compile it, merely copied the empty .c into an empty shell script.

所以它没有编译它,只是将空的.c复制到一个空的shell脚本中。

The reason why this was not discarded as completely senseless and off-topic by the judges must have been that the empty file is (at least traditionally) a legal compilation unit in C and compiles to an .o without problems if you want to -- it just isn't enough to form a complete program.

之所以没有因为法官的完全无意义和偏离主题而被抛弃的原因一定是空文件(至少在传统上)是C中的合法编译单元并且如果你想要编译成.o而没有问题 - 仅仅形成一个完整的程序是不够的。

#2


8  

main is certainly the standard entry point for C programs, but it's possible for non-standard C programs to start somewhere else, for instance Windows GUI apps tend to start at WinMain instead, or the linker has an /ENTRY directive so you can completely bypass the CRT and start your application with a custom function.

main肯定是C程序的标准入口点,但非标准C程序可能从其他地方启动,例如Windows GUI应用程序往往从WinMain开始,或者链接器有/ ENTRY指令,因此您可以完全绕过CRT并使用自定义功能启动您的应用程序。

#3


4  

If a program in a hosted environment doesn't have a main then it's not C or C++, as per the standards.

如果托管环境中的程序没有主程序,那么根据标准,它不是C或C ++。

#1


11  

Supposedly there have been some compilers that accepted a program without main() and supplied their own no-op fallback main() from a library. However, such programs are not conforming.

据说有些编译器接受了一个没有main()的程序,并从库中提供了自己的无操作回退main()。但是,这些程序并不符合要求。

The incidence you're referring to was the smr entry of the 1994 IOCCC. However, as published by the contest, it didn't use a C compiler at all! The Makefile stanza for it contained:

您所指的事件是1994年IOCCC的smr入境。但是,正如竞赛所公布的那样,它根本没有使用C编译器!它的Makefile节包含:

smr: smr.c
    @${RM} -rf smr
    ${CP} smr.c smr
    ${CHMOD} +x smr

So it didn't compile it, merely copied the empty .c into an empty shell script.

所以它没有编译它,只是将空的.c复制到一个空的shell脚本中。

The reason why this was not discarded as completely senseless and off-topic by the judges must have been that the empty file is (at least traditionally) a legal compilation unit in C and compiles to an .o without problems if you want to -- it just isn't enough to form a complete program.

之所以没有因为法官的完全无意义和偏离主题而被抛弃的原因一定是空文件(至少在传统上)是C中的合法编译单元并且如果你想要编译成.o而没有问题 - 仅仅形成一个完整的程序是不够的。

#2


8  

main is certainly the standard entry point for C programs, but it's possible for non-standard C programs to start somewhere else, for instance Windows GUI apps tend to start at WinMain instead, or the linker has an /ENTRY directive so you can completely bypass the CRT and start your application with a custom function.

main肯定是C程序的标准入口点,但非标准C程序可能从其他地方启动,例如Windows GUI应用程序往往从WinMain开始,或者链接器有/ ENTRY指令,因此您可以完全绕过CRT并使用自定义功能启动您的应用程序。

#3


4  

If a program in a hosted environment doesn't have a main then it's not C or C++, as per the standards.

如果托管环境中的程序没有主程序,那么根据标准,它不是C或C ++。