-1

I am trying to create a simple input to simulate Real Time Clock values. All I want the code to do is calculate to difference in time between the two tm structs (Time1Start and Time1End). The month, day, year, etc do not matter as these calculations are assumed to be done on the same day.
I think it is right to assume that the hours and minutes fall on the first day of 1900 since it hasn't been initialized.

I am getting a warning and a couple errors as soon as I try to manipulate either struct. The code is as follows:

#include <time.h>

struct tm Time1Start;    
Time1Start.tm_hour = 0;  //start of day - errors start here (line 38)
Time1Start.tm_min = 0;

struct tm Time1End;
Time1End.tm_hour = 17;   //5:XX o'clock
Time1End.tm_min = 30;    //5:30

double seconds;
double minutes;
seconds = difftime(mktime(Time1Start), mktime(Time1End));
minutes = seconds / 60;

The errors I recieve are:

build.h:38: warning: (374) missing basic type; int assumed

build.h:38: error: (984) type redeclared

build.h:38: error: (1098) conflicting declarations for variable "Time1Start" (build.h:37)

I am running in MPLAB X IDE v3.30 using the X8 compiler.
Please help

Destrif
  • 2,104
  • 1
  • 14
  • 22
Spiff
  • 1
  • 1

1 Answers1

0

You can not have general statements outside of functions, you need to set up the structure members in your main function, or some other initialization functions.

So the code should instead look like this:

#include <time.h>

struct tm Time1Start;    
struct tm Time1End;

double seconds;
double minutes;

int main(void)
{
    Time1Start.tm_hour = 0;  //start of day - errors start here (line 38)
    Time1Start.tm_min = 0;

    Time1End.tm_hour = 17;   //5:XX o'clock
    Time1End.tm_min = 30;    //5:30

    seconds = difftime(mktime(Time1Start), mktime(Time1End));
    minutes = seconds / 60;

    // Rest of the main function code...

    return 0;
}

On a related note, unless the variables are used by different functions and can't be passed as arguments to those functions, then don't use global variables, define the variables as local variables inside the main function instead. Or if they are only used in another single function, declare them as local variables inside that function which uses them.

Note that the above should be in a source file. If you want or need the variables to actually be global, then you should put the declarations in a header file that you include in the source files that need the variables. That header file could look something like

// First an include guard (https://en.wikipedia.org/wiki/Include_guard)
#ifndef BUILD_H
#define BUILD_H

#include <time.h>

extern struct tm Time1Start;    
extern struct tm Time1End;

extern double seconds;
extern double minutes;

#endif // BUILD_H

The extern keyword is what makes these a declaration of the variables, instead of a definition. It basically tells the compiler "this variable is defined somewhere, it's okay to use it".


As for the error message, older standards of the C language allowed variables to be defined without an explicit type, and then they were implicitly declared as int, so instead of e.g.

int some_variable = 5;

it used to be enough to have

some_variable = 5;

Now the first warning message is telling you that you are missing the type, and this implicit int declaration is done. Then you get an error because the variable have already been declared using a different type.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Not the downvoter but I think you might be wrong. However, calling functions outside of functions will almost certainly fail. – KevinDTimm Jul 07 '16 at 15:21
  • @KevinDTimm In the global scope, outside of any functions, you can only declare things, define things, and that's it. You can not have statements like assignments or other expressions. – Some programmer dude Jul 07 '16 at 15:28
  • Thanks; after rereading your answer I saw where my first comment was invalid. Thx. – KevinDTimm Jul 07 '16 at 16:42