DEV Community

Cover image for C++ from the beginning - your first program
Chris Noring for Microsoft Azure

Posted on • Updated on

C++ from the beginning - your first program

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

Resources

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
  • Mac Mac usually have g++ installed already but this guide shows you how to install clang and use it with Visual Studio Code.
  • Windows

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:
   #include <iostream>
   using namespace std;

   int main()
   {
     cout << "First program in C++\n";
     return 0;
   }
Enter fullscreen mode Exit fullscreen mode

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:
   g++ app.cpp
Enter fullscreen mode Exit fullscreen mode

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.

  2. Run the file:

   ./a.out # Linux, macOS
   a.out.exe # windows
Enter fullscreen mode Exit fullscreen mode

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):
#include 
using namespace std;

int main()
{
  cout << "First program in C++\n";
  cout << "Here's another row"; 
  return 0;
}
  1. Compile and run the code again:
   g++ app.cpp
   ./a.out
Enter fullscreen mode Exit fullscreen mode

You should see:

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

 Explain the code:

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

#include 

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

#include 
using namespace std;

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.

#include 
using namespace std;

int main()
{
}

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.

#include 
using namespace std;

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

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

#include 
using namespace std;

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

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.

Top comments (0)