用strcmp函数比较字符串,返回的值不正确

#include <stdio.h>
#include <stdlib.h>
int main()
{
char x[20],y[20];
scanf("%s %s",&x,&y);
int f;
f=strcmp(x,y);
printf("f=%d",f);
return 0;
}

img

x和y在哪定义呢???
strcmp的返回值
当等于0时,两个字符串想等
当>0时,第一个字符串大于第二个字符串
当<0时,第一个字符串小雨第二个字符串
128大于0,表明第一个字符串大于第二个字符串,没毛病

首先,你的x和y变量没有定义
第二,strcmp函数的返回值
当等于0时,两个字符串想等
当>0时,第一个字符串大于第二个字符串
当<0时,第一个字符串小雨第二个字符串
x比y大,所以返回的是一个大于0的数
解决问题请采纳

测试图:

img


代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char x[20],y[20];
    gets(x);
    getchar();
    gets(y);
    printf("%d\n",strcmp(x,y));    
    return 0;
}



希望对题主有所帮助,可以的话,帮忙点个采纳!