C/C++: #define

#define is precompile directive for compiler. Basically, #define has three functionalities, text substitutions, macro and conditional compiler directives.

1. Define Text Substitutions

#define MAXNUMBER 1024

#define BUFFER 2048

#define TRUE 1

#define FALSE 0

2. Define Macro

#define mem_clear(name) memset( name, ‘ ‘, sizeof(name))

#define max(a, b) ( (a) > (b) ? (a) : (b) )

However, sometimes, define macro do have some drawbacks. If “a” is “++i” in above definition, the “i” will be increment two times. Macro doesn’t have memory address. Macro doesn’t have recursive algorithm. Thus, Macro is hard to debug.

3. Conditional Precompile Directive

This feature has more advantage. we can put #ifdef, #ifndef and #endif in anywhere in the body of program. It will cooperate with configuration file to realize the certain functions.

#ifdef FAST_FUNC
#ifndef BUFFERED_FUNC
rslt = func((char **) &srcbuf_ptr,
&inbytes_left,
(char **) &trgtbuf_ptr,
&outbytes_left);
#endif

Discussion Area - Leave a Comment




Spam Protection by WP-SpamFree Plugin