I wanted some information on the return values :

1. Is it always necessary for a return value of main() to be an integer.
2. What happens if the return value of main() is not integer.
3. When a return value other than 0 is returned - does it always indicate that an error has occurred ?
4. If the answer to '3' is yes then what steps does the OS take ?

EDIT: Misspelled Return but can't seem to edit thread name :(

Recommended Answers

All 4 Replies

1. Is it always necessary for a return value of main() to be an integer.

In C, the compiler may allow main() to return a void, but its behaviour is undefined and therefore its highly not recommended. As a reference, the C++ standard explicitly prohibits returning void, and some compilers would ignore your void and convert it to an int returning zero.

2. What happens if the return value of main() is not integer.

Well, if its a void, the consequences of such a program are technically unpredictable, but it is likely it would just return 0. If its a char or any other kind of non-int, assuming the compiler will even allow this, the compiler would probably attempt to cast it into an int, but this all entirely dependant on the compiler. All this speculation is avoided by using the standard int.

3. When a return value other than 0 is returned - does it always indicate that an error has occurred ?
4. If the answer to '3' is yes then what steps does the OS take ?

The meaning of the return value always depends on the context of the call. For instance, if you make a program to run from a batch script, you might want its return integer be a part of a switch of some kind with all non-zero actions that do different things to the script.

Normally, the Operating system will treat zero as good and non-zero as bad, but honestly it doesn't usually care what it returns, unless the process that requested the program to run actually needs to know that information.

Hey i would like to know how i can see the return value of my program under Windows xp.
My IDE- Eclipse CDT
MY Compiler -MinGW

When i run my program in Codeblocks, the ide specifies -'The program terminated with status 0....'

how do i see this in Eclipse/Command prompt ?

Thanks again.

I beleive you do something like this in windows XP. (Since I am logged into Ubuntu right now I cant test this).. Right after you execute the command in a batch program, the ERRORLEVEL environment variable should be assigned the last returned code, so doing this should show you that return code.

ECHO.%ERRORLEVEL%

Look at this link for some good information on the subject:

http://www.robvanderwoude.com/errorlevel.php

When a return value other than 0 is returned - does it always indicate that an error has occurred ?
When program terminates with 0, this means program has been ended correctly as per complier.
else if it ends up with non-zero or value 1 then it means that there is code still awaiting for execution or abnormal termination may occured (like exit() function in c).

It may be due to thread at the run-time that requires 1/0, im not sure about them.
But 0 means everything is cool as per complier else 1 or non-zero means abnormal termination of program or run-time issues affecting programs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.