Unary Operator in C | Unary Operator in C - Types and Example

C has a type of operators that act upon a single operand to produce a new value. These types of operators are known as unary operators. Unary operators generally precede their single operands, though some unary operators are written after their operands.

A unary operator is an operator used to operate on a single operand to return a new value. All the unary operators have equal precedence, and their associativity is from right to left. When we combine the unary operator with an operand, we get the unary expression.  


Types of Unary Operator in C:

Following are the types of unary operators found in the C language:

  • unary minus
  • increment
  • decrement
  • NOT
  • sizeof ()
  • Address of operator


Unary Minus Operator:

The Unary Minus operator is represented using the symbol (-).This operator changes the sign of any given argument. It means it changes the positive number to the negative, and a negative number becomes the positive number using the unary minus operator.


For example:

int a = 20;

int b = -a;    // b = -20

The unary minus operator is totally different from the arithmetic operator subtraction (-), as subtraction (-) requires two operands.


Increment operator:

The increment operator is denoted by the "++" symbol. This operator is used to increment the value of any given variable by 1. The increment operator can be used  in two ways:

  • Prefix increment
  • Postfix Increment

 

Prefix Increment

In this method, the operator precedes the given operand (Example, ++a). Thus, the value of the operand gets changed before it is used.


For example:

int a = 1;

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

 

Postfix Increment

In this method, the operator follows the given operand (Example, a++). Thus, the value of the available operand gets changed after it is used.

 

For example:

int a= 1;

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

int c=a ;       // c= 2

 

Decrement Operator:

The decrement operator is opposite to the increment operator. It is represented by the (--) symbol. This operator is used to decrement the value of any given variable by 1.The decrement operator can be used in two ways:


Prefix Decrement

In this method, the operator precedes the given operand (Example, -- a). Thus, the value of the operand gets changed before it is used.


For example:

int a= 1;

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


Postfix Decrement

In this method, the operator follows the given operand (Example, b--). Thus, the value of the available operand gets changed after it is used.


For example:

int a = 1;

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

int c = a;      // c = 0

 

NOT Operator:

The not operator is represented by the symbol ( ! ) This operator is used to reverse the logical state of the available operand. It means that if a condition is true, the Logical NOT operator will make it false.

In simple words,

  • If a is true, then !a will be false.
  • If a is false, then !a will be true.

 

Sizeof() Operator:

The sizeof() operator finds the size of different data types or operandssizeof is greatly used in dynamic memory allocation.

This operator can be used to compute the size of its operand in terms of bytes. It returns the size of a variable. It can be applied to any data type, float type, pointer type variables.

This type of operator always precedes the given operand.  When sizeof() is used with the data types, it simply returns the amount of memory allocated to that data type.


Syntax:

sizeof(data_variable);  

 OR

sizeof(type_name)


Example:  

sizeof(int)    returns 2, since an integer occupies 2 bytes of memory

 

Address of Operator:

The address of operator is denoted as ampersand (&) symbol. This operator gives an address of a variable. The address of operator is used to return the address (memory address) of any variable. The addresses that are returned by this operator are called pointers because they point to the variable present in the memory.


For example:

int a;

int *ptr;

ptr = &a;     // address of a is copied to the location ptr.



Post a Comment

0 Comments