Formal parameters in C language can be constants, which are declared as constant parameters by adding the const keyword before the parameter type. Advantages of constant parameters: Improve code robustness and readability; Disadvantages: Limit flexibility and may cause performance overhead.
Can formal parameters in C language be constants?
Answer: Can
Detailed explanation:
In C language, formal parameters (function parameters) Can be a constant. You can declare a parameter as a constant parameter by prefixing it with the keyword const
. Constant parameters have the following characteristics:
Advantages:
Disadvantages:
Example:
<code class="c">void print_array(const int *arr, int size) { // arr is a constant pointer to an array of int // size is a constant integer for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } }</code>
In this example, the arr
parameter is declared as a constant pointer pointing to an int
array. size
The formal parameter is declared as a constant integer. Therefore, function print_array
cannot modify the passed array or its size.
The above is the detailed content of Can formal parameters be constants in C language?. For more information, please follow other related articles on the PHP Chinese website!