C-Programming Unit-4
C-Programming Unit-4
C- Programming
Unit 4: Operators & Expression
Operators
An operator is a symbol that tells the compiler to perform specific mathematical or
logical functions. C language provides the following types of operators …
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Unary Operators
6. Conditional Operators
7. Bitwise Operators
1.Arithmetic Operators
It includes basic arithmetic operations like addition, subtraction, multiplication,
division, modulus operations, increment, and decrement.
Teksan Gharti
BE SEM - I
C- Programming
Program: -
#include<stdio.h>
int main()
{
int a=10, b=7;
printf("Arithmetic Operators\n");
printf("The Addition of %d and %d is: %d\n",a,b,a+b);
printf("The Subtraction of %d and %d is: %d\n",a,b,a-b);
printf("The Multiplication of %d and %d is: %d\n",a,b,a*b);
printf("The Division of %d and %d is: %d\n",a,b,a/b);
printf("The Modulus operation of %d and %d is: %d\n",a,b,a%b);
printf("The Incremented value ++a is: %d\n",++a);
printf("The Decremented value --b is: %d\n",--b);
return 0;
}
Output: -
Teksan Gharti
BE SEM - I
C- Programming
2.Relational Operators
It is used to compare two numbers by checking whether they are equal or not, less
than, less than or equal to, greater than, greater than or equal to.
If the relational statement is satisfied (it is true), then the program will return the
value 1, otherwise, if the relational statement is not satisfied (it is false), the program will
return the value 0.
Program
#include <stdio.h>
int main()
{
int a=10, b=10, c=20;
printf("The Relational Operators in C\n");
printf("For %d > %d : The output is: %d \n", a, b, a > b); // condition is false
printf("For %d > %d : The output is: %d \n", a, c, a > c); // condition is false
printf("For %d < %d : The output is: %d \n", a, b, a < b); // condition is false
printf("For %d < %d : The output is: %d \n", a, c, a < c); // condition is true
printf("For %d >= %d : The output is: %d \n", a, b, a >= b); // condition is true
printf("For %d >= %d : The output is: %d \n", a, c, a >= c); // condition is false
printf("For %d <= %d : The output is: %d \n", a, b, a <= b); // condition is true
printf("For %d <= %d : The output is: %d \n", a, c, a <= c); // condition is true
return 0;
}
Teksan Gharti
BE SEM - I
C- Programming
Output: -
3.Logical Operators
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.
If the logical statement is satisfied (it is true), then the program will return the value 1,
otherwise, if the relational statement is not satisfied (it is false), the program will return the
value 0.
Teksan Gharti
BE SEM - I
C- Programming
Program
#include <stdio.h>
int main()
{
int a = 10, b = 10, c = 20, answer;
printf("Logical Operators in C \n");
Output: -
Teksan Gharti
BE SEM - I
C- Programming
4.Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =.
• = (Assignment): - Used to assign a value from right side operand to left side operand.
• += (Addition Assignment): - To store the sum of both the operands to the left side
operand.
• -= (Subtraction Assignment): – To store the difference of both the operands to the
left side operand.
• *= (Multiplication Assignment): – To store the product of both the operands to the
left side operand.
• /= (Division Assignment): – To store the division of both the operands to the left side
operand.
• %= (Remainder Assignment): – To store the remainder of both the operands to the
left side operand.
Program
#include<stdio.h>
int main()
{
printf("Assignment operator in c \n");
Teksan Gharti
BE SEM - I
C- Programming
Output: -
5.Unary operators
Unary operators are special operators that operate upon one operand only. Generally,
operators require two operands to operate upon.For example, in the expression a+b the
operator ‘+’ operates upon two operands namely, 'a' and 'b'. Hence, an example of a binary
operator.
The increment operator increases the value of a variable by one. And it is of two types,
the post increment and pre increment.
The pre increment operator first increases the value by 1 and then uses it for
calculation whereas the post increment operator perform its calculation first and then
increases the value by 1.Similar explanation can be extended for the decrement operator
which also has two type same as that of the increment operator.
Program: -
#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;
}
Teksan Gharti
BE SEM - I
C- Programming
Output: -
Similarly, there are other unary operators in C such as the address-of operator denoted
by the ampersand symbol ‘&’, which returns the address of its operand.Suppose we have the
following statement:
hours = 24;
Suppose that the address where 'hours' is stored is 0FF8. (PC addresses often are given as
hexadecimal values.) Then the statement
Teksan Gharti
BE SEM - I
C- Programming
6.Conditional operator (ternary operator)
This operator evaluates a Boolean expression and assign the value based on the result.
Syntax:
variable num1 = (expression) ? value if true : value if false
If the expression results true then the first value before the colon (:) is assigned to
the variable num1 else the second value is assigned to the num1.
Example:
Int A,B;
B= (A > 100 ? 0 : 1);
In above example, if A is greater than 100, 0 is assigned to the variable B else the second
value i.e. 1 is assigned to the variable B.
Program: -
#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: -
Teksan Gharti
BE SEM - I
C- Programming
7.Bitwise Operators
It is based on the principle of performing operations bit by bit which is based on
Boolean algebra. It increases the processing speed and hence the efficiency of the program.
• & (Bitwise AND): – Converts the value of both the operands into binary form and
performs AND operation bit by bit.
• | (Bitwise OR): – Converts the value of both the operands into binary form and
performs OR operation bit by bit.
• ^ (Bitwise exclusive OR): – Converts the value of both the operands into binary form
and performs EXCLUSIVE OR operation bit by bit.
• ~ (One’s complement operator): - Converts the operand into its complementary
form.
• << – Left shift
• >> – Right shift
Note: Bitwise operators are not applicable in the case of float and double data type in C.
In order to clearly understand bitwise operators, let us see the truth table for various
bitwise operations and understand how it is associated with Boolean algebra.
• AND – Both the operands should have boolean value 1 for the result to be 1.
• OR – At least one operand should have boolean value 1 for the result to be 1.
• XOR (EXCLUSIVE OR) – Either the first operand should have boolean value 1 or
the second operand should have boolean value 1. Both cannot have the boolean value
1 simultaneously.
• One Complement: Complement operator that just changes the bit from 0 to 1 and 1
to 0.
• Left shift: It specifies the value of the left operand to be shifted to the left by the
number of bits specified by its right operand
• Right shift: It species the value of the left operand to be shifted to the right by the
number of bits specified by its right operand.
Teksan Gharti
BE SEM - I
C- Programming
Example
Consider two operands, a and b with values:
a = 26 and b=14
Bitwise AND
a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a & b = 0 1 0 1 0 which is equal to 10
Bitwise OR
a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a | b = 1 1 1 1 0 which is equal to 30
Bitwise XOR
a = 26 = 1 1 0 1 0
b = 14 = 0 1 1 1 0
________
a | b = 1 0 1 0 0 which is equal to 20
Left shift:
num1 << 2 is left shift operator that moves the bits to the left, discards the far-left bit, and
assigns the rightmost bit a value of 0. In our case output is 44 which is equivalent to
00101100
Right shift:
num1 >> 2 is right shift operator that moves the bits to the right, discards the far-right bit, and
assigns the leftmost bit a value of 0. In our case output is 2 which is equivalent to 00000010
Teksan Gharti
BE SEM - I
C- Programming
Program
#include <stdio.h>
int main()
{
Output: -
==================================================================
Teksan Gharti