2

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?

Community
  • 1
  • 1
thetic
  • 174
  • 1
  • 11
  • I think standard says it is correct. It is related to compatible type, can find the right reference actually. – Jean-Baptiste Yunès Oct 21 '16 at 14:32
  • Actually, this question is not a duplicate. I would suggest you reread it more carefully and reopen it. – glauxosdever Oct 21 '16 at 15:59
  • Please do. I'm not asking about C++ and I'm not asking about pros and cons of this style. I am asking about the C standard. – thetic Oct 21 '16 at 19:52
  • @haccks Can you please reopen this? The linked duplicate does not answer my question. – thetic Oct 22 '16 at 03:07
  • In Standard C your code is correct. IDK whether your compiler claims to be conformant – M.M Oct 22 '16 at 06:43
  • When the type is a simple built-in type (such as `int`), the `const` doesn't provide any value. The called function gets a copy of the value from the calling function, and any changes made to the argument by the called function have no effect in the calling function. Pointer types are different. (Structure and union types as arguments are also a copy of the corresponding value in the calling function; again, changes in the called function don't affect the calling function. Pointers to structures and unions are different, of course.) – Jonathan Leffler Oct 22 '16 at 07:09
  • Function prototype `int func(const int arg);` is equivalent to `int func(int arg);` Compiler will see as if there is no `const` qualifier in the the parameter `arg`. – haccks Oct 22 '16 at 07:11
  • @Jonathan Leffler : The "value" of `const` in this case is that it prevents the functon implementation from changing the local value of the parameter. Could be useful in those environments that want to enforce this as a coding gudeline. – AnT stands with Russia Oct 22 '16 at 07:18
  • @AnT: I know, and I'm questioning the value of that annotation. Opinions may vary. Coding standards do, too. And they're not all equally good (opinions or coding standards). – Jonathan Leffler Oct 22 '16 at 07:20

2 Answers2

3

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.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Not sure if differed in C89 or C99, not at my usual station – M.M Oct 22 '16 at 06:48
  • 1
    In C99, the section number is different (§6.7.5.3), but the wording is substantially the same. In C90, the section number is different again (§6.5.4.3), and the wording is simpler: _(For each parameter declared with function or array type, its type for these comparisons is the one that results from conversion to a pointer type, as in 6.7.1. For each parameter declared with qualified type, its type for these comparisons is the unqualified version of its declared type.)_ – Jonathan Leffler Oct 22 '16 at 07:05
  • Yes, that is how *type compatibility* is defined. But where does it say that maching between definition and prototype is based on *type compatibility*? (As opposed to, say, requiring the exact match of all parameter types). I mean, I'm sure that it is based on type compatibility, I just don't remember where it connects these concepts. – AnT stands with Russia Oct 22 '16 at 07:14
  • @AnT 6.7/4 "All declarations in the same scope that refer to the same object or function shall specify compatible types." – M.M Oct 22 '16 at 07:21
-2

I think this type of error occurred for different types of compiler. So C18 compiler check function declaration & definition but others doesn't. And C/CPP platform dependent programming language.

Ramesh
  • 200
  • 1
  • 2
  • 13