string.h头文件和strings.h的区别

首先我们看一下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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值