In C, I encounter this weird problem:
For the following code:
#include <stdio.h>
#include <math.h>
int main(int argc, char *argv[]) {
unsigned int i = 0;
for (int j = i - 1; j < i + 1; j++) {
printf("RUN1?\n");
}
int j = i - 1;
int k = i + 1;
for (; j < k; j++) {
printf("RUN2?\n");
}
printf("DONE?");
}
The first loop did not loop (0 times) while the second loop can run 2 times. I don’t see what’s fundamentally different.
The below also works…
for (;j < (int)(i+1); j++) {}
I have seen some posts here, but did not find any good explanation.
Maybe someone can link one here?
>Solution :
The difference between the 2 loops is in the types used for the condition:
- In the 1st loop the condition is:
j < i + 1. Sinceiis unsigned (and so isi+1)jis also converted tounsignedto perform the comparison. As anunsignedit is a high number and never< i+1. - In the 2nd loop the condition is
j < k. These are both signed values and therefore they are not converted tounsigned.jis initialized to-1wheni-1is converted tosigned int. The result is the one you expected (2 iterations of the loop).
Note:
When the unsigned i-1 is converted to signed you get an overflow. The result is implementation defined. A common result (which you also get) is -1 but it is not guaranteed.