Ask your own question, for FREE!
Computer Science 6 Online
OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (rsmith6559):

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.

OpenStudy (anonymous):

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

OpenStudy (rsmith6559):

Actually, C++ requires that main() return an integer.

OpenStudy (anonymous):

@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

OpenStudy (rsmith6559):

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; }

OpenStudy (anonymous):

@rsmith ur IDE(compiler) please

OpenStudy (rsmith6559):

Editor: vi, compiler: g++

OpenStudy (anonymous):

I m using visual studio 2010,visual studio 2012 & codeblocks as well.but there is not mandatory to return integer.

OpenStudy (anonymous):

u can download visual studio 2010 http://www.microsoft.com/visualstudio/en-in also codeblocks http://www.codeblocks.org/downloads

OpenStudy (rsmith6559):

When has Micro$oft EVER complied properly with standards?

OpenStudy (anonymous):

@rsmith did u try???

OpenStudy (rsmith6559):

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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!