0

In a book (don't remember which one) they used:

void main(void)

In school I learned:

int main(void)

Is there any case when void main(void) is actually correct? Or at least not explicitly wrong?

EDIT: According to the proposed answers since C99 it is not correct. What about earlier versions? Is it explicitly wrong or just nothing said about it? Why do C compiler not complain about it?

  • 4
    Throw away that book, please. – Yu Hao Sep 10 '13 at 07:42
  • [What's the correct declaration of main()?](http://c-faq.com/ansi/maindecl.html) – Dayal rai Sep 10 '13 at 07:46
  • This is one of the less-common times I must admit your school taught you correctly. And buy a different book. – WhozCraig Sep 10 '13 at 07:57
  • Most C references, both online and in books, are *crap*. My go-to reference since 1986 has been [Harbison & Steele](http://careferencemanual.com/). Even they have some minor issues, but as a rule it's one of the best C references around. – John Bode Sep 10 '13 at 11:36

3 Answers3

2

Never, ever use void main(void). It is not standard.

Always use one of

int main(void);

int main();

int main(int argc, char **argv);

int main(int argc, char *argv[]);

And the best use of said book is to use it to light your first fire for the winter.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

Per the C standard

C99 §5.1.2.2.1 Program startup

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;10 or in some other implementation-defined manner.

10) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char **argv, and so on.

The closing clause grants implementations their own vices, which is to say, if a program does not follow this, it is no longer standard-compliant and instead reliant on the implementation for compatibility. If you want your main() to work everywhere, follow one of these and you'll be ok.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Note that "implementation-defined" means that the compiler must document that `void main()` is a legal signature; otherwise, the behavior is undefined. – John Bode Sep 10 '13 at 11:32
0

void main(void) is allowed by some(all?) C compilers. However, it should not be used anyway. Because at least since C99 is it not allowed. However, I did not find a C compiler which complains about it.

E.g. void.c:

#include <stdio.h>
void main(void)
{
   printf("hello world");
}

gcc void.c

Compiles. Also check http://www.compileonline.com/compile_c_online.php

So in conclusion (even though I do not find references): In earliest C versions void main(void) was probably not forbidden.

However: Without specifying a return value, you just do not now what your program returns. So, standard or not, correct or false, do not use it, because it makes your program non-deterministic.

Jack Miller
  • 6,843
  • 3
  • 48
  • 66