HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02

Chapter 8 C Library Functions

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

The C library (/usr/lib/hpux32/libc.so or /usr/lib/hpux64/libc.so) is divided into different subsections. Each subsection has a header file that defines the objects found in that section of the library.

The standard headers are:

<assert.h>     <locale.h>    <stddef.h>
<ctype.h>      <math.h>      <stdio.h>
<errno.h>      <setjmp.h>    <stdlib.h>
<float.h>      <signal.h>    <string.h>
<limits.h>     <stdarg.h>    <time.h>

The order of inclusion of these header files using the #include directive makes no difference. Also, if you include the same header file more than once, an error does not occur.

Function names beginning with an underscore (_) are reserved for library use; you should not specify identifiers that begin with an underscore.

To use some facilities, the C source code must include the preprocessor directive:

     #include <libraryname.h>

The preprocessor looks for the particular header file defined in libraryname in a standard location on the system.

The standard location is /usr/include.

The libraryname must be enclosed in angle brackets. For example, if you want to use the fprintf function, which is in the standard I/O library, your program must specify

     #include <stdio.h>

because the definition of fprintf, as well as various types and variables used by the I/O function, are found in the stdio.h header file.

The C library contains both functions and macros. The use of macros improves the execution speed of certain frequently used operations. One drawback to using macros is that they do not have an address. For example, if a function expects the address of (a pointer to) another function as an argument, you cannot use a macro name in that argument. The following example illustrates the drawback:

     #define add1(x) ((x)+=1)

     extern f();

     main()
     {
          .
.
.
          f(add1); <This construct is illegal.
          .
.
.
       }

Using add1 as an argument causes an error.

The #undef directive may be used to reference a true function instead of a macro.

There are three ways in which a function can be used:

  • In a header file (which might generate a macro)

#include <string.h>
i = strlen(x);

  • By explicit declaration

extern int strlen();
i=strlen(x);

  • By implicit declaration

i = strlen(x);

NOTE: It is recommended you always include a header to declare C library functions.

For more information on C library functions, see the HP-UX Reference and HP-UX Linker and Libraries User Guide.

© Hewlett-Packard Development Company, L.P.