开发者

Is c function prototype mismatch merely a warning

开发者 https://www.devze.com 2023-04-07 00:13 出处:网络
please take a look at my codes below #include <stdio.h> void printOut() { static int i = 0; if (i < 10)

please take a look at my codes below

#include <stdio.h>

void printOut()
{
 static int i = 0;
 if (i < 10)
 {
  printOut(i);
 }
}

int main(int argc, char *argv[])
{

  return 0;
}

i guess there should be an error due to my invoking the non-existed function prototype.Actually, the code compiles well with mingw5 compiler, which is weird for me, then i change to Borland Compiler, i get a warnin开发者_JAVA百科g message said that no printOut function prototype, is this only a warning ? What is more, the code executes well without any pop-up error windows.


In C, a function without any parameters can still take parameters.

That's why it compiles. The way to specify that it doesn't take any parameters is:

void printOut(void)

This is the proper way to do, but is less common especially for those from a C++ background.


Your program's behavior is undefined, because you define printOut() with no parameters, but you call it with one argument. You need to fix it. But you've written it in such a way that the compiler isn't required to diagnose the problem. (gcc, for example, doesn't warn about the parameter mismatch, even with -std=c99 -pedantic -Wall -Wextra -O3.)

The reasons for this are historical.

Pre-ANSI C (prior to 1989) didn't have prototypes; function declarations could not specify the expected type or number of arguments. Function definition, on the other hand, specified the function's parameters, but not in a way that the compiler could use to diagnose mismatched calls. For example, a function with one int parameter might be declared (say, in a header file) like this:

int plus_one();

and defined (say, in the corresponding .c file) like this:

int plus_one(n)
int n;
{
     return n + 1;
}

The parameter information was buried inside the definition.

ANSI C added prototypes, so the above could written like this:

int plus_one(int n);

int plus_one(int n)
{
    return n + 1;
}

But the language continued to support the old-style declarations and definitions, so as not to break existing code. Even the upcoming C201X standard still permits pre-ANSI function declarations and definitions, though they've been obsolescent for 22 years now.

In your definition:

void printOut()
{
    ...
}

you're using an old-style function definition. It says that printOut has no parameters -- but it doesn't let the compiler warn you if you call it incorrectly. Inside your function you call it with one argument. The behavior of this call is undefined. It could quietly ignore the extraneous argument -- or it could conceivably corrupt the stack and cause your program to die horribly. (The latter is unlikely; for historical reasons, most C calling conventions are tolerant of such errors.)

If you want your printOut() function to have no parameters and you want the compiler to complain if you call it incorrectly, define it as:

void printOut(void)
{
    ...
}

This is the one and only correct way to write it in C.

Of course if you simply make this change in your program and then add a call to printOut() in main(), you'll have an infinite recursive loop on your hands. You probably want printOUt() to take an int argument:

void printOut(int n)
{
    ...
}

As it happens, C++ has different rules. C++ was derived from C, but with less concern for backward compatibility. When Stroustrup added prototypes to C++, he dropped old-style declarations altogether. Since there was no need for a special-case void marker for parameterless functions, void printOut() in C++ says explicitly that printOut has no parameters, and a call with arguments is an error. C++ also permits void printOut(void) for compatibility with C, but that's probably not used very often (it's rarely useful to write code that's both valid C and valid C++.) C and C++ are two different languages; you should follow the rules for whichever language you're using.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号