1
votes

As per ISO_14882_2014

3.6 Start and termination [basic.start]

3.6.1 Main function [basic.start.main]

2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a declared return type of type int, but otherwise its type is implementation-defined.

but otherwise its type is implementation-defined. That means compiler may have its own return type for main(). Can you point me to the location where GNU G++ compiler says about main()'s return type.

2
The "otherwise" applies to the parameter types IIUC - StoryTeller - Unslander Monica
In what you found it states It shall have a declared return type of type int. That is a full stop. main always has to return an int because of that sentence. It's the parameters that are implementation defined. - NathanOliver
I see. the comma (,) after return type of type int, is misleading. - Ujjwal
@UjjwalKumar Not really. "but otherwise" means "except for this", not "alternatively". - Max Langhof

2 Answers

4
votes

The return type must be int; the "its type" that follows refers to main, not to the return type. That is, the arguments that main takes are implementation defined. Each implementation must, however, provide at least int main() and int main(int, char**).

1
votes

Assuming i386 architecture. From glibc start.S we call __libc_start_main. From glibc csu/libc-start.c __libc_start_main we call main with the pointer type int (*main) (int, char **, char **) (the MAIN_AUXVEC_DECL shouldn't be defined on i386 I think, only for powerpc). So the implementation defined type of main function is int main(int, char **, char **). Because of how stack works on i386, the forms int main(void) and int main() and int main(int) and int main(int, char**) will work. The main return type is int.