-3

I came to know that while writing a c program we write "return 0" to tell the os that the program is executed successfully. My question is how can we tell the os while writing the program itself without even executing the program that the program had executed successfully. Can someone tell me what exactly "return 0" does.

Mat
  • 202,337
  • 40
  • 393
  • 406
Jack
  • 17
  • That's not a signal to the OS, but a signal to its parent process (who may or may not use it). – Elliott Jan 13 '22 at 12:06
  • 1
    Doesn't do anything extraordinary. Returning `0` is a convention. `0` means success; other values mean some error (eg `1` means "no memory", `2` means "file not found", ... (*I just made this up; these values do not necessarily correspond to anything already existing*)) – pmg Jan 13 '22 at 12:06
  • Does [this](https://stackoverflow.com/a/2637687/2550406) help? Basically, `return 0` makes the program return a value `0`. If you run it from a shell, for example, you can see what the return value was - it must be some integer. But 0 being used for success is just a convention because zero vs nonzero can be interpreted as a boolean (no error vs error). And for distinguishing kinds of error, you usually need more variants than for kinds of success. – lucidbrot Jan 13 '22 at 12:09
  • You don't really *tell the OS*, you just tell whoever happens to execute your program and looks at the return value. Usually, you would do this `return 0` at the end after successful execution, and if something goes wrong in the middle (e.g. if you need internet access but have none) then you would return a different number as an error code, in an if statement that checks for the problem. – lucidbrot Jan 13 '22 at 12:10
  • you mean 0 means success and 1 means error but how can we know about success and error without even executing the program while writing the program itself. Also, In VS code I haven't seen any changes when we return 0 or 1. – Jack Jan 13 '22 at 12:15
  • `Program returned: 139`. Goddamnit...! It does _something_, it provides you with unhelpful error codes. 139 (nonsense) meaning SIGSEGV (nonsense) meaning invalid access to storage, as in an array/pointer related bug or stack overflow (helpful information for troubleshooting the issue). And this is how we design horrible user interfaces: by displaying mysterious magic numbers instead of textual information regarding the actual error. – Lundin Jan 13 '22 at 12:25
  • 3
    _"without even executing the program that the program had executed successfully"_? Huh? – Jabberwocky Jan 13 '22 at 12:57

3 Answers3

0

The return value of the main function is passed to the exit() function by the C startup code, providing the OS an exit status available to its parent process.

The OS itself does not do anything with this exit status, but shell scripts and other programs invoking multiple processes may use the exit status to determine what to do next. For example the make utility uses the exit status of the commands, eg: when invoking the C compiler, to proceed with the next command or stop the build process with an error status.

Returning 0 or EXIT_SUCCESS at the end of the main function is a convention to tell the calling process that the program completed successfully. It has become implicit in C99 so legacy programs that do not have a return statement can be considered to have completed successfully when recompiled with a C99 compiler. Nevertheless, it is considered good style to provide the exit status explicitly.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

return 0 means returning the integer value 0 from a function, which is generally done inside a program.

You are referring to exit(0), which means that you stop a program successfully. Generally, when you do exit(i) with i an integer value, then that i value is the error code, explaining what happened wrongly. More information can be found here.

Dominique
  • 16,450
  • 15
  • 56
  • 112
0

Once you finish writing your code and compiling it you could execute it wherever you want correct? Once it is finished executing that piece of software you made will return a number (in your example zero).

In most cases that do absolutely nothing because there is nobody actively looking for that return code.

But you might find yourself in situations where you want to execute that software in an automated environment or something like that. let's say a script, that calls your software, and if everything goes ok, it goes on to do something else, but if it is not ok it tries again. That script or other software that is calling your first software has the power to read and interpret that return.

You can also do that manually to actually see that a certain software return code is:

Windows:

  • open cmd
  • run the software
  • run echo Return Code: %errorlevel%

Linux:

  • open terminal
  • run software
  • run echo Return Code: $?

Those pre-defined variables will give you the value your last command returned and you can do something about it.

By convention (as mentioned before) 0 means that everything was ok, and anything other than that indicates that there has been an error. If you write your code with multiple error checks, and when something goes wrong return different values. you will know straight away what went wrong with your execution.