Macro
A macro is a name or identifier that represents a (usually larger) combination of other names, identifiers, or symbols. Typically these represented identifiers will be a list of commands or program code. Macros have two similar but subtly different interpretations, and which one is used depends chiefly on the program interpreting it.
The simplest kind of macro is what might be called a "substitution macro." This type of macro is found in programming languages like C and C++, which support a "pre-processor" stage immediately before the compiler proper takes over to turn the computer program into a form the computer can understand. In these languages a macro is defined like this:
- #define MY_MACRO printf("this is my macro!");
This line tells the pre-processor to replace every occurrence of the text "MY_MACRO" it finds in the code with the expression printf("this is my macro!"). This means that if the macro is used like this
- if(printing == TRUE) {
- MY_MACRO
- }
the compiler will see the code
- if(printing == TRUE) {
- printf("this is my macro!");
- }
after the pre-processor has finished. It is also possible to create macros that behave like functions. The macro
- #define MY_MACRO(a) printf("this is my %s!," a)
when invoked like this
- if(printing == TRUE) {
- MY_MACRO("macro")
- }
will generate the code
- if(printing == TRUE) {
- printf("this is my macro %s!," "macro");
- }
The C and C++ pre-processor is a complex and powerful utility, and it has many more features than are discussed above. Nevertheless, using macros is generally frowned upon and used as a last resort by programmers; for not only do macros make debugging code harder (it is hard to tell what is going on without constantly referring back to the macro definition), but macros that take parameters are not type-checked by the compiler even though their substituted code is. A result of this is that messages reporting errors on the line of code containing the macro use the name of the function the macro calls rather than the name of the macro. This can be very confusing.
The second type of macro is a "function macro." This type is more like a sequence of commands (which can be run by using a single name or keystroke) than a simple fragment of text to be substituted. In many ways they are akin to simple programs themselves, and, indeed, so-called "macros" in some complex software have developed into full-blown programming languages (for example Microsoft's "VBA" or "Visual Basic for Applications").
Today, any self-respecting word processor or spreadsheet will have a macro facility in it. Suppose, for example, that a writer is using a word processor and wants to indent every other line by eight spaces. In a word-processor that supports macros it would be possible to tell it to carry out the following commands and to run them by pressing a unique combination of keys or by typing a single command:
- 1. move the cursor to start of the first line
- 2. move the cursor down by two lines
- 3. insert eight spaces
- 4. repeat from step 2
Most word processors will allow the user to write these actions like a programmer, or even record them by doing the actions while in a special mode.
Macros can also be used to insert words and phrases that are used often, like names and addresses or salutations for letters. In this case the behavior of the macro (replacing a section of text with something else) behaves very much like the C and C++ macros do.
This is the complete article, containing 555 words
(approx. 2 pages at 300 words per page).