Jump to content

return in main function

- - - - -

  • Please log in to reply
5 replies to this topic

#1
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
Hi
what does
return 0;
mean in main() function in a C(++) Console Application? how about
return 1;
or
return 2;
?
are they different?
what if 'return' is removed?
I wrote this ebook! Will you translate it into English for free!?:confused: PM me!

#2
Directive

Directive

    Newbie

  • Members
  • Pip
  • 5 posts
The value returned after main() is an indicator of how the program was terminated.

return 0; is traditionally used to indicate that the program terminated without errors. If the return is omitted, the function will automatically return 0. This is the only case I know of when omitting the return statement is allowed (not including void functions), but I think its best to include it for consistency's sake.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It tells the OS/calling environment whether it terminated in a normal (0) state or a non-normal (other) state.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
irancplusplus

irancplusplus

    Learning Programmer

  • Members
  • PipPipPip
  • 65 posts
how can one get what is returned in main using a batch file?
I wrote this ebook! Will you translate it into English for free!?:confused: PM me!

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
On Unix systems you can with $? (bash), on Windows you can with %ERRORLEVEL%. See this for more info.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Just an FYI, never omit the return statement in main, and never declare main as void. If you want I can explain the details, but it's kinda complicated. Just trust me on this.

These are always acceptable.
int main(int argc, char **argv)
int main()

This is non-standard and will only work on some compilers, but is a well-known extension.
int main(int argc, char **argv, char **env)

Don't do this.
void main( /* Any combinations of the above arguments */ )

sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users