In this lab, you should decide on the correct responses before running the code to test the real result.
1. Write parameterized macros that compute the following values.
(a) The cube of x.
(b) The remainder when n is divided by 4.
(c) 1 if the product of x and y is less than 100, 0 otherwise.
(d) The number of elements in a one-dimensional array (see the discussion of the sizeof operator in Section 8.1) Write a program to test these macros. Do your macros always work? If not, describe what arguments would make them fail.
Hint: What will happen if the provided argument(s) have side effect?
2. Show what the following program will look like after preprocessing. You may ignore any lines added to the program as a result of including the < stdio.h header. What will be the output of this program?
Show the output produced by each of the following program fragments.
(a) strcpy(s, "abcde"); i=0;
putchar(TOUPPER(s[++i])); putchar(‘\n’);
(b) strcpy(s, "01234"); i=0;
putchar(TOUPPER(s[++i])); putchar(‘\n’);
Did you get the results that you expected? If not, explain why?
4. C compilers usually provide some method of specifying the value of a macro at the time a program is compiled. This ability makes it easy to change the value of a macro without editing any of the program’s files. Most compilers (including gcc) support the -D option, which allows the value of a macro to be specified on the command line, i.e., defines a macro as if by using #define. Many compilers also support the -U option, which undefines a macro as if by using #undef.
Type the following program:
#include <stdio.h
#ifdef DEBUG
#define PRINT_DEBUG(n) printf("Value of " #n ": %d\n", n)