Take the 2-minute tour ×

Possible Duplicate:
difference between main(void) and main() in c

I know this is super basic and some other threads already talked about similar questions. But I have a book (Absolute Beginners Guide to C) where all the code is written within the function main(). The int is always left out. How is that possible to run? Is that ok with maybe an older version of stdio.h?
Here is the sample code:

#include <stdio.h>

main()  // not int main()
printf("This is it\n");
return 0;
share|improve this question

marked as duplicate by William Pursell, Anirudh Ramanathan, Mike, Blastfurnace, Mario Nov 24 '12 at 19:32


The main difference is time. In 1984, it was okay to write main(). In 2012, it is not okay, but many compilers accept it for backwards compatibility. Many authors believe it is still the mid-80s. –  William Pursell Nov 24 '12 at 17:09
See stackoverflow.com/questions/4260048/… –  onon15 Nov 24 '12 at 17:10
@WilliamPursell - it's definitely not a duplicate of that question. The question here does not have any relation main(void). –  Flexo Nov 24 '12 at 17:14

4 Answers 4

up vote 3 down vote accepted

I think the c89 standard will allow main() but c99 and above won't . You have to use int main() otherwise .

share|improve this answer
And I got c99. Now it makes sense why my compiler wouldn't like it. Thanks. –  Martin Nov 24 '12 at 17:19
btw try out this book for C : link –  Deepankar Bajpeyi Nov 24 '12 at 17:25
Actually you should use int main(void). –  effeffe Nov 24 '12 at 20:48

main() works but is confusing, in C the main function always returns an int, to specify exit status, so the correct syntax is int main(), but if you do not bother to set the exit status then main() is enough, but the good C books will always have int main().

share|improve this answer

For int main: has to return an integer value, which can be handled from the process who started your program..

eg you started your application using your terminal, that terminal process can get and handle that return value.

If you have void main: you dont have to return a value within your program.. because you dont need for example to check if everything went as you expected when execution finished... But i think despite that, a value is returned, because thats how all processes work(must return a value).. and in this case the value will always be 0 or 1..

So.. its a good practise to return a value that describe the result of your execution... eg 0 is everything okay, -1 something is wrong

In future, if you work with processes(your app is a process), you can handle those return codes..

Edit: if you dont "care" about return value, it's recomended to use void istead of no return type, so you dont have any compilation issues..

share|improve this answer
The OP doesn't have void main anywhere in sight. Not specifiying a return is not the same as void. –  Flexo Nov 24 '12 at 17:12
okay, edited post to clarify this... on a modern compiler without return type this wont compile, right @Flexo? –  Paschalis Nov 24 '12 at 17:20
"if you dont [sic] "care" about return value, it's recomended [sic] to use void istead [sic] of no return type," - it is most definitely not recommended to use void main - that's undefined behaviour. And no return type specified does not imply void anyway. –  Flexo Nov 24 '12 at 17:21

These kind of questions are highly standard-version dependent, so a general answer doesn't make much sense.

From a C89 draft (correct me if official C89 Standard is different, it's not freely avalaible):

The function called at program startup is named main. The implementation declares no prototype for this function. It can be defined with no parameters:




C99 and C11 standard say the same but they add something at the and:


or equivalent;[9] or in some other implementation-defined manner.


In general things that are not defined from the standard leads to undefined behavior, so that code is UB in C89/C90, and it could be valid in C99 and C11, but that's implementation-defined.

P. S.: as you can see, you should also add void in the parameters list, without it the behavior is defined as above.

share|improve this answer
