Utkarsh Srivastav
Utkarsh Srivastav

Reputation: 3227

What happens if main() does not return an int value?

I know that in C compilers the main() function is called by the _start() function which has code something like this:

exit(main()); // return value of main is returned

How does _start() work when main() does not return int, for example if its return type is void, float, or something else?

Upvotes: 23

Views: 9193

Answers (8)

bwDraco
bwDraco

Reputation: 2542

Assume we're using Visual Studio 2012.

For C++ programs, Visual Studio allows void to be specified as the return type, even though this is forbidden by the C++ standard. Under the standard, main() must return an int in hosted implementations.

For C programs, any return type is allowed for main(), but returning something other than an int results in unspecified behavior. For example, under Visual Studio 2012, returning 0.0 from double main() results in a return value of 0xcccccccc when the program is run in the debugger (see In Visual Studio C++, what are the memory allocation representations?).

Upvotes: 0

Soren
Soren

Reputation: 14718

The C-standard does not allow you to return any other value than int or void -- the c-compiler specifically test for the signature of main to make sure it is compatible.

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263647

The C standard never mentions this _start function; I don't believe C++ does either.

In C prior to the 1999 ISO standard, if execution reaches the end of main() without executing a return statement, or executes a return statement that doesn't specify a value, then "the termination status returned to the host environment is undefined". In practice, I've seen implementations where such a program returns a status of 1 (failure), or some arbitrary value in memory such as the result of the last function that was called.

The 1999 ISO C standard changed this: "reaching the } that terminates the main function returns a value of 0". This matches the rule that C++ has had at least since the first ISO C++ standard in 1998.

(As a matter of style, I prefer to have an explicit return 0; at the end of main, even if it's not strictly required. This is consistent with int functions other than main, and it makes for better portability to pre-C99 C compilers.)

All this assumes that main is defined with a return type of int. That's the only type that's specifically supported by the C standard (either int main(void) or int main(int argc, char *argv[]) or equivalent), but (hosted) implementations may support other implementation-defined definitions. The C90 standard doesn't explicitly cover this case, but C99 says, "If the return type is not compatible with int, the termination status returned to the host environment is unspecified."

The C++ standard is a bit different. For hosted implementations, main must be defined to return int. The parameters are implementation-defined, but both the standard forms of C must be supported.

For a hosted implementation in either C or C++, there is no good reason I know of to define main with a return type other than int. Just use one of the two standard definitions, and the question won't arise.

For "freestanding implementations", "the name and type of the function called at program startup are implementation-defined". So the entry point might legitimately return void or something else, and it might not even be called main. Note that a "freestanding implementation" is one "in which C program execution may take place without any benefit of an operating system", typically an embedded system.

Upvotes: 16

Alok Save
Alok Save

Reputation: 206656

If the return type of main is not an int then the return value is implementation defined.
In short an Implementation is allowed to have different return type than int for main but none of the known implementations support anything other than int.
Ideally, You will need to refer to the documentation of your platform and the compiler to see what exact behavior it defines because it is allowed to have the flexibility do so by the standard.

Reference:

C++03 Standard:

3.6.1 Main function [basic.start.main]

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

int main() { /* ... */ }

and

int main(int argc, char* argv[]) { /* ... */ }

.....

Upvotes: 2

dreamlax
dreamlax

Reputation: 95405

Standard implementations of C expect main to return int just because it is defined that way in the C standard. Returning something other than int (or a type compatible with int) usually results in undefined behaviour—meaning there is no way to tell what will happen.

However, there are non-standard implementations of C, for example, the Plan 9 operating system uses void main(), here is a list their utilities' source code. Plan 9 C code is quite a bit different to K&R, ANSI, C99 or C11. Here's a link explaining how Plan 9 uses the C language.

Upvotes: 2

Rob Kennedy
Rob Kennedy

Reputation: 163357

If main doesn't return int, then you have an ill-formed program and behavior is undefined. Anything can happen. Your program might crash, or it might run as though nothing were wrong at all.

Let's suppose main returned something other than int, and your compiler and linker allowed the program to be made. The caller doesn't know that, though. If the caller expects returned int values to be returned in the EAX (Intel) register, then that's what it will read to determine the return value of main. If your faulty main stored a float value there, then it will be interpreted as an int instead. (That doesn't mean it will get truncated. It means the bits making up the layout of a floating-point value will instead make up an int instead.) If your faulty main returned void, then it didn't store anything in the expected register, so the caller will get whatever value was previously stored in that register instead.

If your main returns some type that it expects to store someplace that the caller didn't' reserve memory for (such as a large struct), then it will end up overwriting something else, perhaps something important to the clean shutdown of the program, causing your program to crash.

Upvotes: 15

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727127

In C++ it would be a compile error to return anything other than int from main():

error: ‘::main’ must return ‘int’

In C it is a warning, you will get a float reinterpreted as an int: for example, 2.1F would be reinterpreted as 224.

Upvotes: 7

Puppy
Puppy

Reputation: 147054

The function will return an implementation-defined value. For example, in C++, main implicitly returns 0. In this case of a void main then this would simply be returned by _start. However, there are virtually no implementations that would allow any arbitrary return type- it's baked into the operating system that a process exits with an integral value.

Upvotes: 8

Related Questions