0

I need the simplest possible answer to this. I have looked up in various sites for the answer. If the inputs to a programme is such that the output is 10, will command return 0 force the programme to return the value 0 instead of 10? I have written simple C programmes on Borland IDE without return 0 and they work fine.

ThePhysicist
  • 135
  • 1
  • 1
  • 4
  • 5
    [Here you'll find a good explanation](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c). – Maroun Jan 04 '14 at 10:29

5 Answers5

4

Nothing to add to Maroun's link. That's the way if you want to know more about return values.

Just a few words for a novice:

int main()
{
  return 0;
}

and

int main()
{
  return 22;
}

and

void main()
{

}

will all behave the same way if you're not checking the return value for a program and compile on most default-set compilers.

When you execute a program on the command line (windows) or shell (linux), you can check whether that program failed or completed its tasks successfully by checking the return value, that is, exactly that value you're returning.

Following the standard is a good habit by the way, and I therefore strongly recommend it to you.

Marco A.
  • 43,032
  • 26
  • 132
  • 246
1

I assume you're talking about the main() function: If you return anything else than 0 (which is the same as omitting return altogether) this is assumed to be an error code, and can be used to notify other programs that the current program failed to do what it is supposed to do.

Njol
  • 3,271
  • 17
  • 32
1

The main function is generally supposed to return a value and after it returns something it finishes execution.The return 0 means success and returning a non-zero number means failure. Thus we "return 0" at the end of main function. But you can run the main function without the return 0.It works the same . Sources : What is the proper declaration of main?

Community
  • 1
  • 1
Sunil Kumar
  • 6,112
  • 6
  • 36
  • 40
1

The return code of the main function is passed back to the operating system. What it does with this depends. If you execute a program on the commandline you can simply ignore it. In scripts this returncode can be used to determine how to proceed.

If you use void main() it will return 0.

Here is an example for DOS:

rem test.bat
@echo off
MainReturnValTest
@if "%ERRORLEVEL%" == "0" goto good
:fail
    echo Execution Failed
    echo return value = %ERRORLEVEL%
    goto end

:good
    echo Execution succeeded
    echo Return value = %ERRORLEVEL%
    goto end

:end

The same can of course be done on other OSes using different syntax. For example in Linux you could use:

myprogram && echo success

which would only print success if the returncode was 0.

The operation of your program is not affected of course.

Devolus
  • 21,661
  • 13
  • 66
  • 113
1
  1. The return value has nothing to do with your Output value . That is, even if your output is 10, you return 0.
  2. Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there are some exceptions.
PG1
  • 1,220
  • 2
  • 12
  • 27