meta data for this page
  •  

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

courses:ct30a5002:preprocessormacros [2013/09/19 14:39]
julaakko created
courses:ct30a5002:preprocessormacros [2013/09/23 18:50] (current)
julaakko
Line 1: Line 1:
 +===== 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.
 +<code c>
 +#define FULL_HOUSE 8
 +#define SMALL_STRAIGHT 9
 +...
 +</​code>​
 +
 +Then it would be easy to access e.g. the fields of Yahtzee table by:
 +<code c>
 +score_sheet->​values[FULL_HOUSE] = 25;
 +</​code>​
 +without remembering the actual integer value.
 +
 +The preprocessor macros are also useful when including headers, e.g:
 +
 +<code c>
 +/* 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);
 +#endif
 +</​code>​
 +
 +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.
 +
 +
 +----
 +[[courses:​ct30a5002:​start#​code_snippets|CT30A5002 - Games and Networking]]