Skip to main content
Filter by
Sorted by
Tagged with
1813 votes
6 answers
205k views

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.2k
364 votes
15 answers
83k views

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.4k
311 votes
2 answers
200k views

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?
Whoami's user avatar
  • 14.5k
306 votes
6 answers
24k views

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,285
262 votes
11 answers
112k views

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 ...
Andrew Tomazos's user avatar
242 votes
1 answer
156k views

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,165
227 votes
32 answers
251k views

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
  • 25.2k
218 votes
6 answers
70k views

Does anyone know how to properly save/reuse macros recorded inside of a vim editor?
jnadro52's user avatar
  • 3,644
215 votes
15 answers
214k views

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
  • 15.6k
206 votes
10 answers
95k views

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
  • 60.6k
199 votes
5 answers
166k views

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

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
138k views

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

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 ...
Dimitrios Bouzas's user avatar
160 votes
6 answers
96k views

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
157 votes
11 answers
85k views

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

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,747
149 votes
8 answers
133k views

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,201
148 votes
18 answers
150k views

Is there some way of getting optional parameters with C++ Macros? Some sort of overloading would be nice too.
Cenoc's user avatar
  • 11.8k
146 votes
5 answers
255k views

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,959
146 votes
3 answers
83k views

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,653
145 votes
17 answers
139k views

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,453
143 votes
6 answers
190k views

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,930
140 votes
9 answers
66k views

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 "...
PoP's user avatar
  • 2,215
137 votes
15 answers
93k views

How does an inline function differ from a preprocessor macro?
Subodh's user avatar
  • 1,413
134 votes
4 answers
139k views

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,277
133 votes
4 answers
91k views

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 D's user avatar
  • 5,770
132 votes
2 answers
79k views

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,323
129 votes
25 answers
143k views

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.4k
123 votes
3 answers
50k views

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
118 votes
10 answers
79k views

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
111 votes
13 answers
84k views

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
111 votes
5 answers
42k views

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,260
108 votes
7 answers
95k views

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
107 votes
9 answers
89k views

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,637
101 votes
3 answers
33k views

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
  • 24.1k
97 votes
7 answers
8k views

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
95 votes
8 answers
42k views

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
91 votes
4 answers
9k views

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
  • 10.5k
91 votes
2 answers
85k views

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,336
89 votes
3 answers
16k views

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
  • 8,282
88 votes
9 answers
191k views

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
85 votes
18 answers
42k views

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,462
84 votes
3 answers
5k views

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,745
83 votes
6 answers
77k views

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,077
83 votes
6 answers
55k views

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
  • 871
82 votes
2 answers
7k views

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
82 votes
3 answers
27k views

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
81 votes
12 answers
22k views

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
  • 181k
78 votes
5 answers
110k views

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 #...
atxe's user avatar
  • 5,069

1
2 3 4 5
278