函数名: strcmp
头文件: <string.h>
函数原型: int strcmp(const char *str1,const char *str2);
功 能: 比较两个字符串的大小,区分大小写
参 数: str1和str2为要比较的字符串
返回值: str1 > str2 , 返回 1;
str1 < str2 , 返回 -1;
str1 == str2 , 返回 0;
程序例:比较两个字符串的大小,并输出结果
#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;}
运行结果:
buffer2isgreaterthanbuffer1buffer2islessthanbuffer3