Structure of a C++ Program

Understanding headers, main function and statements in a C++ program.

Structure of a C++ Program

C++ program कई अलग-अलग components (घटकों) से मिलकर बना होता है। Program की structure (संरचना) को समझना programming सीखने का एक महत्वपूर्ण आधार है। एक सामान्य C++ program में comments, header files, namespace declaration, main() function, variable declarations, executable statements और return statement जैसे components हो सकते हैं।

Basic Structure: Comments → Header Files → Namespace → main() Function → Declarations → Statements → return

Basic Structure of a C++ Program

एक simple C++ program का सामान्य structure निम्न प्रकार हो सकता है:

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int b = 20;

    int sum = a + b;

    cout << "Sum = " << sum;

    return 0;
}
Output:
Sum = 30

ऊपर दिए गए program में प्रत्येक component का अपना specific purpose है।

Main Components of a C++ Program

Component Purpose
Comments Program के बारे में explanation या information देना
Header File Required library declarations और facilities उपलब्ध कराना
Namespace Names को organize करना और name conflicts से बचने में सहायता करना
main() Function Program का मुख्य entry point
Declarations Variables आदि को declare करना
Statements Program की actual operations perform करना
return Statement Function से value/status वापस करना

1. Comments

Comments program में लिखे गए ऐसे explanatory text होते हैं जिन्हें compiler program के executable instructions के रूप में treat नहीं करता। Comments का उपयोग code को समझाने और उसकी readability improve करने के लिए किया जाता है।

C++ में मुख्य रूप से दो प्रकार के comments होते हैं:

  • Single-line Comment: // से शुरू होता है।
  • Multi-line Comment: /* और */ के बीच लिखा जाता है।

Example:

#include <iostream>
using namespace std;

int main()
{
    // This is a single-line comment

    /*
       This is a
       multi-line comment
    */

    cout << "C++ Programming";

    return 0;
}
Output:
C++ Programming

Comments program के output को directly affect नहीं करते, लेकिन code को समझने और maintain करने में helpful होते हैं।

2. Header Files

Header file में ऐसी declarations और facilities होती हैं जिनका उपयोग program में किया जा सकता है। C++ Standard Library की सुविधाओं का उपयोग करने के लिए appropriate header files को include किया जाता है।

#include <iostream> एक common example है। इसका उपयोग standard input और output facilities, जैसे cin और cout, के लिए किया जाता है।

#include <iostream>

int main()
{
    std::cout << "Hello C++";
    return 0;
}
Output:
Hello C++
याद रखें: #include <iostream> compiler को iostream header की declarations उपलब्ध कराने का निर्देश देता है।

3. Preprocessor Directive

#include से शुरू होने वाला statement एक preprocessor directive है। यह compilation process के preprocessor stage में process किया जाता है।

उदाहरण:

#include <iostream>

यहाँ #include directive द्वारा iostream header को program में include करने के लिए निर्देश दिया गया है।

4. Namespace

Namespace names को organize करने का एक तरीका है। C++ Standard Library के अधिकांश names std namespace में defined होते हैं।

इसलिए program में using namespace std; लिखकर cout जैसे names को सीधे उपयोग किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello";
    return 0;
}
Output:
Hello

using namespace std; के बिना Standard Library के names को explicitly std:: prefix के साथ भी लिखा जा सकता है:

#include <iostream>

int main()
{
    std::cout << "Hello";
    return 0;
}
Output:
Hello
Important: using namespace std; लिखना अनिवार्य नहीं है। std::cout, std::cin आदि का उपयोग करके namespace को explicitly specify किया जा सकता है।

5. main() Function

main() C++ program का मुख्य function है। Program का execution main() function से शुरू होता है। एक hosted C++ program में सामान्यतः एक main() function होता है।

एक basic main function:

int main()
{
    return 0;
}

यहाँ int यह बताता है कि main() function integer status value return करता है।

याद रखें: main() C++ program का primary entry point है।

6. Curly Braces { }

C++ में curly braces { } statements के एक block की सीमा निर्धारित करती हैं। main() function के अंदर लिखे statements curly braces के बीच होते हैं।

int main()
{
    cout << "Hello";
    return 0;
}

यहाँ { function body की शुरुआत और } function body के अंत को दर्शाता है।

7. Variable Declaration

Program में data store करने के लिए variables का उपयोग किया जाता है। Variable को use करने से पहले उसे declare किया जा सकता है। Declaration में variable का data type और name बताया जाता है।

#include <iostream>
using namespace std;

int main()
{
    int age;
    double marks;

    age = 17;
    marks = 85.5;

    cout << "Age = " << age << endl;
    cout << "Marks = " << marks;

    return 0;
}
Output:
Age = 17
Marks = 85.5

8. Initialization

Initialization का अर्थ variable को create करते समय उसे initial value देना है। C++ में variable declaration के साथ ही initialization किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    int number = 25;

    cout << "Number = " << number;

    return 0;
}
Output:
Number = 25

9. Executable Statements

Executable statements वे statements हैं जो program के execution के दौरान कोई operation perform करते हैं। इनमें assignment, calculation, function call, input/output और control statements आदि शामिल हो सकते हैं।

उदाहरण:

int a = 10;
int b = 20;
int sum = a + b;
cout << sum;

इन statements में variables बनाए जाते हैं, calculation की जाती है और result display किया जाता है।

10. Output Statement

C++ में standard output के लिए cout का उपयोग किया जाता है। cout के साथ << insertion operator का उपयोग किया जाता है।

#include <iostream>
using namespace std;

int main()
{
    cout << "Welcome";
    return 0;
}
Output:
Welcome

11. Input Statement

C++ में standard input लेने के लिए cin का उपयोग किया जाता है। इसके साथ >> extraction operator का उपयोग किया जाता है।

#include <iostream>
using namespace std;

int main()
{
    int number;

    cout << "Enter a number: ";
    cin >> number;

    cout << "You entered: " << number;

    return 0;
}
Sample Output:
Enter a number: 25
You entered: 25
याद रखें:
cin → Input
cout → Output

12. Operators and Expressions

C++ program में calculations और other operations perform करने के लिए operators और expressions का उपयोग किया जाता है।

उदाहरण:

#include <iostream>
using namespace std;

int main()
{
    int a = 15;
    int b = 5;

    int result = a * b;

    cout << "Result = " << result;

    return 0;
}
Output:
Result = 75

यहाँ * multiplication operator है और a * b एक expression है।

13. return Statement

return statement function से control और optional value वापस करने के लिए उपयोग किया जाता है। main() में return 0; सामान्यतः successful program termination का संकेत देता है।

int main()
{
    return 0;
}
return 0; सामान्यतः operating environment को successful termination का status देता है।

14. Semicolon ;

C++ में कई statements के अंत में semicolon (;) लगाया जाता है। यह compiler को statement के end को identify करने में सहायता करता है।

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    cout << a;

    return 0;
}
Output:
10

उपरोक्त program में variable declaration, output statement और return statement के अंत में semicolon लगाया गया है।

Complete Example of C++ Program Structure

अब एक ऐसे program को देखते हैं जिसमें comments, header, namespace, variables, calculation, output और return statement सभी components शामिल हैं:

// Program to calculate area of a rectangle

#include <iostream>
using namespace std;

int main()
{
    int length = 10;
    int width = 5;

    int area = length * width;

    cout << "Area = " << area;

    return 0;
}
Output:
Area = 50

Step-by-Step Structure of the Example

Part Code Purpose
Comment // Program to calculate area... Program का purpose बताता है।
Header #include <iostream> Input/output facilities उपलब्ध कराता है।
Namespace using namespace std; Standard Library names को सीधे use करने देता है।
Function int main() Program का main entry point है।
Variables int length = 10; Data store करते हैं।
Expression length * width Area calculate करता है।
Output cout Result display करता है।
Return return 0; Successful termination status देता है।

Order of Execution

C++ program में statements का execution सामान्यतः ऊपर से नीचे की ओर होता है, जब तक कि कोई control structure या function call execution flow को बदल न दे।

ऊपर दिए गए rectangle program में execution का basic flow इस प्रकार है:

Start → Initialize Variables → Calculate Area → Display Result → return 0 → End

Whitespace and Indentation

C++ में spaces, tabs और indentation का उपयोग code को readable बनाने के लिए किया जाता है। सामान्यतः indentation compiler के लिए आवश्यक नहीं होती, लेकिन अच्छी indentation program को समझना और maintain करना आसान बनाती है।

उदाहरण:

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int b = 20;

    cout << a + b;

    return 0;
}
Output:
30

Important Points

  • C++ program कई components से मिलकर बना होता है।
  • #include preprocessor directive का उपयोग header file include करने के लिए किया जाता है।
  • <iostream> standard input/output facilities के लिए commonly used header है।
  • main() C++ program का primary entry point है।
  • Curly braces { } function या block की boundaries define करती हैं।
  • Variables data store करने के लिए उपयोग किए जाते हैं।
  • cin standard input लेने के लिए और cout standard output देने के लिए commonly उपयोग होते हैं।
  • << output stream के साथ insertion operator है।
  • >> input stream के साथ extraction operator है।
  • कई C++ statements के अंत में semicolon ; लगाया जाता है।
  • return 0; main function से successful termination status indicate करता है।
  • Comments code की readability और documentation improve करते हैं।

Board Focus

Exam के लिए याद रखें:
Header File → #include <iostream>
Namespace → using namespace std;
Main Function → int main()
Output → cout <<
Input → cin >>
Block → { }
Statement Terminator → ;
Successful Return → return 0;
Single-line Comment → //
Multi-line Comment → /* ... */

Board Important Questions

Very Short Answer Questions

Q1. C++ program का मुख्य entry point क्या है?

Answer: main() function C++ program का मुख्य entry point है।

Q2. C++ में output display करने के लिए किसका उपयोग किया जाता है?

Answer: cout का उपयोग किया जाता है।

Q3. C++ में input लेने के लिए किसका उपयोग किया जाता है?

Answer: cin का उपयोग किया जाता है।

Q4. C++ में standard input/output के लिए कौन-सा header commonly उपयोग होता है?

Answer: iostream header।

Q5. C++ में statement के अंत में सामान्यतः कौन-सा symbol लगाया जाता है?

Answer: Semicolon (;)।

Q6. return 0; का क्या अर्थ है?

Answer: यह main function से successful termination status return करने के लिए commonly उपयोग होता है।

Q7. C++ में single-line comment के लिए कौन-सा symbol उपयोग होता है?

Answer: //

Q8. C++ में multi-line comment कैसे लिखा जाता है?

Answer: /* और */ के बीच।

Short Answer Questions

Q9. C++ program के मुख्य components लिखिए।

Answer: C++ program में comments, header files, namespace declaration, main() function, variable declarations, executable statements और return statement जैसे components हो सकते हैं।

Q10. #include <iostream> का क्या उपयोग है?

Answer: #include <iostream> एक preprocessor directive है जो iostream header को program में include करने का निर्देश देता है। यह standard input/output से संबंधित declarations और facilities, जैसे cin और cout, के उपयोग में सहायता करता है।

Q11. using namespace std; का क्या उपयोग है?

Answer: यह statement standard namespace के names को बिना std:: prefix के उपयोग करने की सुविधा देता है। उदाहरण के लिए, इसके बाद std::cout की जगह सीधे cout लिखा जा सकता है।

Q12. C++ में comments का क्या महत्व है?

Answer: Comments का उपयोग program के code को explain करने, उसका purpose बताने और readability तथा maintainability improve करने के लिए किया जाता है। Comments executable instructions नहीं होते।

Q13. cin और cout में अंतर बताइए।

cin cout
Input लेने के लिए उपयोग होता है। Output display करने के लिए उपयोग होता है।
>> extraction operator का उपयोग करता है। << insertion operator का उपयोग करता है।

Long Answer Questions

Q14. C++ program की basic structure को उदाहरण सहित समझाइए।

Answer: एक सामान्य C++ program में header file, namespace declaration, main() function, variable declarations, executable statements और return statement शामिल हो सकते हैं। Header file required library facilities उपलब्ध कराने में सहायता करती है। Namespace names को organize करता है। main() program का primary entry point है। Variables data store करते हैं और executable statements required operations perform करते हैं। अंत में return statement function से status या value return कर सकता है।

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int b = 20;

    cout << "Sum = " << a + b;

    return 0;
}
Output:
Sum = 30

Q15. C++ program में main(), cout, cin और return 0; की भूमिका समझाइए।

Answer: main() program का primary entry point है। cout standard output पर data display करने के लिए उपयोग होता है। cin standard input से data लेने के लिए उपयोग होता है। return 0; main function से successful termination status indicate करने के लिए commonly उपयोग किया जाता है।

Quick Revision

  • Comment: Code को explain करने के लिए।
  • Header: Required library facilities के लिए।
  • Namespace: Names को organize करने के लिए।
  • main(): Program का primary entry point।
  • Variable: Data store करने के लिए।
  • cin: Input लेने के लिए।
  • cout: Output देने के लिए।
  • return 0: Successful termination status के लिए commonly उपयोग।
  • ; कई statements का terminator।
  • { } Code block की boundaries।
One-Line Revision: एक सामान्य C++ program में header files और अन्य declarations के बाद main() function में variables तथा executable statements लिखे जाते हैं और program के अंत में return statement दिया जा सकता है।

Practice Questions

  1. C++ program की basic structure लिखिए।
  2. C++ program में header file का क्या महत्व है?
  3. #include <iostream> का क्या उपयोग है?
  4. using namespace std; क्या करता है?
  5. main() function की क्या भूमिका है?
  6. C++ में comments क्या हैं? इनके प्रकार लिखिए।
  7. cin और cout में अंतर बताइए।
  8. << और >> operators का उपयोग समझाइए।
  9. C++ में semicolon का क्या महत्व है?
  10. return 0; का क्या अर्थ है?
  11. C++ program में variables और executable statements की भूमिका समझाइए।
  12. एक simple C++ program लिखिए जो दो numbers का sum display करे और उसका output बताइए।
  13. C++ program के विभिन्न components को उदाहरण सहित समझाइए।
  14. एक complete C++ program की structure को विस्तार से समझाइए।
Lesson 17 of 39
On This Page