Search This Blog

Translate

macro example in c-what is preprocessor in c

macro example in c,macro in c programming language,macro in c programming example,what is preprocessor in c programming,preprocessor directives in c,#if preprocessor,c preprocessor commands,preprocessor functionmacro in c programming, macros in c programming, macro program in c, macro programming in c, what is macro in c programming, what is macros in c programming, define macro in c language, macro expansion in c program, macros examples in c, macro example in c, what is macro in c language with example, macros examples,c macro example,



Explain C per-processor directives its FEATURES and its types  what is MACRO EXPANSION, MACROS WITH ARGUMENT with example with source code



Explain C PER-PROCESSOR

It is a program that processes our source program before it is passed to the compiler.

 FEATURES OF PER-PROCESSOR

 The per-processor offers several features called per-processor directives. Each of these per-processor directives begin with a # symbol. The directives can be placed anywhere in a program but are most often placed at the beginning of a program, before the first function definition.

We would learn the following per-processor directives here:

(a) Macro expansion             

 (b) File inclusion  

(c) Conditional Compilation             

(d) Miscellaneous directives

MACRO EXPANSION

Have a look at the following program.
   
#define UPPER 25
            main( )
           {
           int i ;
           for ( i = 1 ; i <= UPPER ; i++ )
         printf ( "\n%d", i ) ;
          }


In this program instead of writing 25 in the for loop we are writing it in the form of UPPER, which has already been defined before main( ) through the statement,#define UPPER 25 This statement is called ‘macro definition’ or more commonly, just a ‘macro’. What purpose does it serve? During preprocessing, the preprocessor replaces every occurrence of UPPER in the program with 25.
When we compile the program, before the source code passes to the compiler it is examined by the C preprocessor for any macro definitions. When it sees the #define directive, it goes through the entire program in search of the macro templates; wherever it finds one, it replaces the macro template with the appropriate macro expansion.
In C programming it is customary to use capital letters for macro template. This makes it easy for programmers to pick out all the macro templates when reading through the program.
Note that a macro template and its macro expansion are separated by blanks or tabs. A space between # and define is optional. Remember that a macro definition is never to be terminated by a semicolon.

MACROS WITH ARGUMENT-

The macros that we have used so far are called simple macros. Macros can have arguments, just as functions can. Here is an example that illustrates this fact.

#define Area) ( 3.14 * x * x )
main( )
{
float r1 = 6.25, r2 = 2.5, a ;
a = AREA ( r1 ) ;
printf ( "\area of circle = %f", a ) ;
a = AREA ( r2 ) ;
printf ( "\area of circle = %f", a ) ;
}

Here’s the output of the program...
Area of circle = 122.656250
Area of circle = 19.625000

In this program wherever the pre-processor finds the phrase Area) it expands it into the statement( 3.14 * x * x ). However, that’s not all that it does. The x in the macro template Area) is an argument that matches the x in the macro expansion ( 3.14 * x * x ). The statement AREA(r1) in the program causes the variable r1 to be substituted for x. Thus the statement AREA(r1) is equivalent to: ( 3.14 * r1 * r1 )
After the above source code has passed through the pre-processor, what the compiler gets to work on will be this:
 

main( )
{
float r1 = 6.25, r2 = 2.5, a ;
a = 3.14 * r1 *r1 ;
printf ( "Area of circle = %f\n", a ) ;
a = 3.14 *r2 * r2 ;
printf ( "Area of circle = %f", a ) ;
}
Here is another example of macros with arguments:
#define ISDIGIT(y) ( y>= 48 && y <= 57 )
main( )
{
charch ;
printf ( "Enter any digit " ) ;
scanf ( "%c", &ch ) ;
if ( ISDIGIT ( ch ) )
printf ( "\nYou entered a digit" ) ;
else
printf ( "\nIllegal input" ) ;
}

macro in c programming, macros in c programming, macro program in c, macro programming in c, what is macro in c programming, what is macros in c programming, define macro in c language, macro expansion in c program, macros examples in c, macro example in c, what is macro in c language with example, macros examples,c macro example,



macro example in c,macro in c programming language,macro in c programming example,what is preprocessor in c programming,preprocessor directives in c,#if preprocessor,c preprocessor commands,preprocessor functionmacro in c programming, macros in c programming, macro program in c, macro programming in c, what is macro in c programming, what is macros in c programming, define macro in c language, macro expansion in c program, macros examples in c, macro example in c, what is macro in c language with example, macros examples,c macro example,

C Program example List

Google+ Badge