C #define preprocessor

#include <stdio.h>

#define PI 3.14159
#define SQUARE(x) ((x) * (x))

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

    printf("The area of the circle with radius %f is %f\n", radius, area);

    return 0;
}
  • Explanation:

    • #include <stdio.h>: This line includes the standard input-output library in the C program, allowing the use of functions like printf and scanf.

    • #define PI 3.14159: This line defines a symbolic constant named PI with the value 3.14159. Whenever the compiler encounters PI in the code, it replaces it with the value 3.14159.

    • #define SQUARE(x) ((x) (x)): This line defines a macro named SQUARE that takes a single argument x and calculates the square of x using the expression ((x) (x)). Macros are expanded by the preprocessor, and here, the expression is enclosed in parentheses to handle cases where the argument x is an expression itself.

    • int main() { ... }: This is the starting point of the C program, where the main function is defined.

    • double radius = 5.0;: Declares a variable radius of type double and assigns it the value 5.0.

    • double area = PI * SQUARE(radius);: Calculates the area of a circle using the defined PI constant and the SQUARE macro. The result is stored in the variable area.

    • printf("The area of the circle with radius %f is %f\n", radius, area);: Prints the calculated area along with the radius using the printf function.

    • return 0;: Indicates the successful execution of the program. The value 0 is returned to the operating system.