fiftyplus
fiftyplus

Reputation: 561

something about #define syntax in C

could any one explain what does

#define something (54)

mean? Why 54 is inside a bracket?

Upvotes: 4

Views: 1718

Answers (7)

Brent Bradburn
Brent Bradburn

Reputation: 54989

Why would you use this

#define SOMETHING    (54)

instead of this

const int SOMETHING = 54;

I'm not sure what the answer to that is -- I think that this kind of use of #define may be somewhat archaic. That said, the use of the parentheses is nice, because it makes the two types of declarations behave in basically the same way. So now if you want to tweak the value...

#define SOMETHING (54+1)

or

const int SOMETHING = 54+1;

...they are still generally equivalent as "manifest constants". Without the parentheses, they would not be equivalent because the #define is applied using simple substitution before the expression is evaluated.

So if the #define value is applied like so...

int a = 2*SOMETHING;

...you would get different results depending on whether or not the parentheses were used in the declaration:

int a = 2*54+1; // no parentheses -- answer is 109
int a = 2*(54+1); // parentheses -- answer is 110

Of course as long as the assigned value is a simple constant, as in your example, the presence of the parentheses makes no difference to the compiler. It is only for the benefit of the humans who are writing this code or who might modify this code in the future. By consistently applying this form of declaration, the author is attempting to reduce the occurrence of human error.

Upvotes: 1

Josh Petitt
Josh Petitt

Reputation: 9589

It is in general a good idea to put any #define statement inside parenthesis. This is a good habit, and most daily programmers adhere to good habits.

For instance:

#define TWO_PLUS_ONE    2 + 1

if I use it like this:

3 * TWO_PLUS_ONE

I would expect to see the answer as 9, however due to operator precedence, the calculated answer would be 7. There are dozens of corner cases you could find like this (see http://www.gimpel.com/html/bugs.htm). This is why C++ programmers scream, "Macros are evil!". But we are C programmers, we are elite, we ain't scared.

The same example:

#define TWO_PLUS_ONE    (2 + 1)

This will give the expected result in all situations.

Most programmers want their practices to apply in all situations, so it is easy to remember, easy to practice and easy to do. So in the simple case of

#define SOMETHING    (54)

Just do it. It is a good idea. Learn to be part of the team, they are trying to help you. BTW, next they will say, "it should really be (54u)"

Upvotes: 4

sarnold
sarnold

Reputation: 104090

Probably it was written this way to make future changes easy; consider changing 54 to 1024*54 or 1024+54 -- with the parens, the change can be made directly without much further thought. If the change is made without the parens, then the precedence of operator evaluation matters everywhere that something is used in the program.

Upvotes: 0

Chimera
Chimera

Reputation: 6038

#define something (54) tells the C pre-processor to replace any text matching "something" with "(54)" before the code is actually compiled.

The reason you will often see the use of ( ) around a #define is that in some cases it prevents the replaced text from having adverse or undefined behavior when the #defined text is replaced into a larger expression. Such adverse effect might be the changing of operator precedence etc..

Upvotes: 4

Armfoot
Armfoot

Reputation: 4931

Parenthesis in that case are ignored. It's the same as writing:

#define something 54

They just help you like in math such as:

#define something (54-2)/2

Is different from

#define someotherthing 54-2/2

Upvotes: 1

Ed Swangren
Ed Swangren

Reputation: 124770

The parentheses are superfluous. I cannot imagine a scenario in which (54) would differ semantically from 54. Now, if I'm wrong... show me; I'll have learned something new.

Upvotes: 1

Seth Robertson
Seth Robertson

Reputation: 31471

"()" are parenthesis and not [brackets]. It is essentially a no-op. Any time where 54 would be valid, (54) would be valid, just like (50+4) would be valid, or (27*2) or any other expression.

Perhaps you can give us more information about this "quality standards" error you are seeing? Perhaps someone doesn't like the parens since they are unnecessary.

Upvotes: 0

Related Questions