preprocessor truncates long values to int

I've tried to define a long value for number of seconds in a day with a '#define' and a 'static const long'. Both get truncated by the preprocessor, but only when the value is calculated.

EG.

static const long seconds_in_a_day = 246060; // seconds_in_a_day == 20864

static const long seconds_in_a_day = 86400; // seconds_in_a_day == 86400

Note:

86400 == 246060

20864 == 86400 & 0x7fff

Looks like all preprocessor math is done as a 16 bit int. A bit odd, I've never seen a preprocessor do this.

I've search the forum and can't seen it discussed. Do you think it's a bug? Should I report it?

Many thanks,
Richard. :slight_smile:

static const long seconds_in_a_day = 24L*60L*60L;

It's not the preprocessor's fault and it's not a bug.

Wow, fast response!

I was hoping it was going to be something like this. Had me scratching my head for quite a while this one. :slight_smile: Spent too long in the PC world. :wink:

Thanks.

That exact example is discussed here: Gammon Forum : Electronics : Microprocessors : Integer arithmetic and overflow

I even got the same result: 20864

Spent too long in the PC world.

The size of "int" varies from platform to platform. The compiler is doing exactly what it is supposed to.