473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2385
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.