0% found this document useful (0 votes)
12 views9 pages

Unary Operator

The document explains unary operators in C programming, which operate on a single operand to return a new value. It details various types of unary operators including Unary Minus, Unary Plus, Increment, Decrement, Logical Negation, Address Operator, and Sizeof() operator, along with their syntax and examples. Additionally, it describes the functionality of each operator and provides code snippets to illustrate their usage.

Uploaded by

Jia Bola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

Unary Operator

The document explains unary operators in C programming, which operate on a single operand to return a new value. It details various types of unary operators including Unary Minus, Unary Plus, Increment, Decrement, Logical Negation, Address Operator, and Sizeof() operator, along with their syntax and examples. Additionally, it describes the functionality of each operator and provides code snippets to illustrate their usage.

Uploaded by

Jia Bola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

UNARY OPERATOR:

1. A unary operator is an operator used to operate on a single operand to return a new


value.
2. In other words, it is an operator that updates the value of an operand or expression's
value by using the appropriate unary operators.

3.In Unary Operator, operators have equal priority from right to left side associativity.

Types of the Unary Operator


Following are the types of the unary operators in the C programming language.

1. Unary Minus (-)


2. Unary Plus (+)
3. Increment (++)
4. Decrement (--)
5. Logical Negation (!)
6. Address Operator (&)
7. Sizeof() operator

Unary Minus (-)


The Unary Minus operator is represented using the symbol (-). The unary
operator is used to change the sign of any positive value to a negative value. It
means it changes the positive number to the negative, and a negative number
becomes the positive number using the unary minus operator.

Syntax

1. int a = 2;
2. int b = -(a);

Example:

#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 represented as the "+" symbol, and it does not change to
the operand value.

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 is the unary increment operator, which is denoted by the "++" symbol.

The "++" symbol represents the operand's value is increased by 1.

It can be used in two ways, as the post-increment and the pre-increment.

Pre Increment:
The pre-increment operator is represented as (++a), which means the value of variable
'a' is increment by 1 before using operand to the expression.

For example:

x = 10;
A = ++x;

Post Increment:
The (a++) symbol represents the post-increment operator, which means the value of
'a' is incremented by 1 after assigning the original value to the expression or another
variable.

For example:

x = 10;
A = x++;
EXAMPLE:

#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.

Backward Skip 10sPlay Video


Unary Decrement Operator (--)
The unary decrement operator is opposite to the unary increment operator.

The Unary decrement operator is represented by the double minus (--) symbol, and it is
used to decrease the operand value by 1 according to the decrement's types.

The Unary decrement operator is of two types: the Pre decrement operator and the Post
Decrement operator.

Pre Decrement: The pre decrement operator is denoted as (--a) symbol, meaning the
operand value is decreased by 1 before assigning to another variable or expression.

Syntax

int pre = --a;

Post Decrement: The Post decrement operator is denoted as (a--) symbol, which means the
original value is decreased by 1 after assigning to another variable or expression.

Syntax

int post = a--;

#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 is a keyword used to find the size of different data types or operands like int,
float, char, double, etc.

Syntax;

sizeof(data_variable);

EXAMPLE:
#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 used to reverse the given condition. For example, if the operand
is true, the logical not operator (!) reverses and return false; if the operand is false, the
logical operator returns 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 denoted as ampersand (&) symbol, which is used to find
the address of a variable defined in computer memory.

Syntax

int a = 5;
int b = &a; // variable b hold the address of variable a

Example :

#include <stdio.h>
#include <conio.h>
int main ()
{
// declare variables
int a = 10;
int b;
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

You might also like