Theory
डिब्बे भरे, अब कुछ कीजिए
Marks program 3 subject के marks रखता है: 78, 65, 92। रखना पिछले lesson में हुआ। आज accountant का काम: total, percentage, और वह सवाल जो हर student असल में पूछता है, मैं पास हुआ या नहीं?
इनमें से हर एक किसी operator का काम है। Operators C के verbs (क्रियाएँ) हैं, और किसी भी भाषा की क्रियाओं की तरह, 2 जो लगभग एक जैसी दिखती हैं (= और ==) बिल्कुल अलग मतलब रखती हैं, और इन्हें उलझाने से असली payroll के runs crash हुए हैं।
At a glance
Operator परिवार
| परिवार | Operators | एक उदाहरण |
|---|---|---|
| Arithmetic | + - * / % | total = m1 + m2 + m3 |
| Relational | < > <= >= == != | marks >= 35 देता है 1 या 0 |
| Logical | && || ! | दोनों subjects में पास: a && b |
| Assignment | = += -= *= /= | marks += 5 (grace marks) |
| Ternary | ? : | result = (m >= 35) ? 'P' : 'F' |
| Bitwise | & | ^ << >> | अलग-अलग bits पर काम |
| sizeof() | sizeof(int) | डिब्बे का size bytes में: 4 |
Theory
Arithmetic के 2 मशहूर surprise
Surprise 1, integer division: int डिब्बों के साथ 235 / 3 है 78, 78.33 नहीं। 2 ints बँटकर एक int बनते हैं; decimals हटा दिए जाते हैं (फिर वही truncation)। Decimals चाहिए? एक तरफ़ को floating बनाइए: 235 / 3.0 देता है 78.33।
Surprise 2, % operator: % है modulus, भाग के बाद बचा remainder: 7 % 2 है 1, 10 % 5 है 0। इसका percentages से कोई नाता नहीं, चिह्न चाहे जो लगे। (सम/विषम test, cyclic rolls, और exam के आधे tricky सवाल % पर चलते हैं।)
Practical
Marks program गणना करता है
#include <stdio.h>
int main() {
int m1 = 78, m2 = 65, m3 = 92;
int total = m1 + m2 + m3;
float percentage = total / 3.0; /* 3.0, not 3 */
char result = (percentage >= 35) ? 'P' : 'F';
printf("Total: %d\n", total);
printf("Percentage: %f\n", percentage);
printf("Result: %c\n", result);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Ternary को वाक्य की तरह पढ़िए
Line result = (percentage >= 35) ? 'P' : 'F'; ternary operator है। पहले इसे अंग्रेज़ी वाक्य की तरह ज़ोर से पढ़िए, फिर बताइए percentage 78.33 के लिए result में क्या रहता है।
Show the answer
"क्या percentage कम से कम 35 है? हाँ तो 'P' लो; वरना 'F' लो।" 78.33 के लिए condition सच है, इसलिए result में 'P' रहता है।
Ternary एक line का if-else है: condition ? valueIfTrue : valueIfFalse। Exams किसी value के लिए इसका output पूछना पसंद करते हैं, और इसका नाम: C का इकलौता operator जो 3 operands लेता है।
Quiz
C में 7 % 2 की value क्या है, और अलग से 7 / 2 की (दोनों ints)?
- 7 % 2 है 1, और 7 / 2 है 3
- 7 % 2 है 3.5, और 7 / 2 है 3.5
- 7 % 2 है 3, और 7 / 2 है 1
- 7 % 2 है 1, और 7 / 2 है 3.5
Show the answer
7 % 2 है 1, और 7 / 2 है 3
Integer division 7 / 2 सिर्फ़ पूरा हिस्सा रखता है: 3। Modulus 7 % 2 सिर्फ़ remainder रखता है: 1। दोनों मिलकर किसी भी भाग को quotient और remainder में बाँट देते हैं। Option C इन्हें उलट देता है, standard trap, और 3.5 वाला कोई भी option भूल गया कि दोनों तरफ़ ints हैं।
Quiz
एक student perfect score जाँचने के इरादे से if (marks = 100) लिखता है। असल में क्या होता है?
- marks को 100 ASSIGN हो जाता है, और condition हमेशा सच रहती है
- C इरादे के मुताबिक़ marks की 100 से तुलना करता है
- Compiler इसे हमेशा error कहकर reject करता है
- Condition हमेशा झूठ रहती है
Show the answer
marks को 100 ASSIGN हो जाता है, और condition हमेशा सच रहती है
= assign करता है: marks 100 बन जाता है, उसकी असली value मिट जाती है, और expression की value (100, non-zero) हर बार TRUE गिनी जाती है। ज़्यादातर compilers इसे चुपचाप मान लेते हैं। तुलना के लिए == चाहिए था। मंत्र गाँठ बाँध लीजिए: = assign करता है, == पूछता है। इसी एक character से असल दुनिया में outages हुए हैं।
Watch out
3 mark-खाऊ
= बनाम == (assign बनाम compare, ऊपर)। Integer division: ints के साथ total / 3 चुपचाप decimals गिरा देता है; 3.0 लिखिए या float लीजिए। Relational results: C में तुलना 1 या 0 देती है, "true/false" शब्द नहीं, इसलिए printf("%d", 5 > 3) 1 print करता है, predict-the-output का चहेता सवाल। Bitwise operators मौजूद हैं (& | ^ << >>) और bits पर काम करते हैं; इस stage पर exams उनके NAMES चाहते हैं, महारत नहीं।
Theory
sizeof: नापने का फ़ीता
sizeof(int) जवाब देता है "यह डिब्बा कितने bytes का है?", आम तौर पर 4, और sizeof(marks) एक variable नापता है। यह function जैसा दिखता है पर है एक operator, exams ठीक यही पूछते हैं। यह आपको arrays और pointers के साथ फिर मिलेगा, जहाँ डिब्बे नापना रोज़ का काम बन जाता है। अगला lesson: हर program के ऊपर की toolbox lines, #include और #define, आख़िरकार समझाई हुईं।
Summary
Key takeaways
- Operator परिवार: arithmetic, relational, logical, assignment, ternary, bitwise, sizeof।
- Integer division काट देता है (7/2 है 3); % remainder देता है (7%2 है 1)।
- = assign करता है, == पूछता है; if (marks = 100) assign करता है और हमेशा सच है।
- Relational और logical results 1 (true) या 0 (false) होते हैं।
- Ternary: condition ? ifTrue : ifFalse, C का इकलौता 3-operand operator।
- sizeof एक operator है जो डिब्बे के bytes बताता है।
- याद रखने का hook: operators C के verbs हैं; = assign करता है, == पूछता है।