C/C++ Programmer's Guide (G06.27+, H06.08+, J06.03+)

Following Good Programming Practices
In general, the more you follow good programming practices, the easier it will be to port your
program to other hardware and software environments. A few of the most important good
programming practices are:
Use strictly conforming C language features as described in the ISO/ANSI C standard as
much as possible. Isolate HP defined and extended C language features into specific modules.
Use function prototypes.
Place all environment-specific function declarations in a common header file, such as
environh.
Make sure the type of a function’s actual and formal parameters are the same.
Define every function with an explicit return type. Make sure the type of a return expression
is the same as the return type of the function.
Write your programs to a template similar to:
Feature-test macros
System headers
Local headers
Macros
File scope variables
External variables
External functions
Structures and unions
Signal-catching functions
Functions
Main function
Do not write code that relies on processor architecture. Be careful when writing code that
relies on word size, pointer size, bit fields, arithmetic precision, byte order, stack size, stack
growth, heap size, and heap growth.
Do not make assumptions about the size and format of any data type:
Use type short and type long instead of type int, if possible. In particular, do not
interchange between type int and type long.
Do not assign an int to or from a pointer without an explicit type cast.
Do not assume that different pointer types are the same.
Use unsigned types for bit fields.
There are many commercially available texts that describe how to write portable applications in
C, including:
Horton, Mark. Portable C Software. Prentice Hall, Inc., 1990.
Jaeschke, Rex. Portability and the C Language. Hayden Books, 1989.
46 Introduction to HP C and C++ for NonStop Systems