Skip to main content

Is it better to use #define or const int for constants?

Arduino is an odd hybrid, where some C++ functionality is used in the embedded world—traditionally a C environment. Indeed, a lot of Arduino code is very C like though.

C has traditionally used #defines for constants. There are a number of reasons for this:

  1. You can't set array sizes using const int.
  2. You can't use const int as case statement labels (though this does work in some compilers)
  3. You can't initialize a const with another const.

You can check this question on StackOverflow for more reasoning.

So, what should we use for Arduino? I tend towards #define, but I see some code using const and some using a blend.

Cybergibbons
  • 5.4k
  • 7
  • 34
  • 51