我们可以在C中的主要功能中定义功能原型



在我的大学中,我们的老师教我们"我们在主要功能之前定义功能原型。如:

#include<stdio.h>
void findmax(float, float);
int main()
{
    //code goes here
}

但是今天我的朋友向我展示了他们得知他们在主要功能中放置了原型。喜欢:

#include<stdio.h>
int main()
{
    void findmax(float, float);
    float firstnum, secondnum;
    printf("Enter first:");
    scanf("%f", &firstnum);
    printf("Enter second:");
    scanf("%f", &secondnum);
    findmax(firstnum, secondnum);
}
void findmax(float x, float y)
{
    float maxnum;
    if(x>y)
    {
            maxnum=x;
    }
    else
    {
       maxnum=y;
    }
    printf("The max is %f", maxnum);
}

他们俩都起作用。我想知道它们之间是否存在差异。谢谢。

我们可以在c?

中的主函数中定义函数原型

是。

我想知道它们之间是否有差异

区别在于,在第一个摘要原型中是全局的,而第二个原型是局部对mainfindmax在声明和/或定义后将可见。

#include<stdio.h>
void foo();
int main()
{
    void findmax(float, float); 
    foo();
    findmax(10, 20);  // It knows findmax by the prototype declared above
}
void findmax(float x, float y)
{
    float maxnum;
    if(x>y)
        maxnum=x;
    else
        maxnum=y;
    printf("The max is %f", maxnum);
}
void foo(){   // foo is using findmax after its definition.
    findmax(12, 30);
}

如果在函数外部声明 foo,则可以从同一文件中的任何函数调用:

void foo(); // <-- global declaration
int main() {
  foo();    // <-- works, foo() is declared globally
}
void otherfunc() {
  foo();    // <-- works, foo() is declared globally
}

但是,如果在功能中声明foo,则只能在同一范围内使用:

int main() {
  void foo(); // <-- scoped declaration
  foo();      // works, foo() is declared in same scope
}
void otherfunc() {
  foo();      // ERROR: foo() is not declared in scope of otherfunc()
}

无论哪种情况,foo都必须在使用之前声明。

如果在main()中声明该函数,则在main()中范围范围内范围内范围,并且无法在另一个函数中访问它。但是,如果您在文件的开头或标题文件中声明它,则可以在任何函数中使用它。

对您的问题的简单答案只是肯定的。但是当涉及到范围时,我想添加更多。

其他人建议的是正确的 - 如果您在另一个功能中声明功能,则第一个范围通常仅限于第二个功能。对于您提供的示例,这样说是完全安全的。但这可能永远不是这种情况。考虑以下代码:

#include <stdio.h>
/* Can be accessed from anywhere in this file
 * because defined before anything else.
 */
void print_hello() {
        puts("Hello!");
}
int main() {
        void print_hello(); /* Not needed actually */
        void print_namaste();
        void print_hi();
        print_namaste();
        print_hi();
}
/* The following two functions cannot be
 * accessed from within the above two functions
 * unless these two are declared inside them
 * or at the starting of this file
 */
void print_namaste() {
        puts("Namaste!");
        print_hello();
}
void print_hi() {
        puts("Hi!");
}

在这里您可以看到print_hello()main()内声明。但这是多余的。print_hello()已经引入编译器。无需声明在此文件中使用它(如果您在其他文件中使用它并独立编译,则必须在这些文件中声明它或编写标头文件以进行此操作)。我的意思是,main()中声明print_hello()尚未使其本地化,至少在此示例中。您可以看到print_namaste()成功地调用它,而无需任何其他声明。

print_namaste()print_hi()并非如此。他们需要在main()中使用声明,因为仅在main()被定义后才引入编译器。

print_namaste()仅对我们声明的功能以及在 print_namaste()之后定义的功能可见。在此示例中,print_namaste()不可见print_hello()(除非在其内部或之前声明),但是main()可见,因为它在其内部声明,并且print_hi可见,因为其定义仅在print_namaste()的定义之后。

在VC2008中,以下是有效的:

int main(void)
{
    void foo(void);
    //...
}
void test(void)
{
    foo();
}

我相信原型声明在任何地方都有效,并且其范围至少从声明到文件结尾。

查看C99标准:

6.2.1标识符范围

标识符可以表示对象;功能;结构,工会或枚举;Typedef名称;标签名称;宏名称;或一个宏参数...对于标识符指定的每个不同实体,标识符是可见的(即可以是使用)仅在程序文本的区域中,称为其范围...标识符的范围取决于其声明的位置(在声明器或类型说明符)。如果声明者或键入声明标识符的指定符出现在任何块或参数列表之外,标识符具有文件范围,在翻译单元的末尾终止。如果声明器或键入指定符声明标识符出现在一个块中或参数声明列表中函数定义,标识符具有块范围,该范围终止于关联的块。

这意味着VC2008是错误的。

最新更新