Unary Operators in C
Here are the implementation of Unary Operator in C language:
#include <stdio.h>
int main()
{
int a = 10;
int b = 5;
// Unary increment (++)
// Increment 'a' by 1 before using its value
printf("Unary Increment: %d\n", ++a);
// 'a' has been incremented
printf("a after increment: %d\n", a);
// Unary decrement (--)
// Decrement 'b' by 1 before using its value
printf("Unary Decrement: %d\n", --b);
// 'b' has been decremented
printf("b after decrement: %d\n", b);
// Unary plus (+)
int c = -5;
// The unary plus doesn't change the value of 'c'
printf("Unary Plus: %d\n", +c);
// Unary minus (-)
// The unary minus negates the value of 'c'
printf("Unary Minus: %d\n", -c);
// Unary logical NOT (!)
int d = 0;
// Logical NOT of 0 is 1 (true)
printf("Unary Logical NOT: %d\n", !d);
// Unary bitwise NOT (~)
unsigned int e = 1;
// Bitwise NOT of 1 is UINT_MAX - 1
printf("Unary Bitwise NOT: %u\n", ~e);
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