Take the 2-minute tour ×

I'm following a guide to learn curses, and all of the C code within prototypes functions before main(), then defines them afterward. In my C++ learnings, I had heard about function prototyping but never done it, and as far as I know it doesn't make too much of a difference on how the code is compiled. Is it a programmer's personal choice more than anything else? If so, why was it included in C at all?

share|improve this question
You will need forward declaration when two functions call each other. –  KennyTM Oct 29 '10 at 19:52
possible duplicate of Are prototypes required for all functions in C89, C90 or C99? –  Jens Gustedt Oct 29 '10 at 19:56
I don't see why. I'm not asking if they're necessary, I just don't understand why they're in the standard in the first place. –  Maulrus Oct 29 '10 at 19:58
The short answer is: Writing a compiler is hard enough, requiring everything to be declared beforehand is an effective way to keep the compiler one-pass. –  delnan Oct 29 '10 at 20:42
Why are people closing this as an exact duplicate? The other question is asking if it's required. This one is asking why one would ever use it at all. Just cause they both have the word prototype in the title doesn't mean they're the same question. –  Maulrus Oct 30 '10 at 4:52

5 Answers 5

up vote 3 down vote accepted

In C prototyping is needed so that your program knows that you have a function called x() when you have not gotten to defining it, that way y() knows that there is and exists a x(). C does top down compilation, so it needs to be defined before hand is the short answer.




more code ...
maybe even y();
share|improve this answer
I think this answer completely misses the point. For one thing, you can call any function also without a prototype in scope and before it is defined (certain rules apply). Second, "top down compilation" (whatever that means exactly) is not part of C and there's no requirement to compile C in some specific way. –  Jens Apr 12 '11 at 17:39
Third, your code snipped does not even contain proper prototypes! It is so sloppy, gcc -Wall -pedantic -std=c89 x.c produces 10 warnings. –  Jens Apr 12 '11 at 17:49

It allows you to have a situation in which say you can have an iterator class defined in a separate .h file which includes the parent container class. Since you've included the parent header in the iterator, you can't have a method like say "getIterator()" because the return type would have to be the iterator class and therefore it would require that you include the iterator header inside the parent header creating a cyclic loop of inclusions (one includes the other which includes itself which includes the other again, etc.).

If you put the iterator class prototype inside the parent container, you can have such a method without including the iterator header. It only works because you're simply saying that such an object exists and will be defined.

There are ways of getting around it like having a precompiled header, but in my opinion it's less elegant and comes with a slew of disadvantages. Of couurse this is C++, not C. However, in practice you might have a situation in which you'd like to arrange code in this fashion, classes aside.

share|improve this answer
An iterator class, in C? The question mentions C++, but is about C rationales, so I think a different example is called for :-) –  Steve Jessop Oct 29 '10 at 20:05

I was under the impression that it was so customers could have access to the .h file for libraries and see what functions were available to them, without having to see the implementation (which would be in another file).

Useful to see what the function returns/what parameters.

share|improve this answer
Getting available functions from headers is a bad idea because headers may contain arbitrary magic not available to the application programmer. A serious developer will get all the information he needs from the documentation supplied with the libraries. For Standard C, POSIX and many more (even Windows APIs) the documentation is freely available and possibly even already there (man pages, Windows help, ...) If you need to glean a prototype from a header, you're on the wrong track. –  Jens Apr 12 '11 at 17:57

Function prototyping is a remnant from the olden days of compiler writing. It used to be considered horribly inefficient for a compiler to have to make multiple passes over a source file to compile it.

In C, in certain contexts, referring to a function in one manner is syntactically equivalent to referring to a variable: consider taking a pointer to a function versus taking a pointer to a variable. In the compiler's intermediate representation, the two are semantically distinct, but syntactically, whether an identifier is a variable, a function name, or an invalid identifier cannot be determined from the context.

Since it's not determinable from the context, without function prototypes, the compiler would need to make an extra pass over each one of your source files each time one of them compiles. This would add an extra O(n) factor for any compilation (that is, if compilation were O(m), it would now be O(m*n)), where n is the number of files in your project. In large projects, where compilation is already on the order of hours, having a two-pass compiler is highly undesirable.

Forward declaring all your functions would allow the compiler to build a table of functions as it scanned the file, and be able to determine when it encountered an identifier whether it referred to a function or a variable.

As a result of this, C (and by extension, C++) compilers can be extremely efficient in compilation.

share|improve this answer
That's not necessarily true - if they didn't need function prototypes, they wouldn't have to re-parse the same #include thirty times over. That's why precompiled headers are so effective at reducing build times in MSVC- because #including takes so long. –  Puppy Oct 29 '10 at 21:04
I like the start. Yes the compilation model is to blame. But I like the defending of this model much less. Suggestion that C++ compilers can be extremely efficient in compilation, at the end is a disaster. Come on! –  Maciej Hehl Oct 29 '10 at 21:11

Function prototyping originally wasn't included in C. When you called a function, the compiler just took your word for it that it would exist and took the type of arguments you provided. If you got the argument order, number, or type wrong, too bad – your code would fail, possibly in mysterious ways, at runtime.

Later versions of C added function prototyping in order to address these problems. Your arguments are implicitly converted to the declared types under some circumstances or flagged as incompatible with the prototype, and the compiler could flag as an error the wrong order and number of types. This had the side effect of enabling varargs functions and the special argument handling they require.

Note that, in C (and unlike in C++), a function declared foo_t func() is not the same as a function declared as foo_t func(void). The latter is prototyped to have no arguments. The former declares a function without a prototype.

share|improve this answer
+1 for the last section on the difference between a declaration and a prototype. –  legends2k Jun 9 at 13:11

Your Answer


