macro11

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Presented by:- Udit sharma C.S.E 3rd sem Roll no:0905cs081088 : 

Presented by:- Udit sharma C.S.E 3rd sem Roll no:0905cs081088 Macros in C

Contents : 

Contents

What is macro in c ? : 

What is macro in c ? A macro is a shortened version of a computer command which makes The computer Carry Out set of actions.

Slide 4: 

#include<stdio.h> #define LIMIT 25 Void main() {int I; for(i=1;i<=LIMIT; i++) printf(“\n%d” , i); } #define LIMIT 25 This statement is called the “macros defination” or more commonly just a ‘macro’.

$ During preprocessing the compiler replaces every occurrence of LIMIT in the program with 25 . $ LIMIT is called the ‘macro templates’. $ 25 is called their corresponding ‘macro expansions’ How we use macro in our programm. $ It is customary to use capital letters for macro template. $ A macro template and its macro expansion are saperated by blanks or tabs.

Slide 6: 

How program works with macros.. Source code compiler C preprocesser #define Macro templates Macro template replace by macro expansion

Some important points should be keep in mind when using macros.. : 

Some important points should be keep in mind when using macros.. * Be careful not to leave a space the macro template and its argument while defining the macro ,like this #define AREA (x) (3.14*x*x) * The entire macro expansion should be enclosed within parentheses. if you will not do it , what would happen

Slide 8: 

j = 64 / SQUARE ( 4 ) ; } The output of the above program would be: j = 64 whereas, what we expected was j = 4. In this the macro was expanded into j = 64 / 4 * 4 ; which yielded 64. #include<stdio.h> #define square(n)n*n void main() { int j;

Macros with argument… : 

Macros with argument… #include<stdio.h> #define AREA(x) ( 3.14 * x * x ) main( ) { float r1 = 6.25, r2 = 2.5, a ; a = AREA ( r1 ) ; printf ( "\nArea of circle = %f", a ) ; a = AREA ( r2 ) ; printf ( "\nArea of circle = %f", a ) ; } Here’s the output of the program... Area of circle = 122.656250 Area of circle = 19.625000

Macros versus function.. : 

Macros versus function.. the preprocessor replaces the macro template with its macro expansion, in a stupid, unthinking, literal way. But in a function call the control is passed to a function and a useful value is returned back from the function. Macro make the program run faster but increase size of program whereas function make the program smaller and compact.

Where the #define derictive is mostly used.. : 

Where the #define derictive is mostly used.. A #define directive is many a times used to define operators .. #define AND && main( ) { int f = 1, x = 4, y = 90 ; if ( ( f < 5 ) AND ( x <= 20 OR y <= 45 ) ) printf ( "\nYour PC will always work fine..." ) ; else printf ( "\nIn front of the maintenance man" ) ; }

Where the #define derictive is mostlyused.. : 

Where the #define derictive is mostlyused.. A #define directive could be used even to replace a condition.. #define AND && #define ARANGE ( a > 25 AND a < 50 ) main( ) { int a = 30 ; if ( ARANGE ) printf ( "within range" ) ; else printf ( "out of range" ) ; }

Slide 13: 

* A #define directive could be used to replace even an entire C statement. #define FOUND printf ( "The Yankee Doodle Virus" ) ; main( ) {char signature ; if ( signature == 'Y' ) FOUND else printf ( "Safe... as yet !" ) ; } Where the #define derictive is mostly used..

Advantages of macros. : 

Advantages of macros. Macros makes program faster . It makes program more easier. Easy to handle.

Disadvatages of macros. : 

Disadvatages of macros. The replacement by the c preprocesser is illogical so it is not necessary that the macro will give expected result. It increase the size of the programm .

Thank you for giving valuable time have a nice day ! : 

16 Thank you for giving valuable time have a nice day !