Library Function in C

Library Function used in C :

On this page we will discuss about standard library function used in C programming language and how to use them. The standard library function is basically collection of sub-libraries, which contains the code for several functions.
library function in C

Standard Library Function in C

In C Programming, the standard library functions are simply the inbuilt functions whose syntax and definition are present in their respective header files.
These header file are included in our program in order to use these function.

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.

Run
#include<stdio.h>
#include<conio.h>
int 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

Advantages of using library function in C

  • These functions have undergone testing for multiple times hence they work and are simple to use.
  • These functions are optimized to give maximum performance.
  • These functions saves a significant amount of development time.
  • These functions can do same thing on every computer and hence they are portable.

Example 1:

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-

Run
#include<stdio.h>
#include<conio.h>
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);
}

Output:

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

Example 2:

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

Run
#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;
}

Output:

Mon Mar 21 09:10:30 2022
Today is Monday, March 21.
The time is 09:10 AM.

Example 3:

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.

Run
#include<stdio.h>
#include<conio.h>
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));
}

Output:

z = 1.3 - 4.9i

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription