Miscellaneous Functions

Miscellaneous Functions

Overview

The standard function library defines several utility functions. They include various conversions, variable-length argument processing, sorting and searching, and random number generation. Many of the functions covered here require the use of the C header <stdlib.h> or its C++ equivalent <cstdlib>. This chapter will use the C header name, but references to <stdlib.h> also apply to <cstdlib>.


In <stdlib.h> are declared the types div_t and ldiv_t, which are the types of the values returned by div( ) and ldiv( ), respectively. C99 adds the lldiv_t type and the lldiv( ) function. Also declared are the types size_t and wchar_t. The following macros are also defined:
Macro
Meaning
MB_CUR_MAX
Maximum length (in bytes) of a multibyte character
NULL
A null pointer
RAND_MAX
The maximum value that can be returned by the rand( ) function
EXIT_FAILURE
The value returned to the calling process when a program terminates because of an abnormal condition
EXIT_SUCCESS
The value returned to the calling process when a program terminates normal
 If a function requires a header other than <stdlib.h>, that function description will 
discuss it.

abort

#include <stdlib.h>void abort(void); 
The abort( ) function causes immediate abnormal termination of a program. Generally, no files are flushed. In environments that support it, abort( ) will return an implementation-defined value to the calling process (usually the operating system) indicating failure. Related functions are exit( ) and atexit( ).

abs

#include <stdlib.h>int abs(int num);
The abs( ) function returns the absolute value 
of num.
  A related function is fabs( ).

assert

#include <assert.h>void assert(int exp); 
The assert( ) macro, defined in the headers <assert.h> and <cassert>, writes error information to stderr and then aborts program execution if the expression exp evaluates to zero. Otherwise, assert( ) does nothing. Although the exact output is implementation defined, many compilers use a message similar to this:
Assertion failed: <expression>, file <file>, line <linenum>For C99, the message will also include the name of the function that contained assert( ).The assert( ) macro is generally used to help verify that a program is operating correctly, with the expression being devised in such a way that it evaluates to true only when no errors have taken place.It is not necessary to remove the assert( ) statements from the source code once a program is debugged because if the macro NDEBUG is defined (as anything), the assert( ) macros will be ignored.
A related function is abort( ).

atexit

#include <stdlib.h>int atexit(void (*func)(void)); 
The atexit( ) function causes the function pointed to by func to be called upon normal program termination. The atexit( ) function returns zero if the function is successfully registered as a termination function and nonzero otherwise.
At least 32 termination functions can be registered, and they will be called in the reverse order of their registration.
Related functions are exit( ) and abort( ).

atof

#include <stdlib.h>double atof(const char *str);
The atof( ) function converts the string pointed to by str into a double value. The string must contain a valid floating-point number. If this is not the case, the returned value is undefined.
The number can be terminated by any character that cannot be part of a valid floating-point number. This includes whitespace, punctuation (other than periods), and characters other than E or e. This means that if atof( ) is called with “100.00HELLO”, the value 100.00 will be returned.
Related functions are atoi( ) and atol( ).

atoi

#include <stdlib.h>int atoi(const char *str); 
The atoi( ) function converts the string pointed to by str into an int value. The string must contain a valid integer number. If this is not the case, the returned value is undefined.
The number can be terminated by any character that cannot be part of an integer number. This includes whitespace, punctuation, and characters. This means that if atoi( ) is called with “123.23”, the integer value 123 will be returned, and the “.23” is ignored.
Related functions are atof( ) and atol( ).

atol

#include <stdlib.h>long int atol(const char *str);
The atol( ) function converts the string pointed to by str into a long int value. The string must contain a valid integer number. If this is not the case, the returned value is undefined.
 The number can be terminated by any character that cannot be part of an integer number. This includes whitespace, punctuation, and characters. This means that if atol( ) is called with “123.23”, the long integer value 123L will be returned, and the “.23” is ignored.
 Related functions are atof( ), atoi( ), and atoll( ).

Followers