简体   繁体   中英

wcscoll returns different result than expected

Consider this code:

#include <wchar.h>
#include <locale.h>
#include <stdio.h>

int main(void) {
    setlocale(LC_ALL, "pl_PL.UTF-8");
    printf("%d\n", wcscoll(L"ą", L"b"));
    return 0;
}

The output is

158

But I'd expect it to be -1 , since ą is just before b in the polish alphabet. Why did it return 158? And if not in this way, how can one compare words alphabetically?

I tried it in my Linux machine and I get 1 as output, positive just like yours.

Then I edited the supported locales at /etc/locale.gen , uncommented pl_PL.UTF-8 (not enabled by default), run sudo locale-gen and now it gives -4 , that is negative, as expected.

The conclusion is that your system configuration, as is, does not support the selected locale.

Check the return value of setlocale ; it's probably not recognizing your country/codepage string.

MS locale names use dashes, not underscores . If you're on Windows, try passing pl-PL instead of pl_PL.UTF-8 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM