A commenter has suggested that
byteis 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 charuint8_t vs unsigned char and When is uint8_t ≠ unsigned char?When is uint8_t ≠ unsigned char?.Another commenter has suggested that using byte will not necessarily improve performance, because numbers smaller than
intwill be promoted toint(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
Describe the "obvious" problem because, yeah, it even took me a minute or so to realize what was going on
cjs
- 594
- 2
- 16
Nick Gammon ♦
- 38.9k
- 13
- 70
- 126
Nick Gammon ♦
- 38.9k
- 13
- 70
- 126
Nick Gammon ♦
- 38.9k
- 13
- 70
- 126
Nick Gammon ♦
- 38.9k
- 13
- 70
- 126