whats the reason behind using int main over void main?
in most books ive read they often discourage the use of void main. but offer little info about it
>whats the reason behind using int main over void main?
Because the standard dictates that every C/C++ program must return a value to indicate its success. It also says that a program must return either 0 or EXIT_SUCCESS if the execution was successful. Declaring main() as void doesn't mean the program won't return a value -- it just means that the value it returns will probably not be 0.
And that's bad because now the operating system thinks that the execution failed. Other horrible things can happen, too. The excuse "it works for me" is not valid because it's quite possible it will eventually screw you up, and you probably don't want to spend hours looking for a bug that came of bad coding habits.
More info:
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376
http://users.aber.ac.uk/auj/voidmain.shtml
To summarize: because the standard says so.
people sometimes prefer int main as it has a return type. and return types are good practice for detecting good/bad returns from a function.
i read some places that it's only possible to pass parameters to ur main if it's return type is int. if that's true, that'd be another reason obviously, as the parameters in main offer useful information on the program being ran/pc specs. which may actually give reason for int main being useful over void main, so other programs can call your main function.
ok. thanks for the good info
Surprising Salem didn't comment on this.. :)
[PS: those wondering why, see his icon/avatar]
>Surprising Salem didn't comment on this..
He probably doesn't want to explain it for the umpteenth time, like me.
Always Remember The GOLDEN RULE that
when any funtion needs to return any value or recieves a value then it should proceed with a type of value it can recieve usually an int
for eg. :
in ur query;
int main()
will recieve any integer value by using return keyword followed by its value ending with semicolon (;)
FOR EG . :
return 0;
here zero (0) is a signal for normal termination for mani() function
now for void
this is the reverse of what i said above...!!!
any funtion that do not need any value to be passed or recieve any value the proceed that function declaration with void keyword
for eg. :
void display()
>Always Remember The GOLDEN RULE that
That's the golden rule? Gold must be pretty worthless these days if all of the vastly superior rules were shoved aside for that one.
>when any funtion needs to return any value or recieves a value then it
>should proceed with a type of value it can recieve usually an int
Um, return and receive tend to be mutually exclusive. You should pick one when talking about return types. I'd recommend "return". "receive" is generally what one means by parameter or argument.
>ending with semicolon (;)
I think it's a a fair assumption that we all know what a semicolon is.
And of course, you didn't answer the question of why int main is preferred over void main. You just made the common mistake of pretending that main is a normal function and propagated the misconception that main's return type is the decision of the programmer. Both of which are false.
i'll probably get yelled at or even -repped, but, @ my university they don't even use a type for main... they just start the main as main(){
... i know this is wrong, but, what is its effect?
some compilers will give errors if you try that
its not the ANSI standard
i tried it in borland's turbo c++ and bloodshed's dev c++... none of both gave me errors... not with main, neither with void main...
Some compilers permit nonstandard syntax when it comes to declaring main(), but that's not the same as saying int main() isn't the standard or that it's standard to use void main() or just plain main(). If you want your code to compile on as many compilers as possible then you should use int main(). Not all compilers will accept void main() or main(). All standard compliant compilers will compile int main().
though not every compiler accepts the same syntax at all... or even the same libraries... for example... i've had a really bad time passing from dev to turbo c++...
> though not every compiler accepts the same syntax at all.
Yes they will.
If you take the trouble to learn the language as defined by ANSI/ISO, then you'll have no problem writing code which will work on any ANSI/ISO compatible compiler.
The reason most people come unstuck is the first time they try to use another compiler (or port their code). At that point, they realise they've been using 'dialect C'. In other words, the 'C' which their compiler will let them get away with, not the proper 'C'.
Continually compiling the same code with multiple compilers set at their max warning levels is a good way of making sure that your code is up to spec. Running lint is a good idea as well if you want that professional touch.
Also bear in mind that some old versions of turbo c++ and nearly all versions of Visual Studio do not adhere to the standard
Well Nichito ,
I believe that ANSI C actually standardized the fact that a function with no return type specified will be taken as int.
I believe thats the reason most compilers let u have it your way with that..
Can't say the same about Standard C++ though..
Also bear in mind that some old versions of turbo c++ and nearly all versions of Visual Studio do not adhere to the standard
the c++ front-end from Edison Design Group is the only implementation to support the complete C++ standard ISO/IEC 14882:2003. see: http://www.edg.com/index.php?location=c_lang
a good test for compiler conformance is to look at the implementation of the Dinkumware C++ library
(a completely conforming implementation of the standard C++ library ISO/IEC 14882:1998, as corrected through 2003.). as per dinkumware,
"As a general rule, you can expect reasonably complete library functionality when using Microsoft Visual C++ compilers from V7.1 onward, GCC compilers from V3.0 onward, and all compilers using the EDG front end. Only EDG compilers offer complete Standard C and C++ conformance (when used with Dinkumware libraries), and only EDG V3.5 and later supports fixed-point arithmetic (TR18037)."
http://www.dinkumware.com/manuals/
current versions of the microsoft c++ compiler use libraries from dinkumware; gcc libstdc++ implementations from version 3.4.0 are also fairly conformant. the recently released Turbo C++ 2006 (sep 2006) includes the Dinkumware C++ libraries and my guess is that the conformance would be reasonable.
>I believe that ANSI C actually standardized the fact that a function with
>no return type specified will be taken as int.
The implicit int rule was removed in the most recent standard. It was a poor practice before, and it's illegal now.
>I believe thats the reason most compilers let u have it your way with that..
Sort of. The latest standard isn't widely implemented yet, so most compilers will be using the previous standard to compile. In that standard, implicit int is still allowed. But the community doesn't recommend it for various good reasons.
thank you very much for the explanation... i've been asking this to all of my teachers and none of them has had a convincing answer...
thnks :)
Thanks Narue for that insight..
But can you provide me a link to the latest standard docs..
That would help me quite a lot..
>But can you provide me a link to the latest standard docs..
The actual standard isn't freely available, though you can buy the PDF from www.ansi.org for $18. However, the draft documents are close enough (with only differences in wording for the most part).
You can find the C89 standard (the old one) here, and the C99 standard (the current one) here. I don't have a handy link to the C++ draft standard, but I'm sure google can give one to you with minimal effort.
Thanks a ton..
You made my day..
I'm working through a text book that does the same thing.
Game Programming All in One by Bruno Miguel Teixeira de Sousa (wow thats a mouthful).
Seems a good text, but I wonder about the choice of using main() as opposed to int main() ?
Well it doesn't compile on Vis C++, stating that C++ does not support assumed int.
Whats the deal? Why would the author recommend that?
Seems a good text, but I wonder about the choice of using main() as opposed to int main() ?
C++ evolved from C89, and C89 supports a feature called implicit int. If the type is not explicitly stated, int is assumed. These two definitions were the same in C89:
main()
{
return 0;
}
int main()
{
return 0;
}
C99 and standard C++ removed that feature, so newer compilers are starting to disallow it completely.
Whats the deal? Why would the author recommend that?
C++ went for a long time before it was standardized. A lot of compilers and books were written before implicit int was removed. Your book was probably written before 1998. That is the risk of using newer compilers with code from older books, but it helps you learn to manage legacy code. :D
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.