Load the file named macro.c and display it on your screen for a better look at a macro and its use.
#define WRONG(A) A*A*A /* Wrong macro for cube */
#define CUBE(A) (A)*(A)*(A) /* Right macro for cube */
#define SQUR(A) (A)*(A) /* Right macro for square */
#define START 1
#define STOP 9
main( )
{
int i,offset;
offset = 5;
for (i = START;i <= STOP;i++) {
printf("The square of %3d is %4d, and its cube is %6d\n",
i+offset,SQUR(i+offset),CUBE(i+offset));
printf("The wrong of %3d is %6d\n",i+offset,WRONG(i+offset));
}
}
The first line defines a macro named "WRONG" that appears to get the cube of "A", and indeed it does in some cases, but it fails miserably in others. The second macro named "CUBE" actually does get the cube in all cases.
Consider the program itself where the CUBE of i+offset is calculated. If i is 1, which it is the first time through, then we will be looking for the cube of 1+5 = 6, which will result in 216. When using "CUBE", we group the values like this, (1+5)*(1+5)*(1+5) = 6*6*6 = 216. However, when we use WRONG, we group them as 1+5*1+5*1+5 = 1+5+5+5 = 16 which is a wrong answer. The parentheses are therefore required to properly group the variables together. It should be clear to you that either "CUBE" or "WRONG" would arrive at a correct answer for a single term replacement such as we did in the last program. The correct values of the cube and the square of the numbers are printed out as well as the wrong values for your inspection.
The remainder of the program is simple and will be left to your inspection and understanding.
#define WRONG(A) A*A*A /* Wrong macro for cube */
#define CUBE(A) (A)*(A)*(A) /* Right macro for cube */
#define SQUR(A) (A)*(A) /* Right macro for square */
#define START 1
#define STOP 9
main( )
{
int i,offset;
offset = 5;
for (i = START;i <= STOP;i++) {
printf("The square of %3d is %4d, and its cube is %6d\n",
i+offset,SQUR(i+offset),CUBE(i+offset));
printf("The wrong of %3d is %6d\n",i+offset,WRONG(i+offset));
}
}
The first line defines a macro named "WRONG" that appears to get the cube of "A", and indeed it does in some cases, but it fails miserably in others. The second macro named "CUBE" actually does get the cube in all cases.
Consider the program itself where the CUBE of i+offset is calculated. If i is 1, which it is the first time through, then we will be looking for the cube of 1+5 = 6, which will result in 216. When using "CUBE", we group the values like this, (1+5)*(1+5)*(1+5) = 6*6*6 = 216. However, when we use WRONG, we group them as 1+5*1+5*1+5 = 1+5+5+5 = 16 which is a wrong answer. The parentheses are therefore required to properly group the variables together. It should be clear to you that either "CUBE" or "WRONG" would arrive at a correct answer for a single term replacement such as we did in the last program. The correct values of the cube and the square of the numbers are printed out as well as the wrong values for your inspection.
The remainder of the program is simple and will be left to your inspection and understanding.