Logical Flaw In Your Code

You need to have a clear understanding on how overflow/underflow works,

  if (studentsNUMBER < 0 or studentsNUMBER > 32767){
    ...
  }

You have declared studentsNUMBER as an integer whose max value can be 32767. Now you are comparing studentNUMBER > 32767 which is will never be the case as studentNUMBER will never be able to cross the limit.

Suggestions

Mind that once there has been overflow/underflow, Undefined Behavior is occurred and your program can do anything abnormal.

One of many approaches to overcome such situation can be like,

long result, 
int studentNUMBER, keyENTERED;
...
result = (studentsNUMBER * 10) + keyENTERED;
if (result > 32767)
{
    // overflow occurred 
}

Hope you will get a concept.

Answer from Sazzad Hissain Khan on Stack Overflow
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Integer overflow understanding - Programming - Arduino Forum
I'm trying to understand the integer overflow mechanism, but I don't get it yet. The following code has unexpected (to me) behavior : int counter = 0; uint32_t longcounter = 0; void loop() { counter = counter + 5; longcounter = counter *1000; } This works fine until the 'counter' reaches 30. with the next addition of 5 the 'longcounter' will get a -30536 value.
Published   March 9, 2019
Discussions

Weird Arduino math....

If you look in the log window you will see the compiler complains about an integer overflow calculating (410*410). It is treating 410 as a 16 bit integer, so overflows at 65534. You need to tell the compiler that you are using the long type.

unsigned long Potencia = long(410)*long(410); or unsigned long Potencia = (410L*410L);

Interesting problem. If you watch log window while compiling and you see red text flying by, it means there is a compiler warning. You can click in the log window, press <Ctrl>A then <Ctrl>C to copy the log. Then you can paste into Notepad with <Ctrl>V so you can review the results.

warning: integer overflow in expression [-Woverflow]
Potencia = (410*410) ;
                 ^
More on reddit.com
🌐 r/arduino
6
6
October 8, 2021
bluetooth - integer overflow in expression [-Woverflow] - TinyCircuit - Arduino Stack Exchange
I have a tiny circuit Bluetooth shield and I was testing out the sample code that was provided by tinycircuit. However, when I tried uploading the sample code into the board, I kept having the foll... More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
c++ - integer overflow in expression of type 'int' results - Arduino Stack Exchange
i need to wake up my ESP every 60 min to read some data and post it to server, all process working fine when i use numbers of minute below 60 (converted in microsecond x 1000000) but when i use 60 ... More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
Integer overflow error help
I am making a Hall effect tachometer. I have five magnets on a rotating disc in front of a US5881 Code I'm using: #include LiquidCrystal lcd(7, 8, 9, 10, 11, 12); int encoder_pin = 2; unsigned int … More on forum.arduino.cc
🌐 forum.arduino.cc
4
0
February 18, 2018
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Integer overflow problem when it is within range? - Programming - Arduino Forum
July 29, 2022 - Hello. I am trying to understand why the variable H is overflowing. It is of type float which according to the guide can take very large numbers: Floating-point numbers can be as large as 3.4028235E+38 and as low as -…
🌐
Reddit
reddit.com › r/arduino › weird arduino math....
r/arduino on Reddit: Weird Arduino math....
October 8, 2021 -

Im getting weird results... is my Arduino nano about to die??

i've fixed the reading to 410 just in case i had a bad reading on the potentiometer. soy you can disregard the first part.

 Serial.print("vision logaritimca: _pot1:");
 int Reading= analogRead(ch1l);
 Serial.print(Reading);
 Serial.print(" a la potencia de 2: ");

 unsigned long Potencia = (410*410) ;
  Serial.println(Potencia);
  Serial.print("Resultado: ");
  Serial.print(potencia/4104);

Acording to mi calculator, 410 by 410 is 168100

but this is what im geting intstead.... What im doing wrong, sorry im tired debugging and this is beyond my actual comprehension, i hope this has a simple answer

🌐
LinkedIn
linkedin.com › pulse › ariane-5-space-crash-my-arduino-issue-integer-daniel
Ariane 5 space crash === My Arduino issue : integer overflows
June 23, 2019 - A simple validation would have ... criticized by others). But within C and C++ for instance, a value can exceed, and the value will simply overflow the range. Conveniently, this is called Integer overflow....
🌐
YouTube
youtube.com › watch
Arduino - Int Overflow Explained (and how to avoid having this ...
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Find elsewhere
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Integer overflow error help - Programming - Arduino Forum
February 18, 2018 - I am making a Hall effect tachometer. I have five magnets on a rotating disc in front of a US5881 Code I'm using: #include LiquidCrystal lcd(7, 8, 9, 10, 11, 12); int encoder_pin = 2; unsigned int rpm; volatile byte pulses; unsigned long timeold; unsigned int pulsesperturn = 5; void counter(){ pulses++; } void setup() { lcd.begin(16,2); lcd.print("RPM:"); pinMode(encoder_pin, INPUT); attachInterrupt(0, counter, FALLING); pulses = 0; rpm = 0; timeold = 0; } v...
🌐
ArduinoJson
arduinojson.org › news › 2019-03-22 - arduinojson 6.10.0
ArduinoJson 6.10.0
March 22, 2019 - When you increment the value of an integer beyond its capacity, you cause an “integer overflow.” In C++, unsigned integer overflow is well defined: when value passes the maximum, it restarts from zero.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
[Solved] Problem with overflowing integer - Programming - Arduino Forum
August 2, 2019 - Every named variable here is 16bits integer (by default). The goal was to retrieve the length of the weight vector exerced on my accelerometer · The result of this should always be near 16384, as it is corresponding to 1g for my accelerometer. My issue is that it, no matter what, gives me ...
🌐
GitHub
github.com › arduino › Arduino › issues › 3590
integer overflow during variable declaration · Issue #3590 · arduino/Arduino
July 26, 2015 - I'm seeing any calculation in a variable declaration fail if the result is over 32000 (probably 32768?). long a = (1000 * 32 ); //compiles OK long b = (1000 * 33 ); //compiler warning: integer overflow in expression [-Woverflow] long c =...
Author   arduino
🌐
Arduino Forum
forum.arduino.cc › projects › programming
I cant overflow some int - Programming - Arduino Forum
October 21, 2020 - Hi,,,can anyone please help ? I am trying to overflow an int but i cant.. Why? I thought that int in Arduino Uno R3 is supposed to overflow at 32.768 ... (Or is it because my arduino is not original?...) The following code is overflowing at 65536 (2 ^ 16) but not in 32768 ...
🌐
Norwegian Creations
norwegiancreations.com › 2018 › 10 › arduino-tutorial-avoiding-the-overflow-issue-when-using-millis-and-micros
Arduino Tutorial: Avoiding the Overflow Issue When Using millis() and micros() – Norwegian Creations
October 11, 2018 - Here we will get a buggy behavior after approximately 50 days when millis() will go from returning a very high number (close to (2^32)-1) to a very low number. This is known as overflow or rollover.
🌐
Lucky Resistor
luckyresistor.me › 2019 › 07 › 10 › real-time-counter-and-integer-overflow
Real Time Counter and Integer Overflow | Lucky Resistor
July 10, 2019 - It is important you never assume, signed integers wrap over in the same way unsigned ones do. Just imagine, you see the least significant part of a larger value: The grey part on the left does not exist. It just explains the principle of the operation. On the CPU level, only one bit of the grey part is kept in the form of an overflow or carry bit.
🌐
GitHub
github.com › wolfSSL › wolfMQTT › issues › 235
Integer overflow on Arduino (ATmega) in "MqttDecode_Vbi" · Issue #235 · wolfSSL/wolfMQTT
September 27, 2021 - For regular Arduino ATmega-chipsets (Uno, Mega, etc.) integers are 16 bits, which means the part at lines 229-232 overflow since the do/while-loop is designed to run up to four times and "MQTT_PACKET_LEN_ENCODE_MASK" is defined as 0x80 in ...
Author   wolfSSL
🌐
Arduino Forum
forum.arduino.cc › projects › general guidance
Integer overflow - General Guidance - Arduino Forum
October 4, 2016 - /Users/HalimHidayat/Documents/Arduino/SP02Test/SP02Test.ino: In function 'void loop()': /Users/HalimHidayat/Documents/Arduino/SP02Test/SP02Test.ino:27:30: warning: integer overflow in expression [-Woverflow] inptI = -(inptV5/(250000001023))1000000; ^ /Users/HalimHidayat/Documents/Arduino/S...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Integer overflow - 2+1 Traps for young players (like me) - Programming - Arduino Forum
October 25, 2022 - I had to do some testing with different settings (I did this a bit generic and careless, admittedly). For some reason, I fell into a number of traps. I hope you can avoid them. The last trap in particular is irritating. Let us begin - What do you expect as a result of this? int8_t i8 = 1000000000 ; int8_t i8f = 1000000000.0f ; I expected both to go into overflow 127 since it is a signed int8.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Basic overflow, type size, and Serial.print unexpected behavior - Programming - Arduino Forum
May 30, 2022 - Trying out a couple of things and got unexpected behavior from printing an int value. Questions: Why is it behaving like this? How do I fix it so that I have a counter that rolls over at a 16bit boundary? Did this test: Test the rollover of in int variable in a for loop.
🌐
Arduino
arduino.cc › en › Reference › int
int | Arduino Documentation
1int countUp = 0; //creates a variable integer called 'countUp' ... 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" ...