Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed why we use return 0 in int main function

+10 votes
asked Jan 28, 2022 by Dhruv Patel (220 points)
closed Apr 8, 2022 by Admin
closed with the note: answered

6 Answers

+7 votes
answered Feb 5, 2022 by HARSH PALIWAL (300 points)
To terminate the main function because when compiler encounters return 0 , it comes out of the function and the program stops
+2 votes
answered Feb 7, 2022 by Shahriyor Yuldashev (690 points)
There are function types that return and don't return results. main() is function that return result that is 0 (zero). If the console encounter 0, it understands that everything is successfully. You can don't use return 0 also.
0 votes
answered Feb 21, 2022 by RAMBARKI PAVAN KUMAR (140 points)
we use main function for the execution of program if the program executes we use RETURN 0.
0 votes
answered Apr 2, 2022 by userdotexe (1,340 points)

0 is similar to true usually in coding,  so if you write/run return 0, it reads as return true, which is basically the same as "exit with success" in human terms. It could also be seen as "return with 0 errors/flags."

But if there is a mistake in your code, then the compiler/interpreter automatically returns 1, with your error before or after it (usually).

:)

0 votes
answered Apr 2, 2022 by misha356 (220 points)
To let the compiler know if the program ran correctly. But you can try to return 1 instead of 0: this way the compiler will understand that there is an error in the program. The compiler can control the return value. For example: if it does not find a return at the end of the function: it will automatically return 0. Or, before the compilation is completed, it will return 1 if there is an error in the code. Online compilers can also read return: if the compiler returned 1, then there is an error in the code, the online compiler will immediately show where the trouble is, and if it gets 0, it will automatically execute the program and write that main() returned 0
+1 vote
answered Apr 3, 2022 by Deepak kumar (160 points)

return 0; is in int main() function shows that the program executed successfully. It also ensures that what you want to do is done perfectly. It denotes the execution status of the main() function.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...