C 如何比较两个字符串的If值?

C 如何比较两个字符串的If值?,c,C,我想比较两个字符串,并显示每个玩家的胜利数量。我不太明白string.h库是如何工作的,但在搜索中我已经展示了它应该适用于这种比较 #include <stdio.h> #include <string.h> int main() { printf("Player 1: "); scanf("%s", &play1); printf("Player 2: "); scanf("%s", &play2);

我想比较两个字符串,并显示每个玩家的胜利数量。我不太明白string.h库是如何工作的,但在搜索中我已经展示了它应该适用于这种比较

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

int main()
{
    printf("Player 1: ");
    scanf("%s", &play1);
    printf("Player 2: ");
    scanf("%s", &play2);            
    printf("Total matches: ");
    scanf("%d", &t_matches);

    for (i = 1; i <= t_matches; i++) {
        printf("Winner match %d: ", i);
        scanf("%s", &win1);
        if (strcmp(win1, play1)) {
            p1++; 
        } else if(strcmp (win1, play2)) {
            p2++; 
        }
    }
    printf("%s win %d matches\n", play1, p1);
    printf("%s win %d matches\n", play2, p2);
}
如果字符串相等,strcmp函数返回0。您正在检查它们是否不相等。相反,您希望:

if (strcmp(win1, play1) == 0) {
    p1++; 
} else if(strcmp (win1, play2) == 0) {
    p2++;
}
win1、play1和play2未在任何位置声明。另请参阅和