原型中的 const 函数参数但不是声明(在 C 中)

const function parameters in prototypes but not declarations (in C)

在用 C 语言编写代码时,我经常在函数定义的函数参数声明中包含 const,而不是它的声明:

int func(int arg);

...

int func(int const arg)
{
    return arg + 1;
}

使用 GCC 和 Clang 为我编译时始终没有问题,但 Microchip 的 C18 编译器声称类型不匹配。

标准对此有何规定?我一直依赖非标准扩展吗?

编辑:我不是在询问这样做的好处,也不是在假设的重复问题 (Use of 'const' for function parameters) 中询问 C++。我问的是 C 标准:这是合法的 ANSI C、C99 还是 C11?

我认为这种类型的错误发生在不同类型的编译器上。 所以 C18 编译器检查函数声明和定义,但其他编译器不检查。和 C/CPP 平台相关的编程语言。

见C11 6.7.6.3/15,谈及函数原型的兼容性:

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.

这表明您的定义与原型兼容。 "qualified" 指存在顶层 constvolatile.