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


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

Updated on: 30-Jul-2019

412 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements