January 19, 2022

Decrypt Info

Because we care, we're security aware.

2022: Don’t you know about Unary Operators in C/C++? Amazing Examples of Unary Operators.

0 0
2 min read
code_decryptinfo
0 0
Read Time:3 Minute, 25 Second

Unary operators are those operators that act upon a single operand to produce a new value. Unary operators we have

  1.  ++ (Post Increment). The operator follows the operand (e.g., a++). The value operand will be altered after it is used.
  2. (Pre Increment).  The operator precedes the operand (e.g., – -a). The value of operand will be altered before it is used.
  3.  + Unary plus operator; indicates positive value
  4.   Unary minus operator; negates an expression. The minus operator changes the sign of its argument. A positive number becomes negative, and a negative number becomes positive.
  5.  !  Logical complement operator; inverts the value of a boolean. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. 

Example 1: 

int i = 10;
int j = i++ + i++ ;
SOP(i); 12
SOP(j); 21

Don’t Miss: Top 8 Search engines for Hackers

Example 2:

int i = 10;
int j = i++ ;
SOP(i); 11
SOP(j); 10

Example 3:

int i = 10;
int j = ++i + ++i ;
SOP(i); 12
SOP(j); 23

Don’t Miss: AWS Certified Advanced Networking Multiple Choice Ques

Example 4:

int i = 10;
int j = ++i + ++i + ++i + i;
SOP(i);13
SOP(j);49

Example 5:

int i = 10;
int j = ++i + i++;
SOP(i); 12
SOP(j); 21

Don’t Miss: [Windows] How To Enable Hibernate Option in Windows 10 with CMD

Example 6:

int i = 10;
int j = i++ + ++i + i++ + ++i;
SOP(i); 14
SOP(j); //

Example 7:

int i = 10;
int j = i-- + i --;
SOP(i); 8
SOP(j); 19

Example 8:

int i = 10;
int j = --i + i-- ;
SOP(i); 8
SOP(j); 18

Don’t Miss: What is Proxy Server? How to install Paros Proxy Server?

Example 9:

int i = 10;
int j = i++ + i-- + --i + ++i;
SOP(i); 10
SOP(j); 40

Example 10:

int i = 10;
int j = i++ + -++i + -i++;
SOP(i); 13
SOP(j); -14

Example 11:

int i = 10;
int j = -++i - ---i + i++ - -++i;
SOP(i);
SOP(j);

The above program will give us an error, because in unary operator concept you can never use — / +++ together

Don’t Miss: Top 30 Networking Commands for Linux Administrators

Example 12:

int i = 10;
int j = -++i + -i-- + i++ - -++i;
System.out.println(i);
System.out.println(j);

Output:
12
0

Example 13:

int i = 10;
SOP(i++); 10++ //10
SOP(i--); 11-- //11
SOP(++i); ++10 //11
OP(--i); --11 //10
SOP(i); 10

C++ program for mixture of prefix(–) and postfix(++) operations:

// C++ program to demonstrate working of unary increment and decrement operators

#include <iostream>
using namespace std;

int main()
{
// Post increment
int a = 1;
cout << "a value: " << a << endl;
int b = a++;
cout << "b value after a++ : " << b << endl;
cout << "a value after a++ : " << a << endl;

// Pre increment
a = 1;
cout << "a value:" << a << endl;
b = ++a;
cout << "b value after ++a : " << b << endl;
cout << "a value after ++a : "<< a << endl;

// Post decrement
a = 5;
cout << "a value before decrement: " << a << endl;
b = a--;
cout << "b value after a-- : " << b << endl;
cout << "a value after a-- : " << a << endl;

// Pre decrement
a = 5;
cout << "a value: "<< a<<endl;
b = --a;
cout << "b value after --a : " << b << endl;
cout << "a value after --a : " << a << endl;

return 0;
}

Don’t Miss: How To Remove Shortcut Virus From Your Computer

Output:

a value: 1
b value after a++ : 1
a value after a++ : 2
a value:1
b value after ++a : 2
a value after ++a : 2
a value before decrement: 5
b value after a-- : 5
a value after a-- : 4
a value: 5
b value after --a : 4
a value after --a : 4

 

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply