Why does an unsigned long constant declaration give the wrong value?

Kind of out of place but i have just find that while looking for something else and looks like i have been doing the following:


#define maxStpT 1250000

unsigned long mtrStpT = maxStpT+4;
unsigned long mtrSTTP = 1250000+4;

const unsigned long MAX = maxStpT+4;
const unsigned long MIN = 1250000+4;

void setup() {
Serial.begin (2000000);
}

void loop() {
Serial.print (mtrStpT);
Serial.print (" ");
Serial.print (mtrSTTP);
Serial.print (" ");
Serial.print (MIN);
Serial.print (" ");
Serial.println (MAX);
}

ALL i get through the serial monitor is
"1250004 1250004 1250004 1250004"
which looks about right.

Putting UL like :
const unsigned long MIN = 1250000ul+4;
still returns 1250004

BUT what about maxStpT ? Is there anything you guys would advise me to keep an eye on?
So anyone knows whether i should still use the ul after "1250000" or did they change the way maths are being done on the unsigned long?
All the above is also true when using * insead of +

The title is odd since the calculations you have shown are indeed correct, but it looks like you are wondering why it is necessary to append UL to unsigned long numbers.

The #define statement just does a textual substitution so as long as your 1250000 lands somewhere where an unsigned long is handled correctly, it will work.

But try this as your first line :

#define maxStpT 125 * 10000

and then this:

#define maxStpT 125UL * 10000

and you should notice a difference. The preprocessor treats such calculations as int unless somehow forced to do otherwise.

So i should only keep an eye on the non INT calculations that are place on the preprocessor part?
thanks