c - gcc implicit signedness of constants -
i've encountered interesting behavior gcc's interpretation of signedness of constants. have piece of code (greatly simplified) looks below:
#define specific_value 0xffffffff //... int32_t value = something; if (value == specific_value) { // } when compile above, warning: comparison between signed , unsigned integer expressions [-wsign-compare]
all , -- seems gcc interprets hex constant unsigned, , doesn't comparison signed integer. however, if change define #define specific_value 0x7fffffff, warning goes away. again, i'm not particularly surprised -- sign bit being 0 make gcc happier interpreting constant signed value. surprises me if change definition #define specific_value int32_c(0xffffffff), still warning. expect explicitly telling compiler interpret constant signed value silence warning.
read c11 § 6.3.1.1 conversions applied integers. § 6.4.4.1 ¶5 specifies type given integer constant. gcc should stick these rules.
the hex constant is unsigned (by standard) int (presuming 32 bit integers). conforms , not chance!
if clear msbit, constant can represented (signed) int, however. comparison goes well. still standard.
the third message lacking definition of int32_c, cannot that. think can solve now. keep in mind, error cannot detected inside `#define', after macro has been expanded.
general rule either add u constant (yes, hex) if want unsigned. or cast constant:
#define uvalue ((uint32_t)0x7fffffff) that better here, not rely on size of int actually.
Comments
Post a Comment