C में Operators
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 करे।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C में operators क्या हैं?
+ या तुलना के लिए ==। C इन्हें arithmetic, relational, logical, assignment, bitwise और कुछ अन्य में बाँटता है, हर एक अलग तरह के काम के लिए।C में operators के प्रकार क्या हैं?
+ - * / %), relational (< > == !=), logical (&& || !), assignment (= += -=), bitwise (& | ^), और विशेष जैसे increment ++, decrement -- और ternary ?:।C में = और == में क्या अंतर है?
= assignment operator है, जो variable में value रखता है। दोहरा == equality operator है, जो दो values की तुलना करके true या false देता है। इन्हें मिलाना बहुत आम bug है, खासकर if conditions के अंदर।C में modulus operator क्या है?
% integer भाग का शेषफल देता है। जैसे 10 % 3 1 है, क्योंकि 10 को 3 से भाग देने पर शेष 1 बचता है। यह अक्सर even/odd जाँचने या values को किसी range में लपेटने को इस्तेमाल होता है।