Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Preprocessor Seema Chandak.

Similar presentations


Presentation on theme: "C Preprocessor Seema Chandak."— Presentation transcript:

1 C Preprocessor Seema Chandak

2 What is PREPROCESSING ? A separate compilation step before the compiler tries to “understand” your program What is PREPROCESSOR Statement Simple text substitution “On the fly”

3 Features of C preprocessor
Preprocessing Occurs before program compiled Inclusion of external files Definition of symbolic constants Macros Conditional compilation Conditional execution All directives begin with # Can only have whitespace before directives Directives not C statements Do not end with ;

4 The #include Preprocessor Directive
#include directive Puts copy of file in place of directive Seen many times in example code Two forms #include <filename> For standard library header files Searches predestinated directories #include "filename" Searches in current directory Normally used for programmer-defined files

5 The #include Preprocessor Directive
Usage Loading header files #include <iostream> Programs with multiple source files Header file Has common declarations and definitions Classes, structures, enumerations, function prototypes Extract commonality of multiple program files

6 #define The #define Preprocessor Directive: Symbolic Constants
Constants represented as symbols When program compiled, all occurrences replaced Format #define identifier replacement-text #define PI Everything to right of identifier replaces text #define PI= Replaces PI with "= " Probably an error Cannot redefine symbolic constants

7 The #define Preprocessor Directive: Symbolic Constants
Advantages Takes no memory Disadvantages Name not be seen by debugger (only replacement text) Do not have specific data type const variables preferred

8 Macro The #define Preprocessor Directive: Macros
Operation specified in #define Intended for legacy C programs Macro without arguments Treated like a symbolic constant Macro with arguments Arguments substituted for replacement text Macro expanded Performs a text substitution No data type checking

9 Example The #define Preprocessor Directive: Macros
Declaration of macro #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) Calling macro area = CIRCLE_AREA( 4 ); Becomes area = ( * ( 4 ) * ( 4 ) ); Use parentheses very important Without them #define CIRCLE_AREA( x ) PI * x * x Call will area = CIRCLE_AREA( c + 2 ); area = * c + 2 * c + 2; Which evaluates incorrectly

10 Example # define M(x) x*x main() { printf(“%d”,M(2+3)); } What is the output ? Why ?

11 ((x+1) > (y-2) ? (x+1) : (y-2))
Macro Substitution with Arguments #define NAME(A, B) repl_text with A & B Defines a macro in which text of argument A is inserted into repl_text wherever parameter A occurs text of argument B is inserted into repl_text wherever parameter B occurs Example:– #define max(A, B) ((A) > (B) ? (A) : (B)) Replace max(x+1, y-2) with ((x+1) > (y-2) ? (x+1) : (y-2))

12 Multiple arguments : #undef The #define Preprocessor Directive: Macros
#define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) ) rectArea = RECTANGLE_AREA( a + 4, b + 7 ); Becomes rectArea = ( ( a + 4 ) * ( b + 7 ) ); #undef Undefined symbolic constant or macro Can later be redefined

13 Control preprocessor directives and compilation
Conditional Compilation Control preprocessor directives and compilation Cannot evaluate cast expressions, sizeof, enumeration constants Structure similar to if #if !defined( NULL ) #define NULL 0 #endif Determines if symbolic constant NULL defined If NULL defined, defined( NULL ) evaluates to 1 #define statement skipped Otherwise #define statement used Every #if ends with #endif

14 Can use else Abbreviations Conditional Compilation #else
#elif is "else if" Abbreviations #ifdef short for #if defined(name) #ifndef short for #if !defined(name)

15 Examples :: #include<stdio.h> #define a 10 main() { }
int a = 50; printf(“\n\t %d”,a); } #incldue <stdio.h> #define square(X) X*X main() { int y ; y= 30/square(4); printf(“\n\t %d “, y); }


Download ppt "C Preprocessor Seema Chandak."

Similar presentations


Ads by Google