Operators, Expressions & Precedence in C
Deep dive into Arithmetic, Relational, Logical, Bitwise, Assignment, Ternary operators, and the Operator Precedence table.
- Arithmetic operators include
+,-,*,/, and modulus%(integers only). - Prefix (
++x) increments before value usage; postfix (x++) evaluates current value first. - Logical operators (
&&,||) use short-circuit evaluation. - Bitwise operators (
&,|,^,~,<<,>>) manipulate data at the individual bit level. - Ternary operator
condition ? exp1 : exp2provides concise conditional branching.
Major Operator Categories in C
Operators tell the compiler to perform specific mathematical, relational, or logical manipulations:
- 1Arithmetic:
+,-,*,/,%(remainder/modulo). Note:%only works with integer operands. - 2Relational:
==(equal),!=(not equal),>,<,>=,<=. Evaluate to1(true) or0(false). - 3Logical:
&&(AND): Returns 1 if both operands are non-zero.||(OR): Returns 1 if at least one operand is non-zero.!(NOT): Inverts boolean truth value.
- 4Bitwise:
&(AND),|(OR),^(XOR),~(NOT / 1's complement).<<(Left shift by $n$ bits, multiplies integer by $2^n$).>>(Right shift by $n$ bits, divides integer by $2^n$).
- 5Conditional / Ternary:
condition ? expr1 : expr2;
Writing `x = x++ + ++x;` or `printf("%d %d", x++, x++);` causes undefined behaviour in C because there is no sequence point between modifications. Never modify the same variable twice within a single expression!
Bitwise Operators & Operator Precedence
Bitwise operations are critical in systems programming, embedded firmware, cryptography, and network packet parsing.
Test Your Understanding
Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.
Your Current Score
0 / 2 correct(0 attempted)
What is the result of 17 % 5 in C?
BeginnerWhat will (8 >> 2) evaluate to in C?
Intermediate