Questions tagged [c-preprocessor]

A textual macro processor applied before compiling C and C++ language programs. This tag can also be used for questions about other compilers/languages derived from or that have identical features, such as the #directives in Objective-C or C#.

Filter by
Sorted by
Tagged with
0 votes
1 answer
17 views

Comparing enum / define in keil ARM compiler

I'm facing difficulties to make the following conditional compilation working: enum { MIDI_USB_DEV_MCU_IDX, MIDI_WC_BLE_IDX, MIDI_CONN_1_IDX, MIDI_CONN_2_IDX,MIDI_IN_OUT_UARTS_NB }; #define ...
Jerome's user avatar
  • 611
0 votes
1 answer
74 views

Macro-like function with macros inside the body

I'm trying to make macro-like function that will "create" few lines of new macros. ARM-GCC preprocessor is not happy with that macro-like function. Any ideas how to achieve same ...
Pararera's user avatar
  • 372
3 votes
1 answer
101 views

How to include a double quote in a C macro defintion?

I have the following test program: #define q " int main() { printf(q hello world q); } Compiling this with gcc (I've tried both versions 12.3 and 11.4 on Ubuntu) produces this error: error: ...
Andrew Merrill's user avatar
0 votes
1 answer
85 views

How to configure a header based on the sources that include it in bazel?

I want to have a target that has such defines attribute that changes based on the current package I'm in. Something along the lines: A.BUILD: cc_library( name = "A", hdrs = ["...
cppbest's user avatar
  • 79
0 votes
4 answers
197 views

Use macros to implement a for statement with its own loop counter in C++

My requirement is to count the total number of times each for statement in the program loops during the entire runtime of the program, for example: int for_counter_1 = 0; int for_counter_2 = 0; void ...
Jemtaly's user avatar
  • 63
0 votes
0 answers
63 views

Mapping between line:column numbers in C or C++ code before and after preprocessing

When there's a syntax error in a C or C++ source file, both GCC and Clang report the exact line and column of where the error is. For example, if we try to compile this code fragment: #include <...
Bass's user avatar
  • 5,014
1 vote
3 answers
114 views

Generic function pointer inside struct with compile time validation

I have header file String.h typedef struct String String; typedef String* String_t; ... struct String { //... other methods void (*push_str)(String_t self, void *str); ??? } void ...
u23r's user avatar
  • 11
2 votes
0 answers
67 views

How does application of the token concatenation operator ## interact with the prohibition against recursive macro expansion?

Rules of the standard and question The standard says the following about the rescanning phase (after #/##-processing and parameter substitution) of macro expansion (C17 draft, 6.10.3.4 ¶2): If the ...
Lover of Structure's user avatar
0 votes
1 answer
30 views

Making CMake use -dD -dI switches when generating preprocessed C files via Makefile?

Consider the following CMake example: cmake_minimum_required(VERSION 3.13) if (NOT(EXISTS "${CMAKE_SOURCE_DIR}/main.c" AND NOT IS_DIRECTORY "${CMAKE_SOURCE_DIR}/main.c")) ...
sdbbs's user avatar
  • 4,478
6 votes
1 answer
207 views

Example illustrating the unspecified relative evaluation order of # and ## in the C preprocessor

Some commentary about the accepted answer is at the bottom of this question post. Problem statement According to the C standard (C17 draft, 6.10.3.2 ¶2): The order of evaluation of [the] # and ## ...
Lover of Structure's user avatar
-1 votes
3 answers
146 views

C macro to bitwise-OR together a variable number of arguments (lightweight solution without P99)

I need a macro that bitwise-ORs together its (variable number of) arguments: int main() { printf("%d\n", BITWISE_OR_MULTIPLE_ARGS(1, 2, 4)); printf("%d\n", ...
swineone's user avatar
  • 2,384
-4 votes
2 answers
174 views

Is there an equivalent to awk sprintf for use in C compiler #define directives

For context, I am trying to do some simplistic localization, as a first step into that "ocean", with limited language options. I want to be more "inclusive" than just limited to ...
Eric Marceau's user avatar
  • 1,615
0 votes
1 answer
61 views

How to have a macro expand into the arg name of another macro

for example: #define Y (b * 2) #define X(b) Y int main() { printf("%d", X(4)); // Want it to print 8, but doesn't work } Is something like this possible with C macros?
Abdulmalek Almkainzi's user avatar
-1 votes
1 answer
102 views

C Macro Preprocessor to include a number in string

I would like to insert arbitrary control codes x into a string but use decimal numbers rather than hex (\ux) or oct (\0x). To use a pointless but immediately understandable example, I would be happy ...
Kay's user avatar
  • 131
-1 votes
1 answer
31 views

#if and #elif doesn't work as intended in Cpp

I am trying to make some sort of easy way to define mcu that I use, and by defining mcu, different header files should be included. I tried doing it with #if and #elif directives : #include "...
Dominykas's user avatar
  • 119
2 votes
1 answer
61 views

How to stringize by concatenating two tokens using token-pasting in c using macros?

Want to concatenate two tokens and convert the result to string using macros and token-pasting and stringizing operators only. #include <stdio.h> #define concat_(s1, s2) s1##s2 #define concat(...
thirdeye's user avatar
  • 170
-9 votes
1 answer
83 views

something went wrong when I tried to apply #define in a function [closed]

I tried to apply #define in a conditional statement, however, it seems that something went wrong. // The mode is forward by default #define FOR if ( mode == "forward") { clog << &...
Zahi's user avatar
  • 3
4 votes
2 answers
198 views

Is #define X defined(Y) a valid C/C++ macro definition?

I read somewhere that #define X defined(Y) was invalid, but it seems to work. Here is an example: #define WIN_PLAT defined(_WIN32) #if WIN_PLAT # undef WIN_PLAT # define WIN_PLAT 1 #else # ...
Lucas Orlandi's user avatar
0 votes
2 answers
93 views

Are parentheses needed when a define sets something to an integer?

In C, it's known that #define macros should make use of parentheses. See e.g. C macros and use of arguments in parentheses. However, in embedded code, I frequently see this used even with integers. ...
SRobertJames's user avatar
  • 8,561
0 votes
2 answers
110 views

stdint for C++98 (gcc/clang)

Why do GCC/clang complain about including cstdint but not about stdint.h when compiling for C++98? Compiling with -std=c++98 -x c++ -Wall -Wextra -pedantic outputs for #include <cstdint>: This ...
user5534993's user avatar
4 votes
3 answers
138 views

How to escape characters in include directives?

Let's say I have a header file called a"b.h or a>b.h, how do I escape the " or > character in an include directive? // this does not work #include "a\"b.h"
eyelash's user avatar
  • 3,277
0 votes
1 answer
69 views

Insert length of #defined string literal at first entry of literal at compile time

I'm writing a C-Program with ESP-ZigbeeSDK where I need to set an default string, like #define mystring. But ZigBee requires that the first char of the literal indicates the length of the string #...
Tropaion's user avatar
0 votes
3 answers
100 views

How can you set data type of #define to long double?

I have a c-code where I define some variables for pre-processing using the #define command. However, I think that there are ways to toggle between float and double data types by doing for example: #...
Abhinav Kumar's user avatar
1 vote
0 answers
94 views

Is it possible to write a macro that takes two pairs of parentheses and transform it to a single macro call? [closed]

I wonder if it’s possible to write a macro that works as follows: A(a, b, c)(1, 2) Where each set of parentheses actually takes an arbitrary number of arguments. I’d like to pass each argument list ...
Michael Jung's user avatar
4 votes
2 answers
143 views

C++ variadic macro: split argurments at a special separator argument

I have a variadic macro M that takes a number of parameters separated by a special argument SEPARATOR: M(a, b, c, SEPARATOR, 1, 2, 3); I’d like to have two macros BEFORE and AFTER that I can call ...
Michael Jung's user avatar
8 votes
3 answers
387 views

How can I use "nullptr" for null pointers prior to C23?

In C23, the nullptr keyword got standardized. I would prefer to use nullptr instead of NULL prior to C23 too because it means that I could write code which compiles in: C, prior to C23 C, since C23 C+...
Jan Schultke's user avatar
  • 22.4k
1 vote
3 answers
76 views

How can I put the numeric value of an enum constant into a string in C?

I need a way to put the numeric value of an enum constant into a string constant as a single byte (at compile time). enum { SPECIAL_CHAR = 230 }; const char *str = "hello" ...
anton-tchekov's user avatar
1 vote
2 answers
106 views

List of token separators in the C pre-processor

#define foo 123 foo _foo ->foo &foo Running gcc -E on the above file outputs this: 123 _foo ->123 &123 Which shows _ is not a token separator and _foo is a whole word. Where can I ...
Dan's user avatar
  • 2,734
0 votes
0 answers
27 views

C token pasting operator [duplicate]

Is there any way to use a prior defined macro in the concatenation using the ## operator? I would like: #define PREFIX MR_ #define MAKE_TITLE(NAME) PREFIX ## NAME MAKE_TITLE(SMITH) MR_SMITH Rather ...
Kay's user avatar
  • 131
7 votes
2 answers
386 views

Do preprocessor defines substitute in `operator""_name`

Consider the following example provided by Aykhan Hagverdili: #include <string> using std::operator""s; #define s foobar auto s = "hello world"s; Some compilers would ...
Alex Guteniev's user avatar
0 votes
0 answers
56 views

Is there a way to determine the value of the `gcc -std=` flag inside the source code? [duplicate]

Does gcc define a macro or something else, when I use the -std=c++zz flag? Imagine, that I have a (nice and easy and fast and...) solution in my program which only works with g++ -std=c++20. However I ...
Tom Solid's user avatar
  • 2,226
0 votes
3 answers
63 views

How to stop duplicate function definitions in C that are created by macro expansion

for example, let's say I have a macro that expands into a struct definition with corresponding functions like so: array.h #ifndef ARRAY_H #define ARRAY_H #include <assert.h> #define Array(t, ...
Abdulmalek Almkainzi's user avatar
4 votes
1 answer
46 views

how do I implement a Variadic noop in C with evaluation

I want to use a noop macro in C (similar to msvc's __noop) that still evaluates its arguments. example: #ifdef _DEBUG #define printf_debug(...) printf(__VA_ARGS__) #else #define printf_debug(...) NOOP(...
Badasahog's user avatar
  • 589
2 votes
1 answer
106 views

How to use a text macro in processor's include-directive

I need to constantly change the #include header file name (there are a lot of them), so I need something similar to this: #define NUMBER 23 #include "file${NUMBER}.h" int main() { ...
BrokenBootLoader's user avatar
-1 votes
2 answers
69 views

Is there a way to substring a C macro agrument and call different functions based on its name?

for example: #define PRINT(var) do { \ print_##var[2..] (var); \ } while(0) void print_foo(int foo) { // some code } void print_bar(int bar) { // some code } int main() { ...
Abdulmalek Almkainzi's user avatar
0 votes
0 answers
26 views

gcc: C preprocessor, stringify the result of a macro [duplicate]

I try to print "Linux is number linux" in C language with C preprocessor, but it always print "Linux is number 1". It only happened with GCC, not tinycc. Expected: Linux is number ...
Mike Chen's user avatar
0 votes
2 answers
135 views

String literal and macro concatenation in preprocessor

FINAL UPDATE: After reading many related questions and answers it seems to me the answer is simple, albeit disappointing: It is impossible to concatenate string literals in preprocessor. Adding macro ...
Maple's user avatar
  • 239
10 votes
1 answer
488 views

How does GCC know the error location (in the source code), when its preprocessor has removed comments?

I'm wondering how does GCC know where the error is (in the source code), when its preprocessor has removed comments? I googled it, but I couldn't find it. I'll explain what I mean: I have C code like ...
Wolf's user avatar
  • 111
1 vote
1 answer
103 views

How to check if macro is defined and its value is true in C

I am working on a project that has multiple products and they all share the same code base. Each product has a product_XXX.h file associated with it that use #define directive to define macros. I ...
bonitoo2005's user avatar
1 vote
0 answers
61 views

Why we need #define FN(x) do{ f1(x); f2(x);} while(0) instead of #define FN(x) { f1(x); f2(x); } [duplicate]

why using do{}while(0) in macro expansion #define FN(x) do{ f1(x); f2(x);} while(0) Below code will do the same right? #define FN(x) { f1(x); f2(x); } if(condition) FN(val) // **without a semicolon*...
user2999236's user avatar
3 votes
2 answers
123 views

How to create a lookup table from list of constants with a macro?

I want to create a lookup up table from a constant value to a constant name struct char TABLE[128][32] = { [CONST_1] = "CONST_1",[CONST_2] = "CONST_2", ... }; I defined two macros ...
andreoss's user avatar
  • 1,600
1 vote
2 answers
157 views

The commas in __VA_ARGS__ leads to wrong expansion of macro definition

The commas in __VA_ARGS__ leads to wrong expansion of macro definition, like this My_Stu_t my_stu = { .member2 = {1}, 3}( member3, {2,3} ) }; I hope the correct result should be like this ...
yyd's user avatar
  • 33
0 votes
2 answers
84 views

Where and why to use preprocessor macros and directives in C++? [duplicate]

In a tutorial I saw they used preprocessor directives to define macro. Why should I use macros instead of normal functions. // Why should I use this #define GROW_CAPACITY(capacity) \ ((capacity) &...
Ata Ege's user avatar
  • 40
-1 votes
3 answers
65 views

I can't wrap my head around this sentence from the GNU C PREPROCESSOR documentation

Link to the page It is in the 9th row under the "tokenization" section in the "Preprocessor overview" It's as follows: The only identifier which can be considered a preprocessing ...
hjtomi's user avatar
  • 9
1 vote
2 answers
300 views

#define and #ifdef macro in Rust

Is it possible to rewrite this C++ code into Rust? #define is_unionfind_imported class UnionFind { }; class Kruskal { #ifndef is_unionfind_imported #error "UnionFind not imported" #...
ynn's user avatar
  • 3,542
1 vote
2 answers
92 views

How can I reference function prototype parameter default values in a #define in C++?

Suppose I have a function prototype which defaults several parameters: bool debug_log(char* text, int len, bool log_always = true, // Defaults to yes, log SRgba* ...
Rick C. Hodgin's user avatar
0 votes
2 answers
103 views

Adding custom version for C/C++ code file

My question is related to C source files. Let me try and explain in simple terms. Let's say I've 2 executables as below - main1.exe - created by linking main1.c, f1.c, f2.c main2.exe - created by ...
Kailash's user avatar
  • 53
3 votes
2 answers
105 views

Is there any C++ code that can't be passed through an identity preprocessor macro?

Given: #define IDENTITY(...) __VA_ARGS__ Is there any code that the compiler will reject, that it would normally accept, when wrapped in an invocation of this macro? The simpler #define IDENTITY(x) x ...
Joseph Garvin's user avatar
0 votes
1 answer
34 views

C preprocessor stringify macro without expanding its components

i would like to pass a string via a preprocessor macro to my source code. The desired output is something like this: $ cc -DFOO="hello world" test.c test $ ./test foo says: hello world $ cc -...
umläute's user avatar
  • 29.2k
0 votes
1 answer
102 views

Macro to determine whether another macro is set

In my code I often have constructs like this: #ifdef SOME_MACRO int some_value=1; #else int some_value=0; #endif // ... if (some_value) { // ... } For clarity I would like to have something ...
paperjam's user avatar
  • 8,341

1
2 3 4 5
117