#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?
Examples of role in a SentenceHe plays the role of the villain. She was given the starring role. I had a minor role in the play. After her husband left her, she had to take on the dual role of mother and father for her children.
the position or purpose that someone or something has in a situation, organization, society, or relationship: What is his role in this project?
role, in sociology, the behaviour expected of an individual who occupies a given social position or status. A role is a comprehensive pattern of behaviour that is socially recognized, providing a means of identifying and placing an individual in a society.
The definition of a role is a part or character someone performs or the function or position of a person. An example of a role is the character of the nurse in Romeo and Juliet. An example of a role is doing accounting for a business.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With