views:

295

answers:

3
#define one 0
#ifdef one
printf("one is defined ");
#ifndef one
printf("one is not defined ");

In this what is the role of #ifdef and #ifndef, and what's the output?

+20  A: 

Text inside an ifdef/endif or ifndef/endif pair will be left in or removed by the pre-processor depending on the condition. ifdef means "if the following is defined" while ifndef means "if the following is not defined".

So:

#define one 0
#ifdef one
    printf("one is defined ");
#endif
#ifndef one
    printf("one is not defined ");
#endif

is equivalent to:

printf("one is defined ");

since one is defined so the ifdef is true and the ifndef is false. It doesn't matter what it's defined as. A similar (better in my opinion) piece of code to that would be:

#define one 0
#ifdef one
    printf("one is defined ");
#else
    printf("one is not defined ");
#endif

since that specifies the intent more clearly in this particular situation.

In your particular case, the text after the ifdef is not removed since one is defined. The text after the ifndef is removed for the same reason. There will need to be two closing endif lines at some point and the first will cause lines to start being included again, as follows:

     #define one 0
+--- #ifdef one
|    printf("one is defined ");     // Everything in here is included.
| +- #ifndef one
| |  printf("one is not defined "); // Everything in here is excluded.
| |  :
| +- #endif
|    :                              // Everything in here is included again.
+--- #endif
paxdiablo
+6  A: 

Someone should mention that in the question there is a little trap. #ifdef will only check if the following symbol has been defined via #define or by command line, but its value (its substitution in fact) is irrelevant. You could even write

#define one

precompilers accept that. But if you use #if it's another thing.

#define one 0
#if one
    printf("one evaluates to a truth ");
#endif
#if !one
    printf("one does not evaluate to truth ");
#endif

will give one does not evaluate to truth. The keyword defined allows to get the desired behaviour.

#if defined(one) 

is therefore equivalent to #ifdef

The advantage of the #if construct is to allow a better handling of code paths, try to do something like that with the old #ifdef/#ifndef pair.

#if defined(ORA_PROC) || defined(__GNUC) && __GNUC_VERSION > 300
tristopia
This is an important subtlety that often goes overlooked.
Nighthawk
Not **some precompilers**. All. It's part of the standard. Please fix.
R..
done. I was not sure and hadn't the standard handy. And I remember having porting problems with that, but it was 20 years ago, so it might have been a non compliant compiler, as it was before ANSI C.
tristopia
A: 

ok thanks to all people.. but

let say #if defined(ORA_PROC) || defined(__GNUC) && __GNUC_VERSION > 300 here already ORA_PRDC, __GUUC VERSION its not defined before than how we can say that #if defined(ORA_PROC) || defined(__GNUC) && __GNUC_VERSION > 300

how can we apply condition.. and is this for only preprocessor conditions before coming o compiler.. preprocessor directory only linked witll preprocessor...

ohh GOD clear it to me that what is prerocessor..

piyapiya
If you have a question, please feel free to ask it using the "Ask Question" button at the top of this page. (Please try to use complete sentences and format your code when you do ask a question.)
James McNellis
The "#if defined" clauses check if the conditions are defined before accessing them. C++ uses a two-stage compiling process. First the precompiler is called to process #if statements and other precompiler statements (#pragma, etc), and then that output is sent to the compiler to generate the files that the linker then combines into the final executable.
Remy Lebeau - TeamB
i did not get complete.... i jsut come to know this that we can use conditions in main for compiler but using preprocessor we can use that condition on any executable window.. no no i ma not geting...
piyapiya
and tell me one thing more..
piyapiya
typedef struct _op_polygon // Polygon set structure{ int *hole_; // Hole / external contour flags std::vector< op_linestring > rings_; // rings _op_polygon() { hole_ = NULL; } Bool operator==(_op_polygon int rn2 = other.rings_.size(); if( rn1 != rn2 ) return FALSE; for( int i=0; i<rn1; i++ ) if( !(rings_[i] == other.rings_[i]) ) return FALSE; return TRUE; }} op_polygon;
piyapiya
typedef struct _op_polygon // Polygon set structure{ int *hole_; // Hole / external contour flags std::vector< op_linestring > rings_; // rings _op_polygon() { hole_ = NULL; } Bool operator==(_op_polygon int rn2 = other.rings_.size(); if( rn1 != rn2 ) return FALSE; for( int i=0; i<rn1; i++ ) if( !(rings_[i] == other.rings_[i]) ) return FALSE; return TRUE; }} op_polygon;
piyapiya
now here i use typedef to give just another name of _op_polygon struct.. her we sue this Bool operator==(_op_polygon int rn2 = other.rings_.size(); if( rn1 != rn2 ) return FALSE; for( int i=0; i<rn1; i++ ) if( !(rings_[i] == other.rings_[i]) ) return FALSE; return TRUE; }
piyapiya
sorry, i got it....
piyapiya
ok now i have more questions...why template is sued..
piyapiya
like i make a template <class T> and than i maek a class of template_list in this i make two more class that are of class link and class iterator. my question is that template is sued to group classes is that ok . if yes than we should compbine classes by template {}
piyapiya
@piyapiya : Template is NOT used to group classes. template keyword is used to create generic classes and functions, which means depending on the data type you specify(while creating a class or passing param to a function call), the compiler will generate code for class or function which accepts the specified data type. You will need to read through the books to understand the concept of templates. Please read and return back to ask the Q if you still dont understand it.
Als