Each standard library function is declared in one or more of the standard headers. These headers also contain all the macro and type definitions that the C standard provides. This chapter describes the contents and use of the standard headers.
Each of the standard headers contains a set of related function declarations, macros, and type definitions. For example, mathematical functions are declared in the header math.h. The standard headers are also called header files, as the contents of each header are usually stored in a file. Strictly speaking, however, the standard does not require the headers to be organized in files.
The C standard defines the following 29 headers (those marked with an asterisk were added in C11):
assert.h |
inttypes.h |
signal.h |
stdint.h |
threads.h* |
complex.h |
iso646.h |
stdalign.h* |
stdio.h |
time.h |
ctype.h |
limits.h |
stdarg.h |
stdlib.h |
uchar.h* |
errno.h |
locale.h |
stdatomic.h* |
stdnoreturn.h* |
wchar.h |
fenv.h |
math.h |
stdbool.h |
string.h |
wctype.h |
float.h |
setjmp.h |
stddef.h |
tgmath.h |
The headers complex.h, stdatomic.h, and threads.h are optional components. There are standard macros that a C11 implementation can define to indicate that it does not include these options. If the macro __STDC_NO_COMPLEX__, __STDC_NO_ATOMICS__, or __STDC_NO_THREADS__ is defined as equal to 1, the implementation does not include the corresponding optional header.
You can add the contents of a standard header to a source file by inserting an #include directive, which must be placed outside all functions (see “Inserting the Contents of Header Files”). You can include the standard headers as many times as you want, and in any order. However, before the #include directive for any header, your program must not define any macro with the same name as an identifier in that header. To make sure that your programs respect this condition, always include the required standard headers at the beginning of your source files, before any header files of your own.
C programs run in one of two execution environments: hosted or freestanding. Most common programs run in a hosted environment; that is, under the control and with the support of an operating system. In a hosted environment, the full capabilities of the standard library are available. Furthermore, programs compiled for a hosted environment must define a function named main(), which is the first function invoked on program start.
A program designed for a freestanding environment runs without the support of an operating system. In a freestanding environment, the name and type of the first function invoked when a program starts is determined by the given implementation. Programs for a freestanding environment cannot use complex floating-point types, and may be limited to the following headers:
float.h |
stdalign.h |
stddef.h |
iso646.h |
stdarg.h |
stdint.h |
limits.h |
stdbool.h |
stdnoreturn.h |
Specific implementations may also provide additional standard library resources.
All standard library functions have external linkage. You may use standard library functions without including the corresponding header by declaring them in your own code. However, if a standard function requires a type defined in the header, then you must include the header.
The standard library functions are not guaranteed to be reentrant—that is, two calls to a standard library function may not safely be in execution concurrently in one process. One reason for this rule is that several of the functions use and modify static or thread-local variables, for example.
As a result, you can’t generally call standard library functions in signal handling routines. Signals are asynchronous, which means that a program may receive a signal at any time, even while it’s executing a standard library function. If that happens, and the handler for that signal calls the same standard function, then the function must be reentrant. It is up to individual implementations to determine which functions are reentrant, or whether to provide a reentrant version of the whole standard library.
Most of the standard library functions—with a few explicitly specified exceptions—are thread-safe, meaning they can be safely executed by several threads “simultaneously.” In other words, the standard functions must be so implemented that any objects they use internally are not subject to data races when called in more than one thread. In particular, they must not use static objects without ensuring synchronization. However, you the programmer are responsible for coordinating different threads’ access to any objects referred to directly or indirectly by a function’s arguments.
Each stream has a corresponding lock which the functions in the I/O library use to obtain exclusive access to the stream before performing an operation. In this way, the standard library functions prevent data races when several threads access a given stream.
As the programmer, you are responsible for calling functions and function-like macros with valid arguments. Wrong arguments can cause severe runtime errors. Typical mistakes to avoid include the following:
Argument values outside the domain of the function, as in the following call:
doublex=-1.0,y=sqrt(x);
Pointer arguments that do not point to an object or a function, as in this function call with an uninitialized pointer argument:
char*msg;strcpy(msg,"error");
Arguments whose type does not match that expected by a function with a variable number of arguments. In the following example, the conversion specifier %f calls for a float pointer argument, but &x is a pointer to double:
doublex;scanf("%f",&x);
Array address arguments that point to an array that isn’t large enough to accommodate data written by the function. Here is an example:
charname[]="Hi ";strcat(name,"Alice");
Macros in the standard library make full use of parentheses so that you can use them in expressions in the same way as individual identifiers. Furthermore, each function-like macro in the standard library uses its arguments only once.1 This means that you can call these macros in the same way as ordinary functions, even using expressions with side effects as arguments. Here is an example:
intc='A';while(c<='Z')putchar(c++);// Output: 'ABC ... XYZ'
The functions in the standard library may be implemented both as macros and as functions. In such cases, the same header file contains both a function prototype and a macro definition for a given function name. As a result, each use of the function name after you include the header file invokes the macro. The following example calls the macro or function toupper() to convert a lowercase letter to uppercase:
#include <ctype.h>/* ... */c=toupper(c);// Invokes the macro toupper(), if there is one.
However, if you specifically want to call a function and not a macro with the same name, you can use the #undef directive to cancel the macro definition:
#include <ctype.h>#undef toupper// Remove any macro definition with this name./* ... */c=toupper(c)// Calls the function toupper().
You can also call a function rather than a macro with the same name by setting the name in parentheses:
#include <ctype.h>/* ... */c=(toupper)(c)// Calls the function toupper().
Finally, you can omit the header containing the macro definition, and declare the function explicitly in your source file:
externinttoupper(int);/* ... */c=toupper(c)// Calls the function toupper().
When choosing identifiers to use in your programs, you must be aware that certain identifiers are reserved for the standard library. Reserved identifiers include the following:
All identifiers that begin with an underscore followed by a second underscore or an uppercase letter are always reserved. Thus, you cannot use identifiers such as __x or _Max, even for local variables or labels.
All other identifiers that begin with an underscore are reserved as identifiers with file scope. Thus, you cannot use an identifier such as _a_ as the name of a function or a global variable, although you can use it for a parameter, a local variable, or a label. The identifiers of structure or union members can also begin with an underscore, as long as the second character is not another underscore or an uppercase letter.
Identifiers declared with external linkage in the standard headers are reserved as identifiers with external linkage. Such identifiers include function names, as well as the names of global variables such as errno. Although you cannot declare these identifiers with external linkage as names for your own functions or objects, you may use them for other purposes. For example, in a source file that does not include string.h, you may define a static function named strcpy().
The identifiers of all macros defined in any header you include are reserved.
Identifiers declared with file scope in the standard headers are reserved within their respective name spaces. Once you include a header in a source file, you cannot use any identifier that is declared with file scope in that header for another purpose in the same name space (see “Identifier Name Spaces”) or as a macro name.
Although some of the conditions listed here have “loopholes” that allow you to reuse identifiers in a certain name space or with static linkage, overloading identifiers can cause confusion, and it’s generally safest to avoid the identifiers declared in the standard headers completely. In the following sections, we also list identifiers that have been reserved for future extensions of the C standard. The last three rules in the previous list apply to such reserved identifiers as well.
Many traditional functions in the C standard library copy strings to arrays that are provided by the programmer as pointer arguments. There is no way for these functions to test whether the given destination array is large enough to accommodate the result. The programmer alone is responsible for ensuring that no data is written past the end of an array, where it could modify adjacent objects in memory. This is a significant threat to the reliability and security of a program, and can cause it to crash.
To alleviate this problem, Appendix K of the C11 standard, “Bounds-checking Interfaces,” introduces many new functions as secure alternatives to the traditional standard C functions. These alternative functions, also called the secure functions, take an additional argument which specifies the size of the destination array. The secure functions use this information to ensure that the results they produce do not exceed the array’s bounds. The names of the secure functions end with the suffix _s (s for “secure”), as in strcpy_s(), for example. Unlike the traditional function strcpy(), the function strcpy_s() only copies a string if the specified destination vector is large enough to accommodate it.
Support for the bounds-checking functions is optional. They are available only in implementations that define the macro __STDC_LIB_EXT1__.
If these functions are provided, their declarations and the accompanying type and macro definitions are included in the same headers that provide the corresponding traditional functions. For example, the header stdio.h then contains the declaration of scanf_s() in addition to scanf(), and string.h contains the declaration of strcpy_s() alongside strcpy(). To make the declarations of the secure functions visible to the compiler, however, your program must define the macro __STDC_WANT_LIB_EXT1__ as equal to 1 before including the corresponding headers, for example, by using the lines:
#define __STDC_WANT_LIB_EXT1__ 1#include <stdio.h>
To prevent name conflicts with functions defined in your program or in other libraries it uses, you can ensure that the secure functions are not visible by defining the macro __STDC_WANT_LIB_EXT1__ as equal to 0. If __STDC_WANT_LIB_EXT1__ is not defined before the program includes a standard header, the corresponding secure functions may or may not be available, depending on the given compiler.
The parameter that specifies the size of an array in a bounds-checking function has the type rsize_t. This type is defined in the header stddef.h as equal to size_t. However, rsize_t places a special restriction on the value of a variable: a variable of the type rsize_t must not be assigned a value greater than that of the macro RSIZE_MAX. Passing an array length argument greater than RSIZE_MAX to a bounds-checking function causes an error. This constraint can detect errors that arise through the conversion of negative numbers to unsigned types, as such conversions result in very large positive numbers.
The secure functions perform other tests in addition to bounds-checking. For example, they test whether pointers passed as arguments are non-null. All the conditions that must be fulfilled for a function to execute successfully are called the function’s runtime constraints.
If a secure function’s runtime constraints are violated, the destination objects remain unchanged, and the function calls a runtime constraint handler, passing it a return value and an error message. The handler can end the program by calling abort(), or return to the secure function which called it. A program may replace the default runtime constraint handler with another standard handler or with a function of its own by calling the function set_constraint_handler_s(). For details, see the description of the function set_constraint_handler_s() in Chapter 18.
The return value of a secure function indicates whether an error has occurred. Many of the secure functions have a return value of the type errno_t. This type is defined in the header errno.h as int. These secure functions return the value 0 after a successful call, and a nonzero value if an error has occured.
The following subsections list the standard headers in alphabetical order, with brief descriptions of their contents, including all the types and macros defined in them.
The standard functions are described in the next two chapters: Chapter 17 summarizes the functions that the standard library provides for each area of application—the mathematical functions, string manipulation functions, functions for time and date operations, and so on. Chapter 18 then provides a detailed description of each function individually, in alphabetical order, with examples illustrating their use.
This header defines the function-like macro assert(), which tests whether the value of an expression is nonzero in the running program. If you define the macro NDEBUG before including assert.h , then calls to assert() have no effect.
In C11, the header assert.h defines the macro static_assert as a synonym for the keyword _Static_assert. A _Static_assert declaration tests a constant expression for a nonzero value at compile time (see “_Static_assert Declarations”).
C99 supports arithmetic with complex numbers by introducing complex floating-point types and including appropriate functions in the math library. The header file complex.h contains the prototypes of the complex math functions and defines the related macros. For a brief description of complex numbers and their representation in C, see “Complex Floating-Point Types”.
Under the C11 standard, support for complex numbers is optional. The header complex.h is absent if the macro __STDC_NO_COMPLEX__ is defined.
The names of the mathematical functions for complex numbers all begin with the letter c. For example, csin() is the complex sine function, and cexp() the complex exponential function. You can find a complete list of these functions in “Mathematical Functions”. In addition, the following function names are reserved for future extensions:
cerf() cerfc() cexp2() cexpm1() clog10() clog1p() clog2() clgamma() ctgamma()
The same names with the suffixes f (for float _Complex) and l (for long double _Complex) are also reserved.
The header file complex.h defines the following macros:
complexThis is a synonym for the keyword _Complex.
_Complex_IThis macro represents an expression of type const float _Complex whose value is the imaginary unit, i.
IThis macro is a synonym for _Complex_I (or for _Imaginary_I, if defined), and likewise represents the imaginary unit, i.
A C11 implementation may also include types to represent pure imaginary numbers. If and only if a given C implementation includes such types, it defines the two following macros:
imaginaryThis is a synonym for the keyword _Imaginary.
_Imaginary_IThis macro represents an expression of type const float _Imaginary whose value is the imaginary unit, i. If _Imaginary_I is defined, the macro I is defined as a synonym for it.
C11 also provides the function-like macros CMPLX, CMPXF, and CMPLXL to compose a complex number from its real and imaginary parts.
This header contains the declarations of functions to classify and convert single characters. These include the following functions, which are usually also implemented as macros:
isalnum() isalpha() isblank() iscntrl() isdigit() isgraph() islower() isprint() ispunct() isspace() isupper() isxdigit() tolower() toupper()
These functions or macros take an argument of type int, whose value must be between 0 and 255, inclusive, or EOF. The macro EOF is defined in stdio.h. The classification of characters, and hence the behavior of these functions (except isdigit() and isxdigit()), is dependent on the current locale.
All names that begin with is or to followed by a lowercase letter are reserved for future extensions.
The header errno.h defines the macro errno as representing a thread-local error variable of the type int. Various functions in the standard library set errno to a specified positive value to indicate the type of error encountered during execution. For each function that uses errno, its possible values are indicated in the function’s description in Chapter 18.
The identifier errno is not necessarily declared as a global variable. It may be a macro that represents a modifiable lvalue with the type int. For example, if _errno() is a function that returns a pointer to int, then errno could be defined as follows:
#define errno (* _errno())
When the program starts, errno in the initial thread has the value zero. The initial value of errno in any other thread is undetermined. Because no standard function sets the value of errno to zero, a program that uses errno to detect errors should set the value of errno to zero before calling a standard library function.
The header errno.h also defines an appropriate macro constant for each possible value of errno. The names of these macros begin with E, and include at least these three:
EDOMDomain error; the function is mathematically not defined for the given value of the argument.
EILSEQIllegal sequence. For example, a multibyte character conversion function may have encountered a sequence of bytes that cannot be interpreted as a multibyte character in the encoding used.
ERANGERange error; the function’s mathematical result is not representable by its return type.
All macro names that begin with E followed by a digit or an uppercase letter are reserved for future extensions.
C11 implementations that support the new bounds-checking, “secure” functions also define the type errno_t in the header errno.h as a synonym for int.
C99 introduced the floating-point environment, which provides system variables to allow programs to deal flexibly with floating-point exceptions and control modes. (See also “Mathematical Functions”.) The header fenv.h contains all the declarations that may be used in accessing the floating-point environment, although implementations are not required to support floating-point exceptions or control modes.
The header fenv.h contains the following definitions to manipulate the floating-point environment:
fenv_tA type capable of representing the floating-point environment as a whole.
FE_DFL_ENVAn object of the type const fenv_t *; points to the default floating-point environment, which is in effect when the program starts.
Implementations that support floating-point exceptions also define an integer macro corresponding to the status flag for each kind of exception that can occur. Standard names for these macros are:
FE_DIVBYZERO, FE_INEXACT, FE_INVALID, FE_OVERFLOW, FE_UNDERFLOWThese macros allow you to select one or more kinds of exceptions when accessing the status flags. You can also combine several such macros using the bitwise OR operator (|) to obtain a value that represents several kinds of exceptions.
FE_ALL_EXCEPTThis macro represents the bitwise OR of all the exception macros defined in the given implementation.
If a given implementation does not support one or more of the exceptions indicated by these macros, then the corresponding macro is not defined. Furthermore, implementations may also define other exception macros, with names that begin with FE_ followed by an uppercase letter.
In addition to the macros listed previously, implementations that support floating-point exceptions also define a type for the floating-point exception status flags:
fexcept_tThis type represents all of the floating-point exception status flags, including all the information that the given implementation provides about exceptions. Such information may include the address of the instruction that raised the exception, for example. This type is used by the functions fegetexceptflag() and fesetexceptflag().
Implementations may allow programs to query or set the way floating-point results are rounded. If so, the header fenv.h defines the following macros as distinct integer constants:
|
|
|
|
A given implementation might not define all of these macros if it does not support the corresponding rounding direction, and might also define macro names for other rounding modes that it does support. The function fegetround() returns the current rounding mode—that is, the value of the corresponding macro name; and fesetround() sets the rounding mode as specified by its argument.
The header file float.h defines macros that describe the value range, the precision, and other properties of the types float, double, and long double.
The values of the macros in float.h refer to the following normalized representation of a floating-point number x:
x = s × 0.d1d2...dp × be
The symbols in this representation have the following meanings and conditions:
sThe sign of x; s = 1 or s = -1
diA base b digit in the significand (also called the mantissa) of x (0.d1d2…dp in the general representation); d1 > 0 if x ≠ 0
pThe number of digits in the significand (or to be more precise, in the fraction part)
bThe base of the exponent; b > 1
eThe integer exponent; emin ≤ e ≤ emax
The floating-point types may also be able to represent other values besides normalized floating-point numbers, such as the following kinds of values:
Subnormal floating-point numbers, or those for which x ≠ 0, e = emin, and d1 = 0.
Non-normalized floating-point numbers, for which x ≠ 0, e > emin, and d1 = 0.
Infinities; that is, values that represent +∞ or −∞.
NaNs, or values that do not represent valid floating-point numbers. NaN stands for “not a number.”
NaNs can be either quiet or signaling NaNs. When a signaling NaN occurs in the evaluation of an arithmetic expression, it sets the exception flag FE_INVALID in the floating-point environment. Quiet NaNs do not set the exception flag.
The following two macros defined in the header float.h provide details about how floating-point arithmetic is performed:
FLT_ROUNDSThis macro represents the currently active rounding direction, and is the only macro defined in float.h whose value can change during runtime. It can have the following values:
-1 |
Undetermined |
0 |
Toward zero |
1 |
Toward the nearest representable value |
2 |
Toward the next greater value |
3 |
Toward the next smaller value |
Other values may stand for implementation-defined rounding modes. If the implementation supports different rounding modes, you can change the active rounding mode by calling the function fesetround().
FLT_EVAL_METHODThe macro FLT_EVAL_METHOD has one of several possible values, but does not change during the program’s runtime. This macro indicates the floating-point format used internally for operations on floating-point numbers. The internal format may have greater precision and a broader value range than the operands’ type. The possible values of FLT_EVAL_METHOD have the following meanings:
-1 |
Undetermined |
0 |
Arithmetic operations are performed with the precision of the operands’ type. |
1 |
Operations on |
2 |
All operations are performed internally in |
For a given base, the precision with which numbers are represented is determined by the number of digits in the significand, and the value range is indicated by the least and greatest values of the exponent. These values are provided, for each real floating-point type, by the following macros. The macro names with the prefix FLT_ represent characteristics of the type float; those with the prefix DBL_ refer to double; and those with LDBL_ refer to long double. The value of FLT_RADIX applies to all three floating-point types.
FLT_RADIXThe radix or base (b) of the exponential representation of floating point numbers; usually 2
FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIGThe number of digits in the significand or mantissa (p)
FLT_MIN_EXP, DBL_MIN_EXP, LDBL_MIN_EXPThe smallest negative exponent to the base FLT_RADIX (emin)
FLT_MAX_EXP, DBL_MAX_EXP, LDBL_MAX_EXPThe largest positive exponent to the base FLT_RADIX (emax)
In practice, it is useful to have the precision and the value range of a floating-point type in decimal notation. Macros for these characteristics are listed in Table 16-1. The values in the second column represent the C standard’s minimum requirements. The values in the third column are the requirements of the IEC 60559 standard for floating-point numbers with single and double precision. In most C implementations, the types float and double have these IEC 60559 characteristics.
| Macro | ISO 9899 | IEC 60559 | Meaning |
|---|---|---|---|
FLT_DIGDBL_DIGLDBL_DIG |
6 10 10 |
6 15 |
The precision as a number of decimal digits. A decimal floating-point number of this many digits, stored in binary representation, always yields the same value to this many digits when converted back to decimal notation. |
DECIMAL_DIG |
10 | 17 | The number of decimal digits necessary to represent any number of the largest floating-point type supported so that it can be converted to decimal notation and back to binary representation without its value changing. |
FLT_MIN_10_EXPDBL_MIN_10_EXPLDBL_MIN_10_EXP |
-37 -37 -37 |
-37 -307 |
The smallest negative exponent to base 10, n, such that 10n is within the positive range of the type. |
FLT_MAX_10_EXPDBL_MAX_10_EXPLDBL_MAX_10_EXP |
+37 +37 +37 |
+38 +308 |
The greatest exponent to base 10, n, such that 10n is within the range of the type. |
FLT_MINDBL_MINLDBL_MIN |
1E-37 1E-37 1E-37 |
1.17549435E-38F 2.2250738585072014E-308 |
The smallest representable positive floating-point number. |
FLT_MAXDBL_MAXLDBL_MAX |
1E+37 1E+37 1E+37 |
3.40282347E+38F 1.7976931348623157E+308 |
The greatest representable finite floating-point number. |
FLT_EPSILONDBL_EPSILONLDBL_EPSILON |
1E-5 1E-9 1E-9 |
1.19209290E-07F 2.2204460492503131E-16 |
The positive difference between 1 and the smallest representable number greater than 1. |
The header inttypes.h includes the header stdint.h, and contains extensions to it. The header stdint.h defines integer types with specified bit widths, including the types intmax_t and uintmax_t, which represent the widest integer types implemented. (See also “Integer Types Defined in Standard Headers”.)
The header inttypes.h defines the following structure type:
imaxdiv_tThis is a structure type of two members named quot and rem, whose type is intmax_t. The function imaxdiv() divides one number of type intmax_t by another, and stores the quotient and remainder in an object of type struct imaxdiv_t.
In addition to imaxdiv(), the header inttypes.h also declares the function imaxabs(), which returns the absolute value of an integer of the type intmax_t, and four functions to convert strings into integers with the type intmax_t or uintmax_t.
Furthermore, inttypes.h defines macros for string literals that you can use as type specifiers in format string arguments to the printf and scanf functions. The header contains macros to specify each of the types defined in stdint.h.
The names of the type specifier macros for the printf family of functions begin with the prefix PRI, followed by a conversion specifier (d, i, o, x, or X) and a sequence of uppercase letters that refers to a type name. For example, the macro names with the conversion specifier d are:
PRIdN PRIdLEASTN PRIdFASTN PRIdMAX PRIdPTR
The letter N at the end of the first three macro names listed here is a placeholder for a decimal number indicating the bit width of a given type. Commonly implemented values are 8, 16, 32, and 64.
Other PRI… macro names are analogous to the five just listed, but have different conversion specifiers in place of the letter d, such as i, o, x, or X. The following example uses a variable with the type int_fast32_t:
#include <inttypes.h>int_fast32_ti32Var;/* ... */printf("The value of i32Var, in hexadecimal notation: ""%10"PRIxFAST32"\n",i32Var);
The preprocessor concatenates the string literals "%10" and PRIxFAST32 to form the full conversion specification. The resulting output of i32Var has a field width of 10 characters.
The names of the conversion specifier macros for the scanf family of functions begins with the prefix SCN. The remaining characters are the same as the corresponding PRI… macros, except that there is no conversion specifier X for scanf(). For example, the macro names with the conversion specifier d are:
SCNdN SCNdLEASTN SCNdFASTN SCNdMAX SCNdPTR
Again, the letter N at the end of the first three macro names as listed here is a placeholder for a decimal number indicating the bit width of a given type. Commonly implemented values are 8, 16, 32, and 64.
The header iso646.h defines the eleven macros listed in Table 16-2, which you can use as synonyms for C’s logical and bitwise operators.
| Macro | Meaning |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The header limits.h contains macros to represent the least and greatest representable value of each integer type. These macros are listed in Table 16-3. The numeric values in the table represent the minimum requirements of the C standard.
| Type | Minimum | Maximum | Maximum value of the unsigned type |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The range of the type char depends on whether char is signed or unsigned. If char is signed, then CHAR_MIN is equal to SCHAR_MIN and CHAR_MAX equal to SCHAR_MAX. If char is unsigned, then CHAR_MIN is zero and CHAR_MAX is equal to UCHAR_MAX.
The header limits.h also defines the following two macros:
CHAR_BITThe number of bits in a byte, which must be at least 8.
MB_LEN_MAXThe maximum number of bytes in a multibyte character, which must be at least 1.
The value of the macro CHAR_BIT determines the value of UCHAR_MAX: UCHAR_MAX is equal to 2CHAR_BIT − 1.
The value of MB_LEN_MAX is greater than or equal to the value of MB_CUR_MAX, which is defined in the header stdlib.h. MB_CUR_MAX represents the maximum number of bytes in a multibyte character in the current locale. More specifically, the value depends on the locale setting for the LC_CTYPE category (see the description of setlocale() in Chapter 18 for details). If the current locale uses a stateful multibyte encoding, then both MB_LEN_MAX and MB_CUR_MAX include the number of bytes necessary for a state-shift sequence before the actual multibyte character.
The standard library supports the development of C programs that are able to adapt to local cultural conventions. For example, programs may use locale-specific character sets or formats for currency information.
The header locale.h declares two functions, the type struct lconv, the macro NULL for the null pointer constant, and macros whose names begin with LC_ for the locale information categories.
The function setlocale() allows you to query or set the current locale. The information that makes up the locale is divided into categories, which you can query and set individually. The following integer macros are defined to designate these categories:
LC_ALL LC_COLLATE LC_CTYPE LC_MONETARY LC_NUMERIC LC_TIME
The function setlocale() takes one of these macros as its first argument, and operates on the corresponding locale category. The meanings of the macros are described under the setlocale() function in Chapter 18. Implementations may also define additional macros whose names start with LC_ followed by an uppercase letter.
The second function declared in locale.h is localeconv(), which supplies information about the conventions of the current locale by filling the members of a structure of the type struct lconv. localeconv() returns a pointer to the structure. The structure contains members to describe the local formatting of numerals, monetary amounts, and date and time information. For details, see the description of localeconv() in Chapter 18.
The header math.h declares the mathematical functions for real floating-point numbers, and the related macros and types.
The mathematical functions for integer types are declared in stdlib.h, and those for complex numbers in complex.h. In addition, the header tgmath.h defines the type-generic macros, which allow you to call mathematical functions by uniform names regardless of the arguments’ type. For a summary of the mathematical functions in the standard library, see “Mathematical Functions”.
The header math.h defines the two types float_t and double_t. These types represent the floating-point precision used internally by the given implementation in evaluating arithmetic expressions of the types float and double. (If you use operands of the type float_t or double_t in your programs, they will not need to be converted before arithmetic operations, as float and double may.) The value of the macro FLT_EVAL_METHOD, defined in the header float.h, indicates which basic types correspond to float_t and double_t. The possible values of FLT_EVAL_METHOD are explained in Table 16-4.
| FLT_EVAL_METHOD | float_t | double_t |
|---|---|---|
0 |
float |
double |
1 |
double |
double |
2 |
long double |
long double |
Any other value of FLT_EVAL_METHOD indicates that the evaluation of floating-point expressions is implementation-defined.
In addition to normalized floating-point numbers, the floating-point types can also represent other values, such as infinities and NaNs (see “Normalized representation of floating-point numbers”). C99 specifies five classes of floating-point values, and defines an integer macro to designate each of these categories. The five macros are:
FP_ZERO FP_NORMAL FP_SUBNORMAL FP_INFINITE FP_NAN
Implementations may also define additional categories, and corresponding macros whose names begin with FP_ followed by an uppercase letter.
math.h defines the following function-like macros to classify floating-point values:
fpclassify()This macro expands to the value of the FP_… macro that designates the category of its floating-point argument.
isfinite(), isinf(), isnan(), isnormal(), signbit()These function-like macros test whether their argument belongs to a specific category.
The header math.h also defines the following macros:
HUGE_VAL, HUGE_VALF, HUGE_VALLHUGE_VAL represents a large positive value with the type double. Mathematical functions that return double can return the value of HUGE_VAL, with the appropriate sign, when the result exceeds the finite value range of double. The value of HUGE_VAL may also represent a positive infinity, if the implementation supports such a value.
HUGE_VALF and HUGE_VALL are analogous to HUGE_VAL, but have the types float and long double.
INFINITYThis macro’s value is constant expression of type float that represents a positive or unsigned infinity, if such a value is representable in the given implementation. If not, then INFINITY represents a constant expression of type float that yields an overflow when evaluated, so that the compiler generates an error message when processing it.
NANNaN stands for “not a number.” The macro NAN is a constant of type float whose value is not a valid floating-point number. It is defined only if the implementation supports quiet NaNs—that is, if a NaN can occur without raising a floating-point exception.
FP_FAST_FMA, FP_FAST_FMAF, FP_FAST_FMALFMA stands for “fused multiply-and-add.” The macro FP_FAST_FMA is defined if the function call fma(x,y,z) can be evaluated at least as fast as the mathematically equivalent expression x*y+z, for x, y, and z of type double. This is typically the case if the fma() function makes use of a special FMA machine operation.
The macros FP_FAST_FMAF and FP_FAST_FMAL are analogous to FP_FAST_FMA, but refer to the types float and long double.
FP_ILOGB0, FP_ILOGBNANThese macros represent the respective values returned by the function call ilogb(x) when the argument x is zero or NaN. FP_ILOGB0 is equal either to INT_MIN or to -INT_MAX, and FP_ILOGBNAN equals either INT_MIN or INT_MAX.
MATH_ERRNO, MATH_ERREXCEPT, math_errhandlingMATH_ERRNO is the constant 1 and MATH_ERREXCEPT is the constant 2. These values are represented by distinct bits, and hence can be used as bit masks in querying the value of math_errhandling. The identifier math_errhandling is either a macro or an external variable with the type int. Its value is constant throughout runtime, and you can query it in your programs to determine whether the mathematical functions indicate errors by raising exceptions or by providing an error code, or both. If the expression math_errhandling & MATH_ERRNO is not equal to zero, then the program can read the global error variable errno to identify domain and range errors in math function calls. Similarly, if math_errhandling & MATH_ERREXCEPT is nonzero, then the math functions indicate errors using the floating-point environment’s exception flags. For more details, see “Error Handling”.
If a given implementation supports programs that use floating-point exceptions, then the header fenv.h must define at least the macros FE_DIVBYZERO, FE_INVALID, and FE_OVERFLOW.
The header setjmp.h declares the function longjmp(), and defines the array type jmp_buf and the function-like macro setjmp().
Calling setjmp() saves the current execution environment, including at least the momentary register and stack values, in a variable whose type is jmp_buf. In this way, the setjmp() call bookmarks a point in the program, which you can then jump back to at any time by calling the companion function longjmp(). In effect, setjmp() and longjmp() allow you to program a nonlocal “goto.”
The header signal.h declares the functions raise() and signal(), as well as related macros and the following integer type:
sig_atomic_tYou can use the type sig_atomic_t to define objects that are accessible in an atomic operation. Such objects are suitable for use in hardware interrupt signal handlers, for example. The value range of this type is described by the values of the macros SIG_ATOMIC_MIN and SIG_ATOMIC_MAX, which are defined in the header stdint.h.
A signal handler is a function that is automatically executed when the program receives a given signal from the operating environment. You can use the function signal() in your programs to install functions of your own as signal handlers.
Each type of signal that programs can receive is identified by a signal number. Accordingly, signal.h defines macros of type int to designate the signal types. The required signal type macros are:
|
|
|
|
|
|
The meanings of these signal types are described along with the signal() function in Chapter 18. Implementations may also define other signals. The names of the corresponding macros begin with SIG or SIG_, followed by an uppercase letter.
The first argument to the function signal() is a signal number. The second is the address of a signal handler function, or one of the following macros:
SIG_DFL, SIG_IGNThese macros are constant expressions whose values cannot be equal to the address of any declarable function. SIG_DFL installs the implementation’s default signal handler for the given signal type. If you call signal() with SIG_IGN as the second argument, the program ignores signals of the given type, if the implementation allows programs to ignore them.
SIG_ERRThis macro represents the value returned by the signal() function if an error occurs.
The header stdalign.h is new in C11, and defines the following four macros:
alignasThis is a synonym for the specifier _Alignas. When an object is defined with the specifier _Alignas, it can have a stricter alignment than its type requires.
alignofThis is a synonym for the operator _Alignof, which obtains the alignment of a type.
__alignas_is_defined, __alignof_is_definedFor more information on the alignment of objects, see “The Alignment of Objects in Memory”.
The header stdarg.h defines one type and four macros for use in accessing the optional arguments of functions that support them (see “Variable Numbers of Arguments”):
va_listFunctions with variable numbers of arguments use an object of the type va_list to access their optional arguments. Such an object is commonly called an argument pointer, as it serves as a reference to a list of optional arguments.
The following function-like macros operate on objects of the type va_list:
va_start()Sets the argument pointer to the first optional argument in the list.
va_arg()Returns the current argument and sets the argument pointer to the next one in the list.
va_copy()va_end()Cleans up after the use of a va_list object. A function with a variable number of arguments must contain a va_end() macro call corresponding to each invocation of va_start() or va_copy().
The macros va_copy() and va_end() may also be implemented as functions.
The header stdatomic.h is new in C11. It contains function declarations and definitions of various types and macros for atomic operations on data that is shared by several threads. For explanations and examples of atomic operations, see “Accessing Shared Data”.
Support for atomic operations is optional: C11 implementations that define the macro __STDC_NO_ATOMICS__ need not provide the header stdatomic.h.
The names of the functions declared begin with the prefix atomic_, as in atomic_store(). All function names that begin with the prefix atomic_ followed by a lowercase letter are reserved for future extensions. Type names that begin with atomic_ or memory_ followed by a lowercase letter are likewise reserved, as are macro names that begin with ATOMIC_ followed by an uppercase letter.
atomic_flagA structure type that is capable of representing the states “set” and “clear,” and is atomically accessible without using a lock.
memory_orderAn enumerated type that defines the following constants used for specifying the memory-ordering constraints of atomic operations:
memory_order_relaxed memory_order_release memory_order_acquire
memory_order_consume memory_order_acq_rel memory_order_seq_cst
For a description of these enumeration constants with examples, see “Memory Ordering”. An argument of the type memory_order is used with the atomic functions whose names end with the suffix _explicit, such as atomic_store_explicit(), and with the function atomic_thread_fence().
The header stdatomic.h also defines the type names listed in Table 16-5, which are synonyms for the integer atomic types named in the right column.
| Atomic type name | Type |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The values of the following macros indicate whether the corresponding atomic types (signed and unsigned) are “lock free”—in other words, whether they permit atomic access without the use of a lock.
|
|
|
|
|
|
|
|
|
|
All of these macros have values of 0, 1, or 2. The value 0 means that the type is never lock-free, 1 means it is lock-free for certain objects, and 2 means it is always lock-free.
In addition to the LOCK_FREE macros, stdatomic.h also defines three other macros:
ATOMIC_FLAG_INITThis macro is an initializer used to initialize an object of the type atomic_flag to the “clear” state.
ATOMIC_VAR_INIT(value)This function-like macro expands to an initializer which can be used to initialize an atomic object that is capable of storing the argument’s value.
Atomic objects can also be initialized using the generic function atomic_init(). In any case, the initialization of an atomic object is not an atomic operation. Like non-atomic objects, atomic objects with static or thread-local storage duration which are not explicitly initialized have the initial value 0.
kill_dependency(y)This function-like macro breaks a dependency chain that was started by a consume operation—that is, by an atomic load operation with the memory order specification memory_order_consume. The macro’s return value is the value of the argument y, and is no longer a part of a dependency chain. This allows the compiler to apply further optimization.
The header stddef.h defines three types and two macros for use in all kinds of programs. The three types are:
ptrdiff_tA signed integer type that represents the difference between two pointers.
size_tAn unsigned integer type used to represent the result of sizeof operations; also defined in stdlib.h, wchar.h, stdio.h, and string.h.
wchar_tAn integer type that is wide enough to store any code in the largest extended character set that the implementation supports; also defined in stdlib.h and wchar.h.
Macros that specify the least and greatest representable values of these three types are defined in the header stdint.h .
In C11 implementations, one or two other types are also defined in stddef.h:
max_align_tIn C11, this is an object type with the largest possible alignment that the implementation supports in all contexts. It may be a type with an alignment of 8 or 16, for example.
rsize_tThis type is equivalent with size_t, and is defined only if the C11 implementation supports the secure standard functions with bounds-checking. If rsize_t is defined, then the macro RSIZE_MAX is also defined in the header stdint.h, typically with a value less than that of SIZE_MAX. In standard functions with a parameter of the type rsize_t, passing a value greater than RSIZE_MAX violates a runtime constraint.
The two macros defined in stddef.h are:
NULLThis macro represents a null pointer constant, which is an integer constant expression with the value 0, or such an expression cast as the type void *. The macro NULL is also defined in the headers stdio.h, stdlib.h, string.h, time.h, and wchar.h.
offsetof( structure_type, member )This macro yields an integer constant with type size_t whose value is the number of bytes between the beginning of the structure and the beginning of its member member. The member must not be a bit-field.
The header stdint.h defines integer types with specific bit widths, and macros that indicate the value ranges of these and other types. For example, you can use the int64_t type, defined in stdint.h, to define a signed, 64-bit integer.
If a signed type of a given specific width is defined, then the corresponding unsigned type is also defined, and vice versa. Unsigned types have names that start with u (such as uint64_t, for example), which is followed by the name of the corresponding signed type (such as int64_t).
For each type defined in stdint.h, macros are also defined to designate the type’s least and greatest representable values. Table 16-6 lists the names of these macros, with the standard’s requirements for their values. The word “exactly” in the table indicates that the standard specifies an exact value rather than a maximum or minimum. Otherwise, the standard allows the implementation to exceed the ranges given in the table. The letter N before an underscore in the type names as listed here is a placeholder for a decimal number indicating the bit width of a given type. Commonly implemented values are 8, 16, 32, and 64.
| Type | Minimum | Maximum | Maximum value of the unsigned type |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For the meanings of the fixed-width integer type names, and the C standard’s requirements as to which of them must be defined, see “Integer Types Defined in Standard Headers”.
The header stdint.h also contains macros to document the value ranges of types defined in other headers. These types are listed in Table 16-7. The numbers in the table represent the minimum requirements of the C standard. The types sig_atomic_t, wchar_t, and wint_t may be defined as signed or unsigned.
| Type | Minimum | Maximum |
|---|---|---|
|
|
|
|
|
|
|
N/A |
|
|
N/A |
|
|
|
|
|
|
|
The types ptrdiff_t, size_t, rsize_t and wchar_t are described in “stddef.h”. The type rsize_t, and hence the corresponding macro RSIZE_MAX, are only defined if the implementation supports the bounds-checking, “secure” functions. The type sig_atomic_t is described in “signal.h”, and wint_t is described in “wchar.h”.
For each decimal number N for which the stdint.h header defines a type int_least N_t (an integer type that is at leastN bits wide), the header also defines two function-like macros to generate values with the type int_leastN_t. Arguments to these macros must be constants in decimal, octal, or hexadecimal notation, and must be within the value range of the intended type (see “Integer Constants”). The macros are:
INTN_C(value), UINTN_C(value)Expands to a signed or unsigned integer constant with the specified value and the type int_leastN_t or uint_leastN_t, which is at least N bits wide. For example, if uint_least32_t is defined as a synonym for the type unsigned long, then the macro call UINT32_C(123) may expand to the constant 123UL.
The following macros are defined for the types intmax_t and uintmax_t:
INTMAX_C(value), UINTMAX_C(value)These macros expand to a constant with the specified value and the type intmax_t or uintmax_t.
The header stdio.h contains the declarations of all the basic functions for input and output, as well as related macro and type definitions. The declarations for wide-character I/O functions—that is, for input and output of characters with the type wchar_t—are contained in the header file wchar.h (see also Chapter 13).
In addition to size_t, which is discussed in “stddef.h”, stdio.h defines the following two types:
FILEAn object of the type FILE contains all the information necessary for controlling an I/O stream. This information includes a pointer to the stream’s buffer, a file access position indicator, and flags to indicate error and end-of-file conditions.
fpos_tObjects of this type, which is the return type of the fgetpos() function, are able to store all the information pertaining to a file access position. You can use the fsetpos() function to resume file processing at the position described by an fpos_t object.
In C11 implementations that support the bounds-checking, “secure” functions, the header stdio.h also declares the types errno_t (see “errno.h”) and rsize_t (see “stddef.h”).
The header stdio.h defines the macro NULL (described in “stddef.h”) as well as the following 12 macros, all of which represent integer constant expressions:
_IOFBF, _IOLBF, _IONBFThese constants are used as arguments to the setvbuf() function, and specify I/O buffering modes. The names stand for “fully buffered,” “line buffered,” and “not buffered.”
BUFSIZThis is the size of the buffer activated by the setbuf() function, in bytes.
EOF“End of file.” A negative value (usually -1) with type int. Various functions return the constant EOF to indicate an attempt to read at the end of a file, or to indicate an error.
FILENAME_MAXThis constant indicates how big a char array must be to store the longest filename supported by the fopen() function.
FOPEN_MAXPrograms are allowed to have at least this number of files open simultaneously.
L_tmpnamThis constant indicates how big a char array must be to store a filename generated by the tmpnam() function.
SEEK_SET, SEEK_CUR, SEEK_ENDThese constants are used as the third argument to the fseek() function.
TMP_MAXThe maximum number of unique filenames that the tmpnam() function can generate. This number is at least 25.
C11 implementations that support the new bounds-checking, “secure” functions also define the following macros:
L_tmpnam_s, TMP_MAX_SThe meanings of these macros in the context of the function tmpnam_s() are analogous to those of the macros L_tmpnam and TMP_MAX, described in the preceding list, for the function tmpnam().
The header stdio.h also declares three objects:
The header stdlib.h declares general utility functions for the following purposes:
Conversion of numeral strings into binary numeric values
Random number generation
Memory management
Communication with the operating system
Searching and sorting
Integer arithmetic
Conversion of multibyte characters to wide characters and vice versa
stdlib.h also defines the types size_t and wchar_t, which are described in “stddef.h”, as well as the following three types:
div_t, ldiv_t, lldiv_tThese are structure types used to hold the results of the integer division functions div(), ldiv(), and lldiv(). These types are structures of two members, quot and rem, which have the type int, long, or long long.
In C11 implementations that support the bounds-checking, “secure” functions, the header stdlib.h also declares the types errno_t (see “errno.h”), and rsize_t (see “stddef.h”), and the following type:
constraint_handler_tThis is the function-pointer type of the constraint handler argument passed to the function set_constraint_handler_s(). The last handler function passed in this way to the set_constraint_handler_s() function is called when a runtime constraint is violated during a call to a “secure” function.
The header stdlib.h defines the macro NULL (see “stddef.h”) as well as the following four macros:
EXIT_FAILURE, EXIT_SUCCESSInteger constants that you can pass as arguments to the functions exit() and _Exit() to report your program’s exit status to the operating environment.
MB_CUR_MAXA nonzero integer expression with the type size_t. This is the maximum number of bytes in a multibyte character under the current locale setting for the locale category LC_CTYPE. This value must be less than or equal to MB_LEN_MAX, defined in limits.h.
RAND_MAXAn integer constant that indicates the greatest possible value that can be returned by the function rand().
The header string.h declares the string manipulation functions, along with other functions that operate on byte arrays. The names of these functions begin with str, as in strcpy(), for example, or with mem, as in memcpy(). Function names beginning with str, mem, or wcs followed by a lowercase letter are reserved for future extensions.
The header string.h also defines the type size_t and the macro NULL, described in “stddef.h”.
In C11 implementations that support the bounds-checking, “secure” functions, the header string.h also declares the types errno_t (described in “errno.h”) and rsize_t (described in “stddef.h”).
The header tgmath.h includes the headers math.h and complex.h, and defines the type-generic macros. These macros allow you to call different variants of mathematical functions by a uniform name, regardless of the arguments’ type.
The mathematical functions in the standard library are defined with parameters of specific real or complex floating-point types. Their names indicate types other than double by the prefix c for _Complex, or by the suffixes f for float and l for long double. The type-generic macros are overloaded names for these functions that you can use with arguments of any arithmetic type. These macros detect the arguments’ type and call the appropriate math function.
The header tgmath.h defines type-generic macros for all the mathematical functions with floating-point parameters except modf(), modff(), and modfl(). If a given function is defined for both real and complex or only for real floating-point types, then the corresponding type-generic macro has the same name as the function version for arguments of the type double—that is, the base name of the function with no c prefix and no f or l suffix. For an example, assume the following declarations:
#include <tgmath.h>floatf=0.5F;doubled=1.5;double_Complexz1=-1;longdouble_Complexz2=I;
Each of the macro calls in Table 16-8 then expands to the function call shown in the right column.
| Type-generic macro call | Expansion |
|---|---|
|
|
|
|
|
|
|
|
Arguments with integer types are automatically converted to double. If you use arguments of different types in invoking a type-generic macro with two parameters, such as pow(), the macro calls the function version for the argument type with the higher rank (see “Hierarchy of Types”). If any argument has a complex floating-point type, the macro calls the function for complex numbers.
Several functions are defined only for complex floating-point types. The type-generic macros for these functions have names that start with c, but with no f or l suffix:
carg() cimag() conj() cproj() creal()
If you invoke one of these macros with a real argument, it calls the function for the complex type that corresponds to the argument’s real floating-point type.
The header threads.h, which was introduced in C11, declares the functions for multithreading support, and defines the accompanying types and macros. The header threads.h also includes the header time.h. For details and examples on multithreaded programming using C11 features, see Chapter 14.
Multithreading support is optional in C11: implementations that define the macro __STDC_NO_THREADS__ need not provide the header threads.h.
The functions and types defined in threads.h are related to threads, mutex objects, condition variables and thread-specific storage. Accordingly, the names of the functions and types begin with one of the prefixes thrd_, mtx_, cnd_ and tss_. Other names beginning with any of these prefixes, followed by a lowercase letter, are reserved for future extensions.
thrd_tthrd_start_tThe type int (*)(void*) (that is, a pointer to a function that takes one void-pointer argument and returns an integer). This is the function pointer type passed as an argument to the function thrd_create() to specify the function that a new thread will execute.
mtx_tcnd_ttss_tThe type of an object that represents a pointer to thread-specific storage.
tss_dtor_tThe type void (*)(void*) (that is, a pointer to a function that takes one void-pointer argument and has no return value). This is the function-pointer type of the argument passed to the function tss_create() to specify the destructor function for the thread-specific storage requested.
once_flagThe header threads.h defines the following enumeration constants for the return value of the thread functions:
thrd_successIndicates that the function succeeded in performing the requested operation.
thrd_errorIndicates that an error occurred during the execution of the function.
thrd_busyIndicates that the function failed because a required resource is still in use.
thrd_nomemIndicates that the function was unable to allocate sufficient memory.
thrd_timeoutIndicates that the time limit specified in the function call expired before the function was able to obtain the required resource.
Three constants are defined for use as an argument to the function mtx_init() to specify the properties of the new mutex to be created. The three constants are used to form one of four argument values as follows:
The threads.h header defines the following three macros:
The header time.h declares the standard functions, macros, and types for manipulating date and time information (by the Gregorian calendar). These functions are listed in “Date and Time”.
The types declared in time.h are size_t (see stddef.h in this chapter) and the following three types:
clock_tThis is the arithmetic type returned by the function clock() (usually defined as unsigned long).
time_tThis is an arithmetic type returned by the functions timer() and mktime() (usually defined as long).
struct tmThe members of this structure represent a date or a time, broken down into seconds, minutes, hours, the day of the month, and so on. The functions gmtime() and localtime() return a pointer to struct tm. The structure’s members are described under the gmtime() function in Chapter 18.
In C11 implementations that support the bounds-checking, “secure” functions, the header time.h also declares the types errno_t (see “errno.h”) and rsize_t (see “stddef.h”).
The header time.h defines the macro NULL (see stddef.h) and the following macro:
In C11, the new header uchar.h declares types and functions for processing Unicode characters. The types declared are size_t (see “stddef.h”), mbstate_t (see “wchar.h”), and the following two new types:
char16_tAn unsigned integer type for 16-bit characters. This type is the same as uint_least16_t. Implementations that define the macro __STDC_UTF_16__ use UTF-16 encoding for characters of the type char16_t. The macro is not defined if a different encoding is used.
char32_tAn unsigned integer type for 32-bit characters. This type is the same as uint_least32_t. Implementations that define the macro __STDC_UTF_32__ use UTF-32 encoding for characters of the type char32_t. The macro is not defined if a different encoding is used.
The types uint_least16_t and uint_least32_t are described in “stdint.h”. The header uchar.h declares the following four functions for converting 16-bit or 32-bit Unicode characters to multibyte characters and vice versa: mbrtoc16(), c16rtomb(), mbrtoc32(), and c32rtomb().
Functions and types for processing wide characters of the type wchar_t are declared in the header wchar.h.
The headers stdio.h, stdlib.h, string.h, and time.h all declare functions for processing byte-character strings—that is, strings of characters with the type char. The header wchar.h declares similar functions for wide strings: strings of wide characters, which have the type wchar_t. The names of these functions generally contain an additional w, as in wprintf(), for example, or start with wcs instead of str, as in wcscpy(), which is the name of the wide-string version of the strcpy() function.
Furthermore, the header wchar.h declares more functions for converting multibyte characters to wide characters and vice versa, in addition to those declared in stdlib.h. wchar.h declares functions for the following kinds of purposes:
Wide and multibyte character I/O
Conversion of wide-string numerals
Copying, concatenating, and comparing wide strings and wide-character arrays
Formatting date and time information in wide strings
Conversion of multibyte characters to wide characters and vice versa
The types defined in wchar.h are size_t and wchar_t (explained in “stddef.h”); struct tm (see time.h); and the following two types:
mbstate_tObjects of this type store the parsing state information involved in the conversion of a multibyte string to a wide-character string, or vice versa.
wint_tAn integer type whose bit width is at least that of int. wint_t must be wide enough to represent the value range of wchar_t and the value of the macro WEOF. The types wint_t and wchar_t may be identical.
In C11 implementations that support the bounds-checking, “secure” functions, the header wchar.h also declares the types errno_t (see “errno.h”) and rsize_t (see “stddef.h”).
The header wchar.h defines the macro NULL (see “stddef.h”), the macros WCHAR_MIN and WCHAR_MAX (see “stdint.h”), and the following macro:
The header wctype.h declares functions to classify and convert wide characters. These functions are analogous to those for byte characters declared in the header ctype.h. In addition, wctype.h declares extensible wide-character classification and conversion functions.
The types defined in wctype.h are wint_t (described in “wchar.h”) and the following two types:
wctrans_tThis is a scalar type to represent locale-specific mapping rules. You can obtain a value of this type by calling the wctrans() function, and use it as an argument to the function towctrans() to perform a locale-specific wide-character conversion.
wctype_tThis is a scalar type to represent locale-specific character categories. You can obtain a value of this type by calling the wctype() function, and pass it as an argument to the function iswctype() to determine whether a given wide character belongs to the given category.
The header wctype.h also defines the macro WEOF, described in “wchar.h”.
1 The C11 standard contradicts itself on this point. In describing the use of library functions, it says, “Any invocation of a library function that is implemented as a macro shall expand to code that evaluates each of its arguments exactly once, fully protected by parentheses where necessary, so it is generally safe to use arbitrary expressions as arguments,” but in its descriptions of the functions putc(), putwc(), getc(), and getwc(), the standard contains warnings like this one: “The putc function is equivalent to fputc, except that if it is implemented as a macro, it may evaluate stream more than once, so that argument should never be an expression with side effects.”