#Over using parentheses

24 messages · Page 1 of 1 (latest)

sweet swan
#

I have a tendency to over use parentheses to make sure my code does what I want it to do. I could always just test this by running the code to see if it does what I want it to do but I just get more assurance this way since sometimes I’m not sure what takes precedence over the other. Is it good or bad practice to be doing this? Or neither?

Ie. #define SetBit(addr, bit) ((*((volatile uint32_t*)(addr))) |= (1<<(bit)))

green anvilBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

grim lion
#

or maybe that's not what the code is doing, it becomes a bit unclear with that many

#

it's alright but using parentheses for literally every operator is a bit much

sweet swan
#

Makes sense

#

I guess I don’t really need the last set of parentheses

#

Just curious, I only noticed one set of parentheses around the whole macro

#

Unless I’m misunderstanding it

sweet swan
#

Don’t blame you, I guess it would make it hard to read aswell 😂😂

#

I guess just knowing precedence would help me go a long way then

grim lion
#

since they're both unary prefix operators

sweet swan
#

True since casting has higher precedence than de referencing

#

Right?

grim lion
#

it's just the order you write them in

#

e.g. -*x dereference then negate, *-x negate then dereference

sweet swan
#

Ohhh okay

#

Makes sense now

#

Thanks!

green anvilBOT
#

@sweet swan Has your question been resolved? If so, type !solved :)

sweet swan
#

!solved