Home  >  Article  >  Backend Development  >  C language strcmp function usage

C language strcmp function usage

Guanhui
GuanhuiOriginal
2020-07-30 10:20:184565browse

C language strcmp function usage

C语言strcmp函数用法

strcmp函数语法为“int strcmp(char *str1,char *str2)”,其作用是比较字符串str1和str2是否相同,如果相同则返回0,如果不同,前者大于后者则返回1,否则返回-1。

简单示例

char a[]="abcd";
char *b="abcd";
char *d="abcde";
int d=strcmp(a,b); //那么d的值是0
d=strcmp(b,d); //d的值是-1 因为 '\0' 比'e' 小
d=strcmp(d,b); //d的值是1,因为 'e' 比'\0'大
#include <stdio.h>
#include <string.h>

int main(){
    char str1[50] = { 0 };
    char str2[50] = { 0 };
    int i = 1;

    do {
        printf("******第%d次输入******\n", i);
        gets(str1);
        gets(str2);
        i++;
    } while ( strcmp(str1, str2) );

    return 0;
}

推荐教程:《PHP》《C#

The above is the detailed content of C language strcmp function usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn