Are there any rules on when integers should be declared or defined? Sometimes I see this done before setup and sometimes I see them declared in setup. What are the rules that tell me when I should declare stuff?
Like most of the tutorial pages on Arduino.cc you should not rely on the information given here. Some are outright wrong e.g. the part about "variable rollover" using a (signed) int as an example.
There are many good C and C++ tutorials on the web. Google is your friend here
olf2012:
Like most of the tutorial pages on Arduino.cc you should not rely on the information given here. Some are outright wrong e.g. the part about "variable rollover" using a (signed) int as an example.
I have submitted recommendations for improvements to that page:
I'm happy to add to that issue if anyone has any recommendations.
Any specific suggestions for improvements to the Arduino documentation are welcome. It's unfortunate that the quality isn't better to start with but I've found the Arduino team is very receptive to suggestions, even if it takes them a little while to implement them sometimes.
It's now possible to submit pull requests for change proposals directly to the repository that contains the content the Language Reference pages are generated from:
There is an edit link on every one of those pages.
Other documentation don't have that system so you need to submit an issue report with suggested improvements here:
The specific page in question seems like it might be an orphan which is not linked to from any other page on the website but it will still show up in Google searches so it still needs to either be of high quality or removed.
Good point! I've changed x to a byte. There should be a fairly good guarantee that will be an unsigned 8 bit integer when using any official Arduino core.
I am referring to the fact that signed variables are not guaranteed to rollover as described. Any future compiler upgrade could silently break the code
olf2012:
Any future compiler upgrade could silently break the code
I believe the point is that a signed integer rolling over is a serious problem regardless of the final outcome. But, the description is terse so I could be mistaken.
Signed integer overflow is undefined behavior. It may happen to roll over but this should not be counted on. There actually is a way to make it not undefined behavior:
Unsigned integer rollover is not undefined behavior so I think the change to an unsigned type in the example is an improvement. Maybe it would be worth adding an explanation about signed overflow being UB to that page.
My suggestions have already been accepted. Pretty impressive response time from Arduino! Thank you olf2012 for pointing out that page needed some work and Coding Badly for the improvement. The world is now a very slightly better place.