user6936: Why do people use a variable to specify a pin number when the pin is unlikely to change throughout the execution of the code?
Many times I see an int being used for a pin definition,
int led = 13;

when the use of a const int
const int led = 13;

or enum, or #define 
#define LED 13

makes much more sense.
It is even in tutorials on the Arduino site, for example, the first tutorial that most people run, Blink.
I read somewhere that const int is preferred over #define. Why isn't this encouraged right from the beginning, rather than allowing people to develop bad habits, from the outset? I noticed it a while back, but recently it has started to irritate me, hence the question.
Memory/processing/computing wise is a const int, enum, or for that matter #define, better than a plain int, i.e. occupies less memory, stored in different memory (Flash, EEPROM, SRAM), faster execution, quicker to compile?

This may appear to be a duplicate of Is it better to use #define or const int for constants?, but I am addressing the question of why people use variables, and how does the performance improve when they don't, rather than which type of constant is better.

user29026: As a 2-week newbie to Arduino I'd pick up on the general idea of Arduino being occupied by non-programmers. Most sketches I have examined, including those on the Arduino site, show a total lack of order,  with sketches that do not work & barely a coherent comment in sight. Flow charts are non-existent, and the "Libraries" are an un-moderated jumble.

user10794: const int led = 13;

That is the correct method. Or even:
const byte led = 13;

How many pins do you have?
Some of the tutorials did not quite go through as much quality control as they might have.
Performance will be better using const byte, compare to int however the compiler may be smart enough to realize what you are doing.
What you can do is gently encourage people to use more efficient techniques by using them in your own code.

Responses to comments

A commenter has suggested that byte is not standard C. This is correct, however this is an Arduino StackExchange site, and I believe using standard types supplied by the Arduino IDE is acceptable.
In Arduino.h there is this line:
typedef uint8_t byte;

Note that this is not exactly the same as unsigned char. See uint8_t vs unsigned char and When is uint8_t ≠ unsigned char?.
Another commenter has suggested that using byte will not necessarily improve performance, because numbers smaller than int will be promoted to int (see Integer Promotion Rules if you want more on this).
However in the context of a const identifier, the compiler will generate efficient code in any case. For example, disassembling "blink" gives this in the original form:
00000086 <loop>:
  86:   8d e0           ldi r24, 0x0D   ; 13
  88:   61 e0           ldi r22, 0x01   ; 1
  8a:   1b d1           rcall   .+566       ; 0x2c2 <digitalWrite>

In fact it generates the same code whether the 13:

Is a literal
Is a #define
Is a const int
Is a const byte



The compiler know when it can fit a number into one register and when it can't. However it is good practice to use coding that indicates your intent. Making it const makes it clear that the number won't change, and making it byte (or uint8_t) makes it clear that you are expecting a small number.

Confusing error messages
Another major reason to avoid #define is the error messages you get if you make a mistake. Consider this "blink" sketch which has an error:
#define LED = 13;

void setup() {
  pinMode(LED, OUTPUT);      // <---- line with error
}

void loop() {
  digitalWrite(LED, HIGH);   // <---- line with error 
  delay(1000);             
  digitalWrite(LED, LOW);    // <---- line with error
  delay(1000);              
}

On the surface it looks OK, but it generates these error messages:
Blink.ino: In function ‘void setup()’:
Blink:4: error: expected primary-expression before ‘=’ token
Blink:4: error: expected primary-expression before ‘,’ token
Blink:4: error: expected `;' before ‘)’ token
Blink.ino: In function ‘void loop()’:
Blink:8: error: expected primary-expression before ‘=’ token
Blink:8: error: expected primary-expression before ‘,’ token
Blink:8: error: expected `;' before ‘)’ token
Blink:10: error: expected primary-expression before ‘=’ token
Blink:10: error: expected primary-expression before ‘,’ token
Blink:10: error: expected `;' before ‘)’ token

You look at the first highlighted line (line 4) and don't even see a "=" symbol. Plus, the line looks fine. Now it's fairly obvious what the problem is here (= 13 is being substituted for LED), but when the line is 400 lines further down in the code, it isn't obvious the problem is with the way LED is defined.
I've seen people fall for this many times (including myself).

user4143: As Ignacio has rightly states, it's basically because they don't know better.  And they don't know better because the people who taught them (or the resources they used when learning) didn't know better.
Much of the Arduino code and tutorials are written by people who have never had any training in programming and are very much "self taught" from resources by people who themselves are very much self taught with no proper training in programming.
Many of the snippets of tutorial code I see around the place (and especially those that are only available within YouTube videos --- urgh) would be a fail mark if I were marking them in an exam.
Yes, a const is preferred over a non-const, and even over a #define, because:

A const (like a #define, unlike a non-const) does not allocate any RAM
A const (like a non-const, but unlike a #define) gives the value an explicit type

The second point there is of particular interest.  Unless specifically told otherwise with embedded type-casting ((long)3) or a type suffix (3L) or the presence of a decimal point (3.0), a #define of a number will always be an integer and all mathematics performed on that value will be as if it were an integer.  Most of the time that's not a problem, but you can run into interesting scenarios when you try to #define a value that is larger than an integer can store, such as #define COUNT 70000 and then perform a mathematical operation with other int values on it.  By using a const you get to tell the compiler "This value is to be treated as this variable type" - so you would instead use: const long count = 70000; and all would work as expected.
It also has the knock-on effect that it checks the type when passing the value around the place.  Try passing a const long to a function that expects an int and it would complain about narrowing the variable range (or even completely fail to compile depending on the scenario).  Do that with a #define and it would just silently carry on giving you the wrong results and leave you scratching your head for hours.

6936: <p>Why do people use a variable to specify a pin number when the pin is unlikely to change throughout the execution of the code?</p>

<p>Many times I see an <code>int</code> being used for a pin definition,</p>

<pre><code>int led = 13;
</code></pre>

<p>when the use of a <code>const int</code></p>

<pre><code>const int led = 13;
</code></pre>

<p>or <a href="http://playground.arduino.cc/Code/Enum" rel="noreferrer"><code>enum</code></a>, or <a href="https://www.arduino.cc/en/Reference/Define" rel="noreferrer"><code>#define</code></a> </p>

<pre><code>#define LED 13
</code></pre>

<p>makes much more sense.</p>

<p>It is even in tutorials on the Arduino site, for example, the first tutorial that most people run, <a href="https://www.arduino.cc/en/tutorial/blink" rel="noreferrer">Blink</a>.</p>

<p>I <a href="https://stackoverflow.com/questions/16889213/how-to-use-define-to-assign-pins-in-arduino#comment-24383545">read somewhere</a> that <code>const int</code> is preferred over <code>#define</code>. Why isn't this encouraged right from the beginning, rather than allowing people to develop bad habits, from the outset? I noticed it a while back, but recently it has started to irritate me, hence the question.</p>

<p>Memory/processing/computing wise is a <code>const int</code>, <code>enum</code>, or for that matter <code>#define</code>, better than a plain <code>int</code>, i.e. occupies less memory, stored in <a href="https://www.arduino.cc/en/Tutorial/Memory" rel="noreferrer">different memory</a> (Flash, EEPROM, SRAM), faster execution, quicker to compile?</p>

<hr>

<p>This may appear to be a duplicate of <a href="https://arduino.stackexchange.com/questions/506/is-it-better-to-use-define-or-const-int-for-constants">Is it better to use #define or const int for constants?</a>, but I am addressing the question of why people use variables, and how does the performance improve when they don't, rather than which type of constant is better.</p>
 29026: <p>As a 2-week newbie to Arduino I'd pick up on the general idea of Arduino being occupied by non-programmers. Most sketches I have examined, including those on the Arduino site, show a total lack of order,  with sketches that do not work &amp; barely a coherent comment in sight. Flow charts are non-existent, and the "Libraries" are an un-moderated jumble.</p>
 10794: 

<pre class="lang-C++ prettyprint-override"><code>const int led = 13;
</code></pre>

<p>That is the correct method. Or even:</p>

<pre class="lang-C++ prettyprint-override"><code>const byte led = 13;
</code></pre>

<p>How many pins do you have?</p>

<p>Some of the tutorials did not quite go through as much quality control as they might have.</p>

<p>Performance will be better using <code>const byte</code>, compare to <code>int</code> however the compiler may be smart enough to realize what you are doing.</p>

<p>What you can do is gently encourage people to use more efficient techniques by using them in your own code.</p>

<hr>

<h3>Responses to comments</h3>

<ol>
<li><p>A commenter has suggested that <code>byte</code> is not standard C. This is correct, however this is an Arduino StackExchange site, and I believe using standard types supplied by the Arduino IDE is acceptable.</p>

<p>In Arduino.h there is this line:</p>

<pre class="lang-C++ prettyprint-override"><code>typedef uint8_t byte;
</code></pre>

<p>Note that this is not exactly the same as <code>unsigned char</code>. See <a href="https://stackoverflow.com/questions/1725855/uint8-t-vs-unsigned-char">uint8_t vs unsigned char</a> and <a href="https://stackoverflow.com/questions/16138237/when-is-uint8-t-%E2%89%A0-unsigned-char">When is uint8_t ≠ unsigned char?</a>.</p></li>
<li><p>Another commenter has suggested that using byte will not necessarily improve performance, because numbers smaller than <code>int</code> will be promoted to <code>int</code> (see Integer Promotion Rules if you want more on this).</p>

<p>However in the context of a <strong>const</strong> identifier, the compiler will generate efficient code in any case. For example, disassembling "blink" gives this in the original form:</p>



<pre class="lang-none prettyprint-override"><code>00000086 &lt;loop&gt;:
  86:   8d e0           ldi r24, 0x0D   ; 13
  88:   61 e0           ldi r22, 0x01   ; 1
  8a:   1b d1           rcall   .+566       ; 0x2c2 &lt;digitalWrite&gt;
</code></pre>

<p>In fact it generates the same code whether the <code>13</code>:</p>

<ul>
<li>Is a literal</li>
<li>Is a <code>#define</code></li>
<li>Is a <code>const int</code></li>
<li>Is a <code>const byte</code></li>
</ul></li>
</ol>

<hr>

<p>The compiler know when it can fit a number into one register and when it can't. However it is <em>good practice</em> to use coding that indicates your <em>intent</em>. Making it <code>const</code> makes it clear that the number won't change, and making it <code>byte</code> (or <code>uint8_t</code>) makes it clear that you are expecting a small number.</p>

<hr>

<h3>Confusing error messages</h3>

<p>Another major reason to avoid <code>#define</code> is the error messages you get if you make a mistake. Consider this "blink" sketch which has an error:</p>



<pre class="lang-C++ prettyprint-override"><code>#define LED = 13;

void setup() {
  pinMode(LED, OUTPUT);      // &lt;---- line with error
}

void loop() {
  digitalWrite(LED, HIGH);   // &lt;---- line with error 
  delay(1000);             
  digitalWrite(LED, LOW);    // &lt;---- line with error
  delay(1000);              
}
</code></pre>

<p>On the surface it looks OK, but it generates these error messages:</p>



<pre class="lang-none prettyprint-override"><code>Blink.ino: In function ‘void setup()’:
Blink:4: error: expected primary-expression before ‘=’ token
Blink:4: error: expected primary-expression before ‘,’ token
Blink:4: error: expected `;' before ‘)’ token
Blink.ino: In function ‘void loop()’:
Blink:8: error: expected primary-expression before ‘=’ token
Blink:8: error: expected primary-expression before ‘,’ token
Blink:8: error: expected `;' before ‘)’ token
Blink:10: error: expected primary-expression before ‘=’ token
Blink:10: error: expected primary-expression before ‘,’ token
Blink:10: error: expected `;' before ‘)’ token
</code></pre>

<p>You look at the first highlighted line (line 4) and <em>don't even see</em> a "=" symbol. Plus, the line looks fine. Now it's fairly obvious what the problem is here (<code>= 13</code> is being substituted for <code>LED</code>), but when the line is 400 lines further down in the code, it isn't obvious the problem is with the way LED is defined.</p>

<p>I've seen people fall for this many times (including myself).</p>
 4143: <p>As Ignacio has rightly states, it's basically because they don't know better.  And they don't know better because the people who taught them (or the resources they used when learning) didn't know better.</p>

<p>Much of the Arduino code and tutorials are written by people who have never had any training in programming and are very much "self taught" from resources by people who themselves are very much self taught with no proper training in programming.</p>

<p>Many of the snippets of tutorial code I see around the place (and especially those that are only available within YouTube videos --- urgh) would be a fail mark if I were marking them in an exam.</p>

<p>Yes, a <code>const</code> is preferred over a non-const, and even over a <code>#define</code>, because:</p>

<ul>
<li>A <code>const</code> (like a <code>#define</code>, unlike a non-const) does not allocate any RAM</li>
<li>A <code>const</code> (like a non-const, but unlike a <code>#define</code>) gives the value an explicit <em>type</em></li>
</ul>

<p>The second point there is of particular interest.  Unless specifically told otherwise with embedded type-casting (<code>(long)3</code>) or a type suffix (<code>3L</code>) or the presence of a decimal point (<code>3.0</code>), a <code>#define</code> of a number will always be an integer and all mathematics performed on that value will be as if it were an integer.  Most of the time that's not a problem, but you can run into interesting scenarios when you try to <code>#define</code> a value that is larger than an integer can store, such as <code>#define COUNT 70000</code> and then perform a mathematical operation with other <code>int</code> values on it.  By using a <code>const</code> you get to tell the compiler "This value is to be treated as this variable type" - so you would instead use: <code>const long count = 70000;</code> and all would work as expected.</p>

<p>It also has the knock-on effect that it checks the type when passing the value around the place.  Try passing a <code>const long</code> to a function that expects an <code>int</code> and it would complain about narrowing the variable range (or even completely fail to compile depending on the scenario).  Do that with a <code>#define</code> and it would just silently carry on giving you the wrong results and leave you scratching your head for hours.</p>
