Warning C6303

Format string mismatch.

Remarks

This warning indicates that a format string specifies a narrow character string, but is being passed a wide 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: WCHAR_CHAR_ARGUMENT_TO_FORMAT_FUNCTION

Example

The following sample code generates this warning. buff is a wide character string, but the printf_s call expects a short string as denoted by %s:

#include <stdio.h>

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

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

#include <stdio.h>

void f()
{
    wchar_t buff[5] = L"hi";
    printf_s("%ls", buff);
}

See also

Format specification syntax: printf and wprintf functions
C4477
C6302