Every C compiler provides the standard library functions. The following table describes few C library functions that can be used by programmers’ in their programs. Since standard library functions are huge in numbers, it is not possible to include all of them in this appendix. Programmer can refer to the help of the C compiler. Functions supported by compilers may vary from compiler to compiler.
Header files and functions supported by a compiler are as under.
Function Name & Syntax | Description | Example |
---|---|---|
malloc: void *malloc(size_t size); |
It allocates a block of size bytes from the memory heap. It allows a program to allocate memory explicitly as it’s needed, and in the exact amounts needed. On success, returns pointer to newly allocated block of memory; otherwise null. | char *str=(char *) malloc(10); Allocates space for 10 chracters pointed by str. Typecasting is needed here. |
calloc: void *calloc(size_t nitems, size_t size); |
calloc provides access to the C memory heap, which is available for dynamic allocation of variable-sized blocks of memory. calloc allocates a block (nitems * size) bytes and clears it to 0. On success, returns pointer to newly allocated block of memory; otherwise null. | int *str=(int *) malloc(10, sizeof(int)); Allocates space for 10 chracters as 10*sizeof(int)=10*2=20 bytes pointed by str. Typecasting is also needed here. |
free: void free(void *block); |
free deallocates a memory block allocated by a previous call to calloc, malloc, or realloc. | free(str); Frees the allocated block pointed by str. |
Function Name & Syntax | Description | Example |
---|---|---|
atoi: int atoi(const char *s); |
It converts a string pointed to by s to int. If s is inconvertible, it returns zero. | char *s="123"atoi(s); returns an integer 123. |
exit: void exit(int status); |
It terminates the calling process. Before termination, it closes all files and writes buffered output. status=0 indicates normal exit and non-zero indicates error.This function is also available in process.h file. | exit(0); stops the execution of main function hence terminates the program. |
itoa: char *itoa(int value, char *string, int radix); |
It converts value to a null-terminated string and stores the result in string. radix specifies the base to be used in converting value (2to36). | itoa(123,a,16); The hexadecimal string format of 123 i.e. 7b will be stored in string a. |
random: int random(int num); |
It returns a random number between 0 and (num-1). | random(999); may return 10 |
13.59.108.176