I came across a #define
statement with the following expression. I do not understand what do the whole statement or the comma do that separate the 2 expressions within the parentheses. It seem like a function, I guess it is not.
#define LCD_SELECT_CS1 (PORTD |= (1<<3), PORTD &= ~(1<<2))
I guess there are two questions here:
How it works is by use of the comma operator, which is probably C's least-used operator. It basically means, "first do the first thing, then do the second thing." It is not the same (even though it looks the same) as the commas separating the arguments in a function call, or the commas separating the declarators in a multiple declaration like int i, j;
. See also How does the Comma Operator work?.
And then there's the "style" question of why it was written this way, or if it was a good idea to write it this way.
Macros in C work by textual replacement. They don't have to look like anything, they don't have to make sense, in general the only way to understand them is to look at what you get after expanding them. Here, whenever you write LCD_SELECT_CS1
, what you get is
(PORTD |= (1<<3), PORTD &= ~(1<<2))
and if you can understand what (PORTD |= (1<<3), PORTD &= ~(1<<2))
does, you're done.
Normally, to avoid confusion, a macro ought to expand either to a single object or expression, or if it expands to a sequence of things, it ought to be a "function-like macro" that takes an argument list, more like it was a function. Since this LCD_SELECT_CS1
macro doesn't match either of those patterns, we find that it is confusing, and therefore poor style.