Cook Computing

void main()

July 18, 2002 Written by Charles Cook

I was puzzled by Ingve's comment on my sample code in the entry on Koenig lookup. It turns out that according to the C++ standard main() must always return int. However most compilers also support void, contrary to the standard which only allows:


int main()

or


int main(int argc, char* argv[])

I used void main() to save a line of code in the sample. However I could have used int main() without a return statement because this is allowed by the standard: if execution falls off the end of main(), it is treated as though a "return 0;" was the last statement. Therefore the following program is valid C++:


#include <stdio.h>
int main()
{
  puts("Hello world");
}