1. PROGRAMMING IN C –
Operators
Dr.K.Nithya
Assistant Professor,
Department of CSE,
SRMIST- Trichy
2. Operators in C
● Arithmetic operators
● Logical operators
● Relational operators
● Increment/ Decrement operators
● Assignment operator
● Bitwise operator
● Equality operator
● Other operators
3. Arithmetic operator
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d n",c);
c = a-b;
printf("a-b = %d n",c);
c = a*b;
printf("a*b = %d n",c);
c = a/b;
printf("a/b = %d n",c);
c = a%b;
printf("Remainder when a divided by b = %d n",c);
return 0;
}
4. Logical operator
An expression containing logical
operator returns either 0 or 1
depending upon whether expression
results true or false. Logical operators
are commonly used in decision
making in C programming.
&&
Logical AND. True only if all operands are true
If c = 5 and d = 2 then, expression ((c==5) &&
(d>5)) equals to 0.
||
Logical OR. True only if either one operand is true
If c = 5 and d = 2 then, expression ((c==5) ||
(d>5)) equals to 1.
!
Logical NOT. True only if the operand is 0
If c = 5 then, expression !(c==5) equals to 0.
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d n", result);
result = !(a != b);
printf("!(a != b) is %d n", result);
result = !(a == b);
printf("!(a == b) is %d n", result);
return 0;
}
5. Relational operator
A relational operator checks the
relationship between two operands.
If the relation is true, it returns 1; if
the relation is false, it returns value
0.
Relational operators are used in
decision making and loops.
Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0
6. // Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d n", a, b, a == b);
printf("%d == %d is %d n", a, c, a == c);
printf("%d > %d is %d n", a, b, a > b);
printf("%d > %d is %d n", a, c, a > c);
printf("%d < %d is %d n", a, b, a < b);
printf("%d < %d is %d n", a, c, a < c);
printf("%d != %d is %d n", a, b, a != b);
printf("%d != %d is %d n", a, c, a != c);
printf("%d >= %d is %d n", a, b, a >= b);
printf("%d >= %d is %d n", a, c, a >= c);
printf("%d <= %d is %d n", a, b, a <= b);
printf("%d <= %d is %d n", a, c, a <= c);
return 0;
}
7. Increment/Decrement operator
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d n", ++a);
printf("--b = %d n", --b);
printf("++c = %f n", ++c);
printf("--d = %f n", --d);
return 0;
}
increment ++ and decrement --
- to change the value of an operand
(constant or variable) by 1.
- Increment ++ increases the value by 1
decrement -- decreases the value by 1.
- These two operators are unary
operators (they only operate on a single
operand)
8. Assignment operator
#include <stdio.h>
void main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %dn", c);
c += a; // this means c=c+a;
printf("c = %dn", c);
c -= a; // c is 5
printf("c = %dn", c);
c *= a; // c is 25
printf("c = %dn", c);
c /= a; // c is 5
printf("c = %dn", c);
c %= a; // c = 0
printf("c = %dn", c);
}
9. Bitwise operator
- During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to
bit-level which makes processing faster and saves power.
- Bitwise operators are used in C programming to perform bit-level operations.
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
Ex: Bitwise AND (&)
#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a & b);
return 0;
}
Ex: Bitwise Complement (~)
#include <stdio.h>
int main() {
printf(" %d n", ~35);
printf(" %dn", ~-12);
return 0;
}
10. Equality operator
- It’s a binary operator and works
on two operands, it returns 1 if
value of both operands are
equal else it returns 0.
Syntax:
Operand1 == Operand2
#include <stdio.h>
int main()
{
int a=10;
int b=10;
int result;
result = (a==b);
printf("result: %dn",result);
return 0;
}
11. Other operators
sizeof operator
● It is a compile-time unary operator which can be used to compute the size of its operand.
● The result of sizeof is of the unsigned integral type which is usually denoted by size_t.
Comma Operator
● The comma operator (represented by the token) is a binary operator that evaluates its first operand and
discards the result, it then evaluates the second operand and returns this value (and type).
● The comma operator has the lowest precedence of any C operator.
● Comma acts as both operator and separator.
12. Conditional Operator
● The conditional operator is of the form expression1? expression2: expression3
● Here, Expression1 is the condition to be evaluated. If the condition(expression1) is True then we will execute and return the
result of expression2. otherwise if the condition(Expression1) is false, then we will execute and return the result of
expression3.
● We may replace the use of if..else statements with conditional operators.
dot (.) and arrow (->) Operators
● Member operators are used to reference individual members of classes, structures, and unions.
● The dot operator is applied to the actual object.
● The arrow operator is used with a pointer to an object.
13. Cast Operator
● Casting operators convert one data type to another. For example, int(2.2000) would return 2.
● A cast is a special operator that forces one data type to be converted into another.
● The most general cast supported by most of the C compilers is as follows − [ (type) expression ].
&,* Operator
● Pointer operator & returns the address of a variable. For example &a; will give the actual address of the variable.
● Pointer operator * is a pointer to a variable. For example *var; will pointer to a variable var.
15. Comma operator
#include<stdio.h>
void main()
{
int p = 35;
int q = (p++, ++p);
printf("%d", q);
}
#include <stdio.h>
int main()
{
int a,b;
a = 10,20,30;
b = (10,20,30);
printf("%d, %dn",a,b);
return 0;
}
#include <stdio.h>
int main()
{
int a= 10,20,30;
int b;
b= (10,20,30);
printf("a= %d, b=
%dn",a,b);
return 0;
}
16. Conditional operator
#include <stdio.h>
int main()
{
int age; // variable declaration
printf("Enter your age");
scanf("%d",&age);
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for
voting")); return 0;
}
#include <stdio.h>
int main()
{
int a=5, b;
b=((a==5)?(3):(2));
printf("The value of 'b'
variable is : %d",b);
return 0;
}
18. Right Shift operator (>>)
#include<stdio.h>
int main()
{
int a = 5, b = 9;
printf("a>>1 = %dn", a>>2);
printf("b>>1 = %dn", b>>1);
return 0;
}
#include <stdio.h>
int main ()
{
unsigned int num = 0xff;
num = (num >> 2);
printf (" n After shifting the binary bits to the left side. ");
printf (" n The new value of the unsigned variable num = %d",
num);
return 0;
}