Liviu - 1 year ago 82

C++ Question

I know that

`#define`

`#define SYMBOL string`

If I write, for example

`#define ALPHA 2-1`

#define BETA ALPHA*2

then

`ALPHA = 1`

`BETA = 0`

But if i write something like this

`#define ALPHA (2-1)`

#define BETA ALPHA*2

then

`ALPHA = 1`

`BETA = 2`

Can someone explain me what's the difference between those two ?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

**Order of operations**. The first example become 2-1*2, which equals 2-2.
The second example, on the other hand, expands to (2-1)*2, which evaluates to 1*2.

In the first example:

```
#define ALPHA 2-1
#define BETA ALPHA*2
```

alpha is substituted directly for whatever value you gave it (in this case, 2-1). This leads to BETA expanding into (becoming) 2-1*2, which evaluates to 0, as described above.

In the second example:

```
#define ALPHA (2-1)
#define BETA ALPHA*2
```

Alpha (within the definition of BETA) expands into the value it was set to(2-1), which then causes BETA to expand into (2-1)*2 whenever it is used.

In case you're having trouble with order of opeartions, you can always use the acronym **PEMDAS** to help you (Parenthesis Exponent Multiplication Division Addition Subtraction), which can itself be remembered as "Please Excuse My Dear Aunt Sally". The first operations in the acronym must always be done before the later operations in the acronym (except for multiplication and division (in which you just go from left to right in the equation, since they are considered to have an equal priority, and addition and subtraction (same scenario as multiplication and division).

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**