Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Header Files

I am experienced in C and C++ but I am not veery experienced in making Header files and accesing them from a program,
so I was hoping that someone could give me an example on how I could do something like that!


1 Solution
Hello !

This is not very difficult.

For eg:

Take a COMPILER project.

It is very hard to understand, if you have in the SINGLE FILE.

Now what I will do is , I will divide into different files.

1. Definition file
    Declaring Global Variables,


    Here you declare all the definitions.

2. Lexical Analyzer


    #include "def.h"   /* This is your definition file */

   -- do whatever you want.

   It will include the code of def.h

I hope this helps you
smoothasiceAuthor Commented:
what he said didn't really hellp me out much
The basic idea behind header files is to reuse definitions or even many includes to other headers files.

Let's say that you have a program composed by two modules and booth modules uses the same struct definition.

a) Create a file, let's say BaseProg.H and insert:

1. All includes that the two modules uses.
2. The struct definition in there.

b) In the two modules, remove de common includes
c) Add a line #include "BaseProg.H"

d) Now, every time that you change the struct definition (I'm assuming that you uses some kind of makefile or, better, a kind of integraded environment, the two modules recompiles and Bingo!

If you need a working sample I can supply one.

Last thing

Notice that i wrote #include "BaseProg.h". You will see that the compiler includes uses something like #include <stdio.h>. The difference in the constructions above indicates that the first searches for the included file in the current directory. The other looks in the INCLUDE= directory, normaly the compiler include directory.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Anton, it is hard to understand what you need from the wording of the question.
Do you mean .H files usually accessed by the #include directive?
How to create/manage header files:

Things that can go into header files:

1.  Reusable definitions like
other header files: #include <stdio.h>
constants: #define CONSTANT 8
function prototypes: void print(void);
macros: #define debugmacro(x) printf("Debugging ... %s", #x); x;
class definitions: class dummy { int i; public int access(); };
extern global variables: extern int globalint;

2.  Conditional compilation statements:
eg. different code for different compilers:
#ifdef __STDC__
/* Some defintions here. eg ANSI C function prototypes. */
/* Some other definitions here. eg K&R function prototypes. */

eg. different target requirement:
/* ... */
/* Include debug macros or functions here */
/* Include no debug functions. */

3.  Customized error messages for your own project:
#error Should have declared MYCONSTANT by now!!

4.  Customized features for your compiler:
#pragma /* something your compiler can understand. */

Note: Sometimes header files may not be included more than once.  This is achieved by using include guard:



/* all header material goes here. */


/* put nothing here. */


Now if you include the header twice, then the 1st #include includes content of file but 2nd #include includes nothing.
NewExpert made a point. Like we said here in Brazil "he kiled the snake and showed the stick".

Seems like the wrong point to me.  NewExpert gave a summary of preprocessor features.  However, most of them can be used just as well in source files.
Well I would have said headers are defined by wanting to share common stuff - but also but by what they shouldn't include:

Example: Don't declare storage, do declare definitions */

int x ; /* Don't put this is a .h */
extern int x ; /* Do put this in a .h */
Elaborating Answer2000 one way to do this is:

1. Put this in every header file:

#ifdef      MAIN
#define      EE
#define      EE      extern

2. To define a variable use:

EE int somevar;

3. In the module that contain main/winmain, before all includes, do:

#define MAIN

4. Restrictions. The variables defined as above cannot be initialized in the header.


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now