MCS-201 · Unit 2

Data Types, Operators and Expressions

Section 11 of 11

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

2.11The sizeof Operator, Shorthand Assignment and Precedence

Covers IGNOU MCS-201 §2.18, 2.19, 2.20

This final section completes the operators of the unit: sizeof, which reports storage sizes, the compound (shorthand) assignment operators, and the full precedence table that tells you how any mixed expression is grouped.

The sizeof operator

The sizeof operator is a keyword, not a function. Parentheses are required around a type name (sizeof(int)) but optional around a variable (sizeof x). Print its result with %zu. On typical 64-bit systems, sizeof(char) is 1 (always, by definition), sizeof(int) is 4 and sizeof(double) is 8.

Compound (shorthand) assignment

A compound assignment combines an arithmetic operator with =:

OperatorExampleMeaning
+=a += 2a = a + 2
-=a -= 2a = a - 2
*=a *= 2a = a * 2
/=a /= 2a = a / 2
%=a %= 2a = a % 2

The right-hand side is evaluated completely first: a *= b + 1 means a = a * (b + 1), not a = a * b + 1. C also has <<=, >>=, &=, ^= and |= for the bitwise operators, which appear in later units.

Operator precedence and associativity

The table lists the operators of this unit from highest to lowest precedence. Operators on the same row have equal precedence.

LevelOperatorsAssociativity
1() (function call, grouping)Left to right
2! ++ -- unary + - (type) sizeofRight to left
3* / %Left to right
4+ -Left to right
5< <= > >=Left to right
6== !=Left to right
7&&Left to right
8||Left to right
9?:Right to left
10= += -= *= /= %=Right to left
11,Left to right

A useful memory aid for the order of groups: Unary, Arithmetic (multiplicative before additive), Relational, Equality, Logical (AND before OR), Conditional, Assignment, Comma.

Key takeaways

  • sizeof gives a size in bytes as a size_t; print it with %zu.
  • x op= y means x = x op (y) for the arithmetic operators; there is no &&= or ||=.
  • Precedence order: unary, arithmetic, relational, equality, &&, ||, ?:, assignment, comma.
  • Unary, conditional and assignment operators associate right to left; the others left to right.

Practice questions

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

Question 1 · multiple choice

After int x = 10; x -= 3 * 2;, what is x?

Show answer

Answer: B. 4

The right side is evaluated first: 3 × 2 = 6, then x = 10 - 6 = 4. Applying -= before the multiplication would give 14.

Question 2 · multiple choice

Which is NOT a valid C operator?

Show answer

Answer: C. &&=

C has no &&= (or ||=); compound assignment exists only for arithmetic, shift and bitwise operators.

Question 3 · multiple choice

What is the value of 2 + 3 * 4 > 10 || 0?

Show answer

Answer: D. 1

3 * 4 is 12, 2 + 12 is 14, 14 > 10 is 1, and 1 || 0 is 1. Adding before multiplying would give the wrong intermediate 20.

Question 4 · multiple choice

Which statement about sizeof is correct?

Show answer

Answer: B. It is a unary operator and a keyword

sizeof is a keyword and a unary operator, usually evaluated at compile time; its size_t result is printed with %zu. The size of int depends on the system.

Question 5 · multiple choice

What does this program print?

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

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

Answer: C. 2 8

a %= b makes a = 6 % 4 = 2; then b += a * 2 makes b = 4 + 4 = 8.

Question 6 · short answer

Fully parenthesise and evaluate a = 10, b = 3; r = a / b * b + a % b == a; (with integer variables).

Show worked answer

By precedence: *, /, % first, left to right, then +, then ==, then =. Fully parenthesised: r = ((((a / b) * b) + (a % b)) == a). Evaluate: a / b = 3; 3 * b = 9; a % b = 1; 9 + 1 = 10; 10 == a is 10 == 10, which is 1. So r is 1. This illustrates the identity (a / b) * b + a % b == a for integer division.

Question 7 · short answer

Explain the difference between && and & with 6 && 3 and 6 & 3.

Show worked answer

&& is the logical AND: both operands are non-zero, so 6 && 3 is 1, and it would stop early if the left operand were 0. & is the bitwise AND: it combines corresponding bits. 6 is binary 110 and 3 is 011; ANDing each bit gives 010, which is 2. So 6 && 3 is 1 but 6 & 3 is 2.

↑ ↓ move↵ openTab switch type