13,864 questions
1813
votes
6
answers
205k
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 ...
364
votes
15
answers
83k
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 ...
311
votes
2
answers
200k
views
how to use #ifdef with an OR condition?
I would like to set OR condition in #ifdef directive.?
I tried:
#ifdef LINUX | ANDROID
...
..
#endif
It did not work? What is the proper way?
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.
262
votes
11
answers
112k
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't ...
242
votes
1
answer
156k
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) ...
227
votes
32
answers
251k
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/...
218
votes
6
answers
70k
views
Saving vim macros
Does anyone know how to properly save/reuse macros recorded inside of a vim editor?
215
votes
15
answers
214k
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 ...
206
votes
10
answers
95k
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 ...
199
votes
5
answers
166k
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
138k
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 ...
162
votes
2
answers
57k
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 ...
160
votes
6
answers
96k
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?
157
votes
11
answers
85k
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?
151
votes
2
answers
66k
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 ...
149
votes
8
answers
133k
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 ...
148
votes
18
answers
150k
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.
146
votes
5
answers
255k
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')...
146
votes
3
answers
83k
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;
145
votes
17
answers
139k
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
143
votes
6
answers
190k
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 ...
140
votes
9
answers
66k
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: macro "...
137
votes
15
answers
93k
views
Inline functions vs Preprocessor macros
How does an inline function differ from a preprocessor macro?
134
votes
4
answers
139k
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 ...
133
votes
4
answers
91k
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, ...
132
votes
2
answers
79k
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 ...
129
votes
25
answers
143k
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 ...
123
votes
3
answers
50k
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 ...
118
votes
10
answers
79k
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
...
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 ...
111
votes
5
answers
42k
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 ...
108
votes
7
answers
95k
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.
107
votes
9
answers
89k
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
3
answers
33k
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
8k
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 ...
95
votes
8
answers
42k
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?
91
votes
4
answers
9k
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 ...
91
votes
2
answers
85k
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", ...
89
votes
3
answers
16k
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),*,
}
...
88
votes
9
answers
191k
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:
...
85
votes
18
answers
42k
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 ...
84
votes
3
answers
5k
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 ...
83
votes
6
answers
77k
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 ...
83
votes
6
answers
55k
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 ...
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 ...
82
votes
3
answers
27k
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, ...
81
votes
12
answers
22k
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 ...
78
votes
5
answers
110k
views
How to use a Objective-C #define from Swift
I am migrating a UIViewController class to train a bit with Swift. I am successfully using Objective-C code via the bridging header but I have the need of importing a constants file that contains #...