Gcc does not give any warnings when converting from 64 bits to 32 bits

This is my code:

int main() { uint64_t a = 100; uint32_t b = a; return 0; } 

Gcc command:

 ~$ gcc -o 1 1.c -Wall ~$ 

Can anyone help me?

+4
source share
2 answers

Use the -Wconversion option.

-Wconversion Warn about implicit conversions that may change the value. This includes conversions between real and integer, such as abs (x) when x is double; Conversions between signed and unsigned, for example unsigned ui = -1; and conversions to smaller types, for example sqrtf (M_PI). Do not warn about explicit casts such as abs ((int) x) and ui = (unsigned) -1, or if the value does not change during conversion, as in abs (2.0). Conversion warnings between unsigned and unsigned integers can be disabled using -Wno-sign-conversion.

For C ++, also warn of confusing overload resolution for custom conversions; and conversions that never use the type conversion operator: conversions to void, the same type, base class, or a reference to them. Conversion warnings between signed and unsigned integers are disabled by default in C ++ unless -Wsign-conversion is explicitly enabled.

In your code:

converting to "uint32_t {aka unsigned int}" from "uint64_t {aka long long unsigned int}" may change its value [-Wconversion]

+5
source

It is a common misconception that -Wall includes all warnings.

It includes β€œall design warnings that some users find dubious, which are easy to avoid (or modify to prevent warnings) even when combined with macros” (citing the GCC manual).

Even -Wextra only "includes some additional warning flags that are not activated by -Wall" (again from the GCC manual).

There is also -pedantic , which generates warnings in cases where the meaning of the code is clear to the compiler, but the standard requires that the corresponding compiler produce a message. (By default, GCC needs to continue compiling).

Even if all three are turned on, you will not receive every warning that the compiler can provide. See the compiler manual for more details.

+2
source

All Articles