MCS-201 · Unit 2

Data Types, Operators and Expressions

Section 6 of 11

Block 1 · Unit 2Data Types, Operators and Expressions11 min · Section 6 of 11

2.6Expressions, Assignment and Arithmetic Operators

Covers IGNOU MCS-201 §2.11, 2.12, 2.13

Expressions are how a program calculates. This section introduces the vocabulary (expression, operator, operand), the assignment operator, and the five arithmetic operators, with the exact rules C uses for integer division, remainders, mixed types and order of evaluation.

Expressions, operators and operands

An operator is a symbol that performs an operation; an operand is a value it operates on. In a + 5, + is the operator and a and 5 are operands. Operators are classified by how many operands they take:

KindOperandsExamples
Unary1-x, !flag, ++i
Binary2a + b, x > y
Ternary3c ? a : b

The assignment operator

variable = expression; evaluates the right-hand side and stores the result in the variable on the left. The left side must be something that can hold a value (a variable), not a constant or an expression: 5 = a; and a + b = c; are errors.

An assignment is itself an expression whose value is the value stored, so a = b = 0; sets both to 0 (assignment groups from right to left).

When the types differ, the value is converted to the type of the variable on the left: int i = 4.6; stores 4 (the fraction is dropped), and float b = 20; stores 20.0.

Arithmetic operators

OperatorMeaningExampleResult
+Addition7 + 29
-Subtraction7 - 25
*Multiplication7 * 214
/Division7 / 23 (integers)
%Remainder (modulus)7 % 21

So 10 / 3 is 3, 10 % 3 is 1, 15 % 4 is 3, -7 / 2 is −3, and -7 % 2 is −1 (the remainder takes the sign of the dividend). Dividing by zero is undefined behaviour. C has no exponent operator: a**b is an error, and powers are written with repeated multiplication or the pow function from <math.h>.

Mixed types

When an operator has one integer and one floating operand, the integer is converted and real arithmetic is used:

ExpressionResult
5 / 31
5.0 / 31.666667
5 / 3.01.666667
5.0 / 3.01.666667

The type of each operation depends only on its own operands. The variable on the left of = does not change how the right-hand side is calculated; the finished result is converted only when it is stored.

Precedence and associativity of arithmetic operators

Precedence decides which operator is applied first; associativity decides the order for operators of equal precedence.

  1. Parentheses first; the innermost pair first.
  2. *, / and %: these three have equal precedence, so evaluate them left to right in the order they appear.
  3. + and -: these two have equal precedence, so evaluate them left to right in the order they appear. Neither one comes “before” the other; in 10 - 4 + 2 the - is done first only because it is further left, giving 8.

Algebraic expressions must be written in a single line with explicit *:

AlgebraC
a⋅4c2−dm+n\dfrac{a \cdot 4c^2 - d}{m + n}(a * 4 * c * c - d) / (m + n)
ab−(e+f)⋅4cab - \dfrac{(e + f) \cdot 4}{c}a * b - (e + f) * 4 / c
ab+c\dfrac{a}{b + c}a / (b + c)

Key takeaways

  • An expression combines operands and operators; operators are unary, binary or ternary.
  • = stores the right-hand value in the left-hand variable, converting it if needed.
  • Integer / truncates towards zero; % gives the remainder and works only on integers.
  • *, /, % come before +, -; equal precedence goes left to right; parentheses override.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the value of 17 % 5?

Show answer

Answer: B. 2

17 = 3 × 5 + 2, so the remainder is 2. 3 is the quotient, 17 / 5.

Question 2 · multiple choice

What is stored in x after int x = 9.99;?

Show answer

Answer: C. 9

Converting a floating value to int discards the fraction; it does not round, so the tempting answer 10 is wrong.

Question 3 · multiple choice

Which C expression correctly represents a+bc−d\dfrac{a + b}{c - d}?

Show answer

Answer: D. (a + b) / (c - d)

Both the numerator and the denominator need parentheses. Without them, division happens before addition and subtraction.

Question 4 · multiple choice

What does this program print?

C · int_div.c
#include <stdio.h>

int main(void)
{
    int a = 7, b = 2;
    float r = a / b;
    printf("%.1f\n", r);
    return 0;
}
Show answer

Answer: C. 3.0

a / b uses two integers, giving 3, which is then stored in the float as 3.0. The float variable does not make the division real.

Question 5 · multiple choice

Which line contains an error?

C · power_error.c
#include <stdio.h>

int main(void)
{
    int a = 2, b = 3, e;
    e = a**b;
    printf("%d\n", e);
    return 0;
}
Show answer

Answer: B. Line 6

C has no ** operator; the compiler reads a * (*b), which is invalid for an int. Use a * a * a or pow from <math.h>.

Question 6 · short answer

Evaluate 3 + 4 * 5 % 3 - 6 / 4 step by step, with integer operands.

Show worked answer

First the operators *, % and /, from left to right: 4 * 5 = 20; then 20 % 3 = 2; then 6 / 4 = 1 (integer division). The expression becomes 3 + 2 - 1. Then + and - from left to right: 3 + 2 = 5, and 5 − 1 = 4. The value is 4.

↑ ↓ move↵ openTab switch type