0

I am new to C coding. I have come across an example code and I have trouble understanding the syntax:

//Turn ON MOSFET.
#define SWITCH_STATE_ON     (1)
//Turn OFF MOSFET.
#define SWITCH_STATE_OFF    (0)

Why do we have the brackets around "1" and "0"? I would have written the above but without brackets. Is this a mistake?

//Turn ON MOSFET.
#define SWITCH_STATE_ON     1
//Turn OFF MOSFET.
#define SWITCH_STATE_OFF    0

Thank you!

Lenn
  • 1
  • 3
    Does this answer your question? [C macros and use of arguments in parentheses](https://stackoverflow.com/questions/7186504/c-macros-and-use-of-arguments-in-parentheses) – Michael Apr 14 '21 at 20:14
  • @Michael: That's a different situation, as what's being parenthesized here is not an argument. – Nate Eldredge Apr 14 '21 at 20:34
  • There could be a difference between the two if some exotic token pasting is going on, as in https://stackoverflow.com/questions/12630548/c-expand-macro-with-token-pasting, but otherwise I don't think the parentheses are necessary here. It's more likely just someone trying to stay in the habit of always parenthesizing the expansion of an expression-like macro, because if the constant `1` were replaced by an expression like `1+1` the parentheses would be crucial. – Nate Eldredge Apr 14 '21 at 20:38

4 Answers4

3

In C, after preprocessor macros are replaced, the resulting source code is analyzed according to C grammar. This means that if somebody defines a macro:

define Foo 3+7

and then uses it:

printf("%d\n", 2*Foo);

the result of macro replacement will be printf("%d\n", 2*3+7);. Then this will be interpreted as printf("%d\n", (2*3)+7;, which will print “13”. To avoid this, macros that are intended to be used as expressions are commonly written with parentheses surrounding the replacement list:

#define Foo (3+7)

Then the result of replacement will be printf("%d\n", 2*(3+7));, which will print “20”.

In the case where the replacement list is a single token, such as 0 or 1, the parentheses are not necessary, as this mixing with neighboring tokens cannot occur. However, many people do it as a habit.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

It's to guard against precedence issues if your macro expands to an arithmetic expression or something like that. For example, assume the macro

#define SQR(x) x * x

If you write

x = SQR(1 + 2);

that would expand to

x = 1 + 2 * 1 + 2;

which isn't what you want - what you want is

x = (1 + 2) * (1 + 2);

so you would define the macro as

#define SQR(x) (x) * (x)

or better yet,

#define SQR(x) ((x) * (x))

For a numeric literal like what you have, it doesn't really make a difference. It doesn't hurt, but it doesn't add anything, either.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

Using Xcode, v11.3, I was able to compile (without errors) using both no brackets around the 0 and 1 digits as well as using brackets around both digits.

So, after entering "...#define MOSFET_ON 1 and #define MOSFET_OFF 0, and also

#define MOSFET_ON (1) , #define MOSFET_OFF (0), both worked. I'm not sure why, unless it's a specific IDE you are using, and, or compiler?

fjm
  • 21
  • 1
  • You're not sure why *what*? It sounds like you're saying that you're not sure why both alternatives you tried were accepted, but of course they were, and if that surprised you then it's not clear why so. – John Bollinger Apr 14 '21 at 21:47
-1

According to this:

Syntax

The syntax for creating a constant using #define in the C language is:

 #define CNAME value

OR

 #define CNAME (expression)

which means that 1 is not a value but an expression. As you can't use numbers as variable names i'd assume in this case 1 means 1 and it's pointless to use parentheses.

  • 1
    This is very misleading. As a matter of syntax, a macro's replacement text can be any sequence of (preprocessing) tokens. If a single token then that token does not need to be a value (it can be a name, or keyword, or punctuation). If multiple tokens then the first and last do not need to be opening and closing parentheses, and no part is required to be a C expression. Additionally, `1` is both a value and an expression in C, though that has nothing to do with macro definition syntax. – John Bollinger Apr 14 '21 at 21:31
  • Thank you everybody for your insights. I am relieved to see that parentheses are not required in this particular case. I am not that familiar with C so I was afraid I was missing some subtle syntax detail. The code is an example from the vendor supplying an IoT hardware platform. – Lenn Apr 15 '21 at 13:29