前言:

 C语言中有很多东西容易搞混,最近笔者就遇到了一个问题。这里做个记录。就是memcmp和strcmp两者的用法,这里做个对比:

 功能对比:

 A memcmp:

函数原型: int memcmp(const void *str1, const void *str2, size_t n));

 功能:比较内存区域buf1和buf2的前count个字节。

 返回值:

  • 如果返回值 < 0,则表示 str1 小于 str2。
  • 如果返回值 > 0,则表示 str2 小于 str1。
  • 如果返回值 = 0,则表示 str1 等于 str2。

 B strcmp函数

 函数原型:int strcmp(const char *s1,const char *s2);

 功能:用于比较两个字符串并根据比较结果返回整数

 返回值:

  • 如果返回值 < 0,则表示 str1 小于 str2。
  • 如果返回值 > 0,则表示 str2 小于 str1。
  • 如果返回值 = 0,则表示 str1 等于 str2。

 源码对比:

  下面这个例子,能够很好的诠释两个函数的用法:

 #include <stdio.h>
#include <string.h> int main()
{
unsigned char test1_arr[] = "hello world";
unsigned char test2_arr[] = "hello world"; int ret = memcmp(test1_arr,test2_arr,strlen(test1_arr));
printf("unsigned char memcmp is:%d \n\t ",ret); char test3_arr[] = "hello world";
char test4_arr[] = "hello world"; int reta = strcmp(test3_arr,test4_arr);
printf("char strcmp is:%d \n\t ",reta); unsigned char test5_arr[] = "hello world";
unsigned char test6_arr[] = "hello world"; int retb = strcmp(test5_arr,test6_arr);
printf("char strcmp is:%d \n\t ",retb); return ;
}

strcmp函数和memcmp函数的用法区别及联系的更多相关文章

  1. php中sprintf与printf函数用法区别

    下面是一个示例:四舍五入保留小数点后两位  代码如下 复制代码 <?php$num1 = 21;echo sprintf("%0.2f",$num1)."<b ...

  2. jquery中attr()与prop()函数用法实例详解(附用法区别)

    本文实例讲述了jQuery中attr()与prop()函数用法.分享给大家供大家参考,具体如下: 一.jQuery的attr()方法 jquery中用attr()方法来获取和设置元素属性,attr是a ...

  3. php中strstr、strrchr、substr、stristr四个函数用法区别

    php中strstr.strrchr.substr.stristr四个函数用法区别: php中strstr strrchr substr stristr这四个字符串操作函数特别让人容易混淆,常用的是s ...

  4. php中print_r、var_dump和var_export几个函数的用法区别

    php中print_r.var_dump和var_export几个函数的用法区别

  5. [Reprint]C++函数前和函数后加const修饰符区别

    c++中关于const的用法有很多,const既可以修饰变量,也可以函数,不同的环境下,是有不同的含义.今天来讲讲const加在函数前和函数后面的区别.比如: 01 #include<iostr ...

  6. C++函数前和函数后加const修饰符区别

    class Test(){ public: Test(){} const int foo(int a); const int foo(int a) const; }; 一.概念 当const在函数名前 ...

  7. <系统函数实现>memcmp

    这是我实现的memcmp函数: #include <stdio.h> #include <string.h> /* *int memcmp (const void *s1,co ...

  8. C/C++之宏、内联函数和普通函数的区别

    内联函数的执行过程与带参数宏定义很相似,但参数的处理不同.带参数的宏定义并不对参数进行运算,而是直接替换:内联函数首先是函数,这就意味着函数的很多性质都适用于内联函数,即内联函数先把参数表达式进行运算 ...

  9. render函数和redirect函数的区别+反向解析

    render函数和redirect函数的区别+反向解析 1.视图函数:一定是要包含两个对象的(render源码里面有HttpResponse对象)   request对象:----->所有的请求 ...

随机推荐

  1. 【RN - 基础】之View使用简介

    简介 View是一个容器,支持FlexBox布局. View既可以作为容器容纳其他组件,也可以作为一个组件包含进另一个容器中. 无论运行在哪个平台上,View都会直接对应这个平台的原生视图,如iOS中 ...

  2. 英语口语考试资料Family

    I Love my family   12 years  ago, I was born in a happy family, there was a gentle father, a beautif ...

  3. 利用scatter()绘制颜色映射的二次方曲线

    程序如下: import matplotlib.pyplot as plt x_value = list(range(1, 1001)) y_value = [x**2 for x in x_valu ...

  4. 月下无限连?拒绝无休止switch!

    拒绝无休止switch 一.前言 前天碰到个需求,其实很简单,就是Excel导入,Excel模板长下面这样: 按我平常的逻辑是这样做的: 用文件输入流读取Excel,根据Excel的版本生成不同的对象 ...

  5. mysql查询出所有重复的记录

    假如我们有如下一张数据表(很简单,只是举例而已),表名为student.现在我们要取出其中重复记录.重复是以name相同为判定标准. ID name phone age 1 张三 10086 15 2 ...

  6. PHP的array_walk和array_map函数实现数组值UTF-8转GBK编码

    在PHP中,array_walk() 和 array_map()两个函数都可以实现对数组中每个值的修改,比如本例就是将数组中所有的值,由UTF-8编码转成GBK编码. 当然,除了这两个函数,也可以用 ...

  7. Java修炼——手写服务器项目

    项目工程总览: 1.Dispatcher类(一个请求与响应就是一个Dispatcher) package com.bjsxt.server; import java.io.IOException; i ...

  8. CF547E Milk and Friends(AC自动机的fail指针上建主席树 或 广义后缀自动机的parent线段树合并)

    What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercas ...

  9. Codeves-5037线段树4加强版(线段树? 。。。分块)

    维护一个序列,要求支持下列2种操作: add a b c:区间[a,b]中每个数加上c count a b:查询区间[a,b]中有多少数是k的倍数(k为给定常数) 输入描述 Input Descrip ...

  10. Light oj 1140 How Many Zeroes?

    Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...