0

I want to declare a structure in a header file. When I declare a simple variable in a header file I simply specify the variable as external like this.

The variable in the .c file:

int var;

And the same variable presented in the header file:

extern int var;

So far so good. But what about a struct? If I have the following struct in my .c file

typedef struct
{
   unsigned char seconds;
   unsigned char minutes;
   unsigned char hours;
   unsigned char day;
   unsigned char month;
   union
   { 
      unsigned int year; 
      unsigned char year_byte[2];
   }year_vars;
 }time;

How do I declare the structure in the header file?

1
  • 1
    what you want? structure or structure variable? both are seperate things. Commented Mar 23, 2015 at 13:23

3 Answers 3

1

Declaring a global variable is not the same thing as declaring a type. If the typedef should be visible to everyone that includes your h file, then naturally the typedef needs to be in the h file.

And the other way around: if the typedef is local to your C file, there is no need to present it to the caller at all.

Please note that there is never a reason to use global non-constant variables in C. Replace them with static file scope variables in your C file, that are accessed through setter/getter functions.

Sign up to request clarification or add additional context in comments.

Comments

1

Into the .h go:

  • Definitions of all types to be used for externally visible variables
  • All extern declarations of variables (thus externally visible variables)

Into the .c go:

  • Definitions of all types to be used by internal-only visible variables
  • Definitions of all variables (visible externally and internally)

Comments

0

Type definitions should go in the header only. They do not need to be repeated in the .c files. Simply include the header in your source files to access the type definitions.

time.h

typedef struct
{
  ...
} time;

time.c

#include "time.h"

4 Comments

As a side note, don't pick the same name for your header file as one of the standard libraries.
Why not? #include <time.h> versus #include "time.h" makes it easy to tell which is which.
The compiler is free to implement <time.h> versus "time.h" in pretty much any way it wants, it is impl.defined behavior. Further, the standard actually states that (6.10.2) if you use " " and the header can't be found, the following applies: "The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read # include <h-char-sequence> new-line".
Even more important, 7.1.2 states : "If a file with the same name as one of the above < and > delimited sequences, not provided as part of the implementation, is placed in any of the standard places that are searched for included source files, the behavior is undefined." So the code you posted with #include "time.h" is actually guaranteed to be undefined behavior.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.