Unary Operators in C

Unary Operators are written before their single operand some Unary Operators written after their operands. Most commonly unary minus is used in the program.

Unary Operator

  Operator   Meaning
& Referencing Operator
* Dereferencing Operator
+ Unary PlusOperator
- Unary Minus Operator
++ Increment Operator
-- Decrement Operator
! Logical Not Operator
~   Bitwise Complement Operator  
sizeof Size of operand in bytes
(type) Type Cast

Referencing operator ('&')

Referencing operator ( & ) produces the address of a variable, Ex: &a is a pointer to a. the
parameters are declared as pointers, and the operands are accessed indirectly through them.

Dereferencing Operator ('*')

The unary operator "*" is Dereferencing Operator and it applied to a pointer, it
accesses the object the pointer variable. Ex: *a.

Unary ( +, - ) Operator

The operands of the unary "+" and "-"operator must have arithmetic type, and the result is the value of the operand. The type of the result is the type of the promoted operand.

Increment Operator (++) & Decrement Operator (--)

The "++" adds one to the variables and "--" subtracts one from the variable. Because it acts upon only one variable.
  Operator   Meaning
++a Pre increment
a++   Post increment  
--a Pre decrement
a-- Post decrement
  Consider the statement
a=5, b=5
  Expression     Value  
++a 6
a++ 6
--b 4
b-- 4
Coding for the above Example:
#include<stdio.h>
  int main()
  {
  int a=5,b=5;
  printf("Value of a=%d\n",a);
  printf("Pre increment %d\n",++a);
  printf("After pre increment the value of a=%d\n",a);
  printf("Post increment %d\n",a++);
  printf("After post increment the value of a=%d\n\n",a);
printf("Value of b=%d\n",b);
  printf("Pre decrement %d\n",--b);
  printf("After pre decrement the value of a=%d\n",b);
  printf("Post decrement %d\n",b--);
  printf("After post decrement the value of a=%d\n",b--);
return 0;
  }

Output

Value of a=5
Pre increment 6
After pre increment the value of a=6
Post increment 6
After post increment the value of a=7

Value of b=5
Pre decrement 4
After pre decrement the value of a=4
Post decrement 4
After post decrement the value of a=3

Logical Not Operator

The variable(operand) must be of scalar type (i.e. not an array, a structure or an union).
Consider the statement
a=5,b=7,c=5
  Expression     Interpretation     Value  
!(a==c) False 0
!(a==b) True 1

Bitwise Complement Operator

operand must be of integral type. Each 0 bit in the operand is set to 1, and each 1 bit in the operand is set to 0.
Consider the statement
X=19;
Z=~x;
X=0001 00112
Z=1110 11002

Sizeof Operator

The "sizeof" Operator returns the size of the variable or type. It returns the size in bytes.
#include<stdio.h>
int main()
{
int m;
printf("%d",sizeof(int));
printf("\n%d",sizeof(m));
return 0;
}

Type Cast

Type casting is a way of converting a variable form one data to another data type. For an example if you want to store a integer value into a float then you can type cast int to float.
#include<stdio.h>
int main()
{
int a=5,b=3;
float c;
c=(float) a/b;
printf("a/b=%f",c);
return 0;
}