Open In App

C Library Functions

Improve
Improve
Like Article
Like
Save
Share
Report

The Standard Function Library in C is a huge library of sub-libraries, each of which contains the code for several functions. In order to make use of these libraries, link each library in the broader library through the use of header files. The definitions of these functions are present in their respective header files. In order to use these functions, we have to include the header file in the program. Below are some header files with descriptions:

S No. Header Files Description
1 <assert.h> It checks the value of an expression that we expect to be true under normal circumstances.
If the expression is a nonzero value, the assert macro does nothing.
2 <complex.h>  A set of functions for manipulating complex numbers.
3 <float.h> Defines macro constants specifying the implementation-specific properties of the
floating-point library.
4 <limits.h> These limits specify that a variable cannot store any value beyond these limits, for example-
An unsigned character can store up to a maximum value of 255.
5 <math.h> The math.h header defines various mathematical functions and one macro. All the Functions 
in this library take double as an argument and return double as the result.
6 <stdio.h> The stdio.h header defines three variable types, several macros, and various
function for performing input and output.
7 <time.h> Defines date and time handling functions.
8 <string.h> Strings are defined as an array of characters. The difference between a character array 
and a string is that a string is terminated with a special character ‘\0’.

Implementation: Let’s discuss the implementation of the basic libraries with a C program:

1. stdio.h: This library is use to use the printf() function, the header file <stdio.h> should be included in the program. Below is the C program to implement the above approach:

C




// C program to implement
// the above approach
#include <stdio.h>
 
// Driver code
int main()
{
    printf("GEEKS FOR GEEKS");
    return 0;
}


Output

GEEKS FOR GEEKS

Note: If printf() function is used without including the header file <stdio.h>, an error will be displayed.

2. math.hTo perform any operation related to mathematics, it is necessary to include math.h header file. 

Example 1: sqrt()

Syntax-

double sqrt(double x)

Below is the C program to calculate the square root of any number:

C




// C program to implement
// the above approach
#include <math.h>
#include <stdio.h>
 
// Driver code
int main()
{
    double number, squareRoot;
 
    number = 12.5;
 
    // Computing the square root
    squareRoot = sqrt(number);
 
    printf("Square root of %.2lf =  %.2lf",
           number, squareRoot);
    return 0;
}


Output

Square root of 12.50 =  3.54

Example 2- pow():

Syntax:

double pow(double x, double y)

Below is the C program to calculate the power of any number:

C




// C program to implement
// the above approach
#include <math.h>
#include <stdio.h>
 
// Driver code
int main()
{
    double base, power, result;
    base = 10.0;
    power = 2.0;
 
    // Calculate the result
    result = pow(base, power);
    printf("%.1lf^%.1lf = %.2lf",
           base, power, result);
    return 0;
}


Output

10.0^2.0 = 100.00

Example 3- sin():

Syntax:

double sin(double x)

Below is the C program to calculate the sine of an argument:

C




// C program to implement
// the above approach
#include <math.h>
#include <stdio.h>
 
// Driver code
int main()
{
    double x;
    double result;
    x = 2.3;
    result = sin(x);
    printf("sin(%.2lf) = %.2lf\n",
           x, result);
    x = -2.3;
    result = sin(x);
    printf("sin(%.2lf) = %.2lf\n",
           x, result);
    x = 0;
    result = sin(x);
    printf("sin(%.2lf) = %.2lf\n",
           x, result);
    return 0;
}


Output

sin(2.30) = 0.75
sin(-2.30) = -0.75
sin(0.00) = 0.00

Example 4- cos():

Syntax:

double cos(double x);

Below is the C program to calculate the cosine of an argument: 

C




// C program to implement
// the above approach
#include <math.h>
#include <stdio.h>
#define PI 3.141592654
 
// Driver code
int main()
{
    double arg = 30, result;
 
    // Converting to radian
    arg = (arg * PI) / 180;
 
    result = cos(arg);
    printf("cos of %.2lf radian = %.2lf",
           arg, result);
    return 0;
}


Output

cos of 0.52 radian = 0.87

Example 5- tan():

Syntax:

double tan(double x);

Below is the C program to calculate the tangent of the argument:

C




// C program to implement
// the above approach
#include <math.h>
#include <stdio.h>
 
// Driver code
int main()
{
    double x;
    double result;
    x = 2.3;
    result = tan(x);
    printf("tan(%.2lf) = %.2lf\n",
           x, result);
    x = -2.3;
    result = tan(x);
    printf("tan(%.2lf) = %.2lf\n",
           x, result);
    return 0;
}


Output

tan(2.30) = -1.12
tan(-2.30) = 1.12

Example 6- log():

Syntax- 

double log( double arg );

Below is the C program to calculate the natural logarithm of an argument-

C




// C program to implement
// the above approach
#include <math.h>
#include <stdio.h>
 
// Driver code
int main()
{
    double num = 5.6, result;
    result = log(num);
    printf("log(%.1f) = %.2f",
           num, result);
    return 0;
}


Output

log(5.6) = 1.72

3. float.h: The float.h header file of the C Standard Library contains a set of various platform-dependent constants related to floating-point values. Below is the C program to implement the above approach-

C




// C program to implement
// the above approach
#include <float.h>
#include <stdio.h>
 
// Driver code
int main()
{
    printf("Maximum value of float = %.10e\n",
           FLT_MAX);
    printf("Minimum value of float = %.10e\n",
           FLT_MIN);
}


Output

Maximum value of float = 3.4028234664e+38
Minimum value of float = 1.1754943508e-38

4. limits.h: 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-

C




// 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);
    printf("Minimum value of SHORT INT = %d\n",
           SHRT_MIN);
    printf("Maximum value of SHORT INT = %d\n",
           SHRT_MAX);
    printf("Minimum value of INT = %d\n",
           INT_MIN);
    printf("Maximum value of INT = %d\n",
           INT_MAX);
    printf("Minimum value of CHAR = %d\n",
           CHAR_MIN);
    printf("Maximum value of CHAR = %d\n",
           CHAR_MAX);
    printf("Minimum value of LONG = %ld\n",
           LONG_MIN);
    printf("Maximum value of LONG = %ld\n",
           LONG_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
Minimum value of SHORT INT = -32768
Maximum value of SHORT INT = 32767
Minimum value of INT = -2147483648
Maximum value of INT = 2147483647
Minimum value of CHAR = -128
Maximum value of CHAR = 127
Minimum value of LONG = -9223372036854775808
Maximum value of LONG = 9223372036854775807

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

C




// C program to implement
// the above approach
#include <stdio.h>
#include <time.h>
#define SIZE 256
 
// Driver code
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

Sun May 30 17:27:47 2021
Today is Sunday, May 30.
The time is 05:27 PM.

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

Example 1: strcat(): In C programming, the strcat() functions are used to concatenate(join) two strings. This function concatenates the destination string and the source string, and the result is stored in the destination string.

Syntax- 

char *strcat(char *destination, const char *source)

Below is the C program to implement strcat():

C




// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>
 
// Driver code
int main()
{
    char str1[100] = "Geeks ",
         str2[100] = " For Geeks";
 
    // Concatenates str1 and str2
    strcat(str1, str2);
 
    // Resultant string is stored
    // in str1
    puts(str1);
 
    return 0;
}


Output

Geeks  For Geeks

Example 2- strcmp(): It compares two strings.  If the return value is 0 then the strings are equal or if the return value is non-zero then the strings are not equal.

Syntax:

int strcmp (const char* str1, const char* str2);

 Below is the C program to implement strcmp():

C




// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>
 
// Driver code
int main()
{
    char str1[] = "Geeks",
         str2[] = "gEeks",
         str3[] = "Geeks";
    int result;
 
    // Comparing strings str1
    // and str2
    result = strcmp(str1, str2);
 
    printf("strcmp(str1, str2) = %d\n",
           result);
 
    // Comparing strings str1 and str3
    result = strcmp(str1, str3);
 
    printf("strcmp(str1, str3) = %d\n",
           result);
    return 0;
}


Output

strcmp(str1, str2) = -32
strcmp(str1, str3) = 0

Example 3 – strcpy(): The strcpy() function copies the string pointed by the source to the destination.

Syntax:

char* strcpy(char* destination, const char* source);

Below is the C program to implement the strcpy():

C




// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>
 
// Driver code
int main()
{
    char str1[20] = "Geeks For Geeks";
    char str2[20];
 
    // Copying str1 to str2
    strcpy(str2, str1);
 
    puts(str2);
    return 0;
}


Output

Geeks For Geeks

Example 4 – strlen(): This function calculates the length of the given string.

Syntax:

int strlen(char a[]);

 Below is the C program to implement strlen():

C




// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>
 
// Driver code
int main()
{
    char a[20] = "Program";
    char b[20] = { "Geeks for Geeks" };
    printf("Length of string a = %zu \n",
           strlen(a));
    printf("Length of string b = %zu \n",
           strlen(b));
    return 0;
}


Output

Length of string a = 7 
Length of string b = 15 

7. complex.h: 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.

Below is the C program to implement conjugate of a complex number- 

C




#include <complex.h>
#include <stdio.h>
 
// Driver code
int main(void)
{
    double real = 1.3,
        imag = 4.9;
    double complex z = real+imag*I;
    double complex conj_f = conjf(z);
    printf("z = %.1f + %.1fi\n",
        creal(conj_f),
        cimag(conj_f));
}


Output:

z = 1.3 - 4.9i

8. assert.h: Assertions are statements used to test assumptions made by programmers. For example, we may use an assertion to check if the pointer returned by malloc() is NULL or not.

Syntax-

void assert(int expression); 

C++




// C program to implement
// the above approach
#include <assert.h>
#include <stdio.h>
 
// Driver code
int main()
{
    int x = 7;
 
    // Some big code in between
    // and let's say x is accidentally
    // changed to 9
    x = 9;
 
    // Programmer assumes x to be 7
    // in rest of the code
    assert(x == 7);
 
    // Rest of the code
 
    return 0;
}


Output

Assertion failed: x==7, file test.cpp, line 13 
This application has requested the Runtime to terminate it in an unusual 
way. Please contact the application's support team for more information.


Last Updated : 05 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads