Hey im new to this C++ business but i just wanted to know cause i find lots or errors coming up to me :'( .. whats the difference between int main and void main.

Recommended Answers

All 11 Replies

I don't know. But most here will tell you to never use void main()

If you don't return a value to the operating system, then whatever happens to be in EAX at the time will probably be returned and this may cause undesirable results. That is why void main () is a definite NO NO!

I don't know. But most here will tell you to never use void main()

Startup routines that call main could be assuming that the return value will be pushed onto the stack. If main() does not do this, then this could lead to stack corruption in the program's exit sequence, and cause it to crash.

I don't know. But most here will tell you to never use void main()

They'll probably tell you to never use goto as well, but that doesn't mean you should just listen to them. Find out why, and then do what is best; and yes, the popular opinion can be wrong sometimes.

main() is a special function that, as mentioned, 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). So, there's both technical and conventional reasons why you should not use void.

well ty guys i think im going to ask my IT teacher ... but he sucks thats why i said to ask here first :)
thx again

They'll probably tell you to never use goto as well, but that doesn't mean you should just listen to them. Find out why, and then do what is best; and yes, the popular opinion can be wrong sometimes..

Most, if not all, professional programmers refuse to use the goto statement, and most companies will not allow it. If you are just learning programming for yourself and for the fun of it then by all means do whatever you want. But if you ever intend to make programming a profession then learn the best forms of coding and what to stay away from. Never use goto and never use void main(). Learn that now the right way and you won't have to unlearn it later in a couple years or so.

Most, if not all, professional programmers refuse to use the goto statement, and most companies will not allow it. If you are just learning programming for yourself and for the fun of it then by all means do whatever you want. But if you ever intend to make programming a profession then learn the best forms of coding and what to stay away from. Never use goto and never use void main(). Learn that now the right way and you won't have to unlearn it later in a couple years or so.

Not all professionals refuse to use goto, or else the Linux kernel stopped being updated some while back. Goto sometimes is very useful; just because it's hard to use properly doesn't mean it should never be used. Personally, I'll probably not come across a need to use one in the next several years, if ever. On the other hand, there's surely some other professional programmers that use them regularly, especially in embedded systems or low-level code. void main() on the other hand has no supporting arguments (the only one I can think of - "it saves keystrokes" - is also inaccurate).

Of course some professionals use goto's occasionally ... with reference to the linux kernel, here's Linus Torvalds' take on goto

http://kerneltrap.org/node/553/2131


Although I would put goto's in the same league as Macros, Unions, Multiple Inheritance, reinterpret_cast, placement new ....

in other words - all things which you should generally avoid unless you really, totally understand what you're doing, where the pitfalls are, and what the alternatives are (And have made a concious decision that the benefits of your chosen technique outweighs the potential problems)

Just because some programmers use goto in kernel programs doesn't mean its acceptable to use it in application programs. I've never written a *nix kernel program or even seen one.

Just because some programmers use goto in kernel programs doesn't mean its acceptable to use it in application programs. I've never written a *nix kernel program or even seen one.

Just because you've never needed it doesn't mean that it should never be used. My original point was simply that one should know both sides of an argument like that, and realize that many people often hear something and pass it on as The Law without questioning it or realizing why it should(n't) be done. Kinda like people using Djikstra's paper discussing the use of goto as an argument saying not to use it, when Djikstra is actually making the opposite point. :icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.