Unary operators in C/C++ - GeeksforGeeks

19 Oct, 2021
Unary operator: are operators that act upon a single operand to produce a new value.
Types of unary operators:
unary minus(-)increment(++)decrement(- -)NOT(!)Addressof operator(&)sizeof()
unary minus(-)
increment(++)
decrement(- -)
NOT(!)
Addressof operator(&)
sizeof()
unary minusThe minus operator changes the sign of its argument. A positive number becomes negative, and a negative number becomes positive. int a = 10;
 int b = -a;  // b = -10
unary minus is different from subtraction operator, as subtraction requires two operands.incrementIt is used to increment the value of the variable by 1. The increment can be done in two ways:prefix incrementIn this method, the operator precedes the operand (e.g., ++a). The value of operand will be altered before it is used.  int a = 1;
  int b = ++a;  // b = 2
postfix incrementIn this method, the operator follows the operand (e.g., a++). The value operand will be altered after it is used. int a = 1;
 int b = a++;   // b = 1
 int c = a;     // c = 2
decrementIt is used to decrement the value of the variable by 1. The decrement can be done in two ways:prefix decrementIn this method, the operator precedes the operand (e.g., – -a). The value of operand will be altered before it is used.  int a = 1;
  int b = --a;  // b = 0
postfix decrementIn this method, the operator follows the operand (e.g., a- -). The value of operand will be altered after it is used. int a = 1;
 int b = a--;   // b = 1
 int c = a;     // c = 0
C++ program for combination 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;}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
The above program shows how the postfix and prefix works.NOT(!): It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.   If x is true, then !x is false
   If x is false, then !x is true
Addressof operator(&): It gives an address of a variable. It is used to return the memory address of a variable. These addresses returned by the address-of operator are known as pointers because they “point” to the variable in memory.& gives an address on variable n
int a;
int *ptr;
ptr = &a; // address of a is copied to the location ptr. 
sizeof(): This operator returns the size of its operand, in bytes. The sizeof operator always precedes its operand.The operand is an expression, or it may be a cast.#include <iostream>using namespace std;  int main(){   float n = 0;   cout << "size of n: " << sizeof(n);   return 1;}Output:size of n: 4
This article is contributed by I. HARISH KUMAR. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.My Personal Notes
arrow_drop_upSave
unary minusThe minus operator changes the sign of its argument. A positive number becomes negative, and a negative number becomes positive. int a = 10;
 int b = -a;  // b = -10
unary minus is different from subtraction operator, as subtraction requires two operands.
 int a = 10;
 int b = -a;  // b = -10

unary minus is different from subtraction operator, as subtraction requires two operands.
incrementIt is used to increment the value of the variable by 1. The increment can be done in two ways:prefix incrementIn this method, the operator precedes the operand (e.g., ++a). The value of operand will be altered before it is used.  int a = 1;
  int b = ++a;  // b = 2
postfix incrementIn this method, the operator follows the operand (e.g., a++). The value operand will be altered after it is used. int a = 1;
 int b = a++;   // b = 1
 int c = a;     // c = 2

prefix incrementIn this method, the operator precedes the operand (e.g., ++a). The value of operand will be altered before it is used.  int a = 1;
  int b = ++a;  // b = 2
postfix incrementIn this method, the operator follows the operand (e.g., a++). The value operand will be altered after it is used. int a = 1;
 int b = a++;   // b = 1
 int c = a;     // c = 2

prefix incrementIn this method, the operator precedes the operand (e.g., ++a). The value of operand will be altered before it is used.  int a = 1;
  int b = ++a;  // b = 2

  int a = 1;
  int b = ++a;  // b = 2

postfix incrementIn this method, the operator follows the operand (e.g., a++). The value operand will be altered after it is used. int a = 1;
 int b = a++;   // b = 1
 int c = a;     // c = 2

 int a = 1;
 int b = a++;   // b = 1
 int c = a;     // c = 2

decrementIt is used to decrement the value of the variable by 1. The decrement can be done in two ways:prefix decrementIn this method, the operator precedes the operand (e.g., – -a). The value of operand will be altered before it is used.  int a = 1;
  int b = --a;  // b = 0
postfix decrementIn this method, the operator follows the operand (e.g., a- -). The value of operand will be altered after it is used. int a = 1;
 int b = a--;   // b = 1
 int c = a;     // c = 0
C++ program for combination 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;}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
The above program shows how the postfix and prefix works.
prefix decrementIn this method, the operator precedes the operand (e.g., – -a). The value of operand will be altered before it is used.  int a = 1;
  int b = --a;  // b = 0
postfix decrementIn this method, the operator follows the operand (e.g., a- -). The value of operand will be altered after it is used. int a = 1;
 int b = a--;   // b = 1
 int c = a;     // c = 0

prefix decrementIn this method, the operator precedes the operand (e.g., – -a). The value of operand will be altered before it is used.  int a = 1;
  int b = --a;  // b = 0

  int a = 1;
  int b = --a;  // b = 0

postfix decrementIn this method, the operator follows the operand (e.g., a- -). The value of operand will be altered after it is used. int a = 1;
 int b = a--;   // b = 1
 int c = a;     // c = 0

 int a = 1;
 int b = a--;   // b = 1
 int c = a;     // c = 0

C++ program for combination 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;}
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

The above program shows how the postfix and prefix works.
NOT(!): It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.   If x is true, then !x is false
   If x is false, then !x is true

   If x is true, then !x is false
   If x is false, then !x is true

Addressof operator(&): It gives an address of a variable. It is used to return the memory address of a variable. These addresses returned by the address-of operator are known as pointers because they “point” to the variable in memory.& gives an address on variable n
int a;
int *ptr;
ptr = &a; // address of a is copied to the location ptr. 

& gives an address on variable n
int a;
int *ptr;
ptr = &a; // address of a is copied to the location ptr. 

sizeof(): This operator returns the size of its operand, in bytes. The sizeof operator always precedes its operand.The operand is an expression, or it may be a cast.#include <iostream>using namespace std;  int main(){   float n = 0;   cout << "size of n: " << sizeof(n);   return 1;}Output:size of n: 4
This article is contributed by I. HARISH KUMAR. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.My Personal Notes
arrow_drop_upSave
#include <iostream>using namespace std;  int main(){   float n = 0;   cout << "size of n: " << sizeof(n);   return 1;}
Output:
size of n: 4

This article is contributed by I. HARISH KUMAR. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
SRKReddy1
tusharmalhotra494
gulshankumarar231
khushboogoyal499
C-Operators
CBSE - Class 11
cpp-operator
school-programming
C Language
C++
cpp-operator
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
std::sort() in C++ STL
Bitwise Operators in C/C++
rand() and srand() in C/C++
Left Shift and Right Shift Operators in C/C++
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
std::sort() in C++ STL
Bitwise Operators in C/C++
Socket Programming in C/C++