void return type 
Author Message
 void return type

Can function main really have a return type of void?  I thought OS's
required an integer type return at termination.

If this is true, what really happens when you do void main(void)?

Sean



Tue, 17 Aug 2004 13:30:11 GMT  
 void return type

Quote:
> Can function main really have a return type of void?

Of course not.

Quote:
> I thought OS's
> required an integer type return at termination.

No. The C standard requires it.

Quote:
> If this is true, what really happens when you do void main(void)?

Undefined behavior.

It's just plain stupid. Don't do it.

Bart.



Tue, 17 Aug 2004 13:39:10 GMT  
 void return type

Quote:

> Can function main really have a return type of void?

No.  Read the FAQ.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin


Tue, 17 Aug 2004 13:33:25 GMT  
 void return type

...

Quote:
>> Can function main really have a return type of void?

> Of course not.

Sure it can, not too usefull thou.

...

Quote:
> Undefined behavior.

> It's just plain stupid. Don't do it.

In real life it mostly does not matter, as most often you do not care what a
program returns at exit (maybe in *NIX, where scripting often relay on
return codes). Actually everybody would be pleased with following code.

void main(void)
{
 // ... do a lot of usefull stuff ...
 exit( 0 );

Quote:
}

Why would you do this... well maybe if you want to use main() recursive,
or you like odd solutions, or just want to. But mostly its just "bad
style".

// SVisor



Tue, 17 Aug 2004 16:55:40 GMT  
 void return type

Quote:
>In real life it mostly does not matter, as most often you do not care what a
>program returns at exit (maybe in *NIX, where scripting often relay on
>return codes). Actually everybody would be pleased with following code.

>void main(void)
>{
> // ... do a lot of usefull stuff ...
> exit( 0 );
>}

There are compilers that would diagnose such code:

    fangorn:~/tmp 194> gcc test.c
    test.c: In function `main':
    test.c:5: warning: return type of `main' is not `int'

and even compilers that would treat void main as a fatal error (e.g. DEC C
for OpenVMS).

Are you particularly pleased when the compiler diagnoses or even refuses
to translate your code?

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 17 Aug 2004 18:06:05 GMT  
 void return type

)In real life it mostly does not matter, as most often you do not care what a
)program returns at exit (maybe in *NIX, where scripting often relay on
)return codes). Actually everybody would be pleased with following code.
)
)void main(void)
){
) // ... do a lot of usefull stuff ...
) exit( 0 );
)}
)
)Why would you do this... well maybe if you want to use main() recursive,
)or you like odd solutions, or just want to. But mostly its just "bad
)style".

Speak for yourself. If I were involved in a code inspection here at work
and found that code, I would certainly not be pleased. Au contraire, I
would be very upset.

Mike
--
char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
This message made from 100% recycled bits.
I can explain it for you, but I can't understand it for you.
I don't speak for Alcatel      <- They make me say that.



Wed, 18 Aug 2004 00:54:35 GMT  
 void return type


Quote:
>Can function main really have a return type of void?  I thought OS's
>required an integer type return at termination.

In C89, main MUST be defined as int.

In C99, if main is defined as int (as it should be), the value that is
returned determines if the program succeeded or failed. If it's 0 or
EXIT_SUCCESS, success is returned, if it's EXIT_FAILURE, failure is.
If it's anything other than those three values, either failure or
success can be returned.

Quote:

>If this is true, what really happens when you do void main(void)?

In C89, the computer pulls demons out of your nose.
In C99, whether success or failure is returned is unspecified.

Quote:
>Sean

--
"Pedants make the best programmers" - Richard Heathfield

Indent-o-meter
        01234567
        ^



Wed, 18 Aug 2004 04:11:08 GMT  
 void return type

Quote:



> )In real life it mostly does not matter, as most often you do not care
> what a )program returns at exit (maybe in *NIX, where scripting often
> relay on )return codes). Actually everybody would be pleased with
> following code. )
> )void main(void)
> ){
> ) // ... do a lot of usefull stuff ...
> ) exit( 0 );
> )}
> )
> )Why would you do this... well maybe if you want to use main() recursive,
> )or you like odd solutions, or just want to. But mostly its just "bad
> )style".

> Speak for yourself. If I were involved in a code inspection here at work
> and found that code, I would certainly not be pleased. Au contraire, I
> would be very upset.

> Mike

I don't have access to K&R1 (have K&R2), and so don't know the answer to
this:  Was there ever a time when main() was not required to return int?

One of the reasons I ask is because (having read up on pfaffs..) I dug out
the old Schildt book I'd kept to annotate, and realized that it simply was
never intended to be ANSI C.  There were a couple of pages in the appendix
that mentioned it (copywrite 1987, 1990).  This is the book that uses void
main(void) for everything, of course.

If void main() was acceptable in K&R1, one would expect backwards
compatible compilers to accept it without comment when the K&R C switch was
set (something I've not tried...<grin>).

Thanks,

Bill Tallman



Wed, 18 Aug 2004 04:36:00 GMT  
 void return type

Quote:
> I don't have access to K&R1 (have K&R2), and so don't know the answer to
> this:  Was there ever a time when main() was not required to return int?

No.  `void' was an invention of the ANSI committee, and ANSI C
has always required main() to return int.

Quote:
> One of the reasons I ask is because (having read up on pfaffs..) I dug out
> the old Schildt book I'd kept to annotate, and realized that it simply was
> never intended to be ANSI C.

Schildt's full of {*filter*}and can't be taken as a metric of what is
C, where pre-ANSI or not.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
 \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


Wed, 18 Aug 2004 04:37:01 GMT  
 void return type


*snip*

Quote:
>In C99, whether success or failure is returned is unspecified.

Unless you call exit with a portable value, as others pointed out

--
"Pedants make the best programmers" - Richard Heathfield

Indent-o-meter
        01234567
        ^



Wed, 18 Aug 2004 04:44:07 GMT  
 void return type

Quote:

>No.  `void' was an invention of the ANSI committee, and ANSI C
>has always required main() to return int.

Except in C99

Quote:

>Schildt's full of {*filter*}and can't be taken as a metric of what is
>C, where pre-ANSI or not.

Amen. Even though C99 allows void main, it's still bad.

--
"Pedants make the best programmers" - Richard Heathfield

Indent-o-meter
        01234567
        ^



Wed, 18 Aug 2004 05:41:02 GMT  
 void return type

Quote:

... snip ...

> If void main() was acceptable in K&R1, one would expect backwards
> compatible compilers to accept it without comment when the K&R C
> switch was set (something I've not tried...<grin>).

void main was NOT acceptable.  void was not acceptable, except as
a user defined id.  main returned an int.  There was no standard
whatsoever.  K&R was used as the de-facto reference.

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)



Wed, 18 Aug 2004 07:15:19 GMT  
 void return type

Quote:


> >No.  `void' was an invention of the ANSI committee, and ANSI C
> >has always required main() to return int.

>  Except in C99

Quote chapter and verse, please. (Hint: if you think the clause "some
implementation-defined manner" is meaningful, research the topic, and
think hard whether this misuse of the term "implementation-defined" is
consistent with the rest of the Standard.)

Gergo
--
By doing just a little every day, you can gradually let the task
completely overwhelm you.



Wed, 18 Aug 2004 07:15:26 GMT  
 void return type



)
)*snip*
)
)>In C99, whether success or failure is returned is unspecified.
)
)Unless you call exit with a portable value, as others pointed out

EVEN if one calls exit() with a portable value. Note that a call to a
routine of type void () may require different calling conventions than
a routine of type int (). So this declaration

#include <stdlib.h>

void main(void) {
    /* some stuff */
    exit(0);

Quote:
}

may result in the compiler emitting code which CAN'T BE CALLED BY THE
STARTUP CODE AT ALL. It may not even execute one instruction of main()
before it dies or does something else weird.

Mike

--
char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
This message made from 100% recycled bits.
I can explain it for you, but I can't understand it for you.
I don't speak for Alcatel      <- They make me say that.



Wed, 18 Aug 2004 07:21:44 GMT  
 void return type

Quote:


> > >No.  `void' was an invention of the ANSI committee, and ANSI C
> > >has always required main() to return int.

> >  Except in C99

> Quote chapter and verse, please. (Hint: if you think the clause "some
> implementation-defined manner" is meaningful, research the topic, and
> think hard whether this misuse of the term "implementation-defined" is
> consistent with the rest of the Standard.)

I can't find a definition for "implementation defined manner."  Only
"implementation defined behavior" and "implementation defined value" are
found.

"3.4.1
1 implementation-defined behavior
unspecified behavior where each implementation documents how the choice is
made
2 EXAMPLE An example of implementation-defined behavior is the propagation
of the high-order bit when a signed integer is shifted right."

"3.17.1
1 implementation-defined value
unspecified value where each implementation documents how the choice is
made"

This stuff is salient, of course:

"5.1.2.1 Freestanding environment
1 In a freestanding environment (in which C program execution may take place
without any benefit of an operating system), the name and type of the
function called at program startup are implementation-defined. Any library
facilities available to a freestanding program, other than the minimal set
required by clause 4, are implementation-defined.
2 The effect of program termination in a freestanding environment is
implementationdefined.
5.1.2.2 Hosted environment
1 A hosted environment need not be provided, but shall conform to the
following specifications if present."

Which leads us to this:

"ISO/IEC 9899:1999 (E) ?ISO/IEC
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."
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

What you were speaking of is [obviously] directly above...

"Footnote: 9) Thus, int can be replaced by a typedef name defined as int, or
the type of argv can be written as char ** argv, and so on."

Since there is no definition any where in the standard as to what
"implementation-defined manner" means, I think we can only say that "the
implementation defines it" but have no clarity as to what that might mean.
--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 "The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm



Wed, 18 Aug 2004 07:59:45 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. calling a function w/ void return type in c#

2. HRESULT return type turns into void when adding class from type library

3. void pointer (void *) : how to determine type?

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

5. Help with: C2392 - covariant returns types are not supported in managed types

6. undefined type in arg list vs. return type

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

8. difference between void foo(void) and void foo()

9. void returns?

10. returning void

11. Returning a void value

12. return in a void function

 

 
Powered by phpBB® Forum Software