“static const” vs “#define” vs “enum”
Last Updated :
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;
CPP
// 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;
}
"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
CPP
// 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.
CPP
// 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
User Defined Literals in C++
A literal is used for representing a fixed value in a program. A literal could be anything in a code like a, b, c2. , 'ACB', etc. Similarly, User-Defined Literals (UDL) provides literals for a variety of built-in types that are limited to integer, character, floating-point, string, boolean, and poin
3 min read
How to Define Constants in C++?
In C++, constants are the tokens that are used to represent those values that cannot be changed during the program execution. It is defined only once and remains the same throughout the execution of the program. C++ provides various different methods to define constants. In this article, we will dis
4 min read
User Defined Data Types in C++
User defined data types are those data types that are defined by the user himself. In C++, these data types allow programmers to extend the basic data types provided and create new types that are more suited to their specific needs. C++ supports 5 user-defined data types:Table of ContentClassStructu
4 min read
Enumeration (or enum) in C
In C, an enumeration (or enum) is a user defined data type that contains a set of named integer constants. It is used to assign meaningful names to integer values, which makes a program easy to read and maintain.DefinitionAn enum must be defined before we can use it in program.Cenum enum_name { n1,
5 min read
C++ set for user define data type
The C++ STL set is a data structure used to store the distinct value in ascending or descending order. By default, we can use it to store system defined data type only(eg. int, float, double, pair etc.). And if we want to store user-defined datatype in a set (eg. structure) then the compiler will sh
2 min read
C++ Static Data Members
Static data members are class members that are declared using static keywords. A static member has certain special characteristics which are as follows:Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
5 min read
is_const Template in C++
The std::is_const template of C++ STL is used to check whether the type is a const-qualified or not. It returns a boolean value showing the same. Syntax: template < class T >struct is_const; Template Parameter: This template contains single parameter T (Trait class) to check whether T is a con
2 min read
C++ using vs Typedef
typedef keyword in C++ is used for aliasing existing data types, user-defined data types, and pointers to a more meaningful name. Typedefs allow you to give descriptive names to standard data types, which can also help you self-document your code. Mostly typedefs are used for aliasing, only if the p
2 min read
Difference between #define and const in C
In C, both #define and const define constant values, but these constants differ greatly in their behaviors and implementation. #define is a preprocessor directive used to define constants that are replaced by their value during preprocessing, before actual compilation begins. while const defines a t
2 min read
#define in C
In C programming, #define is a preprocessor directive that is used to define macros. The macros are the identifiers defined by #define which are replaced by their value before compilation. We can define constants and functions like macros using #define. The generics in C are also implemented using t
3 min read