Unary operator
Works with one operand.
Operators tell C to perform a calculation, comparison, logical decision, assignment, or bit-level operation.
total = a + b;, which symbol performs addition?Works with one operand.
Works with two operands.
Uses three expressions.
Perform numeric calculations.
Compare two values.
Combine or reverse conditions.
Store or update a value.
Increase or decrease by one.
Operate on individual bits.
Select one of two expressions.
Special purposes such as size and addresses.
When both operands are integers, the fractional part of division is discarded.
| Expression | C result | Meaning |
|---|
| A | B | A && B | A || B |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 |
Stores the right-hand value in the left-hand variable.
Combines an operation with assignment.
Several arithmetic and bitwise operators have assignment forms.
y = ++x;y = x++;()Parentheses++ -- ! ~Unary* / %Multiplicative+ -Additive<< >>Shift< <= > >= == !=Comparison& ^ | && ||Bitwise and logical?: =Conditional and assignmentBitwise operators inspect or modify individual binary digits. Use unsigned values for the clearest introductory examples.
| A | B | A & B | A | B | A ^ B |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Result bit is 1 only when both input bits are 1.
Result bit is 1 when at least one input bit is 1.
Result bit is 1 when the input bits are different.
(~A) & 0xFFu. Actual unsigned integer width depends on the C implementation.For an unsigned value when no significant bit is lost, shifting left by one corresponds to multiplication by 2.
For a non-negative unsigned value, shifting right by one corresponds to integer division by 2.
&& and || short-circuit& is not a replacement for &&, and | is not a replacement for ||.The conditional operator evaluates one condition and selects one of two expressions.
marks >= 40"Pass""Fail"Useful when choosing one of two values in a short, readable expression.
Preferable when each branch performs multiple statements or needs clearer control flow.
i = i++; are not a safe way to increment a variable.Calculates numeric results.
Produces 0 or 1.
Combines conditions.
Processes individual bits.
Selects one of two expressions.
Make evaluation order clear.