Skip to main content

Library Header Files in C

Library Header files

  • Library header files are header files that provide declarations for the functions, variables, and other constructs defined in a particular library.

  • A library is a collection of precompiled object code that can be linked with a C program to provide additional functionality.

Library Header File Examples

  • stdio.h: Provides declarations for input and output functions like printf() and scanf().
  • stdlib.h: Provides declarations for memory allocation functions like malloc() and free().
  • math.h: Provides declarations for mathematical functions like sin() and sqrt().
  • string.h: Provides declarations for string manipulation functions like strlen() and strcat().

Different Libraries in C

Standard C Library (libc)Provides basic functionality such as input/output, string manipulation, memory management, and mathematical operations.
Math Library (libm)Provides mathematical functions such as trigonometric functions, exponential functions, logarithmic functions, and more.
Input/Output Library (stdio)Provides functions for input and output operations such as reading from and writing to files, as well as functions for interacting with the console.
String Library (string.h)Provides functions for working with strings, such as copying, concatenating, and comparing strings.
Standard Template Library (STL)A collection of data structures and algorithms, such as vectors, linked lists, and sorting algorithms.
Graphics Libraries (OpenGL, DirectX)Provides functions for drawing 2D and 3D graphics.
Database Libraries (SQLite, MySQL)Provides functions for working with databases.

Standard C Library

The standard C library (libc) is a collection of functions and macros that provide basic functionality such as input/output, string manipulation, memory management, and mathematical operations.

Standard C Library functions

Following table summarizes commonly used Standard C Library Function:

CategoryDescriptionFunctions
Input/OutputFunctions for input and output operations.printf, scanf, fprintf, fscanf, fgets, fputc, fgetc
String manipulationFunctions for working with strings.strlen, strcpy, strcat, strcmp, strchr, strstr
MathFunctions for mathematical operations.sin, cos, sqrt, pow, ceil, floor
MemoryFunctions for dynamic memory allocation and manipulation.malloc, calloc, realloc, free, memset, memcpy, memcmp
Date and TimeFunctions for working with dates and times.time, asctime, ctime, localtime, mktime
File handlingFunctions for working with files.fopen, fclose, fread, fwrite, fseek, ftell
MiscellaneousFunctions that do not fit into other categories.exit, rand, srand, system, getchar, putchar

Example

Simple example that shows the use of a printf function from the Standard C Library:

#include <stdio.h> // include the standard input/output header file

int main() {
char name[] = "Skillupwards";
printf("Hello, %s!\n", name);
return 0;
}
Output:

Math Library

The math library (libm) provides mathematical functions such as trigonometric functions, exponential functions, logarithmic functions, and more.

Math Library Functions

Here are some commonly used functions from the Math Library:

CategoryFunctions
Trigonometry sin, cos, tan, asin, acos, atan, atan2
Exponents and Logarithmsexp, exp2, expm1, log, log10, log1p, log2, pow
Square Roots and Powerssqrt, cbrt, hypot, fabs, ceil, floor, round, trunc
Random Numbers rand, srand

Example

#include <stdio.h>
#include <math.h>

int main() {
double radius = 5.0, area;

area = M_PI * pow(radius, 2);

printf("The area of the circle with radius %lf is: %lf", radius, area);

return 0;
}

Input/Output Library

The Input/Output Library (stdio) provides functions for input and output operations such as reading from and writing to files, as well as functions for interacting with the console.

Input/Output Library Functions

Below table lists down three categories of Input/Output Library functions:

CategoryDescriptionFunctions
Standard Input/Output FunctionsUsed for performing input/output operations on the standard input/output streamsprintf, scanf, puts, gets, fopen, fclose, fgetc, fgets, fputc, fputs, fprintf, fscanf
File Input/Output FunctionsUsed for performing input/output operations on filesfopen, fclose, fgetc, fgets, fputc, fputs, fprintf, fscanf, fread, fwrite
Formatted Output FunctionsUsed for formatting and printing dataprintf, fprintf, sprintf, snprintf

Example

#include <stdio.h>

int main() {
char name[] = "Skillupwards";
int age = 30;

printf("Hello, %s! You are %d years old.\n", name, age);

return 0;
}
Output:

Explanation:

  • We have assigned fixed values to the name and age variables. We then use printf to print a message that includes these values.

String Library

The string library (string.h) provides functions for working with strings, such as copying, concatenating, and comparing strings.

String Library Functions

CategoryDescriptionFunctions
String ManipulationUsed for manipulating stringsstrlen, strcpy, strcat, strcmp, strchr, strstr, strtok, strncpy, strncat, strncmp
Character ConversionUsed for converting characters to uppercase, lowercase, or digit formattoupper, tolower, isdigit
Memory Manipulation Used for manipulating memorymemcpy, memmove, memset, memcmp, memchr
Input/OutputUsed for performing input/output operations on stringsputs, gets, fgets, fputs, sscanf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf
Miscellaneous FunctionsMiscellaneous string functionsstrerror, strcoll, strcspn, strerror, strpbrk, strspn, strstr

Example

Below example shows - how to use some of the String Library functions in C:

#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "Hello";
char str2[] = "World";
char buffer[50];

// String manipulation
printf("str1 length = %lu\n", strlen(str1));
strcpy(buffer, str1);
strcat(buffer, " ");
strcat(buffer, str2);
printf("str1 + str2 = %s\n", buffer);
if (strcmp(str1, str2) == 0) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}

// Character conversion
printf("Uppercase str1: ");
for (int i = 0; i < strlen(str1); i++) {
printf("%c", toupper(str1[i]));
}
printf("\n");

// Memory manipulation
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {6, 7, 8, 9, 10};
memcpy(arr1, arr2, sizeof(arr1));
printf("arr1: ");
for (int i = 0; i < sizeof(arr1)/sizeof(arr1[0]); i++) {
printf("%d ", arr1[i]);
}
printf("\n");

return 0;
}
Output:

Standard Template Library (STL)

The Standard Template Library (STL) is a collection of data structures and algorithms, such as vectors, linked lists, and sorting algorithms.

Graphics Libraries

Graphics libraries such as OpenGL and DirectX provide functions for drawing 2D and 3D graphics.

Database Libraries

Database libraries such as SQLite and MySQL provide functions for working with databases.