c-preprocessor

If-directive macro comparison

心不动则不痛 提交于 2021-01-20 22:12:30
问题 Why is the #if condition in the following code fulfilled: #include <iostream> #define VALUE foo int main() { #if VALUE == bar std::cout << "WORKS!" << std::endl; #endif // VALUE } 回答1: The page on cppreference.com states: After all macro expansion and evaluation of defined and __has_include (since C++17) expressions, any identifier which is not a boolean literal is replaced with the number ​0​ (this includes identifiers that are lexically keywords, but not alternative tokens like and). So

How to write generic #define macro in C and write less code

烈酒焚心 提交于 2021-01-02 08:20:52
问题 Let's say I have 2 sets of values for P_A, P_B, P_C as below #define X_P_A 2 #define X_P_B 3 #define X_P_C 4 #define Y_P_A 5 #define Y_P_B 6 #define Y_P_C 7 There are 3 types of users:- once that only need X variants, once that only need Y variants and once those may need both. eg #ifdef X #define P_A X_P_A #define P_B X_P_B #define P_C X_P_C #endif #ifdef Y #define P_A Y_P_A #define P_B Y_P_B #define P_C Y_P_C #endif Users that need both will make the decision at run time and call X_P_<> or

How to write generic #define macro in C and write less code

旧时模样 提交于 2021-01-02 08:20:34
问题 Let's say I have 2 sets of values for P_A, P_B, P_C as below #define X_P_A 2 #define X_P_B 3 #define X_P_C 4 #define Y_P_A 5 #define Y_P_B 6 #define Y_P_C 7 There are 3 types of users:- once that only need X variants, once that only need Y variants and once those may need both. eg #ifdef X #define P_A X_P_A #define P_B X_P_B #define P_C X_P_C #endif #ifdef Y #define P_A Y_P_A #define P_B Y_P_B #define P_C Y_P_C #endif Users that need both will make the decision at run time and call X_P_<> or

Does '#'-character have to be at the start of a line in the C preprocessor? [duplicate]

試著忘記壹切 提交于 2021-01-02 05:19:06
问题 This question already has answers here : Should preprocessor instructions be on the beginning of a line? (5 answers) Closed 5 years ago . I have programmed C for quite a while now. During this time I have learned that it is a common convention to put the "#"-character that comes before preprocessor-directives at column one. Example: #include <stdio.h> int main(void) { #ifdef MACRO1 #ifdef MACRO2 puts("defined(MACRO1) && defined(MACRO2)"); #else puts("defined(MACRO1)"); #endif #else puts("

Preprocessor macro value to Objective-C string literal

 ̄綄美尐妖づ 提交于 2020-12-27 08:38:30
问题 I have a preprocessor macro defined in build settings FOO=BAR That value I want to massage into an Objective-C string literal that can be passed to a method. The following #define does not work, but it should demonstrate what I am trying to achieve: #define FOOLITERAL @"FOO" //want FOOLITERAL to have the value of @"BAR" myMethodThatTakesAnNSString(FOOLITERAL); I expect that I am just missing the obvious somehow, but I cannot seem to find the right preprocessor voodoo to get what I need. 回答1:

Define a 'for' loop macro in C++

試著忘記壹切 提交于 2020-11-30 06:44:31
问题 Perhaps it is not good programming practice, but is it possible to define a for loop macro? For example, #define loop(n) for(int ii = 0; ii < n; ++ ii) works perfectly well, but does not give you the ability to change the variable name ii . It can be used: loop(5) { cout << "hi" << " " << "the value of ii is:" << " " << ii << endl; } But there is no choice of the name/symbol ii . Is it possible to do something like this? loop(symbol_name, n) where the programmer inserts a symbol name into "

What's the meaning of #line in C language?

五迷三道 提交于 2020-11-30 04:24:51
问题 What's the meaning of #line in the C language? Where would it be used? 回答1: It tells the compiler where the following line actually came from. It's usually only the C preprocessor that adds these, for example, when including a file, it tells the compiler (which is basically only seeing one stream of data) that we're looking at a different file. This may sound strange, but the preprocessor simply inserts the header files where you specify your includes, and the compiler works on the whole

Macro as a parameter to another macro

﹥>﹥吖頭↗ 提交于 2020-11-28 08:29:04
问题 I'm trying to pass the parameters to macro SETBIT with another predefined macro like this: #define SETBIT(ADDRESS,BIT,N) {(N) ? (ADDRESS &= ~(1<<BIT)) : (ADDRESS |= (1<<BIT))} #define DAC_SYNC PORTB,3,POS SETBIT(DAC_SYNC); However I receiver error: macro SETBIT requires 3 parameters only 1 given There is an article with the following recommendations: to prevent misnesting of arithmetic operations: #define foo (a,b) or #define bar(x) lose((x)) But even though I still have an error. BTW,