int main() or int main(void)?

F

Frederick Ding

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
 
R

Richard Heathfield

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.
 
F

Frederick Ding

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!
 
F

Frederick Ding

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.
 
R

Richard Heathfield

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 */
}
 
R

Richard Heathfield

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.
 
P

pemo

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?
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
 
M

Malcolm

Richard Heathfield said:
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.
 
C

Chris Hills

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.
 
P

pete

Frederick 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.
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.
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top