September 1998
Intermediate to advanced
848 pages
20h 13m
English
By using arguments, you can create macros that look and act much like functions. A macro with arguments looks very similar to a function because the arguments are enclosed within parentheses. Listing 16.2 provides some examples that illustrate how such a macro function is defined and used. Some of the examples also point out possible pitfalls, so read them carefully.
/* mac_arg.c -- macros with arguments */ #include <stdio.h> #define SQUARE(X) X*X #define PR(X) printf("The result is %d.\n", X) int main(void) { int x = 4; int z; z = SQUARE(x); PR(z); z = SQUARE(2); PR(z); PR(SQUARE(x+2)); PR(100/SQUARE(2)); printf("x is %d\n.", x); PR(SQUARE(++x)); printf("After incrementing, ... |
Read now
Unlock full access