1

I saw the following in a .h-file for the cc2640 mcu:

#define ADC_STATUS_SUCCESS         (0)

From my C knowledge, the compiler is told to put the value of ADC_STATUS_SUCCESS everywhere it occurs, that is (0). But what is the difference in putting just 0?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
jkbs1337
  • 53
  • 6
  • *Almost* no difference. But consider a corner case such as `sqrt ADC_STATUS_SUCCESS;` and think of what will happen in each case. – Eugene Sh. Jan 17 '20 at 15:37
  • 4
    Does this answer your question? [Using Parentheses in Define Preprocessor Statements](https://stackoverflow.com/questions/22155634/using-parentheses-in-define-preprocessor-statements) – The_Average_Engineer Jan 17 '20 at 15:41
  • 1
    thank you Eugene and Bastien, that is interesting! But: What does a programmer think if he puts parenthesis on a single value? Does that make any sense? So maybe my opening question should be more specific in "What do parenthesis do to values in that context?" or something like that. I assume, there isn't more to it as Eugene implied, actually it doesn't make any sense for a value and suggests an additional functionality that just isn't there. – jkbs1337 Jan 17 '20 at 15:47
  • I'd rather wonder why they used macros to represent constants when real constants are readily available. Also, you sometimes see people write `return(0);` in their programs, so sometimes they just don't think and/or copy bad habits from others. – Ulrich Eckhardt Jan 17 '20 at 16:02
  • @UlrichEckhardt Well, magic numbers are bad. – Eugene Sh. Jan 17 '20 at 16:02
  • @EugeneSh. I think they're referring to c99's `const` keyword. – Federico klez Culloca Jan 17 '20 at 16:18
  • I didn't even mean that with the `return(0)` example of bad code, I meant the parentheses. But `static uint32_t const adc_status_success = 0;` would be my choice to define a constant. – Ulrich Eckhardt Jan 17 '20 at 16:19
  • 1
    @UlrichEckhardt OK, I misinterpreted your comment. – Eugene Sh. Jan 17 '20 at 16:19

1 Answers1

2

what is the difference in putting just 0?

None, if you don't write crazy code. It is common to use parentheses for macros that contain expressions to avoid unexpected errors related to operator precedence and similar stuff when using them. In this case though, defining something as 0 or as (0) is the same if it's used in expressions.

What do I mean by "crazy code"? Well, the only difference between the two can be seen in something like the following:

void func(int x) { /* ... */ };

#define ADC_STATUS_SUCCESS 0
func ADC_STATUS_SUCCESS;       // INVALID

#define ADC_STATUS_SUCCESS (0)
func ADC_STATUS_SUCCESS;       // VALID (for the love of God NEVER do this)

I highly doubt this is the case though, nobody in their right mind would write such an abomination. That define is most likely out of habit.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128