在main()之前,之后或之内声明函数是否有优势? [英] Are there advantages of declaring functions before, after or inside main()?

查看:137
本文介绍了在main()之前,之后或之内声明函数是否有优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为嵌入式系统学习C语言.目前,我正在学习基础知识,却找不到一个基本问题的答案.当我编写一个简单的C程序时,我以三种方式声明了一个名为maximum()的函数.我将通过以下示例对其进行解释:

I'm trying to learn C language for embedded systems. At the moment I'm learning the basics and couldn't find an answer to one of a fundamental question. When I wrote a simple C program I declared a function called maximum() in three ways. I will explain it by the following examples:

1-)在下面的程序中,该函数在main之外和main之前声明:

1-) Here in the below program the function is declared outside and before the main:

#include <stdio.h>

int maximum(int x, int y)
 {
    int z;
    z = (x >= y) ? x : y;
    return z;
}

int main(void)
{
    int result = maximum(30, 50);
    printf("%d", result);
    return(0);
}

2-)现在,在函数的下面和下面在main后面声明:

2-) And now below the function is declared outside and after the main:

#include <stdio.h>    

int main(void)
{
    int result = maximum(30, 50);
    printf("%d", result);
    return(0);
}

int maximum(int x, int y)
 {
    int z;
    z = (x >= y) ? x : y;
    return z;
}

3-)最后在主函数内部声明该函数:

3-) And finally below the function is declared inside the main:

#include <stdio.h>

int main(void)
{
    int maximum(int x, int y)
 {
    int z;
    z = (x >= y) ? x : y;
    return z;
 }
    int result = maximum(30, 50);
    printf("%d", result);
    return(0);
}

我尝试了所有上述操作,并且所有执行均无错误.有什么理由比其他人更喜欢吗?

I tried all those above and all executes without error. Is there any reason to prefer one to the others?

推荐答案

从C99开始,在标准C语言中,有必要在调用它们之前先声明函数.这告诉编译器期望返回值的类型,应该传递多少个参数,如何转换它们以正确匹配函数的参数类型.该声明不必是函数的定义,但是通常不是.

In standard C since C99, it is necessary to declare functions before you call them. This tells the compiler what type to expect of the return value, how many arguments it should pass, how it might need to convert them to correctly match the function's parameter types. That declaration does not need to be a definition of the function, however, and frequently it isn't.

在这方面,您的(1)很好,但是您的(2)不合格.但是,(2)碰巧具有与调用未声明函数的旧C期望一致的返回和参数类型,并且许多编译器会接受它并在某些情况下对它执行正确的操作.

Your (1) is fine in this regard, but your (2) is non-conforming. However, (2) happens to have return and parameter types that are consistent with older C expectations for calling an undeclared function, and many compilers will accept it and do the right thing with it under some circumstances.

您的(3)以不同的方式不一致:C不允许嵌套函数.您可以将一个函数声明放在另一个函数中,尽管这样做没有什么好处,但是没有一个完整的函数定义.我知道一个编译器系列确实接受它作为扩展,也许其他人也接受,但是在任何情况下都不应该依赖它.

Your (3) is non-conforming in a different way: C does not allow nested functions. You may put a function declaration inside another function, though there is little advantage to this, but not a whole function definition. One compiler family I know does accept that as an extension, and maybe others do, too, but under no circumstances should you rely on that.

因此,在提出的三个替代方案中,(1)是唯一应使用的替代方案.如果您想拥有更大的自由来放置maximum()函数,则在文件顶部附近提供带有原型的前向声明:

Thus, of the three alternatives presented, (1) is the only one you should use. If you want to have more freedom to place the maximum() function then provide a forward declaration, with prototype, near the top of the file:

(4)

#include <stdio.h>    

// Forward declaration, including prototype:
int maximum(int x, int y);

int main(void)
{
    int result = maximum(30, 50);
    printf("%d", result);
    return(0);
}

int maximum(int x, int y)
 {
    int z;
    z = (x >= y) ? x : y;
    return z;
}

这篇关于在main()之前,之后或之内声明函数是否有优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆