Previous Table of Contents Next


3.10.4.5. Interaction with the Environment

getenv

char *getenv(const char *name)

getenv searches an implementation-defined set of environment variables for one with the given name and returns its value as a string. If no such variable can be found, getenv returns a null pointer.

exit

void exit(int status)

A call to exit requests normal termination of a program. Functions registered by atexit are called, open streams are flushed and closed, and the supplied status is returned to the operating system or other caller as the exit status of the program, just as if that same status had been returned from the initial invocation of main (see section 3.9.2). The two defined exit status values are EXIT_SUCCESS and EXIT_FAILURE. By convention, a value of 0 is also recognized as successful. The interpretation of other exit status values is implementation defined. By definition, the exit function does not return.

atexit

int atexit(void (*func)(void))

atexit arranges that the function pointed to by func be called at normal program exit (after return from main or after a call to exit). atexit returns 0 if the function is successfully registered for execution and nonzero otherwise (which should only happen if the limit of 32 registered functions is exceeded). If several functions are registered, they are called in the reverse order of their registration.

system

int system(const char *s)

system presents the string s to the underlying system’s command processor for execution. The results are obviously entirely system-dependent, but the intent is that the command string be executed, and that its exit status be returned as system’s return value.

abort

void abort(void)

The abort function requests immediate, unsuccessful termination of the program, as if by calling raise(SIGABRT) (see section 3.10.7.2).

3.10.4.6. Integer Arithmetic Functions

abs, labs

int abs(int x)  long int abs(long int x)

Both functions return the absolute value of their arguments.

div, ldiv

div_t div(int num, int denom)
ldiv_t div(long int num, long int denom)

These functions perform division, with two advantages over the built-in / and % operators. First, both a quotient and remainder are computed at once, in one operation. Second, the results are well defined if the numerator or denominator is negative: The quotient is rounded toward 0, and the remainder is therefore negative if num and denom differ in sign.

div’s return value is of type

typedef struct
       {
       int quot, rem;
        } div_t;

(although the members may be in either order). ldiv’s return type is similar, with both members having type long int.

3.10.4.7. Multibyte Character and String Functions

These functions all manipulate multibyte character sequences, which are implementation-defined (and potentially locale-specific) ways of encoding strings of wide characters (that is, arrays of elements of type wchar_t) as sequences of bytes, so that they can be manipulated as ordinary strings. Multibyte character strings are useful because they can save space, and because they can be defined (again, in a locale-specific way) so as to be compatible with existing schemes for encoding multinational characters (e.g., in text files and the like). The support for wide characters and multibyte character sequences in <stdlib.h> is limited; see section 3.10.17 for a description of the expanded functionality defined by NA1.

mblen

int mblen(const char *s, size_t n)

mblen counts the number of characters in the string pointed to by s that form a valid multibyte character and returns that number. At most, n bytes pointed to by s are examined.

mbtowc, wctomb

int mbtowc(wchar_t *dest, const char *src, size_t n)
int wctomb(char *dest, wchar_t src)

These functions convert multibyte character sequences representing single wide characters to and from individual values of type wchar_t. mbtowc examines at most n bytes in the string pointed to by src and converts them, if possible, to a wide character that it stores in *dest, returning the number of characters read from src if this conversion was possible and -1 otherwise. wctomb converts the single wide character value src to a multibyte character sequence that it writes to dest, returning the number of bytes written. (This number will never be greater than MB_CUR_MAX, a constant defined in <stdlib.h>.)

mbstowcs, wcstombs

size_t mbstowcs(wchar_t *dest, const char *src, size_t n)
size_t wcstombs(char *dest, wchar_t src)

These functions convert multibyte character sequences representing multiple wide characters to and from strings of type wchar_t *. mbstowcs converts a multibyte character sequence pointed to by src into wide characters that it stores into dest, writing no more than n wide characters. wcstombs performs the opposite conversion, reading wide characters from src and building a multibyte character sequence in dest (but without writing more than n characters). Both functions stop early if a null character is encountered. In both cases, the return value is the number of items (wide characters or characters, respectively, exclusive of the null character) written to dest.

3.10.5. <math.h>

The header <math.h> defines the usual collection of mathematical functions. For historical reasons, on some systems, the math functions must be explicitly requested at link time (on UNIX systems by including -lm at the end of the command line).

3.10.5.1. Square Root, Exponent, and Logarithmic Functions

double sqrt(double x)
double pow(double x, double y)
double exp(double x)
double log(double x)
double log10(double x)

pow computes x raised to the power y, although it is often implemented using logarithmic identities, and so may not give the expected result if x and y are both integers. exp(x) computes e to the power x. log computes the natural (base e) logarithm; log10 computes the base 10 logarithm.


Previous Table of Contents Next