Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do parentheses mean in an #if defined preprocessor operator?

(I am working on a SDK wherein I have the code of the particular SDK in reference and I am not able to trace out the flow of the program.)

What does

#if defined (AR7x00)

mean? Specifically, what is the purpose of parentheses in a such a preprocessor operator?

like image 920
Trilok M Avatar asked Jan 27 '14 08:01

Trilok M


1 Answers

These three preprocessor directives:

#if defined (AR7x00)

#if defined AR7x00

#ifdef AR7x00

all mean exactly the same thing: that the following code is to be processed only if the macro AR7x00 is currently defined.

The #ifdef ... directive is simply a convenient alternative to #if defined .... There's also a #ifndef ... directive; #ifndef FOO is equivalent to #if ! defined FOO.

As for the parentheses, the syntax for the defined operator allows for either an identifier, or an identifier in parentheses, with no difference in meaning. I'm not entirely sure why the parentheses are optional; I suspect it's just historical. (The language reference in the 1978 first edition of K&R doesn't mention the defined operator. The second edition shows both forms, with and without parentheses.)

Strictly speaking, these are not grouping parentheses of the kind you see in ordinary expressions; they're specifically part of the syntax of the defined operator, which can only be used in preprocessor #if directives. In particular, this:

#if defined ((AR7x00))

is a syntax error.

like image 73
Keith Thompson Avatar answered Oct 07 '22 04:10

Keith Thompson