Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

BE SEM - I

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.

The Arithmetic Operators in C include:


• + (Addition): - This operator is used to add two operands.
• - (Subtraction): – Subtract two operands.
• * (Multiplication): – Multiply two operands.
• / (Division): – Divide two operands and gives the quotient as the answer.
• % (Modulus operation): – Find the remains of two integers and gives the remainder
after the division.
• ++ (Increment): – Used to increment an operand.
• -- (Decrement): – Used to decrement an operand.

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.

The Relational Operators in C include:


• == (Equal to): – This operator is used to check if both operands are equal.
• ! = (Not equal to): – Can check if both operands are not equal.
• > (Greater than): – Can check if the first operand is greater than the second.
• < (Less than): - Can check if the first operand is lesser than the second.
• >= (Greater than equal to): – Check if the first operand is greater than or equal to
the second.
• <= (Less than equal to): – Check if the first operand is lesser than or equal to the
second

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 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, c, a != c); // condition is true


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, 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.

Logical Operators in C Includes –


• && (AND) – It is used to check if both the operands are true.
• || (OR) – These operators are used to check if at least one of the operands is true.
• ! (NOT) – Used to check if the operand is false

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");

answer = (a == b) && (c > b);


printf("For (%d == %d) && (%d != %d), the output is: %d \n",a,b,b,c,answer);
//condition is true

answer = (a == b) && (c < b) && (c>0);


printf("For (%d == %d) && (%d <= %d), the output is: %d
\n",a,b,b,c,answer); //condition is false

answer = (a == b) || (b > c);


printf("For (%d == %d) || (%d < %d), the output is: %d \n",a,b,c,b,answer);
/ /condition is true

answer = (a != b) || (a <= b) || (a>c);


printf("For (%d != %d) || (%d < %d), the output is: %d \n",a,b,c,b,answer);
//condition is true

answer = !(a == b);


printf("For !(%d == %d), the output is: %d \n",a,b,answer); //condition is false

answer = !(a != b);


printf("For !(%d == %d), the output is: %d \n",a,b,answer); //condition is true
return 0;
}

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");

int number = 10, result;


result = number;

printf("result = %d \n", result);

result += number; //Same as result = result + a


printf("result = %d \n", result);

result -= number; //Same as result = result - a


printf("result = %d \n", result);

result *= number; //Same as result = result * a


printf("result = %d \n", result);

result /= number; //Same as result = result / a


printf("result = %d \n", result);

result %= number; //Same as result = result % a


printf("result = %d \n", result);
return 0;
}

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.

In C, there are quite a few unary operators as mentioned below: -


1.) The increment and
2.) the decrement 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

printf("%d %p", hours, &hours);


would produce the following as output… (%p is the specifier for addresses): 24 0FF8

Ultimately, We have following Unary Operators in C.


• ! (logical negation),
• ~ (one’s complement or bitwise negation)
• - (unary minus) (Example: int a = 10; int b = -a; // b = -10)
• + (unary plus),
• & (addressof),
• (dereferencing),
• ++ (pre-increment),
• -- (pre-decrement),
• sizeof operator,
• (type) or cast operator

All Unary Operators have associativity from Right to Left.

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.

The Bitwise Operators in C/C++ Includes –

• & (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.

a b a&b a|b a^b ~a


0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0

• 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

Bitwise One’s Complement


a = 26 = 1 1 0 1 0
________
00101

num1 = 11; /* equal to 00001011*/


num2 = 22; /* equal to 00010110 */

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()
{

printf("Bitwise Operators in C \n");

int a = 26, b = 14;


printf(" Bitwise AND operation %d & %d : %d\n",a,b,a&b);
printf(" Bitwise OR operation %d | %d : %d\n",a,b,a|b);
printf(" Bitwise XOR operation %d ^ %d : %d\n",a,b,a^b);
printf(" Bitwise ONE'S COMPLEMENT ~ %d operation : %d\n",a,~a);
return 0;
}

Output: -

=========================End unit 4==================================

==================================================================

Teksan Gharti

You might also like