Jason S
Jason S

Reputation: 189686

how to combine multiline macros in C with comments at the end of a line

I'm having a brain cramp... is there a way in C to combine a multiline macro with a comment at each line?

e.g.

#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 gets used elsewhere, and make it very human-readable.

Upvotes: 7

Views: 1346

Answers (4)

too honest for this site
too honest for this site

Reputation: 12263

According to a comment, It seems to be an XY-problem. A macro might not be the best approach.

If you are up to a table of constants, the normal way would be just to generate an array once and use that throughout your 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
};

If that is only used in a single file, make it static as shown, else remove the static and add

extern const float cal_table[5];

to the header file of the module.

Note that, if this is for a small MCU (AVR/PIC) without floating point unit, you might be better off with not using float, but fixed-point simulated by integers (e.g. scaled to 1mV).

You can also use this as an initializer for an auto array variable. Make that array a typedef, generate the const as described and memcpy the array to the local variable as required. This is as fast as an initializer, because the compiler also has to copy that to the local array. So you use the const array as a template (still having a single location to change, if required) .

Upvotes: 4

sergej
sergej

Reputation: 18009

Not quite what you are asking for. Without comments, but still human-readable and it calculates the values for you:

#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;

Upvotes: 3

rici
rici

Reputation: 241771

You can use multiline comments as a way to continue macro definitions from one line to another, instead of backslashes. As described in the C standard, §5.1.1.2/1, comments are reduced to a single space during translation phase 3, while preprocessing directives are executed in phase 4. In effect, that means that newline characters inside a multiline comment do not terminate a preprocessing directive, so you could write:

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

Note that line continuations (backslash newline) are removed in phase 2, before comments are recognized as such. So the problem with using C++-style // comments is not that the comment includes the backslash; rather it is that the lines are first concatenated and then the comment extends to the end of the concatenated lines.

Upvotes: 11

Erik Johnson
Erik Johnson

Reputation: 1164

You should be able to do this, but not with C++ style // comments. Try it with traditional /* */ C comments. The C++ comments comment out the rest of the line, including the backslash.

Upvotes: 4

Related Questions