- In this video, we are continuing our discussion
of preprocessor's commands, or directives,
mainly with the conditional directives.
Let's see what they are.
We saw already that we can define MACRO
as a preprocessor command.
In this particular case, if the MACRO
that's also includes an if statement
which basically says the macro is defined.
So if there is such a definition for some random macro
called MACRO, then we are displaying some text.
Another example of a conditional directive
is #if followed by an expression.
So if something equals something
or something is bigger than something,
then we also display some text, endif.
Let's look at two more directives.
This is a two-part directive which is encapsulated
in an if, else, endif structure.
if expression, then we print the text
if that expression is true.
else, meaning that expression is false,
we are displaying another text.
And that's it.
Everything that starts with a pound sign is
a preprocessor directive.
It goes for includes and define and so on.
Let's switch to the code
and see some of those things in action.
So in this example, we'll start
with something a little bit different than before.
#undef MAX_PRICE.
Let's look at headers.
In headers on line 12, I have defined
a MAX_PRICE to be equal 45.
This header file might be included in multiple files.
Somebody thought that this is of useful enough value
that it should be defined in a header.
However, that could be one instance
in which that MAX_PRICE is not applicable.
Therefore, line 15 says,
"undef, undefine MAX_PRICE."
Now that it no longer has a value,
you can define it again as 100.
It is only valid in this file.
It doesn't affect the myHeader.h file.
A second conditional directive is in line 18.
The word in line 18 is shortcut
for if not define THRESHOLD.
If we do not have a macro or a constant
labeled THRESHOLD, then we define it
as THRESHOLD with value 23.751.
Let's take a quick peek at myHeaders.
There is no THRESHOLD defined.
So this if hold, therefore now
we do have a THRESHOLD 23.751.
Finally, another interesting definition
of a macro is line 22.
I'm defining a function which we call greet
which receives one parameter, name.
printf("Thank you for shopping here,
and then the value of that parameter.
So what this greet does, it greets.
So it produces a message that says,
"Thank you for shopping here", and then the parameter.
Let's put this to test.
First, we're going to run it.
It says that MAX_PRICE is 100
because we undefined whatever was there.
THRESHOLD is 23.751000
because we're printing this as a float.
Then the greet says, "Thank you for shopping here."
Now, where this Isac came from?
It came from calling greet with the word Isac.
Notice that even though this is string,
it does not require double quotes
and this is special to macros.
It just takes it the way it is.
Then we have an interesting set of outputs.
File, Date, Time, Line, and ANSI.
If you look at those lines, 33 through 37,
those are predefined macros in C.
So the file knows its own name.
That's line 33 which is printed
File and the full path to it.
Second, the Date, May 9 2014
which is the date that this movie was recorded.
Then the Time, 20:46:51.
This is the time at which it was recorded.
That particular line printf
at line 36 can display its own line.
So wherever you put that printf
with the predefined macro line,
it will tell you which line it is.
Then finally, ANSI, that's the body of standards
that govern, among other things,
the definition of the C language, tells you
that the value of its current standard is one.
That pretty much wraps us our high level overview
of macros, includes, preprocessor directives,
and all related topics.