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.

macros
Filter by
Sorted by
Tagged with
1809 votes
5 answers
203k views

What is ':-!!' in C?

I bumped into this strange macro code in /usr/include/linux/kernel.h: /* Force a compilation error if condition is true, but also produce a result (of value 0 and type size_t), so the expression ...
chmurli's user avatar
  • 15.1k
360 votes
15 answers
78k views

What makes Lisp macros so special?

Reading Paul Graham's essays on programming languages one would think that Lisp macros are the only way to go. As a busy developer, working on other platforms, I have not had the privilege of using ...
minty's user avatar
  • 22.3k
306 votes
6 answers
24k views

Strange definitions of TRUE and FALSE macros

I have seen the following macro definitions in a coding book. #define TRUE '/'/'/' #define FALSE '-'-'-' There was no explanation there. Please explain to me how these will work as TRUE and FALSE.
Keshava GN's user avatar
  • 4,215
302 votes
2 answers
181k views

how to use #ifdef with an OR condition?

Sorry for asking very basic question. I would like to set OR condition in #ifdef directive.? How to do that ? I tried #ifdef LINUX | ANDROID ... .. #endif It did not work? What is the proper way?
Whoami's user avatar
  • 14.1k
249 votes
10 answers
100k views

Overloading Macro on Number of Arguments

I have two macros FOO2 and FOO3: #define FOO2(x,y) ... #define FOO3(x,y,z) ... I want to define a new macro FOO as follows: #define FOO(x,y) FOO2(x,y) #define FOO(x,y,z) FOO3(x,y,z) But this doesn'...
Andrew Tomazos's user avatar
231 votes
1 answer
144k views

C multi-line macro: do/while(0) vs scope block [duplicate]

Possible Duplicates: What’s the use of do while(0) when we define a macro? Why are there sometimes meaningless do/while and if/else statements in C/C++ macros? do { … } while (0) ...
krasnaya's user avatar
  • 3,055
219 votes
31 answers
222k views

__FILE__ macro shows full path

The standard predefined macro __FILE__ available in C shows the full path to the file. Is there any way to shorten the path and get just the filename? I mean instead of /full/path/to/file.c I see to/...
mahmood's user avatar
  • 23.9k
213 votes
6 answers
63k views

Saving vim macros

Does anyone know how to properly save/reuse macros recorded inside of a vim editor?
jnadro52's user avatar
  • 3,574
205 votes
10 answers
93k views

Can I record/play macros in Visual Studio 2012/2013/2015/2017/2019?

Apparently macros were dropped from Visual Studio 2012. Is there a plugin/extension/tool that will let me record & play keyboard macros (much like the record/play temporary macro in ...
laktak's user avatar
  • 58.5k
198 votes
15 answers
185k views

How do I show the value of a #define at compile-time?

I am trying to figure out what version of Boost my code thinks it's using. I want to do something like this: #error BOOST_VERSION but the preprocessor does not expand BOOST_VERSION. I know I could ...
Jim Hunziker's user avatar
  • 14.7k
195 votes
5 answers
157k views

What does "#pragma comment" mean?

What does #pragma comment mean in the following? #pragma comment(lib, "kernel32") #pragma comment(lib, "user32")
user198729's user avatar
  • 62.7k
181 votes
1 answer
9k views

Getting a structural type with an anonymous class's methods from a macro

Suppose we want to write a macro that defines an anonymous class with some type members or methods, and then creates an instance of that class that's statically typed as a structural type with those ...
Travis Brown's user avatar
176 votes
70 answers
132k views

What is the worst real-world macros/pre-processor abuse you've ever come across?

What is the worst real-world macros/pre-processor abuse you've ever come across (please no contrived IOCCC answers *haha*)? Please add a short snippet or story if it is really entertaining. The goal ...
161 votes
2 answers
54k views

Is #pragma once part of the C++11 standard?

Traditionally, the standard and portable way to avoid multiple header inclusions in C++ was/is to use the #ifndef - #define - #endifpre-compiler directives scheme also called macro-guard scheme (see ...
101010's user avatar
  • 42.3k
155 votes
6 answers
89k views

What does the Q_OBJECT macro do? Why do all Qt objects need this macro?

I just started using Qt and noticed that all the example class definitions have the macro Q_OBJECT as the first line. What is the purpose of this preprocessor macro?
Trevor Boyd Smith's user avatar
152 votes
11 answers
80k views

Are typedef and #define the same in C?

I wonder if typedef and #define are the same in C. What are the differences between them?
user avatar
145 votes
8 answers
127k views

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 ...
heedfull's user avatar
  • 3,121
144 votes
2 answers
62k views

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

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 ...
amazingjxq's user avatar
  • 4,577
142 votes
6 answers
175k views

How can I pass a 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 ...
MohamedEzz's user avatar
  • 2,880
141 votes
3 answers
74k views

When should I prefer constexpr variables over 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's user avatar
  • 1,605
140 votes
16 answers
138k views

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's user avatar
  • 11.5k
139 votes
16 answers
130k views

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's user avatar
  • 1,393
137 votes
14 answers
91k views

Inline functions vs Preprocessor macros

How does an inline function differ from a preprocessor macro?
Subodh's user avatar
  • 1,403
131 votes
8 answers
59k views

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<int, int>, map_var); // error: ...
PoP's user avatar
  • 2,115
129 votes
4 answers
129k views

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 ...
Arenim's user avatar
  • 4,187
128 votes
4 answers
87k views

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, ...
Richard Das's user avatar
  • 5,544
128 votes
2 answers
74k views

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 ...
DD.'s user avatar
  • 1,283
127 votes
4 answers
195k views

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')...
kimstik's user avatar
  • 1,729
123 votes
24 answers
134k views

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 ...
manav m-n's user avatar
  • 11.2k
121 votes
3 answers
45k views

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 ...
Pierre Bourdon's user avatar
117 votes
10 answers
78k views

How can I convert from degrees to radians?

I am trying to convert this Obj-C code to Swift code but I don't know what the equivalent of this code should be ? #define DEGREES_TO_RADIANS(degrees)((M_PI * degrees)/180) I googled and found this ...
Dharmesh Kheni's user avatar
115 votes
9 answers
49k views

Is there a macro recorder for Eclipse? [closed]

Is there a good Eclipse plugin for recording and playing back macros? I've tried this one, but it didn't do me any good- it seemed like it wasn't ready for prime time. I know about editor templates, ...
Tim Howland's user avatar
  • 7,949
111 votes
13 answers
84k views

iOS How to detect iPhone X, iPhone 6 plus, iPhone 6, iPhone 5, iPhone 4 by macro?

How to detect device model by macro? i had using something like this but the result on the simulator alway IS_IPHONE_5 #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define ...
phuongho's user avatar
  • 1,147
109 votes
5 answers
37k views

replay a vim macro until end of buffer

I want to run a macro I just recorded in register "x" on every single line of an open buffer, from my cursor to end of the buffer, in vim. How do I do that? I know I can replay the macro n ...
Kevin's user avatar
  • 1,240
106 votes
9 answers
83k views

How can I define a string literal on the GCC command line?

On the GCC command line, I want to define a string such as -Dname=Mary. Then in the source code, I want printf("%s", name); to print Mary. How could I do it?
richard's user avatar
  • 1,617
101 votes
7 answers
81k views

How to view C preprocessor output?

How do I view the output produced by the C pre-processor, prior to its conversion into an object file? I want to see what the MACRO definitions do to my code.
user avatar
99 votes
3 answers
29k views

Vim Macro on Every Line of Visual Selection

I'd like to run a macro on every line in a selection, rather than totalling up the number of lines in my head. For instance, I might write a macro to transform: Last, First Into First Last and I'd ...
Stefan Mai's user avatar
  • 23.7k
97 votes
7 answers
7k views

Why only define a macro if it's not already defined?

All across our C code base, I see every macro defined the following way: #ifndef BEEPTRIM_PITCH_RATE_DEGPS #define BEEPTRIM_PITCH_RATE_DEGPS 0.2f #endif #ifndef ...
Trevor Hickey's user avatar
92 votes
8 answers
36k views

Real-world use of X-Macros

I just learned of X-Macros. What real-world uses of X-Macros have you seen? When are they the right tool for the job?
Agnius Vasiliauskas's user avatar
89 votes
5 answers
8k views

What's the significance of a C function declaration in parentheses apparently forever calling itself?

In gatomic.c of glib there are several function declarations that look like this: gboolean (g_atomic_int_compare_and_exchange_full) (gint *atomic, gint ...
Andreas's user avatar
  • 9,549
86 votes
1 answer
74k views

What is PRIu64 in C?

I am new to C and I am confronted with: #include <stdio.h> #include <inttypes.h> int main(void) { uint64_t foo = 10; printf("foo is equal to %" PRIu64 "!\n", ...
torr's user avatar
  • 1,286
83 votes
18 answers
41k views

What are C macros useful for?

I have written a little bit of C, and I can read it well enough to get a general idea of what it is doing, but every time I have encountered a macro it has thrown me completely. I end up having to ...
Jack Ryan's user avatar
  • 8,436
82 votes
9 answers
178k views

C# Macro definitions in Preprocessor

Is C# able to define macros as is done in the C programming language with pre-processor statements? I would like to simplify regular typing of certain repeating statements such as the following: ...
user avatar
82 votes
2 answers
7k views

What's the point of a PROTOTYPE macro that merely expands to its arguments?

I have a header file which contains #define PROTOTYPE(s) s What is the point of that? Seems like it would just replace the input with itself. There are TONS of other directives around it, but the ...
Charlie Elverson's user avatar
81 votes
6 answers
68k views

Can we have recursive macros?

I want to know if we can have recursive macros in C/C++? If yes, please provide a sample example. Second thing: why am I not able to execute the below code? What is the mistake I am doing? Is it ...
user1367292's user avatar
  • 1,059
81 votes
3 answers
3k views

Understanding the behavior of C's preprocessor when a macro indirectly expands itself

While I was working on a big project full of macro tricks and wizardry, I stumbled upon a bug in which a macro was not expanding properly. The resulting output was "EXPAND(0)", but EXPAND ...
Luiz Martins's user avatar
  • 1,644
80 votes
5 answers
48k views

Any utility to test expand C/C++ #define macros?

It seems I often spend way too much time trying to get a #define macro to do exactly what i want. I'll post my current dilemma below and any help is appreciated. But really the bigger question is ...
Randy 's user avatar
  • 831
80 votes
3 answers
25k views

What are the implications of the linux __user macro?

I was hoping someone could explain the nuances of the __user macro used in the linux kernel source. First of all, the macro: # define __user __attribute__((noderef, address_space(1))) Now, ...
Mr. Shickadance's user avatar
79 votes
3 answers
12k views

How to allow optional trailing commas in macros?

Here's a synthetic example of what I want: macro_rules! define_enum { ($Name:ident { $($Variant:ident),* }) => { pub enum $Name { None, $($Variant),*, } ...
user1244932's user avatar
  • 7,762
78 votes
10 answers
21k views

Array-size macro that rejects pointers

The standard array-size macro that is often taught is #define ARRAYSIZE(arr) (sizeof(arr) / sizeof(arr[0])) or some equivalent formation. However, this kind of thing silently succeeds when a pointer ...
nneonneo's user avatar
  • 174k

1
2 3 4 5
275