Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

char

The following function uses int as the second argument type,

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

Though it is used for a character type. Why is the function defined to use int for the argument of char type? Are there any special reasons for this?

like image 343
misteryes Avatar asked Apr 03 '13 21:04

misteryes


People also ask

Why is the argument type int in all the character handling function?

They have to accept EOF in addition to normal character values. They also predate the invention of function prototypes. At that time, there was no way to pass a char to a function -- it was always promoted to int first. Save this answer.

Why does memset take an int?

memset predates (by quite a bit) the addition of function prototypes to C. Without a prototype, you can't pass a char to a function -- when/if you try, it'll be promoted to int when you pass it, and what the function receives is an int .

What does memchr do in c?

C library function - memchr() The C library function void *memchr(const void *str, int c, size_t n) searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to, by the argument str.

Are char and int interchangeable?

Char is short for character, and should be used for strings. Int is used for whole numbers. Never use char for number.


2 Answers

It is so because this is a very "old" standard function, which existed from the very early times of C language evolution.

Old versions of C did not have such things as function prototypes. Functions were either left undeclared, or declared with "unknown" parameter list, e.g.

void *memchr(); /* non-prototype declaration */

When calling such functions, all argument were subjected to automatic argument promotions, which means that such functions never received argument values of type char or short. Such arguments were always implicitly promoted by the caller to type int and the function itself actually received an int. (This is still true in modern C for functions declared as shown above, i.e. without prototype.)

When eventually C language developed to the point where prototype function declarations were introduced, it was important to align the new declarations with legacy behavior of standard functions and with already compiled legacy libraries.

This is the reason why you will never see such types as char or short in argument lists of legacy function declarations. For the very same reason you won't see type float used there either.


This also means that if for some reason you have to provide a prototype declaration for some existing legacy function defined in K&R style, you have to remember to specify the promoted parameter types in the prototype. E.g. for the function defined as

int some_KandR_function(a, b, c)
char a;
short b;
float c;
{
}

the proper prototype prototype declaration is actually

int some_KandR_function(int a, int b, double c);

but not

int some_KandR_function(char a, short b, float c); // <- Incorrect!
like image 198
AnT Avatar answered Oct 17 '22 02:10

AnT


All of the standard functions that deal in characters do. I think the reason is partly historical (in some pre-standard versions of C, a function couldn't take a char or unsigned char argument, just like varargs arguments can't have character type) and partly for consistency across all such functions.

There are a few character-handling functions that have to use int in order to allow for the possibility of EOF, but memchr isn't one of them.

like image 21
Steve Jessop Avatar answered Oct 17 '22 03:10

Steve Jessop