函数原型

版本新特性 / C89

// *** before: ***
//K&R C: a declaration said nothing about the parameters,
//and a definition listed their types after the parentheses
double average();

double average(sum, count)
double sum;
int count;
{
    return sum / count;
}

double resultOld = average(10.0, 4);
//nothing checked the call: average(10, 4) passed the int 10
//where a double was expected, and the result was garbage

// *** in version C89: ***
//a prototype gives the type of every parameter: the compiler
//checks the number of arguments and converts them
double mean(double sum, int count);

double mean(double sum, int count) {
    return sum / count;
}

double result = mean(10, 4);     //10 is converted to 10.0
//result is 2.5

//mean(10);    <- error: too few arguments to function 'mean'
//and int f(void) for "no parameters" is new in C89 as well

printf("result is %g\n", result);