Preprocessor
Given the following definition
#define DOUBLE(X) 2*X
- What is the value of DOUBLE(1+2)
- What is the value of 4/DOUBLE(2)
- Fix the definition of DOUBLE, and test it.
Write a macro DISP(f,x) so that
DISP(sqrt,3.0)
expands into
printf("sqrt(%g) = %g\n", 3.0, sqrt(3.0));
Test your macro.
Structures and Unions
Given the following definitions
struct point { int x, y };
struct shape {
enum { RECTANGLE, CIRCLE } shape_kind;
struct point center;
union {
struct {
int height, width;
} rectangle;
struct {
int radius;
} radius;
} u;
}
Correct any of the following statements that are illegal:
s.shape_kind = RECTANGLE;
s.center.x = 10 ;
s.height = 25;
s.u.rectangle.width = 8;
s.u.circle=5;
s.u.radius=6;
Function pointers
Write the function sum
with prototype
int sum(int (*f)(int), int start, int end)
The call sum(g,i,j)
should return g(i)+g(i+1)+...+g(j)