Build expressions whose meaning is clear
An expression combines values and operators to compute a result. The result depends on operand types as well as the symbols you write. This lesson covers arithmetic, comparisons, logic and bit operations without relying on ambiguous side effects.
Arithmetic and remainder
+, -, * and / perform arithmetic. Integer division truncates toward zero: 7 / 3 is 2 and -7 / 3 is -2. The remainder operator % takes integer operands. In a = (a/b)*b + a%b, the identity applies when division is defined. Never divide or take remainder by zero.
Comparison and logical conditions
== checks equality; = assigns. Relational and logical operators produce int values 0 or 1. && and || short-circuit, so their right operand is evaluated only if needed. Use that property to guard an operation: count != 0 && total / count > 5.
Complete example
#include
int main(void) {
int earned = 7, available = 3;
double ratio = (double)earned / available;
unsigned flags = 0u;
flags |= 1u << 2;
printf("quotient=%d remainder=%d\n", earned / available, earned % available);
printf("ratio=%.2f enabled=%d\n", ratio, (flags & (1u << 2)) != 0u);
return 0;
} Expected lines: quotient=2 remainder=1 and ratio=2.33 enabled=1. The cast changes the division before assignment. The unsigned mask sets and tests bit 2 without changing the other bits.
Precedence is not evaluation order
Multiplication has higher precedence than addition: 2 + 3 * 4 is 14. Parentheses make grouping explicit. They do not establish the order of unrelated side effects. Do not write expressions such as i++ + i++, whose unsequenced modifications cause undefined behavior. Split updates into separate statements.
Bitwise & and | operate on bits; logical && and || test truth. For shifts, use unsigned operands and ensure the shift count is smaller than the width of the promoted left operand. A negative or excessive shift count is invalid.
Exercises and answers
10 % 4is 2.(double)(5 / 2)is 2.0, while(double)5 / 2is 2.5.- To check whether an integer lies from 10 through 20, write
x >= 10 && x <= 20, not a chained mathematical inequality. - Test an even number with
x % 2 == 0.
हिन्दी संकेत: assignment और comparison अलग हैं। Confusing expressions को छोटे statements में तोड़कर पढ़ें और test करें।