码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-24 22:24:06      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:ext   like   more   tab   tps   命令   targe   enc   ace   

今天使用 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 

 

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

标签:ext   like   more   tab   tps   命令   targe   enc   ace   

原文地址:https://www.cnblogs.com/xuyaowen/p/strings_h_and_string_h.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!