Menu
Coddy logo textTech

Basic Program Structure

Part of the Fundamentals section of Coddy's C++ journey — lesson 4 of 74.

In C++, most executable code is written inside functions. The main function is the entry point of a C++ program — when you run a C++ program, the code inside main is the first to be executed. Some code, like global declarations and preprocessor directives, can appear outside of functions, and you can also create your own custom functions (we'll cover that in a later lesson).

Here's a simple breakdown of a basic C++ program:

#include <iostream> // Preprocessor directive for input/output

int main() { // Main function
    std::cout << "This is my first C++ program!"; // Output statement
    return 0; // Return statement (good practice)
}

In C++, the #include preprocessor directive is used to include header files, which contain declarations of functions and objects that your program can use. In this case, #include <iostream> includes the iostream header, which provides objects like std::cout for outputting text to the console.

Let's break down the key parts of the program above:

  • Preprocessor directive (#include <iostream>): This line runs before compilation and tells the compiler to include the iostream header file, giving your program access to input/output tools.
  • Function (int main() { ... }): A function is a named block of code that performs a task. The int before main indicates the function returns an integer value. The curly braces {} define the scope of the function — everything inside them belongs to main. Scoping means that code inside a block is contained within it and runs as part of that block.
  • Namespace prefix (std::): The std:: prefix tells the compiler to look for cout inside the standard library namespace. A namespace is a way to group related names and avoid conflicts. We'll explore namespaces more in a later lesson.
  • Output statement (std::cout): This prints text to the console.
  • Return statement (return 0;): This signals that the program finished successfully. While C++ does not strictly require a return statement in main, including it is considered good practice.

Important note: In C++, each statement must end with a semicolon (;). The semicolon is mandatory and tells C++ that you've reached the end of a statement. Forgetting to add a semicolon will result in a compilation error. However, note that code blocks enclosed in curly braces {} (like function declarations) don't need semicolons.

challenge icon

Challenge

Beginner

Create a C++ program with a main function. Inside the main function, write code to output the following text:

This is my first C++ program!

Cheat sheet

Basic C++ program structure:

#include <iostream> // Include header for input/output

int main() { // Entry point of the program
    std::cout << "This is my first C++ program!"; // Output text
    return 0; // Signals successful completion
}
  • #include <iostream> — includes input/output tools like std::cout
  • int main() { ... } — the main function; execution starts here
  • std:: — namespace prefix to access standard library names
  • std::cout — prints text to the console
  • Every statement ends with ; — code blocks {} do not

Try it yourself

#include <iostream>
// Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals