C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

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:

  • stdio.h: Provides input and output functions like printf and scanf.
  • stdlib.h: Includes functions involving memory allocation and program termination, such as malloc and exit.
  • math.h: Contains mathematical functions like sqrt, sin, cos, etc.
  • string.h: Includes functions for manipulating strings, such as strcpy, strlen, etc.

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:

  • printf() and scanf(): Functions for formatted input and output, declared in stdio.h.
  • malloc() and free(): Functions for dynamic memory allocation and deallocation, declared in stdlib.h.
  • sqrt(), sin(), cos(): Mathematical functions for square root, sine, and cosine, declared in math.h.

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.

  • stdio.h (Standard Input/Output):
    • printf(): Prints formatted output.
    • scanf(): Reads formatted input.
    • fopen(), fclose(): Opens and closes files.
    • fgets(), fputs(): Reads and writes strings to files.
  • stdlib.h (Standard Library):
    • malloc(), calloc(), free(): Dynamic memory allocation and deallocation.
    • exit(): Exits the program.
    • rand(), srand(): Generates random numbers.
  • string.h (String Manipulation):
    • strlen(): Calculates the length of a string.
    • strcpy(), strcat(), strcmp(): String manipulation functions.
  • math.h (Mathematical Functions):
    • sqrt(), pow(): Square root and power functions.
    • sin(), cos(), tan(): Trigonometric functions.
    • fabs(): Absolute value of a floating-point number.
  • ctype.h (Character Functions):
    • isalpha(), isdigit(): Checks if a character is alphabetic or numeric.
    • toupper(), tolower(): Converts characters to uppercase or lowercase.
  • time.h (Time and Date Functions):
    • time(): Returns the current time.
    • asctime(), localtime(): Converts time to string representation.
  • stdbool.h (Boolean Type):
    • bool: Boolean type (introduced in C99).
    • true, false: Boolean values.

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;
}