3

I am going through the book The C programming Language by K & R to learn c. It says that

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.

I am struggling over this line for the past few days to understand. I think it is an important point. Whatever assumptions that I am making, its not coming true. Can anybody help me to understand it clearly?

The Mask
  • 17,007
  • 37
  • 111
  • 185
noufal
  • 940
  • 3
  • 15
  • 32
  • 1
    What assumptions are you making? – Vaughn Cato Jul 04 '13 at 01:38
  • 1
    http://stackoverflow.com/questions/1314060/c-type-conversion-when-passing-an-argument-on-a-function-call – Arun Jul 04 '13 at 01:38
  • I removed c++ tag because this there's no C++ relation, since C and C++ are different languages. – The Mask Jul 04 '13 at 01:39
  • 2
    Good info in the answers to related question regarding C function prototypes: http://stackoverflow.com/questions/2575153/must-declare-function-prototype-in-c – franji1 Jul 04 '13 at 01:40
  • 1
    You should not be writing C code without prototypes in scope, so the sentence shouldn't matter. However, if you insist on writing retrograde code, you need to know that if you pass a `char` or `short` variable to a function without a prototype in scope, C will automatically convert the values to `int`, and will convert `float` values to `double`. So without a prototype in scope, `somefunc(3.0F)` will be pass a `double` to `somefunc()`; etc. – Jonathan Leffler Jul 04 '13 at 03:24

2 Answers2

5

In pre-ANSI C it was common to have functions with no prototype. In this case only the default type promotions took place.

When there is a prototype, each parameter expression is converted to the type expected by the function, as if there were a cast:

// Declaration
void callMe(char x, int y);
...
// Call
callMe(50, 'x');

The above call is equivalent to calling

callMe((char)50, (int)'x');

This is important because of an implicit agreement between the caller and the callee on passing parameters: the way a parameter is passed, as well as the memory footprint of the parameter, depends on the type. If the caller does not place parameters in memory in the right format, the callee would not be able to use the parameters correctly. That's why the two must agree on the type of each parameter in some way. The standard says that the "agreement" comes in the shape of a function prototype. If the prototype is missing, the standard supplies the "default agreement", i.e. char and short become int, and float becomes double.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

The key thing here is "in the absence of a function prototype", which isn't the usual situation. The most common place you'll see this kind of conversion is in a variable argument list to a function like printf.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132