0

in K&R C, 2nd Edition, Page 42

Since an argument of a function call is an expression, type conversion also takes place when arguments are passed to functions. In the absence of a function prototype, char and short become int, and float becomes double. This is why we have declared function arguments to be int and double even when the function is called with char and float.


my question is

  • confused with the last sentence: if a function func is called with char and float, why don't declare it as return_type func(char, float); directly?
  • One thing I can think of is that it may be more efficient to pass integer and double. But after argumets-passing, it still needed to convert them into char and float, does this really efficient?
/* lower: convert c to lower case; ASCII only */
int lower(int c)  // why don't declare c as char?
{
    if (c >= 'A' && c <= 'Z')
        return c + 'a' - 'A';
    else
        return c;
}
Gie dunn
  • 1
  • 1
  • 3
    It's probably referring to whatever example appeared in the text previously. Additionally, what's quoted applies to C only. In C++ all functions must be declared, and none of the above applies. C and C++ are completely different languages, and in order to avoid confusing yourself you need to be absolutely clear which language's textbook you are reading. – Sam Varshavchik Jun 07 '21 at 11:51
  • 1
    can we have the link to the text from where you found it ? – Arsenic Jun 07 '21 at 11:52
  • Maybe not a dupe but very related: https://stackoverflow.com/questions/1314060/c-type-conversion-when-passing-an-argument-on-a-function-call – Support Ukraine Jun 07 '21 at 12:11
  • Also related: https://stackoverflow.com/questions/17460618/type-conversion-during-function-call-in-c – Support Ukraine Jun 07 '21 at 12:14
  • Does this answer your question? [C: type conversion when passing an argument on a function call](https://stackoverflow.com/questions/1314060/c-type-conversion-when-passing-an-argument-on-a-function-call) – Mad Physicist Jun 07 '21 at 12:14
  • "This is why we have declared function arguments to be int and double even when the function is called with char and float." hmmm... there must be some code just before this paragraph. Post it... – Support Ukraine Jun 07 '21 at 12:27
  • This last sentence makes sense only for functions that are called with no declaration before the call. – the busybee Jun 07 '21 at 14:09

0 Answers0