我参与了与我的一位同事的讨论,他说可以在没有 main 的情况下在 C 和 C++ 中“运行程序”,并且在托管环境中也是如此。我说过,根据标准,这是完全不正确的。然后他让我看this提到的链接
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).
我认为根据 C 和 C++ 标准,解决方案是不正确的。大家怎么看这件事?
另请查看 this链接。
最佳答案
据说有些编译器接受了没有 main()
的程序,并从库中提供了它们自己的无操作回退 main()
。然而,这样的程序并不合规。
您所指的事件是 1994 年 IOCCC 的 smr
条目。然而,正如竞赛所发布的那样,它根本没有使用 C 编译器!它的 Makefile
节包含:
smr: smr.c
@${RM} -rf smr
${CP} smr.c smr
${CHMOD} +x smr
所以它没有编译它,只是将空的 .c
复制到一个空的 shell 脚本中。
为什么这没有被评委认为完全没有意义和偏离主题而丢弃的原因一定是空文件(至少传统上)是 C 中合法的 编译单元 并编译为.o
如果您愿意,也没有问题——这还不足以构成一个完整的程序。
关于c++ - 在没有 main() 的情况下运行的 C 或 C++ 程序是否违反标准?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7140634/