int main() vs int main(void) 
Author Message
 int main() vs int main(void)

Okay, void main() is {*filter*}, but there's something else I've been
wondering about, and I can't find it in the FAQ.

Is there a difference between

  int main()
  int main(void)

?  () is equivalent to (void), right?  Does it matter?  Am I worried about
nothing?

Cap'n Tard,
Jim

.-----------.--------------------.-----------------------------------.

`-----------^--------------------^-----------------------------------'



Sun, 26 Jul 1998 03:00:00 GMT  
 int main() vs int main(void)

Quote:

>Okay, void main() is {*filter*}, but there's something else I've been
>wondering about, and I can't find it in the FAQ.
>Is there a difference between
>  int main()
>  int main(void)
>?  () is equivalent to (void), right?  Does it matter?  Am I worried about
>nothing?

1) It depends on whether you are talking about C or about C++.
2) It depends on whether you are talking about a prototype or a
   function definition
3) It depends on wether you consider the different meanings in
   a prototype as relevant or not. Since you usually don't
   provide a prototype for main(), the answer seems to be yes.

Kurt
--
| Kurt Watzka                             Phone : +49-89-2180-6254




Mon, 27 Jul 1998 03:00:00 GMT  
 int main() vs int main(void)


Quote:
>Okay, void main() is {*filter*}, but there's something else I've been
>wondering about, and I can't find it in the FAQ.

>Is there a difference between

>  int main()
>  int main(void)

>?  () is equivalent to (void), right?  Does it matter?  Am I worried about
>nothing?

My understanding is that in C++, there is absolutely no difference.  But in C,
there is a slight difference.  I think the proper example is the following...

void f(void);
void f(){};

This two line program is the key.  It compile in C++ and doesn't in C.  You
have to change the program to...

void f(void);
void f(void){};

..for it to compile in C.  But, I'm not an expert on the subject.  So, I'd
confirm this by compiling the programs yourself.  No other differences exist
in my books.

Agrivar



Mon, 27 Jul 1998 03:00:00 GMT  
 int main() vs int main(void)

Quote:
>My understanding is that in C++, there is absolutely no difference.  But in C,
>there is a slight difference.  I think the proper example is the following...

>void f(void);
>void f(){};

>This two line program is the key.

This is by no means a C program.  Why?

Quote:
>It compile in C++ and doesn't in C.

The only reason it may not compile in C is because it contains an empty
declaration (can you see it?).

Otherwise it is a legal C translation unit and it does compile (after
removing the empty declaration):

    ues5:~/tmp 25> cat test.c
    void f(void);
    void f(){}
    ues5:~/tmp 26> cc -c test.c
    ues5:~/tmp 27> c89 -c test.c
    ues5:~/tmp 28> gcc -pedantic -c test.c
    ues5:~/tmp 29>

Quote:
>You have to change the program to...

>void f(void);
>void f(void){};

>..for it to compile in C.  

This one is just as broken as the first one and just as correct after
the empty declaration is removed.

Quote:
>But, I'm not an expert on the subject.

A very good reason to keep your mouth shut, in order to avoid confusing
other people with your misconceptions.

Quote:
>So, I'd confirm this by compiling the programs yourself.

It's better to search the confirmation in books, not in compilers.

Quote:
>No other differences exist in my books.

If there is any difference between your examples in your books, then those
books are not about the C programming language.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Mon, 27 Jul 1998 03:00:00 GMT  
 int main() vs int main(void)

Quote:
>Okay, void main() is {*filter*}, but there's something else I've been
>wondering about, and I can't find it in the FAQ.

>Is there a difference between

>  int main()
>  int main(void)

>?  () is equivalent to (void), right?  Does it matter?  Am I worried about
>nothing?

As part of a function definition, the two forms are 100% equivalent.

As function declarations, the second is a prototype while the first
allows main to be called with any (number and type of) arguments.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Mon, 27 Jul 1998 03:00:00 GMT  
 int main() vs int main(void)

Quote:


> writes:

>>Okay, void main() is {*filter*}, but there's something else I've been
>>wondering about, and I can't find it in the FAQ.

>>Is there a difference between

>>  int main()
>>  int main(void)

>>?  () is equivalent to (void), right?  Does it matter?  Am I worried about
>>nothing?

>As part of a function definition, the two forms are 100% equivalent.

>As function declarations, the second is a prototype while the first
>allows main to be called with any (number and type of) arguments.

The first doesn't tell you the correct number and types of the
arguments you must supply, or let the compiler check them. However the
arguments you supply must still match those in the function definition if
you want to avoid undefined behaviour.

It is however correct to say that the first form as a declaration is
compatible with any (fixed argument) declaration/definition of main that
returns int.

--
-----------------------------------------


-----------------------------------------



Tue, 28 Jul 1998 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. main() vs int main() vs int main(void)

2. what's the difference between void main(void) and int main(void)

3. int main vs. void main

4. newbie question....int main, void main, main

5. void main(void) or int main() ??

6. void main() or int main()?

7. void main or int main?

8. int main() OR void main()...

9. void main() <-> int main()

10. void main() or int main()?

11. int main() or void main()

12. void main() v/s int main()

 

 
Powered by phpBB® Forum Software