2.3. C Headers and Library Functions
Header File:
In C, a header is a file that contains declarations for functions, macros, and types, which are used in a program. Header files typically have a .h extension. The #include directive is used to include these header files in your C program.
Here are a few common C headers:
For example, if you want to use functions from the stdio.h header, you include it in your program like this: #include <stdio.h>
Library Functions:
Library functions are predefined functions provided by the C standard library and other libraries. These functions perform common tasks, and you can use them in your programs by including the appropriate headers.
For instance:
Library functions simplify programming by providing reusable and well-tested functionality. They are implemented in libraries, which are collections of object code that are linked with your program during the compilation process.
Here are some list of commonly used header files and their built-in libraries:
some commonly used headers along with examples of functions they declare.
C program that utilizes the math.h header for some basic mathematical operations:
#include <stdio.h>
#include <math.h>
int main() {
// Square root example
double number = 25.0;
double squareRootResult = sqrt(number);
printf("Square root of %.2f is: %.2f\n", number, squareRootResult);
// Power (exponential) example
double base = 2.0;
double exponent = 3.0;
double powerResult = pow(base, exponent);
printf("%.2f raised to the power %.2f is: %.2f\n", base, exponent, powerResult);
// Trigonometric functions example
double angleInRadians = 1.0; // 1 radian is approximately 57.2958 degrees
double sineResult = sin(angleInRadians);
double cosineResult = cos(angleInRadians);
printf("Sin(%.2f) is: %.2f\n", angleInRadians, sineResult);
printf("Cos(%.2f) is: %.2f\n", angleInRadians, cosineResult);
return 0;
}