enum vs. const vs. #define in C/C++



The #define, const, and Enum are all used for declaring constant values, but their behaviours and use cases differ. The #define is a preprocessor directive, which is used to define a macro (it is a string or name, in which you can assign a constant value), whereas const is a keyword with which you can declare a variable with a constant value. Whereas, an Enum is a special user-defined data type that represents a group of constants.

In the following article, we will learn about all three in detail.

Enumeration (Enum)

An Enumeration (or Enum) is a user-defined data type that is used to create a group of constants, which are internally represented as integral.

Syntax

Here is the general syntax for enumeration or Enum in C and C++.

enum enum_name {
    constant_value1,
    constant_value2,
    constant_value3,
    // So on

};

Here, enum is the keyword declaring an enumeration type.
enum_name is an Enum group name provided by the user.
constant_value1 is the value, which is internally represented as an integer starting from 0 and increments by value 1 and so on.

Example

Here is the following example code of enumeration in C and C++.

C C++
#include <stdio.h>

enum Weekday {
     // these were internally represented as integral values starting from 0
    Sunday,     // 0
    Monday,     // 1
    Tuesday,    // 2
    Wednesday,  // 3
    Thursday,   // 4
    Friday,     // 5
    Saturday    // 6
};

int main() {
    // declaring a variable of enum type and assigning value to it
    enum Weekday today;
    today = Wednesday;

    // this is to print the integer value of the enum
    printf("The value of Wednesday is: %d\n", today);

    // here using enum in a switch statement to print our desire output
    switch (today) {
        case Sunday:
            printf("It's Sunday, time to relax!\n");
            break;
        case Monday:
            printf("Back to work on Monday again.\n");
            break;
        case Wednesday:
            printf("It's midweek Wednesday.\n");
            break;
        default:
            printf("Just another weekday.\n");
    }

    return 0;
}

Output

The above program produces the following result:

The value of Wednesday is: 3
It's midweek Wednesday.
#include <iostream>
using namespace std;

enum Weekday {
    // these were internally represented as integral values starting from 0

    Sunday,     // 0
    Monday,     // 1
    Tuesday,    // 2
    Wednesday,  // 3
    Thursday,   // 4
    Friday,     // 5
    Saturday    // 6
};

int main() {
    Weekday today;
    today = Sunday;

    // printing the integer value of the enum
    cout << "The value of Sunday is: " << today << endl;

    // using enum in a switch statement to print our desire ouput depending on week days
    switch (today) {
        case Sunday:
            cout << "It's Sunday, time to relax!" << endl;
            break;
        case Monday:
            cout << "Back to work on Monday again." << endl;
            break;
        case Wednesday:
            cout << "It's midweek Wednesday." << endl;
            break;
        default:
            cout << "Just another weekday." << endl;
    }

    return 0;
}

Output

The above program produces the following result:

The value of Sunday is: 0
It's Sunday, time to relax!

The const keyword

A const is a keyword in C and C++, which is used to declare a variable as constant, which means you cannot change or update the value of that variable once it is initialized with const keyword.

Syntax

Here is the following syntax of const keyword, which is used to declare a value of variable as constant.

const data_type variable_name = value;

Example

Here is the following example code of const keyword in C and C++, where declaring the variable of PI as constant, as the value of PI is universal constant, and we want to prevent it from any further change or accidental modification.

C C++
#include <stdio.h>

int main() {
    // value of variable PI is declared as constant
    const float PI = 3.14159;

    float radius = 5.0;
    float area = PI * radius * radius;

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


    return 0;
}

Output

The above program produces the following result:

Radius: 5.00
Area of the circle: 78.54
#include <iostream>
using namespace std;

int main() {
    // here declaring a value of variable PI as constant
    const float PI = 3.14159;

    float radius = 7.2;
    float area = PI * radius * radius;

    cout << "Radius: " << radius << endl;
    cout << "Area of the circle: " << area << endl;

    return 0;
}

Output

The above program produces the following result:

Radius: 7.2
Area of the circle: 162.86

The define Preprocessor

The #define is a preprocessor directive, which is used to define macros. A macro is a name or identifier (not a regular variable) that stores a constant value, which is further used in a code. So wherever the macro appears in the code, it gets replaced by that constant value before compilation.

Basic Syntax for constants

Here is the following syntax of #define in C and C++.

#define MACRO_NAME value

Macro with Parameters

// like function
#define MACRO_NAME(parameters) expression

Example

Here is the following example code, showcasing the use of #define in C and C++, where we again took an example of calculating the area of a circle. Keeping the value of PI as constant and using define for constant and function.

C C++
#include <stdio.h>

// defining a macro
#define PI 3.14159
#define AREA_OF_CIRCLE(r) (PI * (r) * (r))

int main() {
    double radius = 3.5;

    double area = AREA_OF_CIRCLE(radius);

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

    return 0;
}

Output

The above program produces the following result:

Area of circle with radius 3.50 is: 38.48
#include <iostream>
using namespace std;

// defining a macro
#define PI 3.14159
#define AREA_OF_CIRCLE(r) (PI * (r) * (r))

int main() {
    double radius = 6.0;

    // Using the macro
    double area = AREA_OF_CIRCLE(radius);

    cout << "Area of circle with radius " << radius << " is: " << area << endl;

    return 0;
}

Output

The above program produces the following result:

Area of circle with radius 6 is: 113.097
Akansha Kumari
Akansha Kumari

Hi, I am Akansha, a Technical Content Engineer with a passion for simplifying complex tech concepts.

Updated on: 2025-06-16T17:23:14+05:30

607 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements