Home  >  Article  >  Backend Development  >  What is the usage of strcmp function in C language?

What is the usage of strcmp function in C language?

coldplay.xixi
coldplay.xixiOriginal
2020-07-06 11:22:0916709browse

c语言strcmp函数用法:语法结构为【int strcmp(char *str1, char *str2)】,比较字符串str1和str2是否相同,如果相同则返回0,如果不同,在不同的字符处如果str1的字符大于str2字符,则返回负1。

What is the usage of strcmp function in C language?

c语言strcmp函数用法为:

语法结构为:

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 What is 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