hello everyone, I'm wondering why do we return a value from a c++ program to OS.Is it necessary to always return a value.
If you are talking about "return 0;" This instruction means if the program exit correctly then the program return 0 to tell the OS that this program exit in a normal way, and I think return -1 to tell the OS that this program didn't exit corectly, Good luck.
In Unix, the shell gets a return value from any program, or even built-in commands. You can see the value by executing: echo $? in BASH. BTW, zero is a successful exit, anything else is a problem.
if u r talking about return 0 then i wanna say that its not necessary but this goes to good programming so that in this way it comes to know that program executed correctly
Actually, C++ requires that main() return an integer.
@rsmith its not necessary.if you write data type of void instead of integer then there is no need to return integer.but because of good programming,main should have return type of integer.Moreover in some compilers like codeblocks and VS2010,if we give integer as return type of main then even regrading to return type,compiler doesn't check that if there is returning anything or not
I thought that returning an int was mandatory in C++. According to "Thinking in C++" it is. Just for fun I wrote a test program and tried to compile it: [zeus:~/Desktop/C++]$ g++ -Wall -o testReturn testReturn.cpp testReturn.cpp:5: error: ‘::main’ must return ‘int’ [zeus:~/Desktop/C++]$ cat testReturn.cpp #include <iostream> using namespace std; void main() { cout << "Hello World" << endl; }
@rsmith ur IDE(compiler) please
Editor: vi, compiler: g++
I m using visual studio 2010,visual studio 2012 & codeblocks as well.but there is not mandatory to return integer.
u can download visual studio 2010 http://www.microsoft.com/visualstudio/en-in also codeblocks http://www.codeblocks.org/downloads
When has Micro$oft EVER complied properly with standards?
@rsmith did u try???
When texts that I trust, and a compiler that I respect say that returning an integer is required by the language, and Micro$oft doesn't, I have no issues thinking that M$ didn't properly comply again.
Join our real-time social learning platform and learn together with your friends!