Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

#ifndef...#define pair confusion

Status
Not open for further replies.

syedshan

Advanced Member level 1
Joined
Feb 27, 2012
Messages
463
Helped
27
Reputation
54
Reaction score
26
Trophy points
1,308
Location
Jeonju, South Korea
Activity points
5,134
Hello all,

I understand and sometimes use #ifndef-#define pair when I have to declare directives.
But in some reference designs I have seen the use such that I cannot udnerstand why is it used.
Once such example is the following header file, named UTILS.H, the file contains functions and
pre-processor directives, but it is started as...

Code:
#ifndef _UTILS_H_
#define UTILS_H_

#include <stdio.h>
...
...
...
#endif

my question is why is #ifndef use here and also what is _UTILS_H_ used here.
It can also be started directly from #include <stdio.h>

Bests
Shan
 

Usually it's there because your program may use it in more than one file. For example, if your program had files called file1.c, file2.c and file3.c and more than one of them included '#define UTILS.H', it would flag an error because the name UTILS.H would be being defined more than once. By using #ifndef you are saying "only if UTILS.H has not already been defined somewhere already, define it now'".

Brian.
 
Check

Final step - header file protection

The last thing to do before our conversion is complete, is to protect our header files from multiple inclusion. Take the following example. Say that Main and ADC both refer to each other:

Main.h
Code:
 #include "ADC.h"

ADC.h
Code:
 #include "Main.h"

What happens when this is compiled? The preprocessor will look at Main.h, then include ADC.h. However, ADC.h includes Main.h, which again includes ADC.h, etc...

To guard against this problem, we can use preprocessor defines. The following code snippet is the basic protection setup:

Code:
 #ifndef MAIN_H 
#define MAIN_H 
 
// Header file contents 
 
#endif

This construct, when applied to each of your header files, will protect against multiple inclusions. As each C file is compiled, the associated header file is included, as well as any other referenced header files (via includes in the C file's header file). As each header is included, a check is performed to see if the header's unique token is already defined, and if so the inclusion halts to prevent recursion. If the token is not already defined, the preprocessor defines it and looks at the remainder of the header file's contents. By giving each header file a different token (typically the header's filename in ALL CAPS, and the period replaced by an underscore), this system will prevent any preprocessor troubles.
 
Thank you both for replies.

So it goes like this...Lets say library name is XYZ, then we will write... _XYZ_H_ right?

Well now I understand another debate that I read few times about #pragmas once...

Thanks.
 

So it goes like this...Lets say library name is XYZ, then we will write... _XYZ_H_ right?

That is what is typically used but at the end it is up to you, for example I have seem headers using _XYZ_H_INCLUDED_
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top