enum vs. const vs. #define in C/C++

C++Server Side ProgrammingProgramming

Here we will see what are the differences between enum, const and the #define in C or C++ programs. These three creates some confusion while we have to take the decision for choosing them. Now let us see what are these three things.

const or static const

The const is constant type data, or static const is constant but storage specifier is static. So it will remain active until the program is terminated, and constant type data cannot be updated.

Example

#include <iostream>
using namespace std;
main() {
   int x;
   x = 65700;
   cout << "x is (as integer):" << x << endl;
   x = (short)65700; //will be rounded after 2-bytes
   cout << "x is (as short):" << x << endl;
}

Output

x is (as integer):65700
x is (as short):164
raja
Published on 26-Apr-2019 15:19:32
Advertisements