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()
andscanf()
. - stdlib.h: Provides declarations for memory allocation functions like
malloc()
andfree()
. - math.h: Provides declarations for mathematical functions like
sin()
andsqrt()
. - string.h: Provides declarations for string manipulation functions like
strlen()
andstrcat()
.
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:
Category | Description | Functions |
---|---|---|
Input/Output | Functions for input and output operations. | printf, scanf, fprintf, fscanf, fgets, fputc, fgetc |
String manipulation | Functions for working with strings. | strlen, strcpy, strcat, strcmp, strchr, strstr |
Math | Functions for mathematical operations. | sin, cos, sqrt, pow, ceil, floor |
Memory | Functions for dynamic memory allocation and manipulation. | malloc, calloc, realloc, free, memset, memcpy, memcmp |
Date and Time | Functions for working with dates and times. | time, asctime, ctime, localtime, mktime |
File handling | Functions for working with files. | fopen, fclose, fread, fwrite, fseek, ftell |
Miscellaneous | Functions 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:
Category | Functions |
---|---|
Trigonometry | sin, cos, tan, asin, acos, atan, atan2 |
Exponents and Logarithms | exp, exp2, expm1, log, log10, log1p, log2, pow |
Square Roots and Powers | sqrt, 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:
Category | Description | Functions |
---|---|---|
Standard Input/Output Functions | Used for performing input/output operations on the standard input/output streams | printf, scanf, puts, gets, fopen, fclose, fgetc, fgets, fputc, fputs, fprintf, fscanf |
File Input/Output Functions | Used for performing input/output operations on files | fopen, fclose, fgetc, fgets, fputc, fputs, fprintf, fscanf, fread, fwrite |
Formatted Output Functions | Used for formatting and printing data | printf, 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
andage
variables. We then useprintf
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
Category | Description | Functions |
---|---|---|
String Manipulation | Used for manipulating strings | strlen, strcpy, strcat, strcmp, strchr, strstr, strtok, strncpy, strncat, strncmp |
Character Conversion | Used for converting characters to uppercase, lowercase, or digit format | toupper, tolower, isdigit |
Memory Manipulation | Used for manipulating memory | memcpy, memmove, memset, memcmp, memchr |
Input/Output | Used for performing input/output operations on strings | puts, gets, fgets, fputs, sscanf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf |
Miscellaneous Functions | Miscellaneous string functions | strerror, 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.