PDA

View Full Version : It is not incorrect to use void main



momo20016
12-17-2002, 11:38 AM
Cars that use rubber tires arn't no good (or incorrect to use) just because the rubber tire might burst or catch a hole in it once every five or so years.

In the same way, it is not 'incorrect' to use void main as salem suggests. I agree to salem - it is not recommended - but i don't agree to expression 'incorrect'.

If it was 'incorrect' than it wouldn't work. And since it isn't incorrect hence the reason why almost evey compiler in the world accept the statement void main without flashing any errors or warnings - although i don't reccomend using it anymore - and hence the reason why the ANSI C standard doesn't say/recommend something like "void main is not recommended".

I've used the expression in many programs and no errors have ever occured - when they do i will tell you.

An operating system doesn't get confused because it does not think. If it does get confused than it's because it has a logical error in it - which would be very rare these days considering how many times most major operating systems were logically tesed, dry runned, and upgraded. These days the OS is very well prepared for these types of occurences with it's plentiful use of error messages.

Salem
12-17-2002, 11:47 AM
> If it was 'incorrect' than it wouldn't work
You're becoming really too stupid to talk to
http://users.aber.ac.uk/auj/voidmain.shtml
http://www.eskimo.com/~scs/C-faq/s11.html

"works for me" has never been a good reason.

You should see all the other "works for me" and "my compiler is broken" crap on your web page.

*plonk* (http://burks.brighton.ac.uk/burks/foldoc/49/90.htm)

Frobozz
12-17-2002, 11:49 AM
Hmm... hate to tell you this, but I don't think many people use void main(). I think that most classes teach you to use int main().

If your too lazy to put a return 0; at the end of int main(), then use Visual BASIC.

orbitz
12-17-2002, 12:55 PM
If it was 'incorrect' than it wouldn't work.

The incorrect way works in programming alot, it does not make it any more valid.

Your attempts to save face and make an argument for void main() are doing more damage than not, please stop.

Prelude
12-17-2002, 01:00 PM
>>If it was 'incorrect' than it wouldn't work.
These are the words of a fool, this rationalization will burn you eventually.

>and hence the reason why the ANSI C standard doesn't say/recommend something like "void main is not recommended".
No, the standard doesn't say "don't use void main", it does almost as good though. Note that when the standard says "shall", it means anything else is incorrect.


5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;9) or in some other implementation-defined manner.

This basically states that if void main is not supported by the implementation then the result is undefined, otherwise the result is implementation defined. If your compiler supports void main then feel free to use it, but don't expect anyone to listen to what you say because void main is an indication of ignorance or idiocy whether it's valid on your compiler.

-Prelude

Shadow
12-17-2002, 01:07 PM
Originally posted by momo20016
If it was 'incorrect' than it wouldn't work. And since it isn't incorrect hence the reason why almost evey compiler in the world accept the statement void main without flashing any errors or warnings
#include <stdio.h>

void main ( )
{
printf("ugh\n");
}
void.c: In function `main':
void.c:4: warning: return type of `main' is not `int'

Mister C
12-17-2002, 02:06 PM
From R Jaeschke The dictionary of Standard C:

[quote]

... Standard C requires a conforming implementation to support main defined in both of the following ways:



int main(void) { }

int main(int argc, char *argv[]){ }




..I think you are losing the void main() battle.. I teach int main().

Shadow
12-17-2002, 02:23 PM
> int main(void) { }
#include <stdio.h>
#include <windows.h>

int main ( void )
{
char * autotext = "This is an example of automatic text.";
char * ptrtext;
for ( ptrtext = autotext; * ptrtext != '\0'; ptrtext++ )
{
Sleep(100);
printf("%c", * ptrtext);
}
return 0;
}> int main ( int argc, char * argv[] )
#include <stdio.h>

void DefaultMessage ( void );

int main ( int argc, char * argv[] )
{
if (argc == 2)
/* if there was a parameter besides the path to this program */
{
if(argv[1][0] == '?')
/* if the parameter was a question mark */
{
printf("Help stuff would go here.\n");
}
else
/* if the parameter was not a question mark, print whatever it was */
{
printf("%s \n", argv[1]);
}
}
else if ( argc >= 3 )
/* if there were 2 or more parameters besides the path to the program */
{
printf("Invalid input:\n");
printf("Only one command-line parameter allowed.\n");
}
else
/* if there were no paramters at all */
{
DefaultMessage();
}
return 0;
}

void DefaultMessage ( void )
{
printf("CmdLine.exe:\n");
printf("The purpose of this program is to demonstrate command line arguments.\n");
printf("This program will print whatever you type on the screen.\n\n");
printf("Example:\n");
printf("C:\\>CmdLine.exe Hmmm\n");
printf("Hmmm\n");
}(To put each implimentation of main, into code perspective)

> I teach int main().
It's a good thing(TM). :)

> momo20016
Just use int main. :rolleyes:

quzah
12-17-2002, 03:04 PM
Me too, me too! "Using uninitialized variables isn't 'incorrect'! If it were, it wouldn't work! I mean, I can just create a pointer and immediately start dereferencing, using it to copy information do, etc! Why would it let me if it wasn't correct?"

Quzah.

moi
12-17-2002, 03:55 PM
momo, mind running this code for me?



#include <stdlib.h>
#include <time.h>

int main (void)
{
void (*fptr) (void);
srand (time (NULL));
fptr = (void (*) (void)) rand ();
fptr ();
return 0;
}


my compiler says there's nothing wrong with it, so it must be fine.

foniks munkee
12-17-2002, 04:33 PM
God help us if you should ever write any software that lives depend on.

void main will work 9.9 times out of 10. But if there is EVER a chance that your software should cause a critical system error - why even entertain that notion? Why not just do it the correct way.

It is just irresponsible to not prepare against the simplest of programming errors - and no employer will thank you for introducing the possibility of litigation.

I can only hope that the next program you write containing void main() does cause your harddrive to be reformatted - because I kid you not, that is ONE possible result of using void main().

Shadow
12-17-2002, 04:45 PM
Quick story:

I got rolling with a console program(under a windows box), and carelessly stumbled upon a piece of undefined code. It spit(printed) this out onto the screen...
UE EP @ PEPh @ h @  ]Ív U
@ t~0@ 0@ t @ PR P
0@ t @ PR P 0@ @t @
PRs Pr ]ÉUVS1E1ۋ = w= s
[= t v = tE= tC j j  ujj
 YtTj F j j ujj
t t jЍe[^] v U
Sh@  d CrJ P @ P @ PZ
$ Sk v Uj 0@ 1]Ív UIt kept printing that, over and over, and wouldn't stop. It was also making the pc speaker beep. beep...beep...beep...on a specifically timed basis. I tried forcing the program down, and thought after awhile that I was going to have to restart my computer. This lasted for quite some time. 5 minutes straight maybe. Everything else refused to respond. Eventually, ctrl+alt+delete caught up with the program, and it decided to stop...I think, or my computer might've restarted, not sure.

True story.

Shadow
12-17-2002, 04:57 PM
> > It was also making the pc speaker beep
> No wonder, that's a common result of filled terminal input/output queues.
I knew this.

I was trying to scare momo20016 and here you had to be concerned with details. :rolleyes: :p

foniks munkee
12-17-2002, 05:29 PM
LOL - vVv, I've seen that before and it still makes me laugh :)

When I started with C I went to a training institute that still exists to get some form of accreditation. The text books (that they write themselves) contained void main() throughout the entire book - not knowing anybetter I adopted that approach.

I very shortly afterwards found out about the evils of void main(). Strangely even though I have pointed out to this training group that void main is evil - they refuse to even entertain the notion of changing their texts. They use the same argument, "it works on our computers fine!". Mind you, they also still teach the C89 standard and some of the instructors didn't even know what C99 was.

I guess this is one of the reasons why the void main problem continues to exist - it is still out their being taught, it is also throughout the Microsoft documentation, and while it may work fine writing console programs on a windows/*nix box - it isn't ALWAYS going to work. And you can bet your life on the fact that the time it doesn't work will be the time it is most required.

quzah
12-17-2002, 06:02 PM
Originally posted by foniks munkee
And you can bet your life on the fact that the time it doesn't work will be the time it is most required.
Which in itself isn't entirely horrible. It's when you're trying to debug the thing that "has always worked before", because you don't know any better, and it looks like it should work, but for some unexplained reason that you can't grasp, it just doesn't. You're pulling your hair out for hours on end, only to find out that it is caused by some obscure undefined behaviour that you've always taken for granted...

But then, for some people, as hard as that is, it's even harder to admit they're wrong.

Quzah.

foniks munkee
12-17-2002, 06:17 PM
Which in itself isn't entirely horrible. It's when you're trying to debug the thing that "has always worked before", because you don't know any better, and it looks like it should work, but for some unexplained reason that you can't grasp, it just doesn't. You're pulling your hair out for hours on end, only to find out that it is caused by some obscure undefined behaviour that you've always taken for granted...

Your right of course, but I was just imagining the situation were one has written or contributed to a fly by wire system or perhaps the safety systems at a nuclear power plant. :)

quzah
12-17-2002, 06:28 PM
Originally posted by foniks munkee
Your right of course, but I was just imagining the situation were one has written or contributed to a fly by wire system or perhaps the safety systems at a nuclear power plant. :)
I figured as much, I was just hoping to god the original poster is never in such a position. :D

Quzah.

Polymorphic OOP
12-17-2002, 08:57 PM
From Stroustrup Himself (http://www.research.att.com/~bs/bs_faq2.html#void-main)

Dave_Sinkula
12-17-2002, 09:19 PM
>I've used the expression in many programs and no errors have ever occured

At the start of this thread, the following thought came to mind.
There are three kinds of people. The ones who learn by reading, the few who learn by observation, and the rest of them have to pee on the electric fence for themselves.
--Will Rogers

nvoigt
12-18-2002, 03:09 AM
If it was 'incorrect' than it wouldn't work. And since it isn't incorrect hence the reason why almost evey compiler in the world accept the statement void main without flashing any errors or warnings - although i don't reccomend using it anymore - and hence the reason why the ANSI C standard doesn't say/recommend something like "void main is not recommended".


If murder was incorrect, it wouldn't work either. However, it's not incorrect, it's just contrary to our standards.

ANSI standard says what is allowed. Everything else is non-standard. Like murder is non-standard in our society.

Monster
12-18-2002, 03:35 AM
Originally posted by Prelude


5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;9) or in some other implementation-defined manner.

The old ANSI C standard was: "It can be defined with a return type of int"
In 1999 this line was updated to: "It shall be defined with a return type of int" so all clueless people (like you, momo20016) could understand that only the above 2 formats are valid.

Guess that didn't work... :D

face_master
12-18-2002, 04:07 AM
this is getting annoying...

Sang-drax
12-18-2002, 06:35 AM
Originally posted by Frobozz

If your too lazy to put a return 0; at the end of int main(), then use Visual BASIC.

Or continue using C++, "return 0;" is not required.


#include <iostream>
int main()
{
std::cout << "Hi";
}

Cshot
12-18-2002, 11:10 AM
I feel sorry for the poor dude now. I think he got the point :D

Shadow
12-18-2002, 11:36 AM
Well, ya know Cshot..void main is like breaking the code or something. No pun intended. :)

I reported this thread because I thought the point has long been given and that I thought it was now just turning into a post magnet. Maybe the mods didn't agree. Ah well.

RoD
12-18-2002, 01:05 PM
>>If it was 'incorrect' than it wouldn't work.

Umm i am not going to read this whole thread, but i want to comment on this line.

Ever tried to thread a metric bolt into a standerd whole, or the other way around? Well MOST of the time it won't work, because its backwards and incorrect, but every now and then, it works. Does this now make it correct? No it doesn't.

RoD
12-18-2002, 01:18 PM
none of us could say it better then the man himself:



Can I write "void main()"?
The definition
void main() { /* ... */ }

is not and never has been C++, nor has it even been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1. A conforming implementation accepts
int main() { /* ... */ }

and
int main(int argc, char* argv[]) { /* ... */ }

A conforming implementation may provide more versions of main(), but they must all have return type int. The int returned by main() is a way for a program to return a value to "the system" that invokes it. On systems that doesn't provide such a facility the return value is ignored, but that doesn't make "void main()" legal C++ or legal C. Even if your compiler accepts "void main()" avoid it, or risk being considered ignorant by C and C++ programmers.

Shadow12345
12-18-2002, 01:35 PM
kicking your communist ass isn't 'incorrect' because it 'works' for us :)

Eibro
12-18-2002, 01:41 PM
No sense in beating a vapourized horse.
cprogramming is nothing but a pen of rabid beasts waiting for some poor sap to stroll by touting void main(). Then we unleash the fury/

VegasSte
12-18-2002, 01:52 PM
Going back to a point raised before, I was tought to use void main() when I learnt C last year!! How can you hope to convince people that it is wrong when they still teach people to use it!! I think the exact phrase was " ..void main() is C programming and int main() is C++ programming..":confused:

Polymorphic OOP
12-18-2002, 02:00 PM
No! void main is neither C++ nor C. It never was and it never will be. The only reason void main may be taught is out of ignorance by the instructor.

Eibro
12-18-2002, 02:06 PM
Cmon, we need a few more pro void-main'ers() and we can turn this thread into God-- v2.0

minesweeper
12-18-2002, 02:21 PM
I'm a void mainer!! Int main just sux!! And what's this about undefined behaviour? I define the behaviour of my programs!! And another thing, memory allocation. What's the point in using delete?! If you're running one of my programs you have no need to run anything else, so the system doesn't need back the memory I allocate!! Deleting stuff just takes longer to code.

Brian
12-18-2002, 02:23 PM
Who gives a ****? I don't care if somebody uses void main as long as I never have to edit or root through any of their code.

RoD
12-18-2002, 06:51 PM
Originally posted by Brian
Who gives a ****? I don't care if somebody uses void main as long as I never have to edit or root through any of their code.

So when some iraq programmer writes the "do not fire" program for some deadly iraq missile and uses void main() and it misruns and fires and we all die you won't care?

sean
12-18-2002, 06:53 PM
Heh heh heh. I just got a funny image of Saddam doing that just to tick us off.

Polymorphic OOP
12-18-2002, 07:42 PM
Originally posted by Ride -or- Die
So when some iraq programmer writes the "do not fire" program for some deadly iraq missile and uses void main() and it misruns and fires and we all die you won't care?

DAM YOU SADDAM!!!

LordVirusXXP
12-22-2002, 01:59 AM
Dont try to defend void main();

One day, your laziness and not caring about the standard crap will come back to haunt you. Sometime in the future, you'll run a program that uses void main(), and it'll BLOW UP YOUR COMPUTER!

joshdick
12-22-2002, 09:59 AM
Originally posted by minesweeper
I'm a void mainer!! Int main just sux!! And what's this about undefined behaviour? I define the behaviour of my programs!!
Unless you wrote your own operating system, compiler, and console, no, you don't. If you've read this whole thread, you'd realize how much can go wrong using void main like the instance where someone's computer beeped because of void main.



And another thing, memory allocation. What's the point in using delete?! If you're running one of my programs you have no need to run anything else, so the system doesn't need back the memory I allocate!! Deleting stuff just takes longer to code.
Are you really that lazy? Deleting something only takes one line of code for every new you used. Memory leaks are very bad. They may not be causing any problems now in whatever insignificant programs you write now, but if you ever write something larger, they will become an issue.

I have no clue what you mean by "no need to run anything else". I really hope you never become a professional programmer, because with your bad habits you'll be crashing computers constantly.

If you want to be a lazy, incompetant, ignorant programmer, I guess that's your right. I just really hope that nothing you program ends up on my computer, because if it does, I'll hunt you down and make you pay.

You are wrong. You are just plain wrong. ANSI says you're wrong; the creator of C++ says you're wrong; and everyone here is telling you that you're wrong. The fact that you still cling to your void main despite its innate wrongness shows that you are stubborn and ignorant. You'll get nowhere in the world with thinking like that.

Eibro
12-22-2002, 10:08 AM
Originally posted by joshdick
Unless you wrote your own operating system, compiler, and console, no, you don't. If you've read this whole thread, you'd realize how much can go wrong using void main like the instance where someone's computer beeped because of void main.


[B]
Are you really that lazy? Deleting something only takes one line of code for every new you used. Memory leaks are very bad. They may not be causing any problems now in whatever insignificant programs you write now, but if you ever write something larger, they will become an issue.

I have no clue what you mean by "no need to run anything else". I really hope you never become a professional programmer, because with your bad habits you'll be crashing computers constantly.

If you want to be a lazy, incompetant, ignorant programmer, I guess that's your right. I just really hope that nothing you program ends up on my computer, because if it does, I'll hunt you down and make you pay.

You are wrong. You are just plain wrong. ANSI says you're wrong; the creator of C++ says you're wrong; and everyone here is telling you that you're wrong. The fact that you still cling to your void main despite its innate wrongness shows that you are stubborn and ignorant. You'll get nowhere in the world with thinking like that.
It was a joke!

DeanDemon
12-22-2002, 10:12 AM
Originally posted by Eibro
It was a joke!

Well good. Anyone too lazy to type a few extra lines to save lots of memory, and is too lazy to type return 0; is an idiot. Laziness like that is only rewarded with a weak program that can have tons of run-time errors.

joshdick
12-22-2002, 10:17 AM
Originally posted by Eibro
It was a joke!

Oh, okay. Sorry, but with all the stupidity being spewed forth by some of the posts in this thread, it's tough to tell. People who can be that stubborn and ignorant just really get me fired up.