Theory
નામકરણ વિધિ
તમારા marks program માં હવે ડબ્બા છે, અને ડબ્બાને નામ જોઈએ. તો તમે થોડાં અજમાવો છો:
total_marks... મંજૂર. 2nd_test... નામંજૂર. my-marks... નામંજૂર. int... સખ્તાઈથી નામંજૂર.
Compiler નો mood ખરાબ નથી; એ નામોનો કાયદો લાગુ કરી રહ્યું છે જેમાં બરાબર 4 નિયમ છે, વત્તા શબ્દોની એક નાની યાદી જે C એ પોતાના માટે રાખી લીધી છે. આ કાયદો એક વાર શીખી લો, પછી "આમાંથી valid identifier કયો?" paper નો સૌથી સહેલો one-marker બની જાય છે.
Theory
કડક school માં બાળકનું નામ
Variable નું નામ પાડવું એ બહુ કડક school માં admission માટે બાળકનું નામ પાડવા જેવું છે. Form માં અમુક જ characters ચાલે (hyphen નહીં, space નહીં), નામ આંકડાથી શરૂ ન થઈ શકે, અને staff માટે reserved હોય એવું નામ ન ચાલે, તમે બાળકનું નામ "Principal" ન રાખી શકો. તમારાં ઘડેલાં નામ એટલે identifiers; staff નાં reserved નામ એટલે keywords.
Theory
ત્રણેય શબ્દોની ચોખ્ખી વ્યાખ્યા
- Identifier: variable, function કે array માટે તમે ઘડેલું નામ:
marks,total_marks,computeGrade. - Keyword: C એ fixed અર્થ સાથે reserve કરેલો શબ્દ:
int,if,else,while,for,return,char,float,void... (classic C માં 32, બધા lowercase). નામ તરીકે ક્યારેય નહીં. - Literal: code માં સીધી લખેલી fixed value:
78(integer),3.14(floating),'A'(character, single quotes),"Riya"(string, double quotes).
એક જ line માં ત્રણેય: int marks = 78; keyword, identifier, literal.
At a glance
Identifier ના 4 નિયમ
| નિયમ | Legal | Illegal |
|---|---|---|
| ફક્ત letters, digits, underscore | total_marks | my-marks (hyphen) |
| આંકડાથી શરૂઆત નહીં | test2 | 2nd_test |
| Keyword ન હોઈ શકે | sum_int | int, while |
| Case-sensitive: case બદલાય એટલે નામ બદલાય | marks અને Marks (2 ડબ્બા) | marks ને બદલે Marks લખવું (ચૂપચાપ bug) |
Quiz
આમાંથી VALID C identifier કયો છે?
- _total2
- 2total
- total-2
- return
Show the answer
_total2
Identifiers માં underscore ને letter જેટલો જ દરજ્જો છે, એટલે એનાથી શરૂઆત legal છે: _total2 ચારેય નિયમ પસાર કરે છે. 2total આંકડાથી શરૂ થાય છે, total-2 માં hyphen ઘૂસ્યો છે (C એને total minus 2 વાંચે છે), અને return keyword છે. 4 options વાળી આ pattern દરેક C paper માં કાયમની છે.
Think first
Case ની જાળ
તમારો program int marks = 78; declare કરે છે અને પછી Marks print કરવા જાય છે. પહેલા વિચારો: એ જ ડબ્બો કે જુદો? Compiler શું કહેશે?
Show the answer
જુદો. C case-sensitive છે, એટલે marks અને Marks 2 સાવ અલગ identifiers છે, અને Marks ક્યારેય declare થયું જ નથી, એટલે compiler "undeclared identifier" કહીને અટકે છે. એટલે જ Int અને INT keywords નથી, ફક્ત lowercase int જ છે. એક naming style પસંદ કરો (lowercase, underscores) અને cases ક્યારેય બેદરકારીથી ન ભેળવો.
Watch out
Marks ક્યાં કપાય છે
String માટે 'Riya' કે character માટે "A" લખવું: single quotes = એક character, double quotes = string, અને exams આ ફરક સીધો પૂછે છે. int = 5; અજમાવવું કે variable નું નામ for રાખવું: keywords ને અડાય નહીં. અને table નો ચાલાક નિયમ યાદ રાખો: identifier underscore થી શરૂ થઈ શકે છે, students ઘણી વાર એને illegal ઠરાવીને mark ગુમાવે છે.
Theory
Professional નામકરણ, મફતમાં
કાયદો કહે છે x legal છે; ડહાપણ કહે છે total_marks વધુ સારું છે. 6 મહિના પછી પોતાનો જ code વાંચનાર અજાણ્યો માણસ તમે જ હશો. ડબ્બાનું નામ એમાં જે રહે છે એના પરથી રાખો (pass_marks, pm નહીં), પછી marks program વાર્તાની જેમ વંચાશે. હવે પછીનો lesson નક્કી કરશે કે દરેક ડબ્બાનો TYPE શું છે: આખો number, decimal, કે એક character.
Summary
Key takeaways
- Identifiers: તમારાં ઘડેલાં નામ; ફક્ત letters, digits, underscore; પહેલો આંકડો નહીં; keywords નહીં.
- C case-sensitive છે: marks, Marks અને MARKS 3 જુદાં નામ છે.
- Keywords: 32 reserved lowercase શબ્દો (int, if, while, return...), નામ તરીકે ક્યારેય નહીં.
- Literals: code માંની કાચી values: 78, 3.14, 'A' (single quotes), "Riya" (double quotes).
- int marks = 78; એ એક જ line માં keyword + identifier + literal છે.
- યાદ રાખવાની યુક્તિ: તમારાં ઘડેલાં નામ, C નો reserved staff, અને કાચી values.