Lesson 10 - Understanding short Data Type
Learn how short stores smaller whole numbers, how it compares with int, and when it can help save memory in Arduino/ESP32 projects.
Progress indicator
Lesson 10 of 10
Learning Objectives
- Understand that short stores whole numbers and is often used for smaller ranges than int.
- Learn short syntax, declaration, initialization, range, and memory size basics.
- Compare short with int and understand signed vs unsigned short.
- Understand why short may use less memory and when that matters in embedded systems.
- Recognize overflow and wrap-around behavior with short types.
- Choose short only when the value range is safe for your specific project.
Concept Explanation
What is short
short is a whole-number integer type used to store values in a smaller range than larger integer types on many platforms.
It is useful when your values stay within a smaller range and you want to think about memory more carefully in embedded projects.
In beginner projects, you will usually use int first. You learn short when you start thinking about memory tradeoffs and known value ranges.
short Syntax
Basic syntax:
short variableName;short offset;
short temperatureDelta = -5;
unsigned short count = 120;short can be signed (default short) or explicitly unsigned (unsigned short) depending on whether you need negative values.
Declaring short Variables
Declaring means creating a variable and choosing short as the type so it can store integer (whole number) values.
short errorValue;
short sensorOffset;Initializing short
Initializing means assigning a starting whole-number value when the variable is created.
short delta = -10;
short threshold = 120;Use short only when you know the values will stay within its range.
Beginner tip
If you are not sure about the range yet, start with int and switch to short later after you understand the values.
short Range
The exact range depends on the platform, but on many common systems short is a 16-bit signed integer. A common beginner mental model is that it stores smaller values than a larger integer type.
This smaller range is the tradeoff for lower memory use. If your value grows beyond the safe range, overflow can happen.
short Memory Size
short is commonly 2 bytes on many platforms. This is why it may use less memory than int on platforms where int is larger.
Always verify on your board if exact size matters, because embedded platforms can differ.
Why short Uses Less Memory
The type is designed for a smaller integer storage size (commonly 2 bytes). Fewer bytes can reduce memory usage, but also reduces the range of values it can safely store.
This matters more when you have many variables, arrays, or data records in memory.
When to Use short
- Small offsets or calibration deltas (positive/negative)
- Values that stay in a known small integer range
- Memory-sensitive embedded logic (when range is safe)
- Counters that do not need large ranges (or use unsigned short)
If the value can grow unexpectedly, prefer int (or a larger type) until you confirm the actual range in testing.
Real-Life Example
A calibration delta (for example, -12 or +8) is often a small value. Using short can be a reasonable choice if you know the correction will stay in a small range.
short vs int (Comparison)
| Feature | short | int |
|---|---|---|
| Typical use | Smaller-range integers | General integer values |
| Memory | Often smaller (platform-dependent) | Often larger or equal |
| Range | Smaller than int on many systems | Usually wider range |
Beginner takeaway: use int by default for simpler lessons, and use short only when you have a clear reason (known small range or memory planning).
Signed vs Unsigned short
short (signed short) can store negative and positive values. unsigned short stores only zero and positive values, usually with a larger positive maximum.
short (signed)
Good for offsets like -12 or corrections that may go below zero.
unsigned short
Good for counts that should never be negative, like pulse counts.
Signed vs unsigned is about whether the value may go below zero. This decision is just as important as choosing short itself.
Example Code
This example uses short and unsigned short for a delta, threshold, and a small positive counter.
short sensorDelta = -12;
short threshold = 120;
unsigned short pulseCount = 0;
void setup() {
Serial.begin(115200);
short correctedValue = threshold + sensorDelta;
pulseCount = pulseCount + 1;
Serial.println("short values updated");
}
void loop() {
// Lesson focus is short variables in setup()
}What this example teaches
- How to use signed short for positive/negative deltas
- How to use unsigned short for a non-negative counter
- How to update short values with simple integer math
- How to separate range-limited values by purpose (delta vs counter)
Example Code Explanation
short sensorDelta = -12;stores a small negative offset value.short threshold = 120;stores a small positive whole number.unsigned short pulseCount = 0;stores a non-negative counter.short correctedValue = threshold + sensorDelta;applies the delta to the threshold.pulseCount = pulseCount + 1;increases the unsigned short count.Serial.println(...)prints a message after the values update.
Beginner reading tip
Read the example in 3 groups: signed values (delta), unsigned counter (pulseCount), and calculated result (correctedValue).
Real-life example
A simple sensor calibration step may apply a small correction (+/- value) and count how many times readings were processed.
Overflow and Wrap-Around
If a short-type value goes beyond its allowed range, overflow can happen. For unsigned types, the value can wrap around to a low value (often 0).
unsigned short count = 65535;
count = count + 1; // Wraps to 0 on common 16-bit unsigned shortThis is why choosing the correct data type range is important.
Overflow
Overflow happens when a value goes beyond what the type can store.
Wrap-around
For unsigned types, the value may wrap back to a low value (often 0) after the maximum is exceeded.
In counters, wrap-around may be okay if expected. In measurements, it usually means you chose the wrong type or range.
Common Mistakes with short
- Using short without checking whether the value range is safe.
- Using unsigned short for values that may need to go negative.
- Ignoring overflow/wrap-around in counters.
- Assuming short has the same size/range on every platform without checking docs.
- Using short for values that may silently grow larger over time.
Best Practices for short
- Use short only when you know the value range is small enough.
- Use signed short for positive/negative offsets and unsigned short for non-negative counters.
- Use descriptive names like
sensorDeltaandpulseCount. - Check platform documentation when exact size/range matters.
- Test boundary values during debugging to confirm your chosen type is safe.
Practice Task
- Change
sensorDeltafrom-12to-25and explain howcorrectedValuechanges. - Change
thresholdto80and recalculate the result. - Rename
pulseCounttoprocessedCountfor clearer meaning. - Explain when
unsigned shortis better thanshortfor counters, and whenintmay be safer.
Try it now
Open the simulator workspace and observe variable updates while stepping through examples.