Return Value In Main Function

03 June 2014

Programmers’ theoretical minimum: return value in Main function

This is a little test about the most used function in C++ programs: main function.

Consider the code below:

#include <iostream>
int main()
    std::cout << "Can I be compiled?";

The question is: can this code be compile?. Before answer the question, is a good thing resume shortly the characteristics of main. This function is very special. It is called when our program starts, and its return value is checked when the program exit. The return value is extremely important because it indicate how the program is termintated. Conventionally, zero return value represents exit with no errors, while abnormal termination is signalled by a non-zero return value.

It should be note that a void return value in *main function** is explicitly prohibited by the C++ ISO Standard and should never be used. Indeed, the only valid C++ main signatures are:

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

Moreover, the C++ ISO Standard states: flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function

At this point, tt seems reasonable say that the code above cannot compile. So let’s try…

bb2l@host:~/EPC-Lec2$ g++ example1.cpp -o example1

The code has compiled correctly. No errors have been found.

Someone could objects that we haven’t used any option to show warnings, so let’s try again using Wall

bb2l@host:~/EPC-Lec2$ g++ -Wall esample.1 -o example1

Also in this case, we can see that no errors are present.

Why? Isn’t main like any other C++ function?

Well, as said before, main is a really a very special function. One of its particularity is that it can be left without a return value. In this case zero is returned by default.

##Further Information


blog comments powered by Disqus