Why does the function `memchr()` use `int` for the argument of `char` type?

前端 未结 4 1290
逝去的感伤
逝去的感伤 2021-01-05 00:47

The following function uses int as the second argument type,

memchr(const void *buf, int ch, size_t count);

Though it is used

4条回答
  •  再見小時候
    2021-01-05 01:22

    C never passes arguments smaller than an int to a function. Defining a function argument as char or short is always promoted to an int and defining an unsigned char or unsigned short argument is always promoted to an unsigned int.
    If the unsigned vs signed char were the issue, they would have used a short (I don't recall a single platform where a short is 1 byte long).
    And of course using an int handles the unsigned vs signed char possibility as memchr is only comparing equality.

提交回复
热议问题