So, you want to do coding? Great choice! You’re getting into what is essentially wizardry, teaching an electrified rock how to do new tricks, and C++ is one of the best ways to do that. Yes, C++ may not be the most beginner friendly language, but that’s what I’m here for. I’ve learned the ropes, and it’s quite a fun language once you know the basics.
So why would I even choose C++?
C++ is a very flexible language. It’s used in 10% of GitHub repositories, making it the 5th most popular programming language in the world. C++ is most widely used with low level applications such as operating systems, embedded systems, and performance critical applications and services. C++ is also a very performant language, as it compiles straight to binary (the 1’s and 0’s which your computer can understand) instead of using a layer of abstraction, like Python or JavaScript.
Who is this guide for?
This guide is for people who are interested in programming, but haven’t tried any before. You do need to be able to use your computer, and if you’re on Windows you’ll need admin access.
How do I get started?
First, you’ll need to get a compiler for C++. A compiler translates your code from human-readable C++ into the binary language your computer can understand.
I want to start programming now!
If you’re overexcited, I get that! Programming is fun! Luckily, there are many websites you can use to run code you write online. My personal favourite for C++ is https://cpp.sh, as it’s a very simple runner which just works. If it isn’t your style, try looking up something along the lines of “c++ online”. Then jump down to the “Let’s write some code!” section. Later on I do recommend following the steps below to set up an environment on your own computer, as that gives you more flexibility.
Note: While we’re writing code, I’ll tell you to save and run your code. You can do this by pressing “Run” or “Compile” on your website.
Set up a compiler on your own computer
macOS or Linux users
If you’re on macOS or Linux (including Chromebook Linux and WSL), you should be good to go already! C++ (specifically g++) is installed out of the box on those machines to make coding easy. Test your compiler is working by opening up an app called “Terminal” (sometimes called “Konsole” on Linux). This will bring you to a hacker-looking prompt. You can then type: g++ --version
, and press enter. If you see the version number below where you typed, you should be fine!
If you get an error along the lines of g++: Command not found
, look up on the internet “install c++ compiler (your OS here)”. Otherwise, you’re good to go!
Windows users
If you’re on Windows, things are a bit harder to get working. If you’re just programming for fun, or not planning to distribute your app, you can either try installing gcc (a collection of compilers for different programming languages), or using a website (as explained above). To install GCC, open Command Prompt (Windows 10) or Terminal (Windows 11) and type winget install llvm.llvm
. Press enter and follow the prompts. After that’s finished, type g++ --version
into your terminal, press enter, and check that it tells you a version number. If not, try the online editor, or Windows Subsystem for Linux (find a guide online).
After you get your compiler ready, you need somewhere to write your code.
Getting a code editor
If you’ve gone with an online version of C++, skip this section.
Code editors are, well, editors for your code. You can get most of them for free. You can’t use Word as your code editor because Word is not designed to edit code. I’ve written an article about my favorites, which you can access at Code Editors: My Top 7 Picks.
Now we have our code editor, we can start writing some code!
Let’s write some code!
Note: I recommend typing out the code manually, instead of copying and pasting it. This will help you remember everything you need to do.
First, make a file in your text editor, normally by pressing Ctrl+N. Press Ctrl+S to save it, and make a filename ending in .cpp (e.g. project.cpp). For now, save your code in the Documents folder for ease of access. Make sure to remember where you save your file, and what the filename is!
First of all, we need write these six lines of code. I’ll explain what they do after.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
Save your code by pressing Ctrl+S. Now open your Terminal (or Command Prompt if you’re on Windows 10). By default, your terminal will be focused on your user’s home folder, but we saved our code in the Documents folder. You’ll need to tell your terminal to look in the Documents folder. To do this, type cd Documents
(ensure you have the capital letter). cd
is an acronym for “change directory”.
Now that your terminal is focused on your Documents folder, we can compile our code, turning it into the 1’s and 0’s our computer understands. To do this, run:
g++ (name of your file).cpp -o program
If you’re on Windows, replace “program” with “program.exe”.
If everything went well, your terminal shouldn’t report any errors. If there were errors, check you typed everything right. You can now run the app you’ve just made! If you’re on Linux or macOS, type “./program” into your terminal and press enter. If you’re on Windows, type “program.exe” into your terminal. (If that doesn’t work, you might be in PowerShell. Type “./program.exe” instead.)
When you run your program, it should say “Hello World!” in the terminal! Congrats! You’ve written your first C++ program. Now, let’s look at what each line of code does.
What each line means
#include <iostream>
“iostream” is a library for C++. In most programming languages, libraries give your program extra functionality. In this case, “iostream” lets your program output text to your terminal, read text input, and some other helpful things.
using namespace std;
This line means you have to write less code. It’s a bit complicated.
int main() {
C++ lets you create “functions”, which are pieces of code you can activate at any time. This line creates the main
function, which is where your program starts executing. The int
at the start stands for integer, but I’ll explain that later.
The opening and closing curly brackets contain our code for the main
function.
cout << "Hello World!" << endl;
This is a bit of a complicated line of code, but once you know what each thing does, it’ll become natural.
cout
stands for “character output”, and lets your program say something using the terminal. The two arrows pointing to it send whatever is after to the character output, which will get printed out on the terminal. In this case, we are sending "Hello World!"
to the character output. "Hello World!"
is a string of characters in C++.
But what does endl
mean? endl
stands for “end line”. This just makes sure each time you output something new, you start a fresh new line in your terminal. endl
does the same thing as adding \n
to the end of your string, so if we wanted we could rewrite this line as cout "Hello World!\n"
. It’s up to your personal preference.
The semicolon at the end symbolizes that this line of code is over.
return 0;
Remember when we were starting our main
function? We had the int
keyword at the start. The int
keyword means that our function, once it’s finished, will give back to whatever called it (in this case our computer) a number. This line returns 0 to the computer. Returning 0 signals to the computer that your program finished without error.
Again, the semicolon at the end symbolizes that the line of code is over. Don’t forget semicolons in C++. Ever.
}
This closes the code for our main function.
Doing math
In C++ and most other programming languages, you can solve math problems. Your computer is just a fancy calculator at heart.
Take the code we wrote before (get your text editor back up), and delete the “Hello World!” from your code. It should look like this now:
#include <iostream>
using namespace std;
int main() {
cout << << endl;
return 0;
}
In between the two <<
‘s, write 1 + 1
. This will output the result of 1 + 1
. Save and compile your code as you did before, and rerun the program. You should get a result of 2 in your terminal.
You don’t just have to add. You can subtract, multiply and divide as well. Replace the +
with a -
for subtraction, a *
for multiplication, or a /
for division. Try out a couple of different combinations. Remember to save and compile your code before running it!
Help! I’m stuck!
- Go back to the previous instructions. Have you saved your code, compiled it with the terminal, and run it with the terminal?
- Does your code have this line: `cout << 1 + 1 << endl;`
- If everything really isn’t working, ask an AI assistant like ChatGPT, Claude or Gemini for help. Copy and paste your code in, and ask why it isn’t working.
Variables
C++ lets you set variables. Variables are values that can be reused and changed over time. The basic syntax (rules for writing code) to define a variable is
type name = value;
Here’s an explaination:
- Type is the type of variable you’re going to have. C++ has a couple of different types in built.
int
is for integers (numbers without a decimal),string
is for strings of characters (like “Hello World!”), anddouble
is for numbers with a decimal. - Name is what you’d like to call your variable. You can name your variable anything, so long as it only has letters, numbers and underscores. It’s best to keep the name short, simple and on point. Some good names would be
scoreCounter
,userInput
, orhelpMessage
. - The equals sign tells the computer you’re setting that variable to whatever value is.
- Value is… well… a value. Make sure you set the value to be relevant to the type. If your type is
int
, some valid values would be34
,8
, or3 + 4
(you can do maths inside of variables). If your type isstring
, some valid values would be “Hello World!”, “C++ is fun”, or “Down with Rust!”. - Make sure to remember the semicolon at the end!
Here are some example variables:
int scoreCounter = 550;
string helpMessage = "This is my C++ program";
Note: the keyword string
is part of the iostream
library mentioned before. If you deleted that line, you would have to do strings in a different way.
Using variables
Change your code file to look like this:
#include <iostream>
using namespace std;
int main() {
string myMessage = "Hello there! This is my C++ program.";
return 0;
}
In this code, we’ve created a string variable, but we haven’t done anything with it. After you write the string myMessage
… line, add in a line that says
cout << myMessage << endl;
What this does is it accesses the myMessage
variable which you created, and sends it to the character output. Save, compile and run the code with the terminal, and check whether myMessage pops up.
Changing variables
Did you know that once you’ve made a variable, you can change it? That’s what makes a variable a variable. Let’s try changing a variable.
Redefining a variable
If you’d like to completely change the value of a variable, you can do it like this:
myVariable = content;
Make sure you’ve defined your variable first by using the type before. Here are a couple examples:
int myNum = 5;
myNum = 7;
string myText = "This is my text";
myText = "Now my text is different";
Remember that you need to include iostream, use namespace std, and start your main function first!
Adjusting a variable
Adjusting variables works differently to redefining depending on the type. If your type is an int
(or a double
), you can add to it or increment/decrement it.
(Now is also a good time to mention that //
symbolizes a comment in C++. Any line starting with //
will be ignored.)
int myNum = 5;
// Increment a number by 1
myNum ++;
// You can also do this with subtraction
myNum --;
// Add, subtract, multiply, or divide easily
myNum += 5;
myNum -= 5;
myNum *= 5;
myNum /= 5;
// These lines can also be written like this
myNum = myNum + 5;
If your type is a string
, you can add characters to the end, or join two strings together by adding them.
string myText = "This is my text.";
// Add something to the end of your text
myText += " And I've added something to the end of it.";
// Join two strings together
string myOtherText = "This was my old text: " + myText + " Now this is my new text. Hello!";
I’m confused. How do these variable additions work?
When you use a variable inside your code, your computer substitutes that variable with it’s contents. So if I had string myText = "Hello";
, I could write string myOtherText = myText + " There";
, and the computer will substitute “Hello” into where you write myText
.
Taking input from the users
In a C++ program, there are two main ways to get input from a user – command line arguments, and interactive input. Command line arguments let a user add options to your command. That would look like this:
$ ./program hello
(Sidenote: a dollar sign usually symbolises something you’ve written in your terminal, whereas any lines not starting with a dollar sign is from your program.)
Interactive input lets a user input text while your program is running. This would look like:
$ ./program
What do you like? ice cream
I like ice cream too!
It’s up to you what you use in your program. I’ll show you how to do both.
Command line arguments
Write this code:
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
cout << argv[1] << endl;
return 0;
}
Save and compile. When running, add a command line argument, like said before, and press enter. This code will repeat whatever your first argument is (arguments are seperated by spaces).
You might be wondering what int argc, char* argv[]
inside the brackets mean. Those words are defining variables (in this case, an integer and a pointer to a list of characters. I’ll cover pointers and characters later.) Functions can take arguments from whichever else has called them. We’re taking the command line arguments from the terminal, as well as the amount of them.
Generally, it’s good practice to add int argc, char* argv[]
to the main function every time, but I’ll leave it out for simplicity during the tutorial unless we need it.
Note: What does argv[1]
mean?
In our code, argv
is an array. An array is a sort of list in C++. Saying argv[1]
means we are accessing the item at index 1 in the array. I’ll explain arrays (and other kinds of lists) later on.
Interactive input
Write this code:
#include <iostream>
using namespace std;
int main() {
string userInput;
cout << "What do you like to eat?" << endl;
getline(cin, userInput);
cout << "I like to eat " + userInput + " too!" << endl;
return 0;
}
This will do the following:
- Create a
string
variable calleduserInput
, but it won’t put anything inside of it. - Ask the user “What do you like to eat?”
- Wait for the user to type something in to the terminal and press enter
- Reply with “I like to eat” (what you said) “too!”
- Exit back to the prompt
Save, compile and run the code.
Note: What is getline?
Getline is a function that comes from the iostream
library. It allows us to read from cin
(which stands for character input) and put it into a variable.
Adding logic to your code
In C++, you want your code to be able to do different things depending on what each of your variables are. This is where if
statements come in. if
statements allow you to execute certain code based on certain conditions. Here’s the syntax for an if statement:
if (condition1) {
(this code will be run if condition1 is true)
} else if (condition2) {
(this code will be run if condition2 is true)
} else {
(this code will be run if neither of those are true)
}
And here’s an example (using interactive user input):
#include <iostream>
using namespace std;
int main() {
string userInput;
cout << "What do you like to eat?" << endl;
getline(cin, userInput);
if (userInput == "cheese") {
cout << "mmmm i love cheese" << endl;
} else {
cout << "I don't like that, it's not cheese" << endl;
}
return 0;
}
The ==
is different to the =
we used when defining our variable. ==
compares two values (or variables). If they are the same, then the condition is true and the code runs. Otherwise, the condition is false and it moves on to the next else if
or else
.
Note: You can have as many else if
‘s statements chained together as you want.
Repeating bits of code
Sometimes you might want to do something a number of times. C++ has two kinds of loops, each with a different purpose. I’ll cover both here.
The while
loop
In C++, a while loop will repeat until a condition is not met. It works in a similar way to an if
statement. Here’s the syntax:
while (condition) {
(this code will run until your condition is not met)
}
Here’s an example:
#include <iostream>
using namespace std;
int main() {
string userInput;
while (userInput != "yes") {
cout << "Do you like cheese?" << endl;
getline(cin, userInput);
}
cout << "Glad to know!" << endl;
return 0;
}
What this code does:
- Creates a
string
variable calleduserInput
- Starts a loop, which will end when
userInput
is “yes” - Asks the user whether they like cheese
- If they don’t say yes, it’ll ask them again.
- If they say yes, then the program will finish.
Note: !=
means “is not equal to”.
Another note: If you accidentally create a loop that goes for forever, press Ctrl+C. This will stop your program from running in case of emergency. This works on most programs in the terminal.
The for
loop
In C++, for
loops are a bit more complicated. They take 3 arguments. Here’s the syntax:
for (variable; condition; code;) {
(code will be run here)
}
I’ll explain what you need to put in each part.
Variable
for
loops get you to define a variable which you usually change using the for
statement. Usually it’s an int
. The definition looks exactly like a normal variable declaration. It’s best practice to call this variable i
, or j
if you’ve got a nested loop (a loop inside a loop).
Condition
The condition checks for something, like the while
or if
statements. You can write it exactly like if you’d write it in a while
statement. The condition usually compares the variable you created to something else in your code.
Code
This is a small line of code that runs every iteration of your loop. Usually it’s used to change the variable you defined.
Here’s an example program which will count to 10:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 11; i ++) {
cout << i << endl;
}
return 0;
}
Lists
In C++, there are many types of lists you can use. There are two that I will be covering in this article: arrays and vectors. Arrays are lists with a constant size, whereas vectors are lists which can change size.
Arrays are lists with a constant size. Create one with the following syntax:
type name[length] = [item1, item2];
Arrays are created in a very similar way to normal variables. Here’s an example of an array:
int favouriteNumbers[5] = [7, 12, 500, 3, 27];
Note how I have 5 numbers in my array, and I have set my length as 5.
If you want to access or change an element in an array, you can do that like this:
int favouriteNumbers[5] = [7, 12, 500, 3, 27];
// Output an array element
cout << favouriteNumbers[2] << endl;
// Change an array element
favouriteNumbers[3] = 6;
Note: Lists in C++ use “zero indexing”, which means the first item in the list will be at index 0. The second will be at 1, the third will be at 2, and so on…
Vectors
Vectors work mostly in the same way as arrays, except new elements can be added. Vectors aren’t included as part of the iostream
library, so you’ll need to import the vector
library. This is done by adding at the top of your file:
#include <vector>
Once you’ve done that, the syntax to define a vector is this:
vector<type> name = {item1, item2};
This differs from arrays as you have to:
- Declare that you’re using a vector
- Add your type in the spiky brackets
- You don’t need to provide a length inside the square brackets
Here’s an example:
vector<string> myQuotes = {"I like C++", "Down with Rust!", "It's coding time"};
You can access a vector in the same way you can access an array, but you can also add elements to a vector. The quickest and easiest way to do so is using the push_front
and push_back
keywords, like this:
vector<string> myQuotes = {"I like C++", "Down with Rust!", "It's coding time"};
// Add something to the end of the vector
myQuotes.push_back("Jazz is cool music");
// Add something to the start of the vector
myQuotes.push_front("Cheese is delicious");
There are many more features with vectors you can use, but those are out of scope for this tutorial.
Functions
Functions in C++ are bits of code you can reuse. Here’s how you create a function:
type name(arguments) {
(your code goes here)
return (value);
}
Here’s what everything means:
- Type: This is one of the same types that you set a variable (like an
int
,string
, orvector
). - Name: Give your function a name
- Arguments: Here’s where your code can take in variables from other functions. You can’t access variables from your main function unless you pass them in as arguments. You need to specify a type and a name for the variables being passed in.
- return value: At the end of your function, you need to return something and go back to the main function. What you return needs to be of the same type you specified at the start of the function.
Tip: If your function doesn’t need to return anything, use the void
type.
Here’s an example of how you write and use a function:
#include <iostream>
using namespace std;
string compare(string first, string second) {
return first + " is better than " second;
}
int main() {
cout << compare("cheese", "bread") << endl;
return 0;
}
Notice how I used the function like a variable.
Sidenote: You can put whatever code you want in your functions, it doesn’t just have to be a return statement
Other sidenote: Make sure you surround your function arguments with brackets
Another sidenote: return
doesn’t need brackets around it
Further reading
This tutorial is already long enough, so I’m going to end it here. I hope you’ve enjoyed learning the basics! In a future article I may cover using files, classes, more types, and pointers, but you’ve got the basics down pat!
Here are some more resources to learn more about C++:
W3Schools: This is a very good website which I learned a lot from. https://www.w3schools.com/cpp/default.asp
StackOverflow: People ask questions here about their issues with coding. There’s a lot of information here. If you can’t find your issue, you can ask the community. https://stackoverflow.com/questions/tagged/c%2B%2B
Your Favourite AI Chatbot: AI chatbots know a lot about coding. You can ask them your question and they’ll tell you what’s going wrong. It’s usually better to find what someone’s done on the internet first, but they’re there if you need help for an odd issue. Be wary, as they can make mistakes. Ensure you tell the AI exactly what you want.
In future, I may write more posts about C++. Stay tuned, and have a good day!