C Library Functions
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|
|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:
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()
double sqrt(double x)
Below is the C program to calculate the square root of any number:
Square root of 12.50 = 3.54
Example 2- pow():
double pow(double x, double y)
Below is the C program to calculate the power of any number:
10.0^2.0 = 100.00
Example 3- sin():
double sin(double x)
Below is the C program to calculate the sine of an argument:
sin(2.30) = 0.75 sin(-2.30) = -0.75 sin(0.00) = 0.00
Example 4- cos():
double cos(double x);
Below is the C program to calculate the cosine of an argument:
cos of 0.52 radian = 0.87
Example 5- tan():
double tan(double x);
Below is the C program to calculate the tangent of the argument:
tan(2.30) = -1.12 tan(-2.30) = 1.12
Example 6- log():
double log( double arg );
Below is the C program to calculate the natural logarithm of an argument-
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-
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-
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-
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.
char *strcat(char *destination, const char *source)
Below is the C program to implement strcat():
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.
int strcmp (const char* str1, const char* str2);
Below is the C program to implement strcmp():
strcmp(str1, str2) = -32 strcmp(str1, str3) = 0
Example 3 – strcpy(): The strcpy() function copies the string pointed by the source to the destination.
char* strcpy(char* destination, const char* source);
Below is the C program to implement the strcpy():
Geeks For Geeks
Example 4 – strlen(): This function calculates the length of the given string.
int strlen(char a);
Below is the C program to implement strlen():
Length of string a = 7 Length of string b = 15
Below is the C program to implement conjugate of a complex number-
z = 1.3 - 4.9i
void assert(int expression);
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.