简体   繁体   English

C函数定义和声明

[英]C function definition and declaration

I'm a Java programmer,I learnt a little C++ and now I'm studying a little C for my job. 我是一名Java程序员,我学习了一些C ++,现在我正在为自己的工作学习一些C。 I can't understand C behaviour about function declaration/definition and related function calls. 我不了解有关函数声明/定义和相关函数调用的C行为。 From K&R I know that in C (very different from C++) I can call a function that has not been previously declared,and the compiler assumes an implicit declaration of the type: 从K&R,我知道在C中(与C ++完全不同),我可以调用一个以前未声明的函数,并且编译器假定该类型的隐式声明:

int main() 
{
  function(10);   // implicit function declaration ( int function() )
}

and I know that such a declaration implies a function that accepts a fixed but indefinite number of arguments of any type (as long as each call is consistent with the others). 而且我知道这样的声明意味着一个函数可以接受固定数量的但不确定数量的任何类型的参数(只要每个调用与其他调用一致)。 And I know this is K&R C, before C89, but I want to know how it works as well. 我知道这是C&89之前的K&R C,但我想知道它的工作原理。 Now, I have this test code, I can't understand: 现在,我有了这个测试代码,我无法理解:

#include <stdio.h>

function();

int main()
{
  printf("hello %d",function(1,2,3));
  implicit(11,12,32);     // implicit function declaration ( implicit() )
}


int implicit(int b)
{

}

function(int a)
{

}

in the case of function the declaration (return type is assumed to be int,no assumption about the arguments) does match the definition (the compiler issues a warning) but if I call the function with the wrong arguments,it compiles! function的情况下,声明(返回类型假定为int,对参数没有任何假设)与定义匹配(编译器发出警告),但是如果我用错误的参数调用函数,则它将编译! Same for the function implicit . 同样的功能implicit I can't understand.. 我听不懂

The thing you have to remember is that the compile is pretty much sequential when it comes to declarations and definition. 您需要记住的是,关于声明和定义,编译过程几乎是顺序的。 When the compiler processes the call for function all it has is the declaration that, as you say, doesn't have any assumptions about the arguments, which means you can call the function with any argument you like. 如您所说,当编译器处理对function的调用时,它所具有的声明就是对参数没有任何假设,这意味着您可以使用任意喜欢的参数来调用函数。 When the compiler sees the definition, it doesn't go back to issue an error about the call, it might issue a warning though. 当编译器看到该定义时,它不会返回有关该调用的错误,但是可能会发出警告。

As for the implicit function, when the compiler first sees it it will assume that the arguments are what you pass in the call when it deduces the declaration. 至于implicit函数,当编译器第一次看到它时,它将假定参数是您在推导声明时在调用中传递的参数。 Again it will not know anything else until it later sees the declaration, and may issue a warning then. 同样,它将在以后看到该声明之前不知道其他任何信息,然后可能发出警告。

Calling a function with to many, or to few, arguments leads to undefined behavior , which is why implicitly declared functions are so dangerous, as well as using an empty argument list when declaring functions. 调用具有太多或更少参数的函数会导致未定义的行为 ,这就是为什么隐式声明的函数如此危险以及在声明函数时使用空的参数列表的原因。

There is really nothing to understand. 真的没有什么可理解的。 This is a legacy C behaviour, which was an extremely lax language. 这是传统的C行为,这是一种非常宽松的语言。 As long as compiler could generate assembly instruction, it would gladly compile your code, and leave it to you to clean up the mess. 只要编译器可以生成汇编指令,它就会很高兴地编译您的代码,并将其留给您清理。

This is why it compiles in your case. 这就是为什么它会根据您的情况进行编译。 Compiler can generate instruction to call the function - so it does as asked. 编译器可以生成指令以调用该函数-因此它会按要求执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM