Search code examples
cparentheses

Parentheses around a constant or around a cast expression involving a constant


From time to time I see this code:

#define X1   (13)
#define X2   ((size_t)13)

As I understand, the (outer) () are redundant here. Is that correct?


UPD: Some software development guidelines may require it.

For example: MISRA C:2004, Rule 19.4 (required):

C macros shall only expand to a braced initialiser, a constant, a parenthesised expression, a type qualifier, a storage class specifier, or a do-while-zero construct.

However, MISRA C:2012 has:

#define MY_NULL_2 ( void * ) 0
#define sqrt( x ) ( _BUILTIN_sqrt ( x ) )

Solution

  • I don't believe the first example needs parentheses, but always parenthesizing macro bodies is a reasonable habit.

    The second one will produce unexpected grouping if you remove the outer parentheses from the definition and then invoke it in the context X2[a].