문제

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!

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top