Library Functions in C

What are Library Functions ?

Library functions are pre-defined functions in C programming languages, which are defined in specific header files, and whenever we want to use any library function we simple include that header file in our program. Some examples of such library functions and their header file are given in the adjacent table.

The prototype and data definitions of these functions are present in their respective header files. To use these functions we need to include the header file in our program. For example,

library functions in c

What are header files ?

The C programming language constituted of various different header files in which the programmers can include in their program, to use a variety of different pre-defined library functions which are present in these header files. Some commonly used header files are stdio.h, conio.h, string.h, math.h etc.

Syntax for using header files

  • #include

They are to be declared in the starting of the code, just before the definition of the main() function. Once declaring them, the programmer can use any of the library function of that particular header file.

List of Some Commonly Used Library Function

Library FunctionHeader FileUse for
printf()stdio.hprinting desired output on the screen
scanf()stdio.haccepting data from the user
strcmpstring.hcomparing two strings
length()string.hfinding out the length
pow()math.hfor calculating the exponent
sqrt()math.hfor calculating the square root
getchar()stdio.hreturns the next character typed on the keyboard
putchar()stdio.houtputs a single character to the screen
sprintf()stdio.hSends formatted output to a string
fscanf()stdio.hreads formatted input from a stream

Use of some basic Library Functions

Using library functions is no big a deal, we just have to include the header file for the corresponding library function, in the starting of the program and than we can use all the pre-defined library functions present in that header file. Let’s take an example, and see the use of some basic library functions.

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

#includeint main()
{
    int a, b;
    double sqr=0.0, po= 0.0;
    printf("We'll see the working of some library functions \n");
    printf("Enter first number : ");
    scanf("%d",&a);
    printf("Enter second number : ");
    scanf("%d",&b);
    sqr=sqrt(a);
    po=pow(a,b);
    printf("Square root of %d is %f\n", a,sqr);
    printf("Exponential power of %d is %f", a,po);

}
Output
We'll see the working of some library functions
Enter first number : 25
Enter second number : 2
Square root of 25 is 5.000000
Exponential power of 25 is 625.000000
Library Functions in C

The limits.h header determines various properties of the various variable types. The macros defined in this header limits the values of various variable types like char, int, and long. Below is the C program to implement the above approach-

#include <limits.h>
#include <stdio.h>

// Driver code
int main()
{
printf("Number of bits in a byte %d\n",CHAR_BIT);
printf("Minimum value of SIGNED CHAR = %d\n",SCHAR_MIN);

printf("Maximum value of SIGNED CHAR = %d\n",SCHAR_MAX);
printf("Maximum value of UNSIGNED CHAR = %d\n",UCHAR_MAX);

return (0);
}
Number of bits in a byte 8
Minimum value of SIGNED CHAR = -128
Maximum value of SIGNED CHAR = 127
Maximum value of UNSIGNED CHAR = 255

This header file defines the date and time functions. Below is the C program to implement time() and localtime() functions-

#include <stdio.h>
#include <time.h>
#define SIZE 256

int main(void)
{
    char buffer[SIZE];
    time_t curtime;
    struct tm* loctime;
    // Get the current time.
    curtime = time(NULL);
    // Convert it to local time
    // representation.
    loctime = localtime(&curtime);
    // Print out the date and time
    // in the standard format.
    fputs(asctime(loctime), stdout);
    // Print it out

    strftime(buffer, SIZE,"Today is %A, %B %d.\n",loctime);
    fputs(buffer, stdout);
    strftime(buffer, SIZE,The time is %I:%M %p.\n",loctime);

    fputs(buffer, stdout);
    return 0;
}
Mon Mar 21 09:10:30 2022
Today is Monday, March 21.
The time is 09:10 AM.

For using string functions, it is necessary to include string.h header file in the program.

#include <stdio.h>
#include <string.h>

// Driver code
int main()
{
char str1[100] = "prepinsta ",
str2[100] = "prepration";

// Concatenates str1 and str2
strcat(str1, str2);
// Resultant string is stored
// in str1

puts(str1);
return 0;
}
prepinsta prepration

Functions in this header file are used to perform various operations on complex numbers. Complex numbers are the ones with the real and imaginary parts.

#include <complex.h>
#include <stdio.h>

// Driver code
int main(void)
{
    double real = 1.3, imag = 4.9;
    double complex z = CMPLX(real,imag);

    double complex conj_f = conjf(z);
    printf("z = %.1f% + .1fi\n",creal(conj_f), cimag(conj_f));
}
z = 1.3 - 4.9i