DMP - Chapter 16


The C preprocessor and the C library

Constants: #define

#define THREE 3
#define MSG "This is a message with\
	on two lines"
#define  NINE THREE*THREE
#define  PY printf("Y is a %d\n", y);
#define  FMT "Y is %d\n"

int main(void)
{
	int y = THREE;
	PY;
	y = NINE;
	printf(FMT, y);
	printf("%s\n", MSG);
	printf("THREE: MSG\n");
}

Macro

#define PR(X) printf("The result is %d\n", X);
#define SQUARE(x) x*x

e.g. SQUARE(5), 100 / SQUARE(2)

#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
#define ABS(X) ((X) < 0 ? -(X) : (X))

#undef

Conditional compilation

  • #ifdef, #else, and #endif, #ifndef, #elif, #if defined(MAC)

    Enumerated types

    enum notes {c, csharp, d, eflat,e, f};
    enum tempo {largo=40, moderate=100, vivace=140};
    

    The C library

    exit(), atexit()

    malloc(), calloc()


    Assignment #15 (Due 28 Jan. 1997):


    Go to Ich's Home Page
    Go to the Peabody Home Page
    For comments or questions email to: ich@peabody.jhu.edu