Home >Backend Development >C#.Net Tutorial >Detailed explanation of the usage of strcmp function in C language

Detailed explanation of the usage of strcmp function in C language

hzc
hzcOriginal
2020-07-04 13:15:4215988browse

在c语言中可以使用“strcmp()”函数比较字符串str1和str2是否相同,该函数用于比较字符串,其语法是“strcmp($str1,$str2)”,其参数$str1,$str2表示要比较的字符串。

Detailed explanation of the usage of strcmp function in C language

int strcmp(char *str1, char *str2);

比较字符串str1和str2是否相同。如果相同则返回0;

如果不同,在不同的字符处如果str1的字符大于str2的字符,则返回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'大

推荐教程: 《c语言教程

The above is the detailed content of Detailed explanation of the usage of strcmp function in C language. 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