C++ from the beginning - your first program
Tags: cpp, beginners, tutorial
Published: 2021-11-03 18:49:06

---
title: C++ from the beginning - your first program
published: true
description: A series on C++ starting with your first program.
tags: cpp, beginners, tutorial 
series: C++ beginners
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/maeu5b8eil4q529upohu.jpg
---

> TLDR; welcome to this series on C++, it will take you through the basics of C++ so you at the end of the series is able to use the language in a professional manner.

## The full series

- Your first program - **you are here**
- [Variables](https://dev.to/azure/c-from-the-beginning-variables-22nm)
- [Working with input and output](https://dev.to/azure/c-from-the-beginning-input-and-output-5ad0)
- [Functions](https://dev.to/azure/c-from-the-beginning-functions-5cnl)
- [Control flow](https://dev.to/azure/c-from-the-beginning-control-flow-21jg)
- Working with files
- Pointers
- Error management
- Structs and Classes

## Resources

- [CPP Snippets for VS Code](https://marketplace.visualstudio.com/items?itemName=chris-noring.cpp-snippets&ssr=false#overview)
- [C++ module](https://docs.microsoft.com/en-us/learn/modules/cpp-first-program?wt.mc_id=academic-0000-chnoring)

## Why C++

C++ was invented by Bjarne Stroustrup in 1985. It's estimated to have more than 5.5 million users. 

Where is it used though?

It's used by major corporations like Google, Facebook, Microsoft and so on and here's some areas:

- Medical industry
- Gaming
- Video editing
- Backend systems
- Compilers

All of these areas needs highly performant languages where you are able to ensure speed, control memory usage and more. 

## How do I get started

First, you need a compiler. Check out the below guides to make sure you are set up for your OS:

- [Linux](https://code.visualstudio.com/docs/cpp/config-linux)
- [Mac](https://code.visualstudio.com/docs/cpp/config-clang-mac) Mac usually have g++ installed already but this guide shows you how to install clang and use it with Visual Studio Code.
- [Windows](https://code.visualstudio.com/docs/cpp/config-mingw)

Once you are setup, you are ready to create your first program in C++.

## Your first program

In this first program, you'll take some code, use the compiler to create a program that you can run.

1. Create a file _app.cpp_ and add this code:

   ```cpp
   #include <iostream>
   using namespace std;

   int main()
   {
     cout << "First program in C++\n";
     return 0;
   }
   ```

   The above code prints out "First program in C++". Before we can see the output, let's compile the program.

1. Use the compiler calling `g++ app.cpp`:

   ```bash
   g++ app.cpp
   ```

   Note how it takes the name of your file as input. What happens now are many things:
   - **preprocessing**. A preprocessor deals with directives like your first line `#include` and tries to resolve what they mean. In this case, your first line brings a streaming library `iostream` into scope that allows you to print to the screen. The output is a C++ without preprocessor directives.
   - **compilation**. Next, the compiler takes the C++ output from the first step and turns it into an object file.
   - **linking**. This is the last step, in which the object file is turned into either a library or an executable file that you can run.

1. Look at the output, you should have an _a.out_ file in the same directory as you ran `g++`. It's a runnable file, exe file on Windows and a file with permission X(executable) on Linux/macOS.

1. Run the file:

   ```bash
   ./a.out # Linux, macOS
   a.out.exe # windows
   ```

   You should see "First program in C++". Next, let's try to modify the code by adding a new row:

1. Modify the code like so (add the part in bold):

<pre>
#include <iostream>
using namespace std;

int main()
{
  cout << "First program in C++\n";
  <b>cout << "Here's another row";</b> 
  return 0;
}
</pre> 

1. Compile and run the code again:

   ```bash
   g++ app.cpp
   ./a.out
   ```

   You should see:

   "First program in C++"
   "Here's another row"

## Explain the code:

Let's go through the code, line by line:

<pre>
#include <iostream>
</pre>

The above bring in the streaming library `iostream` that's able to manage reading and writing to the console.

<pre>
#include <iostream>
<b>using namespace std;</b>
</pre>

Next line brings in the namespace `std` which enables you to type `cout`, instead of `std::cout`. A namespace is logical grouping where you usually place all programming constructs that goes together.

<pre>
#include <iostream>
using namespace std;

<b>int main()
{
}</b>
</pre>

In this code, a function `main()` is defined. A function groups programming statements into a named reference that you can call. In the case of `main()`, it's called by the program by itself when the program is started at the console. The `main()` function is also known as the _entry point_, where the program begins.

<pre>
#include <iostream>
using namespace std;

int main()
{
  <b>cout << "First program in C++\n";
  cout << "Here's another row";</b> 
}
</pre> 

Here two calls to `cout` is made, which means it writes out strings to the console that the user can see.


<pre>
#include <iostream>
using namespace std;

int main()
{
  cout << "First program in C++\n";
  cout << "Here's another row"; 
  <b>return 0;</b>
}
</pre> 

Lastly, a `return` statement is added to the `main()` function which means it fulfills the contract of the `main()` function, which says to return a number `int main()`.

## Summary

This was your first look into C++, stay tuned for more parts on C++ in this beginner series.