Unary Operators in C

Unary Operator in C Programming Language

In this article, we are going to discuss a very important topic in C language i.e. how to work with Unary Operators or you can say how to work with increment and decrement operators in the c programming language. Please read our previous article, where we discussed the basics of Operators in C Language. In most of the technical interview questions, they will ask a number of questions on unary operators. So, at the end of this article, you will understand everything about the unary operators in the c language.

Unary Operator in C Programming Language

Unary operator means an operator can perform operations on a single operand only. That means one operand is enough to perform the operation is called a unary operator. Unary operators are briefly classified into two types. They are as follows:

  1. Increment Operators: Increment Operators in C Language is again divided into two types i.e. pre-increment and post-increment.
  2. Decrement Operators: Decrement Operators in C Language are again divided into two types i.e. pre-decrement and post decrement.
How to use Unary Operators in C Programming Language?
  1. Pre increment: It is placed before the variable. For example, ++a will increase the value of the variable a by 1.
  2. Post increment: It is placed after the variable. For example, a++ will also increase the value of the variable a by 1.
  3. Pre decrement: It is placed before the variable. For example, –a will decrease the value of the variable a by 1.
  4. Post decrement: It is placed after the variable. For example, a– will also decrease the value of the variable a by 1.

For a better understanding of the types of Unary Operators and usage, please have a look at the following image.

How to use Unary Operators in C Programming Language?

Note: Increment Operator means to increment the value of the variable by 1 and Decrement Operator means to decrement the value of the variable by 1.

Now, the question is when should Pre-Increment and post-increment increases the value, and similarly when should Pre-decrement and post-decrement decreases the value. To understand this concept very clearly, we need to understand five simple steps. First, we will see the five steps, and then based on these steps we will see some examples to understand this concept clearly.

Five Steps to Understand the Unary Operators in C Language:

If there is some pre-increment or pre decrement in the expression, that should execute first. The second step is to substitute the values in the expression. Once we substitute the values, in the third step we need to evaluate the expression. Followed by the Evaluation, Assignment needs to be performed and the final step is post-increment or post decrement.

Five Steps to Understand the Unary Operators in C Language

Now, if you have any doubt about the above five steps, then don’t worry we will see some examples to understand this step in a better way.

Example: Pre-Increment

First, let us see a basic example that shows the basic difference between pre-increment and post-increment. Please have a look at the following program.

#include<stdio.h>
int main()
{
    int x = 10, y;
    y = ++x;
    printf("%d %d", x, y);
    return 0;
}

Let us understand the above program execution step by step by following the five steps. Here we are declaring two variables x=10 and y; First, the execution starts from the main method, and then the first statement executes i.e. int x = 10, y; Here, the variables x and y get memory allocation at some location and variable x is initialized with a value of 10 and variable y will store some garbage value why because we are not initializing y variable and as it is a local variable so, by default y is going to store some garbage value.

The next statement is expression evaluation and the statement is y = ++ x;. So, what is the first step? The first step is pre-increment and pre-decrement. Is there any pre-increment or pre-decrement in the expression? Yes, there is a pre-increment. So, execute the pre-increment i.e. ++x and this pre-increment will increase the variable x value by 1 and it becomes 11 and also this value is updated in the memory location of the x variable.

The next step is substitution. That is ++x will be substituted or replaced with 11. The third step is evaluation and, in our expression, there is nothing to evaluate, so ignore it. The fourth step is assignment i.e. assigned the value (11) to the left-hand side variable i.e. y. So, the garbage value stored initially in the y memory location will be replaced by 11. And the last step is post-increment and post-decrement. Is there any post-increment and post-decrement in the expression? No, so ignore it. So, five steps are completed means expression evaluation completed.

In the next statement, we are printing the value of x and y and it will print 11 and 11. So, when you run the above program, you will get the output as 11 and 11. For better understanding, please have a look at the below image.

Pre-Increment Operator Example in C Programming Language

Example: Post Increment

Consider the below program. This is the same as the previous example, the only difference here is that we use post-increment operator i.e. y = x++; instead of pre-increment.

#include<stdio.h>
int main()
{
    int x = 10, y;
    y = x++;
    printf("%d %d", x, y);
    return 0;
}

Let us understand the above program execution step by step by following the same five steps as we followed in our previous example. Here we are also declaring two variables x=10 and y; Here, the variables x and y get memory allocation at some location and variable x is initialized with a value of 10 and variable y will store some garbage value.

The next statement is expression evaluation and the statement is y = x++, And we need to evaluate this expression using the same five steps. So, what is the first step? The first step is pre-increment and pre-decrement. Is there any pre-increment or pre-decrement in the expression? No, so, ignore the first step.

The next step is substitution. That is x++ will be substituted or replaced with 10. The third step is evaluation and, in our expression, there is nothing to evaluate, so ignore it. The fourth step is assignment i.e. assigned the value (10) to the left-hand side variable i.e. y. So, the garbage value stored initially in the y will be replaced by 10. And the last step is post-increment and post-decrement. Is there any post-increment and post-decrement in the expression? Yes, there is post-increment. So, execute the post-increment i.e. x++ and this post-increment will increase the variable x value by 1 and it becomes 11 and also this value is updated in the memory location of the x variable. All five steps are completed. Yes, that means the expression evaluation is completed.

In the next statement, we are printing the value of x and y and it will print 11 and 10. So, when you run the above program, you will get the output as 11 and 10.

So, the simple difference between pre-increment/pre-decrement and post-increment/post-decrement is that pre-increment/pre-decrement execute first and post-increment/post-decrement execute last and that is the only difference.

Complex Examples of Increment and Decrement Operators in C Language:

Let us see some more complex examples to understand this concept. Please have a look at the following example. Here, we are declaring three variables x, y, and z and then evaluating the expression as z = x++ * –y; and finally, we are printing the value of x, y, and z in the console.

#include<stdio.h>
int main()
{
    int x = 10, y=20, z;
    z = x++ * --y;
    printf("%d %d %d", x, y, z);
    return 0;
}

Let us evaluate the expression z = x++ * –y; by following the 5 steps:

  1. The First step is pre-increment or pre-decrement. Is there any pre-increment or pre-decrement in the expression? There is no pre-increment but there is a pre-decrement in the expression i.e. –y. So, execute that pre-decrement which will decrease the value of y by 1 i.e. y becomes 19.
  2. The second step is substitution. So, substitute the values of x and y. That means x will be substituted by 10 and y will be substituted by 19.
  3. The third step is evaluation. So, evaluate the expression i.e. 10 * 19 = 190.
  4. The fourth step is the assignment. So, assign the evaluated value to the given variable i.e. 190 will be assigned to z. So, now the z value becomes 190.
  5. The last step is post-increment and post-decrement. Is there any post-increment or post-decrement in the expression? There is no post-decrement but there is a post-increment in the expression i.e. y++. So, execute that post-increment which will increase the value of x by 1 i.e. x becomes 11.

So, when you will execute the above program it will print the x, y, and z values as 11, 19, and 190 respectively. For, better understanding, please have a look at the below image.

Complex Examples of Increment and Decrement Operators in C Language

Example:

Consider the below example. Here, we are declaring a variable x and initializing it with a value of 5 and then evaluating the expression as x = ++x + x++; and finally, we are printing the value of x in the console.

#include<stdio.h>
int main()
{
    int x = 5;
    x = ++x + x++;
    printf("%d", x);
    return 0;
}

The expression x = ++x + x++; will be evaluated based on below 5 steps:

  1. The First step is pre-increment and pre-decrement. Is there any pre-increment or pre-decrement in the expression? There is no pre-decrement but there is a pre-increment in the expression i.e. ++x. So, execute that pre-increment which will decrease the value of x by 1 i.e. x becomes 6.
  2. The second step is substitution. So, substitute the values of x. That means x will be substituted by 6.
  3. The third step is evaluation. So, evaluate the expression i.e. 6 + 6 = 12.
  4. The fourth step is the assignment. So, assign the evaluated value to the given variable i.e. 12 will be assigned to x. So, now the x value becomes 12.
  5. The last step is post-increment and post-decrement. Is there any post-increment or post-decrement in the expression? There is no post-decrement but there is a post-increment in the expression i.e. x++. So, execute that post-increment which will increase the value of x by 1 i.e. x becomes 13.

So, when you will execute the above program, it will print the x value as 13. For, better understanding, please have a look at the below image.

Unary Operators in C Programming Language

Note: The pre-increment operator and the + operators have equal precedence. That means which one is done first is completely compiler-dependent. This is called undefined behavior and these types of statements should be avoided at all costs.

Example:

Consider the below example. Here, we are declaring two variables a and a, and initializing variables a and a with the values 2 and 3 respectively. Then we have three expressions to evaluate the value of a and b and finally, we are printing the value of a and b on the console.

#include<stdio.h>
int main()
{
    int a = 2, b = 3;
    b = a++ + b--;
    a = a-- + ++b;
    b = ++a + --b;
    printf("%d %d", a, b);
    return 0;
}

All three expressions will be evaluated using the same 5 steps:

Expression1: b = a++ + b–;
  1. The First step is pre-increment and pre-decrement and there is no pre-increment and pre-decrement in the expression. So, ignore this step.
  2. The second step is substitution. So, substitute the values of a and b. That means a and b will be substituted by 2 and 3 respectively.
  3. The third step is evaluation. So, evaluate the expression i.e. 2 + 3 = 5.
  4. The fourth step is the assignment. So, assign the evaluated value to the given variable i.e. 5 will be assigned to b. So, now the b value becomes 5.
  5. The last step is post-increment and post-decrement. Is there any post-increment or post-decrement in the expression? Yes, both are present. So, execute the post-increment i.e. a++ and post-decrement i.e. b– which will increase the a value by 1 i.e. a becomes 3, and decrease the b value by 1 i.e. b becomes 4.

So, at the end of this expression, the latest a value is 3, and the b value is 4.

Expression2: a = a– + ++b;
  1. The First step is pre-increment and pre-decrement and there is one pre-increment in the expression i.e. ++b. So, execute the pre-increment which will increase the b value by 1 i.e. b becomes 5.
  2. The second step is substitution. So, substitute the values of a and b. That means a and b will be substituted by 3 and 5 respectively.
  3. The third step is evaluation. So, evaluate the expression i.e. 3 + 5 = 8.
  4. The fourth step is the assignment. So, assign the evaluated value to the given variable i.e. 8 will be assigned to a. So, now a value becomes 8.
  5. The last step is post-increment and post-decrement. Is there any post-increment or post-decrement in the expression? Yes, Post-decrement is there. So, execute the post-decrement i.e. a– which will decrease the a value by 1 i.e. a becomes 7.

So, at the end of this expression, the latest a value is 7, and the b value is 5.

Expression3: b = ++a + –b;
  1. The First step is pre-increment and pre-decrement and both pre-increment (++a) and pre-decrement (–b) is present in the expression. So, execute the pre-increment which will increase a value by 1 i.e. a becomes 8. Similarly, execute the pre-decrement which will decrease b value by 1 i.e. b becomes 4
  2. The second step is substitution. So, substitute the values of a and b. That means a and b will be substituted by 8 and 4 respectively.
  3. The third step is evaluation. So, evaluate the expression i.e. 8 + 4 = 12.
  4. The fourth step is the assignment. So, assign the evaluated value to the given variable i.e. 12 will be assigned to b. So, now the b value becomes 12.
  5. The last step is post-increment and post-decrement. Is there any post-increment or post-decrement in the expression? No, so ignore this step.

So, at the end of this expression, the latest a value is 8 and b value is 12 and that you will see when you run the above program.

As already discussed, the pre-increment (++var) operator and the + operators have equal precedence. So, which operator will execute first will completely depend on the compiler. This is called undefined behavior and these types of statements should be avoided at all costs. So, when you execute the above program, based on your compiler you may get a different output.

Example:

Consider the below example.

#include<stdio.h>
int main()
{
    int a = 5;
    a = a+++a;
    printf("%d", a);
    return 0;
}

The Unary operator has a priority greater than the arithmetic operator, so the compiler will execute the unary operator first. The Compiler will give priority to the unary operator because it has higher priority, that is why we will get output as 11. For better understanding, please have a look at the below diagram.

Unary Operator in C Programming Language with examples

In the next article, I am going to discuss Variables in C Language with Examples. Here, in this article, I try to explain Unary Operator in C Programming Language with examples and I hope you enjoy this Unary Operator in C Language article. Please give your feedback, suggestions about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *