Puzzling behavior with modulo operator

define EEPROM_DBG_START 100

define EEPROM_MAX_CODES 50

define EEPROM_DBG_END EEPROM_DBG_START + EEPROM_MAX_CODES

unsigned int dbgAddress;

void setup() { unsigned int temp;

Serial.begin(115200);

dbgAddress = EEPROM_DBG_START;

// put your setup code here, to run once: Serial.println(dbgAddress); dbgAddress++; Serial.println(dbgAddress);

Serial.println(EEPROM_DBG_END);

temp = dbgAddress; dbgAddress = temp % EEPROM_DBG_END; Serial.println(dbgAddress); }

The output is 100 101 150 51

I expected the output to be 100 101 150 101

#define EEPROM_DBG_END     (EEPROM_DBG_START + EEPROM_MAX_CODES)

Thanks. That works. When I printed EEPROM_DBG_END, it printed the correct value. So, it's strange that the modulo doesn't work as I expected.

No, that is not strange and the operator works as expected.

mod = value % modVal + sameValue;

is different to

mod = value % (modVal + sameValue);

and that is expected.

Got it now. Thanks.

If you use (at least for EEPROM_DBG_END)

const byte EEPROM_DBG_START = 100;
const byte EEPROM_MAX_CODES = 50;
const byte EEPROM_DBG_END = EEPROM_DBG_START + EEPROM_MAX_CODES;

you don’t need the braces around the addition.

#define uses textual replacement, so it is more context sensitive.