Hi Frank,
In your example, I think actually both compilers are giving you a perfectly correct result. However, the results are different based on the ambiguity of your program itself!
Remember, in C, expressions can be evaluated in any order. So that means what you are assuming is the result of the first getch call may infact be the second. So in your code, you may perfectly validly get the calculation:
int length = 256 * 0 + 0x80 // 0x80
- or -
int length = 256 * 0x80 + 0 // 0x8000
and both conform to your program specification. So in the case when you say it runs more than 128 times, I suspect the result is actually the second one. The following example program shows the behaviour:
#include "mbed.h"
unsigned char getch() {
static int first = 1;
if(first) {
first = 0;
return 0;
} else {
return 0x80;
}
}
int main() {
int length = 256 * getch() + getch();
printf("length = %d\n", length);
}
gives:
length = 32768
as expected. So I think the compiler is fine :)
I suspect the first problem in this thread is similarly unrelated to where the problem "appears" to be. If you've looked so hard for the problem that it can't be anything but the compiler broken, it is often actually just you are looking in the wrong place! A good debug method is to talk through the logic of your program to the cardboard cutout/rubber duck/teddy.
Hope that is useful.
Simon
Hi guys,
i've got a big problem.
I've got an unsigned char buffer[255]. I have stored an integer-value in buffer[12]. but when i want to restore the value in buffer[12], the program stops and the cast is not executed.
code of the cast:
unsigned char buffer[255];
int i = 17;
buffer[12] = i; works perfectly
later:
int j = buffer[12]; doesnt work
i've tried lots of implicit and explicit casts, but nothing works. whats the problem? my code should work fine.
thanks
johannes