Warning C6340

Mismatch on sign: 'type' passed as Param(number) when some (signed|unsigned) type is required in call to 'function-name'

Remarks

This warning indicates that sign of the type required by the format specifier and sign of the type of the expression passed in don't match. Using the wrong format specifier is undefined behavior. To fix the warning, make sure that the format specifiers match the types of the expressions passed in.

Code analysis name: FORMAT_SIGN_MISMATCH

Example

#include <cstdio>

void f(unsigned char a)
{
    printf("%hhd\n", a); // C6340 emitted.
}

There are multiple ways to fix the undefined behavior. We can change the format specifier:

#include <cstdio>

void f(unsigned char a)
{
    printf("%hhu\n", a); // No C6340 emitted.
}

We can change the type of the expression:

#include <cstdio>

void f(signed char a)
{
    printf("%hhd\n", a); // No C6340 emitted.
}

As a last resort when overflow can't happen, we can introduce a cast:

#include <cstdio>

void f(long long a)
{
    printf("%d\n", static_cast<int>(a)); // No C6328 emitted.
}

See also

C6328