55 Standard Library Functions For C Programmers
55 Standard Library Functions For C Programmers
These pre-defined functions can simplify your coding tasks, boost efficiency, and
save you valuable development time. Whether you're a seasoned programmer or
just starting out, understanding and utilizing Standard Library functions is
essential for coding scalable C programs.
We'll explore not only their definition and syntax but also an example of each that
you can reference for your programming endeavors.
Table of Contents
1. isalpha()
Example:
#include<stdio.h>
#include<ctype.h>
int main(){
char ch='A';
if(isalpha(ch)){
}else{
return0;
Output:
A is an alphabetic character.
2. isdigit()
Example:
#include<stdio.h>
#include<ctype.h>
int main(){
char ch='5';
if(isdigit(ch)){
printf("%c is a digit.\n",ch);
}else{
return0;
Output:
5 is a digit.
3. islower()
Example:
#include<stdio.h>
#include<ctype.h>
int main(){
char ch='a';
if(islower(ch)){
}else{
return0;
Output:
a is a lowercase letter.
4. isupper()
Example:
#include<stdio.h>
#include<ctype.h>
int main(){
char ch='A';
if(isupper(ch)){
}else{
return0;
Output:
A is an uppercase letter.
5. tolower()
Example:
#include<stdio.h>
#include<ctype.h>
int main(){
char ch='A';
char lowercase=tolower(ch);
return0;
Output:
A converted to lowercase is a.
6. toupper()
Example:
#include<stdio.h>
#include<ctype.h>
int main(){
char ch='a';
char uppercase=toupper(ch);
return0;
Output:
a converted to uppercase is A.
7. assert()
Example:
#include <assert.h>
int main() {
int x = 10;
assert(x == 9); // If false, the program will terminate with an error message.
return0;
Output:
8. NDEBUG
This macro controls whether assertions are enabled or disabled. When NDEBUG is
defined, assertions are turned off. When it's not defined, assertions are active.
Example:
#define NDEBUG
#include <assert.h>
Output:
9. static_assert()
Example:
#include <assert.h>
Output:
10. setlocale(LC_ALL,)
Setting the locale to LC_ALL affects all aspects of a program's behavior related to
language and region.
Example:
#include <locale.h>
#include <stdio.h>
int main() {
return0;
Output:
11. setlocale(LC_TIME,)
Setting the locale to LC_TIME affects the formatting of dates and times.
Example:
#include <locale.h>
#include <stdio.h>
#include <time.h>
int main() {
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char buffer[80];
return0;
Output:
This sets the date and time to Japan’s timezone and formatting.
12.setlocale(LC_CTYPE,)
Example:
#include <locale.h>
#include <stdio.h>
#include <wctype.h>
int main() {
https://blog.hubspot.com/website/standard-library-c-functions#:~:text=In this post%2C we've,%2C mathematical calculations%2C and more. 14/65
11/28/24, 5:07 PM 55 Standard Library Functions for C Programmers
if (iswalpha(wideChar)) {
} else {
return0;
Output:
13. sqrt()
https://blog.hubspot.com/website/standard-library-c-functions#:~:text=In this post%2C we've,%2C mathematical calculations%2C and more. 15/65
11/28/24, 5:07 PM 55 Standard Library Functions for C Programmers
Example:
#include<stdio.h>
#include<math.h>
intmain(){
doublenumber=25.0;
doubleresult=sqrt(number);
return0;
Output:
14. sin()
Example:
#include <stdio.h>
#include <math.h>
int main() {
return0;
Output:
15. exp()
Example:
#include<stdio.h>
#include<math.h>
intmain(){
doublex=2.0;
doubleresult=exp(x);
return0;
Output:
16. round()
Example:
#include<stdio.h>
#include<math.h>
intmain(){
doublenum1=7.3;
doublenum2=7.7;
longrounded1=round(num1);
longrounded2=round(num2);
return0;
Output:
17. strcpy()
Example:
#include <stdio.h>
#include <string.h>
int main() {
char destination[20];
strcpy(destination, source);
return0;
Output:
18. strlen()
Example:
#include <stdio.h>
#include <string.h>
int main() {
return0;
Output:
19. strcat()
Example:
#include <stdio.h>
#include <string.h>
int main() {
strcat(str1, str2);
return0;
Output:
20. strcmp()
Example:
#include <stdio.h>
#include <string.h>
int main() {
if (result < 0) {
} else {
return0;
Output:
21. time()
Example:
#include<stdio.h>
#include<time.h>
intmain(){
time_tcurrentTime;
time(¤tTime);
return0;
Output:
22. strftime()
This function formats time into a custom date and time representation.
Example:
#include <stdio.h>
#include <time.h>
int main() {
struct tm timeinfo;
time_t currentTime;
time(¤tTime);
localtime_r(¤tTime, &timeinfo);
char buffer[80];
return0;
Output:
Custom date and time format: Tuesday, September 19, 2023 16:09:05
23. difftime()
This function calculates the difference in seconds between two time values.
Example:
#include <stdio.h>
#include <time.h>
int main() {
time(&startTime);
time(&endTime);
return0;
Output:
24. sleep()
Example:
#include <stdio.h>
#include <time.h>
int main() {
printf("Sleep complete!\n");
return0;
Output:
Sleep complete!
25. mktime()
Converts a struct tm time structure back to a time_t value. This can be used for
date and time arithmetic.
Example:
#include <stdio.h>
#include <time.h>
int main() {
return0;
Output:
https://blog.hubspot.com/website/standard-library-c-functions#:~:text=In this post%2C we've,%2C mathematical calculations%2C and more. 28/65
11/28/24, 5:07 PM 55 Standard Library Functions for C Programmers
26. clock()
Example:
#include <stdio.h>
#include <time.h>
int main() {
double cpu_time_used;
start = clock();
end = clock();
return0;
Output:
27. asctime()
Example:
#include <stdio.h>
#include <time.h>
int main() {
timeinfo.tm_mday = 1;
return0;
Output:
28. localtime()
This function converts a time_t value into a struct tm time structure for local
time.
Example:
#include <stdio.h>
#include <time.h>
int main() {
time_t currentTime;
struct tm *timeinfo;
time(¤tTime);
timeinfo = localtime(¤tTime);
return0;
Output:
29. nanosleep()
This function makes the program sleep for a specified number of nanoseconds,
allowing precise control over timing.
Example:
#include <stdio.h>
#include <time.h>
int main() {
sleepTime.tv_sec = 0;
nanosleep(&sleepTime, NULL);
https://blog.hubspot.com/website/standard-library-c-functions#:~:text=In this post%2C we've,%2C mathematical calculations%2C and more. 32/65
11/28/24, 5:07 PM 55 Standard Library Functions for C Programmers
printf("Sleep complete!\n");
return0;
Output:
Sleep complete!
30. printf()
This function prints formatted output to the standard output (usually the console).
Example:
#include <stdio.h>
int main() {
return0;
Output:
My age is 30 years.
31. scanf()
Example:
#include <stdio.h>
int main() {
int age;
scanf("%d", &age);
return0;
Output:
These functions are typically used together. They open a file, write formatted data
to it, and then close the file.
Example:
#include <stdio.h>
int main() {
FILE *file;
if (file != NULL) {
fclose(file);
} else {
return0;
Output:
33. getchar()
Example:
#include <stdio.h>
int main() {
char ch;
ch = getchar();
return0;
Output:
Enter a character: G
You entered: G
34. putchar()
Example:
#include <stdio.h>
int main() {
char ch = 'A';
putchar(ch);
return0;
Output:
35. fgets()
This function reads a line of text from the standard input or a file.
Example:
#include <stdio.h>
int main() {
char buffer[100];
return0;
Output:
36. puts()
This function writes a string to the standard output with an automatic newline
character.
Example:
#include <stdio.h>
int main() {
puts(greeting);
return0;
Output:
Hello, World!
Example:
#include<stdio.h>
intisEven(intnumber){
if(number%2==0){
}else{
intmain(){
intnum;
scanf("%d",&num);
if(isEven(num)){
printf("%d is even.\n",num);
}else{
printf("%d is odd.\n",num);
return0;
Output:
Enter an integer: 5
5 is odd.
38. malloc()
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
if (arr != NULL) {
arr[i] = i * 10;
} else {
return0;
Output:
arr[0] = 0
arr[1] = 10
arr[2] = 20
arr[3] = 30
arr[4] = 40
39. free()
Example:
#include <stdlib.h>
int main() {
return0;
https://blog.hubspot.com/website/standard-library-c-functions#:~:text=In this post%2C we've,%2C mathematical calculations%2C and more. 43/65
11/28/24, 5:07 PM 55 Standard Library Functions for C Programmers
Output:
40. rand()
Example:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
return0;
Output:
41. exit()
Example:
#include <stdlib.h>
int main() {
Output:
42. atexit()
Example:
#include <stdio.h>
#include <stdlib.h>
void exitHandler() {
printf("Exiting program...\n");
int main() {
atexit(exitHandler);
return0;
Output:
Exiting program...
44. system()
Example:
#include <stdlib.h>
int main() {
return0;
Output:
45. getenv()
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
if (path != NULL) {
} else {
return0;
Output:
46. signal()
Example:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
int main() {
signal(SIGINT, handleSignal);
while (1) {
return 0;
Output:
47. raise()
Example:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
int main() {
printf("Signal raised.\n");
return 0;
Output:
48. sigaction()
This function sets a custom handler for a signal and provides more control over
signal handling.
Example:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
int main() {
action.sa_handler = handleSignal;
sigemptyset(&action.sa_mask);
https://blog.hubspot.com/website/standard-library-c-functions#:~:text=In this post%2C we've,%2C mathematical calculations%2C and more. 51/65
11/28/24, 5:07 PM 55 Standard Library Functions for C Programmers
action.sa_flags = 0;
while (1) {
return 0;
Output:
49. sigprocmask()
Example:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
int main() {
sigemptyset(&newMask);
sigaddset(&newMask, SIGINT);
while (1) {
return 0;
Output:
50. kill()
Example:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
int main() {
return 0;
Output:
51. va_start()
Example:
#include <stdio.h>
#include <stdarg.h>
va_list args;
va_start(args, count);
va_end(args);
int main() {
return0;
Output:
52. va_arg()
Example:
#include <stdio.h>
#include <stdarg.h>
va_list args;
va_start(args, count);
va_end(args);
int main() {
return 0;
Output:
10 20 30
53. va_end()
Example:
#include <stdio.h>
#include <stdarg.h>
va_list args;
va_start(args, count);
va_end(args);
int main() {
return0;
Output:
List is cleaned.
54. va_copy()
Example:
#include <stdio.h>
#include <stdarg.h>
int main() {
return 0;
Output:
The setjmp() function is used to save the program's state while the longjmp() is
used to jump back to that state, allowing non-local control flow.
Example:
#include <stdio.h>
#include <setjmp.h>
jmp_buf buffer;
void foo() {
printf("foo() called\n");
int main() {
if (setjmp(buffer) == 0) {
printf("setjmp() called\n");
foo();
} else {
printf("longjmp() called\n");
return0;
Output:
setjmp() called
foo() called
longjmp() called
Related Articles
All Products and Features AI Email Writer See All Free Business About Us Customer Support
Free Meeting Scheduler Free Website Builder Tools Careers Join a Local User Group
Breeze AI Tools Free Online Form Builder Clip Creator Board of Directors
Email Tracking Software Free Chatbot Builder Website Grader Investor Relations
Template Marketplace
Campaign Assistant