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;
#include <bits/stdc++.h>
using namespace std;
int addConst( int input)
{
static const int value = 5;
const int constant_not_static = 13;
input += value;
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
#include <bits/stdc++.h>
#define ll long long int
#define f(i, a, b, c) for (ll i = a; i < b; i += c)
using namespace std;
void count(ll input)
{
f(j, 1, input + 1, 1)
{
cout << j << " " ;
}
cout << endl;
}
int main()
{
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.
#include <bits/stdc++.h>
using namespace std;
int main()
{
enum weekdays { mon,
tues,
wed,
thurs,
fri,
sat,
sun };
enum weekdays day1 = wed;
cout << "The value stored in wed is :" << day1 << endl;
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 Oct, 2018
Like Article
Save Article