View
222
Download
0
Category
Preview:
Citation preview
Macros
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.
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 (#).
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.
MacrosLine that begin with # are called
preprocessing directives. Major kinds of preprocessor directives:• File inclusion• Macro definition• Conditional compilation
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.
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.
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)
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 }
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.
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”);• }
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
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.
Recommended