Thu Nov 02 2017

Most useful standard library functions in C language

Programming1221 views
Most useful standard library functions in C language

Standard library functions are the biggest strength of the C language. Library functions are one of the reason behind the huge popularity of C language. There are many library functions are available in C that help you write a good and efficient program. In this geek story we are going to discuss about 10 most useful standard library functions.

So, let's start -

int strcmp(char *str1, char *str2)

strcmp() function belongs to <string.h>, which used to compare the contents of two strings (str1 and str2) character by character. If the first character of both strings is equal, then move to the next character of the two strings and compare them. This comparison continues until the corresponding characters of two strings are different or a null character '\0' is reached. When comparison completed it return the result 0 if both are identical, or a integer of the difference between character corresponding ASCII value.

int isdigit(int val)

isdigit() function belongs to <ctype.h>, which used to check the given argument is a digit or not. Internally, the character is converted to its ASCII value for the checking and returns non-0 if argument is digit 0 to 9.

int isgraph(int val)

isgraph() function is also belongs to <ctype.h>, which used to check whether a character is a graphic character or not. The function takes a single argument and if the argument passed is a graphic character, then it returns a non-zero integer. Else, it returns 0.

malloc(byte-size)

malloc() function belongs to <stdlib.h>, which is known for reserving a block of memory of specified size dynamically. The function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer.

calloc(int num, int elem_size)

calloc() function is also a part of <stdlib.h>. Similar like malloc(), It also used to reserve a block of memory dynamically, but the only difference is that, malloc() allocates a single block of memory, whereas calloc() allocates multiple blocks of memory each of the same size and sets all bits to zero.

free(ptr)

Dynamically allocated memory by calloc() or malloc() doesn't get freed by itself. free() function is used to free the dynamically reserved space.

int rand(void)

<stdlib.h> function rand() is used in C to generate random numbers. If we generate a sequence of random number using rand() function, it will create the same sequence again and again every time the program runs.

void srand(unsigned int)

srand() work with rand() function to sets the starting point for producing a series of pseudo-random integers. It placed before the rand() function calls or at the beginning of the program.

void perror(const char *str)

perror() function belongs to <stdio.h>, which used to prints a descriptive error message to stderr. The error message in str is printed first, followed by a colon and the implementation-defined message that describes the most recent error.

clock()

clock() function belongs to <time.h>, which calculate time taken by a process. This function returns the number of clock ticks elapsed since the start of the program. We can call the function at the beginning and end of the program for which we measure time by subtracting the values.


We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.