Search code examples
cuser-defined-functionsbitwise-operators

Multi-line #define bitwise OR


I'm trying to implement a #define as below:

#define CTRL_EVENT EVT_0 | EVT_1 | EVT_2 | EVT_3 | EVT_4 | EVT_5 | EVT_6 | EVT_7 | EVT_8 | EVT_9 

But in practice the #define will be even longer so I want to be able to split this line across multiple lines for readability purposes, is there a way of doing this?


Solution

  • A backslash immediately before a newline character acts as a line continuation. So you can do this:

    #define CTRL_EVENT \
        (EVT_0 | EVT_1 | EVT_2 | EVT_3 | \
         EVT_4 | EVT_5 | EVT_6 | EVT_7 | \
         EVT_8 | EVT_9)
    

    Also note the parenthesis which prevent unexpected operator grouping in larger expressions.