Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

C Programming Operators

The document discusses different types of operators in C programming language. It describes arithmetic operators like addition, subtraction etc. Relational operators like equal, not equal, less than etc. that compare values and return true or false. Logical operators like AND, OR, NOT that perform logical operations. Bitwise operators like AND, OR, XOR that perform bit-level operations. It provides examples of each type of operator and explains their use and behavior in C programs.

Uploaded by

haldersubhas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

C Programming Operators

The document discusses different types of operators in C programming language. It describes arithmetic operators like addition, subtraction etc. Relational operators like equal, not equal, less than etc. that compare values and return true or false. Logical operators like AND, OR, NOT that perform logical operations. Bitwise operators like AND, OR, XOR that perform bit-level operations. It provides examples of each type of operator and explains their use and behavior in C programs.

Uploaded by

haldersubhas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

C Programming Operators

Operators are used to perform operations. Operators are the symbols which perform
the operation on the some values. These values are known as operands. There are
following types of operators to perform different types of operations in C language:

 Arithmetic Operators
 Relational Operators
 Logical Operators
 Assignment Operators
 Bitwise Operators
 Increment and Decrement Operators
 Misc Operators

1. Arithmetic Operators
Operator Operator Name Description Example
+ Addition Adds two operands I = 40, J= 20I + J =
60
– Subtraction Subtracts second operand from the I = 40, J= 20I – J =
first 20
* Multiplication Multiplies both operands I = 40, J= 20I * J =
800
/ Divide Perform division operation I = 40, J= 20I / J = 2
% Modulus Return the remainder after Division I = 40, J= 20I % J =
0
++ Increment Increase the operand value by 1 I=40,I++ =
41, ++I = 40 (print
40 but next time its
value is 41)
— Decrement Decrease the operand value by 1 I=40I– = 39, –I = 40
(print 40 but next
time its value is 39)
Example 1: Arithmetic Operators
// Working of arithmetic operators
#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;
}
Run Code

Output

a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

The operators + , - and * computes addition, subtraction, and multiplication


respectively as you might have expected.
In normal calculation, 9/4 = 2.25 . However, the output is 2 in the program.
It is because both the variables a and b are integers. Hence, the output is
also an integer. The compiler neglects the term after the decimal point and
shows answer 2 instead of 2.25 .

The modulo operator % computes the remainder. When a=9 is divided


by b=4 , the remainder is 1 . The % operator can only be used with integers.
Suppose a = 5.0 , b = 2.0 , c = 5 and d = 2. Then in C programming,

// Either one of the operands is a floating-point number

a/b = 2.5

a/d = 2.5

c/b = 2.5
// Both operands are integers

c/d = 2

2. Relational Operators

It is also known as comparison operator because it compares the values. After


comparison it returns the Boolean value i.e. either true or false.
Operator Operator Name Description Example
== Equal to If the values of two operands I = 20, J =20(I ==
are equal then it returns true. J) is true
!= Not Equal to If the values of two operands I = 20, J =20(I ==
are not equal then it returns J) is False
true.
< Less than If the value of left operand is I = 40, J =20(I <
less than the value of right J) is False
operand then it returns true
> Greater than If the value of left operand is I = 40, J =20(I >
greater than the value of right J) is True
operand then it returns true
<= Less than or equal to If the value of left operand is I = 40, J =20(I <=
less than or equal to the J) is False
value of right operand then it
returns true.
>= Greater than or equal to If the value of left operand is I = 40, J =20(I >=
greater than or equal to the J) is True
value of right operand then it
returns true.
Example 2: Relational Operators
// 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;
}
Run Code

Output

5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1

3. Logical Operators
Operator Operator Description Example
Name
And Logical AND When Both side condition is true the result is 2<1 and
true otherwise false 2<3False

Or Logical OR When at least one condition is true then result 2<1 or


is true otherwise false 2<3True

Not Logical NOT Reverse the condition Not(5>4)False


Example 3: Logical Operators
// Working of logical operators

#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;
}
Run Code

Output

(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0

Explanation of logical operator program


 (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c >

b) is 1 (true).
 (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
 (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
 (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c <

b) are 0 (false).
 !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a
!= b) is 1 (true).
 !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0
(false).

4. Bitwise Operators

It performs bit by bit operation. Suppose there are two variable I = 10 and J = 20 and
their binary values are
I = 10 = 0000 1010
J = 20 = 0001 0100
Operator Operator Name Description Example
& Binary AND If both bits are 1 then 1 I & J0000 0000
otherwise 0
| Binary OR If one of the bit is 1 then 1 I | J0001 1110
otherwise 0
^ Binary XOR If both bit are same then 0 I ^ J0001 1110
otherwise 1
~ Binary Complement If bit is 1 the make it 0 and if bit ~I1111 0101
is 0 the make it 1
<< Binary Left Shift The left operand is moved left I << 2 will give
by the number of bits specified 240 i.e. 1111 0000
by the right operand.
>> Binary Right Shift The left operand is moved right I >> 2 will give 15
by the number of bits specified i.e. 1111
by the right operand.

In the arithmetic-logic unit (which is within the CPU), mathematical


operations like: addition, subtraction, multiplication and division are done in
bit-level. To perform bit-level operations in C programming, bitwise
operators are used.
Bitwise AND Operator &
The output of bitwise AND is 1 if the corresponding bits of two operands
is 1. If either bit of an operand is 0, the result of corresponding bit is
evaluated to 0.
In C Programming, the bitwise AND operator is denoted by & .
Let us suppose the bitwise AND operation of two integers 12 and 25.

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)

Bit Operation of 12 and 25


00001100
& 00011001
________
00001000 = 8 (In decimal)

Example 1: Bitwise AND


#include <stdio.h>

int main() {

int a = 12, b = 25;


printf("Output = %d", a & b)
;

return 0;
}
Run Code

Output

Output = 8
Bitwise OR Operator |
The output of bitwise OR is 1 if at least one corresponding bit of two
operands is 1. In C Programming, bitwise OR operator is denoted by | .

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25


00001100
| 00011001
________
00011101 = 29 (In decimal)

Example 2: Bitwise OR
#include <stdio.h>

int main() {

int a = 12, b = 25;


printf("Output = %d", a | b);

return 0;
}
Run Code

Output

Output = 29

Bitwise XOR (exclusive OR) Operator ^


The result of bitwise XOR operator is 1 if the corresponding bits of two
operands are opposite. It is denoted by ^ .

12 = 00001100 (In Binary)


25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
00001100
^ 00011001
________
00010101 = 21 (In decimal)

Example 3: Bitwise XOR


#include <stdio.h>

int main() {

int a = 12, b = 25;


printf("Output = %d", a ^ b);

return 0;
}
Run Code

Output

Output = 21

Bitwise Complement Operator ~


Bitwise complement operator is a unary operator (works on only one
operand). It changes 1 to 0 and 0 to 1. It is denoted by ~ .

35 = 00100011 (In Binary)

Bitwise complement Operation of 35


~ 00100011
________
11011100 = 220 (In decimal)

Twist in Bitwise Complement Operator in C Programming


The bitwise complement of 35 ( ~35 ) is -36 instead of 220, but why?
For any integer n , bitwise complement of n will be -(n + 1) . To understand
this, you should have the knowledge of 2's complement.
2's Complement

Two's complement is an operation on binary numbers. The 2's complement


of a number is equal to the complement of that number plus 1. For
example:

Decimal Binary 2's complement


0 00000000 -(11111111+1) = -00000000 = -0(decimal)
1 00000001 -(11111110+1) = -11111111 = -256(decimal)
12 00001100 -(11110011+1) = -11110100 = -244(decimal)
220 11011100 -(00100011+1) = -00100100 = -36(decimal)

Note: Overflow is ignored while computing 2's complement.

The bitwise complement of 35 is 220 (in decimal). The 2's complement


of 220 is -36. Hence, the output is -36 instead of 220.
Bitwise Complement of Any Number N is -(N+1). Here's how:

bitwise complement of N = ~N (represented in 2's complement form)


2'complement of ~N= -(~(~N)+1) = -(N+1)

Example 4: Bitwise complement


#include <stdio.h>

int main() {

printf("Output = %d\n", ~35);


printf("Output = %d\n", ~-12);

return 0;
}
Output

Output = -36
Output = 11

Shift Operators in C programming


There are two shift operators in C programming:

 Right shift operator

 Left shift operator.

Right Shift Operator

Right shift operator shifts all bits towards right by certain number of
specified bits. It is denoted by >> .

212 = 11010100 (In binary)


212 >> 2 = 00110101 (In binary) [Right shift by two bits]
212 >> 7 = 00000001 (In binary)
212 >> 8 = 00000000
212 >> 0 = 11010100 (No Shift)

Left Shift Operator

Left shift operator shifts all bits towards left by a certain number of specified
bits. The bit positions that have been vacated by the left shift operator are
filled with 0. The symbol of the left shift operator is << .

212 = 11010100 (In binary)


212<<1 = 110101000 (In binary) [Left shift by one bit]
212<<0 = 11010100 (Shift by 0)
212<<4 = 110101000000 (In binary) =3392(In decimal)

Example #5: Shift Operators


#include <stdio.h>

int main() {

int num=212, i;

for (i = 0; i <= 2; ++i) {


printf("Right shift by %d: %d\n", i, num >> i);

}
printf("\n");

for (i = 0; i <= 2; ++i) {


printf("Left shift by %d: %d\n", i, num << i);

return 0;
}

Right Shift by 0: 212


Right Shift by 1: 106
Right Shift by 2: 53

Left Shift by 0: 212


Left Shift by 1: 424
Left Shift by 2: 848

5. Assignment Operators
Operator Operator Name Description Example
= Assignment It assigns value from right side operand I = 40It assigns
to left side operand 40 to I
+= Add then assign It performs addition and then result is I+=Jthat means
assigned to left hand operand I=I+J
-= Subtract then It performs subtraction and then result is I-=Jthat means
assign assigned to left hand operand I=I–J
*= Multiply the It performs multiplication and then result I*=Jthat means
assign is assigned to left hand operand. I=I*J
/= Divide then It performs division and then result is I/=Jthat means
assign assigned to left hand operand I=I/J
%= Modulus then It performs modulus and then result is I%=Jthat means
assign assigned to left hand operand I=I%J
<<= Left shift AND It performs Binary left shift and then I<<=5that means
assignment result is assigned to left hand operand I = I << 5
operator
>>= Right shift AND It performs Binary right shift and then I>>=5that means
assignment result is assigned to left hand operand I = I >>=5
operator
&= Bitwise AND It performs bitwise AND and then result I &= 5that means
assignment is assigned to left hand operand I=I&5
operator
^= bitwise exclusive It performs bitwise exclusive OR and I ^= 5that means
OR and then result is assigned to left hand I=I^5
assignment operand
operator
|= bitwise inclusive It performs bitwise inclusive OR and then I |= 5that means
OR and result is assigned to left hand operand I=I|5
assignment
operator
Example 5: Assignment Operators
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;

c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);

return 0;
}
Run Code

Output

c = 5
c = 10
c = 5
c = 25
c = 5
c = 0

C Increment and Decrement Operators


C programming has two operators increment ++ and decrement -- to
change the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases
the value by 1. These two operators are unary operators, meaning they
only operate on a single operand.
Example 2: Increment and Decrement Operators
// 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;
}

Output

++a = 11
--b = 99
++c = 11.500000
--d = 99.500000

Here, the operators ++ and -- are used as prefixes. These two operators
can also be used as postfixes like a++ and a-- .

7. Misc Operators

There are few other important operators including sizeof and ? : supported by C
Language.
Operator Description
sizeof() Returns the size of an variable.
& Returns the address of an variable.
* Pointer to a variable.
?: Conditional Expression

Operators Precedence in C
Category Operator Associativity
Postfix () [] -> . ++ – – Left to right
Unary + – ! ~ ++ – – (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +– Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

You might also like