C Programming

PREPROCESSOR STATEMENTS
The define statement is used to make programs more readable, and allow the inclusion of macros. Consider the following examples,


	#define TRUE    1    /* Do not use a semi-colon , # must be first character on line */
	#define FALSE   0
	#define NULL    0
	#define AND     &
	#define OR      |
	#define EQUALS  ==

			game_over = TRUE;
			while( list_pointer != NULL )
				................


Macros

Macros are inline code which are substituted at compile time. The definition of a macro, which accepts an argument when referenced,


	#define  SQUARE(x)  (x)*(x)

	y = SQUARE(v);


In this case, v is equated with x in the macro definition of square, so the variable y is assigned the square of v. The brackets in the macro definition of square are necessary for correct evaluation. The expansion of the macro becomes


	y = (v) * (v);


Naturally, macro definitions can also contain other macro definitions,


	#define IS_LOWERCASE(x)  (( (x)>='a') && ( (x) <='z') )
	#define TO_UPPERCASE(x)  (IS_LOWERCASE (x)?(x)-'a'+'A':(x))

	while(*string) {
		*string = TO_UPPERCASE(*string);
		++string;
	}


ęCopyright B Brown. 1984-1999. All rights reserved.