Standard C Libraries - stdlib Utility Functions. More...
Go to the source code of this file.
Data Structures | |
struct | div_t |
struct | ldiv_t |
Macros | |
#define | size_t size_t |
#define | wchar_t |
#define | EXIT_FAILURE |
#define | EXIT_SUCCESS |
#define | MB_CUR_MAX |
#define | NULL (0) |
#define | RAND_MAX |
Functions | |
void | abort (void) |
int | abs (int i) |
int | atexit (void(*func)(void)) |
double | atof (const char *s) |
int | atoi (const char *s) |
long | atol (const char *s) |
void * | bsearch (const void *key, const void *base, size_t nelem, size_t size, int *cmp, const void *ck, const void *ce) |
void * | calloc (size_t nelem, size_t size) |
div_t | div (int numer, int denom) |
void | exit (int status) |
void | free (void *ptr) |
char * | getenv (const char *name) |
long | labs (long i) |
ldiv_t | ldiv (long numer, long denom) |
void * | malloc (size_t size) |
int | mblen (const char *s, size_t n) |
Variables | |
size_t mbstowcs * | wcs |
int mbtowc * | pwc |
Standard C Libraries - stdlib Utility Functions.
The header file, stdlib.h, consists of types, macros and functions that provide text conversions, memory management, searching and sorting abilities, and other general utilities.
The documentation in this header file has been copied from the documentation provided with the Microchip MPLAB XC16 compiler. The original lincense agreement included with the XC16 compiler applies!
#define EXIT_FAILURE |
Description: Reports unsuccessful termination.
Include: <stdlib.h>
Remarks:
EXIT_FAILURE is a value for the exit function to return an unsuccessful termination status.
Example:
See exit for example of use.
#define EXIT_SUCCESS |
Description: Reports successful termination.
Include: <stdlib.h>
Remarks:
EXIT_SUCCESS is a value for the exit function to return a successful termination status.
Example:
See exit for example of use.
#define MB_CUR_MAX |
#define NULL (0) |
Description: The value of a null pointer constant.
Include: <stdlib.h>
#define RAND_MAX |
Description: Maximum value capable of being returned by the rand function.
Include: <stdlib.h>
Value: 32767
Description: The type of the result of the sizeof operator.
Include: <stdlib.h>
void abort | ( | void | ) |
Description: Aborts the current process.
Include: <stdlib.h>
Remarks:
abort will cause the processor to reset.
Example:
Output:
Cannot open samp.fil
ABRT
int abs | ( | int | i) |
Description: Calculates the absolute value.
Include: <stdlib.h>
i | integer value |
Remarks:
A negative number is returned as positive; a positive number is unchanged.
Example:
Output:
The absolute value of 12 is 12
The absolute value of -2 is 2
The absolute value of 0 is 0
int atexit | ( | void(*)(void) | func) |
Description: Registers the specified function to be called when the program terminates normally.
Include: <stdlib.h>
func | function to be called |
Remarks:
For the registered functions to be called, the program must terminate with the exit function call.
Example:
Input:
With contents of UartIn.txt (used as stdin input for simulator): 5 Output:
Enter your favorite number: 5
Good Choice
That's an excellent number
Now go count something
Input:
With contents of UartIn.txt (used as stdin input for simulator):
42
Output:
Enter your favorite number: 42
42!?
That's an awful number
Now go count something
double atof | ( | const char * | s) |
Description: Converts a string to a double precision floating-point value.
Include: <stdlib.h>
s | pointer to the string to be converted |
Remarks:
The number may consist of the following:
[whitespace] [sign] digits [.digits]
[ { e | E }[sign]digits]
optional whitespace, followed by an optional sign then a sequence of one or more digits with an optional decimal point, followed by one or more optional digits and an optional e or E followed by an optional signed exponent. The conversion stops when the first unrecognized character is reached. The conversion is the same as strtod(s,0) except it does no error checking so errno will not be set.
Example:
Output:
String = "1.28" float = 1.280000
String = "27.835:e2" float = 2783.500000
String = "Number1" float = 0.000000
int atoi | ( | const char * | s) |
Description: Converts a string to an integer.
Include: <stdlib.h>
s | string to be converted |
Remarks:
The number may consist of the following:
[whitespace] [sign] digits
optional whitespace, followed by an optional sign then a sequence of one or more digits. The conversion stops when the first unrecognized character is reached. The conversion is equivalent to (int) strtol(s,0,10) except it does no error checking so errno will not be set.
Example:
Output:
String = " -127" int = -127
String = "Number1" int = 0
long atol | ( | const char * | s) |
Description: Converts a string to a long integer.
Include: <stdlib.h>
s | string to be converted |
Remarks:
The number may consist of the following:
[whitespace] [sign] digits
optional whitespace, followed by an optional sign then a sequence of one or more digits. The conversion stops when the first unrecognized character is reached. The conversion is equivalent to (int) strtol(s,0,10) except it does no error checking so errno will not be set.
Example:
Output:
String = " -123456" int = -123456
String = "2Number" int = 2
void* bsearch | ( | const void * | key, |
const void * | base, | ||
size_t | nelem, | ||
size_t | size, | ||
int * | cmp, | ||
const void * | ck, | ||
const void * | ce | ||
) |
Description: Performs a binary search.
Include: <stdlib.h>
key | object to search for |
base | pointer to the start of the search data |
nelem | number of elements |
size | size of elements |
cmp | pointer to the comparison function |
ck | pointer to the key for the search |
ce | pointer to the element being compared with the key. |
Remarks:
The value returned by the compare function is <0 if ck is less than ce, 0 if ck is equal to ce or >0 if ck is greater than ce. In the following example, qsort is used to sort the list before bsearch is called. bsearch requires the list to be sorted according to the comparison function. This comp uses ascending order.
Example:
Output:
Sorted List: 16 25 35 47 52 63 93
The value 25 was found
The value 75 was not found
Description: Allocates an array in memory and initializes the elements to 0.
Include: <stdlib.h>
nelem | number of elements |
size | length of each element |
Remarks:
Memory returned by calloc is aligned correctly for any size data element and is initialized to zero.
Example:
Output:
i[0] = 0
i[1] = 0
i[2] = 0
i[3] = 0
i[4] = 0
div_t div | ( | int | numer, |
int | denom | ||
) |
Description: Calculates the quotient and remainder of two numbers.
Include: <stdlib.h>
numer | numerator |
denom | denominator |
Remarks:
The returned quotient will have the same sign as the numerator divided by the denominator. The sign for the remainder will be such that the quotient times the denominator plus the remainder will equal the numerator (quot * denom + rem = numer). Division by zero will invoke the math exception error, which by default, will cause a Reset. Write a math error handler to do something else.
Example:
Output:
For div(7, 3)
The quotient is 2 and the remainder is 1
For div(7, -3)
The quotient is -2 and the remainder is 1
For div(-5, 3)
The quotient is -1 and the remainder is -2
For div(7, 7)
The quotient is 1 and the remainder is 0
For div(7, 0)
Illegal instruction executed
ABRT
void exit | ( | int | status) |
Description: Terminates program after clean up.
Include: <stdlib.h>
status | exit status |
Remarks:
exit calls any functions registered by atexit in reverse order of registration, flushes buffers, closes stream, closes any temporary files created with tmpfile, and resets the processor. This function is customizable. See pic30-libs.
Example:
Output:
Cannot open samp.fil
void free | ( | void * | ptr) |
Description: Frees memory.
Include: <stdlib.h>
ptr | points to memory to be freed |
Remarks:
Frees memory previously allocated with calloc, malloc, or realloc. If free is used on space that has already been deallocated (by a previous call to free or by realloc) or on space not allocated with calloc, malloc, or realloc, the behavior is undefined.
Example:
Output:
Memory allocated
Memory freed
char* getenv | ( | const char * | name) |
Description: Get a value for an environment variable.
Include: <stdlib.h>
name | name of environment variable |
Remarks:
This function must be customized to be used as described (see pic30-libs). By default there are no entries in the environment list for getenv to find.
Example:
Output:
Cannot find environment variable INCLUDE
long labs | ( | long | i) |
Description: Calculates the absolute value of a long integer.
Include: <stdlib.h>
i | long integer value |
Remarks:
A negative number is returned as positive; a positive number is unchanged.
Example:
Output:
The absolute value of 123456 is 123456
The absolute value of -246834 is 246834
The absolute value of 0 is 0
ldiv_t ldiv | ( | long | numer, |
long | denom | ||
) |
Description: Calculates the quotient and remainder of two long integers.
Include: <stdlib.h>
numer | numerator |
denom | denominator |
Remarks:
The returned quotient will have the same sign as the numerator divided by the denominator. The sign for the remainder will be such that the quotient times the denominator plus the remainder will equal the numerator (quot * denom + rem = numer). If the denominator is zero, the behavior is undefined.
Example:
Output:
For ldiv(7, 3)
The quotient is 2 and the remainder is 1
For ldiv(7, -3)
The quotient is -2 and the remainder is 1
For ldiv(-5, 3)
The quotient is -1 and the remainder is -2
For ldiv(7, 7)
The quotient is 1 and the remainder is 0
For ldiv(7, 0)
The quotient is -1 and the remainder is 7
Explanation:
In the last example (ldiv(7,0)) the denominator is zero, the behavior is undefined.
void* malloc | ( | size_t | size) |
Description: Allocates memory.
Include: <stdlib.h>
size | number of characters to allocate |
Remarks:
malloc does not initialize memory it returns.
Example:
Output:
Memory allocated
Memory freed
int mblen | ( | const char * | s, |
size_t | n | ||
) |
Description: Gets the length of a multibyte character. (See Remarks.)
Include: <stdlib.h>
s | points to the multibyte character |
n | number of bytes to check |
Remarks:
The 16-bit compiler does not support multibyte characters with length greater than 1 byte.
int mbtowc * pwc |
Description: Converts a multibyte character to a wide character. (See Remarks.)
Include: <stdlib.h>
pwc | points to the wide character |
s | points to the multibyte character |
n | number of bytes to check |
Remarks:
The resulting wide character will be stored at pwc. The 16-bit compiler does not support multibyte characters with length greater than 1 byte.
size_t mbstowcs * wcs |
Description: Converts a multibyte string to a wide character string. (See Remarks.)
Include: <stdlib.h>
wcs | points to the wide character string |
s | points to the multibyte string |
n | the number of wide characters to convert. |
Remarks:
mbstowcs converts n number of wide characters unless it encounters a null wide character first. The 16-bit compiler does not support multibyte characters with length greater than 1 byte.