Accelerated C++ Solution to Exercise 0-9

Exercise 0-9

What is the shortest valid program?

Solution

According to chapter 0 (Getting Started) of the book Accelerated C++ …

On Main function:

  • Every C++ program must define exactly one function, named main, that returns an int.
  • The implementation runs the program by calling main.
  • A zero returns indicates success; a non-zero return indicates failure.
  • In general, functions must include at least one return statement and are not permitted to fall off the end of the function.
  • The main function is special: it may omit the return; if it does so, the implementation will assume a zero return value.
  • However, explicitly including a return from main is good practice.

The shortest valid program can therefore be as short as a one-liner like this (or can this be made even shorter I wonder?)

int main() {}

Result

The program compiles okay in Code::Block and gives me the following output the the console output window.

Process returned 0 (0x0)   execution time : 0.141 s
Press any key to continue.

Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000

4 thoughts on “Accelerated C++ Solution to Exercise 0-9”

  1. Hey, just had a question. Why not “void main()” instead of “int main()” ?

Comments are closed.