Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a C or C++ program that runs without a main() against standards?

Tags:

c++

c

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.

like image 425
Prasoon Saurav Avatar asked Aug 21 '11 19:08

Prasoon Saurav


1 Answers

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.

like image 52
hmakholm left over Monica Avatar answered Nov 15 '22 23:11

hmakholm left over Monica