Master the Essential C Standard Library Functions for Professional Coding
This guide explores the most commonly used C standard library headers—stdio.h, stdlib.h, and string.h—detailing each function’s purpose, usage, and providing clear code examples such as printf, malloc, strcpy, and qsort to help programmers write cleaner, more efficient C code.
Introduction
Understanding and effectively using the C standard library is essential for writing professional, maintainable C code. This article reviews the most frequently used functions from the three core headers <stdio.h>, <stdlib.h>, and <string.h>, providing concise explanations and runnable examples.
<stdio.h> Functions
Provides input and output facilities.
printf – formats and prints data to standard output.
int printf(const char *format, ...);
printf("Hello, %s!
", "world");scanf – reads formatted input from standard input.
int scanf(const char *format, ...);
int age;
scanf("%d", &age);fprintf – prints formatted data to a file stream.
int fprintf(FILE *stream, const char *format, ...);fscanf – reads formatted data from a file stream.
int fscanf(FILE *stream, const char *format, ...);fopen – opens a file.
FILE *fopen(const char *filename, const char *mode);
FILE *file = fopen("example.txt", "r");fclose – closes an opened file. int fclose(FILE *stream); fgets – reads a line from a file. char *fgets(char *s, int size, FILE *stream); fputs – writes a string to a file. int fputs(const char *s, FILE *stream); fgetc – reads a single character from a file. int fgetc(FILE *stream); fputc – writes a single character to a file. int fputc(int c, FILE *stream); rewind – resets file position to the beginning. void rewind(FILE *stream); feof – tests for end‑of‑file.
int feof(FILE *stream);<stdlib.h> Functions
Offers general utilities such as memory management and conversion.
malloc – allocates a block of memory.
void *malloc(size_t size);
int *arr = (int *)malloc(5 * sizeof(int));calloc – allocates and zero‑initialises memory.
void *calloc(size_t num_elements, size_t element_size);
int *arr = (int *)calloc(5, sizeof(int));realloc – changes the size of an allocated block.
void *realloc(void *ptr, size_t size);
arr = (int *)realloc(arr, 10 * sizeof(int));free – releases allocated memory.
void free(void *ptr);
free(arr);rand – returns a pseudo‑random integer.
int rand(void);
int random_number = rand();srand – seeds the random‑number generator.
void srand(unsigned int seed);
srand(42);abs – computes the absolute value of an integer.
int abs(int n);
int absolute_value = abs(-5);atoi – converts a string to an integer.
int atoi(const char *str);
int number = atoi("42");atof – converts a string to a double.
double atof(const char *str);
double pi = atof("3.14159");exit – terminates the program.
void exit(int status);
exit(0); // normal terminationsystem – executes a system command.
int system(const char *command);
system("ls -l");qsort – performs quick‑sort on an array.
void qsort(void *base, size_t num_elements, size_t element_size,
int (*compare_function)(const void *, const void *));
int compare(const void *a, const void *b) { return (*(int *)a - *(int *)b); }
int arr[] = {5, 2, 9, 1, 5};
qsort(arr, 5, sizeof(int), compare);<string.h> Functions
Contains utilities for handling C strings.
strcpy – copies a string.
char *strcpy(char *dest, const char *src);
char destination[20];
char source[] = "Hello, world!";
strcpy(destination, source);strcat – concatenates two strings.
char *strcat(char *dest, const char *src);
char str1[20] = "Hello, ";
char str2[] = "world!";
strcat(str1, str2);strlen – returns the length of a string.
size_t strlen(const char *s);
char str[] = "Hello";
size_t length = strlen(str);strcmp – compares two strings.
int strcmp(const char *s1, const char *s2);
char str1[] = "apple";
char str2[] = "banana";
int result = strcmp(str1, str2);strncmp – compares the first n characters of two strings.
int strncmp(const char *s1, const char *s2, size_t n);
char str1[] = "apple";
char str2[] = "appetizer";
int result = strncmp(str1, str2, 3);strchr – finds the first occurrence of a character.
char *strchr(const char *s, int c);
char str[] = "Hello, world!";
char *result = strchr(str, 'w');strstr – finds the first occurrence of a substring.
char *strstr(const char *haystack, const char *needle);
char str[] = "The quick brown fox";
char *result = strstr(str, "brown");strtok – tokenises a string based on delimiters.
char *strtok(char *str, const char *delimiters);
char str[] = "apple,banana,grape";
char *token = strtok(str, ",");
while (token != NULL) {
printf("%s
", token);
token = strtok(NULL, ",");
}Conclusion
The functions presented above form the backbone of everyday C programming, enabling efficient input/output, memory management, and string manipulation. Mastering these standard library calls helps developers write cleaner, faster, and more reliable code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
