All Questions
Tagged with macros c-preprocessor
1,329
questions
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'...
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 ...
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?
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 ...
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 ...
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.
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?
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:
...
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 ...
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 ...
66
votes
6
answers
59k
views
How to undefine a define at commandline using GCC
How do I at compile time undefine a compiler macro using GCC? I tried some compile arguments to GCC, like -D, but I can't get to see the "not defined" message.
#include <iostream>
#...
65
votes
4
answers
3k
views
Why is this macro replaced as 20 instead 10?
1. #define NUM 10
2. #define FOO NUM
3. #undef NUM
4. #define NUM 20
5.
6. FOO
When I only run the preprocessor, the output file contains 20.
However, from what I understand, the preprocessor ...
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) "" ...
62
votes
5
answers
91k
views
Which Cross Platform Preprocessor Defines? (__WIN32__ or __WIN32 or WIN32 )?
I often see __WIN32, WIN32 or __WIN32__. I assume that this depends on the used preprocessor (either one from visual studio, or gcc etc).
Do I now have to check first for os and then for the used ...
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 ...
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
11
answers
31k
views
Why aren't there macros in C#?
When learning C# for the first time, I was astonished that they had no support for macros in the same capacity that exists in C/C++. I realize that the #define keyword exists in C#, but it is greatly ...
51
votes
8
answers
48k
views
How to generate a newline in a cpp macro?
How do I write a cpp macro which expands to include newlines?
49
votes
7
answers
25k
views
Variadic recursive preprocessor macros - is it possible?
I've run into a little theoretical problem. In a piece of code I'm maintaining there's a set of macros like
#define MAX_OF_2(a, b) (a) > (b) ? (a) : (b)
#define MAX_OF_3(a, b, c) MAX_OF_2(...
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 ...
48
votes
5
answers
143k
views
Error: macro names must be identifiers using #ifdef 0
I have the source code of an application written in C++ and I just want to comment something using:
#ifdef 0
...
#endif
And I get this error
error: macro names must be identifiers
Why is this ...
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
3
answers
15k
views
Can I redefine a C++ macro then define it back?
I am using both the JUCE Library and a number of Boost headers in my code. Juce defines "T" as a macro (groan), and Boost often uses "T" in it's template definitions. The result is that if you ...
44
votes
2
answers
57k
views
What's the difference between #if and #ifdef Objective-C preprocessor macro?
How to define preprocessor macros in build settings, like IPAD_BUILD, and IPHONE_BUILD (and how to use them in my factory methods)?
I'm using these by heart now, would be cool to know what is going ...
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 #...
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), ...
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)
...
38
votes
3
answers
11k
views
What is the difference between a preprocessor macro with no arguments, and one with zero arguments
Is there any reason to prefer
#define MY_MACRO() ..stuff..
to
#define MY_MACRO ..stuff..
Don't use macros is not a valid answer...
33
votes
7
answers
30k
views
Macros to create strings in C
Alternative titles (to aid search)
Convert a preprocessor token to a string
How can I make a char string from a C macro's value?
Original Question
I would like to use C #define to build literal ...
32
votes
18
answers
19k
views
The most useful user-made C-macros (in GCC, also C99)? [closed]
What C macro is in your opinion is the most useful? I have found the following one, which I use to do vector arithmetic in C:
#define v3_op_v3(x, op, y, z) {z[0]=x[0] op y[0]; \
...
32
votes
2
answers
20k
views
Expand macro inside string literal
What I'm trying to do is to #define a macro:
#define a(2)
and later use it inside a string literal: string = "a";.
I want that string to be interpreted not as string but to get the value of a, i.e. ...
32
votes
4
answers
10k
views
Why is this C or C++ macro not expanded by the preprocessor?
Can someone points me the problem in the code when compiled with gcc 4.1.0.
#define X 10
int main()
{
double a = 1e-X;
return 0;
}
I am getting error:Exponent has no digits.
When I replace X ...
32
votes
7
answers
40k
views
Is it possible for C preprocessor macros to contain preprocessor directives?
I would like to do the equivalent of the following:
#define print_max(TYPE) \
# ifdef TYPE##_MAX \
printf("%lld\n", TYPE##_MAX); \
# endif
print_max(INT);
Now the #ifdef or any nested ...
30
votes
8
answers
109k
views
Is it possible to use a if statement inside #define?
I'm trying to make a macro with the following formula: (a^2/(a+b))*b, and I want to make sure that the there will be no dividing by zero.
#define SUM_A( x, y ) if( x == 0 || y == 0) { 0 } else { ( ( ...
30
votes
1
answer
10k
views
GCC define function-like macros using -D argument
Problem
I am trying to remove __attribute__ from my C code before I send it into a parser. Is there a way to define function-like macros using the -D argument?
Solution using header file
#define ...
29
votes
5
answers
31k
views
Determine #defined string length at compile time
I have a C-program (an Apache module, i.e. the program runs often), which is going to write() a 0-terminated string over a socket, so I need to know its length.
The string is #defined as:
#define ...
29
votes
5
answers
26k
views
Is #if defined MACRO equivalent to #ifdef MACRO?
I have code that I want to have two modes, debug and verbose. I define them in my header file as,
#define verbose TRUE
#define debug TRUE
In my code so far, I have just been using
#if(debug)
//code
...
28
votes
8
answers
39k
views
Creating a string list and an enum list from a C++ macro
In order to make my code shorter and easier to change I want to replace something like
enum{ E_AAA, E_BBB, E_CCC };
static const char *strings{"AAA", "BBB", "CCC" };
With a macro, like INIT(AAA, BBB,...
27
votes
3
answers
41k
views
What exactly do C include guards do?
Let's say I have a header file "header.h" with a function definition.
#ifndef HEADER_FILE
#define HEADER_FILE
int two(void){
return 2;
}
#endif
This header file has an include guard. ...
26
votes
13
answers
14k
views
Can I determine if an argument is string literal?
Is it possible to determine if an argument passed in macro or function is a string literal at compile time or run time?
For example,
#define is_string_literal(X)
...
...
is_string_literal("...
26
votes
3
answers
13k
views
Macro evaluation order [duplicate]
Possible Duplicate:
# and ## in macros
why the output of second printf is f(1,2) what is the order in which macro is evaluated?
#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#...
24
votes
3
answers
4k
views
C++ macro '##' doesn't work after '->' operator
I have a shared_ptr object x, which has get and set methods as follows:
x->a_value();
x->set_a_value();
x->b_value();
x->set_b_value();
When i try to define a macro:
#define MAC(...
24
votes
2
answers
13k
views
Difference between #pragma and _Pragma() in C
What is the difference between #pragma and _Pragma() in C?
syntax:
#pragma arg
and
_Pragma(arg)
When should I use _Pragma(arg)?