meta data for this page

Using #define preprocessor macros

If some constant number is used widely among your code it might be easier to define it with #define preprocessor macro. E.g.

#define FULL_HOUSE 8

Then it would be easy to access e.g. the fields of Yahtzee table by:

score_sheet->values[FULL_HOUSE] = 25;

without remembering the actual integer value.

The preprocessor macros are also useful when including headers, e.g:

/* If __HEADER_FILE_H_ (e.g. name of this header in capital letters) is not defined
   use the next lines until #else or #elif or #endif
#ifndef __HEADER_FILE_H_
/* Define the header name , include stdio.h and declare function */
#define __HEADER_FILE_H_
#include <stdio.h>
void funcion(int input);

as the block inside #ifndef and #endif is included while compiling only if the first #ifndef is true, i.e. the file is included for the first time in the compilation process.

CT30A5002 - Games and Networking