Jump to content

Why do people use void main?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
20 replies to this topic

#1
Coder Zombie

Coder Zombie

    Newbie

  • Members
  • PipPip
  • 28 posts
So many times in bad books and online tutorials and so many people's code they use void main.The standard for such a long time now has said main is only an int function.Why do people still do this even when most compilers issue warnings and people online give them grief.Is return 0 really that painful to type???
I'm the master of code rot

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I'm not sure why people would use it. Apparently programmers are lazy (which isn't always a bad thing). I'm fairly certain return 0 is failure though, you should use EXIT_SUCCESS if your program completes successfully.

#3
roboticforest

roboticforest

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts
And who says I'm required to use EXIT_SUCCESS? :-D

I guess viod main -could- be considered easier to explain to noobs, or it could be conservation of vertical text space taken to an extreme. To be honest, I'm not really sure why so many people don't use int main. Being forced to add return 0 is not something I've ever thought of as a bad thing.
Dave

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
As I recall, perhaps incorrectly, void main is OK in C, but not C++. Since many people learn C first, they get into the habit of using void main. It can be a tough habit to break.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
roboticforest

roboticforest

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts

WingedPanther said:

As I recall, perhaps incorrectly, void main is OK in C, but not C++. Since many people learn C first, they get into the habit of using void main. It can be a tough habit to break.

That makes sense. I learned C++ first, so haven't had too many cases where I was doing something "the C way".
Dave

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
One of my frustrations is some of the C++ books that want to teach you C first. C requires you to perform a number of actions a certain way. Then you have to "unlearn" the C way to use the options that C++ provides. Historically, C++ was meant to be maximally compatible with C to make transitioning from C to C++ as easy as possible, but C++ offers so much more than C, I can't see the value in starting with C-shackles on :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
roboticforest

roboticforest

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts

WingedPanther said:

One of my frustrations is some of the C++ books that want to teach you C first. C requires you to perform a number of actions a certain way. Then you have to "unlearn" the C way to use the options that C++ provides. Historically, C++ was meant to be maximally compatible with C to make transitioning from C to C++ as easy as possible, but C++ offers so much more than C, I can't see the value in starting with C-shackles on :)

I guess most tutorial makers don't see it that way. What a lot of people think is that C++ is just C with some extras, not as a different language. C++ is too different from C for people to view it in such a way. Classes alone can make a program look nothing like a C counterpart.
Dave

#8
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Where I have read, void main is an old standard and not used anymore. So I guess that makes int main superior?
Posted Image

#9
roboticforest

roboticforest

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts

Turk4n said:

Where I have read, void main is an old standard and not used anymore. So I guess that makes int main superior?

I guess you could look at it that way. I thought the point of int main was so that the OS had a way to tell if your program had issues. I'm not even sure it really matters anymore these days other than int main is "proper".
Dave

#10
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
I will refer to this, and this, and quote this, void main [...] is not and never has been C++, nor has it even been C.

#11
roboticforest

roboticforest

    Programmer

  • Members
  • PipPipPipPip
  • 110 posts

v0id said:

I will refer to this, and this, and quote this, void main [...] is not and never has been C++, nor has it even been C.

Ha ha! I've read that first one before. I must have forgotten.
Dave

#12
FakeRobotGymnast

FakeRobotGymnast

    Learning Programmer

  • Members
  • PipPipPip
  • 43 posts
void main in acceptable. All it basically says is that you take care of all exceptions internally, and nothing gets passed to the function that calls main (forgot the name, I might look it up later). You can end it with ExitProcess(exitCode); if you like, but it's unnecessary. I stand by my motto of "if it's unnecessary, don't include it" in the case of return 0.