HP C Programmer's Guide (92434-90009)

138 Chapter5
Programming for Portability
Porting to ANSI Mode HP C
an initial value of 3.14:
const float pi = 3.14;
A const variable can be used like any other variable. For example:
area = pi * (radius * radius);
But attempting to assign a value to a const variable causes a compile error:
pi = 3.1416; /* This causes an error. */
Only obvious attempts to modify const variables are detected. Assignments made using
pointer references to const variables may not be detected by the compiler.
However, pointers may be declared using the const qualifier. For example:
char *const prompt = "Press return to continue> ";
An attempt to reassign the const pointer prompt causes a compiler error. For example:
prompt = "Exiting program."; /* Causes a compile time error. */
The volatile qualifier provides a way to tell the compiler that the value of a variable may
change in ways not known to the compiler. The volatile qualifier is useful when declaring
variables that may be altered by signal handlers, device drivers, the operating system, or
routines that use shared memory. It may also prevent certain optimizations from
occurring.
The optimizer makes assumptions about how variables are used within a program. It
assumes that the contents of memory will not be changed by entities other than the
current program. The volatile qualifier forces the compiler to be more conservative in its
assumptions regarding the variable.
The volatile qualifier can also be used for regular variables and pointers. For example:
volatile int intlist[100];
volatile char *revision_level;
For further information on the HP C optimizer and its assumptions, see Chapter 4,
"Optimizing HP C Programs." For further information on the const and volatile
qualifiers see the HP C/UX Reference Manual.
ANSI Mode Function Prototypes
Function prototypes are function declarations that contain parameter type lists.
Prototype-style function declarations are available only in ANSI mode. You are encouraged
to use the prototype-style of function declarations.
Adding function prototypes to existing C programs yields three advantages:
Better type checking between declarations and calls because the number and types of
the parameters are part of the function's parameter list. For example:
struct s
{
int i;
}
int old_way(x)