#define preprocessor


The #define preprocessor allows us to define symbolic names, constants, and macros. A quick example

	#define PI 3.14159

This statement will translate every occurance of PI in the program to 3.14159. Here is a more complete example:

	#define PI 3.14159
	main()
	{
   	   int r=10;
	   float cir;
	   cir = PI * (r*r);
	}

This feature is partically usefull for defining 'magic' numbers.

Always use () around any expression to avoid errors of order of operation when the define is expanded. For example:

#define THREE 1 + 2
#define FOUR (2 + 2)
	main() {
		x = THREE * 2; //x is now 1 + 2 * 2 = 5, not 6
		y = FOUR * 2; //y is now (2 + 2) * 2 = 8
		}
	

Use #undef to remove a definition.


See Also:


Examples:

example program.

Conflict between enum and #define.


Top Master Index Keywords Functions

See also:


Martin Leslie