NZEC Why should my main method have a return 0 statement in C ??

Why should my main method have a return 0 statement in C ??

I will try to give you a brief idea of why we need to add a return 0 at the end based on my limited knowledge. Even i had this doubt last year and this is what i believe must happen .The online judge is a process which keeps running continuously. Whenever a new submission comes , this main process creates another new process with its own allotted memory to handle this submission.

The parent process needs to monitor this child process it has created to provide feedback to the user and detect errors if any.It is easy for the parent to detect errors in the child if some error occurs as these errors will generate a system interrupt. However if no errors are generated, then it is not possible(or may not be easy) for parent to detect that child has completed its execution. The return 0 statement at the end of your program is a message from the child to parent that it has completed execution successfully and the parent can now kill this process and free the memory associated with this process for use by other process. Feel free to correct me if there is anything wrong in this explanation

1 Like

OHK GOT IT…TY :slight_smile:

it’s just a convention in the C and unix worlds to return zero for a normal, successful execution and return a non zero value otherwise. There isn’t really a problem telling when a child process has finished because memory cleanup occurs before the return of control. by the time the parent receives the return code, the child has already died and had it’s memory freed.

I don’t think return value can have much purpose here apart from telling the judge whether or not to fail the code immediately without checking the output.

tldr - you should do it because your code will fail if you don’t.

2 Likes