Home >Backend Development >C#.Net Tutorial >How to use the string comparison function strcmp in C++?
#What is the usage of string comparison function strcmp in C?
Function prototype:
int strcmp(const char *s1, const char *s2);1
Header file:
#include bbed3fed50f96ac7490cfc6a498c4bc51
Function: Used to compare two strings
Parameters: s1 and s2 are two for comparison. String
Return value: If the strings s1 and s2 are equal, zero is returned; if s1 is greater than s2, a number greater than zero is returned; otherwise, a number less than zero is returned.
Explanation: The strcmp() function compares two strings based on the value of the ACSII code; the strcmp() function first subtracts the first character value of s2 from the first character value of the s1 string. character, if the difference is zero, continue the comparison; if the difference is not zero, return the difference.
Until different characters appear or '\0' is encountered.
Special note: strcmp(const char *s1, const char * s2)Only strings can be compared here, numbers and other parameters cannot be compared.
Code example:
#include <string.h> int main(void){ char *p="aBc"; char *q="Abc"; char *h="abc"; printf("strcmp(p,q):%d\n",strcmp(p,q)); printf("strcmp(p,h):%d\n",strcmp(p,h)); return 0;} //结果: //strcmp(p,q):32 //strcmp(p,h):-32
Recommended tutorial: "C Language"
The above is the detailed content of How to use the string comparison function strcmp in C++?. For more information, please follow other related articles on the PHP Chinese website!