Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# What does IFNDEF do and why do I care to use it?  Please explain.

Posted on 2011-02-12
Medium Priority
608 Views
Last Modified: 2013-12-14
Please see code...

What is the point of IFNDEF?

And FUNCTIONS_H_INCLUDED?

And define FUNCTIONS_H_INCLUDED?
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

class MathFunc
{
public:
static int add(int operand1, int operand2);
};

#endif
0
Question by:shaolinfunk
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 3
• 2
6 Comments

LVL 86

Assisted Solution

jkr earned 1000 total points
ID: 34879787
The construct above is a so called "header guard" (http://en.wikipedia.org/wiki/Include_guard), which is used to ensure that a header file and the declarations it holds are only seen once by the compiler, thus avoiding "symbolmultiply defined" errors. See also http://faculty.cs.niu.edu/~mcmahon/CS241/c241man/node90.html ("Include guard")
0

LVL 45

Accepted Solution

AndyAinscow earned 1000 total points
ID: 34879788
The # indicates something the compiler should be checking.
#ifndef   - if not defined
#ifdef    - if defined
both work with the #else and #endif, like the if...else... statements in code.

The FUNCTIONS_H_INCLUDED is just something (unique) you made up.

#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

class MathFunc
{
public:
static int add(int operand1, int operand2);
};

#endif
basicaly that tells the compiler that if FUNCTIONS_H_INCLUDED has not yet been defined that it should enter this code section, assign a value to FUNCTIONS_H_INCLUDED (so it won't be entred again) then compile that block of code
0

LVL 86

Expert Comment

ID: 34879795
BTW, to elaborate on your code example:
#ifndef FUNCTIONS_H_INCLUDED // precompiler checks if that symbol has already been set. If so, the entire code up to '#endif' will be skipped
#define FUNCTIONS_H_INCLUDED // symbol not encountered earlier, define it. A second encounter will trigger the effect menioned above

class MathFunc
{
public:
static int add(int operand1, int operand2);
};

#endif // Kinda the 'closing brace' for the conditional compilation
0

LVL 86

Expert Comment

ID: 34879800
And a final 'PS:' on the whole concept of conditional compilation: http://en.wikipedia.org/wiki/C_preprocessor#Conditional_compilation
0

LVL 1

Author Closing Comment

ID: 34879803
Very illuminating.  Thank you, kind sirs!
0

LVL 1

Author Comment

ID: 34879806
Thank you for that additional post jkr.  That helped!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generatâ€¦
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month8 days, 9 hours left to enroll

#### 596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.