Thread: return value from main

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    91

    return value from main

    i recently found out that it should be only int main() and not void main() as per standards.
    also,i found out that the return value is 0 in case of a normal termination.
    but what is the use of this return value?where can it be used?

    now don't gun me down as usual im just a beginner.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The shell calling the program receives the return value. In Windows console, for example, this value is retrieved through the ERRORLEVEL environment variable. In the BASH shell, it's the $? environment variable.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    In typical bash scripts the variable $? (as mentioned above) is checked after important commands.
    The script continues if it is 0 (or depending upon the program, something else) and handles the errors if it denotes an error.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm pretty sure the C standard does not specify which value (e.g. 0) is for success. I think that's up to the OS/environment, but *nix systems generally use 0 for success. The standard does however define two macros, EXIT_FAILURE and EXIT_SUCCESS that are safe and portable, and probably your best bet. Another thing to note, main is always specified as returning int, but the OS/environment may interpret that as signed or unsigned, and as a char, short or int. E.g.

    Code:
    $ cat foo.c
    int main(void)
    {
        return -1;
    }
    $ gcc -Wall foo.c -o foo
    $ ./foo
    $ echo $?
    255
    Linux interprets the return code as a signed char. Not sure what windows does.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    0 is always success as well.
    Even on those systems (such as VMS) where "success" is a non-zero value.

    However, if you return something else (other than 0 or the two constants), then you will need to check how your environment interprets specific return values.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by theju112 View Post
    i recently found out that it should be only int main() and not void main() as per standards.
    also,i found out that the return value is 0 in case of a normal termination.
    but what is the use of this return value?where can it be used?

    now don't gun me down as usual im just a beginner.
    Actually the minimum C program is..
    Code:
    int main (void)
      {
    
         // your code here
    
        return ErrorCode; }   // typically this is 0
    The returned value in windows can be used by batch files (IF ERRORLEVEL) or by other programs (In the case of CreateProcess()) to judge the sucess or failure of a program. A program that returns a non-0 value might well be considered to have failed.

    Now for the "shooting you down part"... In your first thread, you were told this at least half a dozen times and roundly ignored it.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Another thing to note, main is always specified as returning int, but the OS/environment may interpret that as signed or unsigned, and as a char, short or int. ... Linux interprets the return code as a signed char. Not sure what windows does.
    unsigned int ... so that programs can report windows error codes when returning.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Quote Originally Posted by manasij7479 View Post
    The script continues if it is 0 (or depending upon the program, something else) and handles the errors if it denotes an error.
    this is the exact answer i needed.
    any other value apart from 0 would indicate an error. so the return value is used for error handling.
    Last edited by theju112; 08-19-2011 at 05:47 PM.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by theju112 View Post
    this is the exact answer i needed.
    so any other value apart from 0 would indicate an error. the OS(shell) can now try to solve this error.
    If you return any value other than 0 or EXIT_SUCCESS, that would indicate an error. Note: The actual value returned to the OS is implementation defined.
    Last edited by AndrewHunter; 08-19-2011 at 05:56 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    If you return any value other than 0 or EXIT_SUCCESS, that would indicate an error. Note: The actual value returned to the OS is implementation defined.
    Also note that many return values may not constitue errors... One example is in batch files, where the ERRORLEVEL keyword contains the return value from the process... You might use 0 to continue along, 10 to branch to a different place in the script, 123 might take you some where else...
    A great many programs return values that are not, strictly speaking, errors.

    Another example is a child program hatched with CreateProcess()... it's return value can be used to signal it's parent what to do next using GetExitCodeProcess() in a switch statement... In that case you can have hundreds of possiblities...

    The only true error returned to the OS is EXIT_FAILURE.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. main( ) without return
    By adnilsah017 in forum C Programming
    Replies: 4
    Last Post: 06-05-2011, 08:17 PM
  2. return value from main()
    By megastar in forum C Programming
    Replies: 18
    Last Post: 06-28-2007, 07:27 PM
  3. Why does main need to return a value?
    By jimboob in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2004, 10:48 PM
  4. return to the main()
    By Dummies102 in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2002, 01:23 PM
  5. return from main
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-11-2002, 10:04 AM