Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 input
int 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;
}


Output:

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 number
void 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;
}


Output:

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;
}


Output:

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.



Last Updated : 25 Oct, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads