What is Standard Library in C?

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:

advertisement
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:

Free 30-Day Java Certification Bootcamp is Live. Join Now!
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:

advertisement
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.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.