Home>Article>Backend Development> What is the usage of strncmp function?
The usage of strncmp function is to compare the first n characters of string str1 and str2. It is a string comparison function. The code is [int strcmp(char *str1,char * str2, int n)].
[Related learning recommendations:C language tutorial video]
strncmp Function usage:
Function prototype
int strcmp(char *str1,char * str2,int n)
Function
Compare the first n characters of string str1 and str2 character.
Header file
#include
Return value
Return value: Return integer value: When str1551862083d0782c87c6d6045f54f36f7str2, the return value is >0.
Other instructions: None at the moment.
Description
(1) str1: the first string to be compared;
(2) str2: the second string to be compared String;
(3) n: n is the number of characters for the comparison between the specified str1 and str2;
Example:
#include#include int main(void) { char *str1="Hello,I am sky2098,I liking programing!"; char *str2="Hello,I am sky2098,gramk has gone。"; int n=13; //指定比较前13个字符 int inttemp; inttemp=strncmp(str1,str2,n); //将字符串比较的返回值保存在int型变量inttemp中 if(inttemp<0) { printf("strlen(str1) < strlen(str2)"); } else if(inttemp>0) { printf("strlen(str1) > strlen(str2)"); } else { printf("strlen(str1) == strlen(str2)"); } return 0; }
#include#include int main(void) { char *str1="Hello,I am sky2098,I liking programing!"; char *str2="Hello,I am sky2098,gramk has gone。"; int n=strlen(str2); int inttemp; inttemp=strncmp(str1,str2,n); //将字符串比较的返回值保存在int型变量inttemp中 if(inttemp<0) { printf("strlen(str1) < strlen(str2)"); } else if(inttemp>0) { printf("strlen(str1) > strlen(str2)"); } else { printf("strlen(str1) == strlen(str2)"); } return 0; }
If you want to know more about programming learning, please pay attention to thephp trainingcolumn!
The above is the detailed content of What is the usage of strncmp function?. For more information, please follow other related articles on the PHP Chinese website!