C Programming

⌘K
  1. Home
  2. Docs
  3. C Programming
  4. Basic Elements of C
  5. Library Functions and Preprocessor Directives in C

Library Functions and Preprocessor Directives in C

Library functions are predefined functions provided by the C standard library that perform common tasks such as input/output operations, string handling, mathematical calculations, memory management, etc.

  • These functions save programmers from writing common functionalities from scratch.

Examples of Common Library Functions:

Common Library Functions in c

Example Using Library Functions:

#include <stdio.h>
#include <string.h>
#include <math.h>

int main() {
    char str1[20] = "Hello";
    char str2[20] = "World";

    // Using string library function strcat to concatenate strings
    strcat(str1, " ");
    strcat(str1, str2);

    printf("Concatenated string: %s\n", str1);

    // Using math library function pow to calculate power
    double result = pow(2.0, 3.0);
    printf("2 raised to power 3 is %.0f\n", result);

    return 0;
}

Preprocessor directives are instructions for the C preprocessor, which runs before the actual compilation of the program.

  • They are used to include files, define macros, conditionally compile code, and more.
  • Preprocessor directives always start with the # symbol and do not end with a semicolon.

Common Preprocessor Directives:

Common Preprocessor Directives in c

Example Using Preprocessor Directives:

#include <stdio.h>

#define PI 3.14159

int main() {
    float radius = 5.0;
    float area = PI * radius * radius;

    printf("Area of circle: %.2f\n", area);

    return 0;
}

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *