C Operators, Precedence & 6 Real-World Practice Programs
Welcome to Phase 3 (Part 2): C Operators, Expression Evaluation & 6 Practice Programs Masterclass! Operators are the fundamental building blocks that allow you to perform computations, make logical decisions, and manipulate raw binary bits in memory. In this comprehensive guide, you will master all C operator categories (Arithmetic, Relational, Logical, Bitwise, Ternary), prefix vs postfix increment/decrement mechanics, operator precedence & associativity rules, and construct 6 real-world practical computation programs.
C language provides 6 major families of operators:
+(Addition),-(Subtraction)*(Multiplication),/(Division)%(Modulus / Remainder) โ ๏ธ Integers only!
==(Equal to),!=(Not equal)>,<,>=,<=- Returns
1for True,0for False
&&(Logical AND โ Short-circuit!)||(Logical OR โ Short-circuit!)!(Logical NOT / Inversion)
&(Bitwise AND),|(Bitwise OR)^(XOR),~(NOT / Invert bits)<<(Left Shift),>>(Right Shift)
โก Prefix (++x) vs Postfix (x++) Mechanics
โข Prefix (++x): Value ni First increment chesi, tharvatha current expression lo use chesthundhi ("Increment first, use later").
โข Postfix (x++): Current expression lo Original value ni use chesi, tharvatha increment chesthundhi ("Use first, increment later").
Conditional (Ternary) Operator: condition ? expr1 : expr2
Simple if-else conditions ni single-line expression ga rayadaniki vadathamu:
int max = (a > b) ? a : b;
Complex expressions lo eeh operator mundhu evaluate avvali anedhi Precedence decide chesthundhi:
| Priority | Operators | Description | Associativity |
|---|---|---|---|
| 1 (Highest) | (), [], ->, . | Parentheses, Array Subscript, Member Access | Left to Right |
| 2 | ++, --, !, ~, +, -, (type), *, &, sizeof | Unary operators, Type cast, Address, Dereference | Right to Left |
| 3 | *, /, % | Multiplication, Division, Modulus | Left to Right |
| 4 | +, - | Addition, Subtraction | Left to Right |
| 5 | <<, >> | Bitwise Left and Right Shifts | Left to Right |
| 6 | <, <=, >, >= | Relational Comparisons | Left to Right |
| 7 | ==, != | Equality / Inequality | Left to Right |
| 8 | & | Bitwise AND | Left to Right |
| 9 | ^ | Bitwise XOR | Left to Right |
| 10 | | | Bitwise OR | Left to Right |
| 11 | && | Logical AND | Left to Right |
| 12 | || | Logical OR | Left to Right |
| 13 | ?: | Ternary Conditional | Right to Left |
| 14 (Lowest) | =, +=, -=, *=, /=, %= | Assignment & Compound Assignments | Right to Left |
Practical implementation of arithmetic and operator logic across 6 essential algorithms:
๐ Formulas Used in the 6 Programs:
1. Add Two Numbers: $Sum = a + b$
2. Simple Interest: $SI = rac{P imes T imes R}{100}$
3. Rectangle Area & Perimeter: $Area = L imes W$, $Perimeter = 2(L + W)$
4. Celsius to Fahrenheit: $F = (C imes rac{9.0}{5.0}) + 32.0$
5. Student Average: $Avg = rac{M_1 + M_2 + M_3}{3.0}$
6. Bill Calculator: $Final = Subtotal + (Subtotal imes rac{GST%}{100})$
#include <stdio.h>
int main(void) {
// 1. Add Two Numbers
int num1 = 45, num2 = 30;
printf("1. Sum of %d + %d = %d\n", num1, num2, num1 + num2);
// 2. Simple Interest: P = 10000, T = 2 years, R = 7.5%
double P = 10000.0, T = 2.0, R = 7.5;
double SI = (P * T * R) / 100.0;
printf("2. Simple Interest on Rs.%.0f: Rs.%.2f\n", P, SI);
// 3. Rectangle Area & Perimeter (L = 12.5, W = 6.0)
double length = 12.5, width = 6.0;
double area = length * width;
double perimeter = 2.0 * (length + width);
printf("3. Rectangle Area = %.2f sq.units | Perimeter = %.2f units\n", area, perimeter);
// 4. Celsius to Fahrenheit (37ยฐC normal body temp)
double celsius = 37.0;
double fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
printf("4. 37ยฐC in Fahrenheit = %.1fยฐF\n", fahrenheit);
// 5. Student Average Marks (85, 92, 78)
int m1 = 85, m2 = 92, m3 = 78;
double average = (m1 + m2 + m3) / 3.0; // Using 3.0 avoids integer division!
printf("5. Student Average: %.2f%%\n", average);
// 6. Supermarket Bill Calculator with 18% GST
double itemPrice = 1250.0;
int quantity = 3;
double subtotal = itemPrice * quantity;
double finalBill = subtotal + (subtotal * 0.18);
printf("6. Bill: Subtotal = Rs.%.2f | With 18%% GST = Rs.%.2f\n", subtotal, finalBill);
return 0;
}
Test arithmetic operations, Celsius conversion, and student average in our live GCC compiler:
#include <stdio.h>
int main(void) {
double celsius = 100.0; // Boiling point of water
double fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
printf("%.1fยฐC = %.1fยฐF\n", celsius, fahrenheit);
return 0;
}