codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

2 / 87 topics
1Getting Started with C++2Your First C++ Program3C++ Syntax4C++ Comments5Basic Input / Output
Tutorials/C++ Programming/Your First C++ Program
⚡C++ Programming

Your First C++ Program

Updated 2026-05-12
15 min read

Your First C++ Program

Welcome to your journey into the world of C++ programming! In this tutorial, you'll learn how to write and run your first C++ program. We'll cover essential components like #include, main(), and cout. Understanding these basics will set a solid foundation for more complex programs in the future.

Introduction

Programming is like building with blocks; each block has its specific role. In C++, the smallest unit of code is called a statement, and it ends with a semicolon (;). The main() function acts as the entry point of your program, similar to how you start building from the bottom of a tower.

1. Writing Your First Program

Let's start by writing a simple "Hello, World!" program in C++. This is traditional first program that every programmer writes to get familiar with the language.

Step-by-Step Guide

  1. Open your preferred text editor or IDE: You can use editors like Visual Studio Code, Sublime Text, or even a simple notepad.
  2. Create a new file and save it as hello.cpp.
  3. Write the following code:
hello.cpp
1#include <iostream>
2
3int main() {
4 std::cout << "Hello, World!" << std::endl;
5 return 0;
6}

Explanation

  • #include &lt;iostream&gt;: This line includes the iostream library which is necessary for input and output operations. It's like importing a toolbox that contains functions to display text on the screen.

  • int main(): This is the main function where the execution of your program begins. Every C++ program must have one main() function.

  • std::cout << "Hello, World!" << std::endl;: This line uses the cout object from the iostream library to print "Hello, World!" to the console. The << operator is used to insert data into the output stream, and std::endl is used to end the line and flush the buffer.

  • return 0;: This statement indicates that the program has ended successfully. Returning 0 is a convention to signify successful execution.

2. Compiling Your Program

To run your C++ program, you need to compile it first using a compiler like g++. Here’s how:

  1. Open your terminal or command prompt.
  2. Navigate to the directory where hello.cpp is saved.
  3. Compile the program with the following command:
Terminal
$ g++ hello.cpp -o hello
  • g++: This is the GNU Compiler Collection, which compiles C++ code.
  • hello.cpp: The name of your source file.
  • -o hello: This option tells the compiler to output an executable named hello.

3. Running Your Program

After successful compilation, you can run your program using:

Terminal
$ ./hello

You should see the following output:

Output
Hello, World!

Congratulations! You've just written and executed your first C++ program.

4. Understanding Program Structure

Let's break down the structure of a basic C++ program:

  1. Preprocessor Directives: Lines starting with # are preprocessor directives. They are processed before the actual compilation starts.

    • #include &lt;iostream&gt;: Includes the iostream library for input and output operations.
  2. Global Declarations: These are declared outside any function and can be accessed by all functions in the program.

  3. Function Definitions:

    • int main(): The main function where execution begins.
      • { ... }: Braces define a block of code.
      • Statements inside braces are executed sequentially.
      • return 0;: Indicates successful termination.

5. Common Mistakes and Tips

  • Missing Semicolons: C++ requires semicolons at the end of most statements. Forgetting them can lead to compilation errors.

  • Case Sensitivity: C++ is case-sensitive. cout and Cout are different identifiers.

  • Indentation: While not mandatory, proper indentation makes your code more readable and helps avoid syntax errors.

Tip

It's a good practice to compile your program after writing each line of code to catch errors early.

6. Practical Example

Let’s write a simple program that adds two numbers and prints the result:

add_numbers.cpp
1#include <iostream>
2
3int main() {
4 int num1 = 5;
5 int num2 = 3;
6 int sum = num1 + num2;
7
8 std::cout << "The sum of " << num1 << " and " << num2 << " is " << sum << std::endl;
9 return 0;
10}

Output:

Output
The sum of 5 and 3 is 8

Explanation

  • We declared three integer variables: num1, num2, and sum.
  • We assigned values to num1 and num2.
  • We calculated the sum of num1 and num2 and stored it in sum.
  • We used cout to print the result.

7. Summary

In this tutorial, you learned how to write, compile, and run your first C++ program. You understood the basic structure of a C++ program, including preprocessor directives, functions, and statements. Key concepts like #include, main(), and cout were introduced, and common mistakes were highlighted.

ConceptDescription
#includeIncludes libraries for input/output operations.
main()The entry point of the program where execution begins.
coutUsed to output data to the console.
CompilationConverts source code into an executable file using a compiler like g++.
ExecutionRunning the compiled executable to see the output of your program.

8. What's Next?

Now that you have a basic understanding of C++ syntax and structure, it’s time to dive deeper into more advanced topics such as data types, operators, control structures, and functions. In the next tutorial, we will explore C++ syntax in detail, helping you write more complex programs.

Stay tuned for the next chapter where you'll learn about variables, constants, and different data types in C++. Happy coding!


PreviousGetting Started with C++Next C++ Syntax

Recommended Gear

Getting Started with C++C++ Syntax