return value of main

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

Hi,

the ANSI-C 99 standard specifies that the main function
has "int" as return type. However, there are still lots
of people declaring "void" as main return type.

Did previous ANSI-C standards defined "void" as return type
or is this issue just a lack of C knowledge?

Regards,
Chris
 
I

Ian Collins

Christian said:
Hi,

the ANSI-C 99 standard specifies that the main function
has "int" as return type. However, there are still lots
of people declaring "void" as main return type.

Did previous ANSI-C standards defined "void" as return type
or is this issue just a lack of C knowledge?
The latter.

Try compiling void main(){} with gcc -Wall -ansi -pedantic.
 
P

Papastefanos Serafeim

? "Christian Christmann said:
the ANSI-C 99 standard specifies that the main function
has "int" as return type. However, there are still lots
of people declaring "void" as main return type.

Did previous ANSI-C standards defined "void" as return type
or is this issue just a lack of C knowledge?

Check questions 11.12a-11.15 of the C faq
(http://c-faq.com/ansi/).

Previous ANSI-C standards did not define void as return type.
 
M

Martin Ambuhl

Papastefanos said:
Check questions 11.12a-11.15 of the C faq
(http://c-faq.com/ansi/).
Previous ANSI-C standards did not define void as return type.

Is this just sloppy posting, in which you meant "No ANSI C standard has
defined void as the return type for main", or is it
Jacob's disease of trying to take away features that have been part of C
since the *first* ANSI C standard (1989) striking again?

Check 3.1.2.5 of the 1989 C standard for the void type.
 
K

Keith Thompson

Christian Christmann said:
the ANSI-C 99 standard specifies that the main function
has "int" as return type. However, there are still lots
of people declaring "void" as main return type.

Did previous ANSI-C standards defined "void" as return type
or is this issue just a lack of C knowledge?

It's pure ignorance. No C standard has ever defined main as returning
void.
 
R

Richard Heathfield

Christian Christmann said:
Hi,

the ANSI-C 99 standard specifies that the main function
has "int" as return type. However, there are still lots
of people declaring "void" as main return type.

Did previous ANSI-C standards defined "void" as return type

No, never, but see below.
or is this issue just a lack of C knowledge?

Mostly, yes.

The void type has never been a correct return type for main except in the
following circumstances, which also cover all other possible return types
(double main, struct tm main, union myunion main, etc):

1) in C90 and C99, if the implementation is freestanding and documents the
type in question as the appropriate return type for the entry point (which
might not even be called main);
2) in C99 *only*, if the implementation explicitly documents the return type
in question as being supported.

In each case, returning a value of a type other than int from main means
that your program is not portable to implementations that do not document
and support such a return type.

So, as with so much in life, the simple answer doesn't quite tell the full
story.

For maximal portability, use int as the type of your return value from main.
The only case where this doesn't work is where your (necessarily
freestanding) implementation specifically /requires/ you to do otherwise,
in which case your program - or at least the entry point part of it - isn't
going to be portable anyway.

A classic example of a freestanding implementation is Visual C when used in
the normal way to compile Windows programs - the entry point is not main,
but WinMain, and therefore the implementation *must* be freestanding! :)

Ironically for a company that has done so much to spread the void main myth,
Microsoft's WinMain is required to return int!
 
K

Keith Thompson

Richard Heathfield said:
Christian Christmann said:

No, never, but see below.


Mostly, yes.

The void type has never been a correct return type for main except in the
following circumstances, which also cover all other possible return types
(double main, struct tm main, union myunion main, etc):

1) in C90 and C99, if the implementation is freestanding and documents the
type in question as the appropriate return type for the entry point (which
might not even be called main);
2) in C99 *only*, if the implementation explicitly documents the return type
in question as being supported.
[...]

Both the C90 and C99 standards have the following:

A conforming implementation may have extensions (including
additional library functions), provided they do not alter the
behavior of any strictly conforming program.

Allowing "void main(void)" would be such an extension. (Because of
this, C99 5.1.2.2.1's explicit permission to allow other forms of main
is, I believe, redundant.)
 
C

Christopher Benson-Manica

Keith Thompson said:
Allowing "void main(void)" would be such an extension. (Because of
this, C99 5.1.2.2.1's explicit permission to allow other forms of main
is, I believe, redundant.)

Redundant, but also perhaps yet necessary, given the relative
abundance of implementations offering that extension and the
consequent confusion.
 
D

Dingo

Martin said:
Is this just sloppy posting, in which you meant "No ANSI C standard has
defined void as the return type for main", or is it
Jacob's disease of trying to take away features that have been part of C
since the *first* ANSI C standard (1989) striking again?

Given the context offered by the quoted material, the meaning of
Papastefanos's statement is clear. The same cannot be said for
your statement about "Jacob's disease."
 
K

Kenny McCormack

Dingo said:
Given the context offered by the quoted material, the meaning of
Papastefanos's statement is clear. The same cannot be said for
your statement about "Jacob's disease."

Shhhh. If you disagree with a "regular", you risk being branded a,
gasp!, "troll".
 
R

Richard Heathfield

Kenny McCormack said:
Shhhh. If you disagree with a "regular", you risk being branded a,
gasp!, "troll".

Nonsense. I disagree with regulars a heck of a lot, and nobody with a brain
ever called me a troll.
 
K

Kenny McCormack

Kenny McCormack said:


Nonsense. I disagree with regulars a heck of a lot, and nobody with a brain
ever called me a troll.

I've never seen you disagree with them on a fundamental aspect of canon.
You are clearly in the fold.
 
W

Walter Roberson

Kenny McCormack said:
I've never seen you disagree with them on a fundamental aspect of canon.
You are clearly in the fold.

Canon... let's see, that would be:

2) I am the ISO C, thine Standards; Thou shalt have no other Standards
before me.

3) The internal organs of compilers are Icky, and Thou shalt not
reveal them here, for We art Squeemish.

4) Thou shalt not rely on unacknowledged UB or IB.

5) Thou shalt not demand answers to thine homework.

6) Thou shalt not remove attributions of quotations.

7) Thou shalt acknowledge the practical difficulties of using C99.

1) Thou shalt not top-post.
 
K

Keith Thompson

Canon... let's see, that would be:

2) I am the ISO C, thine Standards; Thou shalt have no other Standards
before me.

3) The internal organs of compilers are Icky, and Thou shalt not
reveal them here, for We art Squeemish.

4) Thou shalt not rely on unacknowledged UB or IB.

5) Thou shalt not demand answers to thine homework.

6) Thou shalt not remove attributions of quotations.

7) Thou shalt acknowledge the practical difficulties of using C99.

1) Thou shalt not top-post.

0) Thou shalt not feed the trolls.
 
R

Rod Pemberton

Keith Thompson said:
It's pure ignorance. No C standard has ever defined main as returning
void.

Chris Torek has covered this in the past... 'void main' predates all formal
C standards except K&R (1979). The links below indicate 'void' was for the
PCC compiler for 4.1c BSD (about 1981). It is a logical derivative (not
"pure ignorance") from the more common method of defining the void type for
early C compilers that were lacking 'void':

#define void int

If you search the Usenet archives via Google Groups advanced search from
1981-1989, you'll see that people struggled to find a good way to define
void:

#define void int
#define void (char *(0))
#define void (void)
etc...


FYI, the first Google Usenet msg use of 'void main' was in 1984:
http://groups.google.com/group/net.lang.c/msg/644f6489bb23c5ab?hl=en

FYI, the first Google Usenet use of '#define void' was in 1983:
http://groups.google.com/group/net.sources/msg/e127701c7c30168f?hl=en

FYI, the first Google Usent use of 'void' was in 1982:
http://groups.google.com/group/net.bugs.4bsd/msg/3ba57c1ff84d6835?hl=en

Chris Torek on void type:
http://groups.google.com/group/comp.lang.misc/msg/8119ec36057ce7c?hl=en

Probable source of Chris' comments about 'void' (i.e., PCC compiler for
4.1c BSD circa 1981):
http://www.faqs.org/docs/artu/c_evolution.html



Rod Pemberton
 
R

Rod Pemberton

Keith Thompson said:
It's pure ignorance. No C standard has ever defined main as returning
void.

Chris Torek has covered this in the past... 'void main' predates all formal
C standards except K&R (1979). The links below indicate 'void' was for the
PCC compiler for 4.1c BSD (about 1981). It is a logical derivative (not
"pure ignorance") from the more common method of defining the void type for
early C compilers that were lacking 'void':

#define void int

If you search the Usenet archives via Google Groups advanced search from
1981-1989, you'll see that people struggled to find a good way to define
void:

#define void int
#define void (char *(0))
#define void (void)
etc...


FYI, the first Google Usenet msg use of 'void main' was in 1984:
http://groups.google.com/group/net.lang.c/msg/644f6489bb23c5ab?hl=en

FYI, the first Google Usenet use of '#define void' was in 1983:
http://groups.google.com/group/net.sources/msg/e127701c7c30168f?hl=en

FYI, the first Google Usent use of 'void' was in 1982:
http://groups.google.com/group/net.bugs.4bsd/msg/3ba57c1ff84d6835?hl=en

Chris Torek on void type:
http://groups.google.com/group/comp.lang.misc/msg/8119ec36057ce7c?hl=en

Probable source of Chris' comments about 'void' (i.e., PCC compiler for
4.1c BSD circa 1981):
http://www.faqs.org/docs/artu/c_evolution.html



Rod Pemberton
 
D

Dik T. Winter

>
> Chris Torek has covered this in the past... 'void main' predates all formal
> C standards except K&R (1979). The links below indicate 'void' was for the
> PCC compiler for 4.1c BSD (about 1981).

Read that it is *not* about "void main"...
> FYI, the first Google Usenet msg use of 'void main' was in 1984:
> http://groups.google.com/group/net.lang.c/msg/644f6489bb23c5ab?hl=en

But was that correct?
> FYI, the first Google Usenet use of '#define void' was in 1983:
> http://groups.google.com/group/net.sources/msg/e127701c7c30168f?hl=en

That is #define VOID (char *)0, defining a null pointer.
> FYI, the first Google Usent use of 'void' was in 1982:
> http://groups.google.com/group/net.bugs.4bsd/msg/3ba57c1ff84d6835?hl=en

Yup, common use, casting the return of a function to void, even at that
time (to keep lint happy).

So, most of your references are about the type void, not about "void main".
 
K

Keith Thompson

Rod Pemberton said:
Chris Torek has covered this in the past... 'void main' predates all formal
C standards except K&R (1979). The links below indicate 'void' was for the
PCC compiler for 4.1c BSD (about 1981). It is a logical derivative (not
"pure ignorance") from the more common method of defining the void type for
early C compilers that were lacking 'void':

#define void int

If you search the Usenet archives via Google Groups advanced search from
1981-1989, you'll see that people struggled to find a good way to define
void:

#define void int
#define void (char *(0))
#define void (void)
etc...

Only the first of these makes any sense, as far as I can tell.
FYI, the first Google Usenet msg use of 'void main' was in 1984:
http://groups.google.com/group/net.lang.c/msg/644f6489bb23c5ab?hl=en

FYI, the first Google Usenet use of '#define void' was in 1983:
http://groups.google.com/group/net.sources/msg/e127701c7c30168f?hl=en

FYI, the first Google Usent use of 'void' was in 1982:
http://groups.google.com/group/net.bugs.4bsd/msg/3ba57c1ff84d6835?hl=en

Chris Torek on void type:
http://groups.google.com/group/comp.lang.misc/msg/8119ec36057ce7c?hl=en

Probable source of Chris' comments about 'void' (i.e., PCC compiler for
4.1c BSD circa 1981):
http://www.faqs.org/docs/artu/c_evolution.html

The "Chris Torek on void type" article you cited indicates that void
itself is older than I had realized; apparently it appeared around
1979, about the same time as "enum". "void*", on the other hand, was
an invention of the ANSI committee.

And as long as we're citing Chris Torek, here's something he wrote
2000-11-02,
<http://groups.google.com/group/comp.lang.c/msg/a39611f02ed05852?hl=en&>:
[...]
| I believe authors use "void main" primarily out of ignorance.
|
| There are lots of complicated rules you can fiddle with, like
| investigating exactly how your particular compiler and/or OS work,
| whether your program will ever be run from a batch file or makefile,
| and so on. But there is one very simple rule that always works:
|
| If you write "int main", and return 0, your program *will* work
| (provided you have no other bugs). If you write "void main", your
| program *might* work; but if you use "int main", it *will* work.
| Rather than worrying about when and where "void main" might work,
| why not use the one that is absolutely, positively, 100% sure?

Admittedly he was talking about current C, not the historical context.

But most of the reference you cite talk about the void type, not
specifically about "void main". Apparently ignorant uses of "void
main" are also older than I realized, but I see nothing indicating
that it was ever correct. Unless you can cite a standard or some
pre-ANSI de facto standard that says "void main" is valid, my point
stands.
 
R

Rod Pemberton

Keith Thompson said:
But most of the reference you cite talk about the void type, not
specifically about "void main". Apparently ignorant uses of "void
main" are also older than I realized, but I see nothing indicating
that it was ever correct. Unless you can cite a standard or some
pre-ANSI de facto standard that says "void main" is valid, my point
stands.

'int' is a type-specifier. 'void' is a type-specifier. 'void main' has
'void' used as a type-specifier instead of 'int' as a type-specifier. So,
the key issue is how was 'void' used as a type-specifier _before_ (i.e.,
from 1979-80) it was standardized as part of the C language. As I
indicated, 1) 'void', on compilers that didn't supply it, was defined to a
valid type-specifier, usually 'int' and 2) it was added to an important Unix
compiler of the era PCC. If programmers of the era understood that 'void'
was usually an alias for 'int', it wouldn't be an ignorant use, would it?
It would only be ignorant if they continued to use it long after C90.


Rod Pemberton
 
R

Rod Pemberton

Dik T. Winter said:
Read that it is *not* about "void main"...
....
So, most of your references are about the type void, not about "void
main".

You could search for other slightly later references too. I just posted the
earliest I could find. See also reply to KT.

Rod Pemberton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top