HP C Programmer's Guide (92434-90009)

140 Chapter5
Programming for Portability
Porting to ANSI Mode HP C
for declaring function parameter types
*/
extern double sqrt(double);
/* The function "sqrt" is called later
on in the program.
*/
sqrt(1);
In this example, any value passed to sqrt is automatically converted to double.
Compiling an existing program in ANSI mode yields some of these advantages because of
the existence of prototypes in the standard header files. To take full advantage of
prototypes in existing programs, change old-style declarations (without prototype) to new
style declarations. On HP-UX, the tool protogen (see
protogen(1)
in the on-line man
pages) helps add prototypes to existing programs. For each source file, protogen can
produce a header file of prototypes and a modified source file that includes prototype
declarations.
Mixing Old-Style Function Definitions with ANSI Function Declarations
A common pitfall when mixing prototypes with old-style function definitions is to overlook
the ANSI rule that for parameter types to be compatible, the parameter type in the
prototype must match the parameter type resulting from default promotions applied to the
parameter in the old-style function definition.
For example:
void func1(char c);
void func1(c)
char c;
{}
gets the following message when compiled in ANSI mode:
Inconsistent parameter list declaration for "func1"
The parameter type for c in the prototype is char. The parameter type for c in the
definition func1 is also char, but it expects an int because it is an old-style function
definition and in the absence of a prototype, char is promoted to int.
Changing the prototype to:
void func1(int c);
fixes the error.
The ANSI C standard does not require a compiler to do any parameter type checking if
prototypes are not used. Value parameters whose sizes are larger than 64 bits (8 bytes)
will be passed via a short pointer to the high-order byte of the parameter value. The
receiving function then makes a copy of the parameter pointed to by this short pointer in
its own local memory.