#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

 
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.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…