15

Is there any reason why I never see main's prototype declared in C programs, ie:

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

int main(int argc, char* argv[])
{
    return 0;
}

Always seemed inconsistent..

Rob W
  • 341,306
  • 83
  • 791
  • 678
user318904
  • 2,968
  • 4
  • 28
  • 37

5 Answers5

11

C language standard, draft n1256:

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

Emphasis mine.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • 6
    as far as I can tell, this is irrelevant to the question: user-level code is not considered part of the implementation... – Christoph Feb 16 '11 at 19:40
  • @Christoph: I'm with you; nothing stops user code from declaring a prototype for the `main` function. – Matteo Italia Feb 16 '11 at 19:59
  • 1
    Re comments: By "implementation" the standard means the particular version of the compiler & system you are running on. As in features that it says must or may be "implementation defined" by a particular compiler/system. Eg the further options for declaring main than the given two. Barring such an extension, main must be declared as the standard says. – philipxy Nov 09 '14 at 22:02
  • 2
    The quote is indeed irrelevant and it doesn't answer the question. – Lundin Oct 13 '17 at 11:43
10

Declaring the prototype means that you want to call it elsewhere, which makes no sense for main() function.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • _which makes no sense for main() function_ Why, the standard allows it? – jinawee Nov 05 '18 at 14:01
  • 2
    @jinawee Allowed by the standard and making sense is different. Why would you need the the main function declaration elsewhere? – CharlesB Nov 07 '18 at 13:15
2

There's no need for a prototype, since main shouldn't be called by other procedures (and in C++ calling main is actually forbidden).

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • 3
    as far as I can tell, the C99 standard does not disallow calling `main()` from user-level code; in fact, the wording of section 5.1.2.2.3 implies that this is indeed the case: where there is an "initial call to the main function", one expects that subsequent calls are possible... – Christoph Feb 16 '11 at 19:53
  • @Christoph: indeed I said that it's forbidden only in C++; in C it's just bad practice. By the way, I'd say that with *initial call* it isn't contrasting the first call with subsequent calls, but it's just saying that such call happens at the beginning of the execution. – Matteo Italia Feb 16 '11 at 19:57
  • I read that as "returning from the initial call is equivalent to `exit()`, whereas returning from a subsequent calls is just a return"; however, you are right that repeated calls to `main()` may have undesired side-effects in practice (eg gcc will repeatedly call `__main()`), which is a pity, imo: recursively calling `main()` might be nice for argument parsing... – Christoph Feb 16 '11 at 20:15
  • @Christoph: I think your interpretation is correct, when I wrote that comment I didn't check the relevant passage. :S – Matteo Italia Feb 16 '11 at 20:26
2

The simple reason being that the control always first go to main.Thus it is automatically located by the compiler thus giving its prototype is redundant and is of no use.

Also we use prototype when call to a function is made prior to its definition.Thus looking at the function prototype compiler can decide whether call is legitimate or not.But in case of main we are accustomed to provide its definition along with its declaration (which is logically correct also)thus no need of prototype.

Even when we make our c programs organized into multiple files there is no need for prototype of main.

Algorithmist
  • 6,657
  • 7
  • 35
  • 49
1

The original purpose of prototypes was to support forward references to functions that could be processed by single pass compilers.

In other words, if you have something like this:

void function_A(){
    printf( "%d", function_B( 5 ) );
}

int function_B( int x ){
    return x * 2;
}

function_B is called before it is defined. This will create problems for simple compilers. To avoid these problems, putting prototypes at the beginning of the file, ensures that the compiler knows about all the functions in the file ahead of time, so forward references are not a problem to type check.

Since all the valid forms of the main function are known to the compiler beforehand, it is unnecessary to create a prototype, because the compiler can type check a forward reference to main() without it.

Tyler Durden
  • 11,156
  • 9
  • 64
  • 126