# what information does Header files contain

#### Parth786

Joined Jun 19, 2017
642
Hi
I want to understand the basic of header files in depth. I googled search and found some information. This page has good information https://en.wikibooks.org/wiki/C_Programming/Preprocessor. header file is file which tells compiler to include code from header file in program. header file contain two essential things: the definitions and prototypes of functions
Code:
#include<reg51.h>
#include<delay.h>
#include "delay.h"
#include<string.h>
#include "string.h"
............
Some header files are inbuilt in compiler For example #include<delay.h> and I think, we need to add multiple header files in long program.
I want to design a header file. What information need to design new header files ? How would you design new header file ?

Note : I want to learn skills to design any header file use in embedded system

Last edited:

#### WBahn

Joined Mar 31, 2012
26,398
You can put in a header file pretty much whatever you want. The contents are imported pretty much just as if they had been copied/pasted at the location where the #include statement is.

The usual role for header files is to put thing that need to be seen in multiple files. They can also be used to organize parameters. There are LOTS of things they are useful for.

#### Parth786

Joined Jun 19, 2017
642
You can put in a header file pretty much whatever you want. The contents are imported pretty much just as if they had been copied/pasted at the location where the #include statement is.

The usual role for header files is to put thing that need to be seen in multiple files. They can also be used to organize parameters. There are LOTS of things they are useful for.
Can we start with simple example, later we will discuss more complex Examples :
C:
#include <REG51.h>
#define LED_ON  (1)  /* LED ON */
#define LED_OFF  (0)  /* LED OFF */
sbit LED = P1^7;  /* LED connected to port P1 of pin 7 */

/* this is delay function */
void Delay (unsigned int Loop)
{
for (Loop = 0; Loop < 50000; Loop++);
{

}
}

void main()
{
LED = LED_ON;  /* at start LED will should ON */

while (1)
{
LED = LED_ON;  /* Turn ON LED */
Delay(100);  /* Wait for some time */
LED = LED_OFF;  /* turn off LED */
Delay(1000);  /* Wait for some time */
}
}

#### spinnaker

Joined Oct 29, 2009
7,835
Can we start with simple example, later we will discuss more complex Examples :
C:
#include <REG51.h>
#define LED_ON  (1)  /* LED ON */
#define LED_OFF  (0)  /* LED OFF */
sbit LED = P1^7;  /* LED connected to port P1 of pin 7 */

/* this is delay function */
void Delay (unsigned int Loop)
{
for (Loop = 0; Loop < 50000; Loop++);
{

}
}

void main()
{
LED = LED_ON;  /* at start LED will should ON */

while (1)
{
LED = LED_ON;  /* Turn ON LED */
Delay(100);  /* Wait for some time */
LED = LED_OFF;  /* turn off LED */
Delay(1000);  /* Wait for some time */
}
}
You are kidding right??

Don't you see the #include already in there? How do you think you would add #include"Delay.h" ?

#### Parth786

Joined Jun 19, 2017
642
You are kidding right??

Don't you see the #include already in there? How do you think you would add #include"Delay.h" ?
My mistake, I couldn't explain it in right way. I can write program for LED blinking and now I want to write program for LED blinking, where I want to add this #include"Delay.h" header file because my main purpose to design header file so How to design this #include"Delay.h" header file

#### WBahn

Joined Mar 31, 2012
26,398
Why won't you get a textbook on C programming and work through it?

#### Parth786

Joined Jun 19, 2017
642
Why won't you get a textbook on C programming and work through it?
I have text books but there is not enough information. I tried to fix issues myself, visited on some links and tutorials. but I couldn't get much help from their descriptions. I was looking some thing like pesudo code or algorithm or hand written explanations or top level designing module

Last edited:

#### Parth786

Joined Jun 19, 2017
642
I studied that link and I found that header file contain definitions and prototypes of functions. I am explaining with example
Code:
#ifndef HEADER_NAME_H
/*... Program code ... */
#endif
I think this is basic step to create any header file.I am trying to create header file for delay.h and below are the code for delay.h
Code:
#ifndef  DELAY_H
#define DELAY_H
void delay(unsigned int );
#endif /*  DELAY_H
Please let me know if you find any mistakes in my explanation I will try to improve my basic understanding regarding header files

Last edited:

#### Parth786

Joined Jun 19, 2017
642
Last edited:

#### WBahn

Joined Mar 31, 2012
26,398
I studied that link and I found that header file contain definitions and prototypes of functions. I am explaining with example
Code:
#ifndef HEADER_NAME_H
/*... Program code ... */
#endif
I think this is basic step to create any header file.I am trying to create header file for delay.h and below are the code for delay.h
Code:
#ifndef  DELAY_H
#define DELAY_H
void delay(unsigned int );
#endif /*  DELAY_H
Please let me know if you find any mistakes in my explanation I will try to improve my basic understanding regarding header files
Have you even tried to compile anything using that header file? You have a block comment that you open but never close.

Do you have ANY idea for the reason for the #ifndef wrapper? Or are you just parroting code you found someplace?

#### xox

Joined Sep 8, 2017
678
The long and short of it is this: if you want to reuse code sensibly in C you need to use header files. Why? Because if you place all of the code in a single implementation file (say "Delay.c") and #include the file from within several modules which are to be compiled separately and then try to link everything together, you'll most likely get a bunch of "multiple reference" linker errors. So instead you put your declarations in a header file, all of the definitions in an "implementation file", and then compile the latter into an object file (you can also turn it into a shared or dynamic library) which then gets linked to your program. It's a bit of a pain in the arse, I know (and probably explains why languages such as Java have become so popular), but that's just the way it is. That said if you're just getting started and plan to write "single file" programs for now you could possibly get by including the implementation file itself (ie: "Delay.c") but just keep in mind that it's a very limited approach and of course "highly frowned upon".

#### MrSoftware

Joined Oct 29, 2013
2,027
Start reading about the compilation steps, starting with the preprocessor (preprocessor, compilation, link, etc..). Once you understand the compilation steps, the way a header file works will be obvious.

#### ArakelTheDragon

Joined Nov 18, 2016
1,353