user53822: I'm writing an Arduino library to communicate with an I2C device, and I'm wondering what the best way is to declare all the register addresses so as to save memory.
Using #defines:
#define REGISTER_MOTOR_1_MODE 0x44
#define REGISTER_MOTOR_2_MODE 0x47

Or using static const:
static const uint8_t REGISTER_MOTOR_1_MODE = 0x44;
static const uint8_t REGISTER_MOTOR_2_MODE = 0x47;

(Obviously I have more than just two registers I need to declare, but I thought two would illustrate the point just fine)

user8384: Theoretically both approaches should consume the same amount of space and will probably result in the same code.

However, rather than that meaning "pick any one", what that actually means is that you should turn your attention to other factors when deciding which one to use.
As a rule of thumb you should generally avoid macros because they don't respect scoping rules due to how they work.
This can lead to some serious problems in certain cases.
See Why are preprocessor macros evil and what are the alternatives?.

However, I'd like to point out a third option: constexpr.
constexpr uint8_t motorMode1Register = 0x44;

constexpr implies const and is a better expression of the intent that the value should be capable of being evaluated at compile time.
constexpr literally means "this is a constant expression and thus can be evaluated at compile time".
As a result of this, constexpr variables can be used in situations where other variables cannot, such as template parameters.
See Constexpr vs Macros.

user2967: A #define is a preprocessor macro. As Gerben says in his comment, it's just an automated find-and-replace. 
If you use it to hold things like C string constants, e.g. #define ERROR_STRING "You messed up, bud!" it could actually cause your program to take more memory, since that same string literal will be duplicated every time you reference it. In that case a static const would be both type-safe AND reduce memory usage.

user4143: You'll find no noticeable difference memory-wise between the two.
The only real difference is that the const method also imposes a type to the value, which can be useful for function overloading or mathematical operations.

53822: <p>I'm writing an Arduino library to communicate with an I2C device, and I'm wondering what the best way is to declare all the register addresses so as to save memory.</p>

<p>Using <code>#defines</code>:</p>

<pre><code>#define REGISTER_MOTOR_1_MODE 0x44
#define REGISTER_MOTOR_2_MODE 0x47
</code></pre>

<p>Or using <code>static const</code>:</p>

<pre><code>static const uint8_t REGISTER_MOTOR_1_MODE = 0x44;
static const uint8_t REGISTER_MOTOR_2_MODE = 0x47;
</code></pre>

<p>(Obviously I have more than just two registers I need to declare, but I thought two would illustrate the point just fine)</p>
 8384: <p>Theoretically both approaches should consume the same amount of space and will probably result in the same code.</p>

<hr>

<p>However, rather than that meaning "pick any one", what that actually means is that you should turn your attention to other factors when deciding which one to use.</p>

<p>As a rule of thumb you should generally avoid macros because they don't respect scoping rules due to how they work.<br>
This can lead to some serious problems in certain cases.<br>
See <a href="https://stackoverflow.com/q/14041453">Why are preprocessor macros evil and what are the alternatives?</a>.</p>

<hr>

<p>However, I'd like to point out a third option: <code>constexpr</code>.</p>

<pre class="lang-cpp prettyprint-override"><code>constexpr uint8_t motorMode1Register = 0x44;
</code></pre>

<p><code>constexpr</code> implies <code>const</code> and is a better expression of the intent that the value should be capable of being evaluated at compile time.<br>
<code>constexpr</code> literally means "this is a constant expression and thus can be evaluated at compile time".<br>
As a result of this, <code>constexpr</code> variables can be used in situations where other variables cannot, such as template parameters.</p>

<p>See <a href="https://stackoverflow.com/q/42388077">Constexpr vs Macros</a>.</p>
 2967: <p>A <code>#define</code> is a preprocessor macro. As Gerben says in his comment, it's just an automated find-and-replace. </p>

<p>If you use it to hold things like C string constants, e.g. <code>#define ERROR_STRING "You messed up, bud!"</code> it could actually cause your program to take more memory, since that same string literal will be duplicated every time you reference it. In that case a static const would be both type-safe AND reduce memory usage.</p>
 4143: <p>You'll find no noticeable difference memory-wise between the two.</p>

<p>The only real difference is that the <code>const</code> method also imposes a type to the value, which can be useful for function overloading or mathematical operations.</p>
