Questions tagged [macros]

***DO NOT USE for VBA / MS-Office languages. Use [vba] instead.*** A macro is a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to an output sequence (also often a sequence of characters) according to a defined procedure.

A macro is a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to an output sequence (also often a sequence of characters) according to a defined procedure. The mapping process that instantiates (transforms) a macro into a specific output sequence is known as macro expansion.

Wikipedia Article for Macro

'Macros' created in VBA/MS-Office applications, while sharing the same name, are not the same concept as defined here. Questions relating to VBA should be tagged and the respective Office-Product, e.g. , . Please see the tag wiki entry page for more information regarding proper usage.

Similarly, questions relating to other macro programming languages should not use this tag.

13505 questions
150
votes
11 answers

Are typedef and #define the same in c?

I wonder if typedef and #define are the same in c?
user188276
143
votes
8 answers

Where are the recorded macros stored in Notepad++?

I have recorded a macro that I want to share with my work colleague. In what location are these recorded macros saved, so that I can add it to his machine? If interested, the macro is for taking a list of values and adding quotes and comma so that…
heedfull
  • 3,071
  • 3
  • 17
  • 11
142
votes
2 answers

What's the use of do while(0) when we define a macro?

Possible Duplicates: Do-While and if-else statements in C/C++ macros do { … } while (0) — what is it good for? I'm reading the linux kernel and I found many macros like this: #define INIT_LIST_HEAD(ptr) do { \ (ptr)->next = (ptr); (ptr)->prev…
amazingjxq
  • 4,487
  • 7
  • 33
  • 35
138
votes
3 answers

Constexpr vs macros

Where should I prefer using macros and where should I prefer constexpr? Aren't they basically the same? #define MAX_HEIGHT 720 vs constexpr unsigned int max_height = 720;
Tom Dorone
  • 1,575
  • 2
  • 9
  • 7
137
votes
6 answers

How to pass macro definition from "make" command line arguments (-D) to C source code?

I usually pass macro definitions from "make command line" to a "makefile" using the option : -Dname=value. The definition is accessible inside the makefile. I also pass macro definitions from the "makefile" to the "source code" using the similar…
MohamedEzz
  • 2,830
  • 3
  • 20
  • 26
137
votes
16 answers

Is there a __CLASS__ macro in C++?

Is there a __CLASS__ macro in C++ which gives the class name similar to __FUNCTION__ macro which gives the function name
mortal
  • 1,373
  • 2
  • 9
  • 6
136
votes
14 answers

Inline functions vs Preprocessor macros

How does an inline function differ from a preprocessor macro?
Subodh
  • 1,393
  • 2
  • 11
  • 8
132
votes
15 answers

Optional Parameters with C++ Macros

Is there some way of getting optional parameters with C++ Macros? Some sort of overloading would be nice too.
Cenoc
  • 11,172
  • 21
  • 58
  • 92
128
votes
4 answers

Swift: how to use PREPROCESSOR Flags (like `#if DEBUG`) to implement API keys?

In Objective-C it was sometimes useful to use static string constants to define alternate API keys (for example to differentiate between RELEASE and DEBUG keys for analytics packages, like MixPanel, Flurry or Crashlytics): #if DEBUG static NSString…
cleverbit
  • 5,514
  • 5
  • 28
  • 38
128
votes
8 answers

Comma in C/C++ macro

Say we have a macro like this #define FOO(type,name) type name Which we could use like FOO(int, int_var); But not always as simply as that: FOO(std::map, map_var); // error: macro "FOO" passed 3 arguments, but takes just 2 Of course we…
PoP
  • 2,055
  • 3
  • 16
  • 20
127
votes
4 answers

How to identify platform/compiler from preprocessor macros?

I'm writing a cross-platform code, which should compile at linux, windows, Mac OS. On windows, I must support visual studio and mingw. There are some pieces of platform-specific code, which I should place in #ifdef .. #endif environment. For…
Arenim
  • 4,097
  • 3
  • 21
  • 31
127
votes
3 answers

Creating C macro with ## and __LINE__ (token concatenation with positioning macro)

I want to create a C macro that creates a function with a name based on the line number. I thought I could do something like (the real function would have statements within the braces): #define UNIQUE static void Unique_##__LINE__(void) {} Which I…
DD.
  • 1,273
  • 2
  • 9
  • 4
122
votes
24 answers

Macro definition to determine big endian or little endian machine?

Is there a one line macro definition to determine the endianness of the machine? I am using the following code but converting it to macro would be too long: unsigned char test_endian( void ) { int test_var = 1; unsigned char *test_endian =…
manav m-n
  • 11,136
  • 23
  • 74
  • 97
118
votes
3 answers

What predefined macro can I use to detect clang?

I'm trying to detect the compiler used to compile my source code. I can easily find predefined macros to check for MSVC or GCC (see https://github.com/cpredef/predef for example), but I cannot find any macro to check for clang. Does someone know if…
Pierre Bourdon
  • 10,521
  • 4
  • 33
  • 27
118
votes
3 answers

Jinja2 inline comments

How can I put comments inside Jinja2 argument list declaration ? Everything I have tried gives an error: jinja2.exceptions.TemplateSyntaxError: unexpected char u'#' {{ Switch('var', [('1', 'foo'), # comment 1 ('2', 'bar'), ## comment…
kimstik
  • 1,611
  • 2
  • 15
  • 14