Library Functions in C

C standard library functions are one of the most important and useful topics in the C programming language. Without standard C library functions, you cannot execute a program in C. These functions are provided by the system and then stored in the library.

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

What are Standard library functions in C?

These functions are also known as built-in functions in C. And these functions are stored in a common collection called a library. Each function provides a different operation. With the help of these standard functions, a programmer can get the desired output of his/her program easily.

During the time of designing compilers, these library functions are created.

Advantages of using C standard library functions

1. Usability:- One of the important advantages of using these functions is that it will allow a programmer to use the pre-existing code available in the C compiler. These library functions are easy to use.

2. Optimization:- A group of developers always make sure that these standard library functions are up to date. And they are also making efficient code that will optimize the performance.

3. Portable:- You can use these standard library functions anytime and anywhere. They are platform-independent.

4. Time Saving:- If you have to find a square root and print the output on the screen then you don’t have to write numerous lines of code. With the help of standard C library functions, you can do this type of work easily.

5. User-Friendly Syntax:- In C, the syntax of these library functions is easy to understand and implement.

6. Flexibility:- A programmer can easily modify their code or program with the help of standard library functions.

Header files in C

You can use these standard C library functions by declaring header files. Different header files include different functions and different operations.

Below is the list of header files in C:-

Header FileWhat it does
stdio.hMainly used to perform input and output operations like print(), scanf().
string.hMainly used to perform string handling operations like strlen(), strcmp() etc.
conio.hUsed to perform console input and output operations.
stdlib.hMainly used to perform standard utility functions like malloc(), calloc() etc.
math.hMainly used to perform mathematical operations like sqrt(), pow() etc.
ctype.hUsed to perform character type functions like isdigit(), isalpha() etc.
time.hUsed to perform operations related to date and time.
assert.hUsed to verify assumptions made by a program and print a message if the assumption is false using functions like assert() etc.
locale.hMainly used to perform localization like functions such as setlocale() etc.
signal.hMainly used to perform signal handling functions such as signal(), raise() etc.
setjmp.hUsed to perform jump functions.
stdarg.hUsed to perform standard argument functions like va_start() etc.
errno.hFor performing error handling operations such as errno() to indicate the errors.

Below are the header files in detail.

stdio.h

A programmer has to put this header file in every program. This is a basic header file in C. This header file mainly used to perform input and output functions like:-

  • printf():- Display the output on the screen.
  • scanf():- Take input from the user.
  • getchar():- Return characters on the screen.
  • putchar():- Display output as a single character.
  • fgets():- Take line as an input.
  • puts():- Display a line on the screen.
  • fopen():- Open a file.
  • fclose():- Close a file.
  • getw():- Reads an integer from file.
  • putw():- Writes an integer to file.
  • fgetc():- Reads a character from file.
  • putc():- Writes a character to file.
  • fputc():- Writes a character to file.
  • fgets():- Mainly used to read a string one line at a time.
  • fputs():- Writes string to a file.
  • feof():- Finds end of file.
  • fgetchar:- Reads a character from the keyboard.
  • fgetc():- Reads a character from file.
  • fprintf():- Writes formatted data to a file.
  • fscanf():- Reads formatted data from a file.
  • fputchar:- Writes a character from the keyboard.
  • fseek():- Moves file pointer to given location.
  • SEEK_SET:- Used for moving the file pointer at the starting of a file.
  • SEEK_CUR:- Moves file pointer at given location.
  • SEEK_END:- Moves file pointer at the end of file.
  • ftell():- Gives current position of file pointer.
  • rewind():- You can move the file pointer at the beginning of the file.
  • putc():- Writes a character to file.
  • sprint():- Writes formatted output to string.
  • sscanf():- Reads formatted input from a string.
  • remove():- Deletes a file.
  • flush():- Flushes a file.

Example of stdio.h in C

#include <stdio.h>
int main() {
  char name[20]="TechVidvan";
  printf("Tutorial name is: %s\n",name);
  int age;
printf("Enter your age: \n");
  scanf("%d",&age);
  printf("Your age is: %d\n",age);
  return 0;
}

Output

Tutorial name is: TechVidvan
Enter your age:
Your age is: 20

<stdlib.h> header file

This header file is used to perform dynamic allocations through malloc(), calloc(), free(), realloc().

malloc function in C:-

It is used for dynamic allocation in C. It is mainly used for allocating a block of memory dynamically. And it returns a null pointer during the execution of the program.

Syntax of C Malloc Function

pointer_name = (cast_type *) malloc (byte_size);

Example

pointer = malloc(int *) malloc(10 * sizeof(int));

Example:- malloc() function

#include <stdlib.h>
#include <stdio.h>
int main(){
int *point;
point = malloc(10 * sizeof(*point)); // block of 15 integers!
*(point + 1) = 10; /* assign 480 to sixth integer */
printf("Value of second integer is %d",*(point + 1));
}

Output

Value of second integer is 10

calloc() function in C

Calloc stands for Contiguous allocation. The main purpose of this is to allocate multiple blocks of memory. This function is used for allocating memory to complex data structures like arrays, structures etc.

Syntax of C Calloc Function

pointer = (cast_type *) calloc (no_of_bytes, size_of_cast_type);

Example of Calloc Function in C

pointer = calloc(4, sizeof(int));

Example of C calloc() function

#include <stdio.h>
#include <stdlib.h>
int main() {
int a, *point, sum = 0;
point = calloc(4, sizeof(int));
printf("TechVidvan Tutorial: calloc() function!\n");
printf("Sum of first 4 terms!\n");
for (a=0;a<4;a++) {
*(point + a) = a;
sum = sum + *(point + a);
}
printf("Sum is: %d", sum);
return 0;
}

Output

TechVidvan Tutorial: calloc() function!
Sum of first 4 terms!
Sum is: 6

free() function in C

It is used to deallocate or release memory in C programs. During dynamic allocation, you have to free some memory explicitly. If not, you may encounter errors in compilation.

Syntax:-

free(name_of_pointer);

Example:-

free(point);

Example:- free() function

#include <stdio.h>
#include <stdlib.h>
int main() {
int *point = malloc(4 * sizeof(*point));
*(point + 3) = 5;
printf("Third integer value is: %d",*(point + 3));
free(point);
}

Output

Third integer value is: 5

realloc() function in C

It is used for allocating more memory to the already allocated memory. realloc() stands for reallocation of memory.

Syntax:-

point = realloc(point, new_sizeof())

Example:-

point = (int*) malloc(4 * sizeof(int));
point = realloc(point, 6 * sizeof(int));

Example:- free() function

include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
char *string_name;
string_name = (char *) malloc(20);
strcpy(string_name, "TechVidvan");
string_name = (char *) realloc(string_name, 30); // Reallocating memory
strcat(string_name, " Tutorials!");
printf("Favourite website for C tutorials is: %s", string_name);
return(0);
}

Output

Favourite website for C tutorials is: TechVidvan Tutorials!

NOTE:- If you want to use the above mentioned functions then you have to include <stdlib.h> header file in your program code.

<math.h> header file

This header file will let you perform some mathematical operations in your program. This header file includes functions such as:-

  • sqrt() – Find the square root of a number
  • pow() – Find the power raised to that number.
  • fabs() – Find the absolute value of a number.
  • log() – Find the logarithm of a number.
  • sin() – Find the sine value of a number.
  • cos() – Find the cosine value of a number.
  • tan() – Find the tangent value of a number.

Example:- <math.h> header file

#include <stdio.h>
#include <math.h>
int main()
{
printf("TechVidvan Tutorials: <math.h> header file examples!\n\n");
int num=4, squareRoot;
int power = 3, result_of_power;
double num1 = -7.2, res;
double tan_res;
squareRoot = sqrt(num);
printf("Square root of %d is: %d\n", num, squareRoot);
result_of_power = pow(num,power);
printf("Power of %d is: %d\n",num,result_of_power);
res = fabs(num1);
printf("Absolute value of %f is: %d\n",num1,res);
tan_res = tan(num);
printf("Tangent value of %d is: %f",num,tan_res);
return 0;
}

Output

TechVidvan Tutorials: <math.h> header file examples!

Square root of 4 is: 2
Power of 4 is: 64
Absolute value of -7.200000 is: 0
Tangent value of 4 is: 1.157821

NOTE:- If you want to use the above mentioned functions then you have to include <math.h> header file in your program code.

<ctype.h> header file

This header file includes functions which will help you in character handling. The <ctype.h> header file has functions such as:-

  • isalpha() – Check if the character is an alphabet or not.
  • isdigit() – Check if the character is a digit or not.
  • isalnum() – Check if the character is alphanumeric or not.
  • isupper() – Check if the character is in uppercase or not
  • islower() – Check if the character is in lowercase or not.
  • toupper() – Convert the character into uppercase.
  • tolower() – Convert the character into lowercase.
  • iscntrl() – Check if the character is a control character or not.
  • isgraph() – Check if the character is a graphic character or not.
  • isprint() – Check if the character is a printable character or not
  • ispunct() – Check if the character is a punctuation mark or not.
  • isspace() – Check if the character is a white-space character or not.
  • isxdigit() – Check if the character is hexadecimal or not.

Example:- <ctype.h> header file

#include <stdio.h>
#include <ctype.h>
int main()
{
printf("TechVidvan Tutorials: <ctype.h> header file examples!\n\n");
char a,res;
char b,c;
a='T';
b = 't';
c = '7';
printf("Converting %c to lower case: %c\n",a,tolower(a));
if(isalpha(a)){
  printf("It is a character!\n");
}
if(isdigit(a)){
  printf("It is a digit!\n");
}
else{
  printf("It is not  digit!\n");
}
if(isalnum(c)){
  printf("It is alphanumeric!\n");
}
printf("Converting %c to upper case: %c\n",b,toupper(b));
return 0;
}

Output

TechVidvan Tutorials: <ctype.h> header file examples!Converting T to lower case: t
It is a character!
It is not digit!
It is alphanumeric!
Converting t to upper case: T

NOTE:- You have to include “#include <ctype.h>” to run the above character handling functions.

<string.h> header file

This header file has functions which will help in handling and manipulating strings in your program code.

<string.h> header file has functions such as:-

  • strcpy(s1, s2):- Copies string s2 into string s1.
  • strcat(s1, s2):- Used for concatenating string s2 at the end of string s1.
  • strlen(s1):- Returns the length of string s1.
  • strcmp(s1, s2):- Compares string s1 and s2.
  • strlwr(s1):- Converts string s1 to lowercase.
  • strupr(s1):- Converts string s1 to uppercase.
  • strchr(s1, c):- Mainly used to return a pointer to the first occurrence of character c in s1.
  • strstr(s1, s2):- Used to return a pointer to the first occurrence of s2 in string s1.
  • strrev(s1):- Reverse the string s1.
  • strncmp(s1, s2, n):- It returns greater than 0 if s1>s2. And it returns less than 0 if s1<s2. It returns 0 if the first n characters of s1 is equal to the first n characters of s2.
  • strncpy(s1, s2, n):- Copies the first n characters of s2 to s1.
  • strncat(s1, s2, n):– Concatenates first n characters of s2 to the end of s1.

Example:- <string.h> header file

#include <stdio.h>
#include <string.h>
int main () {
char s1[20] = "Greeting from ";
char s2[20] = "TechVidvan!";
char s3[20];
int len;
int cmp;
strcpy(s3, s1); // copies string s1 into s3!
printf("Coping string s1 into s3: %s\n", s3 );
strcat( s1, s2); // concatenates s1 and s2!
printf("Concatenating string s1 and s2: %s\n", s1 );
len = strlen(s1); // length of string s1
printf("Length of string s1 is: %d\n", len );
// comparing string s1 and s2!
cmp = strcmp(s1,s2);
if (cmp == 0){
  printf("String s1 and s2 are equal!\n");
}
else{
  printf("String s1 and s2 are not equal!\n");
}
return 0;

Output

Coping string s1 into s3: Greeting from
Concatenating string s1 and s2: Greeting from TechVidvan!
Length of string s1 is: 25
String s1 and s2 are not equal!

NOTE:- You have to include “#include <string.h>” to run the above string handling functions.

<conio.h> header file

Main purpose of this header file is to perform input/output operations such as clrscr() to clear the screen and getch() to get the character from the keyboard.

NOTE:- You cannot use <conio.h> header file in Linux because Linux does not support it. But in Windows, you can use it.

Summary

Without header files, you cannot run any C programs. Different header files include different functions which help a programmer to write numerous lines of code easily. C supports different header files like stdio.h, conio.h, string.h, math.h, stdlib.h etc. These header files allow a programmer to use the pre-existing code available in the C compiler.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.