macros. there are three basic phases for c programming. preprocessing, compiling, and linking. c...

13
Macros

Upload: peregrine-osborne

Post on 30-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

Macros

Page 2: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

Macros• There are three basic phases for C

programming . • preprocessing, compiling, and linking. • C input file is first passed to a preprocessing

program called the preprocessor.• The output of the preprocessor is passed

to the C compiler. • The output of the C compiler is then fed

into a linker program. • The linker finally produces an executable

form of your program.

Page 3: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

Macros• C Preprocessor is just a text

substitution tool and they instruct compiler to do required pre-processing before actual compilation.• Preprocessing is done before actual

compilation begins.• All preprocessor commands begin with a

pound symbol (#).

Page 4: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

Macros• There are essentially three uses of the

preprocessor--directives, constants, and macros. • Directives are commands that tell the

preprocessor to skip part of a file, include another file, or define a constant or macro. • All other uses of the preprocessor involve

processing #define'd constants or macros. • Typically, constants and macros are written

in ALL CAPS to indicate they are special.

Page 5: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

MacrosLine that begin with # are called

preprocessing directives. Major kinds of preprocessor directives:• File inclusion• Macro definition• Conditional compilation

Page 6: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

Macros• File inclusion• Used to include a file• Syntax # include<filename>• # include “filename”The difference between <> and “”¨ looks firstly in the header path for the header

file ¨ whereas "" looks in the current directory of the

file for the header.

Page 7: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

Macros• Macro Definition• A macro is a fragment of code which has been

given a name. • Whenever the name is used, it is replaced by

the contents of the macro. • The #define directive specifies a macro

identifier and a replacement list, and terminates with a new-line character. • The #undef directive is used to cancel a

definition for a macro.

Page 8: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

Macros• Macro Definition• syntax• #define identifier replacement-list • Eg• #include <stdio.h>• #define MACRO 25 • Void main() • { • printf("\nMACRO Defined with value %d", MACRO); • }• Output• MACRO Defined with value 25• (MACRO will be replaced to 25)

Page 9: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

Macros• Macro Definition• Examples of simple macros:• #define N 100• #define PI 3.14159• #define WARNING "Warning: nonstandard feature"• #define BEGIN {• #define END }• ---------------• void main()• BEGIN // replace {• int a[N];// replace int a[100]• printf(“\nvalue of Pie %f”,PI);// print 3.14159• printf(“%s”,WARNING);• END // replace }

Page 10: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

Macros• Parameterized Macro• Function-like macros can take arguments, just like

functions. • Insert parameters between the pair of parentheses

in the macro definition. • To invoke a macro that takes arguments, • ie the name of the macro followed by a list of actual

arguments in parentheses, separated by commas. • The number of arguments give must match the

number of parameters in the macro definition. • When the macro is expanded, each use of a

parameter in its body is replaced by the corresponding argument.

Page 11: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

Macros• Eg• #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))

• printf(“minimum %d”,MIN(a, b)); ==> x = ((a) < (b) ? (a) : (b));

• # define EVEN(N) (N%2==0)

• if(EVEN(2)==1)• {• printf(“even”);• }

Page 12: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

Macros• Conditional Compilation• The conditional directives are:–#if - Test if a compile time condition is true–#else - The alternative for #if–#ifdef - If this macro is defined–#ifndef - If this macro is not defined–#elif - #else an #if in one statement–#endif - End preprocessor conditional

Page 13: Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program

Macros• Conditional Compilation• #if defined(CREDIT)• credit(); • #elif defined(DEBIT) • debit(); • #else printerror(); • #endif• The function call to credit is compiled if the identifier CREDIT is

defined. • If the identifier DEBIT is defined, the function call to debit is

compiled. • If neither identifier is defined, the call to printerror is compiled. • Note thatCREDIT and credit are distinct identifiers in C and C++

because their cases are different.