【问题标题】:how function visible to other functions in program?程序中的其他功能如何看到功能?
【发布时间】:2017-09-19 07:06:32
【问题描述】:
#include<stdio.h>
int main(void) 
{
  int a=3,b=4;
  int c;
  c = max_min(a,b);
  printf("the max no is %d\n",c);
}
int max_min(int a,int b)
{
  if(a>b)
    return a;
  else if(b>a)
    return b;
}

错误:“max_min”未在此范围内声明 c = max_min(a,b);

请帮帮我

【问题讨论】:

  • 这是一条非常可通过 Google 搜索的错误消息。您自己可以更快地得到答案,而不会浪费任何人的时间。
  • Function not declared in this scope 和其他数百个可能重复
  • 未在此范围内声明的函数和其他数百个函数的可能副本

标签: c


【解决方案1】:

要么在main 之前定义你的函数,要么给出你的函数的前向声明。像这样-

#include<stdio.h>
int max_min(int a,int b);       // forward declaration 
int main(void){
 // your code

}
// rest of code

您需要在使用函数之前定义或提供前向声明,否则编译器会抛出错误,因为在此之前它看不到函数定义或声明。

【讨论】:

    【解决方案2】:

    声明它。 IE。将int max_min(int a,int b); 放在main 上方。这将告诉编译器该函数

    #include<stdio.h>
    
    int max_min(int a,int b);
    
    int main(void) 
    {
      int a=3,b=4;
      int c;
      c = max_min(a,b);
      printf("the max no is %d\n",c);
    }
    int max_min(int a,int b)
    {
      if(a>b)
        return a;
      else if(b>a)
        return b;
    }
    

    【讨论】:

      【解决方案3】:

      添加一个原型告诉编译器你已经定义了函数min_max。

      #include<stdio.h>
      
      int max_min(int a,int b);
      
      int main(void) 
      {
       int a=3,b=4;
       int c;
       c = max_min(a,b);
       printf("the max no is %d\n",c);
       }
      int max_min(int a,int b)
      {
      if(a>b)
      return a;
      else if(b>a)
       return b;
      }
      

      或在 main 之前定义 min_max

      #include<stdio.h>
      
      int max_min(int a,int b)
      {
      if(a>b)
      return a;
      else if(b>a)
       return b;
      }
      
      int main(void) 
      {
       int a=3,b=4;
       int c;
       c = max_min(a,b);
       printf("the max no is %d\n",c);
       }
      

      【讨论】:

        【解决方案4】:

        您需要一个声明函数的函数原型,以便编译器知道从main 调用的内容。

        #include<stdio.h>
        
        int max_min(int a,int b);       // function protoype
        
        int main(void) 
        {
            int a=3,b=4;
            int c;
            c = max_min(a,b);
            printf("the max no is %d\n",c);
        }
        
        int max_min(int a,int b)
        {
            if(a>b)
                return a;
            else if(b>a)
                return b;
        }
        

        顺便说一句,编译器应该报告“并非所有控制路径都返回值”,因为在 a == b 时函数没有返回值。

        【讨论】:

          【解决方案5】:

          你真的需要花几天时间阅读一本关于 C 编程的书。

          您还应该查看C reference 之类的网站。

          在 C 中,每个函数都应该在使用前声明(可能在某些 included 标头中)。

          所以将你的max_min 函数移到你的main 之前,或者声明它:

          int max_min(int a,int b);
          

          在声明时,您不需要命名形式,因此可以使用以下较短的声明:

          int max_min(int,int);
          

          顺便说一句,max_min 确实是一个糟糕且令人困惑的名称(因为您不计算最小值,只计算最大值)。你的意思是max 或者my_max 或者maximum

          不要忘记使用所有警告和调试信息进行编译(例如,gcc -Wall -g 如果使用 GCC 编译器)。然后使用调试器(例如GNU gdb)。也许聪明的编译器会注意到你没有涵盖a 等于b 的情况。

          对于多个输入,您需要 test your program(您也可以使用 formal methods 来证明其正确性 w.r.t. specifications,例如在 Frama-C 的帮助下)。您可以从传递给main 的程序参数中获取它们,而不是使用ab 的各种值重新编译代码(用atoi 转换它们),例如

          int main(int argc, char**argv) {
             int a= 10;
             int b= 5;
             if (argc>1) 
               a = atoi(argv[1]);
             if (argc>2) 
               b = atoi(argv[2]);
             printf("a=%d b=%d\n", a, b);
          

          等等……

          然后您可以使用./myprog 3 4 运行您的程序(在某些终端中)以测试a 等于3 和b 等于4。实际上您将运行in the debugger(例如gdb --args ./myprog 3 4) .

          顺便说一句,您还可以读取(使用scanfab 的值。

          【讨论】:

            【解决方案6】:

            试一试

            首先在Main方法

            之前声明方法
            #include<stdio.h>
            int max_min(int,int);
            int main(void) 
            {
              int a=3,b=4;
              int c;
              c = max_min(a,b);
              printf("the max no is %d\n",c);
            }
            int max_min(int a,int b)
            {
              if(a>b)
               return a;
              else if(b>a)
               return b;
            }
            

            或者在main方法之前指定函数

             #include<stdio.h>
             int max_min(int a,int b)
            {
              if(a>b)
               return a;
              else if(b>a)
               return b;
            }
            int main(void) 
            {
              int a=3,b=4;
              int c;
              c = max_min(a,b);
              printf("the max no is %d\n",c);
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-01-06
              • 1970-01-01
              • 1970-01-01
              • 2017-03-13
              • 2014-04-10
              • 2021-06-26
              • 2021-11-27
              相关资源
              最近更新 更多