静态声明遵循非静态声明错误消息

时间:2014-04-21 21:04:27

标签: c compiler-errors

编译器在

上给出了一条错误消息
line 20: "static declaration of ‘timeDifference’ follows non-static declaration"

然后另一个

line 12: "previous declaration of ‘timeDifference’ was here"

我知道它与我的函数'timeDifference'有关。这是我的代码:

#include <stdio.h>

struct time
{
    int hours;
    int minutes;
    int seconds;
};

main ()
{
    int timeDifference (struct time  diff);

    struct time  early, late, difference;

    printf ("Enter Start Time hh:mm:ss ");

    scanf  ("%d:%d:%d", &early.hours, &early.minutes, &early.seconds);

    printf ("Enter End Time hh:mm:ss ");

    scanf  ("%d:%d:%d", &late.hours, &late.minutes, &late.seconds);

    int timeDifference (struct time  diff)
    {
            if (late.seconds < early.seconds)
            late.seconds += 60;
            late.minutes -= 1;
            if (late.minutes < early.minutes)
            late.minutes += 60;
            late.hours -= 1;
            if (late.hours < early.hours)
            late.hours += 24;

            diff.seconds = late.seconds - early.seconds;
            diff.minutes = late.minutes - early.minutes;
            diff.hours   = late.hours   - early.hours;
    }

    return 0;
}

4 个答案:

答案 0 :(得分:1)

你在C中的另一个函数中不能有一个函数。第12行的timeDifference声明和从第20行开始的函数本身(定义)需要移到{{1}之外1}}功能。

答案 1 :(得分:0)

timeDifference的定义移到main()函数之外。根据C标准,此类构造未定义。有些编译器确实接受了它,但在你的情况下,编译器似乎放弃了。

答案 2 :(得分:0)

你不能在另一个函数中声明一个函数,将timeDifference移到main之外  在你旁边没有给main一个返回类型,把它int main()

答案 3 :(得分:0)

虽然以下代码未完整,但它可能会帮助您:

#include <stdio.h>

struct time
   {
   int hours;
   int minutes;
   int seconds;
   };

int timeDifference(
      struct time *I__early,
      struct time *I__late,
      struct time *_O_diff
      )
   {
   if(I__late->seconds < I__early->seconds)
      I__late->seconds += 60;

   I__late->minutes -= 1;
   if(I__late->minutes < I__early->minutes)
      I__late->minutes += 60;

   I__late->hours -= 1;
   if (I__late->hours < I__early->hours)
      I__late->hours += 24;

   _O_diff->seconds = I__late->seconds - I__early->seconds;
   _O_diff->minutes = I__late->minutes - I__early->minutes;
   _O_diff->hours   = I__late->hours   - I__early->hours;

   return(0);
   }

int main()
   {
   struct time  early, late, difference;

   printf ("Enter Start Time hh:mm:ss ");
   scanf  ("%d:%d:%d", &early.hours, &early.minutes, &early.seconds);
   printf ("Enter End Time hh:mm:ss ");
   scanf  ("%d:%d:%d", &late.hours, &late.minutes, &late.seconds);

   timeDifference(&early, &late, &difference);
   ... 

   return(0);
   }