What is the Standard Library in C?
The Standard Library in C is a set of pre-written functions and macros that come bundled with your C compiler. These functions help you do common programming tasks like:
- Input and output
- String handling
- Math operations
- Memory management
- File handling
- Time manipulation
- Error handling
Instead of writing your own code for these tasks, you can use functions from the standard library.
Why Use the Standard Library?
Here are some reasons why the C Standard Library is useful:
- Saves Time: No need to write basic code again and again.
- Well-Tested: These functions are reliable and widely used.
- Portable: They work across different systems and compilers.
- Efficient: Most functions are optimized for performance.
Common C Standard Library Header Files and Their Purpose
Header File | Description |
---|---|
<stdio.h> | Input and Output functions (printf, scanf, fopen, etc.) |
<stdlib.h> | Utility functions (malloc, free, exit, atoi, etc.) |
<string.h> | String handling (strcpy, strlen, strcmp, etc.) |
<math.h> | Math functions (sqrt, pow, sin, etc.) |
<ctype.h> | Character classification (isdigit, isalpha, etc.) |
<time.h> | Date and time utilities (time, clock, difftime) |
<assert.h> | Diagnostic debugging (assert) |
<limits.h> | Limits of data types (INT_MAX, CHAR_MIN, etc.) |
<float.h> | Floating-point limits |
<stddef.h> | Common macros like NULL, size_t, etc. |
Examples of Standard Library in C
1. <stdio.h> – Standard Input/Output
It is used for Input and output functions like printf, scanf, fgets, etc.
#include <stdio.h> int main() { printf("Hello from Sanfoundry!\n"); return 0; }
Output:
Hello from Sanfoundry!
2. <stdlib.h> – Standard Library
It is used for Memory allocation, process control, conversions, etc.
#include <stdlib.h> #include <stdio.h> int main() { int *ptr = (int *)malloc(sizeof(int)); *ptr = 100; printf("Value = %d\n", *ptr); free(ptr); return 0; }
Output:
Value = 100
3. <string.h> – String Handling
It is used for Operations like strcpy, strlen, strcmp, etc.
#include <string.h> #include <stdio.h> int main() { char str1[] = "Sanfoundry"; char str2[20]; strcpy(str2, str1); printf("Copied string: %s\n", str2); return 0; }
Output:
Copied string: Sanfoundry
4. <math.h> – Mathematical Functions
It is used for sqrt, pow, sin, cos, etc.
#include <math.h> #include <stdio.h> int main() { double result = pow(2, 3); // 2^3 = 8 printf("2 raised to power 3 is %.0f\n", result); return 0; }
Output:
2 raised to power 3 is 8
5. <ctype.h> – Character Handling
It is used for Check character types like isdigit, isalpha, tolower, etc.
#include <ctype.h> #include <stdio.h> int main() { char c = 'A'; if (isalpha(c)) { printf("%c is an alphabet.\n", c); } return 0; }
Output:
A is an alphabet.
6. <time.h> – Date and Time Functions
It is used to getting system time, measuring program execution time, delays, etc.
#include <stdio.h> #include <time.h> int main() { time_t now; time(&now); printf("Current time: %s", ctime(&now)); return 0; }
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Apply for C Internship
- Check C Books
- Check Computer Science Books