Theory
याददाश्त की कमी वाला program
आपका marks वाला program शान से Marks: 78 print करता है। फिर अगला test होता है और Riya 92 ले आती है।
अब तक आपके पास एक ही रास्ता है: source खोलिए, text बदलिए, फिर से compile कीजिए, फिर से चलाइए। वह भी एक student के लिए। 60 students, हर हफ्ते tests... आप full-time typist बन जाएँगे।
जो idea missing है वह है एक डिब्बा: memory की एक जगह जहाँ program कोई value रखता है, देखता है, और चलते-चलते बदल भी देता है। C उस डिब्बे को variable कहता है, और यह पूरी programming का सबसे ज़्यादा इस्तेमाल होने वाला idea है।
Theory
Label वाले डिब्बे और खुदा हुआ nameplate
रसोई की shelf पर रखे steel के डिब्बे सोचिए, हर एक पर label: चीनी, चाय, इलायची। Label वही रहता है; अंदर का सामान महीने भर बदलता रहता है। यही variable है: नाम वाला डिब्बा, बदलने लायक सामान। और shelf के बगल में टँगा है एक खुदा हुआ nameplate: दुकान का नाम, लगाते समय तय हुआ, फिर कभी नहीं बदला। यही constant है। C के programs label वाले डिब्बों की shelf हैं, साथ में कुछ खुदे हुए nameplates।
Theory
पहले वर्णमाला: C का character set
शब्दों से पहले वर्णमाला। C का source code सिर्फ उसके character set से बन सकता है:
- Letters: A से Z, a से z
- Digits: 0 से 9
- Special symbols:
+ - * / % = < > ( ) { } [ ] ; : , . # & ! ?वगैरह - Whitespace: space, tab, newline
C program में आप जो भी कभी type करेंगे, variable के नाम, numbers, operators, सब इसी set से बनते हैं। (Display के लिए quotes के अंदर हिंदी या गुजराती अक्षर रह सकते हैं, पर code खुद इसी वर्णमाला में बोलता है।)
Practical
Marks program को मिले पहले डिब्बे
#include <stdio.h>
int main() {
int marks = 78; /* declare + initialize */
printf("Marks: %d\n", marks);
marks = 92; /* assignment: new contents */
printf("After retest: %d\n", marks);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Theory
Variable और constant की ठीक-ठीक परिभाषा
Variable memory की एक नाम वाली जगह है जिसकी value program चलते हुए बदल सकती है। उसकी ज़िंदगी के 3 पल:
- Declaration:
int marks;डिब्बा reserve करता है और तय करता है उसमें क्या रहेगा (एक int)। - Initialization:
int marks = 78;declare करता है और साथ में पहली value भी डाल देता है। - Assignment:
marks = 92;अंदर का सामान बदल देता है (पुराना 78 गया)।
Constant कभी नहीं बदलता: खुद literals (78, 3.14, 'A'), या नाम वाले constants: const int PASS = 35; (locked डिब्बा) और #define (2 lessons आगे मिलेगा)।
Think first
Output की भविष्यवाणी कीजिए
ऊपर का code block एक बार फिर, line by line पढ़िए। यह ठीक-ठीक कौन सी 2 lines print करता है?
Show the answer
Marks: 78
After retest: 92
दूसरा printf 92 print करता है, 78 नहीं: दूसरी बार देखने से पहले assignment ने डिब्बे का सामान बदल दिया था। Variable हमेशा अपना मौजूदा सामान दिखाता है, और पुरानी value कहीं store नहीं रहती। (%d एक placeholder है जो int print करता है, जल्द ही formally मिलेगा।)
Quiz
C का compiler कौन सी line REJECT कर देगा?
- marks = 78; जो marks के किसी भी declaration से पहले लिखी गई हो
- int marks = 78;
- marks = 92; जो int marks = 78; के बाद लिखी गई हो
- int marks; और बाद में marks = 78;
Show the answer
marks = 78; जो marks के किसी भी declaration से पहले लिखी गई हो
C की माँग है declaration before use: जो डिब्बा declare ही नहीं हुआ उसमें assign करना compile error है ("undeclared identifier")। बाकी तीनों legal life cycle हैं: declare+initialize, फिर से assign, या पहले declare और बाद में भरना। Declaration-before-use exam की spot-the-error lines का favourite है।
Watch out
शुरुआती 2 ठोकरें
Declare करने से पहले use करना: compiler "undeclared identifier" कहकर रुक जाता है, C कभी अंदाज़े से डिब्बा नहीं बना देता। `=` को "equals" पढ़ना: C में = का मतलब है assign, दाईं तरफ़ वाली चीज़ बाएँ डिब्बे में डालो। marks = 92 एक action है, कोई दावा नहीं। ("क्या ये बराबर हैं?" पूछने के लिए == है, और इन दोनों की अदला-बदली C का THE classic bug है, जिसका formal शिकार operators वाले lesson में होगा।)
Theory
आगे का रास्ता
अब जब डिब्बे बन गए हैं, 3 सवाल आते हैं, हर एक का अपना lesson: डिब्बे का नाम क्या रखा जा सकता है (identifiers, keywords, अगला lesson), डिब्बों के कौन से types होते हैं (int बनाम float बनाम char), और उन्हें जोड़ने वाले operations कौन से हैं (operators)। इस unit के अंत तक marks program input लेगा, percentage निकालेगा और report print करेगा, सब इन्हीं डिब्बों से।
Summary
Key takeaways
- C का character set: letters, digits, special symbols, whitespace, यही code की वर्णमाला है।
- Variable memory का नाम वाला डिब्बा है जिसकी value चलते program में बदल सकती है।
- Declaration डिब्बा reserve करता है; initialization पहली बार भरता है; assignment सामान बदलता है।
- Declaration before use, हमेशा: बिना declare किए नाम compile errors हैं।
- Constants कभी नहीं बदलते: literals (78, 'A') और नाम वाले constants (const, #define)।
- याद रखने का hook: label वाले डिब्बे (variables), खुदा हुआ nameplate (constants)।