Types of the Unary Operator
Following are the types of the unary operators in the C programming language.
- Unary Minus (-)
- Unary Plus (+)
- Increment (++)
- Decrement (--)
- Logical Negation (!)
- Address Operator (&)
- Sizeof operator
Unary Minus (-)
The symbol (-) represents the Unary Minus operator in programming. This operator is employed to convert a positive value into a negative one by changing its sign. Essentially, it transforms a positive number into a negative one, while also converting a negative number into a positive one through the unary minus operator.
Syntax
int a = 2;
int b = -(a);
Example 1:
#include <stdio.h>
#include <conio.h>
int main ()
{
int a = 5; // positive value of a.
int b = -(a); // use unary minus operator to change the value
int n1 = 20;
int n2 = -30;
printf (" The value of a: %d \n", a);
printf (" The value of b: %d \n", b);
printf (" The value of -n1: %d \n", -n1);
printf (" The value of -n2: %d ", -n2);
return 0;
}
Output
The value of a: 5
The value of b: -5
The value of -n1: -20
The value of -n2: 30
Unary plus (+)
The unary plus operator is denoted by the symbol "+", and it preserves the value of the operand.
Example 2:
#include <stdio.h>
#include <conio.h>
int main ()
{
int a = 10; // use unary plus operator
int b = (-10); // It does not change the operand value
printf (" The value of a: %d \n", a);
printf (" The value of b: %d \n", b);
return 0;
}
Output
The value of a: 10
The value of b: -10
Unary Increment Operator (++)
It represents the unary increment operator, indicated by the "++" symbol, which increments the value of the operand by 1. This operator can function in two manners: post-increment and pre-increment.
Pre Increment: The pre-increment operator is denoted as (++a) and it increments the value of the variable 'a' by 1 before it is used in the expression.
For example:
x = 10;
A = ++x;
The starting value of x is 10, and by employing the pre-increment operator (++x), the operand value increments by 1 before being assigned to the variable 'A'.
The symbol (a++) denotes the post-increment operator, where 'a' is increased by 1 after assigning its original value to the expression or a different variable.
For example:
x = 10;
A = x++;
Here, the initial value of the x variable is 10, and the post-increment operator (x++) is employed to assign the incremented value of 'x' to the variable 'A'.
Example 3:
#include <stdio.h>
#include <conio.h>
int main ()
{
int x, y, a, b; // declare local variable
a = 10;
x = ++a; // It shows pre increment operator
printf (" Pre Increment Operator");
// Here the value of x is increased by 1.
printf (" \n The value of x is %d.", x);
printf (" \n The value of a is %d.", a);
b = 20;
y = b++; // It shows the post increment operator
printf (" \n\n Post Increment Operator");
printf (" \n The value of y is %d.", y);
// get updated value of b
printf (" \n The value of b is %d.", b);
return 0;
}
Output
Pre Increment Operator
The value of x is 11.
The value of a is 11.
Post Increment Operator
The value of y is 20.
The value of b is 21.
Unary Decrement Operator (--)
The unary decrement operator functions in an opposing manner to the unary increment operator. It is denoted by the double minus (--) symbol and is applied to reduce the value of the operand by 1 based on the type of decrement operation. There are two variations of the unary decrement operator: the Pre-decrement operator and the Post-decrement operator.
Pre Decrement : The pre decrement operator is represented by the symbol (--a), indicating that the value of the operand is reduced by 1 before being assigned to another variable or expression.
Syntax
int pre = --a;
The Post decrement operator is represented by the (a--) symbol. It signifies that the initial value is reduced by 1 after being assigned to a different variable or expression.
Syntax
int post = a--;
Example 4:
#include <stdio.h>
#include <conio.h>
int main ()
{
int x, y, a, b; // declare local variable
a = 10;
x = --a; // It shows pre decrement operator
printf (" Pre Decrement Operator");
// Here the value of x is decreased by 1.
printf (" \n The value of x is %d.", x);
printf (" \n The value of a is %d.", a);
b = 20;
y = b--; // It shows the post decrement operator
printf (" \n\n Post Decrement Operator");
printf (" \n The value of y is %d.", y);
// get updated value of b
printf (" \n The value of b is %d.", b);
return 0;
}
Output
Pre Decrement Operator
The value of x is 9.
The value of a is 9.
Post Decrement Operator
The value of y is 20.
The value of b is 19.
Unary Sizeof Operator
The sizeof keyword is utilized to determine the size of various data types or operands such as integers, floating-point numbers, characters, doubles, and so forth.
Syntax
sizeof(data_variable);
Example 5:
#include <stdio.h>
#include <conio.h>
int main ()
{
// declaration of different types of data variables
int x;
float y;
char ch;
double z;
// use sizeof() operator and pass the different data type variable to get their size.
printf (" The size of the int (x) variable is: %d", sizeof(x));
printf (" \n The size of the float (y) variable is: %d", sizeof(y));
printf (" \n The size of the char (ch) variable is: %d", sizeof(ch));
printf (" \n The size of the double (z) variable is: %d", sizeof(z));
return 0;
}
Output
The size of the int (x) variable is: 4
The size of the float (y) variable is: 4
The size of the char (ch) variable is: 1
The size of the double (z) variable is: 8
Logical Not (!) Operator
The logical NOT operator is employed to invert the provided condition. For instance, when the operand is true, the logical NOT operator (!) flips it to false; conversely, if the operand is false, the logical operator yields true.
Syntax
bool a = true;
bool b = !a; // It reverse the condition of variable b
Example 6:
#include <stdio.h>
#include <stdbool.h>
int main ()
{
// declare variables
bool a = true;
bool b;
b = !a; // use logical operator to reverse the condition
printf (" The Boolean value of a is: %d", a);
printf (" \n The Boolean value of b is: %d", b);
bool c = 0;
bool d = !c;
printf (" \n The Boolean value of c is: %d", c);
printf (" \n The Boolean value of d is: %d", d);
return 0;
}
Output
The Boolean value of a is: 1
The Boolean value of b is: 0
The Boolean value of c is: 0
The Boolean value of d is: 1
AddressOf Operator (&)
The Unary AddressOf Operator is represented by the ampersand (&) symbol, and it is employed to determine the memory location of a variable stored in the computer's memory.
Syntax
int a = 5;
int b = &a; // variable b hold the address of variable a
Example 7:
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare variables
int a = 10;
int b;
// use addressof (&) operator to assign the address
b = &a;
printf (" The value of variable a is: %d", a);
printf (" \n The address of variable b is: %d", b);
return 0;
}
Output
The value of variable a is: 10
The address of variable b is: 6487704