Chapter 2 (Subject: PPS) part:2
C Programming Operators
An operator is a symbol that operates on a value or a variable. For example: + is an operator to
perform addition.
C operators are symbols that are used to perform mathematical or logical manipulations. The C
programming language is rich with built-in operators. Operators take part in a program for
manipulating data and variables and form a part of the mathematical or logical expressions.
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment and Decrement Operators
6. Conditional Operator
7. Bitwise Operators
8. Special Operators
1. Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction,
multiplication, division etc on numerical values (constants and variables).
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% remainder after division (modulo division)
Example 1: Arithmetic Operators
1. // Working of arithmetic operators
2. #include <stdio.h>
3. int main()
4. {
5. int a = 9,b = 4, c;
6.
7. c = a+b;
8. printf("a+b = %d \n",c);
9. c = a-b;
10. printf("a-b = %d \n",c);
11. c = a*b;
12. printf("a*b = %d \n",c);
13. c = a/b;
14. printf("a/b = %d \n",c);
15. c = a%b;
16. printf("Remainder when a divided by b = %d \n",c);
17.
18. return 0;
19. }
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. The % operator is not with float or double data
type.
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. 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
1. // Working of increment and decrement operators
2. #include <stdio.h>
3. int main()
4. {
5. int a = 10, b = 100;
6. float c = 10.5, d = 100.5;
7.
8. printf("++a = %d \n", ++a);
9. printf("--b = %d \n", --b);
10. printf("++c = %f \n", ++c);
11. printf("--d = %f \n", --d);
12.
13. return 0;
14. }
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--. Visit this page to learn more about how increment and decrement
operators work when used as postfix.
3. Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
Operator Example Same as
/= a /= b a = a/b
%= a %= b a = a%b
Example 3: Assignment Operators
1. // Working of assignment operators
2. #include <stdio.h>
3. int main()
4. {
5. int a = 5, c;
6.
7. c = a; // c is 5
8. printf("c = %d\n", c);
9. c += a; // c is 10
10. printf("c = %d\n", c);
11. c -= a; // c is 5
12. printf("c = %d\n", c);
13. c *= a; // c is 25
14. printf("c = %d\n", c);
15. c /= a; // c is 5
16. printf("c = %d\n", c);
17. c %= a; // c = 0
18. printf("c = %d\n", c);
19.
20. return 0;
21. }
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
4. Relational Operators
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
Example 4: Relational Operators
1. // Working of relational operators
2. #include <stdio.h>
3. int main()
4. {
5. int a = 5, b = 5, c = 10;
6.
7. printf("%d == %d is %d \n", a, b, a == b);
8. printf("%d == %d is %d \n", a, c, a == c);
9. printf("%d > %d is %d \n", a, b, a > b);
10. printf("%d > %d is %d \n", a, c, a > c);
11. printf("%d < %d is %d \n", a, b, a < b);
12. printf("%d < %d is %d \n", a, c, a < c);
13. printf("%d != %d is %d \n", a, b, a != b);
14. printf("%d != %d is %d \n", a, c, a != c);
15. printf("%d >= %d is %d \n", a, b, a >= b);
16. printf("%d >= %d is %d \n", a, c, a >= c);
17. printf("%d <= %d is %d \n", a, b, a <= b);
18. printf("%d <= %d is %d \n", a, c, a <= c);
19.
20. return 0;
21. }
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
5. 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.
Operator Meaning Example
Logical AND. True only if all If c = 5 and d = 2 then, expression ((c==5) &&
&& operands are true (d>5)) equals to 0.
Logical OR. True only if either If c = 5 and d = 2 then, expression ((c==5) ||
|| one operand is true (d>5)) equals to 1.
Logical NOT. True only if the
! operand is 0 If c = 5 then, expression !(c==5) equals to 0.
Example 5: Logical Operators
1. // Working of logical operators
2.
3. #include <stdio.h>
4. int main()
5. {
6. int a = 5, b = 5, c = 10, result;
7.
8. result = (a == b) && (c > b);
9. printf("(a == b) && (c > b) is %d \n", result);
10. result = (a == b) && (c < b);
11. printf("(a == b) && (c < b) is %d \n", result);
12. result = (a == b) || (c < b);
13. printf("(a == b) || (c < b) is %d \n", result);
14. result = (a != b) || (c < b);
15. printf("(a != b) || (c < b) is %d \n", result);
16. result = !(a != b);
17. printf("!(a == b) is %d \n", result);
18. result = !(a == b);
19. printf("!(a == b) is %d \n", result);
20.
21. return 0;
22. }
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).
6. Conditional Operator
The conditional operator is kind of similar to the if-else statement as it does follow the same
algorithm as of if-else statement but the conditional operator takes less space and helps to write
the if-else statements in the shortest way possible.
Syntax:
variable = Expression1 ? Expression2 : Expression3
It can be visualized into if-else statement as:
if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
Since the Conditional Operator ‘?:’ takes three operands to work, hence they are also
called ternary operators.
Working:
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True
then Expression2 will be executed and the result will be returned. Otherwise, if the
condition(Expression1) is false then Expression3 will be executed and the result will be
returned.
// Working of conditional operators
23. #include <stdio.h>
24. int main()
25. {
26. int a = 5, b = 10, c = 10, max;
27. max = (a > b) ? a : b;
28. printf(“Largest number between %d, and %d is %d”, a, b, max);
29. return 0;
30. }
Output:
Largest number between 5 and 10 is 10.
7. Bitwise Operators
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
1. 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.
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: Bitwise AND
1. #include <stdio.h>
2. int main()
3. {
4. int a = 12, b = 25;
5. printf("Output = %d", a&b);
6. return 0;
7. }
Output
Output = 8
2. 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 : Bitwise OR
1. #include <stdio.h>
2. int main()
3. {
4. int a = 12, b = 25;
5. printf("Output = %d", a|b);
6. return 0;
7. }
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: Bitwise XOR
1. #include <stdio.h>
2. int main()
3. {
4. int a = 12, b = 25;
5. printf("Output = %d", a^b);
6. return 0;
7. }
Output
Output = 21
3. Bitwise complement operator ~
Bitwise compliment operator is an 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.
4. Shift Operators
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 certain number of specified bits. It is denoted by
<<.
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: Shift Operators
1. #include <stdio.h>
2. int main()
3. {
4. int num=212, i;
5. for (i=0; i<=2; ++i)
6. printf("Right shift by %d: %d\n", i, num>>i);
7.
8. printf("\n");
9.
10. for (i=0; i<=2; ++i)
11. printf("Left shift by %d: %d\n", i, num<<i);
12.
13. return 0;
14. }
Right Shift by 0: 212
Right Shift by 1: 106
Right Shift by 2: 53
8. Special Operators
1. Comma Operator
Comma operators are used to link related expressions together. For example:
1. int a, c = 5, d;
2. The sizeof operator
The sizeof is a unary operator that returns the size of data (constants, variables, array, structure,
etc).
Example : sizeof Operator
1. #include <stdio.h>
2. int main()
3. {
4. int a;
5. float b;
6. double c;
7. char d;
8. printf("Size of int=%lu bytes\n",sizeof(a));
9. printf("Size of float=%lu bytes\n",sizeof(b));
10. printf("Size of double=%lu bytes\n",sizeof(c));
11. printf("Size of char=%lu byte\n",sizeof(d));
12.
13. return 0;
14. }
Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Other operators such as ternary operator ?:, reference operator &, dereference operator * and
member selection operator -> will be discussed in later