Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 Foundation  ·  Lesson 05

Structure of a C++ Program

"Structure" का मतलब क्या है?

हर C++ program — दो lines के "Hello" से लेकर school management system तक — एक ही तरह के हिस्सों से, एक ही क्रम में बनता है। इस structure को समझने से आप कोई भी program जल्दी पढ़ सकते हैं और अपना program बिना कोई हिस्सा भूले लिख सकते हैं। Class 11–12 और first-year exams में यह पसंदीदा short-answer question भी है।

सभी हिस्से एक नज़र में

#हिस्साउदाहरणज़रूरी?
1Documentation (comments)// Average marks निकालने का programनहीं, पर अच्छा है
2Preprocessor directives#include <iostream>हाँ, अगर library features इस्तेमाल हों
3Namespace declarationusing namespace std;नहीं
4Global declarationsconst int SUBJECTS = 5;, structs, classesनहीं
5Function declarations / definitionsdouble average(int total);नहीं
6main() functionint main() { ... }हाँ — ठीक एक

पूरा उदाहरण

C++
// 1. Documentation: student के average marks निकालने का program

// 2. Preprocessor directive
#include <iostream>

// 3. Namespace
using namespace std;

// 4. Global declaration
const int SUBJECTS = 5;

// 5. Function declaration (prototype)
double average(int total);

// 6. main() — execution यहीं से शुरू
int main() {
    int marks[SUBJECTS] = {78, 85, 69, 92, 74};
    int total = 0;

    for (int i = 0; i < SUBJECTS; i++) {
        total += marks[i];
    }

    cout << "Total   : " << total << endl;
    cout << "Average : " << average(total) << endl;
    return 0;
}

// 5. Function definition
double average(int total) {
    return static_cast<double>(total) / SUBJECTS;
}
Total : 398 Average : 79.6

हर हिस्से की व्याख्या

1. Documentation section

ऊपर लिखे comments program का उद्देश्य, लेखक और तारीख़ बताते हैं। Compiler इन्हें अनदेखा करता है। देखें C++ में comments।

2. Preprocessor directives

# से शुरू होने वाली lines को compilation से पहले preprocessor संभालता है। #include <iostream> से cin और cout की declarations जुड़ जाती हैं। दूसरे आम headers हैं <string>, <vector>, <cmath> और <iomanip>। देखें header files।

3. Namespace declaration

Standard library std namespace में रहती है। using namespace std; लिखने से std::cout की जगह सिर्फ cout लिख सकते हैं। छोटे programs में यह सुविधाजनक है; बड़े projects में std:: साफ़ लिखें। देखें namespaces।

4. Global declarations

किसी भी function के बाहर declare किए गए constants, structures, classes और (कभी-कभार) variables global होते हैं — नीचे के सभी functions उन्हें देख सकते हैं। Global variables कम से कम रखें; global constants ठीक हैं।

5. Functions

Function को call करने से पहले declare करना ज़रूरी है। या तो उसे main() के ऊपर define करें, या main() के ऊपर prototype (double average(int total);) लिखें और पूरी definition नीचे — जैसा उदाहरण में है।

6. main() function

int main() से program शुरू होता है। इसका body { } में होता है। return 0; operating system को बताता है कि program सफलतापूर्वक ख़त्म हुआ। पूरे program में ठीक एक main() होना चाहिए।

Program कैसे process होता है

  1. Preprocessing: comments हटते हैं और #include files जुड़ती हैं।
  2. Compilation: code में errors जाँची जाती हैं और वह object code बनता है।
  3. Linking: आपका object code library code (जैसे cout के पीछे का code) से जुड़कर executable बनता है।
  4. Execution: operating system program को main() से शुरू करता है।
Terminal
g++ average.cpp -o average
./average

Statements, blocks और semicolons

  • हर statement semicolon ; पर ख़त्म होता है।
  • { } के अंदर statements का समूह block कहलाता है; function body, loops और if के body blocks हैं।
  • C++ case-sensitive है: Main, MAIN और main अलग नाम हैं।
  • Spaces और line breaks की कोई पाबंदी नहीं, पर अच्छा indentation पढ़ने वालों के लिए ज़रूरी है।

Class वाला structure

Object-oriented programs में class आमतौर पर global section में declare होती है और main() से इस्तेमाल होती है:

C++
#include <iostream>
#include <string>

class Student {
public:
    std::string name;
    int marks;
    void show() const {
        std::cout << name << " scored " << marks << "\n";
    }
};

int main() {
    Student s{"Priya", 91};
    s.show();
    return 0;
}
Priya scored 91

आम गलतियाँ

  • #include <iostream> भूलना — "cout was not declared in this scope"।
  • Prototype के बिना function को declare होने से पहले call करना।
  • void main() लिखना — standard C++ में int main() ज़रूरी है।
  • Class या struct definition के बाद semicolon भूलना: class A { };
  • साथ compile होने वाली files में दो main() functions।
🏋️ अभ्यास

सभी छह हिस्सों वाला program लिखें: header comment, #include <iostream>, global constant TAX_RATE, function prototype double withTax(double price), main() जो price पढ़कर tax सहित price print करे, और main() के बाद function की definition।

सारांश

  • C++ program में documentation, preprocessor directives, namespace, global declarations, functions और main() होते हैं।
  • सिर्फ main() अनिवार्य है, और वह ठीक एक होना चाहिए।
  • #include lines compilation से पहले process होती हैं।
  • Function call करने से पहले उसे declare (या prototype) करें।
  • return 0; सफल समाप्ति बताता है।

अगला: C++ में comments · पिछला: पहला C++ program।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ program की basic structure क्या होती है?
एक सामान्य C++ program में होते हैं: (1) #include <iostream> जैसे preprocessor directives, (2) optional using declaration या namespace, (3) constants, structures, classes जैसी global declarations, (4) user-defined functions, और (5) main() function जहाँ से execution शुरू होता है, जो आमतौर पर return 0; पर ख़त्म होता है।
C++ में main() ज़रूरी क्यों है?
main() entry point है: जब operating system आपका program चलाता है, execution main() के पहले statement से शुरू होता है। पूरे program में ठीक एक main() होना चाहिए, वरना linker "undefined reference to main" error देता है।
C++ में return 0 का क्या मतलब है?
return 0; main() को ख़त्म करता है और operating system को 0 वापस भेजता है, जिसका अर्थ है "program सफलतापूर्वक पूरा हुआ"। Non-zero value error बताती है। main() में इसे न लिखें तो C++ अपने-आप 0 लौटाता है।
#include और #include "myfile.h" में क्या अंतर है?
Angle brackets compiler को standard system include folders में ढूँढने को कहते हैं — library headers के लिए। Double quotes पहले आपके project folder में ढूँढते हैं — अपनी header files के लिए।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicComments in C++ आपकी प्रगति: 5 / 81 (6%) · Lesson 6 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें