[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The C language provides no built-in facilities for performing such common operations as input/output, memory management, string manipulation, and the like. Instead, these facilities are defined in a standard library, which you compile and link with your programs.
The GNU C library, described in this document, defines all of the library functions that are specified by the ANSI C standard, as well as additional features specific to POSIX and other derivatives of the Unix operating system, and extensions specific to the GNU system.
The purpose of this manual is to tell you how to use the facilities of the GNU library. We have mentioned which features belong to which standards to help you identify things that are potentially nonportable to other systems. But the emphasis in this manual is not on strict portability.
1.1 Getting Started | What this manual is for and how to use it. | |
1.2 Standards and Portability | Standards and sources upon which the GNU C library is based. | |
1.3 Using the Library | Some practical uses for the library. | |
1.4 Roadmap to the Manual | Overview of the remaining chapters in this manual. |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This manual is written with the assumption that you are at least somewhat familiar with the C programming language and basic programming concepts. Specifically, familiarity with ANSI standard C (see section ANSI C), rather than “traditional” pre-ANSI C dialects, is assumed.
The GNU C library includes several header files, each of which provides definitions and declarations for a group of related facilities; this information is used by the C compiler when processing your program. For example, the header file ‘stdio.h’ declares facilities for performing input and output, and the header file ‘string.h’ declares string processing utilities. The organization of this manual generally follows the same division as the header files.
If you are reading this manual for the first time, you should read all of the introductory material and skim the remaining chapters. There are a lot of functions in the GNU C library and it’s not realistic to expect that you will be able to remember exactly how to use each and every one of them. It’s more important to become generally familiar with the kinds of facilities that the library provides, so that when you are writing your programs you can recognize when to make use of library functions, and where in this manual you can find more specific information about them.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section discusses the various standards and other sources that the GNU C library is based upon. These sources include the ANSI C and POSIX standards, and the System V and Berkeley Unix implementations.
The primary focus of this manual is to tell you how to make effective use of the GNU library facilities. But if you are concerned about making your programs compatible with these standards, or portable to operating systems other than GNU, this can affect how you use the library. This section gives you an overview of these standards, so that you will know what they are when they are mentioned in other parts of the manual.
@xref{Library Summary}, for an alphabetical list of the functions and other symbols provided by the library. This list also states which standards each function or symbol comes from.
1.2.1 ANSI C | The American National Standard for the C programming language. | |
1.2.2 POSIX (The Portable Operating System Interface) | The IEEE 1003 standards for operating systems. | |
1.2.3 Berkeley Unix | BSD and SunOS. | |
1.2.4 SVID (The System V Interface Description) | The System V Interface Description. |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The GNU C library is compatible with the C standard adopted by the American National Standards Institute (ANSI): American National Standard X3.159-1989—“ANSI C”. The header files and library facilities that make up the GNU library are a superset of those specified by the ANSI C standard.
If you are concerned about strict adherence to the ANSI C standard, you should use the ‘-ansi’ option when you compile your programs with the GNU C compiler. This tells the compiler to define only ANSI standard features from the library header files, unless you explicitly ask for additional features. See section Feature Test Macros, for information on how to do this.
Being able to restrict the library to include only ANSI C features is important because ANSI C puts limitations on what names can be defined by the library implementation, and the GNU extensions don’t fit these limitations. See section Reserved Names, for more information about these restrictions.
This manual does not attempt to give you complete details on the differences between ANSI C and older dialects. It gives advice on how to write programs to work portably under multiple C dialects, but does not aim for completeness.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The GNU library is also compatible with the IEEE POSIX family of standards, known more formally as the Portable Operating System Interface for Computer Environments. POSIX is derived mostly from various versions of the Unix operating system.
The library facilities specified by the POSIX standards are a superset of those required by ANSI C; POSIX specifies additional features for ANSI C functions, as well as specifying new additional functions. In general, the additional requirements and functionality defined by the POSIX standards are aimed at providing lower-level support for a particular kind of operating system environment, rather than general programming language support which can run in many diverse operating system environments.
The GNU C library implements all of the functions specified in IEEE Std 1003.1-1990, the POSIX System Application Program Interface, commonly referred to as POSIX.1. The primary extensions to the ANSI C facilities specified by this standard include file system interface primitives (@pxref{File System Interface}), device-specific terminal control functions (@pxref{Low-Level Terminal Interface}), and process control functions (@pxref{Child Processes}).
Some facilities from IEEE Std 1003.2-1992, the POSIX Shell and Utilities standard (POSIX.2) are also implemented in the GNU library. These include utilities for dealing with regular expressions and other pattern matching facilities (@pxref{Pattern Matching}).
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The GNU C library defines facilities from some versions of Unix which are not formally standardized, specifically from the 4.2 BSD, 4.3 BSD, and 4.4 BSD Unix systems (also known as Berkeley Unix) and from SunOS (a popular 4.2 BSD derivative that includes some Unix System V functionality). These systems support most of the ANSI and POSIX facilities, and 4.4 BSD and newer releases of SunOS in fact support them all.
The BSD facilities include symbolic links (@pxref{Symbolic Links}), the
select
function (@pxref{Waiting for I/O}), the BSD signal
functions (@pxref{BSD Signal Handling}), and sockets (@pxref{Sockets}).
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The System V Interface Description (SVID) is a document describing the AT&T Unix System V operating system. It is to some extent a superset of the POSIX standard (see section POSIX (The Portable Operating System Interface)).
The GNU C library defines some of the facilities required by the SVID that are not also required by the ANSI or POSIX standards, for compatibility with System V Unix and other Unix systems (such as SunOS) which include these facilities. However, many of the more obscure and less generally useful facilities required by the SVID are not included. (In fact, Unix System V itself does not provide them all.)
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section describes some of the practical issues involved in using the GNU C library.
1.3.1 Header Files | How to include the header files in your programs. | |
1.3.2 Macro Definitions of Functions | Some functions in the library may really be implemented as macros. | |
1.3.3 Reserved Names | The C standard reserves some names for the library, and some for users. | |
1.3.4 Feature Test Macros | How to control what names are defined. |
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Libraries for use by C programs really consist of two parts: header files that define types and macros and declare variables and functions; and the actual library or archive that contains the definitions of the variables and functions.
(Recall that in C, a declaration merely provides information that a function or variable exists and gives its type. For a function declaration, information about the types of its arguments might be provided as well. The purpose of declarations is to allow the compiler to correctly process references to the declared variables and functions. A definition, on the other hand, actually allocates storage for a variable or says what a function does.)
In order to use the facilities in the GNU C library, you should be sure that your program source files include the appropriate header files. This is so that the compiler has declarations of these facilities available and can correctly process references to them. Once your program has been compiled, the linker resolves these references to the actual definitions provided in the archive file.
Header files are included into a program source file by the ‘#include’ preprocessor directive. The C language supports two forms of this directive; the first,
#include "header"
is typically used to include a header file header that you write yourself; this would contain definitions and declarations describing the interfaces between the different parts of your particular application. By contrast,
#include <file.h>
is typically used to include a header file ‘file.h’ that contains definitions and declarations for a standard library. This file would normally be installed in a standard place by your system administrator. You should use this second form for the C library header files.
Typically, ‘#include’ directives are placed at the top of the C source file, before any other code. If you begin your source files with some comments explaining what the code in the file does (a good idea), put the ‘#include’ directives immediately afterwards, following the feature test macro definition (see section Feature Test Macros).
For more information about the use of header files and ‘#include’ directives, see Header Files in The GNU C Preprocessor Manual.
The GNU C library provides several header files, each of which contains the type and macro definitions and variable and function declarations for a group of related facilities. This means that your programs may need to include several header files, depending on exactly which facilities you are using.
Some library header files include other library header files automatically. However, as a matter of programming style, you should not rely on this; it is better to explicitly include all the header files required for the library facilities you are using. The GNU C library header files have been written in such a way that it doesn’t matter if a header file is accidentally included more than once; including a header file a second time has no effect. Likewise, if your program needs to include multiple header files, the order in which they are included doesn’t matter.
Compatibility Note: Inclusion of standard header files in any order and any number of times works in any ANSI C implementation. However, this has traditionally not been the case in many older C implementations.
Strictly speaking, you don’t have to include a header file to use a function it declares; you could declare the function explicitly yourself, according to the specifications in this manual. But it is usually better to include the header file because it may define types and macros that are not otherwise available and because it may define more efficient macro replacements for some functions. It is also a sure way to have the correct declaration.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
If we describe something as a function in this manual, it may have a macro definition as well. This normally has no effect on how your program runs—the macro definition does the same thing as the function would. In particular, macro equivalents for library functions evaluate arguments exactly once, in the same way that a function call would. The main reason for these macro definitions is that sometimes they can produce an inline expansion that is considerably faster than an actual function call.
Taking the address of a library function works even if it is also defined as a macro. This is because, in this context, the name of the function isn’t followed by the left parenthesis that is syntactically necessary to recognize a macro call.
You might occasionally want to avoid using the macro definition of a function—perhaps to make your program easier to debug. There are two ways you can do this:
For example, suppose the header file ‘stdlib.h’ declares a function
named abs
with
extern int abs (int);
and also provides a macro definition for abs
. Then, in:
#include <stdlib.h> int f (int *i) { return (abs (++*i)); }
the reference to abs
might refer to either a macro or a function.
On the other hand, in each of the following examples the reference is
to a function and not a macro.
#include <stdlib.h> int g (int *i) { return ((abs)(++*i)); } #undef abs int h (int *i) { return (abs (++*i)); }
Since macro definitions that double for a function behave in exactly the same way as the actual function version, there is usually no need for any of these methods. In fact, removing macro definitions usually just makes your program slower.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The names of all library types, macros, variables and functions that come from the ANSI C standard are reserved unconditionally; your program may not redefine these names. All other library names are reserved if your program explicitly includes the header file that defines or declares them. There are several reasons for these restrictions:
exit
to do something completely different from
what the standard exit
function does, for example. Preventing
this situation helps to make your programs easier to understand and
contributes to modularity and maintainability.
In addition to the names documented in this manual, reserved names include all external identifiers (global functions and variables) that begin with an underscore (‘_’) and all identifiers regardless of use that begin with either two underscores or an underscore followed by a capital letter are reserved names. This is so that the library and header files can define functions, variables, and macros for internal purposes without risk of conflict with names in user programs.
Some additional classes of identifier names are reserved for future extensions to the C language. While using these names for your own purposes right now might not cause a problem, they do raise the possibility of conflict with future versions of the C standard, so you should avoid these names.
float
and long double
arguments,
respectively.
In addition, some individual header files reserve names beyond those that they actually define. You only need to worry about these restrictions if your program includes that particular header file.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The exact set of features available when you compile a source file is controlled by which feature test macros you define.
If you compile your programs using ‘gcc -ansi’, you get only the ANSI C library features, unless you explicitly request additional features by defining one or more of the feature macros. See GNU CC Command Options in The GNU CC Manual, for more information about GCC options.
You should define these macros by using ‘#define’ preprocessor
directives at the top of your source code files. These directives
must come before any #include
of a system header file. It
is best to make them the very first thing in the file, preceded only by
comments. You could also use the ‘-D’ option to GCC, but it’s
better if you make the source files indicate their own meaning in a
self-contained way.
If you define this macro, then the functionality from the POSIX.1 standard (IEEE Standard 1003.1) is available, as well as all of the ANSI C facilities.
If you define this macro with a value of 1
, then the
functionality from the POSIX.1 standard (IEEE Standard 1003.1) is made
available. If you define this macro with a value of 2
, then both
the functionality from the POSIX.1 standard and the functionality from
the POSIX.2 standard (IEEE Standard 1003.2) are made available. This is
in addition to the ANSI C facilities.
If you define this macro, functionality derived from 4.3 BSD Unix is included as well as the ANSI C, POSIX.1, and POSIX.2 material.
Some of the features derived from 4.3 BSD Unix conflict with the corresponding features specified by the POSIX.1 standard. If this macro is defined, the 4.3 BSD definitions take precedence over the POSIX definitions.
Due to the nature of some of the conflicts between 4.3 BSD and POSIX.1,
you need to use a special BSD compatibility library when linking
programs compiled for BSD compatibility. This is because some functions
must be defined in two different ways, one of them in the normal C
library, and one of them in the compatibility library. If your program
defines _BSD_SOURCE
, you must give the option ‘-lbsd-compat’
to the compiler or linker when linking the program, to tell it to find
functions in this special compatibility library before looking for them in
the normal C library.
If you define this macro, functionality derived from SVID is included as well as the ANSI C, POSIX.1, and POSIX.2 material.
If you define this macro, everything is included: ANSI C, POSIX.1, POSIX.2, BSD, SVID, and GNU extensions. In the cases where POSIX.1 conflicts with BSD, the POSIX definitions take precedence.
If you want to get the full effect of _GNU_SOURCE
but make the
BSD definitions take precedence over the POSIX definitions, use this
sequence of definitions:
#define _GNU_SOURCE #define _BSD_SOURCE #define _SVID_SOURCE
Note that if you do this, you must link your program with the BSD compatibility library by passing the ‘-lbsd-compat’ option to the compiler or linker. Note: If you forget to do this, you may get very strange errors at run time.
We recommend you use _GNU_SOURCE
in new programs. If you don’t
specify the ‘-ansi’ option to GCC and don’t define any of these macros
explicitly, the effect is the same as defining _GNU_SOURCE
.
When you define a feature test macro to request a larger class of features,
it is harmless to define in addition a feature test macro for a subset of
those features. For example, if you define _POSIX_C_SOURCE
, then
defining _POSIX_SOURCE
as well has no effect. Likewise, if you
define _GNU_SOURCE
, then defining either _POSIX_SOURCE
or
_POSIX_C_SOURCE
or _SVID_SOURCE
as well has no effect.
Note, however, that the features of _BSD_SOURCE
are not a subset of
any of the other feature test macros supported. This is because it defines
BSD features that take precedence over the POSIX features that are
requested by the other macros. For this reason, defining
_BSD_SOURCE
in addition to the other feature test macros does have
an effect: it causes the BSD features to take priority over the conflicting
POSIX features.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Here is an overview of the contents of the remaining chapters of this manual.
sizeof
operator and the symbolic constant NULL
, how to write functions
accepting variable numbers of arguments, and constants describing the
ranges and other properties of the numerical types. There is also a simple
debugging mechanism which allows you to put assertions in your code, and
have diagnostic messages printed if the tests fail.
isspace
) and functions for
performing case conversion.
FILE *
objects). These are the normal C library functions
from ‘stdio.h’.
char
data type.
setjmp
and
longjmp
functions. These functions provide a facility for
goto
-like jumps which can jump from one function to another.
If you already know the name of the facility you are interested in, you can look it up in @ref{Library Summary}. This gives you a summary of its syntax and a pointer to where you can find a more detailed description. This appendix is particularly useful if you just want to verify the order and type of arguments to a function, for example. It also tells you what standard or system each function, variable, or macro is derived from.
[Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 29, 2022 using texi2html 5.0.
The buttons in the navigation panels have the following meaning:
Button | Name | Go to | From 1.2.3 go to |
---|---|---|---|
[ << ] | FastBack | Beginning of this chapter or previous chapter | 1 |
[ < ] | Back | Previous section in reading order | 1.2.2 |
[ Up ] | Up | Up section | 1.2 |
[ > ] | Forward | Next section in reading order | 1.2.4 |
[ >> ] | FastForward | Next chapter | 2 |
[Top] | Top | Cover (top) of document | |
[Contents] | Contents | Table of contents | |
[Index] | Index | Index | |
[ ? ] | About | About (help) |
where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:
This document was generated on March 29, 2022 using texi2html 5.0.