In c++ we use "return 0;" statement.what is actual purpose of using it. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

In c++ we use "return 0;" statement.what is actual purpose of using it.

i meant when there is no error in programme it must show "0" somewhere.(may be a silly que but m beginner)

18th Jul 2017, 11:52 AM
ⓐⓡⓞⓗⓘ
ⓐⓡⓞⓗⓘ - avatar
19 Answers
+ 12
Answer By @Devvit : Technically, in C or C++ main function has to return a value because it is declared as "int main" which means "main function should return integer data type" if main is declared like "void main", then there's no need of return 0. Some compilers even accept and compile the code even if u dont write return 0. varies from compiler to compiler.
18th Jul 2017, 12:07 PM
Hassan Ashari
Hassan Ashari - avatar
+ 10
Consider: int main() { try { int i = 1 / 0; throw 1; } catch (...) { return 1; } return 0; } It is actually up to you to determine which return value you want to signal that your program has run successfully. In other situations where your program just crashes due to faulty pointers, the value will not be 0. You can look into different implementations of main() and make sure that each main runs successfully. E.g. https://code.sololearn.com/ca0X1CRI8ZoS/?ref=app Try altering one of the main() to crash and see what happens to the return value.
18th Jul 2017, 1:35 PM
Hatsy Rei
Hatsy Rei - avatar
+ 9
Well I Am Using Code::Blocks.()
18th Jul 2017, 12:20 PM
Hassan Ashari
Hassan Ashari - avatar
+ 8
thank you both
18th Jul 2017, 12:12 PM
ⓐⓡⓞⓗⓘ
ⓐⓡⓞⓗⓘ - avatar
+ 8
BTW #Mubashir And #Arohi "Code::Blocks" Is Great With It's Interfaces , Plugins And All Other Stuff.
18th Jul 2017, 12:37 PM
Hassan Ashari
Hassan Ashari - avatar
+ 6
what if we dont use this statement?
18th Jul 2017, 12:02 PM
ⓐⓡⓞⓗⓘ
ⓐⓡⓞⓗⓘ - avatar
+ 5
i hv sublime
18th Jul 2017, 12:24 PM
ⓐⓡⓞⓗⓘ
ⓐⓡⓞⓗⓘ - avatar
+ 5
nothing nice m confused which one is better
18th Jul 2017, 12:28 PM
ⓐⓡⓞⓗⓘ
ⓐⓡⓞⓗⓘ - avatar
+ 4
In fact, when you do not put it and your compiler accept it, it is because your compiler add this line at the end of main The return value of main can be used by another program running yours. It is generally used for error checking (0 : no error, any other value : error).
18th Jul 2017, 12:10 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
@ ~ swim ~ I mean before Ansi C ^^ but that le not really important as before ANSI, C was just at its beginning
31st Jul 2017, 9:56 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
I simply use G++ with emacs on linux :)
18th Jul 2017, 12:16 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
@ ~ swim ~, it was at the beginning, but looooooong loooooooong time ago !
31st Jul 2017, 7:10 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
You're welcome !
18th Jul 2017, 12:13 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
@Mubashir, I have Mint 17, but I am not a confident Linux user, rather a beginner ^^
18th Jul 2017, 12:41 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
if we not use return 0 , than it may return garbage value..
19th Jul 2017, 2:29 AM
RAHUL KUMAR
RAHUL KUMAR - avatar
+ 1
Let's say you have one program to decompress a file and another to open the file. If there is an error in the decompression, the second program should not start running. To ensure that, you can make the second program only be called if the first returns 0. Remember that your program is not the only one being run in the machine and that some systems require different programs, often in different languages, to work properly.
19th Jul 2017, 5:29 PM
Denis Felipe
Denis Felipe - avatar
+ 1
which book is best for c programming for basic ??? pls suggest me anyone??
29th Jul 2017, 8:58 PM
sayed islam safin
sayed islam safin - avatar
+ 1
The return 0 statement is important. If it wasn't, main would return void. You have to consider that C is way more than just the programs you usually write. It ranges from kernels to embedded systems, going trough dekstop apps and tools. Sending signals to state how a program ended execution is important. Specially when your system uses different programming languages for different tools and programs that need to communicate with each other.
31st Jul 2017, 10:26 AM
Denis Felipe
Denis Felipe - avatar
0
hi. I'm new but my name is Ayo Im using my mom's email could u help me understand c# well
5th Sep 2017, 9:16 PM
Esther Fadumiye
Esther Fadumiye - avatar