If all the counters are related and you want to give them cohesion in the form of a type, enum
is the best choice because they do not allow you to mistakenly assign them a wrong value.
Pin pin1 = PIN_00; //Ok
Pin pin2 = 0x10; //Não compila
Now if the purpose is just to name a number magical, "there is controversy. I personally prefer% with% of% than% with% because%% of variables are handled by the same compiler. You have an identifier, with a well-defined type and value. The const
is treated by the preprocessor, which can generate unexpected results in some cases. For example:
const int CONST = 2 + 5;
#define DEFINE 2 + 5
int x = 3 * CONST; //Resultado = 3 * (2 + 5)
int y = 3 * DEFINE; //Resultado = 3 * 2 + 5 !!!
Another problem is that the type of #define
is only defined by the literal, which can also cause problems:
#define FATOR 1.5
double val = FATOR / 2;
If someday someone changes the value from const
to define
(instead of define
) that division becomes an integer division, whose result is FATOR
, instead of 1
expected. This problem does not happen if 1.0
is 0
.