Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Why return used in c++

Status
Not open for further replies.

Mahammad

Member level 3
Joined
Jul 8, 2011
Messages
56
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,632
Hello,
i am new in programming c++ .i can't get why return 0 is used in main function of c++ .what if i use return 1 (or some other integer ) .please reveal me about it
 

the function main() is the first one called by OS after loading the program.

execution starts from function main().
when the program execution is finished you return to the OS through
return().

by return(0) we are informing OS that everything went on well.

a 0 is 'all well'.
a nonzero number returned is to indicate 'some error has occured in the execution'.

you can refer to your compiler manual , what each return code indicates (to OS).
 
  • Like
Reactions: yura717

    yura717

    Points: 2
    Helpful Answer Positive Rating
When the program terminates, meaning the main() function has ended, it returns a value to the OS and then goes back to the prompt or desktop. The value is technically called the 'errorlevel' and can be any value from 0 to 255. Most programs return zero to mean it finished sucessfullly but you can return any value you want. When back in the OS it is possible to read the errorlevel and take different actions depending on it's value. If you are using Windows, type 'errorlevel' in to the search box on the help screen, it will give you more information.

Brian.
 

Thanks to all , i am having one more doubt that why we are writing int main().what it indicates
 

....why we are writing int main().what it indicates

Just like a normal function definition, the main() function must defined as well. The statement "int main()" is actually short for "int main(void)", which defines a function "main" which has no arguments, hence the "void" in parentheses, and returns a type integer, signified by the "int" in front of the function name. The return type of integer "int" corresponds with the "return" statement initially discussed in your thread.

Code:
[COLOR="#FF0000"]int[/COLOR] main(void)
{
   ...
   ...
   [COLOR="#FF0000"]return(0);[/COLOR] //Can also be written as "return 0;" function main returns an integer "0" and passes execution back to OS
}

Hope the info helps your understanding,

BigDog
 

bigdogguru said:
The statement "int main()" is actually short for "int main(void)"
I don't believe this is true in all cases, I've seen compilers give warnings about not having a return statement when "int main()" is used, with "int main(void)" no warning was issued.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top