Home » C Popular & Interesting Questions

# What is the difference between **Unary** and **Binary** Operators in C and C++?

## Difference between Unary and Binary Operators in C, C++

**Difference b/w Unary and Binary Operators** - In this section you will learn about **Unary and Binary Operators**. What are they and why they used and how to use these operators in our C and C++ programs?

#### What is an Operator?

**Operator is a symbol or special character which is used to perform a specific task**, the task/meaning of operator is defined in the compiler. For example + is a operator which is used to add two values.

#### What are Unary Operators?

**The Operators which operate on Single Operand known as Unary Operators**, some of the unary operators are:

++ Increment Operator -- Decrement Operator & Address Of Operator - Unary Minus Operators ~ (One’s Compliment) Negation Operator ! Logical NOT and so on..

**Let’s understand by example:**

#include <stdio.h> int main() { int x=10; int y; y=(-10); printf("x=%d, y=%d\n",x,y); printf("Address of x: %08X\n",&x); return 0; }

**Output**

x=10, y=-10 Address of x: 8C668378

In the given example we are using **two operations based on Unary operator**:

(-10) here - is a **Unary Minus operator** which is operating an action with 10 and value of y will be -10.

&x here & is a **Unary Address of Operator** which is returning the **address** of variable x.

#### What are Binary Operators?

**The Operators which operate on Two Operands known as Binary Operators**, some of the binary operators are:

+ Binary Plus Operator - Binary Minus Operator == Equal to Operator < Less than Operator and so on..

**Let’s understand by example:**

#include <stdio.h> int main() { int x,y; int res1,res2; x=10; y=20; res1=x+y; res2=(x==y); printf("res1: %d, res2: %d\n",res1,res2); return 0; }

**Output**

res1: 30, res2: 0

In the given example we are using **two operations based on Binary operator**:

x+y here + is a **Binary Plus Operator** which is adding values of x and y and return 30 in res1 variable.

x==y here == is a **Binary Equal to Operator** which is comparing values of x and y and return 0 in res2 variable because values of x and y are not same.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.

Learn PCB Designing: PCB DESIGNING TUTORIAL