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

导致静态和非静态声明错误的C程序,c,declaration,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 m

以下是我的程序中出现问题的代码:

    #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编写的,以防任何人需要知道该程序所用的编程语言。 我试图通过输入{}和其他代码来修复程序,但是没有用,这意味着错误没有解决。
如果一个有声望的程序员能帮我解决这个问题,那就太好了。

看起来你是在主。。。?这不仅是不好的做法,而且从那里的法规来看,这似乎是非法的。除非你把静电放在它前面

如果要使用该函数,请不要在使用该函数之前放置其返回类型。而不是:

int area( int L, int W);
使用

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

更好的方法是,在这里为.h文件命名,并将函数放在其中。那么

在这里包括姓名


使用我告诉过你的函数。除了int L、int W之外,将其替换为预先声明的变量L和W,前面不带int。

以下建议代码:

#include "stdio.h"


// prototypes
int   area (int, int);
float cookTime (float);


int main(void)
{
    int funcnum;

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

    switch(funcnum)  
    {
        case 1:  
            printf("You entered 1."
                   " This is now the Turkey Time function.\n"); 

            float weight;

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

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

            float cTim = cookTime (weight);

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

        case 2:  
            printf("You entered 2."
                   " This is now the Area function.\n");

            int len;
            int 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);

            int ans = area (len,wid);

            printf("Area is %i.\n",ans);
            break;

        default:    
            printf("You did not enter 1 or 2,"
                   " meaning that you are not running"
                   " a function this time.\n");
            break;
    }

    return 0;
} // end function: main


int area (int L, int W)
{
    int ans = L*W;
    return ans;
} // end function: area


float cookTime (float w)
{
    float cTim = w*15.0f;   // << note the proper 'float' literal
    return cTim;
} // end function: cookTime
干净地编译。 正确地原型化子功能。 正确声明子函数。 消除了大多数只是代码重复的注释。 在代码块之间、函数之间、代码活动之间使用适当的垂直间距。 无法检查系统功能(即scanf)返回的值。 遵循公理:每行只有一条语句,每条语句最多有一个变量声明。 尊重列表打印输出的右边界。 正确写入“float”文本 现在建议的守则是:

#include "stdio.h"


// prototypes
int   area (int, int);
float cookTime (float);


int main(void)
{
    int funcnum;

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

    switch(funcnum)  
    {
        case 1:  
            printf("You entered 1."
                   " This is now the Turkey Time function.\n"); 

            float weight;

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

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

            float cTim = cookTime (weight);

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

        case 2:  
            printf("You entered 2."
                   " This is now the Area function.\n");

            int len;
            int 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);

            int ans = area (len,wid);

            printf("Area is %i.\n",ans);
            break;

        default:    
            printf("You did not enter 1 or 2,"
                   " meaning that you are not running"
                   " a function this time.\n");
            break;
    }

    return 0;
} // end function: main


int area (int L, int W)
{
    int ans = L*W;
    return ans;
} // end function: area


float cookTime (float w)
{
    float cTim = w*15.0f;   // << note the proper 'float' literal
    return cTim;
} // end function: cookTime

你有一个很好的答案,但是你关于把它留在MAIN而不是void的建议是完全错误的。这是标准参考,请参阅:。谢谢,我更改了它