Posts tagged C Preprocessor
The C preprocessor blindly expands the macro with whatever they are defined with. Let us examine the following example.
#define CHARPTR char* CHARPTR p1,p2;
The C preprocessor expands the above code to the following.
But we would like p2 to be a character pointer but not a character variable.
So macro are not generally used for variable type definitions as they are not suitable for all sorts of usages. Instead typedef is used as shown below.
typedef char* CHARPTR; CHARPTR p1,p2;
With the above code, the variables p1 and p2 will effectively become character pointers.
The following example shows how a macro MyPrintf can be defined which is mapped to printf function call.
#define MyPrintf(fmt,args...) printf(fmt,args)
It is required to mask all debug messages for production builds. This can be achieved by using a macro that takes variable number of arguments. In such cases, the following can be done to enable or disable such messages at compile time using HAVE_DEBUG switch. One advantage of this method is that it will leave no print on the target binary when HAVE_DEBUG is not defined. In other words, if the HAVE_DEBUG compile-time switch is not defined, no strings passed to MyPrintf function will More >