void main() or int main()?

Pages: 12
@Catfish

The compiler does add a silent return 0; if one is not provided. Thus...
1
2
3
int main()
{
}

... is perfectly legal.
@Albatross

Thank you. Though, your post lacks of arguments, but it has facts. Good one.

The compiler does add a silent return 0; if one is not provided. Thus...
1
2
3
	int main()
{
}



... is perfectly legal.


So as void main add silent value.
Ortonas wrote:
So as void main add silent value.

According to the C++ standard, void main() is not legal C++.
closed account (1vRz3TCk)
According to the C++ standard, void main() is not legal C++.

It is also not valid C.

Edit:
If you use void main() then you are asking for nothing to be returned, so I doubt that it will implicitly return an value (but as it is not standard, who knows what it will actually do).
Last edited on
It will return something. Program itself is also stored in memory and also has some value. Probably the only thing in C++ which literally returns nothing is constructor.
closed account (1vRz3TCk)
Well yes it will return 'something', the type of which is void. The void type is an incomplete type with an empty set of values. That being said void main() should return a void and not int (not that it is valid anyway).


closed account (S6k9GNh0)
No, the main entry point is special, it will always return something, even if it's wrapped over void main() (in which case I'm assuming it always returns 0).
Last edited on
closed account (1vRz3TCk)
the main entry point is special and it has a return type of int. If you have any other return type it is non-standard and thus you can not say with any certainty what the implementer would do.
In libc.so.6, when the function __libc_start_main gets around to calling the main function, it does so with a prototype looking like this:

1
2
3
4
5
6
7
8
extern int BP_SYM (__libc_start_main) (int (*main) (int, char **, char **),
		int argc,
		char *__unbounded *__unbounded ubp_av,
		void (*init) (void),
		void (*fini) (void),
		void (*rtld_fini) (void),
		void *__unbounded stack_end)
__attribute__ ((noreturn));


You'll note that it expects main to return an int.
Topic archived. No new replies allowed.
Pages: 12