All Questions
3,189
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 ...
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/...
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 ...
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?
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 ...
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: ...
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 ...
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 ...
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?
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 ...
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
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, ...
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 ...
65
votes
3
answers
2k
views
In a GNU C macro envSet(name), what does (void) "" name mean?
I came across this syntax today and couldn't work out what it meant:
// Uses the GNU C statement expression extension
#define envSet(name) ({ \
static int initialised; \
static bool set; \
(void) "" ...
61
votes
6
answers
42k
views
Which macro to wrap Mac OS X specific code in C/C++
While reading various C and C++ sources, I have encountered two macros __APPLE__ and __OSX__. I found plenty of use of __OSX__ in various codes, especially those originating from *BSD systems.
...
59
votes
4
answers
33k
views
What does #x inside a C macro mean?
For example I have a macro:
#define PRINT(int) printf(#int "%d\n",int)
I kinda know what is the result.
But how come #int repersent the whole thing?
I kinda forget this detail. Can anybody kindely ...
55
votes
11
answers
69k
views
Why use Macros in C? [duplicate]
Possible Duplicate:
What are C macros useful for?
Every few months I get an itch to go learn some bit of C that my crap college programming education never covered. Today it's macros. My basic ...
55
votes
7
answers
148k
views
How to use __DATE__ and __TIME__ predefined macros in as two integers, then stringify?
Want to use __ DATE __ and __ TIME __ as integer for giving automated version to my code in compile time.
#define STRINGIZER(arg) #arg
#define STR_VALUE(arg) STRINGIZER(arg)
#define ...
54
votes
2
answers
52k
views
error: pasting "." and "red" does not give a valid preprocessing token
I'm implementing The X macro, but I have a problem with a simple macro expansion. This macro (see below) is used into several macros usage examples, by including in this article.
The compiler gives ...
52
votes
1
answer
15k
views
WRITE_ONCE in linux kernel lists
I am reading the linux kernel implementation of doubled linked list. I do not understand the use of the macro WRITE_ONCE(x, val). It is defined as follow in compiler.h:
#define WRITE_ONCE(x, val) x=(...
49
votes
3
answers
12k
views
Opposite of C preprocessor "stringification"
When using C preprocessor one can stringify macro argument like this:
#define TO_STRING(x) "a string with " #x
and so when used, the result is as follows:
TO_STRING(test) will expand to: "a string ...
47
votes
8
answers
45k
views
Escaping a # symbol in a #define macro?
Without going into the gory details I want to use a #define macro that will expand to a #include but the '#' sign is confusing the preprocessor (as it thinks I want to quote an argument.)
For example,...
47
votes
5
answers
58k
views
Portable UNUSED parameter macro used on function signature for C and C++
I'm interested in creating a macro for eliminating the unused variable warning.
This question describes a way to suppress the unused parameter warning by writing a macro inside the function code:
...
46
votes
21
answers
14k
views
C/C++: How to use the do-while(0); construct without compiler warnings like C4127?
I'm often using do-while(0) constructs in my #defines, for the reasons described in this answer. Also, I'm trying to set the compiler's warning level as high as possible to catch more potential ...
46
votes
7
answers
36k
views
What makes a better constant in C, a macro or an enum?
I am confused about when to use macros or enums. Both can be used as constants, but what is the difference between them and what is the advantage of either one? Is it somehow related to compiler level ...
44
votes
3
answers
2k
views
So many parentheses in gcc standard headers
Why are constant expressions in GCC header files surrounded by parentheses, like this?
#define INTMAX_MIN (-9223372036854775807LL)
#define INTMAX_MAX (9223372036854775807LL)
What would be the ...
43
votes
2
answers
54k
views
Expand macros inside quoted string [duplicate]
Possible Duplicate:
C Macros to create strings
I have a function which accepts one argument of type char*, like f("string");
If the string argument is defined by-the-fly in the function call, ...
43
votes
9
answers
56k
views
macro definition containing #include directive
Is there a way to define a macro that contains a #include
directive in its body?
If I just put
the "#include", it gives the error
C2162: "expected macro formal parameter"
since here I am not using #...
43
votes
7
answers
14k
views
When was the NULL macro not 0?
I vaguely remember reading about this a couple of years ago, but I can't find any reference on the net.
Can you give me an example where the NULL macro didn't expand to 0?
Edit for clarity: Today it ...
43
votes
3
answers
30k
views
Can you capitalize a pasted token in a macro?
In a C macro, is it possible to capitalize a pasted-in token? For example, I currently have the following macro:
#define TEST(name, keyword) \
test_##name:
TEST_##keyword##_KEYWORD
I ...
41
votes
8
answers
43k
views
The need for parentheses in macros in C [duplicate]
I tried to play with the definition of the macro SQR in the following code:
#define SQR(x) (x*x)
int main()
{
int a, b=3;
a = SQR(b+5); // Ideally should be replaced with (3+5*5+3), ...
40
votes
3
answers
79k
views
C macro: #if check for equality
Is there a way to do check for numerical equality in macros?
I want to do something like
#define choice 3
#if choice == 3
....
#endif
#if choice == 4
...
#endif
Does C macros have support for ...
40
votes
9
answers
34k
views
Inline function v. Macro in C -- What's the Overhead (Memory/Speed)?
I searched Stack Overflow for the pros/cons of function-like macros v. inline functions.
I found the following discussion:
Pros and Cons of Different macro function / inline methods in C
...but it ...
39
votes
5
answers
93k
views
How do you implement header guards, and what can you put between them?
At LearnCpp.com | 1.10 — A first look at the preprocessor. Under Header guards, there are those code snippets:
add.h:
#include "mymath.h"
int add(int x, int y);
subtract.h:
#include "...
39
votes
3
answers
44k
views
Is there a macro definition to check the Linux kernel version?
I'm wondering if there is a gcc macro that will tell me the Linux kernel version so I can set variable types appropriately. If not, how would I go about defining my own macro that does this?
39
votes
6
answers
72k
views
Is there a way to both check a macro is defined and it equals a certain value at the same time
I regularly use object-like preprocessor macros as boolean flags in C code to turn on and off sections of code.
For example
#define DEBUG_PRINT 1
And then use it like
#if(DEBUG_PRINT == 1)
...
39
votes
4
answers
24k
views
Equivalents to MSVC's _countof in other compilers?
Are there any builtin equivalents to _countof provided by other compilers, in particular GCC and Clang? Are there any non-macro forms?
39
votes
3
answers
13k
views
When do we need #ifdef before #undef?
In many of the C and C++ files I have seen macros like this:
#ifdef X
#undef X // no statements in between
#endif
I feel that, it's adequate to simply write:
#undef X
If the macro X wasn't ...