Library Functions in C
Learn via video course

Inbuilt Functions / Standard Library Functions
Overview
C has many in-built functions which we can use in our program to make it efficient and readable. They are grouped in a header file. There are many header files in C. The library functions in C contain functions for input/output operations, error handling, mathematical operations, graphics, etc.
Scope of the Article
- This article explains what header files are in C.
- The article covers the different header files present in C.
- It covers the various library functions in C.
- It also discusses the advantages of using header files in C.
Introduction
C has many in-built functions which can make our work easier and code readable. Inbuilt functions are already defined in C and could be directly used in the program. These functions are grouped in a library, which can be accessed by including those header files in our program.
Header Files for Library Functions in C programming
C has many libraries with pre-defined functions in the form of header files. To use these functions in our code, we need to include these header files. Header files contain definitions of functions, macros, and datatypes which we could use directly in our program by including the respective header file. The syntax for including these header files in our program is as follows.
All the header files should have a ".h" extension. We can also have user-defined header files in C. We can create a file that contains the user-defined functions and save it with a ".h" extension.
Implementation of Library Functions in C
-
stdio.h: This is a standard input/output header file. This header file contains definitions of functions that perform input/output operations such as scanf, printf, gets, puts, etc.
-
printf: This function prints data on the screen.
Output
-
scanf: This function is used to read data from the user.
Input: 3 Output
-
-
math.h: This header file contains mathematical functions. It contains functions such as sqrt, pow, exp, log, sin, etc.
-
sqrt(): This function takes a double argument and returns the square root. It can be mathematically expressed as . Prototype
Example
Output
-
pow(): This function takes two arguments, the base and the exponent. It returns the value of the base raised to the exponent. It can be mathematically expressed as .
Prototype
Example
Output
-
sin(): This function returns the sine value of the argument. The argument should be expressed in radians. It can be expressed as .
Prototype
Example
Output
-
cos(): This function returns the cosine value of the argument. The argument should be expressed in radians. It can be mathematically expressed as .
Prototype
Example
Output
-
tan(): The function returns the tangent value of the argument. The argument should be expressed in radians. It can be mathematically as .
Prototype
Example
Output
-
log(): This function returns the logarithm value of the argument to the base e(natural logarithm). It can be mathematically expressed as .
Prototype
Example
Output
-
-
float.h: This header file contains a set of platform-dependent constants for floating-point values. The floating point values consist of four components.
Components Description Sign It can be either positive or negative. Base The base or radix of the exponent representation Exponent Exponent, an integer between a minimum and a maximum . Mantissa Precision, the number of digits in the series Floating-point value = *
Some of the macro definitions are as follows.
Note: FLT,DBL, LDBL stands for float, double, and long double data types, respectively. DIG implies digits, MANT implies mantissa, EXP implies exponent, and RADIX implies base.
Name Value Description FLT_RADIX 2 or greater Base of floating points FLT_DIG 6 or greater Number of decimal digits that can be rounded into a floating-point and back without change in the degree of precision. DBL_DIG/LDBL_DIG 10 or greater Number of decimal digits that can be rounded into a floating-point and back without change in the degree of precision. DECIMAL_DIG Number of decimal digits that can be rounded into a floating-point type and back again to the same decimal digits, without loss in precision. FLT_MANT_DIG/ DBL_MANT_DIG/ LDBL_MANT_DIG Number of digits in the mantissa or precision FLT_MIN_EXP/ DBL_MIN_EXP/ LDBL_MIN_EXP Minimum negative integer value for the exponent that generates a normalized floating-point number. FLT_MIN_10_EXP/ DBL_MIN_10_EXP/ LDBL_MIN_10_EXP -37 or smaller Minimum negative integer value for the exponent of a base-10 expression that would generate a normalized floating-point number. -
limits.h: This header file defines macros which define various properties related to various data types.
Macro Value Description CHAR_BIT 8 Number of bits in a char. CHAR_MAX 127 It is the maximum value for the char data type. CHAR_MIN -128 It is the minimum value for the char data type. INT_MAX 2147483647 It is the maximum value for the int data type. INT_MIN -2147483648 It is the minimum value for the int data type. LONG_MAX 9223372036854775807 It is the maximum value for the long int data type. LONG_MIN -9223372036854775808 It is the minimum value for the long int data type. SHRT_MAX 32767 It is the maximum value for the short int data type. SHRT_MIN -32768 It is the minimum value for the short int data type. SCHAR_MAX 127 It is the maximum value for the signed char data type. SCHAR_MIN -128 It is the minimum value for the signed char data type. UCHAR_MAX 255 It is the maximum value for the unsigned char data type. UINT_MAX 4294967295 It is the maximum value for the unsigned int data type. ULONG_MAX 18446744073709551615 It is the maximum value for the unsigned long int data type. USHRT_MAX 65535 It is the maximum value for the unsigned short int data type. -
time.h: This header file defines data types and functions to get and manipulate time and date information.
-
time(): This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds.
Prototype
Example
Output
-
Explanation
The struct holds information about the date and time. In the above example, we declare a variable timeval of time_t type and local_time of struct type. The time function returns the time passed in seconds since 00:00:00 UTC, January 1, 1970. The localtime function returns a variable of struct type holding information about the date and time of the user. The asctime function returns a pointer to the string that stores the local_time information stored in the structure local_time.
-
conio.h: It stands for Console Input Output Header file. It defines functions for formatting the output and getting input in the console.
-
clrscr(): It stands for clear screen. When clrscr() is called in a program, everything printed on the console is cleared.
Example
Output
-
getch(): This function holds the output screen until it reads a single character from the keyboard to exit the console screen. Prototype
Example
The console screen closes when it reads any character from the keyboard.
-
-
string.h: This header file is used to perform operations on the string. It contains functions such as strcpy, strcat, strcmp etc.
-
strcat: It appends one string at the end of another string. The second string argument is appended at the end of the first string argument.
Prototype
Example
Output
-
strcmp(): This function compares two strings. If the strings are equal, it returns zero. If they are not equal, it returns a non-zero value. If two strings are equal, they have the same length and characters at each index in both strings should be the same.
Prototype
Example
Output
-
strcpy(): This function copies the second string argument in the first string argument.
Prototype
The size of the string1 should be large enough to store than the entire string2 or the function will show undefined behaviour. Example
Output
-
strlen(): This function takes a string as an argument and returns the length of the string. The return type is size_t(unsigned integer).
Prototype
Example
Output
-
-
complex.h: This header file contains data types for declaring complex and imaginary numbers and various functions which can be performed on them. This header file was added to the C99 standard.
The data types are double complex, long complex, float complex, double imaginary, long imaginary and float imaginary. Let us see some examples to understand it better.
Output
Explanation The creal function returns the real part of the complex number. The cimag function returns the imaginary part of the complex number. We can perform addition, subtraction, multiplication and division on complex numbers as shown above. The conj function returns the conjugate of the complex number. The cabsf function returns the absolute value of a complex number. The phase angle of a complex number is given by the cargf function.
-
assert.h: This header file defines a macro assert.
Assertions are the assumptions made by the C program. The macro assert is used to verify the assumptions made by the C program. If the assumption is wrong, the execution of the program stops and a diagnostic(error) message is printed.
Prototype
Example
Output
Explanation
After coming out of the while loop the value of a is 1. So the expression(a!=1) is not true. Therefore the execution of the program stops and an error message is printed.
-
stdlib.h: This is the general purpose standard library header file. It includes functions for type conversion(atof,atoi,etc), memory allocation and deallocation(malloc,calloc,free,etc), pseudo-random sequence generation(rand,srand,etc), process control(abort,exit,etc), sorting(qsort,bsearch), mathematics(abs, div,etc) etc.
-
malloc(): This function dynamically allocates a block of memory of the specified size and returns the address of that memory location. The allocated memory is not initialized with any value and contains default garbage values.
If it fails to allocate the specified memory size, it returns a null pointer.
Prototype
Example
Output
-
calloc(): This function allocates a block of memory of the specified size and returns the address of the allocated memory block. If the memory allocation fails, it returns a null pointer.
There are two differences between calloc and malloc: * calloc initializes the memory location with the value 0, while malloc doesn't initialize it with any value. * calloc takes two arguments, first number of items and second size of each item. malloc takes just one argument the total size of the memory.
Prototype
Example
Output
Since malloc is faster than calloc, it is better to use malloc for allocating memory. We should use calloc if we want the allocated memory to be initialised be zero rather than default garbage values.
-
-
ctype.h: This header file defines functions that are used to check or transform characters. Some of the functions included in this header file are isprintf(checks if a character is printable), isalnum(checks if a character is alphanumeric), etc.
The functions take an unsigned integer as an argument. The integer is the ASCII value of the character. When we pass a character as the argument, it is converted to its ASCII value for computation. It returns an unsigned integer. If this integer is non-zero it means the argument satisfies the condition and zero if it doesn't.
-
isalpha(): This function checks if the character is an alphabet. It returns a non-zero value if it is an alphabet and zeroes otherwise.
Prototype
Example
Output
From the above example, we can see the function returns a non-zero value for a-z and A-Z and 0 otherwise.
-
islower(): This function checks if the character is a lowercase alphabet. It returns a non-zero number if it is a lower-case alphabet and zero otherwise. The ASCII value of 'a' is 97 and 'z' is 122. So it will return a non-zero number for any argument between 97 and 122.
Prototype
Example
Output
-
-
setjmp.h: This header file defines a macro setjmp(), a function longjmp() and a variable type jmp_buf. These are used to provide control flow that deviates from normal subroutine call and return sequence. It is used to implement exception handling in C.
-
setjmp(): This macro saves env with information about the current environment, which is later used by longjmp(). The macro may return more than once. On direct invocation, it returns zero. If the macro returns from a longjmp() call, it returns the value passed to longjmp as the second argument.
Prototype
-
longjmp(): This function restores the environment as stated by env and then evaluates the setjmp expression which filled env as value.
It doesn't return anything. It transfers the control to the point where setjmp was last used to fill the env and evaluates the whole expression as val(if val=0 it is evaluated as 1).
Prototype
Example
Output
Explanation
The setjmp macro returns more than once. On direct invocation, it returns zero. When longjmp is called with buf set to the environment, the macro returns. This time it returns the value passed to longjmp as the second argument i.e. 1.
-
-
signal.h: This header file defines how a function handles signal while it executes.
-
signal(): This function sets a function in C to handle a signal. It does not return anything.
Prototype
The parameter sig is the signal number to which a handling function is set. Some important signal numbers are as follows.
Macro Description SIGABRT Abnormal program termination. SIGFPE Floating-point error like division by zero. SIGILL Illegal operation. SIGINT Interrupt signal such as ctrl-C. SIGSEGV Invalid access to storage like segment violation. SIGTERM Termination request. The second argument func is a pointer to a function. It can be user-defined or one of the following.
Function Description SIG_DFL Default handling − The signal is handled by the default action for that particular signal. SIG_IGN Ignore Signal − The signal is ignored. Example
Output
When this program is executed it prints an infinite number of "Scalar Academy" until it receives a signal i.e. the process stops when we press Ctrl+C keys.
-
raise(): This function sends the signal sig to the program.
Prototype
Example
Output
-
-
locale.h: This header file defines location specific settings, such as date formats, currency etc.
-
setlocale(): This function sets or reads locale information for the current program. Prototype
Example
Output
It returns a pointer to the string which corresponds to the locale set. If the request cannot be processed, it returns a null pointer.
-
-
stdarg.h: This header file allows functions to accept an indefinite number of arguments. This concept is used in variadic functions which are functions that accept a variable number of arguments.
-
va_list: It is the pre-defined data type in stdarg.h header file. The variable of this data type represents the list of the arguments the function has.
-
va_start(): It is used to start the iteration of the function arguments of type va_list. The arg_ptr points to the list of arguments. The variable_name is the fixed argument passed into the function.
Prototype
-
va_arg()
is used to retrieve an argument. It returns the value of one argument and modifies arg_ptr to point to the next argument in the list of arguments. Prototype
-
va_end(): It is used to stop retrieving the function arguments. This macro is invoked before the function returns when va_start is invoked in the function. Prototype
Example
Output
-
Explanation
We define a solve function which takes variable arguments. The first argument of the function specifies the number of additional arguments. This function returns the highest number from the input numbers.
-
errno.h: This header file defines macros for reporting and retrieving error conditions using the symbol errno (short for "error number").
The global variable "errno" is assigned a code(number) that can be used to identify the type of error in our program.
Some of the error codes are as follows.
Error Code Description 1 Operation not permitted 2 No such file or directory 3 No such process 4 Interrupted system call 5 I/O error 6 No such device or address -
graphics.h: This header file defines functions to create graphics such as geometrical shapes, simple animation etc.
-
circle: This function draws a circle of a given radius taking (x,y) as the centre.
Prototype
-
rectangle: This function draws a rectangle. It takes four arguments. The first two arguments define the X and Y coordinates of the left top corner and the last two coordinates define the X and Y coordinates of the right bottom corner.
Prototype
-
Advantages of library functions in C
- We need our program to perform some operations. To perform that operation, we need to create various datatypes, macros and functions. The advantage of using inbuilt library functions in C are that we already have many of the functions we need pre-defined. This reduces the work of the programmer.
- It also makes code more efficient as the implementation of the pre-defined function may be more efficient than many of us can write.
- It makes the code more readable by separating the definition of functions from the actual code.
Conclusion
- Inbuilt functions are those which are already defined in C.
- These functions are grouped in header files.
- There are many header files in C.
- They contain functions that are used to perform input/output operations, mathematical operations, error handling, graphics etc.