Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undef'ing a function-like macro

In the C/C++ there are 2 types of macro:

 #define ABC   /* usual */

und

 #define FUNC(a)  /*function-like*/

But how can I undefine them?

Update: So there is no difference between undefing "constant-like macro" and "function-like macro"?

like image 286
osgx Avatar asked Aug 17 '10 15:08

osgx


People also ask

What is a function-like macro?

Function-like macro definition: An identifier followed by a parameter list in parentheses and the replacement tokens. The parameters are imbedded in the replacement code. White space cannot separate the identifier (which is the name of the macro) and the left parenthesis of the parameter list.

How do I undef a macro?

To remove a macro definition using #undef, give only the macro identifier, not a parameter list. You can also apply the #undef directive to an identifier that has no previous definition. This ensures that the identifier is undefined. Macro replacement isn't performed within #undef statements.

Why use a macro instead of a function?

Speed versus size The main benefit of using macros is faster execution time. During preprocessing, a macro is expanded (replaced by its definition) inline each time it is used. A function definition occurs only once regardless of how many times it is called.

What is object like macro and function-like macro?

3.1 Object-like Macros An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like because it looks like a data object in code that uses it. They are most commonly used to give symbolic names to numeric constants. foo = (char *) malloc (1024);


1 Answers

#undef ABC
#undef FUNC

#undef "cancels" out a previous #define. The effect is as though you never had a previous #define for a particular identifier. Do note that #defines do not respect scope, so it's best to use them only when you need to.

Also note that it doesn't matter if one macro identifier uses the "usual" syntax while another uses a "function-like" syntax. #define ABC and #define ABC(A) both define a macro named ABC. If you have both, without #undefing one of them, the latest one "overrides" the other. (Some compilers may emit a warning if this happens.)

like image 108
In silico Avatar answered Sep 20 '22 15:09

In silico