Introduction of C program body structure

हर C program का एक ही skeleton है: ऊपर #include lines, ठीक एक main() जहाँ execution शुरू होता है, { } braces के अंदर semicolon पर खत्म होते statements, और अंत में return 0।

10 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


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

शादी का card

Skeleton को शादी के card जैसा समझिए। सबसे ऊपर venue की सुविधाओं के नाम (#include: program कौन सी facilities use करेगा)। Card की body में एक main event वाला हिस्सा (main()), साफ opening और closing के साथ ({ })। अंदर की हर बात एक line, हर line पर पूर्ण विराम (;)। और card का अंत एक formal closing line से (return 0)। शादियाँ अलग-अलग, card का 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 में सबसे AAKHIR में लिखा है। Execution कहाँ से शुरू होगा?

  1. main() से, वह file में कहीं भी हो
  2. File में सबसे पहले लिखे function से
  3. #include वाली line से
  4. 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 अक्सर AGLI 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 पर shikayat कराता है।
  • C case-sensitive है: main, Main नहीं।
  • #include <stdio.h> compile से पहले printf को पहचान देता है।
  • Memory hook: शादी के card का format, header, एक main event, पूर्ण विराम।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to C language

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Introduction of C program body structure · Computer Programming and Programming Methodology (CPPM) · Gri-Learn