Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of C/C++ Prototypes

I was reading wikipedia on C/C++ Prototype statements and I'm confused:

Wikipedia say: "By including the function prototype, you inform the compiler that the function "fac" takes one integer argument and you enable the compiler to catch these kinds of errors."

and uses the below as an example:

#include <stdio.h>   /*    * If this prototype is provided, the compiler will catch the error    * in main(). If it is omitted, then the error will go unnoticed.   */  int fac(int n);              /* Prototype */   int main(void) {             /* Calling function */      printf("%d\n", fac());   /* ERROR: fac is missing an argument! */      return 0;  }   int fac(int n) {             /* Called function  */      if (n == 0)           return 1;      else           return n * fac(n - 1); } 

But the function definition of the called function already includes all the information that the prototype tells the compiler, so why can't the compiler deduce this information from the called function's definition since they contain identical statements/information letter for letter?

What am I missing? Seems like extra work for no obvious gain.

Edit: Thanks guys. I assumed the compilers were multi-pass I guess. I'm spoiled to current languages like Python. It makes sense since it's so old to need some kludges to do things accurately in a single pass. It seems more obvious to me now. Apparently it requires fairly intimate knowledge of how the compiler links and compiles.

like image 485
pythonnewbie Avatar asked Sep 21 '10 19:09

pythonnewbie


People also ask

What is C prototype?

Introduction to Function Prototype in C. A function prototype is one of the most important features of C programming which was originated from C++. A function prototype is a declaration in the code that instructs the compiler about the data type of the function, arguments and parameter list.

What is the purpose of a prototype in C++?

A function prototype is a declaration in C and C++ of a function, its name, parameters and return type before its actual declaration. This enables the compiler to perform more robust type checking.

Are prototypes necessary in C?

It is not required, but it is bad practice not to use prototypes. With prototypes, the compiler can verify you are calling the function correctly (using the right number and type of parameters).

What are the advantages of functions prototype in C?

Advantages of Function Prototypes in C++: Prototypes enables the compilers to provide stronger type checking. Because of the use of prototypes, the compiler can find and report any questionable type conversions between the arguments used to call a function and the types of its (function;s) parameters.


1 Answers

Two reasons:

  1. The compiler reads the file top-to-bottom. If fac is used in main which is above fac, and no prototype exists, the compiler doesn't know how to check that that call is being done correctly, since it hasn't yet reached the definition of fac.

  2. It's possible to split up a C or C++ program into multiple files. fac may be defined in a completely different file from the file that the compiler is currently processing, and so it needs to know that that function exists somewhere, and how it is supposed to be called.

Note that the comments in the example you posted only apply to C. In C++, that example will always produce an error, even if the prototype is omitted (although it will produce a different error depending on whether the prototype exists or not). In C++, all functions are required to be defined or prototyped before being used.

In C, you can omit the prototype and the compiler will allow you to call the function with any number of arguments (including zero), and will assume a return type of int. But just because it doesn't yell at you during compilation doesn't mean that the program will work right if you don't call the function the right way. That's why it's useful to prototype in C: so the compiler can double-check on your behalf.

The philosophy behind C and C++ that motivates this kind of feature is that these are relatively low-level languages. They don't do a lot of hand-holding and they don't do much if any run-time checking. If your program does something incorrect, it will crash or behave bizarrely. Therefore, the languages incorporate features like this that enable the compiler to identify certain types of errors at compile-time, so that you can more easily find and fix them.

like image 76
Tyler McHenry Avatar answered Sep 22 '22 17:09

Tyler McHenry