Standard Library Functions in C – Use it in Smart Way & Stand Alone in Crowd

Have you ever wondered why Library Functions in C possess great importance in programming? Clear your confusion, because we are going to justify the importance of Library Functions in C through this tutorial and will cover all the important aspects related to it. These concepts will help you in a tremendous way to enhance your programming skills.

In this tutorial, we will discuss:

Standard Library Functions in C

In order to acknowledge the potency of standard library functions, let us consider a situation where you want to simply display a statement without the use of standard output statements, like printf() or puts() or any other inbuilt display functions.

That would probably be a tedious task and an extensive knowledge of programming at the engineering level would be required to simply display a statement as your program output.

This is where the standard library functions in C come into play!

Before we move on it is required to be well acquainted with the skills of Functions in C

1. Standard Library Functions in C

Standard Library Functions are basically the inbuilt functions in the C compiler that makes things easy for the programmer.

As we have already discussed, every C program has at least one function, that is, the main() function. The main() function is also a standard library function in C since it is inbuilt and conveys a specific meaning to the C compiler.

2. Significance of Standard Library Functions in C

2.1 Usability

Standard library functions allow the programmer to use the pre-existing codes available in the C compiler without the need for the user to define his own code by deriving the logic to perform certain basic functions.

2.2 Flexibility

A wide variety of programs can be made by the programmer by making slight modifications while using the standard library functions in C.

2.3 User-friendly syntax

We have already discussed in Function in C tutorial that how easy it is to grasp and use the syntax of functions.

2.4 Optimization and Reliability

All the standard library functions in C have been tested multiple times in order to generate the optimal output with maximum efficiency making it reliable to use.

2.5 Time-saving

Instead of writing numerous lines of codes, these functions help the programmer to save time by simply using the pre-existing functions.

2.6 Portability

Standard library functions are available in the C compiler irrespective of the device you are working on. These functions connote the same meaning and hence serve the same purpose regardless of the operating system or programming environment.

3. Header Files in C

In order to access the standard library functions in C, certain header files need to be included before writing the body of the program.

Don’t move further, if you are not familiar with the Header Files in C.

Here is a tabular representation of a list of header files associated with some of the standard library functions in C:

HEADER FILEMEANINGELUCIDATION
<stdio.h>Standard input-output header
Used to perform input and output operations like scanf() and printf().
<string.h>String header
Used to perform string manipulation operations like strlen and strcpy.
<conio.h>Console input-output header
Used to perform console input and console output operations like clrscr() to clear the screen and getch() to get the character from the keyboard.
<stdlib.h>Standard library header
Used to perform standard utility functions like dynamic memory allocation using functions such as malloc() and calloc().
<math.h>Math header
Used to perform mathematical operations like sqrt() and pow() to obtain the square root and the power of a number respectively.
<ctype.h>Character type header
Used to perform character type functions like isaplha() and isdigit() to find whether the given character is an alphabet or a digit. respectively.
<time.h>Time header
Used to perform functions related to date and time like setdate() and getdate() to modify the system date and get the CPU time respectively.
<assert.h>Assertion header
Used in program assertion functions like assert() to get an integer data type as a parameter which prints stderr only if the parameter passed is 0.
<locale.h>Localization header
Used to perform localization functions like setlocale() and localeconv() to set locale and get locale conventions respectively.
<signal.h>Signal header
Used to perform signal handling functions like signal() and raise() to install signal handler and to raise the signal in the program respectively.
<setjmp.h>Jump header
Used to perform jump functions.
<stdarg.h>Standard argument header
Used to perform standard argument functions like va_start and va_arg() to indicate the start of the variable-length argument list and to fetch the arguments from the variable-length argument list in the program respectively.
<errno.h>Error handling header
Used to perform error handling operations like errno() to indicate errors in the program by initially assigning the value of this function to 0 and then later changing it to indicate errors.

Get a complete guide to learn Data types in C 

Let us discuss some of the commonly used Standard library functions in C in detail:

3.1 <stdio.h>

This is the basic header file used in almost every program written in the C language.
It stands for standard input and standard output used to perform input-output functions, some of which are:

  • printf()– Used to display output on the screen.
  • scanf()– To take input from the user.
  • getchar()– To return characters on the screen.
  • putchar()– To display output as a single character on the screen.
  • fgets()– To take a line as an input.
  • puts()– To display a line as an output.
  • fopen()– To open a file.
  • fclose()– To close a file.

Here is a simple program in C that illustrates the use of <stdio.h> to use scanf() and printf() functions:

#include<stdio.h> // Use of stdio.h header
int main()
{

char name[30];
char line[30];

printf("Enter the name: "); // Use of printf() function
scanf("%s", name); // Use of scanf() function
printf("The name is: %s\n", name);
return 0;
}

Code on Screen-

Using scanf and printf functions in C

Output-

output of scanf and printf functions in C

3.2 <string.h>

We have already discussed the various string manipulation functions in C in detail.

3.3 <stdlib.h>

Functions such as malloc(), calloc(), realloc() and free() can be used while dealing with dynamic memory allocation of variables.

Let us learn these 4 basic functions before using them in our program.

It should be clear that these functions are used for dynamic memory allocation of variables, that is in contrast to arrays that allocate memory in a static (fixed) manner.

3.3.1 malloc()

malloc() stands for memory allocation. This function is responsible for reserving a specific block of memory and returns a null pointer during the execution of the program.

Syntax-

pointer_name = (cast_type * ) malloc (no_of_bytes * size_in_bytes_of_cast_type)

For instance,

pointer = ( float* ) malloc ( 100 * sizeof ( float ) );

Here is a code in C which illustrates the use of malloc() function to find the sum of numbers entered by the user-

#include <stdio.h>
#include <stdlib.h> // Use of stdlib header
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");
int no_of_elements, iteration, *pointer, sum = 0;

printf("Enter number of elements: ");
scanf("%d", &no_of_elements);

pointer = (int*) malloc(no_of_elements * sizeof(int)); // Use of malloc() function

if(pointer == NULL)
{
printf("Sorry! Memory is not allocated.");
exit(0);
}
printf("Enter the elements: ");

/* Implementation of dynamic mememory allocation */

for(iteration = 0; iteration < no_of_elements; iteration++)
{
scanf("%d", pointer + iteration);
sum += *(pointer + iteration);
}

printf("The sum of the elements is = %d\n", sum);
return 0;
}

Code on Screen-

Example of malloc function in C

Output-

output of malloc function in C

Grab this Samurai Technique to Learn Arrays in C

3.3.2 calloc()

calloc stands for contiguous allocation. It is similar to malloc in all respects except the fact that it initializes the memory to 0 and has the ability to allocate numerous blocks of memory before the execution of the program.

Syntax-

pointer_name = (cast_type*) calloc (no_of_bytes, size_of_cast_type);

For instance,

pointer = ( int *) calloc (50, sizeof ( int ) );

Here is a code in C similar to malloc() that illustrates the use of calloc() function to find the sum of numbers entered by the user:

#include <stdio.h>
#include <stdlib.h> // Use of stdlib header
int main()
{
printf("Welcome to DataFlair tutorials!\n\n");
int no_of_elements, iteration, *pointer, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &no_of_elements);
pointer = (int*) calloc(no_of_elements, sizeof(int));
if(pointer == NULL)
{
printf("Sorry! Memory is not allocated.");
exit(0);
}
printf("Enter the elements: ");
/* Implementation of dynamic mememory allocation */
for(iteration = 0; iteration < no_of_elements; iteration++)
{
scanf("%d", pointer + iteration);
sum += *(pointer + iteration);
}
printf("The sum of the elements is = %d\n", sum);
return 0;
}

Code on Screen-

Example of calloc function in C

Output-

Output of calloc function in C

3.3.3 realloc()

realloc stands for reallocation. It is used to change the size of the previously allocated memory in case the previously allocated memory is insufficient to meet the required needs of the variable in C.

Syntax-

pointer_name = realloc(pointer_name, new_size);

For instance,

If initially,
pointer = ( int* ) malloc( 20 * sizeof( int ) );

Then, using realloc

pointer = realloc(pointer, 24 * sizeof( int ) );

Here is a code in C that illustrates the use of realloc() function:

#include <stdio.h>
#include <stdlib.h> // Use of stdlib header
int main()
{
printf("Welcome to DataFlair tutorials!\n\n");
int *pointer,*new_pointer, iteration;
pointer = (int *)malloc(2*sizeof(int));
*pointer = 5;
*(pointer + 1) = 10;
new_pointer = (int *)realloc(pointer, 3*sizeof(int));
*(new_pointer + 2) = 15;
printf("The elements are: ");
for(iteration = 0; iteration < 3; iteration++)
printf("%d\n ", *(new_pointer + iteration));
return 0;
}

Code on Screen-

Example of realloc function in C

Output-

Output of realloc function in C

3.3.4 free()

free is responsible to free the dynamically allocated memory done by malloc(), calloc() or realloc() to the system.

Syntax-

free( pointer_name );

For instance,

free( pointer );

Here is a code in C similar to malloc() that illustrates the use of free() function to find the sum of numbers entered by the user:

#include <stdio.h>
#include <stdlib.h> // Use of stdlib header
int main()
{
printf("Welcome to DataFlair tutorials!\n\n");
int no_of_elements, iteration, *pointer, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &no_of_elements);
pointer = (int*) malloc(no_of_elements * sizeof(int)); // Use of malloc() function
if(pointer == NULL)
{
printf("Sorry! Memory is not allocated.");
exit(0);
}
printf("Enter the elements: ");
/* Implementation of dynamic memmory allocation */
for(iteration = 0; iteration < no_of_elements; iteration++)
{
scanf("%d", pointer + iteration);
sum += *(pointer + iteration);
}
printf("The sum of the elements is = %d\n", sum);
free(pointer); // Use of free() function
return 0;
}

Code on Screen-

Example of free function in C

Output-

Free function in C with Example

3.4 <math.h>

The math header is of great significance as it is used to perform various mathematical operations such as:

  • sqrt() – This function is used to find the square root of a number
  • pow() – This function is used to find the power raised to that number.
  • fabs() – This function is used to find the absolute value of a number.
  • log() – This function is used to find the logarithm of a number.
  • sin() – This function is used to find the sine value of a number.
  • cos() – This function is used to find the cosine value of a number.
  • tan() – This function is used to find the tangent value of a number.

Here is a code in C that illustrates the use of some of the basic <math.h> functions:

#include <stdio.h>
#include <math.h>
int main()
{
printf("Welcome to DataFlair tutorials!\n\n");
double number=5, square_root;
int base = 6, power = 3, power_result;
int integer = -7, integer_result;
square_root = sqrt(number);
printf("The square root of %lf is: %lf\n", number, square_root);
power_result = pow(base,power);
printf("%d raised to the power %d is: %d\n", base, power, power_result);
integer_result = fabs(integer);
printf("The absolute value of %d is: %d\n", integer, integer_result);
return 0;
}

Code on Screen-

Example of math.h function in C

Output-

Output of math.h function in C

3.5 <ctype.h>

This function is popularly used when it comes to character handling.

Some of the functions associated with <ctype.h> are:

  • isalpha() – Used to check if the character is an alphabet or not.
  • isdigit() – Used to check if the character is a digit or not.
  • isalnum() – Used to check if the character is alphanumeric or not.
  • isupper() – Used to check if the character is in uppercase or not
  • islower() – Used to check if the character is in lowercase or not.
  • toupper() – Used to convert the character into uppercase.
  • tolower() – Used to convert the character into lowercase.
  • iscntrl() – Used to check if the character is a control character or not.
  • isgraph() – Used to check if the character is a graphic character or not.
  • isprint() – Used to check if the character is a printable character or not
  • ispunct() – Used to check if the character is a punctuation mark or not.
  • isspace() – Used to check if the character is a white-space character or not.
  • isxdigit() – Used to check if the character is hexadecimal or not.

<ctype.h> is not supported in Linux but it works fairly well in Microsoft Windows.

3.6 <conio.h>

It is used to perform console input and console output operations like clrscr() to clear the screen and getch() to get the character from the keyboard.

Note: The header file <conio.h> is not supported in Linux. It works fairly well on Microsoft Windows.

4. Summary

In this tutorial, we discussed the meaning of Standard library functions in C. We covered each and every aspect associated with it such that the reader understands it in the best way. Then, we discussed the significance of standard library functions in detail. Thereafter, we discussed the various header files needed to access the standard library functions with the help of illustrative C programs. It is safe to proclaim that after completing this tutorial, we have successfully conquered the core concepts of C programming by getting a firm grip on Standard library functions.

Let’s know the reasons for the popularity of Binary tree in C

Do leave a comment in the comment sections below to let us know how much you enjoyed this tutorial. If you have any queries regarding this article, feel free to ask our experts who are always there to clear your doubts.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.