Unary Operators in C++
Here are the implementation of Unary Operator in C++ language:
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 5;
// Unary increment (++)
// Increment 'a' by 1 before using its value
cout << "Unary Increment: " << ++a << endl;
// 'a' has been incremented
cout << "a after increment: " << a << endl;
// Unary decrement (--)
// Decrement 'b' by 1 before using its value
cout << "Unary Decrement: " << --b << endl;
// 'b' has been decremented
cout << "b after decrement: " << b << endl;
// Unary plus (+)
int c = -5;
// The unary plus doesn't change the value of 'c'
cout << "Unary Plus: " << +c << endl;
// Unary minus (-)
// The unary minus negates the value of 'c'
cout << "Unary Minus: " << -c << endl;
// Unary logical NOT (!)
bool d = false;
// Logical NOT of false is true
cout << "Unary Logical NOT: " << !d << endl;
// Unary bitwise NOT (~)
unsigned int e = 1;
// Bitwise NOT of 1 is UINT_MAX - 1
cout << "Unary Bitwise NOT: " << ~e << endl;
return 0;
}
Output
Unary Increment: 11 a after increment: 11 Unary Decrement: 4 b after decrement: 4 Unary Plus: -5 Unary Minus: 5 Unary Logical NOT: 1 Unary Bitwise NOT: 4294967294
Unary Operators in Programming
In programming, operators act as powerful tools for manipulating data and performing various operations. Among these, unary operators stand out, functioning on a single operand to transform or evaluate data in different ways. This post explains the types, implementations, and best practices associated with unary operators across several programming languages.
Table of Content
- What are Unary Operators?
- Types of Unary Operators
- Unary Operators in C
- Unary Operators in C++
- Unary Operators in Java
- Unary Operators in Python
- Unary Operators in C#
- Unary Operators in JavaScript
- Examples and Use Cases of Unary Operator
- Best Practices of Unary Operator
Contact Us