void main(void) 
Author Message
 void main(void)

Somewhere Down the road, I was told that void main(void) or even void
main() was incorrect programming.  I am very curious about this, because I
am a CSC major in college and they are teaching void main(void)?

Can anyone please clairfy this to me.

Thanks
Darrell



Sat, 03 Jul 1999 03:00:00 GMT  
 void main(void)

Quote:

>Somewhere Down the road, I was told that void main(void) or even void
>main() was incorrect programming.  I am very curious about this, because I
>am a CSC major in college and they are teaching void main(void)?
>Can anyone please clairfy this to me.

The questions called "Can I declare main() as void, to shut off these
annoying "main returns no value" messages?", "I believe that declaring
void main() can't fail, since I'm calling exit() instead of returning,
and anyway my operating system ignores a program's exit/return status.",
and "The book I've been using, _C Programing for the Compleat Idiot_,
always uses void main()." from the FAQ should make it clear enough that
"void main()" is not such a good idea. If you have questions on the
wording or meaning of the FAQ, feel free to post them, after reading
the FAQ.

Kurt

--
| Kurt Watzka                             Phone : +49-89-2180-6254



Sat, 03 Jul 1999 03:00:00 GMT  
 void main(void)


Quote:

> Somewhere Down the road, I was told that void main(void) or even void
> main() was incorrect programming.  I am very curious about this, because I
> am a CSC major in college and they are teaching void main(void)?

> Can anyone please clairfy this to me.

The following program

   void main (void) { }

causes undefined behavior. That means if you ask your compiler to compile
it, anything can happen without violating the ANSI C standard. "Anything
can happen" means among other things that the compiler can complain about
this program, that the compiler can compile it without complaining, or
that the compiler crashes and formats your harddisk.

If the compiler compiles this program and you execute it, again anything
can happen without violating the ANSI C standard. "Anything can happen"
means among other things that nothing bad happens, or that your computer
crashes, or that your harddisk is formatted.

On one of the platforms that I am working on, running the program

   void main (void) {}

stops Shell scripts from working (dont ask me what that means, but they
should work so the program is _bad_ ) while

   int main (void) { return 0; }

which is a correct program doesnt stop them from working. The reason why I
mention is that some teacher might tell you "well it isnt legal C but it
works in practice, so it is ok". It isnt legal, and it doesnt work in
practice.

All in all, if your teacher tells you that

   void main (void) {}

is a correct program, then he or she is wrong. If your teacher insists
then he or she should consider learning C before teaching it. The only
legal declarations of main are

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

(Paragraph 5.1.2.2.1 of the ANSI C standard).



Sat, 03 Jul 1999 03:00:00 GMT  
 void main(void)

[ posted and emailed ]


|> Somewhere Down the road, I was told that void main(void) or even void
|> main() was incorrect programming.  I am very curious about this, because I
|> am a CSC major in college and they are teaching void main(void)?

ANSI/ISO C requires main() to be defined as returning an int. Many books
say that main(0 can be declared as void, but this is incorrect.

Remember that main() is a function like any other, and its return type
has been defined by ANSI.

You might want to take a look at the comp.lang.c Frequently Asked
Questions file, available at:

http://www.eskimo.com/~scs/C-faq/top.html

Your question is specifically answered in

http://www.eskimo.com/~scs/C-faq/q11.12.html

and

http://www.eskimo.com/~scs/C-faq/q11.14.html



Sat, 03 Jul 1999 03:00:00 GMT  
 void main(void)


Quote:

>Somewhere Down the road, I was told that void main(void) or even void
>main() was incorrect programming.  I am very curious about this, because I
>am a CSC major in college and they are teaching void main(void)?

>Can anyone please clairfy this to me.

The ISO C standard makes no requirement on what will happen when you feed
void main(void) to a conforming implementation of the C language.

The standard gives you two legal ways for declaring main which will be
understood by a conforming hosted implementation of the C language. Both
of these forms return int. One is:

        int main(int argc, char **argv)

the other is:

        int main(void)

Old-style declarations are also permissible, as are declarations which
have the int return value implicit:

        main()

        main(void)

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

Though it isn't explicitly stated anywhere that void main() is undefined
behavior, it is undefined by omission since nowhere in the document is a
meaning given to it.

It's not that void main() is an example of illegal programming, but rather that
C implementations are not required to translate void main into any well defined
behavior. It's not a constraint or syntax violation, which unfortunately means
that an implementation is not required to diagnose it with a message. Many
implementations support void main() via sheer accident or as a deliberate
extension.  Some programmers are aware of the extension and pigheadedly insist
on using void main() knowing full well that it is not portable, and that it
only works by accident on many implementations. Others fall into the more
ignorant category, which probably includes your course instructor.

Say, if toaster manufacturers were not required to ground the conductive
chassis of a toaster, would you touch it? :)

The question you should aks yourself is: are you taking this course to learn
the real C language, or to learn about some non-portable hack extensions
thereof?



Sat, 03 Jul 1999 03:00:00 GMT  
 void main(void)


Quote:

>[ posted and emailed ]


>|> Somewhere Down the road, I was told that void main(void) or even void
>|> main() was incorrect programming.  I am very curious about this, because I
>|> am a CSC major in college and they are teaching void main(void)?

>ANSI/ISO C requires main() to be defined as returning an int. Many books
>say that main(0 can be declared as void, but this is incorrect.

Well, not exactly. The standard merely places no requirement on the
implementation as to how the program should behave if you do write void main().

This may be taken to be a prescription for how one ought to write programs,
but it doesn't impose any requirements on the programmer :)

If your implementation has documented a meaning for void main(), then it's
safe to use it. However, your program is then not strictly conforming
and not maximally portable because it relies on an extension.

Whereas many extensions are good and cannot be avoided (try writing an
internet-based program without sockets, for example), void main() is
an utterly useless extension which can be easily avoided.

Quote:
>Remember that main() is a function like any other, and its return type
>has been defined by ANSI.

That's a bit of a weak argument. It's not legal for me to declare a function

        int foo(void)

and then call it as though it were int foo(int argc, char **argv). This causes
undefined behavior, even if I change the definition of foo() to the old-style.

In other words, you don't have the latitude of being allowed two types of
argument lists with functions other than main. The main function is, indeed,
special in that the environment can ``figure out'' how it should be called,
provided that it takes on one of the accepted forms, or compatible forms.

The reason void main() is undefined is because the standard does not list
that form as one of the permitted declarations, not because of rules relating
function calls and function declarations. These rules only concern themselves
with relationships between functions and calls defined by the programmer.



Sat, 03 Jul 1999 03:00:00 GMT  
 void main(void)


Quote:

>Somewhere Down the road, I was told that void main(void) or even void
>main() was incorrect programming.  I am very curious about this, because I
>am a CSC major in college and they are teaching void main(void)?

>Can anyone please clairfy this to me.

The ISO C standard specifies two possible signatures for main(), both
of which return int.  Many implementations allow void main() as an
extension, but it isn't legal C.  It buys you nothing, so why not
stick to the legal version?

John
--
John Winters.  Wallingford, Oxon, England.



Sun, 04 Jul 1999 03:00:00 GMT  
 void main(void)

: Somewhere Down the road, I was told that void main(void) or even void
: main() was incorrect programming.  I am very curious about this, because I
: am a CSC major in college and they are teaching void main(void)?

It is not useful in operating systems where the program is supposed to
return an exit code.  If you have void main(), you must have to use exit()
or _exit() to terminate your program, because you cannot return an exit
code from main() then.  Many people consider it bad style to give main no
return type.
Others tend to use void main() for programs that run in a graphical
environment and for which the exit code is obsolete.  However, better
learn C with int main() and always returning an exit code.

--
        --token

What are they going to come out with next? ``Microsoft Bubblesort?''
   (Kaz Kylheku in comp.lang.c)



Sun, 04 Jul 1999 03:00:00 GMT  
 void main(void)

Quote:

> Whereas many extensions are good and cannot be avoided (try writing an
> internet-based program without sockets, for example), void main() is
> an utterly useless extension which can be easily avoided.

Note: There are those who feel that void main() is not an "utterly
useless extension" and beleive that the ANSI/ISO committees should
consider adding it to the Standard.  Just because you do not perceive a
use for void main() does not mean that there is not a use for it.

Further the people who choose to use void main() on implementations that
support it are NOT pigheaded.  They are just programmers who, unlike
youself, have found a valid reason to use void main().  Actually, we can
eliminate all the arguments by making void main() part of the Standard,
there are several methods of implementing it that would meet the
requirements of every Operating System.

One method, for an OS that requires a return: void main() would cause
the compiler to automatically code any return (to the OS) as a
return(EXIT_SUCCESS).  void main() would then indicate to a programmer
that the program does not need to return any specific value to the OS or
that there is simply no (single) reasonable value to return.

Alicia Carla Longstreet
The doctrine of blind obedience and unqualified submission
to any human power, whether civil or ecclesiastical,
is the doctrine of despotism, and ought to have no place
'mong Republicans and Christians.
                                            Angelina Grimk



Sun, 04 Jul 1999 03:00:00 GMT  
 void main(void)


Quote:

>> Whereas many extensions are good and cannot be avoided (try writing an
>> internet-based program without sockets, for example), void main() is
>> an utterly useless extension which can be easily avoided.

>Note: There are those who feel that void main() is not an "utterly
>useless extension" and beleive that the ANSI/ISO committees should
>consider adding it to the Standard.  Just because you do not perceive a
>use for void main() does not mean that there is not a use for it.

If an implementation *documents* this as an extension, fine. Do
note, that it is no longer portable to large numbers of other
implementations. The problem I have with using it on my compiler
(Borland) is that they never document it as an extension. They
do document main(int arc) calling it "legal but unlikely"
(it is not legal in the ANSI C sense) and also main(int argc,
char *argv[], char *env[]). There is no void main(void) to be
found. (All of the examples return an int). So, all we have to
go on is it "works". But so does this:

double main(void)
    {
     return 0.0;
    }

If it is documented as an extension, then it is an extension.
I find no such documentation. So, void main() and double main()
are both examples of undefined behavour that does not currently
blow up in my face.

I do not wish to get started on this again, so as a preemtive
strike, I will say that if you can turn off the agruments main
gets with main(void), turning off the return value with void
main seems to be a example of orthogonal behavour (or at least,
an example of the "principle of least suprise") You should
respect the fact that some implementations or OS's cannot deal
with this situations, as how they call a program or function
which doesn't return a value, may not be the same as with one
that does, and as a result, it doesn't work. And in this case,
Borland *never* commits on paper that void main(void) will
work. (If I am wrong, please point out the documentation on
this.)

Quote:
>Further the people who choose to use void main() on implementations that
>support it are NOT pigheaded.  They are just programmers who, unlike
>youself, have found a valid reason to use void main().

If it is a *documented* extension, perhaps.

--
Craig

Manchester, NH
The less a man makes declarative statements, the
less he's apt to look foolish in retrospect.
  -- Quentin Tarantino in "Four Rooms"



Sun, 04 Jul 1999 03:00:00 GMT  
 void main(void)

Quote:



>>Note: There are those who feel that void main() is not an "utterly
>>useless extension" and beleive that the ANSI/ISO committees should
>>consider adding it to the Standard.  Just because you do not perceive a
>>use for void main() does not mean that there is not a use for it.

>However, no one has, in living memory, provided an example of a use for
>it.
>>They are just programmers who, unlike
>>youself, have found a valid reason to use void main().

>But are apparently unable to describe or explain this reason in anything
>I've ever read...
>I can tell you flat out that there's at least some sympathy on the committee
>for deprecating 'int main(void)', and eventually *requiring* only one
>declaration, because it's undesirable to have an exception in the function
>typing system.

Is there anything wrong with defining macros to make the main
function interface more palatable to some people, such as this:

#include <stdio.h>
#define MAIN int main(int argc, char *argv[]) {
#define ENDMAIN argc = argc; argv[0][0] = argv[0][0]; return 0; }

MAIN
    printf("Hello, simple C!\n");
ENDMAIN

It may seem a bit silly at first, but it completely hides the
fact that main is a function that takes arguments and returns
an int.

--
Craig

Manchester, NH
The less a man makes declarative statements, the
less he's apt to look foolish in retrospect.
  -- Quentin Tarantino in "Four Rooms"



Sun, 04 Jul 1999 03:00:00 GMT  
 void main(void)



Quote:
>Is there anything wrong with defining macros to make the main
>function interface more palatable to some people, such as this:

Yes.

Quote:
>#include <stdio.h>
>#define MAIN int main(int argc, char *argv[]) {
>#define ENDMAIN argc = argc; argv[0][0] = argv[0][0]; return 0; }
>MAIN
>    printf("Hello, simple C!\n");
>ENDMAIN

Ugh!

Quote:
>It may seem a bit silly at first,

It does.

Quote:
>but it completely hides the
>fact that main is a function that takes arguments and returns
>an int.

Ahh.

So, for the same reason, we might want to do
        #define PLUS(x, y) (x) + (y)
which, although it may look silly at first, completely hides the fact that
'+' is an operator that takes operands and produces a result.

I don't think it buys much.  It's like the "BEGIN" and "END" for "{" and "}".
It hides useful information.

-s
--
Copyright 1997 Peter Seebach - seebs at solon.com - C/Unix Wizard

The *other* C FAQ, the hacker FAQ, et al.  http://www.solon.com/~seebs
Unsolicited email (junk mail and ads) is unwelcome, and will be billed for.



Sun, 04 Jul 1999 03:00:00 GMT  
 void main(void)


Quote:

>One method, for an OS that requires a return: void main() would cause
>the compiler to automatically code any return (to the OS) as a
>return(EXIT_SUCCESS).  void main() would then indicate to a programmer
>that the program does not need to return any specific value to the OS or
>that there is simply no (single) reasonable value to return.

  This is semantic nonsense.  *You* try explaining to a beginner that 'main'
is a function, and the 'void' return type means that it doesn't return
a value, except that it does.  Mind you, there are implementations out
there that do actually do implement his, but it's more as an undocumented
hack to please the OS while preventing a barrage of "bug reports" than it
is a useful extension to the language itself.
  Following the 'spirit of C' as enumerated in the Rationale for the
C Standard, there is no reason to provide for void main(), despite the
fact that someone out there might actually have a reason for using it:
All the functionality that void main() could *portably* provide is already
provided quite elegantly by the existing definitions of main().  If it is
useful on one particular system then it can remain an extension on that
particular system.

--
The Amorphous Mass         If I knew what I was doing,



Sun, 04 Jul 1999 03:00:00 GMT  
 void main(void)



Quote:
>Note: There are those who feel that void main() is not an "utterly
>useless extension" and beleive that the ANSI/ISO committees should
>consider adding it to the Standard.  Just because you do not perceive a
>use for void main() does not mean that there is not a use for it.

However, no one has, in living memory, provided an example of a use for
it.

By contrast, at least a couple of people have pointed out the cost we pay
for the implementations which still tolerate this error.  In particular,
many of the implementations which spend extra programmer time on "Pascal"
vs. "C" linkage and calling conventions are doing so to make sure that void
and int functions are interchangable, solely to support incorrect code.

Quote:
>Further the people who choose to use void main() on implementations that
>support it are NOT pigheaded.

Can you give me an example?  I mean no offense, but you're one of the most
pigheaded people I've ever met.  You're probably nearly as stubborn as I am,
and I'm fairly world-class.

Quote:
>They are just programmers who, unlike
>youself, have found a valid reason to use void main().

But are apparently unable to describe or explain this reason in anything
I've ever read...

Quote:
>Actually, we can
>eliminate all the arguments by making void main() part of the Standard,
>there are several methods of implementing it that would meet the
>requirements of every Operating System.

Name one.  Please explain to me how, assuming I make the leap to an
implementation where void and int functions are different, I can support
"void main" without breaking something, when the return code *is* used by
the OS, and the startup code is compiled in advance.  It is *NOT* acceptable
to require me to provide two versions of the startup code, and magically
pass on information to the linker to tell it which to use.

Quote:
>One method, for an OS that requires a return: void main() would cause
>the compiler to automatically code any return (to the OS) as a
>return(EXIT_SUCCESS).  void main() would then indicate to a programmer
>that the program does not need to return any specific value to the OS or
>that there is simply no (single) reasonable value to return.

But then the compiler has to, internally, *ignore* what the programmer
said, and code main as a function returning an integer value, presumably
0 or EXIT_SUCCESS.

It is not the spirit of C to disregard a programmer's specification.

I can tell you flat out that there's at least some sympathy on the committee
for deprecating 'int main(void)', and eventually *requiring* only one
declaration, because it's undesirable to have an exception in the function
typing system.

-s
--
Copyright 1997 Peter Seebach - seebs at solon.com - C/Unix Wizard

The *other* C FAQ, the hacker FAQ, et al.  http://www.solon.com/~seebs
Unsolicited email (junk mail and ads) is unwelcome, and will be billed for.



Sun, 04 Jul 1999 03:00:00 GMT  
 void main(void)


writes:

Quote:
>Note: There are those who feel that void main() is not an "utterly
>useless extension" and believe that the ANSI/ISO committees should
>consider adding it to the Standard.

I agree with this statement (if not the apparent sentiment), but
think it is not much of an argument.  There are those who feel that
most valid fortran-90 programs should also be valid C programs, or
that 80x86 assembly programs should be valid C programs.  Indeed,
think of any change you yourself find bizarre or highly objectionable,
and there are probably people who think it should be done.

What you need to produce is an actual argument for its utility.

Quote:
>Further the people who choose to use void main() on implementations that
>support it are NOT pigheaded.  They are just programmers who, unlike
>youself, have found a valid reason to use void main().

I ask again, what concrete advantage have you (anyone!) found for
writing:

        void main(...

rather than:

        int main(...

that persists across multiple compilers and systems?  (Remember that
the Standard is not especially interested in special cases; special-
case systems can always just add `void main' as an extension.)

Quote:
>One method, for an OS that requires a return: void main() would cause
>the compiler to automatically code any return (to the OS) as a
>return(EXIT_SUCCESS).

Indeed, I have described this myself.

Quote:
>void main() would then indicate to a programmer
>that the program does not need to return any specific value to the OS or
>that there is simply no (single) reasonable value to return.

If there is no single reasonable value to return, why not simply
`return EXIT_SUCCESS;' anyway?  It takes an extra twenty characters
(or nine if you use `return 0;') -- is that really an unreasonable
thing to ask?

C *could* be (re)defined to automatically `#include <stdio.h>' unless
specifically told not to.  I think this would serve even more users
than would adding `void main'.  There are any number of other things
the X3J11 committee *could* do; what is your argument for having them
focus specifically on `void main'?
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc



Sun, 04 Jul 1999 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

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

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

3. Purpose of void main (void) and Functions

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

5. void main(void) ????

6. void main(void) illegal??

7. void main(void)

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

9. void main(void)....revisited!

10. void main(void)

11. Who cares? void main(void)

12. The making of void main(void)

 

 
Powered by phpBB® Forum Software