[ First ] [ Previous ] [ Next ] [ Last ] [ Manuals ]
Require Function Prototypes

(K&R, §A8.6.3, §A10.1) The C compiler lets you choose how to enforce function prototypes. This behavior is controlled by the Require Function Prototypes item in the C/C++ Language Panel.
When the Require Function Prototypes option is on, the compiler generates an error if you use a function that is defined after it is referenced and does not have a prototype. If the function is defined before it is referenced, and does not have a prototype, then the compiler will issue a warning if Require Function Prototypes is on.
This option helps you prevent errors that happen when you call a function before you declare or define it. For example, without a function prototype, you may pass data if the wrong type. As a result, your code may not work as you expect even though it compiles without error.
In Listing 3.2, PrintNum() is called with an integer argument but is later defined to take a floating-point argument.
Unnoticed type-mismatch:
#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 code, you could get this result:
0.000000
Although the compiler does not complain about the type mismatch, the function does not work as you want. Since PrintNum() is not prototyped, the compiler does not know it needs to convert the integer to a floating-point number before calling the function. Instead, the function interprets the bits it received as a floating-point number and prints nonsense.
If you prototype PrintNum() first, as in Listing 3.3, the compiler converts its argument to a floating-point number, and the function prints what you wanted.
Using a prototype to avoid type-mismatch:
#include <stdio.h>
void PrintNum(float x); // Function prototype.
void main(void)
{
PrintNum(1); // Compiler knows to convert int to float.
} // Prints 1.000000.
void PrintNum(float x)
{
printf("%f\n", x);
}
In the above example, the compiler automatically typecast the passed value. In other situations where automatic typecasting is not available, the compiler will generate an error if an argument does not match the data type required by a function prototype. Such a mismatched data type error is easy to locate at compile time. If you do not use prototypes, you get no compiler error. However, at runtime the code may behave strangely, and the cause of the resulting unintentional behavior can be extremely difficult to track down.
The Require Function Prototypes option corresponds to the pragma require_prototypes. To check whether this option is on, use __option (require_prototypes). By default, this option is on.
See also "require_prototypes," and "Checking Options."
[ First ] [ Previous ] [ Next ] [ Last ] [ Manuals ]
Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.
Last updated: August 17, 2000