The Wayback Machine - https://web.archive.org/web/20240919095050/https://www.geeksforgeeks.org/c-library-functions/
Open In App

C Library Functions

Last Updated : 20 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 actual definitions of these functions are stored in separate library files, and declarations in 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 FilesDescription
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.h– To 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.


Similar Reads

Check if two strings are same or not without using library functions
Given two strings S1 and S2, the task is to check whether they are the same or not without using string library functions. Examples: Input: S1 = ”GeeksForGeeks”, S2 = ”GeeksForGeeks”Output: TrueExplanation:S1 and S2 are the same strings Input: S1 = ”GeeksForGeeks”, S2 = ”GeeksforGeeks”Output: False Approach: Follow the steps below to solve the prob
5 min read
Hash Functions and Types of Hash functions
Hash functions are a fundamental concept in computer science and play a crucial role in various applications such as data storage, retrieval, and cryptography. In data structures and algorithms (DSA), hash functions are primarily used in hash tables, which are essential for efficient data management. This article delves into the intricacies of hash
4 min read
C Library Function - difftime()
The difftime() is a C Library function that returns the difference in time, in seconds(i.e. ending time - starting time). It takes two parameters of type time_t and computes the time difference in seconds. The difftime() function is defined inside the &lt;time.h&gt; header file. Syntax The syntax of difftime() function is as follows: double difftim
1 min read
wcstof function in C library
The wcstof() functions convert the initial portion of the wide-character string pointed to by str to a float point value. The str parameter points to a sequence of characters that can be interpreted as a numeric floating-point value. These functions stop reading the string at the first character that it cannot recognize as part of a number i.e. if
2 min read
wprintf() and wscanf in C Library
If you are dealing with the wide characters then printf() and scanf() function cannot be used. There are different functions for input and output C wide string. wprintf() : The wprintf() function writes the wide string pointed to by format to stdout. The wide string format may contain format specifiers starting with % which are replaced by the valu
3 min read
Difference between Header file and Library
Header Files: The files that tell the compiler how to call some functionality (without knowing how the functionality actually works) are called header files. They contain the function prototypes. They also contain Data types and constants used with the libraries. We use #include to use these header files in programs. These files end with .h extensi
3 min read
Pattern Searching using C++ library
Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function that prints all occurrences of pat[] in txt[]. You may assume that n &gt; m.Examples: Input : txt[] = "geeks for geeks" pat[] = "geeks" Output : Pattern found at index 0 Pattern found at index 10 Input : txt[] = "aaaa" pat[] = "aa" Output : Pattern found at index 0 Pattern found a
3 min read
SDL library in C/C++ with examples
SDL is Simple DirectMedia Layer.It is a cross-platform development library designed to provide low level access to audio, keyboard, mouse, joystick, and graphics hardware via OpenGL and Direct3D.It can be used to make animations and video games. It basically provides a set of APIs to interact with various devices like graphics hardware, audio, keyb
4 min read
ctype.h(&lt;cctype&gt;) library in C/C++ with Examples
As string.h header file contains inbuilt functions to handle Strings in C/C++, the ctype.h/&lt;cctype&gt; contains inbuilt functions to handle characters in C/C++ respectively. Characters are of two types: Printable Characters: The characters that are displayed on the terminal. Control Characters: The characters that are initiated to perform a spec
5 min read
Audiere Library
Audiere is a high-level Audio API developed by Chad Austin and Matthew Campbell. It was released on 6 August 2001. It supports direct sound in Windows as well as in Linux. It can play sounds in formats like WAV, MP3, as well as any formats. It can be used in many languages like C, C++, Java, etc. Installing Audiere: For Linux/Windows: For setting u
3 min read
free() Function in C Library With Examples
The free() function in C is used to free or deallocate the dynamically allocated memory and helps in reducing memory wastage. The C free() function cannot be used to free the statically allocated memory (e.g., local variables) or memory allocated on the stack. It can only be used to deallocate the heap memory previously allocated using malloc(), ca
3 min read
C Library Function - putc()
In C, the putc() function is used to write a character passed as an argument to a given stream. It is a standard library function defined in the &lt;stdio.h&gt; header file. The function first converts the character to unsigned char, then writes it to the given stream at the position indicated by the file pointer, and finally increments the file po
4 min read
C Library - exp() Function
In C programming language, the exp() function is a built-in function of &lt;math.h&gt; library which is used to calculate the value of e (Euler's Number = 2.71828) raised to the power x where x is any real number. Syntax: double exp(double x);Arguments: This function takes only one argument of type double.Return Type: This function returns the valu
4 min read
C Library - &lt;string.h&gt;
string.h is a standard header file in the C language that contains functions for manipulating strings (arrays of characters). &lt;string.h&gt; header file contains some useful string functions that can be directly used in a program by invoking the #include preprocessor directive. Syntax: #include &lt;string.h&gt; Example: C/C++ Code // C program to
3 min read
C Library - math.h
The &lt;math.h&gt; header file in C contains the standard math library functions which can be utilized for various mathematical operations. All math.h library functions accept double type as an argument and return double as a result. It also contains some commonly used constants defined as macros. Syntax: #include &lt;math.h&gt;After including the
4 min read
How to create popup message using Alerter Library in android
In this article, we learn about how to create a popup message with the help of Alerter Library. It is better to use Alerter than using Toast or Snackbar in cases if some alert messages are to be displayed to the user. We can add various onClickListners to our alerter message which makes it better and it also has nice appealing UI. Approach: Add the
2 min read
How to print range of basic data types without any library function and constant in C?
How to write C code to print range of basic data types like int, char, short int, unsigned int, unsigned char etc? It is assumed that signed numbers are stored in 2's complement form. We strongly recommend to minimize the browser and try this yourself first. Following are the steps to be followed for unsigned data types. 1) Find number of bytes for
4 min read
Algorithm Library | C++ Magicians STL Algorithm
For all those who aspire to excel in competitive programming, only having a knowledge about containers of STL is of less use till one is not aware what all STL has to offer. STL has an ocean of algorithms, for all &lt; algorithm &gt; library functions : Refer here.Some of the most used algorithms on vectors and most useful one's in Competitive Prog
7 min read
Java program to check palindrome (using library methods)
Given a string, write a Java function to check if it is palindrome or not. A string is said to be palindrome if reverse of the string is same as string. For example, “abba” is palindrome, but “abbc” is not palindrome. The problem here is solved using string reverse function. Examples: Input : malayalam Output : Yes Input : GeeksforGeeks Output : No
1 min read
How to add "graphics.h" C/C++ library to gcc compiler in Linux
While trying c graphic programming on Ubuntu, I figured out that graphic.h is not a standard C library and it is not supported by gcc compiler. So I am writing this article to explain the process.If you want to use graphics.h on Ubuntu platform you need to compile and install libgraph. It is the implementation of turbo c graphics API on Linux using
2 min read
isgraph() C library function
The C library function isgraph() checks whether a character is a graphic character or not. Characters that have graphical representation are known are graphic characters. For example: ':' ';' '?' '@' etc. Syntax - #include &lt;ctype.h> int isgraph(int ch); Return Value - function returns nonzero if ch is any printable character other than a space,
2 min read
Algorithm for JSON Formatter without using external library
JSON FormatterGiven an input string representing a JSON object, print a string denoting the JSON object with the given format and indentation. Formatting rule :Every opening brace should increase one indentation for the following lines.Every close brace should decrease one indentation for the same line and the following lines.Examples :Input: {"use
10 min read
E -Library Management System
In this article, we will discuss the approach to creating an E-Library Management System where the user has the following options: Add book information.Display book information.To list all books of a given author.To list the count of books in the library. Functionalities Required:If the user tries to add a book then the user must have to provide th
10 min read
How to Create a Dynamic Library in C?
In C, a dynamic library is a library that is loaded at runtime rather than compile time. We can create our own dynamic libraries and use them in our programs. In this article, we will learn how to create a dynamic library in C. Example: Input:Hello, geeksforgeek!Output:Hello, geeksforgeek!Creating a Dynamic Library in CTo create a dynamic library i
2 min read
How Do I Create a Library in C?
Libraries are a collection of code that can be used by other programs. They promote code reuse and modularity. In C, we can create our own libraries. In this article, we will learn how to create a library in C. Creating a Library in CIn C, there are multiple methods using which we can create a library.: Static LibraryDynamic LibraryIn this article,
3 min read
How to Create a Static Library in C?
In C, we can create our own libraries that contains the code for the functions or macros that we may need very often. These libraries can be then used in our programs. In this article, we will learn how to create a custom static library and use it with GCC compiler. What is a Static Libraries in C?A static library is a collection of object files th
2 min read
Write one line functions for strcat() and strcmp()
Recursion can be used to do both tasks in one line. Below are one line implementations for stracat() and strcmp(). /* my_strcat(dest, src) copies data of src to dest. To do so, it first reaches end of the string dest using recursive calls my_strcat(++dest, src). Once end of dest is reached, data is copied using (*dest++ = *src++)? my_strcat(dest, s
2 min read
How to write C functions that modify head pointer of a Linked List?
Consider simple representation (without any dummy node) of Linked List. Functions that operate on such Linked lists can be divided into two categories: 1) Functions that do not modify the head pointer: Examples of such functions include, printing a linked list, updating data members of nodes like adding given a value to all nodes, or some other ope
4 min read
C | Functions | Question 11
Output of following program? #include &lt;stdio.h&gt; int main() { int i = 5; printf(&quot;%d %d %d&quot;, i++, i++, i++); return 0; } (A) 7 6 5 (B) 5 6 7 (C) 7 7 7 (D) Compiler Dependent Answer: (D) Explanation: When parameters are passed to a function, the value of every parameter is evaluated before being passed to the function. What is the orde
1 min read
C | Functions | Question 2
In C, parameters are always (A)Passed by value (B)Passed by reference (C)Non-pointer variables are passed by value and pointers are passed by reference (D)Passed by value result Answer: (A)Explanation: In C, function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values. Quiz of this Questio
1 min read
Practice Tags :