Operators in C
π’ Beginner
π Definition
An operator is a symbol that tells the C compiler to perform specific mathematical, relational, bitwise, or logical transformations on data values called operands.
π Multilingual Explanation
English
C provides rich operators for arithmetic, comparisons, logical evaluation, assignment, and bitwise manipulation. In C integer division (int / int), fractional decimal parts are truncated (dropped). Explicit type casting (e.g. (double)a / b) is required to preserve decimal precision.
Hindi
C mein alag-alag kaam karne ke liye operators hote hain. Arithmetic, relational, logical, assignment aur bitwise operators isme shamil hain. Two integers ko divide karne par integer division hota hai (5 / 2 = 2). Decimal value pane ke liye type casting (double)a / b zaroori hai.
Marathi
C madhye vegvegale calculations sathi operators vaparale jatat. Arithmetic, relational, logical, assignment aani bitwise operators hi tyanchi prakar aahet. Don integers cha bhagakar nehmi integer milto (5 / 2 = 2). Decimal uttarasathi type casting (double)a / b kareve lagte.
Hinglish
Operators data variables par operations perform karte hain. Logical comparison ke liye == aur assignment ke liye = use hota hai. Integer division fractional values drop kar deta hai (15 / 4 = 3), isliye exact answer ke liye (float)15 / 4 cast karna padta hai.
π€ Why Do We Use Them?
Operators are the building blocks of algorithms. They calculate total prices, compare user scores, verify access permissions, evaluate complex formulas, and manipulate hardware register flags.
π§ Simple Explanation
- Arithmetic Operators: Perform math like addition (
+) or subtraction (-). - Relational Operators: Ask questions like βIs
aequal tob?β (a == b) or βIsagreater?β (a > b). The answer is always1(true) or0(false). - Logical Operators: Combine multiple conditions like βIs user logged in AND is admin?β (
isLoggedIn && isAdmin).
π Operator Categories in C
1. Arithmetic Operators
| Operator | Name | Example | Result (a=10, b=3) |
|---|---|---|---|
+ |
Addition | a + b |
13 |
- |
Subtraction | a - b |
7 |
* |
Multiplication | a * b |
30 |
/ |
Division | a / b |
3 (Integer division drops .333!) |
% |
Modulus (Remainder) | a % b |
1 (10 divided by 3 leaves remainder 1) |
2. Relational & Equality Operators
| Operator | Name | Example | Evaluation |
|---|---|---|---|
== |
Equal To | a == b |
0 (false) |
!= |
Not Equal To | a != b |
1 (true) |
> |
Greater Than | a > b |
1 (true) |
< |
Less Than | a < b |
0 (false) |
>= |
Greater Than or Equal | a >= 10 |
1 (true) |
<= |
Less Than or Equal | b <= 2 |
0 (false) |
3. Logical Operators
| Operator | Name | Meaning | Result |
|---|---|---|---|
&& |
Logical AND | Returns 1 only if both conditions are true |
(10 > 5 && 3 < 5) -> 1 |
\|\| |
Logical OR | Returns 1 if at least one condition is true |
(10 < 5 \|\| 3 < 5) -> 1 |
! |
Logical NOT | Inverts boolean truth value (!1 -> 0, !0 -> 1) |
!(10 == 10) -> 0 |
4. Increment & Decrement Operators
- Prefix (
++x/--x): Increments/decrements the variable before evaluating the expression. - Postfix (
x++/x--): Evaluates expression using current value first, then increments/decrements.
π‘ Practical Example
Here is a store checkout calculation script demonstrating arithmetic, ternary, assignment, and bitwise operators:
#include <stdio.h>
int main(void) {
int item1Price = 450;
int item2Price = 350;
int quantity1 = 2;
int quantity2 = 1;
// 1. Arithmetic calculations
int rawTotal = (item1Price * quantity1) + (item2Price * quantity2); // (900 + 350) = 1250
// 2. Integer division vs Type Casting
int totalItems = quantity1 + quantity2; // 3
int intAverage = rawTotal / totalItems; // 1250 / 3 = 416 (truncated)
double exactAverage = (double)rawTotal / totalItems; // 416.6667
// 3. Ternary Operator (Condition ? trueValue : falseValue)
double discountPercentage = (rawTotal >= 1000) ? 10.0 : 0.0;
double discountAmount = rawTotal * (discountPercentage / 100.0);
double finalBill = rawTotal - discountAmount;
// 4. Increment and Compound Assignment
int invoiceNumber = 1001;
invoiceNumber++; // invoiceNumber becomes 1002
printf("--- STORE RECEIPT (Invoice #%d) ---\n", invoiceNumber);
printf("Raw Total Cost : $%d\n", rawTotal);
printf("Average/Item (int): $%d\n", intAverage);
printf("Average/Item (exact): $%.2lf\n", exactAverage);
printf("Discount Applied : %.0lf%%\n", discountPercentage);
printf("Discount Amount : $%.2lf\n", discountAmount);
printf("Final Bill Amount : $%.2lf\n", finalBill);
return 0;
}
π Code Breakdown
rawTotal / totalItems: Performs integer division (1250 / 3), truncating the decimal portion resulting in416.(double)rawTotal / totalItems: TypecastsrawTotalto adoublebefore division, forcing C to perform double-precision floating-point division resulting in416.666667.(rawTotal >= 1000) ? 10.0 : 0.0: Ternary operator checks ifrawTotalis at least 1000. If1(true), returns10.0; otherwise returns0.0.
π Output
--- STORE RECEIPT (Invoice #1002) ---
Raw Total Cost : $1250
Average/Item (int): $416
Average/Item (exact): $416.67
Discount Applied : 10%
Discount Amount : $125.00
Final Bill Amount : $1125.00
β οΈ Common Mistakes
- Confusing
=and==: Using=(assignment) inside anifcondition likeif (status = 5)assigns5tostatusand always evaluates totrue! Always use==for comparison:if (status == 5). - Bitwise vs Logical Operators: Confusing single
&or|(bitwise AND / OR operating on individual bit positions) with&&or||(logical boolean evaluation). - Modulus on Floating Point Numbers: The
%operator only works with integer data types (int,char,short,long). Using%withfloatordoublecauses compilation errors (usefmod()from<math.h>instead).
π‘οΈ Safety / Important Precedence Rules
- Parentheses
()have the highest precedence. Always use parentheses in complex formulas to make evaluation order explicit! - Multiplication/Division
* / %execute before Addition/Subtraction+ -. - Relational operators
< <= > >=execute before Equality== !=. - Logical AND
&&executes before Logical OR||.
π Real-World Usage
Bitwise operators (&, |, ^, ~, <<, >>) are used heavily in embedded systems to read hardware sensor registers, pack networking protocol headers, perform cryptography, and optimize graphics routines.
π§ͺ Try It Yourself
- Declare two
intvariables:a = 17andb = 5. - Print results for
a + b,a - b,a * b,a / b(integer division),(double)a / b(casted division), anda % b.
π― Mini Challenge
Write a program that takes a total number of seconds (e.g. 3800 seconds) and converts it into Hours, Minutes, and Remaining Seconds using / and % operators (1 hour = 3600s, 1 min = 60s).
π Related Topics
π§ Navigation
| β C Home | β Previous: Input/Output | Next: Conditionals β |