Skip to content

Commit 7a317a1

Browse files
Fix int documentation on overflow (which is UB)
1 parent 2718840 commit 7a317a1

File tree

2 files changed

+3
-12
lines changed

2 files changed

+3
-12
lines changed

Language/Variables/Data Types/int.adoc

+1-10
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,8 @@ The Arduino takes care of dealing with negative numbers for you, so that arithme
5353

5454
[float]
5555
=== Notes and Warnings
56-
When variables are made to exceed their maximum capacity they "roll over" back to their minimum capacity, note that this happens in both directions.
57-
[source,arduino]
58-
----
59-
int x;
60-
x = -32768;
61-
x = x - 1; // x now contains 32,767 - rolls over in neg. direction
56+
When signed variables are made to exceed their maximum or minimum capacity they _overflow_. The result of an overflow is unpredictable so this should be avoided. A typical symptom of an overflow is the variable "rolling over" from its maximum capacity to its minimum or vice versa, but this is not always the case. If you want this behavior, use link:unsignedInt{ext-relative}[unsigned int].
6257

63-
x = 32767;
64-
x = x + 1; // x now contains -32,768 - rolls over
65-
66-
----
6758
[%hardbreaks]
6859

6960
[float]

Language/Variables/Data Types/unsignedInt.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ The difference between unsigned ints and (signed) ints, lies in the way the high
4646

4747
[float]
4848
=== Notes and Warnings
49-
When variables are made to exceed their maximum capacity they "roll over" back to their minimum capacitiy, note that this happens in both directions
49+
When unsigned variables are made to exceed their maximum capacity they "roll over" back to 0, and also the other way around:
5050

5151
[source,arduino]
5252
----
53-
unsigned int x
53+
unsigned int x;
5454
x = 0;
5555
x = x - 1; // x now contains 65535 - rolls over in neg direction
5656
x = x + 1; // x now contains 0 - rolls over

0 commit comments

Comments
 (0)