3. Macros. Red Hat ENTERPRISE LINUX 4 - USING BINUTILS, ENTERPRISE LINUX 4 - DEVELOPER TOOLS GUIDE
Werbung
Werbung
Chapter 3.
Macros
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. There are two kinds of macros. They differ mostly in what they look like when they are used.
Object-like macros resemble data objects when used, function-like macros resemble function calls.
You may define any valid identifier as a macro, even if it is a C keyword. The preprocessor does not know anything about keywords. This can be useful if you wish to hide a keyword such as const from an older compiler that does not understand it. However, the preprocessor operator defined
(Section 4.2.3
Defined ) can never be defined as a macro, and C++’s named operators (Section 3.7.4
C++ Named Operators ) cannot be macros when you are compiling C++.
3.1. Object-like Macros
An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like because it looks like a data object in code that uses it. They are most commonly used to give symbolic names to numeric constants.
You create macros with the #define directive.
#define is followed by the name of the macro and then the token sequence it should be an abbreviation for, which is variously referred to as the macro’s body , expansion or replacement list . For example,
#define BUFFER_SIZE 1024 defines a macro named BUFFER_SIZE as an abbreviation for the token 1024 . If somewhere after this
#define directive there comes a C statement of the form foo = (char *) malloc (BUFFER_SIZE); then the C preprocessor will recognize and expand the macro BUFFER_SIZE . The C compiler will see the same tokens as it would if you had written foo = (char *) malloc (1024);
By convention, macro names are written in uppercase. Programs are easier to read when it is possible to tell at a glance which names are macros.
The macro’s body ends at the end of the #define line. You may continue the definition onto multiple lines, if necessary, using backslash-newline. When the macro is expanded, however, it will all come out on one line. For example,
#define NUMBERS 1, \
2, \
3 int x[] = { NUMBERS };
==> int x[] = { 1, 2, 3 };
Herunterladen
Werbung