Operators in C
C में Operators
Types of Operators in C
An operator is a symbol that tells the compiler to perform a specific operation on one or more operands. C has a rich set of operators that can be categorized as:
- Arithmetic Operators:
+-*/% - Relational (Comparison) Operators:
==!=><>=<= - Logical Operators:
&&||! - Assignment Operators:
=+=-=*=/=%= - Increment/Decrement:
++-- - Bitwise Operators:
&|^~<<>> - Ternary Operator:
? : - sizeof Operator:
sizeof()
1. Arithmetic Operators
Used to perform basic mathematical operations.
| Operator | Name | Example (a=10, b=3) | Result |
|---|---|---|---|
+ | Addition | a + b | 13 |
- | Subtraction | a - b | 7 |
* | Multiplication | a * b | 30 |
/ | Division | a / b | 3 (integer division!) |
% | Modulus (Remainder) | a % b | 1 |
#include <stdio.h> int main() { int a = 10, b = 3; printf("a + b = %d\n", a + b); printf("a - b = %d\n", a - b); printf("a * b = %d\n", a * b); printf("a / b = %d\n", a / b); // Integer division printf("a %% b = %d\n", a % b); // %% prints single % return 0; }
10 / 3 gives 3, not 3.33! When both operands are integers, C performs integer division (truncates the decimal). Use 10.0 / 3 or cast: (float)10 / 3 to get 3.333333.
2. Relational Operators
Used to compare two values. Always return 1 (true) or 0 (false).
| Operator | Meaning | a=5, b=3 | Result |
|---|---|---|---|
== | 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 >= 5 | 1 (true) |
<= | Less than or equal | a <= 3 | 0 (false) |
= is assignment (sets a value). == is comparison (checks equality). Writing if (a = 5) instead of if (a == 5) is a classic bug!
3. Logical Operators
Used to combine multiple conditions. Return 1 (true) or 0 (false).
| Operator | Name | Example | Result |
|---|---|---|---|
&& | AND | 1 && 0 | 0 — both must be true |
|| | OR | 1 || 0 | 1 — at least one true |
! | NOT | !1 | 0 — reverses the value |
#include <stdio.h> int main() { int marks = 85, attendance = 78; // Eligible if marks >= 80 AND attendance >= 75 if (marks >= 80 && attendance >= 75) { printf("Eligible for scholarship\n"); } // Fail if marks < 40 OR attendance < 75 if (marks < 40 || attendance < 75) { printf("Not eligible\n"); } return 0; }
4. Assignment Operators
| Operator | Example | Equivalent to |
|---|---|---|
= | a = 5 | a = 5 |
+= | a += 3 | a = a + 3 |
-= | a -= 2 | a = a - 2 |
*= | a *= 4 | a = a * 4 |
/= | a /= 2 | a = a / 2 |
%= | a %= 3 | a = a % 3 |
5. Increment & Decrement Operators
++ increases a value by 1, -- decreases by 1. These can be pre (before variable) or post (after variable).
#include <stdio.h> int main() { int a = 5; printf("a++ = %d\n", a++); // Prints 5, THEN increments to 6 printf("a = %d\n", a); // Now a is 6 printf("++a = %d\n", ++a); // Increments to 7, THEN prints 7 printf("a-- = %d\n", a--); // Prints 7, THEN decrements to 6 printf("--a = %d\n", --a); // Decrements to 5, THEN prints 5 return 0; }
6. Ternary Operator ( ? : )
A short form of if-else. Syntax: condition ? value_if_true : value_if_false
#include <stdio.h> int main() { int marks = 75; char *result = (marks >= 40) ? "Pass" : "Fail"; printf("Result: %s\n", result); int a = 10, b = 20; int max = (a > b) ? a : b; printf("Max: %d\n", max); return 0; }
Operator Precedence (Priority)
When an expression has multiple operators, precedence determines which gets evaluated first. Higher precedence = evaluated first.
| Priority | Operators | Associativity |
|---|---|---|
| 1 (Highest) | () [] . -> | Left to Right |
| 2 | ++ -- ! ~ (cast) sizeof | Right to Left |
| 3 | * / % | Left to Right |
| 4 | + - | Left to Right |
| 5 | << >> | Left to Right |
| 6 | < <= > >= | Left to Right |
| 7 | == != | Left to Right |
| 8–12 | Bitwise &, ^, |, &&, || | Left to Right |
| 13 | ?: | Right to Left |
| 14 (Lowest) | = += -= *= /= | Right to Left |
Example: 2 + 3 * 4 → 3 * 4 = 12 first, then 2 + 12 = 14 (not 20!).
Use parentheses () to override precedence: (2 + 3) * 4 = 20.
Summary
- Arithmetic:
+-*/%— basic math operations - Relational:
==!=><— compare values, return 0 or 1 - Logical:
&&||!— combine conditions - Assignment:
=+=-=— assign values - Increment/Decrement:
++--— increase/decrease by 1 - Ternary:
? :— one-line if-else - Don't confuse
=(assignment) with==(comparison)
Write a program that takes two numbers and prints: their sum, difference, product, quotient, remainder, which is greater (using ternary), and whether their sum is even or odd (using %).
Operators के प्रकार
एक operator एक symbol है जो compiler को एक या अधिक operands पर specific operation perform करने के लिए कहता है। C में operators के ये प्रकार हैं:
- Arithmetic Operators:
+-*/% - Relational Operators:
==!=><>=<= - Logical Operators:
&&||! - Assignment Operators:
=+=-=etc. - Increment/Decrement:
++-- - Ternary Operator:
? :
1. Arithmetic Operators
Basic mathematical operations के लिए use होते हैं।
| Operator | नाम | Example (a=10, b=3) | Result |
|---|---|---|---|
+ | जोड़ | a + b | 13 |
- | घटाव | a - b | 7 |
* | गुणा | a * b | 30 |
/ | भाग | a / b | 3 (integer division!) |
% | शेषफल (Modulus) | a % b | 1 |
10 / 3 का result 3 होगा, 3.33 नहीं! जब दोनों operands integers हों, C integer division करता है। Float result के लिए: (float)10 / 3 use करें।
2. Relational Operators
दो values compare करने के लिए use होते हैं। हमेशा 1 (true) या 0 (false) return करते हैं।
| Operator | अर्थ | a=5, b=3 | Result |
|---|---|---|---|
== | बराबर है | a == b | 0 (false) |
!= | बराबर नहीं है | a != b | 1 (true) |
> | बड़ा है | a > b | 1 (true) |
< | छोटा है | a < b | 0 (false) |
= assignment है (value set करता है)। == comparison है (equality check करता है)। if (a = 5) की जगह if (a == 5) लिखना एक classic bug है!
3. Logical Operators
Multiple conditions combine करने के लिए use होते हैं।
| Operator | नाम | Example | Result |
|---|---|---|---|
&& | AND (और) | 1 && 0 | 0 — दोनों true होने चाहिए |
|| | OR (या) | 1 || 0 | 1 — कोई एक true हो |
! | NOT (नहीं) | !1 | 0 — value को reverse करता है |
4. Assignment Operators
| Operator | Example | इसके बराबर |
|---|---|---|
+= | a += 3 | a = a + 3 |
-= | a -= 2 | a = a - 2 |
*= | a *= 4 | a = a * 4 |
/= | a /= 2 | a = a / 2 |
5. Increment और Decrement Operators
int a = 5; a++; // Post-increment: पहले use करो, फिर बढ़ाओ (5 print होगा, फिर 6) ++a; // Pre-increment: पहले बढ़ाओ, फिर use करो (7 print होगा) a--; // Post-decrement --a; // Pre-decrement
6. Ternary Operator ( ? : )
if-else का short form। Syntax: condition ? true_value : false_value
int marks = 75; char *result = (marks >= 40) ? "Pass" : "Fail"; printf("Result: %s\n", result); // Output: Pass int a = 10, b = 20; int max = (a > b) ? a : b; printf("Max: %d\n", max); // Output: 20
Operator Precedence (Priority)
जब expression में multiple operators हों, precedence तय करती है कि पहले कौन evaluate होगा। याद रखें: * / की priority + - से ज़्यादा है।
Example: 2 + 3 * 4 → 3 * 4 = 12 पहले, फिर 2 + 12 = 14
Parentheses use करें: (2 + 3) * 4 = 20
सारांश
- Arithmetic:
+-*/%— basic गणित - Relational:
==!=><— values compare करते हैं - Logical:
&&||!— conditions combine करते हैं - Assignment:
+=-=*=— shorthand assignment - Ternary:
? :— एक-line if-else =और==को confuse न करें!
दो numbers input करके program लिखें जो: sum, difference, product, quotient, remainder, कौन बड़ा है (ternary से), और sum even है या odd (% से) print करे।