printf, wprintf, %s, %S, %ls, char* and wchar*: Errors not announced by a compiler warning?

前端 未结 6 1983
自闭症患者
自闭症患者 2020-12-02 23:29

I have tried the following code:

wprintf(L\"1 %s\\n\",\"some string\"); //Good
wprintf(L\"2 %s\\n\",L\"some string\"); //Not good -> print only first char         


        
6条回答
  •  無奈伤痛
    2020-12-02 23:39

    At least in Visual C++: printf (and other ACSII functions): %s represents an ASCII string %S is a Unicode string wprintf (and other Unicode functions): %s is a Unicode string %S is an ASCII string

    As far as no compiler warnings, printf uses a variable argument list, with only the first argument able to be type checked. The compiler is not designed to parse the format string and type check the parameters that match. In cases of functions like printf, that is up to the programmer

提交回复
热议问题