int main(void) is better than int main()

B

Bill - Cincinnati, OH USA

You are not a troll. The quest for knowledge is not troll-ish.
 
J

Jim Langston

You are not a troll. The quest for knowledge is not troll-ish.

He did not ask a question, he made a statment. One that is contradictory to
common knowledge.

If he had asked which is better, then that is different. He is a troll.
 
J

James Kanze

There is no difference.

As far as the compiler is concerned, no. As far as anyone who
reads the code is concerned, the first marks you as a C hacker,
who doesn't understand C++.

(FWIW: even the C experts find the first an abomination. To be
lived with in C, because there's no other alternative.)
 
L

Lars Uffmann

Now we're getting to where it becomes interesting :)

James said:
As far as the compiler is concerned, no. As far as anyone who
reads the code is concerned, the first marks you as a C hacker,
who doesn't understand C++.

Maybe I don't understand C++ then - after all I did start with Ansi C -
I always thought "int main (void)" was the way to go because - as you
stated later in your post - compilers would complain otherwise. As for
C++, if there is no difference on compiler side, I don't see what the
problem is keeping void in there, even if it is not required. Please
enlighten me...

Best Regards,

Lars
 
E

Erik Wikström

Now we're getting to where it becomes interesting :)



Maybe I don't understand C++ then - after all I did start with Ansi C -
I always thought "int main (void)" was the way to go because - as you
stated later in your post - compilers would complain otherwise. As for
C++, if there is no difference on compiler side, I don't see what the
problem is keeping void in there, even if it is not required. Please
enlighten me...

It slows down reading (and writing), foo() clearly indicates that there
are no arguments, foo(void) at a glance looks like it takes an argument
and you have to see that the type is void before you understand that
there are no arguments. Why should we have to tell the compiler that
there are no arguments when an empty list works just as well, and is
more consistent.
 
J

jkherciueh

Obviously, I don't understand C++, too. I, however, came from Pascal and
Modula 2. I have never programmed in C.
It slows down reading (and writing),

I cannot not confirm that. But then again, I am used to much more verbose
languages.
foo() clearly indicates that there
are no arguments, foo(void) at a glance looks like it takes an argument
and you have to see that the type is void before you understand that
there are no arguments.

I don't think the psychology of reading code (as opposed to the logic of
parsing) works that way.
Why should we have to tell the compiler that
there are no arguments when an empty list works just as well, and is
more consistent.

On the other hand, I find it very nice that

foo( void )

does not look like the function call and clearly indicates that I am looking
at a declaration. For that reason, it also increases grep-ability of the
code.


Best

Kai-Uwe Bux
 
L

Lars Uffmann

@Erik: point taken, at this point I was ready to accept this and apply
it to my own code, but then...

On the other hand, I find it very nice that
foo( void )
does not look like the function call and clearly indicates that I am looking
at a declaration. For that reason, it also increases grep-ability of the
code.


Kai-Uwe also has a very good point there. Especially on the grep-ability :)

I'm thinking especially about forward declarations that will look
exactly like a function call. Have to chew on this for a bit - unless
someone comes up with better arguments ;) Thanks to both of you!

Best Regards,

Lars
 
J

James Kanze

Lars said:
Now we're getting to where it becomes interesting :)
Maybe I don't understand C++ then - after all I did start with
Ansi C - I always thought "int main (void)" was the way to go
because - as you stated later in your post - compilers would
complain otherwise.

When I first started C++, "int main( void )" wouldn't work in
either C or C++. In C, you could define main as either:
int main() { }
or
int main( argc, argv )
int argc ;
char** argv ;
{ }
if you had arguments. The C I learned (around 1982) didn't have
prototypes. (You could also leave out the initial int, and a
lot of programmers did.)

And when I first learned C++, some of the C++ compilers I used
wouldn't accept int main( void ) { } (which wasn't legal in the
original C++).
As for C++, if there is no difference on compiler side, I
don't see what the problem is keeping void in there, even if
it is not required. Please enlighten me...

Because it doesn't belong there. It wasn't there to begin with.
It got added by C when C adopted prototypes, because int main()
already had a defined meaning (which was different). C++ then
adopted it, but only for reasons of C compatibility.

It's ugly, and it's a lie.
 
J

James Kanze

Obviously, I don't understand C++, too. I, however, came from
Pascal and Modula 2. I have never programmed in C.

In Pascal and Modula 2, of course, you don't use anything to
indicate no parameters.
I cannot not confirm that. But then again, I am used to much
more verbose languages.

It's not a question of verbosity. It's a question of avoiding
special cases. The parameters of a function are indicated by a
comma separated list. No parameters is an empty list, e.g. (),
one parameter a list with one element, etc. f(void) is a very
special case, a list with one element which means that there are
no parameters. It's not logical.
I don't think the psychology of reading code (as opposed to
the logic of parsing) works that way.

I think it does.
On the other hand, I find it very nice that
foo( void )
does not look like the function call and clearly indicates
that I am looking at a declaration. For that reason, it also
increases grep-ability of the code.

Which would be a valid argument if it extended to functions
taking arguments.

There are other coding conventions which facilitate the
grepability: the function name in a declaration always starts in
column one, in a function invocation, never.

(Of course, if your code is well organized and documented, you
don't need grepability anyway.)
 
I

Ian Collins

Lars said:
Now we're getting to where it becomes interesting :)



Maybe I don't understand C++ then - after all I did start with Ansi C -
I always thought "int main (void)" was the way to go because - as you
stated later in your post - compilers would complain otherwise. As for
C++, if there is no difference on compiler side, I don't see what the
problem is keeping void in there, even if it is not required. Please
enlighten me...
It doesn't belong in C++, but it does in C where K&R style function
declarations are still legal. In C, f() and f(void) have different
meanings, where they do not in C++.
 
L

Lars Uffmann

Ian said:
It doesn't belong in C++, but it does in C where K&R style function
declarations are still legal. In C, f() and f(void) have different
meanings, where they do not in C++.

After reading James' comments - especially on the grepability - that
function declarations should (and in my case do) start in the first
column, I've decided to use "int main() {}" in the future.
But could you explain the different meanings of f() and f(void) in C to
me? Or rather - point me to a source where it is explained?

Thanks in advance!

Lars
 
I

Ian Collins

Lars said:
After reading James' comments - especially on the grepability - that
function declarations should (and in my case do) start in the first
column, I've decided to use "int main() {}" in the future.
But could you explain the different meanings of f() and f(void) in C to
me? Or rather - point me to a source where it is explained?
The C standard :)

C compilers accept old K&R style function declarations:

void f(); /* Declares a function with unknown parameters */
void f(void); /* Declares a function with no parameters */
 
L

Lars Uffmann

Ian said:
void f(); /* Declares a function with unknown parameters */

o_O unknown parameters! :) I've always meant to use that at some point -
will have to have a look into the equivalent in C++. Thanks a ton!

Best Regards,

Larsa
 

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