Unary Operator
Unary Operator
3.In Unary Operator, operators have equal priority from right to left side associativity.
Syntax
1. int a = 2;
2. int b = -(a);
Example:
#include <stdio.h>
#include <conio.h>
int main ()
{
int a = 5; // positive value of a.
int b = -(a); // use unary minus operator to change the value
int n1 = 20;
int n2 = -30;
printf (" The value of a: %d \n", a);
printf (" The value of b: %d \n", b);
printf (" The value of -n1: %d \n", -n1);
printf (" The value of -n2: %d ", -n2);
return 0;
}
Output
The value of a: 5
The value of b: -5
The value of -n1: -20
The value of -n2: 30
Example 2:
#include <stdio.h>
#include <conio.h>
int main ()
{
int a = 10; // use unary plus operator
int b = (-10); // It does not change the operand value
printf (" The value of a: %d \n", a);
printf (" The value of b: %d \n", b);
return 0;
}
Output
The value of a: 10
The value of b: -10
Pre Increment:
The pre-increment operator is represented as (++a), which means the value of variable
'a' is increment by 1 before using operand to the expression.
For example:
x = 10;
A = ++x;
Post Increment:
The (a++) symbol represents the post-increment operator, which means the value of
'a' is incremented by 1 after assigning the original value to the expression or another
variable.
For example:
x = 10;
A = x++;
EXAMPLE:
#include <stdio.h>
#include <conio.h>
int main ()
{
int x, y, a, b; // declare local variable
a = 10;
x = ++a; // It shows pre increment operator
b = 20;
y = b++; // It shows the post increment operator
printf (" \n\n Post Increment Operator");
printf (" \n The value of y is %d.", y);
// get updated value of b
printf (" \n The value of b is %d.", b);
return 0;
}
Output
The Unary decrement operator is represented by the double minus (--) symbol, and it is
used to decrease the operand value by 1 according to the decrement's types.
The Unary decrement operator is of two types: the Pre decrement operator and the Post
Decrement operator.
Pre Decrement: The pre decrement operator is denoted as (--a) symbol, meaning the
operand value is decreased by 1 before assigning to another variable or expression.
Syntax
Post Decrement: The Post decrement operator is denoted as (a--) symbol, which means the
original value is decreased by 1 after assigning to another variable or expression.
Syntax
#include <stdio.h>
#include <conio.h>
int main ()
{
int x, y, a, b; // declare local variable
a = 10;
x = --a; // It shows pre decrement operator
return 0;
}
Output
Syntax;
sizeof(data_variable);
EXAMPLE:
#include <stdio.h>
#include <conio.h>
int main ()
{
// declaration of different types of data variables
int x;
float y;
char ch;
double z;
// use sizeof() operator and pass the different data type variable to get their size.
printf (" The size of the int (x) variable is: %d", sizeof(x));
printf (" \n The size of the float (y) variable is: %d", sizeof(y));
printf (" \n The size of the char (ch) variable is: %d", sizeof(ch));
printf (" \n The size of the double (z) variable is: %d", sizeof(z));
return 0;
}
Output
Syntax
bool a = true;
bool b = !a; // It reverse the condition of variable b
Example 6:
#include <stdio.h>
#include <stdbool.h>
int main ()
{
// declare variables
bool a = true;
bool b;
b = !a; // use logical operator to reverse the condition
printf (" The Boolean value of a is: %d", a);
printf (" \n The Boolean value of b is: %d", b);
bool c = 0;
bool d = !c;
printf (" \n The Boolean value of c is: %d", c);
printf (" \n The Boolean value of d is: %d", d);
return 0;
}
Output
Syntax
int a = 5;
int b = &a; // variable b hold the address of variable a
Example :
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare variables
int a = 10;
int b;
b = &a;
printf (" The value of variable a is: %d", a);
printf (" \n The address of variable b is: %d", b);
return 0;
}
Output