|
| |
tom111
|
Different between “void main()” and “int main()” (C++) |
|
Hey, this may sound like a dumb question, but when using C++ I use both of them for the same thing and get the same result, what is the difference and what are they used for (“void” and “int” bit before the block of code). Here is an example of what I mean
#include <iostream> int main() { int iA[2]; iA[0] = 1; iA[1] = 2; std::cout << iA[0] << iA[1] << std::endl; } #include <iostream> void main() { int iA[2]; iA[0] = 1; iA[1] = 2; std::cout << iA[0] << iA[1] << std::endl; } Also I know there is a C++ thread, however it seems dead and I have a much better chance at getting a reply to this. Thanks in advance. ------------------- BSc Computer Science student @ Staffordshire University
| |
quote quick quote edit quick edit del posts in thread report
| |
overKill
|
re: Different between “void main()” and “int main()” (C++) |
main() is a special function that returns to the OS a value signifying if it completed successfully or not (0 being success, any other being typically an error). The OS usually doesn't act on that, but other programs waiting for yours might. The C and C++ standards have specified that main() should return an int-compatible type (which void is not). | |
quote quick quote edit quick edit del posts in thread report
| |
LinkMaster03
|
re: Different between “void main()” and “int main()” (C++) |
|
int main() returns an integer value, void main() returns no value.
------------------- "Give a man a link, and he'll have information for a day, teach him to Google and he'll never bug you again."
The day Microsoft makes a product that doesn't suck, is the day they make a vacuum cleaner. | |
quote quick quote edit quick edit del posts in thread report
| |
Siphon
|
re: Different between “void main()” and “int main()” (C++) |
quote LinkMaster03 Yeah, and just use void main() unless you are going to use the return value for something (you won't yet). | |
quote quick quote edit quick edit del posts in thread report
| |
overKill
|
re: Different between “void main()” and “int main()” (C++) |
quote Siphon
| |
quote quick quote edit quick edit del posts in thread report
| |
LinkMaster03
|
re: Different between “void main()” and “int main()” (C++) |
|
Lol, Siphon.
Just have main() return 0, as overKill said. ------------------- "Give a man a link, and he'll have information for a day, teach him to Google and he'll never bug you again."
The day Microsoft makes a product that doesn't suck, is the day they make a vacuum cleaner. | |
quote quick quote edit quick edit del posts in thread report
| |
Siphon
|
re: Different between “void main()” and “int main()” (C++) |
|
Meh, unless your IDE uses it, you don't need it.
| |
quote quick quote edit quick edit del posts in thread report
| |
Liam
|
re: Different between “void main()” and “int main()” (C++) |
|
It is generally good practice to use int main() and return 0.
| |
quote quick quote edit quick edit del posts in thread report
| |
overKill
|
re: Different between “void main()” and “int main()” (C++) |
quote Liam
But before you read what he said, read what I said so you'll know WHY to use int main() and return 0. The Operating System is expecting a return value, so you have to use int main() and return 0. | |
quote quick quote edit quick edit del posts in thread report
| |
Bigfellahull
|
re: Different between “void main()” and “int main()” (C++) |
|
It is such a bad idea to use void main(). In fact - it could potentially be the mistake you make which will give you (or others) the biggest headache.
Not returning a value could cause stack corruption in the programs exit sequence, causing a... you guessed it... crash. It's also possible that some random value is passed back to the OS. So for example, in a working environment, if a tester was investigating a crash on your code, a main function that returns void, won't be able to guarantee that a non zero return value indicates failure. This can be a major headache inducing scenario. So, please, please, please stick to standards. They are there for a reason. ------------------- ![]() | |
quote quick quote edit quick edit del posts in thread report
| |
| [All dates in (PT) time] | Threads List « Next Newest Next Oldest » |
Powered by neoforums v0.9.8b (equilibrium)
Copyright Neo Era Media, Inc. 1999-2009