When writing code in C, I often include const
in function parameter declarations in the function's definition, but not its declaration:
int func(int arg);
...
int func(int const arg)
{
return arg + 1;
}
This has always compiled for me without issue using GCC and Clang, but Microchip's C18 compiler is claiming a type mismatch.
What does the standard have to say about this? Have I been relying on a non-standard extension?
Edit: I am not asking about the benefits of this and I am not asking about C++ as in the supposed duplicate question (Use of 'const' for function parameters). I am asking about the C standard: Is this legal ANSI C, C99, or C11?
See C11 6.7.6.3/15, talking about compatibility of function prototypes:
In the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and each parameter declared with qualified type is taken as having the unqualified version of its declared type.
This specifies that your definition is compatible with the prototype. "qualified" refers to the presence of top-level const
or volatile
.