"static const" vs "#define" vs "enum" ?


"static const"

“static const” is basically a combination of static(a storage specifier) and const(a type qualifier).

The 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.

The 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.

Example

#include<iostream>
using namespace std;
int main(){
int input = 10;
static const int value = 5;
input += value;
cout << input;
return 0;
}

"#define"

Preprocessor commands are called DIRECTIVES, and begin with a pound or hash symbol (#). No white space should appear before the #, and semicolon is NOT required at the end.

Many things that can be done during preprocessing phase include

  • Inclusion of other files through #include directive

  • Definition of symbolic constants and macros through #define directive

Through some preprocessor directives you can also conditionally compile or execute some preprocessor directives.

Note - The preprocessing phase of a C++ program occurs before a program is compiled. The C++ preprocessor is a program that is executed before the source code is compiled.

You have already learnt to work with #include preprocessor directive that lets you include desired header files in your program. This discussion or ours is dedicated to #define preprocessor directive.

The #define preprocessor allows us to define symbolic names and constants. For example,

#define PI 3.14159

This statement will translate every occurrence of PI in the program to 3.14159.

C++ #define Example

Now consider the complete example wherein every occurrence of PI will replace with the value 3.14159, the value defined with #define directive.

Input: PI 3.14159
Output: Area of Circle 314.15

Example

#include<iostream>
using namespace std;
#define PI 3.14159
int main() {
   int r = 10;
   float cir;
   cir = PI * (r * r);
   cout<<"Area of Circle: "<<cir<<endl;
}

"enum"

enum is a user defined data type where we specify a set of values for a variable and the variable can only take one out of a small set of possible values. We use enum keyword to define an Enumeration.

enum direction {East, West, North, South}dir;

Here Enumeration name is direction which can only take one of the four specified values, the dir at the end of the declaration is an enum variable.

Let's take a simple example to understand this:

Here I have assigned the value West to the enum variable dir and when I displayed the value of dir it shows 1. This is because by default the values are in increasing order starting from 0, which means East is 0, West is 1, North is 2 and South is 3.

Input: direction {East, West, North, South} dir=West
Output: 1

Example

#include<iostream>
using namespace std;
enum direction {East, West, North, South} dir;
int main(){
   dir = West;
   cout<<dir;
   return 0;
}

Another way to declare enum variable

As we have seen in the above example that I have declared the enum variable dir during enum declaration, there is another way to declare an enum variable.

Example

#include <iostream>
using namespace std;
enum direction {East, West, North, South};
int main() {
   direction dir;
   dir = South;
   cout<<dir;
   return 0;
}

Output

3

Why use enum in C++

Now that we understand what is enum and how to use them in program, let's discuss why we use them:

Enums are used only when we expect the variable to have one of the possible sets of values, for example, we have a dir variable that holds the direction. Since we have four directions, this variable can take any one of the four values, if we try to assign another random value to this variable, it will throw a compilation error. This increases compile-time checking and avoid errors that occurs by passing in invalid constants.

Another important place where they are used frequently are switch case statements, where all the values that case blocks expect can be defined in an enum. This way we can ensure that the enum variable that we pass in switch parenthesis is not taking any random value that it shouldn’t accept.

Updated on: 20-Aug-2019

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements