 Hello everyone, myself Mr. F.R. Syed from CSC department WIT, Singapore. The topic for my today's lecture is conditional compilation in C language. Now the learning outcome at the end of the session, the students will be able to describe the concept of conditional compilation in C language. Now let us have an introduction of that. Now sometimes the compiler can be made to skip the part of a source code meaning some part of code can be kept uncompiled with the help of some features available in C language. Now for that we may use the preprocessing commands of hash if def and hash end if. This is the general form of the usage of hash if def where we have hash if def space the name of the macro then a few statements and then hash end if. So the macro indicated with this macro name if it is not defined in the program then probably this three statements statement 1, 2, 3 will not be compiled. These will be combined only if this macro given with the macro name is defined. So this is how the hash if def works. Now if the macro name has been hash defined the block of code will get processed meaning it will get compiled. So where exactly do we find that this hash if def could be useful or can be used and when do we require that only a part of the program should be compiled. So we have three such cases the first case will be to comment out some obsolete lines of code meaning those lines of code which are no longer required which were defined initially but those are now not required. So an example that we see over here it says hash if def ok then we have the statement 1 and then hash end if and then statement 2. This means at compilation if this ok macro has been defined only then and then this statement 1 will be compiled otherwise it will not be compiled whereas the statement 2 since it is out of this hash if def and hash end if block. So statement 2 will get anyhow compiled during the process of compilation. The second case could be to make programs portable meaning if we have some code that is intended to be executed on a particular machine based upon its contents. So we can isolate such lines of code that are used for different machines by marking them off with a use of the hash if def preprocessor directive. So this hash if def hash else and hash end if work together like the if else block of C language. So we have an example for that in this program we have the hash if def used with space Intel. So Intel will be a macro over here then we have the code suitable for an Intel PC hash else otherwise code suitable for Motorola PC then hash end if and finally code common to both the computers. If we see how this program works so if the macro Intel has been defined in the program only then and then this code that is intended for the Intel PC will be compiled. Otherwise if that Intel macro is not defined then in that case the code that is applicable for Motorola PC will be compiled whereas the code that is common to both the computers in any case it will get compiled. So to run the program on a particular Intel PC we need to have the macro Intel defined that is we need to have a statement of this form hash defined Intel. So otherwise if we do not define that macro Intel so in that case our code will be able to run on a Motorola PC meaning if Intel macro is not defined so our code will be such that it is applicable for a Motorola PC. So accordingly the code intended for the Motorola PC will get compiled. So similar to hash if def we have one more directive called as hash if and def so which means that if not defined so it works exactly opposite to the hash if def preprocessor directive. So this is the program an example that shows the usage of hash if and def so in the program you have over here hash if and def Intel then the code suitable for Motorola PC hash else the code applicable for Intel PC and then the hash and def and finally the code that is common to both. So if we observe over here if the Intel macro has not been defined so the usage of hash if and def tells that if this Intel macro has not been defined then in that case whatever code we have that is applicable for the Motorola PC will be compiled. Otherwise if it has been defined then in that case the else part the statement of the else part will get compiled or rather the code in the hash else part will get compiled and of course the code common to both will get compiled in both the cases. So now we have a question for the students. Now the students are expected to think and write the answer to the following question. Now pause the video and write your answer. Okay. Now the answer of this question is the output of the code is not defined. Why do you see that output as not defined? It is because when we see in the program we have defined a macro named as M whose expansion is 2 but when we see in the program inside main it says if not defined M2. So here M2 is a different macro and as we know in our program M2 macro is not defined. We have defined a macro with the name M and with the expansion 2. So if and def M2 means if macro M2 is not defined which is nothing but true. So since macro M2 is not defined we get the output as not defined. Now we have one more case where hash if def is used. So let us say if we have a function named as fun which is defined in a file f file 1.h and suppose file 1.h is included in another file file 2.h with the help of hash include. So now in this case if both the files file 1 and file 2 are included using hash include in a program. So then the compiler will raise an error meaning multiple declarations for the function fun. The same file file 1.h gets included twice. So to avoid this we can make use of some other concept so that this multiple inclusion of the same file is avoided. So we can write the following code in the header file in the file 1.h we can write if and def Mac meaning if the macro Mac is not defined only then and then we will define the macro Mac with the help of hash define Mac and along with that we will define the function fun where it will have some appropriate code intended to be done. So this could be included in the header file file 1.h. So that is the advantage of making use of hash if and def. So when file 1.h is included the preprocessor checks whether the macro Mac is defined if it is undefined it gets defined and then the rest of the code is included and inclusion of this file 1.h again is prevented since Mac is already defined. Now we have the next directives as hash if and hash lf. So the first one hash if directive it is used to test whether an expression evaluates to a non-zero value or not. So depending upon the result of the expression if the value or the result is non-zero then it can be said that the subsequent lines up to the hash else hash lf or hash and f are compiled otherwise those are skipped in case if the result of the expression is 0. So using the hash lf which is nothing but another directive using this hash lf the number of hash and defs in the program can be reduced as will be shown with an example. So this is a program that we have where hash if test less than equal to 5 a line is given so then the statement 1 will be compiled hash else statement 2 meaning if the value of test is less than equal to 5 then statement 1 will be compiled otherwise statement 2 will be compiled in case the value of test is not less than equal to 5 and then we have the hash and f. So we can have the nested conditional compilation directives also. So here we have if the value of abc is equal to x then code block 1 will be compiled so that we do with the help of hash if and then we have hash else and inside that hash else we have hash if and hash else once again to check the value of abc whether is it equal to another macro y if it is so then the block of code that is code block 2 will be compiled otherwise code block 3 will be compiled. So in this case we have for wherever we have made use of hash if and hash else we need to have a corresponding hash and if so we can see in the program 2 hash and ifs are used so this can be avoided now that can be done using the hash lf so here hash if abc equal to x then code block 1 will be compiled hash lf that is similar to else if abc equal to y then the code block 2 will be compiled otherwise hash else code block 3 will be compiled and ultimately you have the hash and if so now we have the finally the hash and if directive so it may be sometimes required to cause a defined name to become undefined meaning there could be some situation where a macro which has already been defined needs to be undefined at a later point of time so for that we can make use of this hash and if directive so to undefine a macro which has already been defined using the hash define we can make use of the following directive in this way so that is hash and def space the name of the macro template so as an example we see hash and def abc or you can say when this line is encountered we can say the abc macro is undefined so all the subsequent hash if def space abc statements would evaluate to false because we know that already abc macro has been undefined so such kind of directive is rarely used but it can be used wherever required in any of the applications so that flexibility the language provides so this is the reference used for the video lecture thank you