int main() OR void main()... 
Author Message
 int main() OR void main()...

Something of interest to me...*s*

What is the difference between using "int main()" or "void main()" to
mark the start of the main() function... ???   I know that if you use
the "int" return type for the function, you have to accompany it with an
ending "return" value, but isn't this just a waste of space...???

For instance, doesn't setting the return type for main() as "int"
specify an area in memory for a possible returning "int" value...???
Wouldn't this be using more memory than necessary...???   I'm told that
if the main() function was needed to return a value to another "calling"
program/function that the return type would have to be set, but in my
experience (which is only about a year), I have never seen such a
case...

Trevor...



Sat, 09 Nov 2002 03:00:00 GMT  
 int main() OR void main()...

: Something of interest to me...*s*

: What is the difference between using "int main()" or "void main()" to
: mark the start of the main() function... ???   I know that if you use
: the "int" return type for the function, you have to accompany it with an
: ending "return" value, but isn't this just a waste of space...???

No, it's not a waste of space. The most important point is, the C
standard defines main() as returning int, not void. Using a normal
ANSI-C compiler, if you type void main() instead of int main(), the
compiler is free to generate code to fill your hard drive with fluffy
pink bunnies singing "These sounds fall into my mind" by The
Bucketheads.
Also, most operating systems expect the program to return an exit code
telling if it succeeded or failed. If the program uses void main(), this
exit code will be garbage and have no real bearing to whether the
program succeeded or failed.

: For instance, doesn't setting the return type for main() as "int"
: specify an area in memory for a possible returning "int" value...???
: Wouldn't this be using more memory than necessary...???

If you're THAT short of memory, you should be programming in Assembler,
not C.

: I'm told that
: if the main() function was needed to return a value to another "calling"
: program/function that the return type would have to be set, but in my
: experience (which is only about a year), I have never seen such a
: case...

You were not told the whole truth. The "calling" program/function might
not always be an explicit C program, it could be the OS itself.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"All that flower power is no match for my glower power!"
   - Montgomery Burns



Sat, 09 Nov 2002 03:00:00 GMT  
 int main() OR void main()...

Quote:

> What is the difference between using "int main()" or "void main()" to
> mark the start of the main() function... ???   I know that if you use
> the "int" return type for the function, you have to accompany it with an
> ending "return" value, but isn't this just a waste of space...???

You know, if you'd read the FAQ, you would have had all the information
your could ever desire and then some.
Read <http://www.eskimo.com/~scs/C-faq/q11.12.html> and further for the
answer to your question; but I suggest devouring the lot of it, as it is
well worth the effort.

Richard



Sat, 09 Nov 2002 03:00:00 GMT  
 int main() OR void main()...

Quote:

>What is the difference between using "int main()" or "void main()" to
>mark the start of the main() function... ???

One is right and one is wrong.  The wrong one usually works; the
right one *always* works.

The outlets in modern bathrooms have Ground Fault Current Interruptors
("GFI" or "GFCI" outlets).  Therefore, you can take your toaster
into the bathroom, plug it in, turn it on, and throw it in the tub
with you and probably live to tell about it.  It is still not a
good idea.  Likewise, "void main()" probably will not kill you.
But why bother even trying it?  At least with the toaster in the
tub, you get a *thrill*.  "Void main" does not get you ANYthing.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc




Sat, 09 Nov 2002 03:00:00 GMT  
 int main() OR void main()...

Quote:
> The most important point is, the C
> standard defines main() as returning int, not void. Using a normal
> ANSI-C compiler, if you type void main() instead of int main(), the
> compiler is free to generate code to fill your hard drive with fluffy
> pink bunnies singing "These sounds fall into my mind" by The
> Bucketheads.

actually, if i'm reading n869 right, a non-standard return value from main
just means that the "termination status returned to the host environment is
unspecified", which would mean that the only thing affected would be the
returned value, not the behaviour (unless the host environment depends on
the returned value).  perhaps the pink bunny clause was taken out in C99?

--
             /"\                                m i k e    b u r r e l l

              X        AGAINST HTML MAIL
             / \



Sat, 09 Nov 2002 03:00:00 GMT  
 int main() OR void main()...


Quote:
> Something of interest to me...*s*

> For instance, doesn't setting the return type for main() as "int"
> specify an area in memory for a possible returning "int" value...???

No it does not. It is the function that calls main() that probably sets
aside space for the return value of main (if any). Internally the compiler
vendor will have some function that is part of startup that calls main.
Typically the startup does nos housekeeping initialisation and then does

exit(main(arg, argv));

or

exit(main());

which shows that the return value of main is directly passed to exit (or
something similar). So if you define main like so

void main(void);

it means thaty exit will get an arbitrary value - a badly behaved program.
Note, all of the above is speculative as ISO C standard does not say how the
startup of main implemented just as long as it has certain confirming
properties.

Stephen Howe



Sat, 09 Nov 2002 03:00:00 GMT  
 int main() OR void main()...

Quote:
>Something of interest to me...*s*

>What is the difference between using "int main()" or "void main()" to
>mark the start of the main() function... ???   I know that if you use
>the "int" return type for the function, you have to accompany it with an
>ending "return" value, but isn't this just a waste of space...???

>For instance, doesn't setting the return type for main() as "int"
>specify an area in memory for a possible returning "int" value...???
>Wouldn't this be using more memory than necessary...???   I'm told that
>if the main() function was needed to return a value to another "calling"
>program/function that the return type would have to be set, but in my
>experience (which is only about a year), I have never seen such a
>case...

The Standard said in simple words "the return type of main() is int".
Consequently my C compilers (in strict ANSI) mode do not like me not to
follow the rule :

void main(void)
{

Quote:
}

BC 3.1
 Compiling MAIN.C:
 Error MAIN.C 2: main must have a return type of int

DJGPP
 Compiling: main.c
 main.c(2) Warning:  return type of `main' is not `int'
 no errors
 Creating: proj.exe
 no errors

So the return type of main() is int, and a valid value must be returned. Now
your question is "what is this made for ?"

The use of the returned value belongs to the system. For example, in MS-DOS,
the returned value can be tested in a batch file with ERRORLEVEL. I am quite
sure that such a feature is also available on Unix and Linux. (Are they
scripts on MacOs, AmigaOs or BeOs ?)

--
-hs-
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"Machine-generated code, probably. I'd kill the human that wrote code
using even 31 parameters." --Richard Bos CLC



Sat, 09 Nov 2002 03:00:00 GMT  
 int main() OR void main()...
On Tue, 23 May 2000 20:00:36 GMT,

Quote:

>actually, if i'm reading n869 right, a non-standard return value from main
>just means that the "termination status returned to the host environment is
>unspecified",

Theres a difference between a non-standard return value, and declaring main
to have no return value.

--
Chance is irrelevant.  We will succeed.  -- Seven of Nine



Sat, 09 Nov 2002 03:00:00 GMT  
 int main() OR void main()...

Quote:
>On Tue, 23 May 2000 20:00:36 GMT,

>>actually, if i'm reading n869 right, a non-standard return value from main
>>just means that the "termination status returned to the host environment is
>>unspecified",

>Theres a difference between a non-standard return value, and declaring main
>to have no return value.

IIRC the first is implementation defined behaviour, the second is
undefined behaviour.
--
Mark McIntyre
C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html


Sun, 10 Nov 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. int main() or void main()

2. main() vs int main() vs int main(void)

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

4. newbie question....int main, void main, main

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

6. int main() vs int main(void)

7. int main vs. void main

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

9. void main or int main?

10. void main() <-> int main()

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

12. void main() v/s int main()

 

 
Powered by phpBB® Forum Software