Computer Concept & Programming In C - Unit V - 6

Q.15 What is the C preprocessor? Compare it with C compiler.
Related Questions -
Q. What do you mean by C Preprocessor? Give example of any two preprocessor directives.                                                                      (AKTU. 2010-11)
Q. Define the function of a C preprocessor.                    (AKTU. 2011 - 12)
Ans. If there is a constant appearing in several places in your program it is a good idea to associate a symbolic name to the constant and the using the symbolic name to replace the constant through out the program. There are two advantages of doing so. First your program will be more readable. Second, it is easier to maintain your program.
C has a special program called the preprocessor that allows you to define and associate symbolic names with constants. In fact, the C preprocessor uses the terminology macronames and macrobody to refer to the symbolic names and constants. The C preprocessor runs before the compiler. During the processing, the operation to replace a macro name with its associated macrobody is called macro substitution or macro expansion.
You can put a macro definition any where in your program. However, a macro name has to be defined before it can be used in your program.
In addition, the C preprocessor give you the ability to include other source files. For instance, we have been using the preprocessor directive # include to include C header files, such as stdio.h, stdlib.h and string.h. Also, the C preprocessor enables you to compile different sections of your program under specified conditions.
The C preprocessor versus C compiler: -
One important thing to remember is that the C preprocessor is not a part of the C compiler.
The C preprocessor uses a different syntax. All directives in the C preprocessor  being with a pound sign (#). In other words the pound sign (#) denotes the beginning of a preprocessor directive and it must be the first non space character on the line.
The C preprocessor is line oriented. Each macro statement ends with a new line character, not a semicolon, (only C statements ends with semicolons). One of the most common mistakes made by the programmer is to place a semicolon at the end of a macro statement. Fortunately, many C compilers can catch such errors. Some of the most frequently used directives are # define, # undef, # if, # el if, # else, # if def, # ifndef and # endif.

Q.16 Write short note on defining & calling macros.
Related Questions -
Q. How the macros are defined and called in C? Explain with example. (AKTU. 2008-09) 
Ans. Defining and calling macros: -
The following program defines & calls macros.
# include <stdio.h>
# define SQUARE (A) A*A
# define CUBE (A) A*SQUARE (A)
# define SWAP.EM (A, B, C) C = A; A = B; B = C;
printf (“\nA=%d and B=%d”, A, B);
void main ( )
{
int x = 3;
int a = 55, b = 44, c;
printf (“\n The square of %d is %d”, x, SQUARE (x));
printf (“\n The cube of %d is %d”, x, CUBE(x));
SWAP_EM (a, b, c);
return (0);
}
The output of this program is:
The square of 3 is 9
The cube of 3 is 27
A = 44 and B = 55.

Q.17 Discuss the usage of macro in C.                                  (AKTU. 2009-10)
Ans. A macro is a name given to a block of C statements as a pre-processor directive.  Being a pre-processor, the block of code is communicated to the compiler before entering into the actual coding (main () function). A macro is defined with the preprocessor directive, #define.
The advantage of using macro is the execution speed of the program fragment. When the actual code snippet is to be used, it can be substituted by the name of the macro. The same block of statements, on the other hand, need to be repeatedly hard coded as and when required.The disadvantage of the macro is the size of the program. The reason is, the pre-processor will replace all the macros in the program by its real definition prior to the compilation process of the program.     
A macro is a name given to a block of the code which can be substituted where the code snippet is to be used for more than once.
The advantages of using macro are as follows.
- The speed of the execution of the program is the major advantage of using a macro.
- It saves a lot of time that is spent by the compiler for invoking / calling the functions. 
- It reduces the length of the program.