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 भी है।
सभी हिस्से एक नज़र में
| # | हिस्सा | उदाहरण | ज़रूरी? |
|---|---|---|---|
| 1 | Documentation (comments) | // Average marks निकालने का program | नहीं, पर अच्छा है |
| 2 | Preprocessor directives | #include <iostream> | हाँ, अगर library features इस्तेमाल हों |
| 3 | Namespace declaration | using namespace std; | नहीं |
| 4 | Global declarations | const int SUBJECTS = 5;, structs, classes | नहीं |
| 5 | Function declarations / definitions | double average(int total); | नहीं |
| 6 | main() function | int main() { ... } | हाँ — ठीक एक |
पूरा उदाहरण
// 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;
}हर हिस्से की व्याख्या
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 होता है
- Preprocessing: comments हटते हैं और
#includefiles जुड़ती हैं। - Compilation: code में errors जाँची जाती हैं और वह object code बनता है।
- Linking: आपका object code library code (जैसे
coutके पीछे का code) से जुड़कर executable बनता है। - Execution: operating system program को
main()से शुरू करता है।
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() से इस्तेमाल होती है:
#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;
}आम गलतियाँ
#include <iostream>भूलना — "coutwas 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()अनिवार्य है, और वह ठीक एक होना चाहिए। #includelines compilation से पहले process होती हैं।- Function call करने से पहले उसे declare (या prototype) करें।
return 0;सफल समाप्ति बताता है।
अगला: C++ में comments · पिछला: पहला C++ program।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ program की basic structure क्या होती है?
#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 लौटाता है।