简体   繁体   English

主要内部的功能声明和C中的外部主要功能

[英]function declaration inside main and also outside main in C

The first program compiled properly.second gave error saying too few argument for foo... is global declaration ignored by the compiler in both programs? 第一个程序编译得正确。第二个程序给出了错误,说foo的参数太少......编译器在两个程序中都忽略了全局声明?

first program: 第一个程序:

#include<stdio.h>
    void foo();
int main()
{
  void foo(int);
  foo(1);
  return 0;
}

void foo(int i)
{
   printf("2 ");
}

void f()
{
   foo(1);
}

second program: 第二个程序:

void foo();
int main()
{
  void foo(int);
  foo();
  return 0;
}

void foo()
{
   printf("2 ");
}

void f()
{
   foo();
}

The inner declaration hides declarations at the global scope . 内部声明隐藏了全局范围 内的声明 The second program fails because the declaration void foo(int); 第二个程序失败,因为声明void foo(int); hides the global declaration void foo(); 隐藏全局声明void foo(); ; ; so when you say foo within main you are referring to the one taking an int as argument. 所以当你在mainfoo时,你指的是以int为参数的那个。

I see that you investigate the specific behavior of inner declaration but why not: 我看到你调查内部声明的具体行为,但为什么不:

#include<stdio.h>

void foo(int);

int main()
{
    foo(1);
    return 0;
}

void foo(int i)
{
   printf("2 ");
}

void f()
{
   foo(1);
}

or even: 甚至:

#include<stdio.h>

void foo(int i)
{
   printf("2 ");
}

void f()
{
   foo(1);
}

int main()
{
    foo(1);
    return 0;
}

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

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