Return type for main( ) ??

Why is return type of main( ) always int ??
Why not char or float or something??

See this:

After you read that you will see why it is not possible to return anything other than int
@Smac89 I already went through that stuff.
But i still don't understand why is it not possible?
the return value of main() just tells the state of its successful or unsuccessful run( or compilation) .
so, why can't we use a float value for that??
I don't think you really went through it. Did you encounter this paragraph:

workmad3 wrote:
The valid C++ main signatures are:

int main()
// and

int main(int argc, char* argv[])
// which is equivalent to

int main(int argc, char** argv)

What does that tell you?

cppStandard wrote:
3.6.1 Main function


2 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() { /* ... */ }


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

So to directly answer your question, it is possible to have an implementation of c++ where main returns something other than int. But the standard still requires that the above signatures for main still be present in this implementation
Last edited on
So you mean we can..
But how do we do that?
Since i get error if i try to...
You can if you create your own version of c++ that allows that use. For example some versions of c++ allow the use of void main and then use a different mechanism for specifying the return value of the function (which is still int). So if you are up to the challenge, go ahead and do it but you better have a good reason for reinventing c++ so that main can return types other than int.
Last edited on
ohh.. i see :|
But why don't the versions allow that ??
Basically speaking, the return type of main is int because the standard says it must be int.

I think (someone correct me if I'm wrong) when it says "but otherwise its type is implementation-defined", it means that besides

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

some implementations might also have something like
int main(/* some other args */) { /* stuff */ }
but in any case, the return type has to be int (because the standard says so).

On some systems/compilers, you might be able to write
int main(int argc, char* argv[], char* envp[]) { /* stuff */ }
but that won't necessarily work across all implementations.

void main() is not allowed in proper C++ code, period.
If your compiler will compile it, then it's being especially nice to you.
Try turning on strict ISO C++ conformance to see if it still works. (I hope it doesn't)
closed account (Dy7SLyTq)
because its against the standard. its the same reason why the compilers arent supposed to let you do int x[10];
This question is getting beyond the scope of my knowledge, so I think it is time you posted this on stackoverflow and let the experts set you right. Before that, I think it is worth reading this

@DTSCode, why isn't the compiler supposed to allow one to do that?
Last edited on
okay.. that's what i need to know.. why has the standard not allowed doing that ??
closed account (Dy7SLyTq)
im just guessing here, but lets say you could do char main(). then it would have to be implicitly converted to an int, which is probably its ascii value, which would usually make it a very high number. and what would you return? the point of the int in main is to see how well it executed, like if i made a shell script:
#! /bin/bash

echo | ./myprog;

its more useful when your testing something like ls i guess. there are other things you can use it for though, like returning a random number to the system
> why has the standard not allowed doing that ??

The standard allows two different kinds of C++ implementations.

A hosted implementation is an implementation with full support for both the language and the standard library. This is the kind of implementation that runs under an operating system; the one that is almost always used .

A freestanding C++ implementation is one with language support, but no support the standard library (except for the few mandatory language-support headers). This is the kind of environment that may be present in certain low-end embedded systems.

A freestanding implementation is one in which execution may take place without the benefit of an operating system, and has an implementation-defined set of libraries that includes certain language-support libraries
- IS

In a hosted implementation, main() is the designated start of the program and must conform to the requirements specified by the standard. (One of them being: main() must return int).

In a freestanding implementation, start-up is implementation-defined; there need not be a main() at all; and if there is one, it need not return int.

To put it somewhat simplistically, if you havestd::cout, you must also have main() and it must return int.
Can you provide me an example of "freestanding implementation" ?
Or any link to that?
Arudino comes with a freestanding C++ implementation
See Michael Meissner's post in this thread:;wap2

There are many more examples of freestanding C implementations; for example avr-gcc / avr-libc (used in conjuction with avr-binutils)

All the freestanding implementations that I've seen are based on GCC; but there probably are others as well.

GCC aims towards being usable as a conforming freestanding implementation, or as the compiler for a conforming hosted implementation. By default, it will act as the compiler for a hosted implementation ...

To make it act as a conforming freestanding implementation for a freestanding environment, use the option -ffreestanding; ...

To build an OS kernel, you may well still need to make your own arrangements for linking and startup.

double main( int array1[], double array2[], int array_size1, int array_size_2 )
    const double x = 23.5 ;
    return x + array1[0] + array_size_2 ;

g++-4.8 -std=c++11 -O2 -Wall -pedantic-errors -ffreestanding main.cpp -c && echo ok

Note: It compiles cleanly; but do not try to run this program if it was built using the toolchain for a hosted environment.

g++-4.8 -std=c++11 -O2 -Wall -pedantic-errors main.cpp && echo ok
main.cpp:2:79: error: '::main' must return 'int'
 double main( int array1[], double array2[], int array_size1, int array_size_2 )
main.cpp:2:8: error: first argument of 'int main(int*, double*, int, int)' should be 'int' [-Wmain]
 double main( int array1[], double array2[], int array_size1, int array_size_2 )
main.cpp:2:8: error: second argument of 'int main(int*, double*, int, int)' should be 'char **' [-Wmain]
main.cpp:2:8: error: third argument of 'int main(int*, double*, int, int)' should probably be 'char **' [-Wmain]
main.cpp:2:8: error: 'int main(int*, double*, int, int)' takes only zero or two arguments [-Wmain]
Topic archived. No new replies allowed.