Ad Code

Responsive Advertisement

pre processor.

Explain Preprocessor Directives:- 
The C preprocessor is exactly what its name implies. It is a program that processes our
source program before it is passed to the complier. This is a capability that doesn’t exist in much other higher level language.

The preprocessor offers several features called preprocessor directives.
Each of these preprocessor directives begins with # symbol. The directives can be placed anywhere in the
program but most often placed at the beginning of program. There are four types of
preprocessor directives as follow.

Macro expansion
File inclusion
Conditional compilation
Miscellaneous directives

1). Macro expansion directive 
Macro expansion directive declare as follow 

#define PI 3.1415
The above statement is called macro definition or macro. During preprocessing the
processor replaces every occurrence of PI in program with 3.1415. It is also called macro
templates whereas 3.1415 are called macro expansions. We are declared macro two types
as follow.
Simple macros
Macro with arguments

Important rules for writing macro with argument 
-> Be careful not to leave a blank space between macro templates and its argument while defining the macro.

-> Entire macro expression should be enclosed within parentheses.
As we know, even a function can be written to calculate the area of the circle. Though
macro calls are ‘like’ functions calls, they are not really the same thing.

2).File inclusion directive

-> This preprocessor directive causes one file to be included in another. The preprocessor
command for file inclusion looks like this:
#include “filename”

-> It simply easy causes the entire contents of filename to be inserted into the source code
at that point in the program. Of course this presumes that the file being included is existing.

When and Why this feature is used? 

It can be used in two cases as follow.
-> If we have a very large program, the code is best divided into several different files, each
containing a set of related functions. It is a good practice to keep different sections of a
large program separate. These files are #included at the beginning of main program file.

-> Many times we need some function or some macro definitions almost in all programs that
we write. In such cases these commonly needed functions and macro definitions can be
stored in a file, and that file can be included in every program

Actually there exist two ways to write #include statement. These are as follow.

#include “filename”
#include<filename>
The meaning of each of these forms is given below:

#include “goto.c” :   This command would look for the file goto.c in the current
directory as well as the specified list of directories as mentioned in the include
search path that might have been set up.

#include<goto.c> :  This command would look for the file goto.c in the specified
 list of directories only.

3).Conditional Compilation directives
We can, if we want, have the compiler skip over part of a source code by inserting the
preprocessing commands #ifdef and #endif, which have the general form:

#ifdef macroname
 statement 1;
 statement 2;
 statement 3;
#endif

Example
#include<stdio.h>
#define NUM 10
void main()
{
    // Define another macro if MACRO NUM     defined
  clrscr();
  #ifdef NUM
        #define MAX 20
  #endif
  printf("MAX number is : %d",MAX);
  getch();
}

If macroname has been #defined, the block will be processed as usual; otherwise not.
Where would have been #ifdef be used? When would you like to compile only a part of your
program? In two cases:

-> To “comment out “obsolete line of code. It often happens that a program us changed at
the last minute to satisfy a client. This involves rewriting some part of source code to the
client’s satisfaction and deletes the old code. But veteran programmers are familiar with the
client who changes his mind and wants the old code back again just the way it was. Now you
would definitely not like to retype the deleted code again.

-> One solution in such a situation is to put the old code within a pair of /* */combination.
But we might have already written a comment in the code that we are about to “comment
out”. This would mean we end up with nested comments. Obviously, this solution won’t
work since we can’t nest comments in C.

-> A more sophisticated use of #ifdef has to do with making the programs portable. i.e. to
make them work on two totally different computers. Suppose an organization has two
different types of computers and you are expected to write a program that works on both
the machines. You can do so by isolating the lines of code that must be different for each
machine by marking then off with #ifdef.

#if and #elif directives
The #if directive can be used to test weather an expression evaluates to a nonzero value
or not. If the result of the expression is nonzero, then subsequent lines up to a #else, #elif
and #endif are compiled.
#include<stdio.h>
void main()
{
     #ifdef MAX
         #define MIN 90
    #else
    #define MIN 100
    #endif
   printf("MIN number : %d",MIN);
}

4).Miscellaneous Directives
There are two more preprocessor directives available, though they are not very commonly
used. They are:
#undef
#pragma
#undef directive
-> On some occasions it may be desirable to cause a defined name to become ‘undefined’.
This can be accomplished by means of the #undef directive. In order to undefined a macro
which has been earlier #defined, the directive.
#include<stdio.h>
#define TEMP 10
#ifdef TEMP
 #undef TEMP

#define TEMP 75
#else
 #define TEMP 100
#endif
int main()
{
 printf("%d",TEMP);
return 0;
}
#pragma directive
This directive is another special purpose directive that you can use to turn on or off
certain features. Pragma is very from one compiler to another. There are certain pragma
available with Microsoft C compiler which deals with formatting source listing and placing
comments in the object file generated by the compiler. Turbo C compiler has got a pragma
which allows you to write assembly language statements in your C program.

Post a Comment

0 Comments