HP C Programmer's Guide (92434-90009)

Chapter 5 131
Programming for Portability
General Portability Considerations
The char Data Type
The char data type defaults to signed. If a char is assigned to an int, sign extension takes
place. A char may be declared unsigned to override this default. The line:
unsigned char ch;
declares one byte of unsigned storage named ch. On some non-HP-UX systems, char
variables are unsigned by default.
Register Storage Class
The register storage class is supported on Series 300/400 and 700/800 HP-UX, and if
properly used, can reduce execution time. Using this type should not hinder portability.
However, its usefulness on systems will vary, since some ignore it. Refer to the HP-UX
Assembler and Supporting Tools for Series 300/400 for a more complete description of the
use of the register storage class on Series 300/400.
Also, the register storage class declarations are ignored when optimizing at level 2 or
greater on all Series.
Identifiers
To guarantee portable code to non-HP-UX systems, the ANSI C standard requires
identifier names without external linkage to be significant to 31 case-sensitive characters.
Names with external linkage (identifiers that are defined in another source file) will be
significant to six case-insensitive characters. Typical C programming practice is to name
variables with all lower-case letters, and #define constants with all upper case.
Predefined Symbols
The symbol __hp9000s300 is predefined on Series 300/400; the symbols __hp9000s800
and hppa are predefined on Series 700/800; and __hp9000s700 is predefined on Series 700
only. The symbols __hpux and __unix are predefined on all HP-UX implementations.
This is only an issue if you port code to or from systems that also have predefined these
symbols.
Shift Operators
On left shifts, vacated positions are filled with 0. On right shifts of signed operands,
vacated positions are filled with the sign bit (arithmetic shift). Right shifts of unsigned
operands fill vacated bit positions with 0 (logical shift). Integer constants are treated as
signed unless cast to unsigned. Circular shifts are not supported in any version of C. Shifts
greater than 32 bits give an undefined result.
The sizeof Operator
The sizeof operator yields an unsigned int result, as specified in section 3.3.3.4 of the
ANSI C standard (X3.159-1989). Therefore, expressions involving this operator are
inherently unsigned. Do not expect any expression involving the sizeof operator to have a
negative value (as may occur on some other systems). In particular, logical comparisons of