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 likeprintf
andscanf
.#define PI 3.14159
: This line defines a symbolic constant namedPI
with the value 3.14159. Whenever the compiler encountersPI
in the code, it replaces it with the value 3.14159.#define SQUARE(x) ((x) (x))
: This line defines a macro namedSQUARE
that takes a single argumentx
and calculates the square ofx
using the expression((x) (x))
. Macros are expanded by the preprocessor, and here, the expression is enclosed in parentheses to handle cases where the argumentx
is an expression itself.int main() { ... }
: This is the starting point of the C program, where themain
function is defined.double radius = 5.0;
: Declares a variableradius
of typedouble
and assigns it the value 5.0.double area = PI * SQUARE(radius);
: Calculates the area of a circle using the definedPI
constant and theSQUARE
macro. The result is stored in the variablearea
.printf("The area of the circle with radius %f is %f\n", radius, area);
: Prints the calculated area along with the radius using theprintf
function.return 0;
: Indicates the successful execution of the program. The value 0 is returned to the operating system.