“static const” vs “#define” vs “enum” - GeeksforGeeks

25 Oct, 2018
In this article, we will be analyzing “static const”, “#define” and “enum”. These three are often confusing and choosing which one to use can sometimes be a difficult task.
static const
static const : “static const” is basically a combination of static(a storage specifier) and const(a type qualifier).
Static : determines the lifetime and visibility/accessibility of the variable. This means if a variable is declared as a static variable, it will remain in the memory the whole time when the program is running, while the normal or auto variables are destroyed when the function (where the variable was defined) is over.Const : is a type qualifier. A type qualifier is used to express additional info about a value through type system. When a variable is initialized using the const type qualifier, it will not accept further change in its value.So combining static and const, we can say that when a variable is initialized using static const, it will retain its value till the execution of the program and also, it will not accept any change in its value.
Syntax:
static const data_type name_of_variable = initial_value;

// C++ program to demonstrate the use of// static const  #include <bits/stdc++.h>  using namespace std;// function to add constant value to inputint addConst(int input){    // value = 5 will be stored in    // memory even after the    // execution of the    // function is finished    static const int value = 5;      // constant_not_static will    // not accept change in value    // but it will get destroyed    // after the execution of    // function is complete    const int constant_not_static = 13;    input += value;      // value++; ==> this statement will produce error    return input;}  int main(){    int input = 10;    cout << addConst(input) << endl;      return 0;}
15

“What is #define“?
It is often misinterpreted as a programming statement. But it is actually sets up a macro. A macro causes a text to replace before compilation takes place. To know more about macros refer to macros_vs_function article.
Syntax:
#define token [value]

NOTE: token should not have any spaces, value can have spaces.Example:
 #define ll long long int
// C++ program to demonstrate// the use of #define  #include <bits/stdc++.h>  // defining long long int as => ll#define ll long long int  // defining for loop#define f(i, a, b, c) for (ll i = a; i < b; i += c)using namespace std;  // function to count to a given numbervoid count(ll input){    // loop implemented using macros    // for(long long int j=1; j<input+1;j+=1)    f(j, 1, input + 1, 1)    {        cout << j << " ";    }    cout << endl;}  int main(){    // ll will get replaced by    // long long int    ll num = 10;    count(num);      return 0;}
1 2 3 4 5 6 7 8 9 10

What is enum?
Enumeration is a user-defined data type. It is used to assign names to integral constants to improve code readability. To use enumeration “enum” keyword is used in C/C++.
Syntax:
enum flag{constant1= 2, constant2=3, constant3=4....};

What makes “enum” different from “#define” is that it automatically assigns values to the variables. In the previous example if the values were not assigned=>
enum{constant1, constant2, constantd3...}

The variables will be assigned the values automatically(constant1= 0, constant2= 1, constant3= 2...). There are various advantages of using enum instead of macros. One of them is automatic assignment of values.
// C++ program to demonstrate// the use of enum  #include <bits/stdc++.h>  using namespace std;  int main(){    // declaring weekdays data type    enum weekdays { mon,                    tues,                    wed,                    thurs,                    fri,                    sat,                    sun };    // a variable named day1 holds the value of wed    enum weekdays day1 = wed;      // mon holds 0, tue holds 1 and so on    cout << "The value stored in wed is :" << day1 << endl;      // looping through the values of    // defined integral constants    for (int i = mon; i <= sun; i++)        cout << i << " ";    cout << endl;      return 0;}
The value stored in wed is :2
0 1 2 3 4 5 6

For more on enumeration refer to the Enumeration(or enum) in C article.
C-Struct-Union-Enum
Picked
Technical Scripter 2018
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
TCP Server-Client implementation in C
Exception Handling in C++
Multithreading in C
'this' pointer in C++
Arrow operator -> in C/C++ with Examples
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects