+ All Categories
Home > Documents > 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)

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

Date post: 26-Dec-2015
Category:
Upload: may-singleton
View: 214 times
Download: 0 times
Share this document with a friend
Popular Tags:
26
2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
Transcript
Page 1: 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)

2000 Prentice Hall, Inc. All rights reserved.

Chapter 12 - The Preprocessor Directives (Macros)

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

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 #

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

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 <filename>

• 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)

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

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

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

2000 Prentice Hall, Inc. All rights reserved.

Example

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

2000 Prentice Hall, Inc. All rights reserved.

Example

k=2 * A + B

2 7

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

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 becomearea = ( 3.14159 * ( 4 ) * ( 4 ) );

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

2000 Prentice Hall, Inc. All rights reserved.

Example

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

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 causearea = CIRCLE_AREA( c + 2 );

to becomearea = 3.14159 * c + 2 * c + 2;

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

2000 Prentice Hall, Inc. All rights reserved.

Example

area=22.56Result is not correct!

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

2000 Prentice Hall, Inc. All rights reserved.

12.4 The #define Preprocessor Directive: Macros

• Multiple arguments#define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) )

would causerectArea = RECTANGLE_AREA( a + 4, b + 7 );

to becomerectArea = ( ( a + 4 ) * ( b + 7 ) );

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

2000 Prentice Hall, Inc. All rights reserved.

Example 1 : Correct Way

rectArea=60Result is correct!

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

2000 Prentice Hall, Inc. All rights reserved.

Example 1: Incorrect Way

rectArea=21Result is incorrect!

( x * y )

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

2000 Prentice Hall, Inc. All rights reserved.

Example 2

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

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

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

2000 Prentice Hall, Inc. All rights reserved.

Example

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

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 )

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

2000 Prentice Hall, Inc. All rights reserved.

Example (!defined)#include <stdio.h>#define TAX 18int 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);}

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

2000 Prentice Hall, Inc. All rights reserved.

Example (ifndef)#include <stdio.h>#define TAX 18int 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);}

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

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

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

2000 Prentice Hall, Inc. All rights reserved.

Example#include <stdio.h>

#define DEBUGint 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);}

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

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 causeHELLO( John )

to becomeprintf( “Hello, ” “John” “\n” );

– Strings separated by whitespace are concatenated when using printf

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

2000 Prentice Hall, Inc. All rights reserved.

Example

#include <stdio.h>

#define HELLO( x ) printf( "Hello " #x "\n" );

int main(){

HELLO(John); return(0);}

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

2000 Prentice Hall, Inc. All rights reserved.

12.7 Predefined Symbolic Constants

• Five predefined symbolic constants– Cannot be used in #define or #undef

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

2000 Prentice Hall, Inc. All rights reserved.

Example

#include <stdio.h>int main(){ printf("Line Number=%d\n",__LINE__); printf("Line Number=%d\n",__LINE__);

printf("File Name=%s\n",__FILE__); printf("Date=%s\n",__DATE__); printf("Time=%s\n",__TIME__); return(0);}

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

2000 Prentice Hall, Inc. All rights reserved.

General Example

#include <stdio.h>#include <stdlib.h>

#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;}


Recommended