I have a project which uses gcc version 4.6.3, and I'm forced to compile with "-Wall -Werror -Wconversion". The following simple example shows an error I can't get rid of:
#include <stdint.h>
int main(void) {
uint32_t u = 0;
char c = 1;
u += c;
return (int)u;
}
Compiling it with the above flags gives:
test.c:7:8: error: conversion to ‘uint32_t’ from ‘char’ may change the sign of the result [-Werror=sign-conversion]
Ok, fine. Just add a typecast, right? Nope. Changing line 7 to u += (uint32_t)c
does not make the error go away. Even changing it to u = u + (uint32_t)c
does not make it go away.
Is it possible to fix this?
Please note that the "char" is coming from a string, so I don't have the option to change its type.
The issue is with signed (negative) character. You might try
u += (unsigned) (c&0xff);
This compiles fine here:
u += (unsigned char)c;
This will only silence the warning, however — without doing anything to each c
at run-time, unlike Basile's proposal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With