开发者

Any documentation for calling a function without a prototype in C?

开发者 https://www.devze.com 2023-02-15 16:37 出处:网络
It seems I can call a function without prototype and definition of it in C, and the compilation (no link) worked well. Link would work if the function is defined in other module which is passed to the

It seems I can call a function without prototype and definition of it in C, and the compilation (no link) worked well. Link would work if the function is defined in other module which is passed to the link command. But the call site would assume every parameter passed to it is int type. Is there any documentation for this or any part of the C standard mentioned this behavior?

For example, below source file could be compiled without any problem in VC++ 2010.

// NoPrototype.c
// Compile it as cl.exe /c NoPrototype.c
int main()
{
  FakeFunction(1, 2, 3); // No definition for 开发者_Go百科this function in this compilation unit.
}


Not having a declaration in scope is UB.

J.2 Undefined behavior

— For call to a function without a function prototype in scope where the function is defined with a function prototype, either the prototype ends with an ellipsis or the types of the arguments after promotion are not compatible with the types of the parameters (6.5.2.2).

Comeau rejects your code

"ComeauTest.c", line 5: error: identifier "FakeFunction" is undefined
    FakeFunction(1, 2, 3); // No definition for this function in this compilation unit.
    ^

1 error detected in the compilation of "ComeauTest.c"


That is the way it worked in old, pre-standard C. It was (and is, pre C99) ok to have functions returning int and taking int parameters, without a prototype. It is your responsibility to make sure that is the way the function looks in some other .c file, including actually taking the number of parameters you have in each call.

This is the reason why functions like toupper is int toupper(int c);, not using char. It was originally used without a prototype, and that implicit interface got stuck!

0

精彩评论

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