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#.
5,597
questions
2
votes
1
answer
55
views
Is there a purpose for using a macro that does nothing over an empty macro in c++?
Learning c++ and reading through a project, I found this.
#define EMPTY_MACRO do {} while (0)
...
#if ASSERTS_ENABLED
#define ASSERTCORE(expr) assert(expr)
#else
#define ASSERTCORE(expr) EMPTY_MACRO
...
0
votes
1
answer
32
views
Embed Define into a string
I have a preprocessor define that should determine the size of an array.
This constant should also be passed to a HLSL shader.
For this I need to pass it around as a string.
Is there a way to embed ...
0
votes
1
answer
40
views
Dynamic macro-selection in a loop
I have a header file 'a.h' with some macro-definitions of the type:
Header 'a.h' contents:
#define STREAM1 cout
#define STREAM2 cerr
#define STREAM3 some_out_stream3
#define STREAM4 some_out_stream4
.....
0
votes
1
answer
68
views
Why can't GCC's typeof() be stringified?
I'd like to print the type that typeof() outputs, but typeid is only available in C++. Why can't I use stringification to get the name of this type?
#define GET_STRING(s) #s
#define example(input) ...
0
votes
1
answer
69
views
Determine data model by C preprocessor
I want to write a .h file conforming to C89 that would be understood by most C preprocessors like gcc, cl (Visual Studio) etc. and that would determine the data model used, i.e. how many bits the (...
1
vote
3
answers
106
views
Recursively including a header file over a list of values
Suppose I have an header that is meant to be included several times generating code from a template parameterised over a macro DATA. I use it in this way:
#define DATA this
#include <header.hpp>
...
4
votes
6
answers
95
views
How to assign subsequent bits to C enumeration members?
Here's what I got:
enum X {
NONE = 0x00000000,
FLAG_1 = 0x00000001,
FLAG_2 = 0x00000002,
FLAG_3 = 0x00000004,
FLAG_4 = 0x00000008,
FLAG_5 = 0x00000010,
// ...
FLAG_32 = ...
0
votes
2
answers
50
views
How do these pre-processor defined kernel functions work?
I am trying to execute some kernel functions on the Apple M1, and have come across
this code which appears to use C macros to define some kernel functions at compile time. These are kernel functions ...
1
vote
0
answers
26
views
Which tokens are ignored by MSVC pre-processor in expanding macros?
I saw Macro expansion ignores some tokens in MSVC earlier, and was curious about the behaviour of the MSVC preprocessor. Obviously the correct answer to that and similar questions is "Don't use ...
0
votes
1
answer
20
views
Make Xcode build a C file generated by processing a file with a custom extension
I'm trying to set up Xcode to build generated .c files just like it does for files generated by yacc (*.y) and lex (*.l) files.
Say I have input files with a *.corn extension that are meant to be ...
9
votes
2
answers
298
views
Removing last trailing comma from the arguments of a macro
I need to remove the last trailing comma from a list of macro arguments (because they will be eventually expanded into template arguments where the trailing comma is not admitted).
So I need a macro ...
0
votes
0
answers
53
views
Access the contents of __VA_ARGS__ in a macro (not a function)
One can access the contents of ... inside a function using stdarg.h:
void fn(int nargs, ...){
va_list args; va_start(args,nargs);
i64 arg0 = va_arg(args,i64);
va_end(args);
}
The only way I ...
0
votes
1
answer
77
views
Default arguments to C macros
Suppose I have function bshow() with signature
void bshow(int arg0, int arg1, int arg2);
but for arbitrary reasons I want to implement it as a macro.
Furthermore, I want the function have default ...
0
votes
1
answer
89
views
Is there a way to manipulate preprocessor define scope in C
Just curious. Imagine I need to have a #define A that is the sum of n numbers, and those n numbers have a meaning I'd like to make explicit, but only for the computation of A, to improve readiblity,
...
1
vote
1
answer
8
views
How can I differentiate, in my C/C++ code, between GCC and clang as the compiler?
I usually write GCC-targeted code by checking __GNUC__. Now I want to write some code which behaves differently for GCC and for clang - but it seems clang defines __GNUC__ as well. How do I ...
1
vote
1
answer
20
views
#define scope (microchip studio)
Im using microchip studio
How can I define F_CPU in main.c file and make all files "see" this definition?
I declared in main.c:
#include <avr/io.h>
#include "UART.h"
#define ...
0
votes
1
answer
47
views
mpicc takes long time in alpine container
I have a docker container running alpine:latest in which I installed build-base, openmpi, openmpi-dev,.. and basically everything works fine, except when I run
mpicc -v -time=time_out -o /root/cloud/...
1
vote
1
answer
25
views
MSVC: how to include the # character in the value of a pre-processor macro passed from the command-line
We build/run our embedded C code on MSVC as a convenient development/test environment.
Like most C compilers, MSVC allows the use of -D to define pre-processor macros on the command line, i.e. CL -...
1
vote
1
answer
68
views
How can I unpack an array as separate macro arguments?
I am using the qmk library, which has a LAYOUT macro that takes many parameters. It is used like so (with KC_xxx etc constants):
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[...
1
vote
0
answers
29
views
Substitute just once, concatenate, and substitute again [duplicate]
Given macro definitions like this:
#define GPIOA ((1234)+42)
#define GPIOB ((1234)+84)
#define LEDA_GPIO_Port GPIOA
#define LEDB_GPIO_Port GPIOB
#define MY_PORT_GPIOA 0
#define MY_PORT_GPIOB 1
I ...
0
votes
2
answers
69
views
Can I test the value of a preprocessor directive?
I have a preprocessor directive that I do not set, so I cannot change it, it is either true or false.
Normally I would have done :
#ifdef DIRECTIVE
// code
#endif
But this will always run, since ...
3
votes
1
answer
75
views
What does "#if ..." (with an ellipsis) do?
I was doing some studying of the windows header files and I came upon a preprocessor statement that I didn't recognize. I've been searching for any information on this statement for about a week and I ...
1
vote
1
answer
65
views
What kinds of expressions are allowed in a `#if` (the conditional inclusion preprocesssor directives) [duplicate]
Many sources online (for example, https://en.cppreference.com/w/cpp/preprocessor/conditional#Condition_evaluation) say that the expression need only be an integer constant expression.
The following ...
0
votes
3
answers
119
views
GCC preprocessor directives for Arch Linux
Does GCC (or alternatively Clang) defines any macro when it is compiled for the Arch Linux OS?
I need to check that my software restricts itself from compiling under anything but Arch Linux (the ...
1
vote
0
answers
72
views
Why aren't stringification and concatenation operands macro expanded by standard? [closed]
All C standards from ANSI C/C89 requires that operands of # and ## not be macro expanded and be left as-is. This seems to have been been leading to frequent confusion and frustration, such as the ...
4
votes
4
answers
118
views
C: How to name a struct field `errno`?
I want to define a struct:
struct error
{
int errno;
/* other information */
};
And then I want my code to have error paths that look like this:
struct error my_error;
my_error.errno = errno;
...
2
votes
2
answers
63
views
Add prefix to a macro that calls printf
Having this #define PRINTF(...) printf(__VA_ARGS__) I want to create a macro that calls PRINTF but that adds prefix to the printed string, for example:
#define PRINTF_P(<args>) PRINTF(<...
2
votes
0
answers
28
views
Including C headers from assembly
I've been writing assembly on linux for a while and I often find myself in need of including C headers which contain macro, enum and struct definitions (but clearly I just care about the macros). So, ...
0
votes
1
answer
61
views
C++ Macro with "double function"
I am looking for a way to use a single macro call (multiple times):
#define MAGIC ???????
MAGIC(ref1, "text 1");
MAGIC(ref2, "text 2");
to expand into something like this:
const ...
0
votes
1
answer
54
views
Can I use "(" in preprocessor concatenation in C?
The following gives this error in GCC: pasting "func_x" and "(" does not give a valid preprocessing token
#include <stdio.h>
#define LOG_FUNC(fname) printf( #fname " %d\...
1
vote
2
answers
113
views
problem while using stringize operator in c
#define str(n) n
puts(str(hello));
The above piece of code is working fine in c but the below mentioned code
is not giving any output in the console
#define str(n) #n
char* name="David";
...
0
votes
1
answer
20
views
How to give value in Global Compiler Settings #define in CodeBlocks
so I have this code on codeblocks in which changing value of the preprocessor will have different function, here is the sample.
#ifdef LOOP_ == 1
\\some function
#elif LOOP_ == 2
\\another ...
1
vote
0
answers
21
views
Is there any difference between #define and # define? [duplicate]
I'm studying C and, according to the style guide everyone in my school must abide, I should put a space between # and define, but never in #ifndef or #endif, for instance. If I do either, it says I ...
0
votes
1
answer
42
views
Using __VA_ARGS__ in a nested macro, but the args are truncated
I'm doing something like below:
#define AA(mac, a, ...) mac(a, __VA_ARGS__)
#define MAC1(a, b, c) a##b##c
AA(MAC1, 0, 1, 2)
what I really want is to translate "AA(MAC1, 0, 1, 2)" to "...
0
votes
4
answers
111
views
Should I #include a library header?
Suppose I want only one (or a few) function from a certain header but it's a function I can define with the same efficiency as the library's implementation. Should I include a library header or ...
1
vote
1
answer
64
views
Is double quote (") a preprocessing-token or an unterminated string literal?
Is double quote (") a preprocessing-token or an unterminated string literal?
C11, 6.4 Lexical elements, Syntax, 1:
preprocessing-token:
header-name
identifier
pp-number
character-...
3
votes
1
answer
62
views
How can I use a macro inside another macro in order to concat the definition of the macro to something? C/C++
I am trying to make a macro that will be take a predefined prefix and concatenate it to the wanted function name in the argument of the macro, but I have been unsuccessful.
For example, while this is ...
0
votes
1
answer
64
views
Why do ffmpeg developers use "runtime if" with macrofied condition?
I'm looking to FFmpeg source code and I find it weird to use weird code like this:
#define MACRO 0
void func(void)
{
if (MACRO)
{
//code
}
}
Is there any reason to use "if&...
1
vote
1
answer
97
views
inline ARMv7-M assembly code into c using gcc and macros
I have this code that works fine:
void function( void )
{
__asm volatile
(
" ldr r3, .ADDRESS \n"
" mov r2, %0 \n"
&...
0
votes
2
answers
47
views
How to convert a log2(n) based value to n shifts in a #define statement in C?
I have a definition of the following type in C:
#define NUM_OF_CHANNELS 8
I want to refer to this definition and use it also for shift operations, such as
a = b >> 3
The value 3 comes from ...
2
votes
0
answers
36
views
How to map #define's replacement list containing space(s) to integers (IDs)?
A #define's replacement list containing no spaces can be mapped to integers (IDs):
#define ID_double 1
#define ID_float 2
#define ID_long_double 3
#define ID_(x) ID_##x
#define ...
0
votes
1
answer
70
views
Why does the preprocessor replace comments by single space instead of removing them in C language [closed]
I've read that comments in C language are replaced by single spaces while pre-processing. In the specification
The program is split into tokens separated by white-space characters; comments are
...
1
vote
2
answers
88
views
Type of a struct member at compile time
Is there a way of knowing the type of a struct member at compile time? Something analogous to offsetof(), but for types.
Eg. something like:
typedef struct{
int b;
char c;
}a_t;
typeof(a_t,b) ...
0
votes
1
answer
84
views
Defining a macro as comma (to separate arguemnts with that marco) works properly in functions arguments but not in other macros
I come up with a small example of the problem
Assume I have this macro
#define and ,
And I use it like sum(a and b) which should be expanded to sum(a, b).
My problem here is that if sum is defined by ...
0
votes
1
answer
68
views
Is there a way to use variable's data in C?
I'm trying to create something like a library. I found out that, using #define macros, you can pretty much do anything you want with your code, and I tried to chain some of them in some crazy way to ...
-5
votes
3
answers
96
views
In what situations does C preprocessor ## work and not work?
It seems that sometimes concatenation with ## does work and sometimes it does not work.
It is an unreliable feature even though it is clearly vitally necessary for some uses.
Is there a clear set of ...
1
vote
2
answers
80
views
How do I generate code like for using a C macro? #for?
Is there are C macro like the for loop, that can generate code for me to use functions?
for example:
I have
SetSystem1();
SetSystem2();
SetSystem3();
...
SetSystem100();
can I write
#for index = 0 to ...
0
votes
1
answer
81
views
C++ and MSVC #define directive with optional arguments
I'm having trouble trying to get a macro that I'm writing to function correctly. I've read the docs and can't find anything online to help with what I'm looking for.
I am attempting to write a macro ...
1
vote
1
answer
68
views
Can I implement this kind of profiling code with a macro?
Not an expert on preprocessor macro tricks, so if the problem here is just that I'm not familiar with some common macro idiom I'd be happy with just a term to Google. X macros are about as far as I'...
-1
votes
1
answer
72
views
How to print message (warning, error) in preprocess time when a macro is called in c++
I know how to print error, warning and message in preprocess time, but I want that information is printed as soon as some macro is called.
#define EXPECTED_FUTURE(year, day)
// may be need the ...