Dotcpp  >  编程教程  >  mem.h头文件  >  C语言memcmp()函数:比较m字节长的两个字符串s1和s2

C语言memcmp()函数:比较m字节长的两个字符串s1和s2

点击打开在线编译器,边学边练

函数名:memcmp

头文件:<mem.h>

函数原型: void* memcmp(void *s1,void *s2,unsigned m);

功能:比较m字节长的两个字符串s1和s2

参数: s1 和 s2 为要比较的字符串

           unsigned m  为要比较的前m个字节

返回值:s1<s2   返回-1

             s1=s2   返回等于0

             s1>s2   返回1


程序例:比较给定的字符串s1和s2的大小,并将结果输出

#include<stdio.h>

#include<mem.h>

#include<string.h>

int main(void){

   char *s1="I love www.dotcpp.com!";

   char *s2="I like www.dotcpp.com!";

   int* t = (int*)memcmp(s1,s2,strlen(s1));

   if(!t){

      printf("s1 is the same as s2\n");

   }else if(t>0){

      printf("s1 is great than s2\n");

   }else{

      printf("s1 is less than s2\n");

   }

   return 0;

}

 

运行结果

s1 is great than s2



本文固定URL:https://www.dotcpp.com/course/491

C语言函数库
assert.h头文件
ctype.h头文件
float.h头文件
io.h头文件
math.h头文件
mem.h头文件
setjmp.h头文件
stdio.h头文件
stdlib.h头文件
sigal.h头文件
string.h头文件
time.h头文件
Dotcpp在线编译      (登录可减少运行等待时间)