468,539 Members | 1,480 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,539 developers. It's quick & easy.

int main() or int main(void)?

Hi, guys!
I met a problem:
Should I use "int main()" or "int main(void)", is that a kind of
"style"
problem?
And which is the standard C99 recommend?
If it's a "style" one, where can I get the standard or nearly
standard C
style? K&R or what?

Thanks a lot!

Frederick Ding
2005-12-3

Dec 3 '05 #1
10 2157
Frederick Ding said:
Hi, guys!
I met a problem:
Should I use "int main()" or "int main(void)", is that a kind of
"style"
problem?


int main(void) is a full function prototype, and thus is preferable to a
mere declaration.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 3 '05 #2
Thanks!
I does not mean the prototype, but full function:
I think the main function is only used only once in a .c file, so
it does not need
a pre-declare or something like that.
Am I right? Thanks for your help!
Should I use:
#include <stdio.h>
int main()
{
....
return 0;
}

or this:
#include <stdio.h>
int main(void)
{
....
return 0;
}
Thanks!

Dec 3 '05 #3
I checked the K&R(2nd Edition) just now, and find that it is really
obsolete, the function "main" in it only writes : main() , even without
a return value type.
Maybe I should drop the book and pickup a new one that conform to C99
standard.

Dec 3 '05 #4
Frederick Ding said:
Thanks!
I does not mean the prototype, but full function:


Then you don't understand what "function prototype" means. A function
prototype is a function declaration that lists its parameters. A function
definition is also a function declaration. A prototype allows the compiler
to do type checking and argument counting (except, of course, in the case
of variable argument lists). Consider this extremely simplified example of
why function prototypes are important:

int mani(int i)
{
return i / 2;
}

int main(void) /* <--- this is a prototype */
{
return main(1); /* mani(1) was intended. The compiler
* can and must catch this error. */
}

Now consider this:

int mani(int i)
{
return i / 2;
}

int main() /* <--- this is NOT a prototype */
{
return main(1); /* mani(1) was intended. The compiler
* is not required to catch this error.
* gcc certainly didn't when I tried it
* with -W -Wall -ansi -pedantic */
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 3 '05 #5
Frederick Ding said:
I checked the K&R(2nd Edition) just now, and find that it is really
obsolete, the function "main" in it only writes : main() , even without
a return value type.
Well, there's an implicit int there. Nevertheless, you are right - this is
what I would consider to be poor style. K&R is not a perfect book. It is
merely the very best C book available.
Maybe I should drop the book and pickup a new one that conform to C99
standard.


You would be doing yourself a disservice. K&R remains far and away the best
available book on C. It's high time someone wrote a better one, but they
have not done so yet.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 3 '05 #6
Thank you very much, now I see.

Dec 3 '05 #7

"Frederick Ding" <di*******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi, guys!
I met a problem:
Should I use "int main()" or "int main(void)", is that a kind of
"style"
problem?
And which is the standard C99 recommend?
If it's a "style" one, where can I get the standard or nearly
standard C
style? K&R or what?


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.

C: A Reference Manual is a good book - as is K&R.

Also, check out the K&R errata
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html

Dec 3 '05 #8

"Richard Heathfield" <in*****@invalid.invalid> wrote
You would be doing yourself a disservice. K&R remains far and away the
best
available book on C. It's high time someone wrote a better one, but they
have not done so yet.

Someone did have a very good go with C Unleashed.
It's more a second book on C than a primer, though.
Dec 3 '05 #9
In article <11**********************@o13g2000cwo.googlegroups .com>,
Frederick Ding <di*******@gmail.com> writes
I checked the K&R(2nd Edition) just now, and find that it is really
obsolete, the function "main" in it only writes : main() , even without
a return value type.
Maybe I should drop the book and pickup a new one that conform to C99
standard.


It depends which compiler you are using. C99 compiler are few and far
between.

Return values should ALWAYS be used except in some self hosted systems
where there is no OS to return to.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

Dec 4 '05 #10
Frederick Ding wrote:

I checked the K&R(2nd Edition) just now, and find that it is really
obsolete, the function "main" in it only writes : main() ,
even without
a return value type.
Maybe I should drop the book and pickup a new one that conform to C99
standard.


At any rate, download the errata,
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html

K&R 1, is older than void,
and a lot of things like int main(void)
didn't make it into K&R 2.

If you have K&R2 and the errata, and clc and
(N869 or a copy of one or both standards)
you should be OK.

--
pete
Dec 4 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.