+ 4

Why do we write int before main?

1st Jul 2020, 3:06 PM
Simran Sinha
Simran Sinha - avatar
9 odpowiedzi
+ 10
Adding to what Aksita G said. Return value of main is used as exit code of the program. If your program executed without any errors then it returns 0 otherwise error code is returned. So in C++ it is illegal to declare main as void, according to latest C++ standards
1st Jul 2020, 3:26 PM
Arsenic
Arsenic - avatar
+ 5
The main() function takes arguments, and returns some value. The program starts executing from this main() function. So the operating system calls this function. When some value is returned from main(), it is returned to operating system The void main() indicates that the main() function will not return any value, but the int main() indicates that the main() can return integer type data. But if we want to terminate the program using exit() method, then we have to return some integer values (zero or non-zero). In that situation, the void main() will not work. So it is good to use int main() over the void main().
1st Jul 2020, 3:11 PM
123
+ 3
Aditya rout actually 0 is for successfull execution. And if the program have error in it then it returns it's error code. If there would have been only 2 possibilities then return type would have been Boolean instead of integer.
3rd Jul 2020, 9:43 AM
Arsenic
Arsenic - avatar
+ 2
It's the return type of the main function.
2nd Jul 2020, 9:19 PM
Sonic
Sonic - avatar
0
•Because the main function returns type integer,i.e either '1/program ran successfully' or '0/program compile error '. •The massage is returned by the ' return ' object of 'ostream ' class.
3rd Jul 2020, 9:38 AM
Aditya rout
Aditya rout - avatar
- 1
In C language only main is used and in C++ int main() is used...
2nd Jul 2020, 9:26 AM
Uzair
Uzair - avatar