Can someone tell me what the advantage is of using Int Main instead of Void Main? And, does Void Main also return a value in the end?:confused: -:confused: -:confused:
This may sound kinda noob but I am:p
Printable View
Can someone tell me what the advantage is of using Int Main instead of Void Main? And, does Void Main also return a value in the end?:confused: -:confused: -:confused:
This may sound kinda noob but I am:p
void main( ) is not valid C or C++. The only valid return type for main is int.
int main is standard (look for Salem's avatar), void main is not (and therefore, incorrect). The return value tells the system (and possibly other programs) the exit status of your program.
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
struct StringData
{
char string[100];
};
void main()
{
StringData data[10];
for (int x=0;x<9;x++)
{
cout<<"Please enter a variable #"<<x<<":";
cin>>data[x].string;
}
cout<<"Data "<<data[1].string<<endl;
cout<<"Data "<<data[2].string<<endl;
cout<<"Data "<<data[3].string<<endl;
system("pause");
}
________________
These are both the same, only once thing is different!
The upper code uses VOID MAIN and the bottom code
use INT MAIN. They both work, no difference at all! Does
it matter what I use??
________________
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
struct StringData
{
char string[100];
};
int main()
{
StringData data[10];
for (int x=0;x<9;x++)
{
cout<<"Please enter a variable #"<<x<<":";
cin>>data[x].string;
}
cout<<"Data "<<data[1].string<<endl;
cout<<"Data "<<data[2].string<<endl;
cout<<"Data "<<data[3].string<<endl;
system("pause");
}
You should always use int main( ), because void main( ) is not valid C++.
OKay, thx for the advice!
I noticed that some programs contain a \n in cout functions.
like this:
cout<<"\nHello";
Why is this? I couldn't find the answer in a tutorial, but I know it is used in other functions.
'\n' is the new-line character. When used with cout, it is roughly equivalent to endl.
That depends on what you mean by "work". On your particular compiler, it may give the results you expect. But just because *YOUR* compiler allows incorrect code to compile doesn't mean that other compilers will.Quote:
Originally posted by FromHolland
These are both the same, only once thing is different!
The upper code uses VOID MAIN and the bottom code
use INT MAIN. They both work, no difference at all! Does
it matter what I use??
void main IS an error. It's not legal C++. It's unfortunate, but many compilers don't report it as an error -- this is a flaw in the compilers, though. It "works" on many compilers as an undesired byproduct of how those compilers implement name mangling. It's not guaranteed to work.
Heck, I've seen compilers that would compile:
but it does NOT mean it will perform as expected, and it does NOT mean the program is legal C++Code:class Node{
/*...*/
};
Node main(Node in){
}
Is this question going to stop someday?!
Almost every month we have a new post about "int main() or void main()", "int main() VS. void main()"....
I think mods should reply to this kind of post with "READ THE FAQ'S", and then close the post!
What do you think...