Beginner-unfriendly behavior of long int initialization #1629
Labels
conclusion: invalid
Issue/PR not valid
topic: code
Related to content of the project itself
type: enhancement
Proposed improvement
Describe the request
Request: Implement IDE warnings for integer assignments that do not behave as a beginner would expect.
Rationale: Experienced C programmers will know that
long int x = 100 * 1000;
is a dangerous thing to write, because the two numbers will be interpreted as plain ints. As a consequence, when compiled on an Arduino Uno, the multiplication is interpreted as a 16-bit operation, and the end result will be x = 34,464. A beginner might reasonably expect that x = 100,000 at the end of this. While I fully realize that silent integer overflow is a problem with the C/C++ language rather than with the Arduino IDE per se, the fact is that may Arduino users may not be C/C++ experts. In fact, one of the key reasons that Arduino is so popular, is that for the most part you don't have to be a C/C++ expert to use it.
Another great example is
delay(66*1000)
which most people would expect to result in a 66 second delay. In fact, 66*1000 overflows to 464, so the actual delay is less than half a second.
If it is not possible to make the compiler understand the obvious intention in these cases, it would be really helpful if the IDE could flag this sort of thing with a visible warning.
Describe the current behavior
The Arduino IDE does not notice calculations where C quirks result in different results than a casual user would expect. No compiler warnings are generated either.
Arduino IDE version
2.0.1
Operating system
Linux
Operating system version
Ubuntu 22.04
Additional context
No response
Issue checklist
The text was updated successfully, but these errors were encountered: