Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c++ setup

Status
Not open for further replies.

denisfinn

Programmer
Jun 7, 2003
49
US
hey all!

the normal way i set up c++ programs is:

#include <iostream>

int main()
{
return 0;
}

someone told me that I should be doing:

#include <iostream>

VOID main()
{
return 0;
}

the difference being void in the second one.

can someone tell me the true way? (or best)
 
Are you sure the code of the second way can compile?
It tries to return an int value but the main was declared to return void (why uppercase?)

The first way is a correct one, and usually there is a bit more to add :
Code:
...
int main (int arg_count, char** args)
{
  ...
  return 0;
}

This way you can handle command-line arguments that were passed to the compiled application.


--
Globos
 
> someone told me that I should be doing:
>
> #include <iostream>
>
> VOID main()


That person is absolutely wrong.


Especially if this is an instructor, please point them to:


and help stop the spread of this misinformation.


Note that the link above is to a FAQ written by the inventor of C++, so yes, it's reputable.
 
yeah someone had told me the second way was the correct way and i got worried when it wouldn't compile

can anyone recommend a book for C++? i've been using Sam's Teach Yourself C++ in 24 Hours and I'm beginning to think that it may not be so good for a learner....

thanks!
 
void main() with lower is an old C style that wasn't very helpful. The 0 returned in toy programs does do much, but tell the OS that the program finished properly. Exiting with non-zero can be meaningful to the OS, spesifically to the Shell. So, c++ should let you compile with out a integer return on the main function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top