why we write int in hello world

in the typical hello world program why do we write int?

1
2
3
4
  int main()
{
  std::cout << "Hello World!";
}


we do nothing with an integer there. so why say int?
Actually we can even omit int and the code still works. If you don't understand why int is there you can choose not to use it.

1
2
3
4
main()
{
  std::cout << "Hello World!";
}
> we do nothing with an integer there. so why say int?

The int returned by main() is a way for a program to return a value to "the system" that invokes it. On systems that doesn't provide such a facility the return value is ignored.
- Stroustrup http://www.stroustrup.com/bs_faq2.html#void-main



> Actually we can even omit int and the code still works

No, it doesn't.

Note also that neither ISO C++ nor C99 allows you to leave the type out of a declaration. That is, in contrast to C89 and ARM C++ ,"int" is not assumed where a type is missing in a declaration. Consequently:
1
2
3
	#include<iostream>

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

is an error because the return type of main() is missing.
- Stroustrup http://www.stroustrup.com/bs_faq2.html#void-main


See:
clang++: error: C++ requires a type specifier for all declarations
g++ (need to demand conformance with this compiler): error: ISO C++ forbids declaration of 'main' with no type
http://coliru.stacked-crooked.com/a/142570a9f22f3f03

Microsoft: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
http://rextester.com/RXAUI71578
Last edited on
Edit: I didn't JLBorges post while I was doing mine :+)

Actually we can even omit int and the code still works.


Well, this is a pedantic warning, nevertheless one shouldn't promote bad habits.

1:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wpedantic]

The main function does return an int to the OS, it's just that the return 0; at the end can be omitted. One can return different values to indicate some kind of failure. This is handy when 2 programs must both run without errors before some other program or script can execute.

Last edited on
many compilers also support
void main()

but this is also bad practice. It was done a lot some years ago and you still sometimes see code that does it.
Last edited on
ketanco wrote:
we do nothing with an integer there. so why say int?

The code that calls main is out of your control. The operating system's program loader has a line of code that is effectively int rc = main(ac, av); and you can't recompile it. It will execute that line of code regardless of whether your program defines the main function is int main(), void main(), or double main() (setting aside that neither 'void main' nor 'double main' are permitted in C++)

The program loader will read an int from wherever ints returned from functions go, always. On popular modern platforms, ints are returned in a CPU register, so the code doesn't crash, it just reads a junk value and makes the program poorly usable in batch files. I remember reading that there exist(ed?) platforms where returned values go on the stack and void main crashes.
Last edited on
Int would be the return type that the function gives. Since you are not returning any value in this function, int is unnecessary.
@ufrnkiddo

That is completely wrong, main does return an int, implicitly.
@TheldeasMan

Really? Thanks for the correction!
Topic archived. No new replies allowed.