The Wayback Machine - https://web.archive.org/web/20240717024245/https://www.geeksforgeeks.org/static-const-vs-define-vs-enum/
Open In App

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

Last Updated : 25 Oct, 2018
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.



Similar Reads

Difference between const char *p, char * const p and const char * const p
Prerequisite: Pointers There is a lot of confusion when char, const, *, p are all used in different permutations and meanings change according to which is placed where. Following article focus on differentiation and usage of all of these. The qualifier const can be applied to the declaration of any variable to specify that its value will not be cha
3 min read
Why can't a const variable be used to define an Array's initial size in C?
What is an array? An array is a collection of items of the same data type stored at contiguous memory locations. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The base value is index
3 min read
Difference between #define and const in C?
#define is a preprocessor directive. Data defined by the #define macro definition are preprocessed, so that your entire code can use it. This can free up space and increase compilation times.const variables are considered variables, and not macro definitions. Long story short: CONSTs are handled by the compiler, where as #DEFINEs are handled by the
2 min read
Internal static variable vs. External static variable with Examples in C
The static variable may be internal or external depending on the place of declaration. Both kinds of static variables are stored in initialized data segments. Internal Static Variables Internal Static variables are defined as those having static variables which are declared inside a function and extend up to the end of the particular function. Synt
2 min read
Difference between Struct and Enum in C/C++ with Examples
Structure in C++ A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. The ‘struct’ keyword is used to create a structure. Syntax: struct structureName{ member1; member2; member3; . . . memberN; }; Below is the implementation: C/C++ Code #inc
3 min read
Enumeration (or enum) in C
Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain. enum State {Working = 1, Failed = 0}; The keyword 'enum' is used to declare new enumeration types in C and C++. Following is an example of enum declaration. // The name of enumeration i
4 min read
C++ | const keyword | Question 1
Predict the output of following program #include &lt;iostream&gt; using namespace std; int main() { const char* p = &quot;12345&quot;; const char **q = &amp;p; *q = &quot;abcde&quot;; const char *s = ++p; p = &quot;XYZWVU&quot;; cout &lt;&lt; *++s; return 0; } (A) Compiler Error (B) c (C) b (D) Garbage Value Answer: (B) Explanation: Output is ‘c’ c
1 min read
C++ | const keyword | Question 2
In C++, const qualifier can be applied to 1) Member functions of a class 2) Function arguments 3) To a class data member which is declared as static 4) Reference variables (A) Only 1, 2 and 3 (B) Only 1, 2 and 4 (C) All (D) Only 1, 3 and 4 Answer: (C) Explanation: When a function is declared as const, it cannot modify data members of its class. Whe
1 min read
C++ | const keyword | Question 3
Predict the output of following program. #include &lt;iostream&gt; using namespace std; class Point { int x, y; public: Point(int i = 0, int j =0) { x = i; y = j; } int getX() const { return x; } int getY() {return y;} }; int main() { const Point t; cout &lt;&lt; t.getX() &lt;&lt; &quot; &quot;; cout &lt;&lt; t.getY(); return 0; } (A) Garbage Value
1 min read
C++ | const keyword | Question 5
#include &lt;stdio.h&gt; int main() { const int x; x = 10; printf(&quot;%d&quot;, x); return 0; } (A) Compiler Error (B) 10 (C) 0 (D) Runtime Error Answer: (A) Explanation: One cannot change the value of 'const' variable except at the time of initialization. Compiler does check this.Quiz of this Question
1 min read
three90RightbarBannerImg