Theory
યાદશક્તિ વગરનો program
તમારો marks વાળો program રુઆબથી Marks: 78 print કરે છે. પછી બીજો test આવે છે અને Riya 92 લઈ આવે છે.
અત્યાર સુધી તમારી પાસે એક જ રસ્તો છે: source ખોલો, text બદલો, ફરી compile કરો, ફરી ચલાવો. એ પણ એક student માટે. 60 students, દર અઠવાડિયે tests... તમે full-time typist બની જશો.
જે idea ખૂટે છે એ છે ડબ્બો: 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; (તાળાવાળો ડબ્બો) અને #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 એ int print કરતો placeholder છે, થોડા વખતમાં 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 નું માનીતું છે.
Watch out
શરૂઆતની 2 ઠોકરો
Declare કર્યા પહેલાં વાપરવું: compiler "undeclared identifier" કહીને અટકી જાય છે, C ક્યારેય અંદાજે ડબ્બો બનાવી દેતું નથી. `=` ને "equals" વાંચવું: C માં = નો અર્થ છે assign, જમણી બાજુનું ડાબા ડબ્બામાં નાખો. marks = 92 એ ક્રિયા છે, દાવો નહીં. ("આ 2 સરખાં છે?" પૂછવા માટે == છે, અને એ બેની અદલાબદલી એ C નો THE classic bug છે, જેનો formal શિકાર operators ના lesson માં થશે.)
Theory
હવે પછીનો રસ્તો
હવે ડબ્બા બની ગયા છે, એટલે 3 સવાલ આવે છે, દરેકનો પોતાનો lesson: ડબ્બાનું નામ શું રાખી શકાય (identifiers, keywords, હવે પછી), ડબ્બાના કયા 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).
- યાદ રાખવાની યુક્તિ: label વાળા ડબ્બા (variables), કોતરેલું nameplate (constants).