Back to C Programming
2026-07-126 min read

Standard library function

Learn Standard library function step by step with clear examples and exercises.

Title: Standard Library Functions in C - A full guide for Exams, Interviews, and Real-World Debugging

Why This Matters

Standard library functions are precompiled functions provided by the compiler to perform common tasks without requiring users to write their own implementations. They are a part of various header files such as stdio.h, math.h, ctype.h, and more, which can be included in your C programs using the #include preprocessor directive.

Understanding these functions is crucial for writing efficient C programs, acing coding interviews, and debugging real-world issues. This lesson will provide an in-depth look at standard library functions, their usage, common mistakes, practice questions, and frequently asked questions.

Prerequisites

Before diving into standard library functions, you should have a solid grasp of:

  1. Basic C syntax: variables, data types, operators, control structures (if-else, for, while)
  2. Arrays and pointers
  3. File handling
  4. Basic understanding of memory management in C
  5. Understanding of data structures like linked lists and trees (optional but recommended)
  6. Familiarity with the concept of recursion (optional but recommended)

Core Concept

Standard library functions offer a vast array of precompiled functions that perform various operations without requiring users to write their own implementations. They are a part of several header files such as stdio.h, math.h, ctype.h, and more, which can be included in your C programs using the #include preprocessor directive.

Commonly Used Standard Library Functions

  1. printf(): prints formatted output to the console
  2. scanf(): reads formatted input from the user
  3. putchar(): writes a single character to the standard output
  4. getchar(): reads a single character from the standard input
  5. sqrt(): calculates the square root of a number
  6. pow(): raises a number to a power
  7. abs(): returns the absolute value of a number
  8. rand(): generates a random number
  9. fopen(), fclose(), fprintf(), fscanf(): handle file operations
  10. strlen(), strcmp(), strcpy(), strcat(): manipulate strings
  11. sscanf(), vsprintf(), vprintf(), vfprintf(): handle variable arguments for formatted I/O
  12. qsort(): sorts an array using a user-defined comparison function (from stdlib.h)
  13. memmove(), memcpy(), memset(), memchr(): manipulate memory blocks
  14. calloc(), realloc(), free(): manage dynamic memory allocation
  15. fseek(), ftell(), rewind(): handle file positioning

Using Standard Library Functions

To use standard library functions, include the appropriate header files at the beginning of your C program:

#include <stdio.h> // for printf(), scanf(), putchar(), getchar()
#include <math.h> // for sqrt(), pow()
#include <stdlib.h> // for rand(), malloc(), calloc(), realloc(), free()
#include <string.h> // for strlen(), strcmp(), strcpy(), strcat()
#include <ctype.h> // for isalpha(), isdigit(), toupper(), tolower()

After including the header files, you can use the functions as needed in your program.

Worked Example

Let's create a simple C program that calculates the factorial of a number using recursion and the printf() function:

#include <stdio.h>

long long factorial(int n);

int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);

printf("Factorial of %.d is %.lld\n", num, factorial(num));

return 0;
}

long long factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}

In this example, we first include the stdio.h header file to access the printf(), scanf(), and necessary functions for input/output operations. We then declare a recursive function, factorial(), to calculate the factorial of a number.

We prompt the user for input using printf(), read the number using scanf(), call the factorial() function with the entered number as an argument, and display the result using another call to printf().

Common Mistakes

  1. Forgetting to include the appropriate header file: Without including the necessary header files, you won't be able to use the standard library functions in your program.
  2. Mismatching input and output types: Make sure that the data type of the variable used for reading input matches the expected return type of the function used for reading it. For example, using %d instead of %lld with scanf().
  3. Not handling errors: Some standard library functions may return error codes or set error flags in case of an issue. Failing to check for these can lead to unexpected behavior.
  4. Incorrect function usage: Make sure you understand how each function works and use it accordingly. For example, using printf() with a wrong format specifier or passing the wrong number of arguments.
  5. Not checking for user input validation: Always validate user input to prevent potential security vulnerabilities or runtime errors.
  6. ### Mistakes in Specific Functions
  • scanf(): Be aware that scanf() skips whitespace characters by default, so it may not read the input as expected if there are spaces or newlines present. Use fgets() to read a line of input with spaces and handle the input manually.
  • printf(): When formatting floating-point numbers using printf(), use the %f format specifier for better precision and avoid issues caused by rounding errors.
  • rand(): Be aware that rand() generates random numbers within a range, but the upper limit is exclusive. If you want to generate numbers between 1 and 100, use rand() % 100 + 1.

Practice Questions

  1. Write a program that calculates and prints the sum of two numbers using scanf() and printf().
  2. Create a program that generates 10 random numbers between 1 and 100, sorts them in ascending order, and displays the sorted list. Use rand(), an array, and qsort() (from the stdlib.h header).
  3. Write a program that checks if a given number is prime using the sqrt() function.
  4. Create a program that concatenates two strings using strcat(). Test it with multiple input combinations.
  5. Write a program that counts the number of vowels in a user-entered string using the ctype.h header and its functions.
  6. Write a program that finds the maximum number from an array using for loops, if statements, and the sizeof() operator.
  7. Create a program that reads a file line by line using fgets(). Store each line in a dynamically allocated array and count the total number of lines.
  8. Write a program that reverses a string using recursion and the strlen(), strcpy(), and substr() functions (you can implement substr() using pointers).
  9. Create a program that finds the factorial of a given number using the pow() function and nested loops.
  10. Write a program that calculates the greatest common divisor (GCD) of two numbers using Euclid's algorithm and the modulus operator (%).

FAQ

  1. Why should I use standard library functions instead of writing my own implementations?

Standard library functions are precompiled, optimized versions of common algorithms that can save you time and reduce errors in your code. They also allow for more efficient memory usage and faster execution times.

  1. How do I include header files in my C program?

You can include header files using the #include preprocessor directive at the beginning of your C source file. For example, to include the standard input/output header file, you would write:

#include <stdio.h>
  1. What is the difference between printf() and putchar()?

Both functions are used for outputting characters in C, but they have different usage scenarios. printf() is a formatted I/O function that can print multiple values in various formats, while putchar() writes a single character to the standard output.

  1. Why does my program crash when using scanf()?

Crashes with scanf() often occur due to mismatching input and output types or not handling user input validation. Always make sure that the data type of the variable used for reading input matches the expected return type of the function used for reading it, and validate user input to prevent potential security vulnerabilities or runtime errors.

  1. What is the purpose of the ctype.h header file in C?

The ctype.h header file provides a set of functions that can be used to test the type of characters (e.g., alphabets, digits, whitespace) and convert them to uppercase or lowercase. Some examples include isalpha(), isdigit(), toupper(), and tolower().