nightshade
nightshade

Reputation: 638

Why do I see so much C code that omits a return type for main? Why does it work?

I went to this website that compiles C online and saw the main function declared without a return type.

I'm aware of some questions concerning this topic here, but I didn't find any about omitting the return type. I tried to compile the code using gcc and it worked.

Does this mean that if we don't put a return type on main, it will assume it is int (or any other type)?

Upvotes: 1

Views: 158

Answers (3)

haccks
haccks

Reputation: 106012

If the return type is omitted in C89, the function is presumed to return a value of type int. In C99/11, it is not legal to omit the return type of a function. The signature of main is defined as:

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

Compile your code with -std=c99 flag and your compiler will raise a warning about the omitted return type.

Upvotes: 1

Ulrich Eckhardt
Ulrich Eckhardt

Reputation: 17415

Try to turn on warnings, GCC should tell you that this is normally forbidden:

test.c:1:1: warning: return type defaults to ‘int’ [-Wreturn-type]
 main()
 ^

This used to have a defined meaning in older C, where the default type of functions was int. Compilers still support it in order not to break code and because it is not a big issue, i.e. the code isn't suddenly ambiguous.

Upvotes: 1

Cody Gray
Cody Gray

Reputation: 244732

The C89 standard, to preserve compatibility with the original K&R version of C that did not have function prototypes as we now know them, allowed functions to implicitly return int. Any function that was declared without an explicit return type (i.e., void, float, etc.) was assumed by the compiler to return int.

Thus, when the main function was declared without a return type, it was assumed to return type int. All was well and good, since main was supposed to return int, according to the standard.

However, this changed in C99. The default/implicit int rule was removed from the language specification. Functions without an explicit return type are no longer assumed to return int.

That means that for any modern compiler, adhering to the current version of the C language specification, a declaration of main without a return type is invalid.

As for why it works on GCC, this is because by default, GCC still adheres to the C89/C90 standard, unless you explicitly specify -std=c99 as a compiler flag. And for why you still see this online, well, there are two reasons. The first is the one I've already given: it was legal in older versions of the language specification, and lots of old code hasn't been updated. The second reason is that, unfortunately, there is lots of bad C code online and in books. You may have just found some.

Upvotes: 8

Related Questions