Welcome to the exciting world of C programming! In this lesson, we'll delve into the C Standard Library – a powerful collection of pre-built functions that make your coding journey more efficient and practical.
The C Standard Library is a set of functions provided by the C language to perform various operations such as input/output, string manipulation, math calculations, and more. These functions are a part of the C language specification and are available across different C compilers.
char: Represents a single character.int: Represents a 32-bit signed integer.float: Represents a single-precision floating-point number.double: Represents a double-precision floating-point number.void: Represents the absence of a value.printf(): Prints formatted output to the standard output (the terminal).scanf(): Reads formatted input from the standard input (the keyboard).pow(): Raises a number to a power.sqrt(): Calculates the square root of a number.strlen(): Returns the length of a string.strcpy(): Copies the string from the source to the destination.strcmp(): Compares two strings lexicographically.Let's create a simple C program that calculates the square root of a number using the sqrt() function.
#include <stdio.h>
#include <math.h>
int main() {
double number, result;
printf("Enter a number: ");
scanf("%lf", &number);
result = sqrt(number);
printf("The square root of %.2lf is %.2lf\n", number, result);
return 0;
}In this example, we include the math header <math.h> to use the sqrt() function. We then ask the user for a number, calculate its square root, and print the result.
Which header file should you include to use the `sqrt()` function?