Any C or Arduino tutorial quickly gets into integer types, where there’s a tradeoff between the memory required for a variable, and the range of values it can hold:
char a; int b; long c;
But soon you might encounter code that looks like this, perhaps with no explanation:
int8_t a; int16_t b; int32_t c;
A funny thing about C is that those first three types—char
, int
, long
—do not have a canonical size that’s true among all situations! It’s totally implementation-dependent…
For example, on the Arduino UNO and similar, int
(and cousin unsigned int
) are 16-bit values…two bytes in RAM…with a range from −32,768 to 32,767 (or 0 to 65,535 for the unsigned variant).
But switch to most any 32-bit microcontroller and now those int
s are 32-bit values…four bytes in RAM, with a much larger range. Same type names, but different utilization. Sometimes, code falls over due to this change.
The stdint.h header file…included automatically by the Arduino compiler…declares a set of known-sized integer types which can be relied on.
int8_t
is always, by definition an 8-bit signed value (−128 to +127), uint8_t
is 8 bits unsigned (0 to 255). One byte in RAM.
int16_t
is always, by definition an 16-bit signed value (−32,768 to +32,767), uint16_t
is 16 bits unsigned (0 to 65,535). Two bytes in RAM.
int32_t
is always, by definition a 32-bit signed value (−2,147,483,648 to +2,147,483,647), uint32_t
is 32 bits unsigned (0 to 4,294,967,295). Four bytes in RAM.
If you get into programming outside the Arduino environment, these types are not automatically declared, and one must explicitly:
#include <stdint.h>
Page last edited March 08, 2024
Text editor powered by tinymce.