In statements like these, where both are entered into the source code with the same encoding (UTF-8) and the locale is set up properly, is there any practical difference between them?
printf("ο Δικαιοπολις εν αγρω εστιν\n");
printf("%ls", L"ο Δικαιοπολις εν αγρω εστιν\n");
And consequently is there any reason to prefer one over the other when doing output? I imagine the second performs a fair bit worse, but does it have any advantage (or disadvantage) over a multibyte literal?
EDIT: There are no issues with these strings printing. But I'm not using the wide string functions, because I want to be able to use printf
etc. as well. So the question is are these ways of printing any different (given the situation outlined above), and if so, does the second one have any advantage?
EDIT2: Following the comments below, I now know that this program works -- which I thought wasn't possible:
int main()
{
setlocale(LC_ALL, "");
wprintf(L"ο Δικαιοπολις εν αγρω εστιν\n"); // wide output
freopen(NULL, "w", stdout); // lets me switch
printf("ο Δικαιοπολις εν αγρω εστιν\n"); // byte output
}
EDIT3: I've done some further research by looking at what's going on with the two types. Take a simpler string:
wchar_t *wides = L"£100 π";
char *mbs = "£100 π";
The compiler is generating different code. The wide string is:
.string "\243"
.string ""
.string ""
.string "1"
.string ""
.string ""
.string "0"
.string ""
.string ""
.string "0"
.string ""
.string ""
.string " "
.string ""
.string ""
.string "\300\003"
.string ""
.string ""
.string ""
.string ""
.string ""
While the second is:
.string "\302\243100 \317\200"
And looking at the Unicode encodings, the second is plain UTF-8. The wide character representation is UTF-32. I realise this is going to be implementation-dependent.
So perhaps the wide character representation of literals is more portable? My system will not print UTF-16/UTF-32 encodings directly, so it is being automatically converted to UTF-8 for output.
%s
rather than%ls
. Or I'm still misunderstanding the question. – Doorpost