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 =:
Operator
Example
Meaning
+=
a += 2
a = a + 2
-=
a -= 2
a = a - 2
*=
a *= 2
a = a * 2
/=
a /= 2
a = a / 2
%=
a %= 2
a = 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.
Level
Operators
Associativity
1
() (function call, grouping)
Left to right
2
!++-- unary +-(type)sizeof
Right 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 ||=.
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 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.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/data-types-operators-and-expressions/the-sizeof-operator-shorthand-assignment-and-precedence/ to read this page.