MAP

GTU MCA MATERIAL | C LANGUAGE | Preprocessor

Preprocessor:
vIt is a program that processes source program before it is passed to the complier.
vPreprocessor commands often known as directives.
vPreprocessor directives begin with a # symbol.
vThe directives can be placed anywhere in a program but generally it is beginning of a
     Program before main () or particular function.
vThese directives can be divided into 3 categories.
    1) Mecro substitution directive
    2) File inclusion directive
    3) Complier control directive
1) Macro substitution directive
   vMacro substitution is a process where an identifier in a program is replaced by a    
         predefined string composed of one or more token.
  vExample:
         #define a 25
Main ()
           {
                        Int i;
                        For (i=1;i<=a;i++)
                        {
                                    Printf(“%d”,i);
                        }
                        getch();
            }
   vThis # define a 25 statement is called “macro definition” or just a
       “macro”.
   va is often called “macro templates” and 5 is their “macro 
       expansion”.                             
  vWhen we compile the program it is check by the preprocessor for
       any macro definition before the source code passes to the    
        complier.
   vWe can use capital letter fot macro template this makes it easy for programmer to pick
       out all the macro template when reading through the program.
   vMacro template and its macro expansion are sepatated by blanks or tabs.
   vRemember that a macro definition is never to be terminated by a semicolon.
   vIt is not necessary that you can declare macro before the main function you can declare  
        anywhere in the program.
For Example:
main()
{
            #define pf printf
            pf(“Jay Swaminarayan”);
            getch();             
}

v #define directive is many a times used to define operators.
    #define AND &&
    #define    OR    ||

  2) File inclusion directive

v An external file containing functions or macro definitions can be included as a part of a program so that we need not rewrite those functions or macro definitions. This is achieved by the preprocessor directive.
For example:  #include “filename”

Where filename is the name of the file containing the required definitions or functions. At this point, the preprocessor inserts the entire contents of filename into the source code of the program. When the filename is included within the double quotation marks, the search for the file is made first in the current directory and then in the standard directories.
For example:-
                                                #include<filename>
Without double quotation marks. In this case, the file is searched only in the standard directories.
        
            Nesting of included files is allowed. That is, an include file can included file can include other files. However, a file cannot include itself.
             If an included file is not found, an error is reported and compilation is terminated.
             We can make use of a definition of function contained in any of these files by including them in the program as shown below:
                                                #include<stdio.h>
                                                #include<conio.h>

3) COMPILER CONTORL DIRECTIVES: -


1)      You have included a file containing some macro definitions. It is not known whether a particular macro (say, test) has been defined in that header file. However, you want to be certain that test is define (or not defined).
2)      Suppose a customer has two different type of computer and you are required to write a program that will run on both the system.
                                    One solution to these problems is to develop different programs to suit the needs of different situations. Another method is develop a single. Comprehensive program that includes all optional codes and then directs the compiler to skip over certain parts of source code when they are not required. Fortunately, the c preprocessor offers a feature known as conditional compilation. Which can be used to ‘switch’ on or off a particular line or group of lines in a program.

Situation 1

                     This situation refers to the conditional definition of a macro. We want ensure that the macro TEST is always defined. irrespective of whether it has been defined in the header file or not. This can be achieved as follows:
                                    #include           “DEFINE.H”
                                    #ifndef                       TEST
                                    #define                        TEST   1
                                    #endif
                                    ….
                                    ….
                     DEFINE.H is the header file that is supposed to contain the definition of TEST macro. The directive.
                                    #ifndef            TEST
                     Searches for the definition of TEST in the header file and if not defined, then all the lines between the #ifndef and the corresponding #endif directive are left ‘active’ in the program.
That is, the preprocessor directive
                                    #define     TEST
is processed.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More