Most useful standard library functions in C language

Most useful standard library functions in C language

November 2, 2017
3 min read
1272 views
0

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.


Was this article helpful?Vote to let the author know
0
Share this article

Loading comments...

Related Articles

Cloud Hosting India: Which Provider is the Best? Complete Guide

Cloud Hosting India: Which Provider is the Best? Complete Guide

Cloud hosting uses a network of interconnected servers rather than relying on a single physical machine. Your website or application runs on virtualized infrastructure, allowing resources such as CPU, RAM, storage, and networking to be allocated according to workload requirements

8 views
Read More
Read full article: Cloud Hosting India: Which Provider is the Best? Complete Guide
How to Connect a Headless CMS to Your React App

How to Connect a Headless CMS to Your React App

React remains the most-used JavaScript framework, with the State of JS 2025 survey putting adoption at 83.6%. A growing share of that base pairs React with a react js cms rather than hardcoding content.

123 views
Read More
Read full article: How to Connect a Headless CMS to Your React App