**Unary operator in ‘C’**

**Unary Increment Operator and Unary Decrement Operator**

There are a couple of unary operators in ‘C’.

**++** is a symbol for the unary increment operator.

**– –** is a unary decrement operator.

These operators need only one operand. That’s why these operators are called **unary operators**.

Consider this expression **x++**. Here, ‘x’ is a variable, and ++ is an expression of incrementing the value of x by 1. Now, **x- –** is an expression of decrementing the value of x by 1 using increment and decrement operators.

Suppose, if I write something like this **x = x + 1**, this is also an increment operation. But here, I used the addition operator, which is the arithmetic operator. Now, the equivalent of this expression using the unary operator would be **x++**. Both are the same. In both cases, the x value will be incremented by 1. So, **x=x+1** is done using the arithmetic operator, and **x++** is done using the unary operator.

First, let’s discuss the unary increment operator ++. ++ is a unary operator and it can appear on either side of an expression.

Consider the expression, as shown in Figure 1. Here there is a variable **x**. If you do **x++**, then **x** is incremented by **1**. And you can also do **++x**, this is the same as **x++**, here also, **x** is incremented by 1. So, the operator, you can use either side of an expression.

You have to remember that the increment operator(**++**) adds 1 to the value of the operand and updates the operand.

Let’s see some examples.

Here there are two variables, **x** and **y**. x is initialized to 5. After that, observe this expression, **y = ++x**. After this expression, y’s value will be 6, and x value will be 6. That is the final result.

Let’s analyze how. Here, the first **++x** expression will be evaluated in this case. If you consider the **++x** expression, this is called pre-incrementing. In this case, the value of the x will be incremented first. So, 5 becomes 6. And after that, that new value will be assigned to y. That’s why 6 will be assigned to y.

So, first the value of x will be incremented by 1, and then the value of x will be assigned to y. So, y = 6, x = 6.

Consider another example. Here there are two variables, x and m. x = 5 and m = x++. This is called post-incrementing. Here, the first value of x will be assigned to m, and then the value of x will be incremented by 1. That’s why, after this **m=x++** expression, the result will be m = 5 and x = 6.

So, there are two types of incrementing. One is pre-incrementing, and another one is post- incrementing.

Now let’s understand pre-decrementing and post-decrementing.

Here x and y are two variables. Consider x = 5. y = – -x; – -x is a pre-decrementing. That’s why the first value of x will be decremented by 1. So, x becomes 4. After that, the new value of x will be assigned to y. So, y =4, x = 4.

Consider another example. Here there are two variables, m and n. n- – is called post-decrementing. In this **m=n- –** expression, first the value of n will be assigned to m, so m will be 5. And after that, n will be decremented by 1. That’s why the result will be m = 5 and n= 4.

In the following article, let’s understand unary operators with pointer variables.

**FastBit Embedded Brain Academy Courses**

Click here: https://fastbitlab.com/course1