为什么和strchr取一个int为被发现炭? [英] Why does strchr take an int for the char to be found?

查看:149
本文介绍了为什么和strchr取一个int为被发现炭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C标准库中的和strchr 函数在字符串中寻找一个字符,但它的签名需要一个 INT 的搜索字符。在这两种实现方式,我发现,实施这一蒙上 INT 字符

The strchr function in the C standard library looks for a char in a string, but its signature takes an int for the search character. In these two implementations I found, the implementation casts this int to a char:

char *strchr(const char *s, int c) {
    while (*s != (char)c) 
        if (!*s++)
            return 0; 
    return (char *)s; 
}

char *strchr(const char *s, int c) {  
    while (*s && *s != (char)c)
       s++;
    if (*s == c)  
      return (char *)s;
    return NULL;
}

有谁知道为什么吗?为什么不采取字符作为参数?

推荐答案

对于原因是纯粹的历史。注意,在C语言的旧时代(K&安培; R C)有,因为没有这样的东西的函数原型的。在那个时代A 和strchr 函数将被宣布为

The reasons for that are purely historical. Note, that in the old days of C language (K&R C) there was no such thing as function prototype. A strchr function in those times would be declared as

char *strchr();

和在K&放定义; R风格

and defined in K&R style as

char *strchr(s, c)
  char *s;
  char c;
{
  /* whatever */
}

不过,在C语言(在K&放大器; RC而在现代的为好)如果函数没有原型声明(如上图所示),在每个函数调用传递的参数受到所谓的通常的算术转换的。在通常的算术转换任何整数类型比 INT 小(或 unsigned int类型)将被转换成 INT (或 unsigned int类型)。即当参数未申报的,只要你传递一个字符值作为参数,这个值的的转换为 INT ,并作为 INT 实际的物理过去了。这同样适用于真。 (顺便说一句,浮动转换为双击按相同的过程)。如果函数里面的参数,实际上是声明为字符(如K&放大器;上述R样式定义),它的的转换回字符类型和用作字符在函数内部。这是它的K&功放的工作原理; r次,而这实际上是如何工作的,这一天在现代的C函数时没有原型或当使用可变参数的参数

However, in C language (in K&R C and in the modern one as well) if the function is declared without a prototype (as shown above), the parameters passed in each function call are subjected to so called usual arithmetic conversions. In usual arithmetic conversions any integral type smaller than int (or unsigned int) is always converted to int (or unsigned int). I.e. when the parameters are undeclared, whenever you pass a char value as an argument, this value is implicitly converted to int, and actually physically passed as an int. The same is true for short. (BTW, float is converted to double by the same process). If inside the function the parameter is actually declared as a char (as in the K&R style definition above), it is implicitly converted back to char type and used as a char inside the function. This is how it worked in K&R times, and this actually is how it works to this day in modern C when function has no prototype or when variadic parameters are used.

如何,提示在现代的C,它具有的函数原型的,并使用现代风格的函数定义的语法。为了preserve和复制和strchr 的传统的功能,如上文所述,我们没有别的选择,只能申报和strchr 为 INT ,并明确将其转换为字符在函数内部。这正是在你所引用的code观摩一下。这也正是为和strchr 在标准中描述的功能。

How, cue in the modern C, which has function prototypes and uses modern-style function definition syntax. In order to preserve and reproduce the "traditional" functionality of strchr, as described above, we have no other choice but to declare the parameter of strchr as an int and explicitly convert it to char inside the function. This is exactly what you observe in the code you quoted. This is exactly as the functionality of strchr is described in the standard.

此外,如果你有一个已编译的遗产库,其中和strchr 在K&放大器定义; R风格如上图所示,你决定为现代原型库,和strchr 适当的声明将是

Moreover, if you have an already-compiled legacy library, where strchr is defined in K&R style as shown above, and you decided to provide modern prototypes for that library, the proper declaration for strchr would be

char *strchr(const char *s, int c);

由于 INT 是上面遗留实施预计将身体作为接收 C 。以字符参数声明这是不正确的。

because int is what the above legacy implementation expects to physically receive as c. Declaring it with a char parameter would be incorrect.

由于这个原因,你永远不会看到传统的标准库函数期望参数类型为字符浮动。所有这些功能将参数类型为声明 INT 双击代替。

For this reason, you will never see "traditional" standard library functions expecting parameters of type char, short or float. All these functions will be declared with parameters of type int or double instead.

这篇关于为什么和strchr取一个int为被发现炭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆