C Programming

Unary Operators in C

Unary operators are those operators who have done their work to only one operand. A binary operator needs two operands. Ternary operator needs three operands. + And – operator denotes only the sign. For example: +5, -7. These denote only the sign of the number. ++ And — are called increment and decrement operators, respectively.

Programming Example 1

This programming example will show how a unary operator named post-increment operator operates on a value.

#include <stdio.h>

int main ()
{
int x=3 ;       // declaring and initializing a variable
x++;            // post increment operator operates
printf("%d", x);
return 0 ;
}

Output

Explanation

Here we declare a variable x. Initialization is done at the time of declaration of variable x. 3 is assigned to the variable. Now we have done x++. Applying the post-increment operator on the variable x. So the value of x is incremented by 1, and the value of x is 4.

Programming Example 2

This programming example will show how unary operators named post-increment and pre-increment operators operate on a value.

#include <stdio.h>

int main ()
{
    int x = 3;
    x ++;
printf ("%d",x);        //post increment (lowest priority)

printf ("\n");
    ++ x;           //pre increment (Highest priority)

printf("%d",x);
printf("\n");
return 0 ;
}

Output

Note: Post Increment priority is the least priority among all the operators, even assignment operators in C Language.

Explanation

Here we declare a variable x, and 3 is assigned to it. Now we apply the post-increment unary operator on the variable x. As we have done x++, so the value of x is incremented by 1. So the first output of the program is 4.

Then we will apply the pre-increment operator on the same variable, x. As we have done ++x, so the value of x is incremented by 1. So the second output of the program is 5.

Programming Example 3

 

This programming example shows how increment and assignment operators work together on a given expression.

#include <stdio.h>

int main ()

{
    int x =3, y;
    y= x++;
printf ("%d, %d \n", x, y);
return 0 ;

}

Output

Explanation

y = x ++;

Here two operators are present. They are Assignment operators and post-increment operators. As, Assignment operators are a larger priority than the post-increment operator. So, Assignment Operator executes at first. So, the value of “y= 3”. Then the post-increment operator works in the expression. Then the value of x is incremented by 1. So, “x = 4”.

Programming Example 4

#include <stdio.h>

int main ()

{
    int x =3, y;
    y= ++x;
printf("%d, %d \n", x, y);
return 0 ;
}

Output

Explanation

y= ++ x;

In the above programming example expression, two operators are present. One is Pre increment operator, and another is the assignment operator. The pre-increment operator has a higher priority than the assignment operator, so the pre-increment operator executes first. The value of x is incremented by one. So the output of the x is 4.

Now, this value of x is assigned to y with the help of the assignment operator. So the value of y is now 4. Both the output of this program is 4.

Programming Example 5

In this programming example, we will learn about the usefulness of pre decrement operator.

#include <stdio.h>

int main ()
{
Int p, q, x, y;
    x = 10;
    p = --x;

printf(" Pre Decrement Operator");
printf(" \n The value of p is %d.", p);
printf(" \n The value of x is %d.", x);
    y = 20;
    q = y--;
printf(" \n\n Post Decrement Operator");
printf(" \n The value of q is %d.", q);
printf (" \n The value of y is %d. \n", y);
return 0 ;
}

Output

Explanation

Here the value of x is 10. Now an expression is given in the program. P=–x;

It means in this expression, both pre decrement operator and assignment operator are present together. As pre decrement operator has a higher priority than the assignment operator. Pre decrement operator executes first. The value of x is decremented by 1 and gets 9. This 9 is assigned to the variable p with the help of the assignment operator.

In the next phase of the program, the expression was q = y–. And the value of y is 20.

It means in this expression, both post decrement operator and assignment operator are present together. As post decrement operator has a higher priority than the assignment operator. Post decrement operator executes first. The value of x is decremented by 1 and gets 19. This 19 is assigned to the variable q with the help of the assignment operator.

Programming Example 6

In this programming example, we will learn about the usefulness of another unary operator ( – ) operator.

#include <stdio.h>

int main ()
{
int a = 20;
int b = -(a);
int x = 12;
int y = -42;
printf(" value of a is : %d \n", a);
printf(" value of b is : %d \n", b);
printf(" value of x is : %d \n", -x);
printf(" value of y is  %d \n", -y);

return 0;
}

Output

Explanation

Here we use another expression int b = -( a ) ;

In this expression, we use minus unary operator and assignment operators. This unary operator turns the value of a into a negative value and then assigns this value to the variable b. So the value of variable a = 20 and value of b = -20.

Another expression which is used here is int y = -42;

The same mechanism is followed here as the above expression.

Programming Example 7

Here we use another important unary operator. This operator is called the sizeof() operator. Now we will learn about the sizeof() operator.

#include<stdio.h>

int main ()
{
    int x ;
printf( " size of x = %d \n ",sizeof(x) ) ; // uses of sizeof() operator.
    return 0 ;
}

Output

C:\Users\RAJ\Desktop\94d2778f-ba41-4f00-bdd3-826c869f14d3.jpg

Explanation

In this programming example, we declare a variable x, which is an integer type, and assign a value 4 inside it. Now we want to know the size of the variable x; we simply use sizeof() operator. We get an output size of x = 4.

Conclusion

We have covered all the unary operators in a very simple manner. From this discussion about the unary operators, we concluded that unary operators are an important element in managing different types of mathematical data or operands in our C language.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com