Reputation: 92864
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
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?
Also check out this link.
Upvotes: 7
Views: 1320
Reputation: 23332
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.
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:
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.
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.
Upvotes: 11
Reputation: 612954
If a program in a hosted environment doesn't have a main then it's not C or C++, as per the standards.
Upvotes: 4
Reputation: 55392
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.
Upvotes: 8