Pelles C forum

C language => Beginner questions => Topic started by: CFred on May 16, 2025, 03:17:26 PM

Title: Can swprintf_s handle wide characters?
Post by: CFred on May 16, 2025, 03:17:26 PM
The following code does not do anything other than demonstrate an issue I experienced with swprintf_s():

#define _UNICODE
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <tchar.h>

int main(void)
{
wchar_t szPath[1024];
wchar_t arguments[1024];
wcscpy(szPath, L"Testing");

swprintf_s(arguments, 1024, L"\"%s\"", szPath);

return 0;
}

This produces an error message:

warning #2234: Argument 4 to 'swprintf_s' does not match the format string; expected 'char *' but found 'wchar_t *'.

Does this mean that argument 4 cannot handle wchar_t strings or have I missed something?
Title: Re: Can swprintf_s handle wide characters?
Post by: John Z on May 16, 2025, 03:21:07 PM
Hi CFred,

try this

swprintf_s(arguments, 1024, L"\"%ls\"", szPath);// lower case L

John Z
Title: Re: Can swprintf_s handle wide characters?
Post by: CFred on May 16, 2025, 04:44:54 PM
Thanks, John Z. This did the trick.