Function name : strcmp
Header file : <string.h>
Function prototype : int strcmp(const char *str1,const char *str2);
Function : Compare the size of two strings, case sensitive
Parameters : str1 and str2 are the strings to be compared
Return value : str1 > str2, return 1;
str1 < str2, return -1;
str1 == str2, return 0;
Program example: Compare the sizes of two strings and output the result
#include<string.h>#include<stdio.h>intmain(void){char*buf1=aaa,*buf2=bbb,*buf3=ccc;intptr=strcmp(buf2,buf1);if(ptr>0) {printf(buffer2isgreaterthanbuffer1n);}elseif(ptr<0){printf(buffer2islessthanbuffer1n);}else{printf(buffer2isequalsbuffer1n);}ptr=strcmp(buf2,buf3);if(ptr>0){ printf(buffer2isgreaterthanbuffer3n);}elseif(ptr<0){printf(buffer2islessthanbuffer3n);}else{printf(buffer2isequalsbuffer3n);}return0;}
Running results:
buffer2isgreaterthanbuffer1buffer2islessthanbuffer3