Edit

Share via


Warning C6302

Format string mismatch.

Remarks

This warning indicates that a format string specifies a wide character string, but is being passed a narrow character string instead. One cause of the warning is because the meaning of %s and %S flip when used with printf or wprintf. This defect can lead to crashes, in addition to potentially incorrect output.

Code analysis name: CHAR_WCHAR_ARGUMENT_TO_FORMAT_FUNCTION

Example

The following code generates this warning. buff is a narrow character string, but wprintf_s is the wide string variant of printf_s and so expects %s to be wide:

void f()
{
    char buff[5] = "hi";
    wprintf_s(L"%s", buff);
}

The following sample code remediates this issue by using %hs to specify a single-byte character string. Alternatively it could have switched to %S, which is a narrow string when used with wprintf like functions. See Format specification syntax: printf and wprintf functions for more options.

void f()
{
    char buff[5] = "hi";
    wprintf_s(L"%hs", buff);
}

See also

Format specification syntax: printf and wprintf functions
C4477
C6303