问题
char *x="Çankırı";
char *y=malloc(sizeof(char)*25);
scanf("%s",y);
if(strcmp(x,y) == 0)
printf("A");
else
printf("%s",y);
I enter 'Çankırı' for y, but else part runs. How can I compare these strings?
Windows10 , codeblocks.
EDIT: I found a clue. Problem is about setlocale function. When I use setlocale(LC_ALL,"TURKISH"), one of the string doesn't work fine(Output is not Çankırı, Ank2r2), and If I use setlocale(LC_ALL,"C"), other one doens't work fine. I don't know how to fix it.
回答1:
You probably need to use functions that accept 'wide' characters. For example:
#include <wchar.h>
wchar_t *x=L"Çankırı";
wchar_t y[25];
wscanf(L"%s",y);
if(wcscmp(x,y) == 0)
wprintf(L"A");
else
wprintf(L"%s",y);
来源:https://stackoverflow.com/questions/42542177/strcmp-function-doesnt-work-while-comparing-string-and-user-input-which-include