Unary Operators in C

Unary Operator in C Programming Language

In this article, we will 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, which discusses the basics of Operators in C Language. 

Unary Operator in C Programming Language

A 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 are 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 do you use Unary Operators in C Programming Language?
  1. Pre-incentment: It is placed before the variable. For example, ++a will increase the value of variable a by 1.
  2. Post-increment: It is placed after the variable. For example, a++ will also increase the variable a’s value by 1.
  3. Pre-decrement: It is placed before the variable. For example, –a will decrease the value of variable a by 1.
  4. Post-decrement: It is placed after the variable. For example, a– will also decrease the value of 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 increase the value, and when should Pre-decrement and post-decrement decrease 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 be executed 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. After the evaluation, an assignment needs to be performed; the final step is post-increment or post-decrement.

Five Steps to Understand the Unary Operators in C Language

Now, if you have any doubts about the above five steps, then don’t worry. We will see some examples to understand these steps better.

Example: Pre-Increment

First, let us see a basic example showing the difference between pre-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 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, variable x is initialized with a value of 10, and variable y will store some garbage value because we are not initializing the y variable, and as it is a local variable.

The next statement is expression evaluation, which 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, increasing the variable x value by 1.

The next step is substitution. That is, ++x will be substituted or replaced with 11. The third step is evaluation; there is nothing to evaluate in our expression, so we should ignore it. The fourth step is the assignment, i.e., assigning 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 is completed.

In the next statement, we are printing the value of x and y, which will print 11 and 11. So, when you run the above program, you will get the output as 11 and 11. For a 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 the 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 five steps 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; variable x is initialized with a value of 10, and variable y will store some garbage value.

The next statement is expression evaluation, which is y = x++. 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; there is nothing to evaluate in our expression, so we should ignore it. The fourth step is assignment i.e., assigning 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. 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 x and y values, which will print 11 and 10, respectively. So, when you run the above program, you will get the outputs 11 and 10.

So, the simple difference between pre-increment/pre-decrement and post-increment/post-decrement is that pre-increment/pre-decrement executes first and post-increment/post-decrement executes 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 declare three variables, x, y, and z, and then evaluate the expression as z = x++ * –y; finally, we print 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 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 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, executing the above program will print the x, y, and z values as 11, 19, and 190, respectively. For a better understanding, please look at the image below.

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++; 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 the 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 a pre-increment in the expression, i.e., ++x. So, execute that pre-increment, which will increase 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 execute the above program, it will print the x value as 13. For a 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 statements should be avoided at all costs.

Example:

Consider the below example. Here, we declare two variables, a and b, and initialize variables a and b with the values 2 and 3, respectively. Then, we have three expressions to evaluate the value of a and b, and finally, we print 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 five steps:

Expression1: b = a++ + b–;
  1. The First step is pre-increment and pre-decrement; 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++, which will increase the a value by 1, i.e. a becomes 3, and post-decrement i.e. b–, which will 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; both pre-increment (++a) and pre-decrement (–b) are 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 the b value is 12, and you will see when you run the above program.

As already discussed, the pre-increment (++) and + operators have equal precedence. So, which operator will execute first will completely depend on the compiler. This is called undefined behavior, and these 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 prioritize the unary operator because it has higher priority. That is why we will get output as 11. For a better understanding, please have a look at the below diagram.

Unary Operator in C Programming Language with examples

In the next article, I will discuss Unary vs. Binary vs. Ternary Operators in C with Examples. In this article, I try to explain the Unary Operator in C Programming Language with examples, and I hope you enjoy this Unary Operator in C Language article. Please give your feedback and suggestions about this article.

Leave a Reply

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