Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)

Similar presentations


Presentation on theme: " 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)"— Presentation transcript:

1  2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)

2  2000 Prentice Hall, Inc. All rights reserved. 12.1 Introduction Preprocessing –Occurs before a program is compiled Why Use? a)Inclusion of other files b)Definition of symbolic constants and macros c)Conditional compilation of program code d)Conditional execution of preprocessor directives Format of preprocessor directives –Lines begin with #

3  2000 Prentice Hall, Inc. All rights reserved. 12.2 The #include Preprocessor Directive #include –Copy of a specified file included in place of the directive –#include Searches standard library for file Use for standard library files –#include "filename" Searches current directory, then standard library Use for user-defined files –Used for: Programs with multiple source files to be compiled together Header file – has common declarations and definitions (structures, function prototypes)

4  2000 Prentice Hall, Inc. All rights reserved. 12.3 The #define Preprocessor Directive: Symbolic Constants #define –Preprocessor directive used to create symbolic constants and macros –Symbolic constants When program compiled, all occurrences of symbolic constant replaced with replacement text –Format #define identifier replacement-text –Example: #define PI 3.14159 –Everything to right of identifier replaces text #define PI = 3.14159 Replaces “ PI ” with " = 3.14159 " –Cannot redefine symbolic constants once they have been created

5  2000 Prentice Hall, Inc. All rights reserved. Example

6  2000 Prentice Hall, Inc. All rights reserved. Example k=2 * A + B 27

7  2000 Prentice Hall, Inc. All rights reserved. 12.4 The #define Preprocessor Directive: Macros Macro –Operation defined in #define –A macro with arguments has its arguments substituted for replacement text, when the macro is expanded –Performs a text substitution – no data type checking –The macro #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) would cause area = CIRCLE_AREA( 4 ); to become area = ( 3.14159 * ( 4 ) * ( 4 ) );

8  2000 Prentice Hall, Inc. All rights reserved. Example

9  2000 Prentice Hall, Inc. All rights reserved. 12.4 The #define Preprocessor Directive: Macros Use parenthesis –Without them the macro #define CIRCLE_AREA( x ) PI * x * x would cause area = CIRCLE_AREA( c + 2 ); to become area = 3.14159 * c + 2 * c + 2;

10  2000 Prentice Hall, Inc. All rights reserved. Example area=22.56 Result is not correct!

11  2000 Prentice Hall, Inc. All rights reserved. 12.4 The #define Preprocessor Directive: Macros Multiple arguments #define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) ) would cause rectArea = RECTANGLE_AREA( a + 4, b + 7 ); to become rectArea = ( ( a + 4 ) * ( b + 7 ) );

12  2000 Prentice Hall, Inc. All rights reserved. Example 1 : Correct Way rectArea=60 Result is correct!

13  2000 Prentice Hall, Inc. All rights reserved. Example 1: Incorrect Way rectArea=21 Result is incorrect! ( x * y )

14  2000 Prentice Hall, Inc. All rights reserved. Example 2

15  2000 Prentice Hall, Inc. All rights reserved. 12.4 The #define Preprocessor Directive: Macros #undef –Undefines a symbolic constant or macro –If a symbolic constant or macro has been undefined it can later be redefined

16  2000 Prentice Hall, Inc. All rights reserved. Example

17  2000 Prentice Hall, Inc. All rights reserved. 12.5 Conditional Compilation Conditional compilation –Control preprocessor directives and compilation –Structure similar to if #if !defined( NULL ) #define NULL 0 #endif Determines if symbolic constant NULL has been defined –If NULL is defined, defined( NULL ) evaluates to 1 –If NULL is not defined, this function defines NULL to be 0 –Every #if must end with #endif –#ifdef short for #if defined( name ) –#ifndef short for #if !defined( name )

18  2000 Prentice Hall, Inc. All rights reserved. Example (!defined) #include #define TAX 18 int main(){ printf("Tax Value=%d\n",TAX); #if !defined(VAT) #define VAT 11 #endif #if !defined(TAX) #define TAX 11 #endif printf("VAT Value=%d\n",VAT); printf("New Tax Value=%d\n",TAX); return(0); }

19  2000 Prentice Hall, Inc. All rights reserved. Example (ifndef) #include #define TAX 18 int main(){ printf("Tax Value=%d\n",TAX); #ifndef VAT #define VAT 11 #endif #ifndef TAX #define TAX 11 #endif printf("VAT Value=%d\n",VAT); printf("New Tax Value=%d\n",TAX); return(0); }

20  2000 Prentice Hall, Inc. All rights reserved. 12.5 Conditional Compilation Debugging #define DEBUG #ifdef DEBUG //some code here #endif –Defining DEBUG enables code –After code corrected, remove #define statement –Debugging statements are now ignored

21  2000 Prentice Hall, Inc. All rights reserved. Example #include #define DEBUG int main(){ int x,y; printf("Enter x value:"); scanf("%d",&x); #ifdef DEBUG printf("x=%d\n",x); #endif printf("Enter y value:"); scanf("%d",&y); #ifdef DEBUG printf("y=%d\n",y); #endif printf("The sum is:%d\n",x+y); return(0); }

22  2000 Prentice Hall, Inc. All rights reserved. 12.6 The # Operator # –Causes a replacement text token to be converted to a string surrounded by quotes –The statement #define HELLO( x ) printf( “Hello, ” #x “\n” ); would cause HELLO( John ) to become printf( “Hello, ” “John” “\n” ); –Strings separated by whitespace are concatenated when using printf

23  2000 Prentice Hall, Inc. All rights reserved. Example #include #define HELLO( x ) printf( "Hello " #x "\n" ); int main(){ HELLO(John); return(0); }

24  2000 Prentice Hall, Inc. All rights reserved. 12.7 Predefined Symbolic Constants Five predefined symbolic constants –Cannot be used in #define or #undef

25  2000 Prentice Hall, Inc. All rights reserved. Example #include int main(){ printf("Line Number=%d\n",__LINE__); printf("File Name=%s\n",__FILE__); printf("Date=%s\n",__DATE__); printf("Time=%s\n",__TIME__); return(0); }

26  2000 Prentice Hall, Inc. All rights reserved. General Example #include #define pi 3.14 #define alan(r) pi*r*r #define yaz(x) printf("sonuc=%f\n",x) int main(){ int R; float a; printf("yaricapi gir: "); scanf("%d",&R); a=alan(R); yaz(a); return 0; }


Download ppt " 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)"

Similar presentations


Ads by Google