Open In App

What’s difference between header files "stdio.h" and "stdlib.h" ?

Last Updated : 02 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C programming, standard header files provide various inbuilt functionalities and two of the most commonly used standard header files are stdio.h and stdlib.h. The <stdio.h> provides Standard Input Output tools such as printf(), scanf(), etc while <stdlib.h> provides some commonly used utility tools malloc(), calloc(), etc.

The primary differences between these two header files are listed in the below table:

Aspect

stdio.h

stdlib.h

Purpose

Focuses on input and output operations (reading and writing to console and files).Provides functions for memory allocation, process control, random number generation, and conversion between data types.

Functions Provided

Includes functions like printf(), scanf(), getchar(), and file I/O functions.Includes functions like malloc(), free(), exit(), atoi(), and rand().

Scope

Primarily deals with handling input and output.Deals with system-level utilities, memory management, and other miscellaneous operations.

Usage

stdio.h is used by almost every C program.stdlib.h is only use when we need to allocate memory in our program.

What is stdio.h?

The stdio.h header file stands for Standard Input Output and provides functions for input and output operations. It includes functions to perform tasks like reading from and writing to the console, handling files, and managing formatted input/output.

Functions provided by stdio.h:

CategoryFunctionDescription
Console I/Oprintf()Prints formatted output to the standard output (console).
scanf()Reads formatted input from the standard input (console).
putchar()Writes a single character to the standard output (console).
getchar()Reads a single character from the standard input (console).
puts()Writes a string followed by a newline to the standard output (console).
gets()Reads a string from the standard input (deprecated due to lack of safety).
File Handlingfopen()Opens a file.
fclose()Closes a file.
fread()Reads data from a file.
fwrite()Writes data to a file.
fseek()Moves the file pointer to a specific position in a file.
ftell()Returns the current position of the file pointer.
rewind()Resets the file pointer to the beginning of the file.
fprintf()Writes formatted output to a file.
fscanf()Reads formatted input from a file.
fgetc()Reads a single character from a file.
fputc()Writes a single character to a file.
fgets()Reads a line of text from a file.
fputs()Writes a string to a file.
feof()Checks if the end-of-file indicator is set for a file.
ferror()Checks for a file error.
Temporary Filestmpfile()Creates a temporary file that is automatically deleted when closed.
tmpnam()Generates a unique temporary file name.
Buffer Managementsetbuf()Sets the buffer for a file stream.
setvbuf()Sets the buffer and mode for a file stream.
Error Handlingperror()Prints an error message describing the last error.
clearerr()Clears the error and end-of-file indicators for a file.

What is stdlib.h?

The stdlib.h header file stands for Standard Library and provides functions for memory allocation, process control, conversions, and other utilities that are not related to input/output. It contains functions for dynamically allocating memory, generating random numbers, and controlling program execution.

Functions provided by stdlib.h:

CategoryFunctionDescription
Memory Managementmalloc()Allocates a block of memory.
calloc()Allocates and initializes a block of memory.
realloc()Resizes a previously allocated block of memory.
free()Deallocates previously allocated memory.
Random Number Generationrand()Generates a pseudo-random number.
srand()Seeds the random number generator with a specific value.
Process Controlexit()Terminates the program immediately.
abort()Abnormally terminates the program.
atexit()Registers a function to be executed upon program termination.
system()Executes a system command.
Type Conversionatoi()Converts a string to an integer.
atol()Converts a string to a long integer.
atof()Converts a string to a floating-point number.
strtol()Converts a string to a long integer with error checking.
strtoll()Converts a string to a long long integer with error checking.
strtoul()Converts a string to an unsigned long integer with error checking.
strtoull()Converts a string to an unsigned long long integer with error checking.
strtod()Converts a string to a double with error checking.
strtof()Converts a string to a float with error checking.
strtold()Converts a string to a long double with error checking.
Searching and Sortingqsort()Performs a quick sort on an array.
bsearch()Performs a binary search in a sorted array.
Mathematical Utilitiesabs()Returns the absolute value of an integer.
labs()Returns the absolute value of a long integer.
llabs()Returns the absolute value of a long long integer.
div()Computes quotient and remainder of integer division.
ldiv()Computes quotient and remainder of long integer division.
lldiv()Computes quotient and remainder of long long integer division.
Environment Handlinggetenv()Retrieves the value of an environment variable.
putenv()Adds or changes an environment variable.
setenv()Sets a new environment variable.
unsetenv()Removes an environment variable.
Multibyte and Wide-Character Conversionmblen()Returns the number of bytes in the next multibyte character.
mbstowcs()Converts a multibyte string to a wide-character string.
mbtowc()Converts a multibyte character to a wide character.
wcstombs()Converts a wide-character string to a multibyte string.
wctomb()Converts a wide character to a multibyte character.
Dynamic Allocation for Arrayaligned_alloc()Allocates memory with a specified alignment.

Conclusion

In summary, stdio.h and stdlib.h are both essential header files in C programming but serve different purposes. stdio.h is focused on input/output operations, including console and file handling, while stdlib.h is used for system-level tasks like memory management, program control, and number conversion. Understanding when and how to use these header files will significantly improve your C programming skills.


Next Article

Similar Reads