Who cares? void main(void) 
Author Message
 Who cares? void main(void)


Quote:
> Hi all,
>    This may cause some flames, but here goes.  I'm relatively new to
>the C language and very interested in learning more.  But, what I don't
>like learning (over and over and over again) is that void main(void) isn't
>a valid syntax for the C language.

Correct.  It is not valid C.

Quote:
>I'm not sure why this is an issue.  

Because people continue using it.

Quote:
>It compiles, it works, it's easy, you don't have to remember to put
>a return 0 or whatever, and most of all - it doesn't seem to make a  
>difference in the program.

Underline 'seem' in that sentance several times.

It doesn't compile - I have at least one compiler/set of options that
will cause it to not compile.

It doesn't work - it kills one of the prime points of any serious OS.

It's not easy - at least, no easier than 'main()', which is also a correct
declaration.

You don't have to put a return(0) - only some compilers will even warn if
you don't, and you can just put 'exit(0)' anyway.

Quote:
>Like I said I'm new to this language so
>maybe I'm missing the C programmers etiquette on this matter but maybe
>we can all give it a rest and concentrate on the more important and
>interesting parts of this language.  God knows there is many.

Perhaps, but this one is, nonetheless, important.  If you get in the habit
of saying 'it works, it compiles, it's easy', then you write code like

char foo[strlen(bar)];

So far as I can tell (one compiler sample space) this works, this compiles,
and this is much easier than using malloc().

Unfortunately, you may not be able to use my code if I do that.

If you declare main() as a void,
1.  You may simply fail to compile, or produce buggy code that won't run,
on some systems.
2.  You may generate meaningless return statii to the environment under any
serious O/S.  This makes your program largely useless to shell scripters,
or anyone else counting on the standard interface to a program.

Quote:
> Dale Moore


-seebs
--

GAT(CS/M/P/SS) d-- H++ s+: !g p? !au a- w+++ v+++/* C++++ UB/V++++ P+ L b+++ !D
3+(NetBSD/Amiga) E- N+++ K !W--- M++/-- V- -po+ Y+ t 5++ jx R G'''' tv- B---
e++ u** h--- f+ r+++ !n x+++* --SeebS-- / The Laughing Prophet


Fri, 18 Apr 1997 03:57:00 GMT  
 Who cares? void main(void)

Quote:


>>But, what I don't like learning (over and over and over again) is
>>that void main(void) isn't a valid syntax for the C language.  I'm
>>not sure why this is an issue.  It compiles, it works ....
>And please note that c.l.c is for discussion about the usage of the C
>language, in a platform independent way, not about C on PC's (this
>remark is related to another posting of yours).

True.  However, it may be worthwhile to point out that void main(void)
is not good practice even on PC's.  It generally results in an
arbitrary value being returned to the calling program, which can cause
errors if that program tests for the return code (for example, calling
your program from makefiles and batch files).

Of course, the practice should also be avoided because it can cause
even worse and undefined problems in other environments (unless there
is a good compelling reason otherwise, like coding for an embedded
system with special requirements).  But perhaps a PC justification
will reduce the need for constantly rehashing this issue.

-ccwf



Fri, 18 Apr 1997 06:37:19 GMT  
 Who cares? void main(void)
[ Editing thanks to inn's rules on posting messages... ]


Quote:
>>>not sure why this is an issue.  It compiles, it works ....

Yeah, on your system, with your compiler, etc. In this case, it shouldn't make
too much of a difference. But I've shot myself in the foot a few times when
writing code in Borland C on the PC that was infact intended for a Unix box
that made use of pointers.. basically, it compiled on both systems fine. Worked
fine on the PC, didn't work that well on the Unix box. I then spent a day or 2
tracing all of the bugs (due to Borland C being nice and friendly when it comes
to null pointers, etc). Now I have code that works on both Unix and Dos... I've
found that just 'cause it compiles, it doesn't mean that it will work... it just
means that the *real* fun is about to begin :)

Quote:
>arbitrary value being returned to the calling program, which can cause
>errors if that program tests for the return code (for example, calling
>your program from makefiles and batch files).

Yep, *very* important... espically if you make use of your programs in an
environment such as unix, where the programs are going to used in a shell
script... but this is not the group to talk of such things :)

Quote:
>system with special requirements).  But perhaps a PC justification

Eck, the *last* thing we want is politically correct code...
Which begs the question, what would politically correct code look like ?

--
Simon



Fri, 18 Apr 1997 21:03:14 GMT  
 Who cares? void main(void)
Wow, my bad form.  I guess to keep the peace I should use
int main().  But, I still think my compiler is the only one that
matters. :)        
--
 Dale Moore



Fri, 18 Apr 1997 22:18:48 GMT  
 Who cares? void main(void)

: >system with special requirements).  But perhaps a PC justification

: Eck, the *last* thing we want is politically correct code...
: Which begs the question, what would politically correct code look like ?

: --
: Simon

Well, for one thing, there could be NO type checking, and absolutly
no discussion of whether variable were signed and unsigned (after
all, it's just a life-style choice).

And I better not catch you comparing any variables to other variables,
they're all doing their best!

Roy

--
-------------------------
Roy Westfall

The views expressed here
are not necessarily shared
by my employer.          



Fri, 18 Apr 1997 23:18:22 GMT  
 Who cares? void main(void)

Quote:
>Wow, my bad form.  I guess to keep the peace I should use
>int main().  But, I still think my compiler is the only one that
>matters. :)        

*Sigh*

But it's not just your compiler.  Just because it compiles doesn't
mean it's working correctly.  You are almost certainly returning a
non-zero exit code to the calling program, which can cause the calling
program, not your program, to fail unexpectedly.  It can cause a make
to fail; it can cause a batch file to fail; it can cause an IF command
you manually type to fail; it can cause any program that calls your
program to fail.

-ccwf



Sat, 19 Apr 1997 11:56:16 GMT  
 Who cares? void main(void)

Quote:
>Wow, my bad form.  I guess to keep the peace I should use
>int main().  But, I still think my compiler is the only one that
>matters. :)        
>--
> Dale Moore

It seems that you will have to use _your_ compiler forever...
I hope that you really _love_ this compiler.

8-)  Francois

/////////  Francois Dufresne                                          ///
///////  Ecole des HEC                                              /////
/////  University of Lausanne                                     ///////



Sat, 19 Apr 1997 21:34:21 GMT  
 Who cares? void main(void)



 s > Eck, the *last* thing we want is politically correct code...
 s > Which begs the question, what would politically correct code look like ?

How's this?

/* pcprog.c: A PC (politically correct) program */

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main (int argc, char *argv[]);

int main (int argc, char *argv[])
{
    int return_status;
    int i;

    if (setlocale (LC_ALL, "") == NULL)
        return_status = EXIT_FAILURE;
    else if (argc != 1) {
        for (i=1; i < argc; i++) {
            (void) printf ("Argument: \"%s\" unrecognized.\n", argv[i]);
        }
        (void) printf ("\nUse: pcprog\n");
        return_status = EXIT_FAILURE;
    } else {
        (void) printf ("Hello, World!\n");

        if (fclose (stdout) != 0)
            return_status = EXIT_FAILURE;
        else
            return_status = EXIT_SUCCESS;
    }
    return (return_status);

Quote:
}

--
|Fidonet:  Thad Smith 1:104/825.14

|
| Standard disclaimer: The views of this user are strictly his own!
| From High Mountain Software & The Bailey Information Exchange BBS
| +1 303 674 0147, Bailey, Colorado, USA 1200-14400 Baud 8N1 24 Hrs
| Gateway Info: Fidonet: 1:104/825.0 (.10), Internet Domain: hms.com


Sun, 20 Apr 1997 09:01:50 GMT  
 Who cares? void main(void)



 s > Eck, the *last* thing we want is politically correct code...
 s > Which begs the question, what would politically correct code look like ?

How's this?

/* pcprog.c: A PC (politically correct) program */

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main (int argc, char *argv[]);

int main (int argc, char *argv[])
{
    int return_status;
    int i;

    if (setlocale (LC_ALL, "") == NULL)
        return_status = EXIT_FAILURE;
    else if (argc != 1) {
        for (i=1; i < argc; i++) {
            (void) printf ("Argument: \"%s\" unrecognized.\n", argv[i]);
        }
        (void) printf ("\nUse: pcprog\n");
        return_status = EXIT_FAILURE;
    } else {
        (void) printf ("Hello, World!\n");

        if (fclose (stdout) != 0)
            return_status = EXIT_FAILURE;
        else
            return_status = EXIT_SUCCESS;
    }
    return (return_status);

Quote:
}

--
|Fidonet:  Thad Smith 1:104/825.14

|
| Standard disclaimer: The views of this user are strictly his own!
| From High Mountain Software & The Bailey Information Exchange BBS
| +1 303 674 0147, Bailey, Colorado, USA 1200-14400 Baud 8N1 24 Hrs
| Gateway Info: Fidonet: 1:104/825.0 (.10), Internet Domain: hms.com


Sun, 20 Apr 1997 09:01:50 GMT  
 Who cares? void main(void)

Quote:


> > Hi all,
> >       This may cause some flames, but here goes.  I'm relatively new to
> >the C language and very interested in learning more.  But, what I don't
> >like learning (over and over and over again) is that void main(void) isn't
> >a valid syntax for the C language.

> Correct.  It is not valid C.

> >I'm not sure why this is an issue.  

> Because people continue using it.

> >It compiles, it works, it's easy, you don't have to remember to put
> >a return 0 or whatever, and most of all - it doesn't seem to make a  
> >difference in the program.

> Underline 'seem' in that sentance several times.

> It doesn't compile - I have at least one compiler/set of options that
> will cause it to not compile.

> It doesn't work - it kills one of the prime points of any serious OS.

[snip]  
> If you declare main() as a void,
> 1.  You may simply fail to compile, or produce buggy code that won't run,
> on some systems.
> 2.  You may generate meaningless return statii to the environment under any
> serious O/S.  This makes your program largely useless to shell scripters,
> or anyone else counting on the standard interface to a program.

> > Dale Moore

> -seebs
> --

> GAT(CS/M/P/SS) d-- H++ s+: !g p? !au a- w+++ v+++/* C++++ UB/V++++ P+ L b+++ !D
> 3+(NetBSD/Amiga) E- N+++ K !W--- M++/-- V- -po+ Y+ t 5++ jx R G'''' tv- B---
> e++ u** h--- f+ r+++ !n x+++* --SeebS-- / The Laughing Prophet

just my 2p's worth.
In AmigaOS (on the commodore machines),  which I use and code (in C) upon,
looking at some of the include files (and the includes in ROM) you will see the
statement :

        void <procedure> (void)

is this bad programming as well?

Even some of the Unix kernals have the above statements in.
I understand your point about return values,  though.
AmigaDOS does use these,  and makes writing scripts somewhat easier!

t.t.f.n

Mark.

--
remember: "My mind functions perfectly on tangents skewed insane"  


   www: http://www.cms.dmu.ac.uk/~se2mg/



Sun, 20 Apr 1997 18:36:39 GMT  
 Who cares? void main(void)
|> just my 2p's worth.
|> In AmigaOS (on the commodore machines),  which I use and code (in C) upon,
|> looking at some of the include files (and the includes in ROM) you will
|> see the statement :
|>
|>   void <procedure> (void)
|>
|> is this bad programming as well?
|>
|> Even some of the Unix kernals have the above statements in.
|> I understand your point about return values,  though.
|> AmigaDOS does use these,  and makes writing scripts somewhat easier!

There is nothing at all wrong with a function which takes no arguments and
does not return anything. The point is that the main() function is not
allowed to be a function which does this. It must return int and nothing but
int. Of course it can take arguments other than void, but I won't get into
that ;-).
--





Mon, 21 Apr 1997 01:38:16 GMT  
 Who cares? void main(void)

Quote:


>: Which begs the question, what would politically correct code look like ?
>Well, for one thing, there could be NO type checking, and absolutly
>no discussion of whether variable were signed and unsigned (after
>all, it's just a life-style choice).
>And I better not catch you comparing any variables to other variables,
>they're all doing their best!

I think short & long should be the first to go !

Along with unnecessary !'s

Rob



Tue, 22 Apr 1997 17:06:33 GMT  
 Who cares? void main(void)

Quote:
>just my 2p's worth.
>In AmigaOS (on the commodore machines),  which I use and code (in C) upon,
>looking at some of the include files (and the includes in ROM) you will see
>the
>statement :
>    void <procedure> (void)
>is this bad programming as well?

Nope.  Not for procedures other than main().  (In fact, for some* cases,
in AmigaDos, main can be a void - but this is not C, anymore, but AmigaDos/
C.)

The problem with declaring *main* as a void is that it's already* defined
not to be.

Consider what would happen if you blithely put

unsigned char *printf(int *ip, char *foo, double x); and then defined a
function which expected to be used that way, and were linked in with
other code.

Bad Things.

Quote:
>Even some of the Unix kernals have the above statements in.
>I understand your point about return values,  though.
>AmigaDOS does use these,  and makes writing scripts somewhat easier!

Oh, sure.  Many functions are legitimately void.  (And thus, are
procedures, not functions, to switch terminology.)  However, main()
in particular is defined by the relevant standard*** to return an
integer, if it returns at all.

-s

Quote:
>t.t.f.n
>Mark.

>   www: http://www.cms.dmu.ac.uk/~se2mg/

-s
--

C/Unix proto-wizard -- C/Unix questions? Send mail for help.


Thu, 24 Apr 1997 12:14:52 GMT  
 Who cares? void main(void)

Quote:


> s > Which begs the question, what would politically correct code look like ?
>How's this?
>/* pcprog.c: A PC (politically correct) program */
>#include <stdio.h>
>#include <stdlib.h>
>#include <locale.h>
>int main (int argc, char *argv[]);

/* the prototype is unecessary when no one else is expected to call you, but
 * a good touch. */

Quote:
>        for (i=1; i < argc; i++) {
>            (void) printf ("Argument: \"%s\" unrecognized.\n", argv[i]);
>        }

Unfortunately, here you invoke potentially undefined behavior.  In particular,
if any of the arguments past the program's name are over 509 characters, some
implementations may fail.

Oh, well.  *almost*.

What you need to do is figure out the longest argument you can safely
print, then use strncpy(), and possibly append '...' to the first 506
characters.

Quote:
>|Fidonet:  Thad Smith 1:104/825.14


-seebs
--

C/Unix proto-wizard -- C/Unix questions? Send mail for help.


Thu, 24 Apr 1997 14:48:16 GMT  
 Who cares? void main(void)


Quote:
>Oh, sure.  Many functions are legitimately void.  (And thus, are
>procedures, not functions, to switch terminology.)  However, main()
>in particular is defined by the relevant standard*** to return an
>integer, if it returns at all.

And it must still be defined as returning an in even f it doesn't return
(so the compiler uses the correct call sequence when main is started).

--
-----------------------------------------


-----------------------------------------



Fri, 25 Apr 1997 21:41:48 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. what's the difference between void main(void) and int main(void)

2. main(void) vs void main ()

3. void main(void) or int main() ??

4. from void (void*) to void (_cdecl*) (void*)

5. Purpose of void main (void) and Functions

6. Is void main(void) a standard or not?

7. void main(void) ????

8. void main(void) illegal??

9. void main(void)

10. Is void main(void) ANSI standard??

11. void main(void)

12. void main(void)....revisited!

 

 
Powered by phpBB® Forum Software