导致静态和非静态声明错误的 C 程序

标签 c declaration

这是我的程序中存在问题的代码:

    #include "stdio.h"
    int main(void)
    {
    int funcnum;

    printf("Welcome \n");
    printf("Please enter a number\n");
    scanf("%i",&funcnum);

    switch(funcnum)  //funcnum is the variable you are checking for a match
    {          //Open curly!
        case 1:  // is funcnum==1?
            printf("You entered 1. This is now the Turkey Time function.\n"); // if funcnum==1, this will happen.
            {
              //DECLARE the "cookTime" function.(Above, outside the MAIN function)
              //It will return a float, and is expecting two floats.
              float cookTime (float);

              //Below is a "global" variable -meaning available to all functions. These are declared outside of any function.
              float cTim;

              float weight;

              printf("Welcome to the turkey timer...\n");

              printf("Please enter a turkey weight \n");
              scanf("%f",&weight);

              cookTime (weight); //Calling (jumping to) the cookTime function and sending it "weight" variable.  

              printf("Cooking time is %.1f minutes.\n\n",cTim); //printing the returned value cTim.
              printf("\tThank you for choosing the MaiCorp Timing System, don't forget the gravy! \n");

              //DEFINE the  function. Note -no semicolon. (just like in main's definition above!)
              float cookTime (float w)
              {   
                cTim = w*15;
                return cTim; //We are sending cTim back to where we left Main.
              }
            }
            break; //break makes the statement end and jump out of the curlies.
        case 2:  // is funcnum==2?
            printf("You entered 2. This is now the Area function.\n");
            {
              //DECLARE the "area" function.(Above, outside the MAIN function)
              //Looking at the declaration we can see that this function will return an int, and is expecting two int's.
              int area (int, int);

              //Here we declare a global variable.  Meaning a variable that is available to all functions. These are declared outside of any function.
              int ans;

              int len,wid;

              printf("Welcome to the rectangle area calculator...\n");
              printf("Please enter a length\n");
              scanf("%i",&len);
              printf("Please enter a width\n");
              scanf("%i",&wid);


              area (len,wid);    //Calling the "area" function, sending it the len and wid integers..

              printf("Area is %i.\n",ans); //printing the returned value "ans"

              //DEFINE the area function. Note -no semicolon. (just like in main's definition above!)

              int area (int L, int W)
              {
                ans = L*W;
                return ans; 
              }
            }
            break;
        default:    //default catches all non matches.
            printf("You did not enter 1 or 2, meaning that you are not running a function this time.\n");
            break;
    }  //close curly!
    return 0;
}

当我运行这个程序时,gcc 版本 4.6.3 编译器给出以下结果:

main.c: In function 'main':
main.c:35:21: error: static declaration of 'cookTime' follows non-
static declaration
           float cookTime (float w)
                 ^~~~~~~~
main.c:17:21: note: previous declaration of 'cookTime' was here
           float cookTime (float);
                 ^~~~~~~~
main.c:67:19: error: static declaration of 'area' follows non-static 
declaration
           int area (int L, int W)
               ^~~~
main.c:47:19: note: previous declaration of 'area' was here
           int area (int, int);
               ^~~~

exit status 1

该程序是用 C 编写的,以防有人需要了解该程序所使用的编程语言。 我尝试通过放入“{}”和其他代码来修复程序,但它没有用(意味着错误没有解决)。 如果有信誉良好的程序员可以帮助我解决这个问题,那就太好了。

最佳答案

看起来您正在 main 中声明一个函数...?这不仅是不好的做法,而且从代码来看它看起来是非法的。除非你把static放在它前面。

如果您想使用该函数,请不要在使用该函数之前输入其返回类型。而不是:

int area( int L, int W);

使用

area(int L, int W);

在 main 中定义一个函数真的很奇怪。就像我说的,我认为这是不允许的,但如果你真的想这样做,我建议将 static 放在函数前面。

更好的是,创建一个 Name_goes_here.h 文件并将函数放入其中。然后,

#include“Name_goes_here.h”

并像我告诉你的那样使用函数。 (除了代替int L、int W之外,用预先声明的变量L和W替换,前面不带int。)

关于导致静态和非静态声明错误的 C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46416482/

相关文章:

c++ - 这是做什么的? C++

c - 在另一个字符串中查找包含一个字符串的所有字符的最小窗口的长度

c - 从字符串中创建一个字符

c - signal() 和 ualarm() 与 select() 冲突

javascript - JS : why is the var not declared when it is declared inside a method of an object and passed as a parameter to the other method of the same object?

javascript - 动态对象定义

c - 试图理解 C 中的 extern

c++ - 具有结构数据类型的链表

php - 文件在 PHP 中乱写

c - 格式化字符串的大小