Operators & Expressions
C's Toolbox
If Python gives you a neat little toolkit, C gives you the entire hardware store. You get the usual arithmetic and comparison operators, but you also get direct access to bitwise operators β tools that let you flip individual bits in memory. This is why C is the language of operating systems and embedded devices.
Let's go through each category, from the everyday hammers to the power tools.
Arithmetic Operators
The basics: + (add), - (subtract), * (multiply), / (divide), and % (modulo β remainder after division). These work just like you'd expect, with one important catch...
Arithmetic β Watch the Integer Division!
/ throws away the decimal part. 5 / 2 gives 2, not 2.5. If you need the decimal, cast at least one operand to double: (double)5 / 2 gives 2.5.Relational & Logical Operators
Relational operators compare two values and return 1 (true) or 0 (false). Logical operators combine conditions. Remember β in C, there's no True/False keywords by default, just 1 and 0.
Comparisons and Logic
Increment & Decrement
The ++ and -- operators add or subtract 1. But where you place them matters: ++x (prefix) increments before using the value, while x++ (postfix) uses the value then increments. This is a classic C interview question.
Prefix vs Postfix
Assignment Operators
Shorthand for updating a variable: +=, -=, *=, /=, %=, and the bitwise variants &=, |=, ^=, <<=, >>=.
Compound Assignment
Bitwise Operators β The Power Tools
These operators work on individual bits β the 0s and 1s that make up every number in memory. They're essential for systems programming, embedded devices, and performance-critical code.
&(AND) β Both bits must be 1|(OR) β At least one bit must be 1^(XOR) β Exactly one bit must be 1~(NOT) β Flip all bits<<(left shift) β Shift bits left (multiply by 2)>>(right shift) β Shift bits right (divide by 2)
Bitwise Operations
x & 1 == 0 is not (x & 1) == 0 β it's actually x & (1 == 0) because == has higher precedence than &. When in doubt, use parentheses. Your future self will thank you.