0

The compiler is giving me an error message on

line 20: "static declaration of ‘timeDifference’ follows non-static declaration"

and then another one on

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

I know it has something to do with my function 'timeDifference'. Here is my code:

#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;
}
dirkk
  • 6,160
  • 5
  • 33
  • 51
user3558029
  • 21
  • 1
  • 1
  • 1
  • 1
    You have prototype of function and the definition in the body of your `main`. Move the prototype to before the `main`, and the function after the `main`. – AntonH Apr 21 '14 at 21:05
  • 1
    @AntonH the prototype can well remain inside main. – bubble Apr 21 '14 at 21:12
  • Read the basics first before trying. http://www.tutorialspoint.com/cprogramming/c_functions.htm – Luke Hutton Apr 21 '14 at 21:12
  • Related Question http://stackoverflow.com/questions/8090975/function-definition-inside-another-function-definition-is-it-valid – bubble Apr 21 '14 at 21:17

4 Answers4

1

You can't have one function inside another in C. The declaration of timeDifference on line 12, and the function itself (the definition) starting at line 20, need to be moved outside your main() function.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
0

Move the definition of timeDifference outside main() function. According to C standard such construct is not defined. Some compilers do take it but in your case the compiler seems to have given up.

bubble
  • 3,408
  • 5
  • 29
  • 51
0

you can't declare a function inside another function, Move timeDifference outside the main beside you didn't give main a return-type, make it int main()

Jalal Mostafa
  • 984
  • 10
  • 23
0

Although the following code is not complete, perhaps it will help you along your way:

#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);
   }
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28