The Standard Function Library in C is a collection of predefined functions provided by the C standard library. These functions are organized into different header files based on their purpose.
- Each header file contains function declarations (prototypes) for a specific group of functions.
- To use a library function, include its corresponding header file using the
#includedirective. The function definitions are stored in separate library files and are linked during compilation.
Some commonly used C header files are:
| Header Files | Description |
|---|---|
| <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. |
| <complex.h> | A set of functions for manipulating complex numbers. |
| <float.h> | Defines macro constants specifying the implementation-specific properties of the floating-point library. |
| <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. |
| <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. |
| <stdio.h> | The stdio.h header defines three variable types, several macros, and various function for performing input and output. |
| <time.h> | Defines date and time handling functions. |
| <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:
#include <stdio.h>
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)
The C program to calculate the square root of any number is:
#include <math.h>
#include <stdio.h>
int main()
{
double number, squareRoot;
number = 12.5;
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)
The C program to calculate the power of any number is:
#include <math.h>
#include <stdio.h>
int main()
{
double base, power, result;
base = 10.0;
power = 2.0;
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)
The C program to calculate the sine of an argument is:
#include <math.h>
#include <stdio.h>
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);
The C program to calculate the cosine of an argument is:
#include <math.h>
#include <stdio.h>
#define PI 3.141592654
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);
The C program to calculate the tangent of the argument is:
#include <math.h>
#include <stdio.h>
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 );
The C program to calculate the natural logarithm of an argument is:
#include <math.h>
#include <stdio.h>
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.
The C program to implement the above approach is:
#include <float.h>
#include <stdio.h>
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.
The C program to implement the above approach is:
#include <limits.h>
#include <stdio.h>
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
time.h header file defines the date and time functions.
The C program to implement time() and localtime() functions is:
#include <stdio.h>
#include <time.h>
#define SIZE 256
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() 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)
The C program to implement strcat() is:
#include <stdio.h>
#include <string.h>
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() 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);
The C program to implement strcmp() is:
#include <stdio.h>
#include <string.h>
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() function copies the string pointed by the source to the destination.
Syntax:
char* strcpy(char* destination, const char* source);The C program to implement the strcpy() is:
#include <stdio.h>
#include <string.h>
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() function calculates the length of the given string.
Syntax:
int strlen(char a[]);
The C program to implement strlen() is:
#include <stdio.h>
#include <string.h>
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
complex.h header file are used to perform various operations on complex numbers. Complex numbers are the ones with the real and imaginary parts.
The C program to implement conjugate of a complex number is:
#include <complex.h>
#include <stdio.h>
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.9i8. 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);
#include <assert.h>
#include <stdio.h>
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.