+ 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)
19 Respostas
+ 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.
+ 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.
+ 9
Well I Am Using Code::Blocks.()
+ 8
thank you both
+ 8
BTW #Mubashir And #Arohi "Code::Blocks" Is Great With It's Interfaces , Plugins And All Other Stuff.
+ 6
what if we dont use this statement?
+ 5
i hv sublime
+ 5
nothing nice m confused which one is better
+ 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).
+ 3
@ ~ swim ~ I mean before Ansi C ^^ but that le not really important as before ANSI, C was just at its beginning
+ 2
I simply use G++ with emacs on linux :)
+ 2
@ ~ swim ~, it was at the beginning, but looooooong loooooooong time ago !
+ 1
You're welcome !
+ 1
@Mubashir, I have Mint 17, but I am not a confident Linux user, rather a beginner ^^
+ 1
if we not use return 0 , than it may return garbage value..
+ 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.
+ 1
which book is best for c programming for basic ???
pls suggest me anyone??
+ 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.
0
hi. I'm new but my name is Ayo Im using my mom's email
could u help me understand c# well