Home > Back-end >  Consult a C function, what is "a function prototype," what is a "no function prototyp
Consult a C function, what is "a function prototype," what is a "no function prototyp

Time:01-13

Consult
K& R "C programming language" in Chinese version 2, 36 pages section 3:
"Is better than that of the parameters of the function call is expression, so it is possible when the parameter passed to a function, type conversion, in the absence of function prototypes, char and short will be converted to type int"

Section 7
"Under normal circumstances, the parameter is through the function prototype declaration"

To ask, what is the "prototype"?
When we pass parameters to a function, whether the data type conversion is whether there is a "prototype"? That is "a function prototype" and "no function prototype"

CodePudding user response:

Function prototypes can be understood as a function definition, function definition, define the parameters of type, char and short turn int, because the range of int than char short range is big, sometimes automatic conversion;

There is no function prototype, that is not defined,

CodePudding user response:

Did not heard of "no function prototype", not the word,

CodePudding user response:

Before C language standardization, the function of the statement is that even if it is actually with parameters:

 # include & lt; stdio.h> 

Float (f);//K& R's traditional function declaration

Int main ()
{
Printf (" % 2 f \ n ", f (2, 3));//2, and 3 types according to their original int the incoming, rather than converted to float the incoming, so the calculation result is wrong,
return 0;
}

Float f (a, b)
Float a, b;
{
Return a * b;
}



Obviously, because there's no parameter declarations in the traditional function declarations, function calls when don't know how to pass parameters into, can only in accordance with the default, char and short converted to int, float into a double,
Standardization of C language function prototype is introduced, namely, demand function statement must have the parameter type:
 # include & lt; stdio.h> 

Float f (float, float);//prototype declarations after standardization, with the parameter types,

Int main ()
{
Printf (" % 2 f \ n ", f (2, 3));//2 and 3 from the incoming after converting int, float, so the calculation results are correct,
return 0;
}

Float (float a, float b/f/standardized function definition
{
Return a * b;
}



Because the function prototype declaration, so when invoked, the compiler can know how to convert the incoming parameters,

CodePudding user response:

If the expression that denotes the called function has a type that does not include a prototype , the integer promotions are performed on each argument, and the arguments that have type float are promoted to double the. These are called the default argument promotions .
  • Related