SrcForge

Lesson 3

Structure of a C++ Program

Covers C++ program parts, execution flow, mistakes, best practices.

Lesson content

Structure of a C++ program

Every C++ program follows a basic structure. Every app uses these building blocks.

  • Documentation (Comments)
  • Preprocessor Directives
  • Namespace Declaration
  • Global Declarations (Optional)
  • main() Function
  • Statements and Program Logic
  • Return Statement
                    C++ Program

      ┌──────────────────┼──────────────────┐
      │                  │                  │
      ▼                  ▼                  ▼
 Documentation     Header Files      Namespace
   (Comments)      (#include)       Declaration


              Global Declarations (Optional)


                 int main() Function

      ┌──────────────────┼──────────────────┐
      │                  │                  │
      ▼                  ▼                  ▼
 Variable          Program Logic      Input/Output
Declarations      (Statements)      (cin / cout)


                    return 0;
// Documentation Section: Explain the purpose of the program

#include <iostream>          // Include the input/output stream library

using namespace std;         // Use the standard namespace

// Global variable declaration (optional)
int globalVariable = 100;

int main()                   // Program execution starts here
{
    // Local variable declaration
    int number = 10;

    // Display a message on the console
    cout << "Welcome to C++ Programming!" << endl;

    // Display the value of the local variable
    cout << "Number = " << number << endl;

    // Return 0 to indicate successful execution
    return 0;
}

Components of a C++ program

1. Documentation section (comments)

Comments describe the program. Compiler ignores them. They aid readers.

// Single-line comment

/*
    Multi-line comment
*/
  • Explain complex code
  • Improve readability
  • Help during maintenance
  • Make collaboration easier

2. Preprocessor directives

Directives start with #. Processed before compilation. #include is most common.

#include <iostream>

iostream gives input/output via cin, cout, and endl.

3. Namespace declaration

Namespace groups identifiers. Avoids naming conflicts. Most beginners use std.

using namespace std;

// Without it, write:
std::cout << "Hello";

// Instead of:
cout << "Hello";

Best practice: prefer std::cout in large projects over using namespace std.

4. Global declarations (optional)

Variables outside main() are global. Accessible from many functions.

int score = 100;

Use global declarations carefully. Overuse hurts maintainability.

5. The main() function

Execution starts at main(). OS calls this function first.

int main()
{
    return 0;
}

Only one main() function allowed per program.

6. Program statements

main() body holds the instructions. These perform the actual tasks.

  • Declaring variables
  • Accepting user input
  • Performing calculations
  • Displaying output
  • Calling functions
int age = 20;

cout << age;

7. Return statement

return ends main(). Sends a value back to the OS.

return 0;

Return value 0 means successful execution.

Program execution flow

Start


Preprocessor Processes Header Files


Compiler Compiles the Source Code


Execution Begins at main()


Program Statements Execute


return 0;


Program Ends

Common mistakes

  • Missing #include <iostream>. Fix: include needed headers.
  • Writing Main() instead of main(). Fix: use lowercase main.
  • Forgetting semicolons. Fix: end statements with semicolon.
  • Using cout without std:: or namespace. Fix: add std:: or declare namespace.
  • Declaring variables after use. Fix: declare before using.
// Incorrect
#include <iostream>

int Main()
{
    cout << "Hello"

    return 0;
}
// Correct
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello";

    return 0;
}

Best practices

  • Write comments only when necessary
  • Include only needed header files
  • Use consistent indentation
  • Keep main() short, move code to functions
  • Prefer std:: prefix in larger projects
  • Use descriptive variable and function names

Summary

  • Execution starts from main()
  • Header files enable standard library features
  • Namespace simplifies standard library usage
  • Statements hold the program logic
  • return 0; signals successful completion
  • Standard structure improves readability and scale

Frequently asked questions

Is main() mandatory in every C++ program?

Yes. Every program needs exactly one main() entry point.

Why do we include header files in C++?

Headers declare library functions and classes. iostream enables cin and cout.

What is the purpose of using namespace std;?

Lets you skip std:: prefix. Larger projects should still prefer std::.

What does return 0; mean?

Signals successful execution. Returns success status to the OS.

Can a C++ program have more than one main()?

No. Multiple main() functions cause a compilation error.