How to combine multi-line macros in C with comments at the end of a line

I have a brain cramp ... is there a way in C to combine a multi-line macro with a comment on each line?

eg.

#define MYARRAY { \
  0.001,   //  5 mV \
  0.002,   // 10 mV \
  0.004,   // 20 mV \
  0.007,   // 35 mV \
  0.013    // 65 mV \
}

I need to define a list of commented array values ​​in a header file that is used elsewhere, and make it very readable.

+4
source share
4 answers

According to the comment, this seems like an XY problem. A macro may not be the best.

If you come across a table of constants, the usual way would be to simply create an array once and use it in all the code:

static const float cal_table [5] = {
    0.001,   //  5 mV
    0.002,   // 10 mV
    0.004,   // 20 mV 
    0.007,   // 35 mV
    0.013    // 65 mV
};

, static, , static

extern const float cal_table[5];

.

, MCU (AVR/PIC) , , float, , (, 1 ).

auto. a typedef, const, , memcpy . , , . , const ( , ).

+3

, . C, & sect; 5.1.1.2/1, 3, 4. , , :

#define MYARRAY { /*
*/   0.001,   /*  5 mV 
*/   0.002,   /* 10 mV
*/   0.004,   /* 20 mV
*/   0.007,   /* 35 mV
*/   0.013    /* 65 mV
*/ }

, ( ) 2, . , ++ - style // , ; , , .

+5

, ++. /* */C. ++ , .

+4

, . , - :

#define MILLI_VOLT(v) (v/5000.0)

#define MYARRAY {   \
    MILLI_VOLT(5),  \
    MILLI_VOLT(10), \
    MILLI_VOLT(20), \
    MILLI_VOLT(35), \
    MILLI_VOLT(65), \
}

double a[] = MYARRAY;
+2

Source: https://habr.com/ru/post/1608902/


All Articles