C Standard library functions or simply C Library functions are inbuilt functions in C programming.
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,
If you want to use the printf()
function, the header file <stdio.h>
should be included.
#include <stdio.h>
int main()
{
printf("Catch me if you can.");
}
If you try to use printf()
without including the stdio.h
the header file, you will get an error.
Advantages of Using C library functions
1. They work
One of the most important reasons you should use library functions is simply because they work. These functions have gone through multiple rigorous testing and are easy to use.
2. The functions are optimized for performance
Since, the functions are “standard library” functions, a dedicated group of developers constantly make them better. In the process, they are able to create the most efficient code optimized for maximum performance.
3. It saves considerable development time
Since the general functions like printing to a screen, calculating the square root, and many more are already written. You shouldn’t worry about creating them once again.
4. The functions are portable
With ever-changing real-world needs, your application is expected to work every time, everywhere. And, these library functions help you in that they do the same thing on every computer.
Example: Square root using sqrt() function
Suppose, you want to find the square root of a number.
To can compute the square root of a number, you can use the sqrt() library function. The function is defined in the math.h
header file.
#include <stdio.h>
#include <math.h>
int main()
{
float num, root;
printf("Enter a number: ");
scanf("%f", &num);
// Computes the square root of num and stores in root.
root = sqrt(num);
printf("Square root of %.2f = %.2f", num, root);
return 0;
}
When you run the program, the output will be:
Enter a number: 12 Square root of 12.00 = 3.46
Library Functions in Different Header Files
C Header Files | |
---|---|
<assert.h> | Program assertion functions |
<ctype.h> | Character type functions |
<locale.h> | Localization functions |
<math.h> | Mathematics functions |
<setjmp.h> | Jump functions |
<signal.h> | Signal handling functions |
<stdarg.h> | Variable arguments handling functions |
<stdio.h> | Standard Input/Output functions |
<stdlib.h> | Standard Utility functions |
<string.h> | String handling functions |
<time.h> | Date time functions |
The C <ctype.h> header file declares a set of functions to classify (and transform) individual characters. For example, isupper() checks whether a character is uppercase or not.
C isalnum()
checks alphanumeric character
C isalpha()
checks whether a character is an alphabet or not
C iscntrl()
checks control character
C isdigit()
checks numeric character
C isgraph()
checks graphic character
C islower()
checks lowercase alphabet
C isprint()
checks printable character
C ispunct()
checks punctuation
C isspace()
check white-space character
C isupper()
checks uppercase alphabet
C isxdigit()
checks hexadecimal digit character
C tolower()
converts alphabet to lowercase
C toupper()
converts to lowercase alphabet
The C <math.h>
header file declares a set of functions to perform mathematical operations such as: sqrt()
to calculate the square root, log()
to find natural logarithm of a number etc.
C atan()
computes the arc tangent of an argument
C atan2()
computes the arc tangent of an argument.
C atanh()
computes arc hyperbolic tangent
C cbrt()
computes cube root of a number
C ceil()
computes the nearest integer greater than argument
C cos()
computes the cosine of an argument.
C cosh()
computer hyperbolic cosine.
C exp()
computes the exponential raised to the argument
C fabs()
computes absolute value
C floor()
calculates the nearest integer less than argument
C hypot()
computes hypotenuse
C log()
computes natural logarithm of an argument.
C log10()
computes the base 10 logarithm of an argument.
C pow()
Computes power of a number
C sin()
compute sine of a number
C sinh()
computes the hyperbolic sine of an argument.
C sqrt()
computes square root of a number
C tan()
computes tangent
C tanh()
computes the hyperbolic tangent of an argument
The C <string.h>
header file declares a set of functions to work strings.
C strcat()
Concatenates two strings
C strcmp()
compares two strings
C strcpy()
copies string
C strlen()
calculates the length of a string