Question

Which data type do I use to store the following value ?

Value: 86400

code:

int d[6];
unsigned int all;
d[0] = 8;
d[1] = 6;
d[2] = 4;
d[3] = 0;
d[4] = 0;

all =  10000*d[0] + 1000*d[1] + 100*d[2] + 10*d[3] +  d[4];
printf("%u", all);

If I copy this code to CodeBlocks, it works fine. However, on MPLAB C18 compiler I would get a different result.

CodeBlock Output: 86400

MPLAB Output: 20864

How do you fix it? Thank you!

Était-ce utile?

La solution

The type int is not guaranteed to be 32 bits, it may be e.g. 16 bits (i.e. have values from 0 to 65535 for unsigned int). This is the case here. You need to use the long type here.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top