Search code examples
cgcctype-conversionimplicit-conversiongcc-warning

Why no warning when uint32 casts to uint8 with gcc C


Compiling with -Wconversion,

uint32_val = 0x00000C00;
uint_8_val = ((uint32_val >> 8) & 0x000000FF);

gives no warning.

What compiler flag shall I use to get a warning?


Solution

  • There is no warning because the expression:

    ((uint32_val >> 8) & 0x000000FF)
    

    is always convertible to a type that fits in a byte. Your uint32_val is not cast, but takes part in a calculation. The compiler can optimize this, and in this case it is clear the result after bit-wise and-ing with 000000ff will always fit the provided l-value type, hence no warning.