Controls whether or not the compiler should expect function prototypes.
#pragma require_prototypes on | off | reset
This pragma only affects non-static functions.
If you enable this pragma, the compiler generates an error message when you use a function that does not have a preceding prototype. Use this pragma to prevent error messages caused by referring to a function before you define it. For example, without a function prototype, you might pass data of the wrong type, as a result your code might not work as you expect even though it compiles without error.
In the following listing, function main() calls PrintNum() with an integer argument even though PrintNum() takes an argument of type float.
#include <stdio.h> void main(void) { PrintNum(1); /* PrintNum() tries to interpret the integer as a float. Prints 0.000000. */ } void PrintNum(float x) { printf("%f\n", x); }
When you run this program, you could get this result:
0.000000
Although the compiler does not complain about the type mismatch, the function does not give the desired result. Since PrintNum() does not have a prototype, the compiler does not know how to generate instructions to convert the integer to a floating-point number before calling PrintNum(). Consequently, the function interprets the bits it received as a floating-point number and prints nonsense.
A prototype for PrintNum(), as in the following listing, gives the compiler sufficient information about the function to generate instructions to properly convert its argument to a floating-point number. The function prints what you expected.
#include <stdio.h> void PrintNum(float x); /* Function prototype. */ void main(void) { PrintNum(1); /* Compiler converts int to float. } Prints 1.000000. */ void PrintNum(float x) { printf("%f\n", x); }
In other situations where automatic conversion is not possible, the compiler generates an error message if an argument does not match the data type required by a function prototype. Such a mismatched data type error is easier to locate at compile time than at runtime.