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

I am just starting to learn C and some of the notes I have been working from
are pre ANSI C and don't really mention function prototypes. Anyway, I have
got to grips with it and undersrand when to use void, but what about its use
around the main function header? I have read in a more recent book to type
"return 0;" at the end of main (before the final closing braces). Why can't
I just type "void main()" and for that matter why can I not type "void
main(void)" which was warned against by some one in this newsgroup. After
all surely main is not recieving any parameters (or should that be
arguements, I'm a bit confused about this). The only reason I can think not
to type void is that it may be to do with the standard library functions.

Thanks in advance,

                                    Ben



Wed, 23 Aug 2000 03:00:00 GMT  
 void main(void) ????

: I am just starting to learn C and some of the notes I have been working from
: are pre ANSI C and don't really mention function prototypes. Anyway, I have
: got to grips with it and undersrand when to use void, but what about its use
: around the main function header? I have read in a more recent book to type
: "return 0;" at the end of main (before the final closing braces). Why can't
: I just type "void main()" and for that matter why can I not type "void
: main(void)" which was warned against by some one in this newsgroup. After
: all surely main is not recieving any parameters (or should that be
: arguements, I'm a bit confused about this). The only reason I can think not
: to type void is that it may be to do with the standard library functions.

Get hold of a copy of the on-line FAQ, which deals with this and a lot
of other important and less-than-obvious details.

ANSI says that main() must return an int, and that's the end of it; but
ANSI also says that (unusually) it can have either no arguments,
int main(void), or two arguments, int main(int argc, char *argv[])
(parameter names may differ).

void main(); declares main as a function with an unknown number of
parameters returning void - not allowed.  void main(void); declares
main as a function with no parameters returning void - not allowed.

If you don't use any command line arguments then int main(void) is
fine (the command line arguments usually appear, with the program
name, in the vector argv).  However, even if you don't want to use
mains exit or return status, which is usually returned to the calling
environment, you must still call main correctly as 'int main(...;
the compiler doesn't have to work correctly if you don't, though many
compilers will in fact accept the extension void main(... and act as
you'd expect.

Will



Wed, 23 Aug 2000 03:00:00 GMT  
 void main(void) ????


Quote:

>I am just starting to learn C and some of the notes I have been working from
>are pre ANSI C and don't really mention function prototypes.

You may want to consider investing in an up-to-date reference.  The
changes between pre-ANSI and ANSI C really are quite major and you'll
probably be making it harder for yourself if you learn the older version.

Quote:
>Anyway, I have
>got to grips with it and undersrand when to use void, but what about its use
>around the main function header?

>I have read in a more recent book to type
>"return 0;" at the end of main (before the final closing braces). Why can't
>I just type "void main()" and for that matter why can I not type "void
>main(void)" which was warned against by some one in this newsgroup.

First, note 'void main()' and 'void main (void)' are equivalent in this
context.  If the parameter list is omitted in a function definition, it's
assumed that there were no parameters, just as if you'd inserted 'void'.

Before I go any further, a point from later in your post:

Quote:
>After
>all surely main is not recieving any parameters (or should that be
>arguements, I'm a bit confused about this).

"Parameter" and "argument" are synonmys in C.  However, the issue with
'main' is the return value, not the parameters.

'main' may be defined in two ways:

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

(The names 'argc' and 'argv' may be changed, of course; additionally you
may use 'char **argv', because that's equivalent to the form above.)

'main' must *return* 'int' but it doesn't have to take any parameters.
If you do choose to take the parameters, you'll get any command-line
arguments which were passed to your program.  A decent C book will
explain this in detail.

Now, the answer to your question about why 'void main' isn't allowed:
because the Standard says so.  The ANSI committee which formalised the
definition of C just didn't allow 'void main'.

This probably isn't a very satisfactory answer, so here are some comments
about _why_ the standardisation committee may have made that decision.
Many operating systems support the concept of a "return value" or "exit
status" to report information back from the running program.  For example,
many UNIX programs use the convention that a return of 0 indicates success,
but any other return value marks some kind of failure (bad arguments, file
not found, etc.)

Now, one of the ways the exit status is set by a C program is by returning
that value from 'main'.  So, 'main' needs to be able to return an 'int'.

The next question is: "if my program *doesn't* want to set an exit code,
why can't I indicate that by declaring 'main' to be void?"  A possible
reason this is disallowed is to do with the startup and exit code.
Typically, around your program the implementation will place a small
"wrapper" to do things like set up stdin and stdout, and so on.  To
execute your program proper, this wrapper will typically call 'main'.
Since the wrapper expects 'main' to return a value, it *must* return
a value -- otherwise you'll get the same situation as you would if
you tried to use the value of a void function in your own code.

Let me repeat: the committee *could* have decided to allow 'void main'
and just leave the implementation to deal with these problems somehow,
but they didn't.  The "correct" answer to your question is simply
"the language definition doesn't allow that".

Cheers,
Richard
--
Richard Stamp
Churchill College, Cambridge



Wed, 23 Aug 2000 03:00:00 GMT  
 void main(void) ????

Quote:

> Let me repeat: the committee *could* have decided to allow 'void main'
> and just leave the implementation to deal with these problems somehow,
> but they didn't.  The "correct" answer to your question is simply
> "the language definition doesn't allow that".

Sorry for jumping in, but I just purchased MS Visual C++ after many
years of programming in C on my old Atari ST. I'm starting over again
with simple examples from my old C books. I've noticed that VC++ seems
to want- void main(void). I had just entered some elemenatary program
with just plain old

main()

and VC++ insisted on void main(void)

or I'm misunderstanding somethin, but when I use void main(void), VC++
seems very happy with it.

Joe
(I'm strictly an amateur)



Wed, 23 Aug 2000 03:00:00 GMT  
 void main(void) ????

Quote:

> got to grips with it and undersrand when to use void, but what about its use
> around the main function header? I have read in a more recent book to type

        Because strictly speaking, the ANSI standard requires the main function
to return int.  This return value is usually used by the operating system (or a
parent process), though it may just be thrown away.  However, in defining the
language, it is better to require main to return an int, then to require all
operating systems to be re-written to sometimes receive an int and sometimes
not.  :)

        So an int return type is required for main.  What you do with it is
your own business.

Quote:
> "return 0;" at the end of main (before the final closing braces). Why can't
> I just type "void main()" and for that matter why can I not type "void
> main(void)" which was warned against by some one in this newsgroup. After
> all surely main is not recieving any parameters (or should that be
> arguements, I'm a bit confused about this). The only reason I can think not
> to type void is that it may be to do with the standard library functions.

        Look at it this way.  main is a standard function to the operating
system, loader, and start-up code.  Those three all expect main to return an
int, so you have to declare main to return an int.

        As for main receiving arguments, it is.  You may disregard the argu-
ments passed by the operating system or parent process, and declare

        int main( void);

        However, the only other standard-approved way of calling main is to
include both parameters,

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

        ANSI C works this way because the ANSI committee said so, and because
this agreement was necessary not to ruffle too many people's feathers.  You
cannot declare main as returning void (your code would NOT be portable).

                                                        -- Stone



Wed, 23 Aug 2000 03:00:00 GMT  
 void main(void) ????

On Sat, 07 Mar 1998 21:32:23 -0500, Joseph Zorzin

Quote:


>> Let me repeat: the committee *could* have decided to allow 'void main'
>> and just leave the implementation to deal with these problems somehow,
>> but they didn't.  The "correct" answer to your question is simply
>> "the language definition doesn't allow that".

>Sorry for jumping in, but I just purchased MS Visual C++ after many
>years of programming in C on my old Atari ST. I'm starting over again
>with simple examples from my old C books. I've noticed that VC++ seems
>to want- void main(void). I had just entered some elemenatary program
>with just plain old

>main()

>and VC++ insisted on void main(void)

>or I'm misunderstanding somethin, but when I use void main(void), VC++
>seems very happy with it.

You're misunderstanding.  VC++ accepts void main(void) without
comment, but the standard doesn't require a diagnostic so this is OK.
VC++ also gives a warning if you define int main(void) and don't
include a return statement.  The return statement isn't required by
the standard, but it's good practice.  The standard doesn't prohibit
the warning and I'd say it's a good one.  You can ignore or turn off
the warning, but its much better to put in the return.  

You can return 0 to indicate success or, after including <stdlib.h>,
EXIT_SUCCESS or EXIT_FAILURE.  You can also return other integer
values, but the meanings of any but those is implementation-defined.
--
Michael M Rubenstein



Thu, 24 Aug 2000 03:00:00 GMT  
 void main(void) ????



Quote:

> > Let me repeat: the committee *could* have decided to allow
'void main'
> > and just leave the implementation to deal with these
problems somehow,
> > but they didn't.  The "correct" answer to your question is
simply
> > "the language definition doesn't allow that".

> Sorry for jumping in, but I just purchased MS Visual C++ after
many
> years of programming in C on my old Atari ST. I'm starting
over again
> with simple examples from my old C books. I've noticed that
VC++ seems
> to want- void main(void). I had just entered some elemenatary
program
> with just plain old

> main()

> and VC++ insisted on void main(void)

> or I'm misunderstanding somethin, but when I use void
main(void), VC++
> seems very happy with it.

> Joe
> (I'm strictly an amateur)

<Jack>

Well, Joe, you're not being completely accurate here.  MSVC does
not insist on void(main).  Here are the errors I got from
compiling this file, and an explanation of what they mean:

==================
main(){}
==================

On MSVC 1.52, 4.0, and 5.0 as both C and C++:

MSVC 1.52:
Compiling...
c:\prog\msvc\test32\main.c
c:\prog\msvc\test32\main.c(1) : warning C4035: 'main' : no
return value
MAIN.C - 0 error(s), 1 warning(s)

Compiling...
c:\prog\msvc\test32\main.cpp
c:\prog\msvc\test32\main.cpp(1) : warning C4508: 'main' :
function should return a value; 'void' return type assumed
MAIN.CPP - 0 error(s), 1 warning(s)
=====================================
MSVC 4.0:
Compiling...
main.c
C:\Projects\timechek\main.c(1) : warning C4035: 'main' : no
return value

Compiling...
main.c
C:\Projects\timechek\main.c(1) : warning C4035: 'main' : no
return value
main.obj - 0 error(s), 1 warning(s)
======================================
MSVC 5.0:
Compiling...
main.c
C:\prog\DevStudio\MyProjects\proj1\main.c(1) : warning C4035:
'main' : no return value

Compiling...
main.cpp
C:\prog\DevStudio\MyProjects\proj1\voidmain.cpp(1) : error
C2561: 'main' : function must return a value
======================================

If you define main() without a return type, you implicitly
declare it as returning int.  This is specific in the ANSI/ISO
standard.  So you get an error because you don't return that
int.

Declaring or defining a function without a return type in
standard C does not define or declare it as returning void.  It
implicitly declares it as returning an int.

This implicit int will become illegal under the coming C9X
update to the standard.

</Jack>



Thu, 24 Aug 2000 03:00:00 GMT  
 void main(void) ????



Quote:
>ANSI says that main() must return an int, and that's the end of it; but
>ANSI also says that (unusually) it can have either no arguments,
>int main(void), or two arguments, int main(int argc, char *argv[])
>(parameter names may differ).

No, actually it says that the programmer may *declare* main either way
and the compiler is obliged to make it work either way.  Actually there
are always the two arguments, so sometimes to make the no-argument form
work a compiler may have to notice the mismatched declaration and adjust
the function linkage to get the two arguments safely out of the way.

There is really no good reason for this, as it confuses beginners, and
perhaps we could change the spec to make it clearer that the environment
is invoking the user-provided main function via
        exit(main(argc,argv));
so the user ought to provide what is actually being called, just as for
any other function in C.



Thu, 24 Aug 2000 03:00:00 GMT  
 void main(void) ????


Quote:

>Sorry for jumping in, but I just purchased MS Visual C++ after many
>years of programming in C on my old Atari ST. I'm starting over again
>with simple examples from my old C books. I've noticed that VC++ seems
>to want- void main(void). I had just entered some elemenatary program
>with just plain old

>main()

>and VC++ insisted on void main(void)
>or I'm misunderstanding somethin, but when I use void main(void), VC++
>seems very happy with it.

When they say it is 'not portable' it just mean in strictly ANSI C.
Since void main() is abused so often, probably 99% or better of
compilers that are used will work with it.  There's only few key strokes
to be gain, so there's no excuse for current programs to use void.

Perhaps these type of sloppiness is why MS environment aren't as
stable as I would like to have.

Aloha



Thu, 24 Aug 2000 03:00:00 GMT  
 void main(void) ????

[...]

| 'main' must *return* 'int' but it doesn't have to take any parameters.
| If you do choose to take the parameters, you'll get any command-line
| arguments which were passed to your program.  A decent C book will
| explain this in detail.
|
| Now, the answer to your question about why 'void main' isn't allowed:
| because the Standard says so.  The ANSI committee which formalised the
| definition of C just didn't allow 'void main'.

I have no problem of returning int from main() simply by obeying
the standard, but what am I returning this integer value (0 or 1)
to? To the MS-DOS system that called my stand-alone program? How
can I use this returned variable? I could not find explanation of
this in my books.

Andrew

 ===============================================
 Andrew M. Kukla        Toronto, Ontario, Canada

 ===============================================



Thu, 24 Aug 2000 03:00:00 GMT  
 void main(void) ????



Quote:
>I have no problem of returning int from main() simply by obeying
>the standard, but what am I returning this integer value (0 or 1)
>to? To the MS-DOS system that called my stand-alone program? How
>can I use this returned variable? I could not find explanation of
>this in my books.

A DOS example:

Suppose your program is called from a batch file, and the batch file
needs to do different things depending on whether your program
succeeded or failed. The ERRORLEVEL test is testing the results of the
return value of your program. The possibility of someone calling your
program from a batch file makes it pretty important for it to return a
meaningful value, and not random garbage, which is what happens with
"void main()".

--

Cameron Foster

http://www2.netcom.com/~cdfoster/



Thu, 24 Aug 2000 03:00:00 GMT  
 void main(void) ????



...

Quote:
>First, note 'void main()' and 'void main (void)' are equivalent in this
>context.  If the parameter list is omitted in a function definition, it's
>assumed that there were no parameters, just as if you'd inserted 'void'.

The prototype frorm does mandate more error checking from the compiler.

Quote:
>Before I go any further, a point from later in your post:

>>After
>>all surely main is not recieving any parameters (or should that be
>>arguements, I'm a bit confused about this).

>"Parameter" and "argument" are synonmys in C.

No, the standard defines an argument to be a value passed to a function by
the caller whereas a parameter is the variable in the called function that
receives it.

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


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



Thu, 24 Aug 2000 03:00:00 GMT  
 void main(void) ????


: [...]
:  
: | 'main' must *return* 'int' but it doesn't have to take any parameters.
: | If you do choose to take the parameters, you'll get any command-line
: | arguments which were passed to your program.  A decent C book will
: | explain this in detail.
: |
: | Now, the answer to your question about why 'void main' isn't allowed:
: | because the Standard says so.  The ANSI committee which formalised the
: | definition of C just didn't allow 'void main'.

: I have no problem of returning int from main() simply by obeying
: the standard, but what am I returning this integer value (0 or 1)
: to? To the MS-DOS system that called my stand-alone program? How
: can I use this returned variable? I could not find explanation of
: this in my books.

Check the MSDOS manual on batch file programming; you'll see something
there about the errorlevel variable, which contains the result of the
most recent command.  That's the return value from main().

Will



Thu, 24 Aug 2000 03:00:00 GMT  
 void main(void) ????

Quote:



> ...

> >First, note 'void main()' and 'void main (void)' are equivalent in this
> >context.  If the parameter list is omitted in a function definition, it's
> >assumed that there were no parameters, just as if you'd inserted 'void'.

> The prototype frorm does mandate more error checking from the compiler.

Such as what! Does it check that the non-specified parameters are not
used within the remainder of the function!

If the function was called recursively then some additional checking
_might_ be done but then that wouldn't be within this context.

Quote:
> >"Parameter" and "argument" are synonmys in C.

> No, the standard defines an argument to be a value passed to a function by
> the caller whereas a parameter is the variable in the called function that
> receives it.

This is semantics that adds nothing to the subject.

Steve



Fri, 25 Aug 2000 03:00:00 GMT  
 void main(void) ????



Quote:


>> >"Parameter" and "argument" are synonmys in C.

>> No, the standard defines an argument to be a value passed to a function by
>> the caller whereas a parameter is the variable in the called function that
>> receives it.

>This is semantics that adds nothing to the subject.

Personally, I appreciated being pointed towards those definitions in the
Standard, which (self-evidently) I hadn't previously noted.  I'd tend
to think it's unwise to rely on this distinction in discussions with
your average C programmer, but it's worth knowing nonetheless.

I appreciate that many people aren't interested in these details, but
there are quite a few of us who are so it's not unreasonable that we
occasionally talk among ourselves.

Cheers,
Richard
--
Richard Stamp
Churchill College, Cambridge



Fri, 25 Aug 2000 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) illegal??

6. void main(void)

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

8. void main(void)

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