0

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!

Ammar
  • 1,203
  • 5
  • 27
  • 64
  • Didn't you already ask this question here? - [Previous Thread](http://stackoverflow.com/questions/16403009/how-to-store-values-in-an-array-to-a-variable-on-mplab) – embedded_guy May 07 '13 at 05:22

1 Answers1

1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621