How can I fix warnings like: “comparison between signed and unsigned”?

Replace

int x;
/* ... */
for(x=0;x<sizeof(arr) / sizeof(int);x++)

by

for(size_t x=0;x<sizeof(arr) / sizeof(int);x++)

But it doesn’t solve the general problem when I have to compare signed values with unsigned values using these compiler options. Is there an cleaner way insted of casting?

In such cases, try to figure out if the signed number can ever have a value which will cause overflow. If not, you can ignore the warning. Otherwise a cast to the unsigned type (if this is the same size or bigger than the signed component) is good enough.

Leave a Comment