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
13,707
questions
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 ...
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 ...
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.
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?
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'...
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) ...
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/...
213
votes
6
answers
63k
views
Saving vim macros
Does anyone know how to properly save/reuse macros recorded inside of a vim editor?
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 ...
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 ...
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")
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 ...
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 ...
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?
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?
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 ...
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 ...
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 ...
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;
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.
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
137
votes
14
answers
91k
views
Inline functions vs Preprocessor macros
How does an inline function differ from a preprocessor macro?
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: ...
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 ...
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, ...
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 ...
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')...
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 ...
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 ...
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
...
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, ...
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 ...
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 ...
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?
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.
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 ...
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 ...
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?
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 ...
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", ...
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 ...
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:
...
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 ...
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 ...
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 ...
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 ...
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, ...
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),*,
}
...
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 ...