Part 4-Operators and Expressions-15.03.2024
Part 4-Operators and Expressions-15.03.2024
The C language’s rich set of operators is one of its distinguished features. These operators
can be enumerated in following categories:
Arithmetic Operators ( +, , *, /, % )
Relational Operators ( <, <=, >, >=, ==, != )
Logical Operators ( !, &&, || )
Bit-wise Operators ( ~, >>, <<, &, | , ^)
Special Operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition,
subtraction, multiplication, etc.
1
Binary Arithmetic Operators
Operator Symbol Form Operation
Addition + x+y Adds value of y to value of x.
Subtraction – x–y Subtracts value of y from value of x.
Multiplication * x*y Multiplies value of x by value of y.
Division / x/y Divides value of x by value of y.
Modulus % x%y Divides value of x by value of y and gives remainder.
Integer Arithmetic
When both the operands are integers, the operations will be called integer arithmetic and
will always yield an integer value.
Real Arithmetic
When both the operands are real numbers, the operations will be called real
arithmetic and will always yield a real value.
That is
6.0 / 7.0 = 0.857143
-1.0 / 3.0 = -0.333333
3.0 / -2.0 = -1.500000
Mixed-mode Arithmetic
When one of the operands is real and the other is integer, the operations will be
called mixed-mode arithmetic and will always yield a real value.
The integer operand is converted into real operand and then real arithmetic is
performed, resulting in a real value.
pg. 2
That is
6 / 7.0 6.0 / 7.0 = 0.857143
-1.0 / 3 -1.0 / 3.0 = -0.333333
Note that
15 / 10.0 = 1.5
15.0 / 10 = 1.5
where as
15 / 10 = 1
Logical Operators
These operators are used to form compound conditions by joining two or more simple
conditions formed using relational operators.
Logical Operators
Operator Symbol Form Meaning
Logical AND && x && y Return True if both the operands are True.
Logical OR || x || y Return True if either of the operands is True.
Return True if operand is false, and False if
Logical NOT ! !x
operand True (complements the operand).
pg. 3
Bitwise Operators
Bitwise operators act on operands as if they were string of binary digits. It operates bit by
bit. Note that these operators are used with integral operands only.
To demonstrate the working of bitwise operators, let x = 10 (0000 1010 in binary) and
y = 4 (0000 0100 in binary), assuming that 8 bits are used to represent an integer number.
Bitwise Operators
Increment operator (++) and Decrement operator (--). These operators can be used with
all basic data types. The increment operator adds 1 to the operand, while decrement
operator subtracts 1 from the operand.
pg. 4
Both operators are unary operators and can be used in the prefix as well as postfix
notation as shown below:
++k; or k++;
--k; or k--;
Here, ++k (as well as k++) is equivalent to k = k + 1 or k += 1, and --k (as well as k--)
is equivalent to k = k - 1 or k -= 1.
These operators are most frequently used in while and for loops as control variables.
While ++k or k++ means the same thing when they form statements independently, they
behave differently when they are used as part of other expressions.
Consider the following statements
int y, k = 5;
y = ++k;
In this case, first the value of k is incremented and then assigned to y, and hence y and k
will have same value as 6. Thus, the above statements are equivalent to following
statements
int y, k = 5;
++k;
y = k;
then first the current value of k (value 5) is assigned to y and then the value of k is
incremented, and hence y will have value 5 while k will have value as 6.
returns value 4.
pg. 5
Use of sizeof Operator
sizeof(int) 2
Size of data type int is 2 on Turbo C/C++ and 4 on Dev
C++.
sizeof(125.25) 8 Real constant by default belong to data type double.
sizeof(125.25F) 4 Real constant is of data type float, and size for float is 4.
double x;
8 Variable x is of type double, and size for double is 8.
sizeof(x);
It is used in the scanf() function and to initialize the pointers. The use of addressof
(&) operator is demonstrated in
Character '*' when prefixed with a pointer variable returns the value stored in a memory
location whose address is held in pointer variable. That is, value is accessed through pointer
variable indirectly, hence its name indirection operator.
int y = 10, x;
int *py;
py = &y;
x = *py;
The expression "*py" returns the values at address which held in pointer variable py, i.e.,
value of variable y which is 10, gets assigned to variable x.
Use of addressof (&) Operators
pg. 6
of variable x and y, respectively.
Conditional/Ternary Operator
Consider the following segment,
if ( a > b ) then
set big = a
else
set big = b
endif
This segments assigns the maximum of the values of a and b to big. It is clear that
the value assigned to variable big will depend on the outcome of the test condition
"a > b".
Such expressions are known as conditional expressions, and can be written using
conditional operator. The syntax of using ternary operator is
exp1 ? exp2 : exp3;
The expression exp1 is evaluated first. If it is non-zero (true), then the expression exp2 is
evaluated, and that is the value of the conditional expression; otherwise expression exp3 is
evaluated, and that is the value of the conditional expression. Note that only one of the
expression exp2 and exp3 is evaluated.
Thus, the above pseudocode segment using conditional operator can be written as
big = ( a > b ) ? a : b;
Assignment Operators
a = 5
Here '=' is a simple assignment operator that assigns the value 5 to the variable a. It can
also be used to assign value of another variable or an expression.
that adds to the variable and later assigns the same. It is equivalent to
a = a + 5
pg. 7
Operators of type '+=' are also called shorthand assignment operators.
Assignment operator
+= x += 5 x = x + 5 |= x |= 5 x = x | 5
-= x -= 5 x = x - 5 ^= x ^= 5 x = x ^ 5
%= x %= 5 x = x % 5
EXPRESSIONS
An expression is a formula consisting of one or more operands and zero or more operators
linked together to compute a value. An operand may be a function reference, a variable, an
array element or a constant.
1. Arithmetic expressions
2. Relational expressions
3. Logical expressions
4. Conditional expressions
Each type of expression takes certain types of operands and uses a specific set of
operators. Evaluation of every expression produces a value of specific type. Expressions
are not statements, but may be components of statements.
For example, consider the line
x = 2.0/3.0 + a * b;
pg. 8
Arithmetic Expressions
Evaluation of Expressions
Precedence
The concept of precedence is well defined in the subject of mathematics.
The rule BODMAS – Brackets, Of, Division, Multiplication, Addition, and
Subtraction.
This expression consists of one addition and one multiplication operator. As you know
from the knowledge of algebra, multiplication has higher precedence than addition,
multiplication is performed before addition.
pg. 9
Therefore, the expression will be evaluated as
( 5 + ( 3 * 4 ) ) ( 5 + 12 ) 17
Operation by Operation
Description of Each Operation
Evaluation of Expression
5 * 4 / ( 1 + 5 * 2 / 3 + 6 ) + 8 * ( 7 / 4 ) Given expression
5 * 4 / ( 1 + 10 / 3 + 6 ) + 8 * ( 7 / 4 ) 5 is multiplied by 2, giving10
5 * 4 / ( 4 + 6 ) + 8 * ( 7 / 4 ) 3 is added to 1, giving 4
5 * 4 / 10 + 8 * ( 7 / 4 ) 6 is added to 4, giving 10
20 / 10 + 8 * 1 5 is multiplied by 4, giving 20
2 + 8 8 is multiplied by 1, giving 8
Associativity is used to determine the order in which different operators with same
precedence are evaluated in an expression.
Precedence is applied before associativity to determine the order in which expressions are
evaluated. Associativity is applied later, if necessary.
pg. 10
Associativity
Associativity is applied when more than one operator of same precedence is used
in an expression.
Associativity can be left-to-right or right-to-left.
Left-to-right associativity evaluates the expression by starting on the left and
moving to the right whereas the right-to-left associativity evaluates the expression
by starting on the right and moving to the left.
The following is a simple example of associativity:
2 + 5 + 7
This expression consists of two addition operators. Here the associativity determines how
the sub expressions are grouped together. Since the addition operator has left-to-right
associativity, the expression will be grouped as
( ( 2 + 5 ) + 7 ) ( 7 + 7 ) 14
pg. 11
Precedence and Associativity of Operators
Precedence
Operator Description Associativity
Level (Rank)
() Function call
[] Array element reference
> Structure operator used with pointer to a structure 1 Left-to-right
. Structure operator used with structure variable
! Logical NOT operator
~ 1's complement
++ Increment
Decrement
+ Unary plus 2 Right-to-left
Unary minus
* Pointer reference (indirection)
& Address of
(type) Type cast
sizeof Size of an operand
* Multiplication
/ Division 3 Left-to-right
% Modulus (remainder)
+ Addition
4 Left-to-right
Subtraction
<< Left shift
5 Left-to-right
>> Right shift
< Less than
<= Less than or equal to
6 Left-to-right
> Greater than
>= Greater than or equal to
== Equal to
7 Left-to-right
!= Not equal to
& Bitwise AND 8 Left-to-right
^ Bitwise exclusive OR 9 Left-to-right
| Bitwise inclusive OR 10 Left-to-right
&& Logical AND 11 Left-to-right
|| Logical OR 12 Left-to-right
?: Condition (ternary) 13 Right-to-left
= +=
= *=
/= %=
Assignment operators 14 Right-to-left
&= ^=
!= <<=
>>=
pg. 12
EXAMPLES
Example 1: Swap values of two variables ‘a’ & ‘b’ using third variable ‘t’.
Solution:
Statement No. a b t
int a=10,b=20,t; /* 1 */ 1 10 20 ?
t = a; /* 2 */ 2 10 20 10
a = b; /* 3 */ 3 20 20 10
b = t; /* 4 */ 4 10 20 10
Example 2: Swap value of two variables ‘a’ & ‘b’ without using third variable and using
arithmetic addition (+) and subtraction () operations only.
Solution:
Example 3: Swap value of two variables ‘a’ & ‘b’ without using third variable and using
arithmetic multiplication (*) and division (/) operations only.
Solution:
Statement No. a b
int a=10,b=20; /* 1 */ 1 10 20
a = a * b; /* 2 */
2 200 20
b = a / b; /* 3 */ 3 200 10
a = a / b; /* 4 */ 4 20 10
This task can also be implemented using following single statement:
a = ( a * b ) / ( b = a );
pg. 13
Example 4: To find the largest of three numbers among a, b, and c.
Solution:
int a = 10,b = 30,c = 25, big;
big = (a>b)?((a>c)?a:c)):((b>c)?b:c));
Example 5: Write appropriate statements to find the sum of the right-most digit and left-
most digit of a 4-digit number stored in n.
Solution:
left_most_digit = n / 1000;
right_most_digit = n % 1000;
sum = left_most_digit + right_most_digit;
float c, f;
f = 1.8 * C + 32;
pg. 14
C PROGRAM FOR PRACTICALS
1. Write a program to find simple and compound interest.
Solution: The formulae to compute simple and the compound interest are
p r t , and
Simple interest
100
r
t
where p is the principle amount, r is the rate of interest per annum, and t is the time of
deposit in years.
Program 1
/* program to compute simple and compound interest */
#include <stdio.h>
#include <math.h>
int main()
{
float p, r, si, ci;
int t;
printf("\nEnter principle amount : ");
scanf("%f", &p);
printf("Enter rate of interest : ");
scanf("%f", &r);
printf("Enter principle amount : ");
scanf("%d", &t);
si = (p*r*t)/100;
ci = p*(pow(1+r/100,t)-1);
printf("\nsimple interest = Rs. %.2f", si);
pg. 15
Result
Enter principle amount : 1000
Enter rate of interest : 5
Enter principle amount : 4
2. Write a program to find the area of a triangle whose measure of three sides is
given as a, b, and c, respectively. The values of a, b, and c must satisfy the
condition that
a + b > c and b + c > a and c + a > b
abc
s
2
area s ( s a)(s b)(s c)
Program 2
/* program to compute area of a triangle whose sides are
given */
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, s, area;
printf("\nEnter value for a : ");
scanf("%f", &a);
printf("Enter value for b : ");
scanf("%f", &b);
printf("Enter value for c : ");
scanf("%f", &c);
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nArea of triangle = %.2f Sq. Units", area);
return 0;
}
pg. 16
Result
pg. 17