Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change case of argument provided to C preprocessor macro

I'm fairly new to the C preprocessor. Is it possible to change the case of an argument provided to a function-like #define? For example, I want to write test(size) and then involve "Size" in the resulting replacement.

like image 750
andyvn22 Avatar asked May 06 '10 00:05

andyvn22


People also ask

Can we change macro value in C?

You can't. Macros are expanded by the Preprocessor, which happens even before the code is compiled. It is a purely textual replacement. If you need to change something at runtime, just replace your macro with a real function call.

Can we pass arguments in macro definition?

Function-like macros can take arguments, just like true functions. To define a macro that uses arguments, you insert parameters between the pair of parentheses in the macro definition that make the macro function-like. The parameters must be valid C identifiers, separated by commas and optionally whitespace.

What does ## do in a macro?

The double-number-sign or token-pasting operator (##), which is sometimes called the merging or combining operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token, and therefore, can't be the first or last token in the macro definition.

What to use instead of #define in C?

If you accidentally redefine a name with a #define , the compiler silently changes the meaning of your program. With const or enum you get an error message.


1 Answers

No. The preprocessor can combine identifiers to form new ones, but it cannot modify an identifier.

like image 51
James McNellis Avatar answered Oct 08 '22 12:10

James McNellis