Lesson 6 of 60 – First C++ Program
10%

First C++ Program

In this lesson, we will create our first C++ program and understand each part of the program step by step. A basic C++ program contains a main function, statements, and, when required, header files for standard library features.

Note: C++ source files commonly use the .cpp extension. We will use iostream and std::cout to display output on the screen.

1. Our First C++ Program

Here is a simple C++ program:

#include <iostream>

int main() {

    std::cout << "Hello World!";

    return 0;
}

This program displays Hello World! on the screen.

2. Create a C++ File

First, create a new file and save it with the .cpp extension.

For example:

first.cpp

The .cpp extension is commonly used for C++ source files.

3. Include the iostream Header

Our program uses std::cout to display output. The declaration for standard stream functionality is provided through the iostream header.

#include <iostream>

The #include directive makes the declarations from the specified header available to the program.

4. What is #include?

#include is a preprocessor directive. It tells the preprocessing stage to include the contents or declarations provided by a header file.

#include <iostream>

It is commonly written before the main function when the program needs features provided by that header.

5. What is iostream?

iostream is a standard C++ header that provides facilities for standard input and output streams.

It is commonly used with:

  • std::cout – standard output
  • std::cin – standard input
  • std::cerr – standard error output
  • std::clog – standard logging output

6. The main() Function

The main() function is the entry point of a normal C++ program. Program execution begins from main.

int main() {

    // Program statements

}

A C++ program normally needs a main function as its starting point.

7. Why int main()?

In the declaration int main(), int is the return type of the main function.

int main() {
    return 0;
}

The integer return value communicates the program's termination status to the environment.

8. The Opening and Closing Braces

The braces { } define the body of a function or other block of C++ code.

int main() {

    // Code goes here

}

Statements written between these braces belong to the body of main.

9. Using std::cout

std::cout is commonly used to send output to the standard output stream.

std::cout << "Hello World!";

The text inside the quotation marks is displayed as output.

10. What is std::?

The std:: prefix identifies a name that belongs to the standard namespace.

std::cout
std::cin
std::string

Using std:: explicitly makes it clear that the name comes from the C++ standard library namespace.

11. The << Operator

The insertion operator << is commonly used with std::cout to send values to the output stream.

std::cout << "Hello";

It can also be used several times in the same statement.

std::cout << "Hello " << "World!";

12. String Literals

Text enclosed in double quotation marks is called a string literal.

"Hello World!"

For example:

std::cout << "Welcome to C++";

13. The Semicolon

Many C++ statements end with a semicolon ;.

std::cout << "Hello";

The semicolon marks the end of the statement.

14. The return 0 Statement

The statement return 0; returns the integer value 0 from the main function.

int main() {

    std::cout << "Hello";

    return 0;
}

A return value of zero is commonly used to indicate successful termination.

15. Complete Program Structure

#include <iostream>

int main() {

    std::cout << "Hello World!";

    return 0;
}

The program contains a header inclusion, the main function, an output statement, and a return statement.

16. Printing Multiple Lines

You can use \n to move the output to the next line.

#include <iostream>

int main() {

    std::cout << "Hello\n";
    std::cout << "Welcome to C++\n";
    std::cout << "Programming is fun!";

    return 0;
}

17. Using std::endl

std::endl inserts a newline into the output stream and also flushes the stream.

#include <iostream>

int main() {

    std::cout << "Hello" << std::endl;
    std::cout << "C++";

    return 0;
}

18. Printing Numbers

C++ can also print numeric values using std::cout.

#include <iostream>

int main() {

    std::cout << 100;

    return 0;
}

The output will be:

100

19. Printing Variables

You can display the value stored in a variable.

#include <iostream>

int main() {

    int age = 20;

    std::cout << age;

    return 0;
}

The value stored in age will be displayed.

20. Printing Text and Variables Together

#include <iostream>

int main() {

    int age = 20;

    std::cout << "Age: " << age;

    return 0;
}

Multiple values can be sent to std::cout using multiple << operators.

21. Taking Input

C++ can also take input from the user using std::cin.

#include <iostream>

int main() {

    int age;

    std::cout << "Enter your age: ";
    std::cin >> age;

    std::cout << "Your age is: " << age;

    return 0;
}

22. The >> Operator

The extraction operator >> is commonly used with std::cin to read input.

int number;

std::cin >> number;

The entered value is stored in the variable number.

23. First Program with User Input

#include <iostream>

int main() {

    int number;

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

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

    return 0;
}

This program asks the user for a number and then displays the entered value.

24. Adding Two Numbers

We can use variables and arithmetic operators in our first programs.

#include <iostream>

int main() {

    int a, b;

    std::cout << "Enter two numbers: ";
    std::cin >> a >> b;

    int sum = a + b;

    std::cout << "Sum = " << sum;

    return 0;
}

25. Comments in the First Program

Comments can be used to explain parts of a C++ program.

#include <iostream>

int main() {

    // Display a message
    std::cout << "Hello World!";

    return 0;
}

The compiler does not treat the comment as an executable statement.

26. Compile the First Program

If you are using GCC, save the program as first.cpp and compile it with:

g++ first.cpp -o first

If there are no compilation errors, an executable named first (or first.exe on Windows) will be created according to the platform.

27. Run the First Program

On Linux or macOS, the executable can commonly be run with:

./first

On Windows, depending on the terminal, you can commonly use:

.\first.exe

The program will then display its output.

28. Common Mistakes

Beginners commonly make these mistakes:

  • Forgetting #include <iostream>
  • Forgetting the semicolon
  • Misspelling std::cout
  • Forgetting quotation marks around text
  • Using the wrong file extension
  • Trying to run a program before compiling it

29. First Program Checklist

Part Purpose
#include <iostream> Provides standard input/output stream facilities
int main() Starting point of the program
{ } Defines the function body
std::cout Displays output
<< Sends data to the output stream
return 0; Returns a successful status from main

30. Complete First C++ Program

Here is a complete beginner-friendly C++ program that uses input, processing, and output:

#include <iostream>

int main() {

    int age;

    std::cout << "Enter your age: ";
    std::cin >> age;

    std::cout << "You are " << age << " years old.";

    return 0;
}

This program demonstrates the basic structure of a C++ application: including a header, starting execution with main(), taking input, and displaying output.

📌 Key Points

  • A C++ source file commonly uses the .cpp extension.
  • #include <iostream> provides standard stream facilities.
  • The main() function is the normal starting point of a C++ program.
  • std::cout is commonly used to display output.
  • std::cin is commonly used to take input.
  • The << operator sends data to an output stream.
  • The >> operator reads data from an input stream.
  • Most C++ statements end with a semicolon.
  • return 0; returns an integer status from main.
  • A C++ program must be compiled before it can normally be executed.

🧠 Quick Quiz

Question: Which function is the normal entry point of a C++ program?