c
  1. c-define

C #define

Syntax

#define name value

Example

#include <stdio.h>

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

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

    printf("Area of a circle with radius %f: %f", radius, area);
    return 0;
}

Output

Area of a circle with radius 5.0: 78.53975

Explanation

C #define is a preprocessor directive that allows you to define constants or create macros. When the preprocessor encounters a #define statement in the source code, it replaces all instances of the defined name with the corresponding value.

Use

The #define directive is often used to define constants, such as pi or the number of seconds in a minute, or to create macros, such as a function that squares a number. By using #define to define a constant, you can make your code more readable and maintainable by giving meaningful names to the constants. Macros created with the #define directive can simplify your code by reducing duplication and making code easier to read.

Important Points

  • #define is a preprocessor directive, meaning it is executed before the compilation process starts.
  • #define can be used to define constants or create macros.
  • Constants defined with #define cannot be changed during the execution of the program.
  • Macros created with #define are useful for performing repetitive tasks.

Summary

C #define is a preprocessor directive that allows you to define constants or create macros. Constants defined with #define cannot be changed during the execution of the program. Macros created with #define are useful for performing repetitive tasks. By defining constants or creating macros, you can make your code more readable, maintainable, and easier to debug.

Published on: