- In this video we will discuss
the definition and usage of macros
which are an interesting feature
that allows our programming to
become easier and gives us a few
powerful tools as we can see.
There are two types of macros.
First lets define formally what a macro is.
Very simple definition.
It's a code fragment that has a label or a name.
We distinguish between two main types of macros.
One is the one that looks like an object.
For example, numbers defined as one, two, three.
So it's a sequence of numbers which then can be used
to assign values when we initialize an array.
So it is an encapsulated object
that has three integers which are now
being used to initialize an array.
That's an Object Macro.
Another type of macro is one
that looks like a function.
So notice the first line where it says define max(X, Y)
meaning a function that finds the higher
of x and y or the larger of x and y.
Depends on your frame of reference.
If you look at that line this is
essentially an if else notation in a shortcut.
Other languages also introduce this notation,
but it was originated in C.
What this line says is compare x and y.
If x is bigger than y,
that's the question mark,
then x else y.
So if x is bigger than y return x.
Otherwise return y.
And we define that as max(X, Y).
We can then use it in multiple ways
in which we need to compare two values.
For example, max(p,q).
Just like with a function I am using
p and q as arguments.
They're being mapped onto x and y,
and I am now comparing p and q.
The value for max(p,q)'s comparison
is being returned in a variable lowercase x,
which is not to be confused with
uppercase X in the definition.
The second we are comparing two values 12 and 5.
Those two values now are being mapped
onto x and y and the macro compares
12 versus 5 so obviously the result will be 12.
Finally we are comparing n+5.
Presumably n has been assigned a value prior to that,
a guest of pointer p that also points
to an address that contains an integer.
So presumably p has been initialized
somewhere and now we have two values
that are integers to compare.
Notice that the macro doesn't care
about the type of those items.
Lets go to the code and see how this works.
The same exact macros are being defined.
The object numbers one to three.
The list is not to be that interesting
so we are not going to talk about it.
It's simply an array of three numbers.
What is interesting is the other macro.
So let's see what we do for it.
First we need to prepare all the variables
that we are going to use.
Line 23 we defined two pointers
p and q and a value n, then two n's,
val one which is 5, val 2 which is 12.
And then we have assigned to those
two pointers p and q the address
of val one and val two.
So now those two pointers point
to five and 12 respectively.
We also initialize the integer n to one.
So lets start our first comparison.
Just like in the previous screen.
We are going to compare
p and q which are pointers.
Comparing two pointers let's see
when we run what does this mean in terms of results.
So as you can see the pointers p and q
are actually addresses in memory.
So we are comparing two addresses
not what is being stored at that address.
So, if you look at the hex values.
The one that the macro chose to
be the larger is 529c6c6c.
Since If you look at what is the end
of those numbers they both identical
except the last two digit 6c and 5c.
6c it's not actually 6c its 60 in hex.
So in that notation 6c is still
bigger than 5c and therefore.
So regardless of what the content
that this pointer point to this address.
So the label of the address itself
which is also a number is being compared.
The second 1 y.
Its obvious 12 is bigger than 5
so the result is 12 and z is 6.
Now where did the 6 come from?
Notice in Line 27 that n is one.
Therefore in Line 31 n + 5 is 6.
So this is one value that we're comparing.
The other one is the pointer p.
If you look at Line 24 value1=5.
Line 25 p points to the address
of value1 which means p points to 5.
Therefore p with a * is 5.
P without the *, if you look at the output
two line above is 529c6c60.
So the last comparison, comparison number 6,
with the number 5, the number 6 is bigger,
therefore z = 6.