 Hello everyone, the topic for my today's lecture is macro expansion in C language. Now, the learning outcome at the end of the session the students will be able to describe the concept of macro expansion in C language, a macro is one of the preprocessor directive in C language. Now, what are macros in C, a macro is actually a fragment of a code which has been given a particular name. So, whenever that name is used we can say that it is replaced by the contents of the macro, like as example you have over here hash defined PQR 15. So, PQR is the macro and 15 is the value assigned to it. So, there is nothing but the macro definition or just called as macro. So, what does a preprocessor do? It replaces every occurrence of the macro PQR in the program with the value 15. So, therefore PQR the macro template and 15 is its expansion or the value that will be replaced for PQR. Let us take a sample program. So, in this program you can see the header file included, then with the help of hash define AB5 we are defining a macro whose value is initialized to 5. And in the main function we have declared a variable i, then we are taking a loop in which the value of i is initialized to 1 and i less than equal to AB. So, here for the macro AB the value of 5 will be replaced. And of course, the next part the iteration and this goes on to print the value of i in every iteration of this loop. And after that we can see in the output that the numbers 1, 2, 3, 4 and 5 are displayed. Now, let us see what is macro expansion. Now, at the time of compilation the C preprocessor it will examine the source code. If at all there is any macro definitions present on what? If it sees any hash define. So, in the entire program it will search for such macro templates. And wherever it finds the macro templates it will replace all these macros with their appropriate expansions. This is done at the time of preprocessing. Now, after completing this procedure then the program is handed over to the compiler for compilation. Generally, the capital letters are used for the macro template. A macro template and its expansion are separated by blanks or tabs. But when we write a hash define the space between hash and define will be an optional one. So, it is not a compulsion to have a space between the hash and the define. And the macro definition should never be terminated by a semicolon. It is possible to change the values of a constant that is used at multiple places in the program by just making a change in the hash define directive meaning where the macro has been defined. If you want to change the value of that macro will change it at the point where it has been defined. So, that actually has an advantage over the fact that if a variable is to be changed in the entire program. So, we need to go to every place and change that value. So, instead of that if some macro is used just changing the value of the macro at the point where it is defined will do the work. This could be quite beneficial for large programs where a particular value is used at multiple places in the program. So, the advantage for this could be the compiler can generate faster and more compact code for the constants than it can do for the variables. And we have a sample program now the points where a macro can be used and for what purpose it can be used. The first usage of macro would be it would be used to define any operator. So, as we can see over here in the program A and D is a macro which is used for the operator logical and that is ampersand ampersand. So, as you can see in the program if x less than 5 and y greater than equal to 50 then and then it will print the output everything is fine. So, here the and macro is used for the logical and operators. So, at pre-processing time this and will be replaced with the logical and that is ampersand used twice. So, the output for this program will be everything is fine. So, another usage of macro will be a macro can be used for a condition as well that is as you can see over here in this hash define c o and d is the name of the macro and it is used for this condition that is percentage greater than equal to 40 and percentage less than equal to 100. If this is required to be used in a statement then we can directly replace that with the name of the macro as can be seen in this example if c o and d. So, that means at pre-processing this condition of percentage greater than equal to 40 and less than percentage less than equal to 100 will replace at pre-processing at this point. So, the output for this program will be as a value of percentage the variable p e r is 63.75. So, this condition will be true. So, therefore, it will display as output within range. So, we have one more case where a macro can be used a macro can be used in fact to replace an entire statement. So, here we can see a macro named as even is used to replace an entire statement that is print f in double quotes this is even number. So, this macro if it is used in a program as can be seen over here if x mod 2 equal to equal to 0 meaning if x is an even number we have the statement even or rather we have the macro even. So, at pre-processing this macro will be replaced with this statement print f into double quotes this is even number. So, the output for this program also will be this is even number since x value is 6 it is even therefore, it displays this is even number as the output. So, now the students are expected to think and write the answer to the following question. This is the program that you have. So, what will be the output of the code? Now, pause the video and write your answer. Okay. Now, the answer for this question will be 12 now how did that come? We know y is a macro used and at pre-processing it will be replaced with its expansion x plus 2. So, if we see over here z equal to 2 into x plus 2. So, that means 2 multiplied by x plus 2. So, when we see the operators we have star operator and plus operator. So, where we know star as the highest priority. So, 2 multiplied by x the current value of x is 5. So, 2 multiplied by 5 equal to 10 plus 2 equal to 12 will be the value assigned to z. So, therefore, we get as output value of z as 12. Now, next we will see macros with arguments. As we know like how we have for functions we can also have arguments for macros. Now, this is an example hash define area into bracket s space into bracket s star s meaning here wherever we have this macro used area with a parameter s it will be replaced with the expansion s multiplied by s or s star s. So, that is what it says whenever it finds the phrase area of s it expands it into s into s. Now, we have a program for that here in this example also we have taken the same macro area of s which will be expanded with s multiplied by s. So, in the program we will see where macro is used at this point macro area is used with the parameter x. Now, value of x is 5. So, when it finds this area of 5 as a macro it will replace it with 5 multiplied by 5 or rather first x multiplied by x. So, 5 multiplied by 5 equal to 25 will be the value assigned to a. So, that is what will be the area of square equal to 25 as the output. So, in the above program the s in the macro template area of s is an argument that will match the s in the expansion s multiplied by s. So, the statement area of x will cause the variable x to be substituted for s and area of x is equivalent to x multiplied by x that is nothing, but its expansion. After pre-processing the compiler will get the following code to work on. Now, what will be the code? The code that will have the macro replaced with its expansion. That is we have a equal to rather than area x into x as its expansion. So, now there are some points to be remembered do not leave a blank between the macro template and its argument while defining a macro because if it is left then probably the appropriate value will not be assigned to the macro or appropriate expansion will not be assigned to the macro. The entire macro expansion should be enclosed generally within parenthesis. The macros can be split into multiple lines with the help of a backslash at the end of each line and if it is unable to debug a macro then the expanded code of the program can be viewed to see how the macros are getting expanded. Now, let us try to compare macros and functions. We know macro calls are like function calls, but they are not the same. So, in a macro call as we know pre-processor replaces the template with its expansion whereas, in a function call first of all the control is passed to the function with arguments then appropriate operations or calculations are performed in the function and then a value is returned back to the point from where the function was called. So, the question arises when to use a macro and when to use a function. So, macros make a program run faster whereas, it increases the program size, but functions make a program comparatively smaller and compact. So, if a macro is used n times in a program then that expansion will goes into the source code at n different places. So, this may increase the program size, but if a function is used and is called from n number of places in the program. So, it will take the same amount of space in the program. Finally, passing arguments to function and then returning back a value will take time. So, this may slow down the program. So, but macros if those are used those are already expanded and those are been placed in the source code before compilation. So, the conclusion will be when to use a macro and when to use a function if a macro is small it makes nice short hand. So, it will avoid the overhead associated with the function calls. But if a macro is large and is used often it must be replaced then with an appropriate function. So, this is the reference used for the video lecture. Thank you.