I am building my project with the warning sign GCC -Wconversion. (gcc (Debian 4.3.2-1.1) 4.3.2) on a 64-bit GNU / Linux OS / hardware. I find it useful in determining where I mixed types or lost clarity about which types should be used.
This is not very useful in most other situations that trigger his warnings, and I ask how I should deal with them:
enum { A = 45, B, C }; char a = A; char b = a + 1; char c = B - 2; char d = (a > b ? b : c)
Due to the unexpected results of the above tests (cases a and c ), I also ask you to explain these differences.
Edit: And the add-in should include all this with (char) to prevent a warning?
Edit2: some additional cases (following from above):
a += A; a++; a += (char)1;
In addition, what I ask is subjective, and I would like to hear how other people deal with conversion warnings in cases where you think that some developers protect the removal of all warnings.
Yae:
One possible solution is to just use int instead of char right? Well, actually, this not only requires more memory, but also slower, as the following code indicates. Math expressions are only for warnings when building with -Wconversion . I assumed that the version using char variables will work slower than when using int due to conversions, but in my (64-bit system with two cores) the version of int slower.
#include <stdio.h> #ifdef USE_INT typedef int var; #else typedef char var; #endif int main() { var start = 10; var end = 100; var n = 5; int b = 100000000; while (b > 0) { n = (start - 5) + (n - (n % 3 ? 1 : 3)); if (n >= end) { n -= (end + 7); n += start + 2; } b--; } return 0; }
Pass -DUSE_INT to gcc to create an int version of the above snippet.