Skip to content

Latest commit

 

History

History
48 lines (35 loc) · 1.64 KB

c6303.md

File metadata and controls

48 lines (35 loc) · 1.64 KB
description title ms.date f1_keywords helpviewer_keywords
Learn more about: Warning C6303
Warning C6303
03/07/2023
C6303
WCHAR_CHAR_ARGUMENT_TO_FORMAT_FUNCTION
__WARNING_WCHAR_CHAR_ARGUMENT_TO_FORMAT_FUNCTION
C6303

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