zoukankan      html  css  js  c++  java
  • strings.h 与 string.h 头文件的区别

    今天使用 man string 来查看 string 文件的使用的方法(毕竟里面的函数名字和传入参数和发挥参数的类型,如果一段时间不使用,会产生遗忘。)

    偶然发现,string.h 的man page 中 出现了 strings.h 的说明。这引起的我的好奇,很奇怪这个strings 和 string 之间的关系。我上网搜了几个帖子,他们写的不够清楚,今天我进行重新整理一下吧:

    首先我们看一下man string 里面的内容:

    可见,strings 头文件中包含了部分函数,没有在 string.h 中出现的。上图的环境是 macOS Sierra 版本号为:10.12.6

    包括; index, rindex, strcasecmp, strncasecmp 这四个函数。

    为了一探这个头文件是不是只有macos 这种 Unix-like 系统中才出现。我在Linux下的ubuntu 系统中也进行了查看。

    我们使用命令: man string 命令,同样可见相同的内容。可见它已经是c 标准库中的头文件。

    进阶:我们到底该用哪个头文件呢?

    为了进一步搞清楚,我们到底在编程的使用string 还是 strings 头文件。我们在 linux 的 /usr/include 文件夹中打开strings 头文件来一窥究竟。

    在 strings.h 文件中给了我们明确的答案:

    大意为: 如果我们使用了string.h 这个头文件,那么我们不需要在进行包含这个 strings.h 这个文件。除非有一种情况。如果 没有定义 __USE_MISC这个变量,这个变量将会在 strings.h 头文件中进行定义。因为 string.h 中没有进行对这个变量进行定义。具体怎么定义的,大家可以在/usr/include/strings.h 这个文件中进行详细查看。

    更进一步:

    为了进一步查看社区中的这两个文件的看法,我们在 stackoverflow 中 找到了这个话题的讨论。和我们的解释大同小异:

    观点1:

    strings.h comes from the BSD branch in the unix evolution. Its content has been standardized by POSIX, but most of it is marked as legacy and can be easily replaced with other functions

    观点2:

    Typically <strings.h> just adds some useful but non-standard additional string functions to the standard header <string.h>. For maximum portability you should only use <string.h> but if you need the functions in <strings.h> more than you need portability then you can use <strings.h>instead of <string.h>.

     具体,这个 strings.h 头文件到底有没有被标准化,我们还需要考证。但是根据两个man page 的表现。我觉得有90% 的可能已经被标准化。因为 mac os 的 man page 中 已经把它归为 Standard C library. strings 与 string.h 不仅仅 只是多一个 s 的区别。
     
    结束.
     
    欢迎大家留言讨论。欢迎转载,转载时请注明出处
     
    参考链接:
    1. https://blog.csdn.net/xin_yu_xin/article/details/38672137
    2. https://stackoverflow.com/questions/4291149/difference-between-string-h-and-strings-h
    3. Linux man page 
  • 相关阅读:
    【学习笔记】pip3 安装使用国内源
    【学习笔记】Team Explorer for Microsoft Visual Studio2015 安装时发生严重错误
    微信聊天记录长图 打印
    Go语言中用 os/exec 执行命令的五种姿势
    Python 代码调试神器:PySnooper
    终于来了!!Pyston v2.0 发布,解决 Python 慢速的救星
    超详细讲解如何使用 pdb 在服务器上调试代码
    超详细图文教你如何使用 PyCharm 进行远程调试
    最全的 pip 使用指南,50 % 你可能都没用过~
    学 Python 一定要学会的几个高阶函数
  • 原文地址:https://www.cnblogs.com/xuyaowen/p/strings_h_and_string_h.html
Copyright © 2011-2022 走看看