Monday, 1 October 2012

preprocessor


The preprocessor is a program that process the code before it basses through the computer preprocessor directivities are placed t in the source program before the main line

These directivities can be divided into three categories

1.      Macros substitution directivities
2.      File inclusion directivities
3.   Compiler control directivities.

Macro Substitution Directives : Macro substitution is a process where an identifier in a program is replaced by a predefined string composed of one or more identifiers. It  accomplishes this task under the direction of ‘#define’ statement. The general form is

                                    # define identifier string

            There are different forms of macro substitutions. The most common forms are

1.      simple macro substitution.
2.      Argumented macro substitution.
3.      Nested macro substitution.

Simple Macro Substitution : simple string replacement is commanly used to define constants.

Ex :                  # define   COUNT   100
                        # define   CAPITAL  “DELHI”

            We have written all macros(identifiers) in capitals. It is a conversion to write all macros in capitals to identify them as symbolic constants.

                                    # define    M    5

            It will replace all occurance of ‘M’ with ‘5’, starting from the line of identifier to
the end of the program.

Ex :     Total  =  M * value ;
            Printf(“M = %d\n”, M);

            These two lines would be changed during pre-processing as follows.

                        Total   =  5 * value ;
                        Printf (“M=%d\n” , 5);
We can use a macro to define almost anything

Ex :                 #define TEST if(x<y)
                        #define AND
                        #define PRINT printf(“very good”);

To build a statement as follows

                        TEST AND PRINT

The preprocessor would translate this line to

                        If(x<y) printf(“very good”);

Macro With Arguments : The preprocessor permits to define more useful form of define replacements. It takes the form

                        # define Identifier(f1,f2,…fn) string

            Here there is no space between the macro identifier and the left paranthesis. The identifiers f1,f2,…..fn are the formal macro arguments that are analogus to the formal arguments in a function definition.

            A macro with arguments is known as a macro call. When a macro is called, the preprocessor substitutes the string replacing the formal parameters with the actual parameters. A simple example of a macro with argument is

                        # define CUBE(X)  (x*x*x)

If the following statement appears in the program
           
                        Volume = CUBE(side);

Then the preprocessor would expand this statement as

                        Volume = side * side * side;

Nesting Of Macros : We can also use one macro in the definition of another macro. i.e.., Macro definitions may be nested.

            Consider the following macro definitions.

                        # define SQUARE(x)  (x*x)
                        # define CUBE(x)       (SQUARE(x)*x)
                        # define SIXTH(x)      (CUBE(x)*CUBE(x))

            The preprocessor expands each #define macro until no more macros appear in the test. For example the last definition is first expands into
           
            (SQUARE(x)*x) * (SQUARE(x) * x)

since ‘SQUARE(x)’ is still a macro, it is further expanded into ( ( x * x ) * x ) *   
( (x*x)*x), which is finally evaluted as x6.

            Macro calls can be nested in much the same fashion as function calls.

Ex :                 #define HALF  ( (x)/2.0 )
                        #deifne Y          HALF( HALF(x) )

Undefining A Macro : A defined macro can be undefined using statement

                                    # undef  identifier

            This is useful when we want to restrict the definition only to a particular part of the program.

File Inclusion :An external file containing functions or macro definitions can be included as a part of a program. This is achieved by a preprocessor directive.

                                    #include “file name”

            Where ‘file name’ is the name of the file containing the required definitions or functions. At this point, the preprocessor inserts the entire contents of the file name into the source code of the program.

            When the file name is included within the “  ” ,then the search for the file is made first in the current directory and then in the standard directories.

            Alternatively this directive takes the form
           
                        #include <file name>
           
Without “ ” in this case this file can include other files. However a file can’t include itself. We can make use of a definition or function contained in anyone of these files by including them in the program as shown below.

                        #include<stdio.h>
                        #include“shiva.c”
                        #define    M      5
                        main( )
                        {
                                   

                        }
Compiler Control Directives : While developing large programs, the ‘C’ pre-processor offers a feature known as conditional compilation, which can be used to ‘switch’ on or off a particular line or a group of lines in a program.

No comments:

Post a Comment