Theory
Anatomy ની class શરૂ
તમે marks program ને compile અને run થતો જોઈ લીધો. આજે એની ચીર-ફાડ.
જે પણ C program તમે ક્યારેય વાંચશો, 5 lines નો હોય કે 5 લાખનો, એનું skeleton એક જ છે: ઉપર કંઈક, એક ખાસ function જ્યાં execution શરૂ થાય, વિસ્તાર બતાવતા braces, અને ચારે બાજુ semicolons.
Skeleton એક વાર શીખી લો, પછી કોઈ C code પારકો નહીં લાગે. એક semicolon ભૂલો, તો compiler નું ભાષણ પણ આજના જ lesson માં છે.
Practical
marks.c ની ચીર-ફાડ
/* The marks program: day two */
#include <stdio.h>
int main() {
printf("Riya\n");
printf("Marks: 78\n");
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Theory
Line-by-line, ઉપરથી નીચે
/* ... */એ comment છે: માણસો માટે notes, compiler માટે અદ્રશ્ય (//એક line નું comment).#include <stdio.h>એ preprocessor directive છે: compile પહેલાં standard input/output ની declarations લઈ આવે છે, એનાથી જprintfઓળખાય છે.int main()એ entry point છે: execution હંમેશા main ના પહેલા statement થી શરૂ થાય છે, main file માં ગમે ત્યાં હોય. દરેક program માં બરાબર એક main.{ }braces main નો block, એનો વિસ્તાર બતાવે છે.- દરેક statement semicolon પર પૂરું થાય છે: C નું પૂર્ણવિરામ.
return 0;operating system ને કહે છે: કામ બરાબર પૂરું થયું.
Theory
લગ્નની કંકોત્રી
Skeleton ને લગ્નની કંકોત્રી જેવું સમજો. સૌથી ઉપર venue ની સુવિધાઓનાં નામ (#include: program કઈ facilities વાપરશે). કંકોત્રીની body માં એક main event વાળો ભાગ (main()), ચોખ્ખા opening અને closing સાથે ({ }). અંદરની દરેક વાત એક line, દરેક line પર પૂર્ણવિરામ (;). અને કંકોત્રીનો અંત એક formal closing line થી (return 0). લગ્નો જુદાં જુદાં, કંકોત્રીનું format હંમેશા એ જ.
Think first
Output predict કરો
ઉપરનો program ફરી એક વાર વાંચો. એ EXACTLY શું print કરશે, અને કેટલી lines માં? (Keyboard-અને-codes વાળા દિવસોનું \n યાદ રાખો.)
Show the answer
2 lines:
Riya
Marks: 78
દરેક \n newline character છે: એ ચાલુ line પૂરી કરે છે. પહેલું \n ન હોત તો બંને text એક જ line પર ભેગા થઈને RiyaMarks: 78 બની જાત. Predict-the-output ના સવાલ આવી જ ઝીણવટો પર જીવે છે, અને \n એમનું favourite છે.
Quiz
એક C file માં 3 functions છે, અને main() file માં સૌથી CHHELLE લખેલું છે. Execution ક્યાંથી શરૂ થશે?
- main() થી, એ file માં ગમે ત્યાં હોય
- File માં સૌથી પહેલા લખેલા function થી
- #include વાળી line થી
- main છેલ્લે હોય તો program compile જ નહીં થાય
Show the answer
main() થી, એ file માં ગમે ત્યાં હોય
main() contractual entry point છે: OS control એને જ સોંપે છે, position ગમે તે હોય. File નો ક્રમ માણસો માટે છે; execution નો ક્રમ main થી શરૂ થાય છે. અને #include compile પહેલાં process થાય છે, એ "ચાલતું" નથી. main છેલ્લે લખેલા programs બિલકુલ બરાબર compile અને run થાય છે.
Watch out
3 classic beginner errors
Semicolon છૂટવું: compiler ઘણી વાર AAGALNI line પર ફરિયાદ કરે છે, તો error line 6 પર દેખાય તો પહેલા line 5 જુઓ. Case ની ભૂલ: C case-sensitive છે, Main, Printf અને PRINTF compiler માટે અજાણ્યા છે. Brace નો હિસાબ: દરેક { ને એનું } જોઈએ; એક brace ની ખોટ confused errors નું આખું page પેદા કરી શકે છે. ત્રણેય compile-time errors છે: pipeline compiler પર અટકે છે, કોઈ .exe બનતું નથી.
Formula
Exam recipe: "explain the structure of a C program"
Skeleton લખો, પછી દરેક ભાગ પર એક line: documentation section (comments), preprocessor directives (#include), single entry point તરીકે main() function, { } ની અંદર declarations અને statements જેમાં દરેક semicolon પર પૂરું, અને છેલ્લે return 0. Example માં marks program ઉમેરો. Skeleton + 6 labelled lines + example = દર વખતે full marks.
Theory
Skeleton કોની રાહ જુએ છે
ધ્યાન આપો: program fixed 78 print કરે છે: marks બદલવા edit અને recompile કરવું પડશે. સાચા programs values ને variables માં રાખે છે, નામવાળા ડબ્બા, અને બરાબર એ જ આગળનો lesson છે (character set, variables, constants). Skeleton એ જ રહેશે; muscles એક એક topic કરીને આવશે.
Summary
Key takeaways
- દરેક C program: comments, #include directives, એક main(), { } માં statements, return 0.
- Execution હંમેશા main() થી શરૂ, એ file માં ગમે ત્યાં હોય.
- દરેક statement semicolon પર પૂરું; છૂટેલું semicolon આગળની line પર ફરિયાદ કરાવે છે.
- C case-sensitive છે: main, Main નહીં.
- #include <stdio.h> compile પહેલાં printf ને ઓળખ આપે છે.
- Memory hook: કંકોત્રીનું format, header, એક main event, પૂર્ણવિરામ.