Is it is compulsory to use return 0; in c++ program ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is it is compulsory to use return 0; in c++ program ?

in c language I faced that it is not compulsory to use return 0; rather to use getch ();

2nd Dec 2016, 6:35 PM
rahul kumar
6 Answers
+ 2
Nope, it depends on the header of the main function. If you write it as int main() then you need to return 0 to the OS else if the header is void main(), you don't need to specify return value as it is by default 0. Also, getch() is a function of class 'conio'. It returns the output available on the screen. If not included in the code, you need to open output window separately in order to view output. Please do hit a 'like', if you find answer helpful!
2nd Dec 2016, 6:43 PM
Rahul Soni
Rahul Soni - avatar
+ 1
it is best practice to do so, because it is mandatory! however, some compilers will insert it at the end of your main function if you forget, but not all will. either way it is needed for your program to run correctly. this answer is for c++. i have very little knowledge of c.
2nd Dec 2016, 6:39 PM
NICKALL [EP]
NICKALL [EP] - avatar
+ 1
finally I came to know that it depends on the header file what we are adding.
2nd Dec 2016, 6:47 PM
rahul kumar
+ 1
no simply use.. void main()
2nd Dec 2016, 11:39 PM
Rahul Sharma
Rahul Sharma - avatar
0
it is compulsory to return 0 because the main method has a return type of int. when writing a console application, getch() will wait for the user to input a character before continuing execution, which should pause the screen and stop it from closing immediately
2nd Dec 2016, 6:44 PM
Stefan du Toit
Stefan du Toit - avatar
- 1
not always,but if the header is int main it should return to the operating system
3rd Dec 2016, 7:35 PM
james mussa