i'm getting errors our static analysis tool following snippet:
uint8_t value = 24u; char buffer[512]; int chars_printed = snprintf(buffer, sizeof(buffer), "the value in hex 0x%02hhx\r\n", value);
the error is:
misra-2004 rule 10.1 violation: implicitly converting non-constant expression in function argument. converting "value", underlying type "unsigned char" (8 bits, unsigned), type "int" (32 bits, signed).
what signedness , bit-width misra expecting "%x" specifier?
the "%x" said take unsigned int
cppreference page.
there no errors iar compiler's misra c 2004 checker.
1 coverity.
the problem printf family implicitly promotes arguments of small integer type int
. implicit type promotions of kind not allowed rule 10.1 , why misra violation error. has nothing format specifier.
for misra-compliance, cast value explicitly before passing function: (uint32_t)value
.
please note misra doesn't allowed use stdio.h in production code.