Skip to content

Beginner-unfriendly behavior of long int initialization #1629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
wagenadl opened this issue Nov 3, 2022 · 1 comment
Closed
3 tasks done

Beginner-unfriendly behavior of long int initialization #1629

wagenadl opened this issue Nov 3, 2022 · 1 comment
Assignees
Labels
conclusion: invalid Issue/PR not valid topic: code Related to content of the project itself type: enhancement Proposed improvement

Comments

@wagenadl
Copy link

wagenadl commented Nov 3, 2022

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

  • I searched for previous requests in the issue tracker
  • I verified the feature was still missing when using the latest nightly build
  • My request contains all necessary details
@wagenadl wagenadl added the type: enhancement Proposed improvement label Nov 3, 2022
@per1234
Copy link
Contributor

per1234 commented Nov 3, 2022

Hi @wagenadl. Thanks for your suggestion.

No compiler warnings are generated either.

I'm happy to tell you that they are. You only need to enable warnings in your preferences and you'll get a nice warning like this:

C:\Users\per\AppData\Local\Temp\.arduinoIDE-unsaved2022103-716-a7vrx4.zaink\sketch_nov3a\sketch_nov3a.ino: In function 'void setup()':
C:\Users\per\AppData\Local\Temp\.arduinoIDE-unsaved2022103-716-a7vrx4.zaink\sketch_nov3a\sketch_nov3a.ino:3:18: warning: integer overflow in expression [-Woverflow]
 long int x = 100 * 1000;
              ~~~~^~~~~~

I'll provide instructions:

  1. Select File > Preferences... from the Arduino IDE menus.
  2. Select any value "Default" or higher from the "Compiler warnings" menu.
  3. Click the OK button.

As for the idea of enabling compiler warnings by default in the IDE preferences, that is tracked here: #1630

@per1234 per1234 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 3, 2022
@per1234 per1234 self-assigned this Nov 3, 2022
@per1234 per1234 added conclusion: invalid Issue/PR not valid topic: code Related to content of the project itself labels Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: invalid Issue/PR not valid topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

No branches or pull requests

2 participants