472,958 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 2365
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: rahulsinner | last post by:
Hello everyone, I have noticed everyone using "int main(void)".But doesnt the standard pronounces it as "int main(int argc,char *argv)".And if i don't specify arguments,why can't i simply put...
37
by: Army1987 | last post by:
Is that in the object line a conforming program? If so, why? If not, why? I'd expect it to be much like int main(void) { for (;;); } But if I compile it with lcc-win32 and run it in its...
36
by: x_knifer_x | last post by:
read the subject it says it all int main(void) is better than int main()
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.