Operators and Expressions
Operators and Expressions
Arithmetic
Operators/Operation Example
+ (Addition) A+B
– (Subtraction) A-B
* (multiplication) A*B
/ (Division) A/B
% (Modulus) A%B
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
ASSIGNMENT OPERATORS IN C:
In C programs, values for the variables are assigned using assignment
operators.
For example, if the value “10” is to be assigned for the variable
“sum”, it can be assigned as “sum = 10;”
There are 2 categories of assignment operators in C language.
They are,
1. Simple assignment operator (Example: = )
2. Compound assignment operators ( Example: +=, -=, *=, /=,
%=, &=, ^= )
Operators Example/Description
sum = 10;
= 10 is assigned to variable sum
sum += 10;
+= This is same as sum = sum + 10
sum -= 10;
-= This is same as sum = sum – 10
sum *= 10;
*= This is same as sum = sum * 10
sum /= 10;
/= This is same as sum = sum / 10
sum %= 10;
%= This is same as sum = sum % 10
sum&=10;
&= This is same as sum = sum & 10
sum ^= 10;
^= This is same as sum = sum ^ 10
1 # include <stdio.h>
2
3 int main()
4 {
5 int Total=0,i;
6 for(i=0;i<10;i++)
7 {
8 Total+=i; // This is same as Total = Toatal+i
9 }
10 printf("Total = %d", Total);
11 }
OUTPUT:
Total = 45
RELATIONAL OPERATORS IN C:
Relational operators are used to find the relation between two
variables. i.e. to compare the values of two variables in a C program.
Operators Example/Description
== x == y (x is equal to y)
!= x != y (x is not equal to y)
LOGICAL OPERATORS IN C:
These operators are used to perform logical operations on the given
expressions.
There are 3 logical operators in C language. They are, logical
AND (&&), logical OR (||) and logical NOT (!).
Operators Example/Description
&&
(logical (x>5)&&(y<5)
AND) It returns true when both conditions are true
(x>=10)||(y>=10)
|| (logical It returns true when at-least one of the
OR) condition is true
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5)
&& (y<5))”
! (logical If “((x>5) && (y<5))” is true, logical NOT
NOT) operator makes it false
int main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf("&& Operator : Both conditions are true\n");
}
if (o>p || p!=20)
{
printf("|| Operator : Only one condition is true\n");
}
if (!(m>n && m !=0))
{
printf("! Operator : Both conditions are true\n");
}
else
{
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n");
}
}
OUTPUT:
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
printf("NOT_opr value = %d\n",NOT_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);
}
OUTPUT:
AND_opr value = 0
OR_opr value = 120
NOT_opr value = -41
XOR_opr value = 120
left_shift value = 80
right_shift value = 20
CONDITIONAL OPERATORS IN C:
Conditional operators return one value if condition is true and
returns another value is condition is false.
This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
In above example, if A is greater than 100, 0 is returned else 1 is
returned. This is equal to if else conditional statements.
EXAMPLE PROGRAM FOR CONDITIONAL/TERNARY OPERATORS IN
C:
#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}
OUTPUT:
x value is 1
y value is 2
Increment/decrement Operators in C:
Increment operators are used to increase the value of the
variable by one and decrement operators are used to decrease
the value of the variable by one in C programs.
Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;
EXAMPLE PROGRAM FOR INCREMENT OPERATORS IN C:
In this program, value of “i” is incremented one by one from 1 up to
9 using “i++” operator and output is displayed as “1 2 3 4 5 6 7 8 9”.
//Example for increment operators
#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}
OUTPUT:
123456789
#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}
OUTPUT:
20 19 18 17 16 15 14 13 12 11
value of i is incremented
Pre increment operator before assigning it to the
(++i) variable i
value of i is decremented
Pre decrement operator before assigning it to the
(–i) variable i
SPECIAL OPERATORS IN C:
Below are some of the special operators that the C programming
language offers.
Operators Description
50