函數名: 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,b uf1);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);}elseif(ptr<0){printf(buffer2islessthanbuffer3n);} {printf(buffer2isequalsbuffer3n);}return0;}
運行結果:
buffer2isgreaterthanbuffer1buffer2islessthanbuffer3