# problem with constant

if you declare a variable this way: const unsigned int trythis = 60*1000;

you will get a random large value (eg 4294961760)

if you declare it this way: const unsigned int trythis = 60*1000L;

it works correctly.

The compiler is not quite smart enough to work this out - be warned! (btw using Arduino 1.0.1)

The compiler is not quite smart enough to work this out - be warned!

The compiler is performing the arithmetic you told it to. The fact is that you didn't tell it properly, at first.

you will get a random large value

Nothing at all random about it - if you were thinking of using it as a random number seed, be warned! (impressive that you got an "int" to print so many significant digits though)

I don't think you're telling the story exactly as it happened. I can't duplicate that result in 1.01. With this code:

``````const unsigned int trythis = 60*1000;
void setup() {
Serial.begin(112500);
Serial.println(trythis);
}
void loop() {}
``````

I get the expected result, 60000. In order to get the your the result you reported, I have to declare the constant as unsigned long .

In the absence of other instructions, the compiler treats literal integer constants as int. Since int is a signed type, the compiler does signed arithmetic. It dutifully does the multiplication, and the result is an overflow for int, which can't express a number bigger than 32767. In this case, the sign bit is a "1". When the result is copied to another type with more bytes, the sign bit is extended to the left, all the way to the MSB - just like it's supposed to be. When that result is interpreted as unsigned long, it's a whopping big number.

If you want to see the code that you posted actually produce a result that you don't expect, try 66*1000.

The result is certainly not random, though. If you want to see that, print your result and 60000 as hexadecimal. You'll see the resemblance.

AWOL:

you will get a random large value

Nothing at all random about it - if you were thinking of using it as a random number seed, be warned! (impressive that you got an "int" to print so many significant digits though)

It's only an int as far as the Print class. The print(int) method just forwards on to the print(long) method, so the negative int value stored in an unsigned int gets expanded out to a negative long value stored in an unsigned long variable, which is a much huger number.