What are Library functions in C? See Example

In the C programming language, library functions are pre-defined functions that are available for programmers to use directly without writing their own implementations. These functions are stored in header files, which must be included in a program using the #include directive.

Library functions help save time, reduce errors, and provide reliable, optimized operations for common programming tasks.


Why Use Library Functions?

  • They are pre-tested and reliable
  • Help you write code faster
  • Reduce the need to reinvent common functionalities
  • Improve program readability
  • Ensure portability across different systems

Common Library Functions in C (With Categories)

Below are the most frequently used categories of library functions along with examples:


1. Input/Output Functions

Header File: stdio.h

FunctionDescription
printf()Prints formatted output to the screen
scanf()Reads formatted input from the keyboard
gets()Reads a string (deprecated)
puts()Prints a string
getchar()Reads a single character
putchar()Prints a single character

2. String Handling Functions

Header File: string.h

FunctionDescription
strlen()Returns the length of a string
strcpy()Copies one string to another
strcat()Concatenates two strings
strcmp()Compares two strings
strrev()Reverses a string (not standard everywhere)

3. Mathematical Functions

Header File: math.h

FunctionDescription
sqrt()Calculates square root
pow()Calculates a number raised to a power
abs()Returns absolute value (int)
fabs()Returns absolute value (float/double)
sin(), cos(), tan()Trigonometric functions
log(), log10()Logarithmic functions

4. Character Handling Functions

Header File: ctype.h

FunctionDescription
isalnum()Checks if alphanumeric
isalpha()Checks if alphabetic
isdigit()Checks if a digit
tolower()Converts to lowercase
toupper()Converts to uppercase

5. Memory Management Functions

Header File: stdlib.h

FunctionDescription
malloc()Allocates memory dynamically
calloc()Allocates multiple memory blocks
realloc()Resizes previously allocated memory
free()Frees dynamically allocated memory

6. General Utility Functions

Header File: stdlib.h

FunctionDescription
system()Executes system commands
exit()Terminates the program
rand()Generates a random number
atoi()Converts string to integer
atof()Converts string to float

7. Time and Date Functions

Header File: time.h

FunctionDescription
time()Returns current system time
ctime()Converts time to string format
difftime()Finds difference between two times

Using a Library Function

Example: Using sqrt() function from <math.h>:

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

int main() {
    double num = 25, result;
    result = sqrt(num);
    printf("Square root of %.2f is %.2f", num, result);
    return 0;
}

Conclusion

Library functions form the backbone of C programming. They reduce complexity, enhance performance, and allow programmers to focus on core logic rather than low-level details. From input/output to string handling, mathematics, and memory management — library functions make C more powerful and efficient.

🔗 View other articles about C Programming:

https://savanka.com/category/learn/c-programming

🔗 External C Documentation:

https://www.w3schools.com/c

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *