Question

Consider the following function declaration and definition. In the header file:

void some_function(int param);

In the source file:

#include "test.h"

void some_function(const int param) {}

int main(void) {
    return 0;
}

Under Visual Studio 2010, compiling as a pure C project, I see warning C4028: formal parameter 1 different from declaration. But as far as I know, this is perfectly valid C, and fairly common practice.

Am I wrong about this, and is VS2010 therefore correct to warn me? Or if it is a spurious warning, can I disable it specifically for this kind of case, but keep the warning on for actual cases of mismatched parameter types?

(The actual compiler that comes with VS2010 is: Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86. My command line is simply cl test.c.)

Was it helpful?

Solution

C89 3.5.4.3 Function declarators has this to say about the parameters:

For two function types to be compatible, both shall specify compatible return types. Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; corresponding parameters shall have compatible types.

Section 3.1.2.6 Compatible type and composite type specifies compatible types as those having the same type. It references 3.5.3 for type qualifiers (of which const is one) and that states:

For two qualified types to be compatible, both shall have the identically qualified version of a compatible type; the order of type qualifiers within a list of specifiers or qualifiers does not affect the specified type.

Now you would normally consider therefore that int and const int are not compatible types. However, I believe that's a mis-reading of the standard simply because int is not a qualified type, therefore that quote above doesn't apply.

There is a more applicable quote later on in 3.5.4.3 stating:

For each parameter declared with qualified type, its type for these comparisons is the unqualified version of its declared type.

Hence int and const int are compared similarly and should be allowed. But that just means it's not an error according to the standard. It may be that Microsoft, in their wisdom, still think it's a suspect action and a good reason to pump out a warning.

Even if you consider that a problem, it's very unlikely Microsoft are going to fix it for you (see below).


Now you may wonder why I'm quoting C89 when there are far more modern standards available.

It's because Microsoft makes no secret that Visual C++ is primarily a C++ compiler and can compile C code only to the earlier itertions of the standard, as per here:

Thanks for taking the time to send us your suggestion. Currently, there are no plans to implement C99 support in VS2010. Once we complete this product cycle, we will be reviewing all customer suggestions, including this one, for our future planning. - Mark Roberts, Microsoft.

And, from the wikipedia page:

According to Herb Sutter, the C compiler is only included for "historical reasons" and is not planned to be further developed. Users are advised to either use only the subset of the C language that is also valid C++, and then use the C++ compiler to compile their code, or to just use a different compiler such as Intel C++ Compiler or the GNU Compiler Collection instead.

This is confirmed in Herb Sutter's own blog, the relevant article here:

We do not plan to support ISO C features that are not part of either C90 or ISO C++.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top