main() in C90

I

Ioannis Vranos

Hi, in C90 is "int main()" valid, the same as "int main(void)" and "int
main(int argc, char *argv[])"?


AFAIK in C99 only "int main(void)" and "int main(int argc, char *argv[])
- and the **argv syntax" are the only valid ones.
 
W

Walter Roberson

Hi, in C90 is "int main()" valid, the same as "int main(void)" and "int
main(int argc, char *argv[])"?
AFAIK in C99 only "int main(void)" and "int main(int argc, char *argv[])
- and the **argv syntax" are the only valid ones.

The relevant rules did not change between C90 and C99. Both permit
"or equivilents".

C99 still permits the old style non-prototype definition of functions:
e.g.,

int foo(bar)
int bar;
{ ... }

is still accepted as well as the newer

int foo(int bar) { ... }

Using (void) in a function declaration or definition
signals lack of parameters, and by transforming back to the old style,
int main(void) would be equivilent to

int main( /* no parameters here */ )
/* no declarations here */

which is more simply written int main()


What is -not- still valid in C99 would be to leave out the int part:
functions no longer default to int return type if no return type
is specified.
 
R

Richard Heathfield

Ioannis Vranos said:
Hi, in C90 is "int main()" valid,

Yes. It is also valid in C99.
the same as "int main(void)" and "int
main(int argc, char *argv[])"?

Yes, those are both valid in C90 and in C99.
AFAIK in C99 only "int main(void)" and "int main(int argc, char *argv[])
- and the **argv syntax" are the only valid ones.

No, int main() is also valid.

Also, any exact equivalent of any of the above is also valid. For example:

typedef int cat;
typedef char **mouse;

cat main(cat run, mouse hide)
 
K

Keith Thompson

Richard Heathfield said:
Ioannis Vranos said:
Hi, in C90 is "int main()" valid,

Yes. It is also valid in C99.
the same as "int main(void)" and "int
main(int argc, char *argv[])"?

Yes, those are both valid in C90 and in C99.
AFAIK in C99 only "int main(void)" and "int main(int argc, char *argv[])
- and the **argv syntax" are the only valid ones.

No, int main() is also valid.

Also, any exact equivalent of any of the above is also valid. For example:

typedef int cat;
typedef char **mouse;

cat main(cat run, mouse hide)

The wording in C99 is such that an argument could be made that "int
main()" is not valid. But there's considerable evidence that authors
of the standard intended it to be valid. See
<http://groups.google.com/group/comp.std.c/browse_thread/thread/fef53cc781d555a4/2d61b64c0ee672e3>
if you're really bored, or want to be.

In practice, "int main() { /* ... */ }" is ok, but IMHO
"int main(void) { /* ... */ }" is stylistically better because it's
more explicit.
 
J

Jack Klein

Hi, in C90 is "int main()" valid, the same as "int main(void)" and "int
main(int argc, char *argv[])"?
AFAIK in C99 only "int main(void)" and "int main(int argc, char *argv[])
- and the **argv syntax" are the only valid ones.

The relevant rules did not change between C90 and C99. Both permit
"or equivilents".

C99 still permits the old style non-prototype definition of functions:
e.g.,

int foo(bar)
int bar;
{ ... }

is still accepted as well as the newer

int foo(int bar) { ... }

Using (void) in a function declaration or definition
signals lack of parameters, and by transforming back to the old style,
int main(void) would be equivilent to

int main( /* no parameters here */ )
/* no declarations here */

which is more simply written int main()

Not quite right. There is a difference between:

type func_name()

....and:

type func_name(void)

....in both function declarations that are definitions as well, and in
function declarations that are not definitions.

In terms of a function declaration that defines the function, empty
parentheses tell the compiler when compiling the body of the function
that it accepts no parameters.

But regardless of whether the declaration is a definition or not, the
empty parentheses do not provide a prototype, and only specify that
the function accepts an unspecified but fixed number and type of
parameters.
What is -not- still valid in C99 would be to leave out the int part:
functions no longer default to int return type if no return type
is specified.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
K

Keith Thompson

Keith Thompson said:
In practice, "int main() { /* ... */ }" is ok, but IMHO
"int main(void) { /* ... */ }" is stylistically better because it's
more explicit.

Here's another reason to prefer "int main(void)" rather than "int main()"
(though it's not a *great* reason).

This:

int main(void) {
main(42);
return 0;
}

requires a diagnostic on the recursive call, whereas this:

int main() {
main(42);
return 0;
}

doesn't. In practice, this matters only if you call main(), which is
almost always a bad idea anyway. Note that the call to main() could
be from another function rather than from main() itself.
 
B

Billy Bong

Hi, in C90 is "int main()" valid, the same as "int main(void)" and "int
main(int argc, char *argv[])"?


AFAIK in C99 only "int main(void)" and "int main(int argc, char *argv[])
- and the **argv syntax" are the only valid ones.

In reality, "void main()" is also valid.

Some compilers issue a warning for this, but you can safely ignore this
warning in 99.999999% or six sigma of the time.

You're more likely to be struck by lightning than come across a compiler
that does not both accept and work with no problems with "void main()".

Microsoft supports "void main()", so that should tell you something,
provided you're living in the real world, of course.
 
B

Billy Bong

Richard Heathfield said:
Ioannis Vranos said:
Hi, in C90 is "int main()" valid,

Yes. It is also valid in C99.
the same as "int main(void)" and "int main(int argc, char *argv[])"?

Yes, those are both valid in C90 and in C99.
AFAIK in C99 only "int main(void)" and "int main(int argc, char
*argv[]) - and the **argv syntax" are the only valid ones.

No, int main() is also valid.

Also, any exact equivalent of any of the above is also valid. For
example:

typedef int cat;
typedef char **mouse;

cat main(cat run, mouse hide)

The wording in C99 is such that an argument could be made that "int
main()" is not valid. But there's considerable evidence that authors of
the standard intended it to be valid.

How credible should we consider this evidence, given that the same
authors put gets() in the standard?
 
C

CBFalconer

Billy said:
.... snip ...

Microsoft supports "void main()", so that should tell you
something, provided you're living in the real world, of course.

Yes, it tells us that MS doesn't understand the C language.
 
R

Richard Bos

Billy Bong said:
Hi, in C90 is "int main()" valid, the same as "int main(void)" and "int
main(int argc, char *argv[])"?

AFAIK in C99 only "int main(void)" and "int main(int argc, char *argv[])
- and the **argv syntax" are the only valid ones.

You've forgotten a variation. In C89, main() is also valid, as an
old-style declaration. I'm not sure about main(void) and main(int argc,
char **argv), since they seem to be a mix of old-style and prototype,
but since they're perverse anyway I'm not going to look it up. Any of
those are, in any case, not allowed under C99.
In reality, "void main()" is also valid.

No, it isn't. It may be widely accepted, but then, so is jaywalking.
Some compilers issue a warning for this, but you can safely ignore this
warning in 99.999999% or six sigma of the time.

You've never run a program from a makefile.
Microsoft supports "void main()",

Micro$oft supports

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)

which tells you all you need to know about assuming that Micro$oft cares
about reasonable, legible code.

Richard
 
R

Richard Heathfield

Richard Bos said:

In C89, main() is also valid, as an
old-style declaration. I'm not sure about main(void) and main(int argc,
char **argv)

They are both legal in C89, being exact equivalents of their explicit int
counterparts.

[...] Any of those are, in any case, not allowed under C99.

Right.

<snip>
 
C

Chris Hills

Richard Bos said:
Billy Bong said:
Hi, in C90 is "int main()" valid, the same as "int main(void)" and "int
main(int argc, char *argv[])"?

AFAIK in C99 only "int main(void)" and "int main(int argc, char *argv[])
- and the **argv syntax" are the only valid ones.

You've forgotten a variation. In C89, main() is also valid, as an
old-style declaration. I'm not sure about main(void) and main(int argc,
char **argv), since they seem to be a mix of old-style and prototype,
but since they're perverse anyway I'm not going to look it up. Any of
those are, in any case, not allowed under C99.
In reality, "void main()" is also valid.

No, it isn't. It may be widely accepted, but then, so is jaywalking.

The only place void main() is should to be valid is in a self
hosted/freestanding (usually) embedded system where there is no RTOS or
in deed anything to return to. Otherwise it should be int main(.....
Micro$oft supports

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)

which tells you all you need to know about assuming that Micro$oft cares
about reasonable, legible code.

Or standards for that matter.

How many M$ Engineers does it take to change a light bulb..... :)
 
R

Richard Heathfield

Chris Hills said:
Or standards for that matter.

What's non-standard about it? It looks to me like a perfectly good C
program entry point for a freestanding implementation.
 
K

Keith Thompson

Billy Bong said:
How credible should we consider this evidence, given that the same
authors put gets() in the standard?

The authors of the standard didn't invent gets(). It was existing
practice at the time the standard was first developed. I'm not saying
that including it in the standard was a good idea, but dropping it
would not have been easy. And it's finally been deprecated in C99 TC3
(better late than never).

The evidence includes statements in the thread I cited by at least one
committee member (which was more about fixing some unclear wording
than about questioning the actual intent).

If you want to discount the credibility of the committee because of
one unsafe function (which nobody is actually required to use), that's
up to you.
 
R

Randy Howard

The authors of the standard didn't invent gets(). It was existing
practice at the time the standard was first developed. I'm not saying
that including it in the standard was a good idea, but dropping it
would not have been easy. And it's finally been deprecated in C99 TC3
(better late than never).

Yeah, one sentence, without adornment, 7.26.9, that reads:
"The gets function is obsolescent, and is deprecated."

How bold.

Earlier they provide all of this, presumably so the Forward reference
can be added.

7.19.7.7 The gets function

Synopsis

1 #include <stdio.h>
char *gets(char *s);

Description

2 The gets function reads characters from the input stream pointed
to by stdin, into the array pointed to by s, until end-of-Þle is
encountered or a new-line character is read. Any new-line
character is discarded, and a null character is written
immediately after the last character read into the array.

Returns

3 The gets function returns s if successful. If end-of-Þle is
encountered and no characters have been read into the array, the
contents of the array remain unchanged and a null pointer is
returned. If a read error occurs during the operation, the array
contents are indeterminate and a null pointer is returned.

Forward references: future library directions (7.26.9).


Note: they didn't mention anything about why not to use it, what
dangers it might have, or even flag it as deprecated in the main
function description. Hardly the flashing warning light it deserves.
 
J

jacob navia

Randy said:
Note: they didn't mention anything about why not to use it, what
dangers it might have, or even flag it as deprecated in the main
function description. Hardly the flashing warning light it deserves.

The asctime() function will make a buffer overflow for any
year/month/date that is out of range but they failed to specify
valid ranges.

When I complained about this they pointed me to a decision where they
said that "corrupting memory can happen" and refused to fix this.
 
B

Bart C

Richard Bos said:
Billy Bong said:
Hi, in C90 is "int main()" valid, the same as "int main(void)" and "int
main(int argc, char *argv[])"?

AFAIK in C99 only "int main(void)" and "int main(int argc, char
*argv[])
- and the **argv syntax" are the only valid ones.

You've forgotten a variation. In C89, main() is also valid, as an
old-style declaration. I'm not sure about main(void) and main(int argc,
char **argv), since they seem to be a mix of old-style and prototype,
but since they're perverse anyway I'm not going to look it up. Any of
those are, in any case, not allowed under C99.
In reality, "void main()" is also valid.

No, it isn't. It may be widely accepted, but then, so is jaywalking.

I found it odd that "void main()" gets 5 times as many hits on Google as
"int main(void)".

Might be easier to change the Standard than those 3 million references.

Bart
 
R

Randy Howard

The asctime() function will make a buffer overflow for any
year/month/date that is out of range but they failed to specify
valid ranges.

When I complained about this they pointed me to a decision where they
said that "corrupting memory can happen" and refused to fix this.

Was this an official response, or something from a thread in
comp.std.c?
 
R

Richard Tobin

The authors of the standard didn't invent gets(). It was existing
practice at the time the standard was first developed. I'm not saying
that including it in the standard was a good idea, but dropping it
would not have been easy. And it's finally been deprecated in C99 TC3
(better late than never).
[/QUOTE]
Yeah, one sentence, without adornment, 7.26.9, that reads:
"The gets function is obsolescent, and is deprecated."

How bold.

It's a standard, not a user guide. The people who need warning about
gets() are not likely to be looking in the standard for guidance.

And of course the Rationale explains what's wrong with it.

-- Richard
 
R

Richard Tobin

No, it isn't. It may be widely accepted, but then, so is jaywalking.
[/QUOTE]

Jaywalking is not illegal, at least in this country. It's more or
less the opposite of "void main()": legal but unsafe.
I found it odd that "void main()" gets 5 times as many hits on Google as
"int main(void)".

Googling for "void main()" is identical to googling for "void main".

Two thirds of them are in fact "static void main", so they are probably
not C code at all.
Might be easier to change the Standard than those 3 million references.

I found zillions of references to Al Gore saying he invented the
Internet. Perhaps we should get him to say it, so they'd be true.

-- Richard
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top