All Questions

Tagged with
Filter by
Sorted by
Tagged with
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'...
Andrew Tomazos's user avatar
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 ...
Jim Hunziker's user avatar
  • 14.7k
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?
user avatar
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: ...
PoP's user avatar
  • 2,115
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 ...
Arenim's user avatar
  • 4,187
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 ...
manav m-n's user avatar
  • 11.2k
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 ...
Pierre Bourdon's user avatar
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.
user avatar
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 ...
Trevor Hickey's user avatar
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?
Agnius Vasiliauskas's user avatar
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 ...
Jack Ryan's user avatar
  • 8,436
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: ...
user avatar
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 ...
user1367292's user avatar
  • 1,059
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 ...
Luiz Martins's user avatar
  • 1,644
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 ...
Randy 's user avatar
  • 831
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 ...
nneonneo's user avatar
  • 174k
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> #...
monkeyking's user avatar
  • 6,820
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 ...
OneZero's user avatar
  • 11.7k
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) "" ...
OMGtechy's user avatar
  • 8,075
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 ...
math's user avatar
  • 8,652
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 ...
Anders Lind's user avatar
  • 4,690
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 ...
Jack's user avatar
  • 16.5k
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 ...
Andrew Garrison's user avatar
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?
user18458's user avatar
  • 511
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(...
Christoffer's user avatar
  • 12.8k
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 ...
MasterM's user avatar
  • 1,183
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 ...
Eduardo's user avatar
  • 20.5k
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,...
Rob's user avatar
  • 77.5k
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 ...
Aftermathew's user avatar
  • 1,968
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 ...
Geri Borbás's user avatar
  • 16.2k
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, ...
davide's user avatar
  • 2,142
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 #...
Bing Jian's user avatar
  • 1,024
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), ...
Kushal's user avatar
  • 3,130
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) ...
gbmhunter's user avatar
  • 1,801
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...
StephQ's user avatar
  • 2,082
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 ...
Richard Stelling's user avatar
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]; \ ...
psihodelia's user avatar
  • 29.9k
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. ...
Amine's user avatar
  • 347
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 ...
Atul 's user avatar
  • 1,126
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 ...
pixelbeat's user avatar
  • 31.1k
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 { ( ( ...
Frey1337's user avatar
  • 433
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 ...
Stefan Bossbaly's user avatar
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 ...
Alexander Farber's user avatar
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 ...
msc's user avatar
  • 33.9k
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,...
Tiago's user avatar
  • 271
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. ...
Izzo's user avatar
  • 4,649
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("...
absurd's user avatar
  • 301
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 #...
Utkarsh Srivastav's user avatar
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(...
250's user avatar
  • 659
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)?
Jayesh's user avatar
  • 4,863

1
2 3 4 5
27