⚙️Tutorial: C++ Part 1

Welcome to this introductory tutorial on C/C++! This tutorial is designed to give you a hands-on start with some of the core concepts of the C of C++, from variables and data types to the use of C- and C++-style strings.
Work through the 10 exercises below. Each question includes a sample solution and an explanation in the associated video. Please try these first before watching the video.
Good luck!
Question 1. Preparation and Hello World.
Section titled “Question 1. Preparation and Hello World.”VS Code Project Setup
Section titled “VS Code Project Setup”This section will guide you through setting up your first C++ project in Visual Studio Code. We will create a simple program that displays a message, compile it, and run it.
Step 1: Create a Project Folder Start by creating a new folder on your computer to hold all the files for this tutorial. You can name it EEN1097/Tutorial1.
Step 2: Open VS Code and Install Extensions Open Visual Studio Code. Go to File > Open Folder… and select the folder you just created.
Next, you need to install the C/C++ extensions.
- Click on the Extensions icon on the left-hand sidebar (or press
Ctrl+Shift+X). - In the search bar, type “C/C++ Extension Pack”.
- Find the extension pack published by Microsoft and click Install. This pack includes a debugger and other useful tools.
Step 3: Create the Source File On the left-hand side of the VS Code window, in the file explorer pane, click the “New File” icon. Name the new file main.cpp. The .cpp extension tells VS Code and your compiler that this is a C++ source code file.
Step 4: Write the Code Copy the following “Hello, EEN1097!” program into your new main.cpp file. This is the code we will use to test your setup.
#include<iostream>using namespace std;
int main(){ cout << "Hello, EEN1097!" << ***change*** "\n"; ***change*** return 0;}Step 5: Run Your Code You can now compile and run your code. The simplest way for a beginner is to use the integrated “Run” button.
- Click the
main.cppfile to make sure it’s the active file. - Click the Run and Debug icon on the left-hand sidebar.
- Click the green “Run and Debug” button at the top of the pane. VS Code will automatically compile and run your code. You should see “Hello, EEN1097!” printed in the integrated Terminal at the bottom of the screen.
If this works as in the output below, you are all set for the rest of the tutorial!
Hello, EEN1097!Explanation
#include <iostream>is a preprocessor directive that imports the necessary library for input and output operations, such as printing to the screen.using namespace std;imports the standard namespace, allowing you to usecoutwithout thestd::prefix.int main() { ... }is the entry point for all C and C++ programs.cout << "Hello, EEN1097!" << "\n";change (updated to match modern practice) change sends the string"Hello, EEN1097!"to the standard output stream. The"\n"character creates a new line. change (Prefer\nfor better performance as it avoids unnecessary buffer flushing). changereturn 0;returns an integer value to the calling process, signifying that the program has finished successfully.
Question 2: Declaring and Initialising Variables
Section titled “Question 2: Declaring and Initialising Variables”A variable is a data item stored in a reserved block of memory. Write a program that declares and initialises a variable of each of the following types: int, float, double, char, and bool using a suitable initial value. Print the value of each variable to the console.
Question 3: Variable Assignment and Operators
Section titled “Question 3: Variable Assignment and Operators”Write a program that declares an integer variable x and initialises it to 5. Then, perform the following operations:
- Assign a new value of
10tox. - Increment
xby1using the pre-increment operator. - Add
5to the value ofxusing a compound assignment operator. - Print the final value of
x.
Question 4: Automatic Type Conversion
Section titled “Question 4: Automatic Type Conversion”C++ has certain automatic conversion rules for basic types. Write a program that demonstrates this behaviour by:
- Declaring an
int xand assigning it the value6.73. - Declaring an
int yand achar cinitialised to'w'. - Assigning the
char cto theint y. - Printing the values of
xandyto the console.
Question 5: The const and volatile Keywords
Section titled “Question 5: The const and volatile Keywords”The const keyword prevents a variable’s value from being changed, which can be useful in embedded applications for storing data in ROM. The volatile keyword tells the compiler not to optimise a variable, as its value can change outside the program’s control.
Task:
- Define a
const floatto store the value of Pi. Demonstrate what happens if you attempt to modify it. \ - Simulate a hardware register by declaring a
volatile intcalledstatusRegisterinitialised to0. Write a loop that repeatedly checksstatusRegisteruntil it becomes non-zero. \ - Inside your program, mimic an “external” change to
statusRegister(e.g. by assigning to it from another part of your code after a delay). Explain whyvolatileis required for the loop to work correctly.
Question 6: Standard Input (cin)
Section titled “Question 6: Standard Input (cin)”The cin object is used for reading information from the standard input stream, which is typically the keyboard. Write a program that asks the user for their name, reads the name from the console, and then prints a personalised greeting.
Question 7: Functions (Pass by Value)
Section titled “Question 7: Functions (Pass by Value)”Functions, or methods, are used to group code that is needed in multiple places. When you pass an argument by value, a copy of the variable is sent to the function, and any changes made inside the function do not affect the original variable.
Write a program that has a function addFive() that takes an integer, adds 5 to it, and returns the result. Call this function from main() and print the result. change Note: For large types that shouldn’t be modified, ‘Pass by Const Reference’ is the modern standard for efficiency. change
Question 8: Functions (Pass by Reference)
Section titled “Question 8: Functions (Pass by Reference)”Passing by reference allows a function to modify the original variable. This is achieved by using the & symbol in the function parameter list.
Modify the addFive() function from the previous question to be a void function that takes an integer by reference and adds 5 to the original variable.
Question 9: C-style Strings
Section titled “Question 9: C-style Strings”C-style strings are character arrays terminated by a null character \0. They are handled using functions from the C standard library.
Write a program that declares two C-style strings, str1 and str2. Assign "Hello " to str1 and "World!" to str2. Use strcat() to concatenate them and strlen() to find the length of the resulting string. Print the concatenated string and its length. change Note: In modern C++, std::string is preferred for safety, and std::string_view (C++17) is used for efficient read-only access. change
Question 10: C++-style Strings
Section titled “Question 10: C++-style Strings”The standard C++ library provides a more powerful and safer way to handle strings with the std::string class.
Write a program that declares two std::string variables, s1 and s2. Assign “C++” to s1 and ” is powerful” to s2. Concatenate them using the + operator, and print the new string along with its length using the length() method.
Solutions
Section titled “Solutions”Here are the video solutions — please do not watch these solutions without having attempted the questions first. Please note that these solutions are somewhat warts and all in that I make errors and correct them live without edits.
Alternatively, you can view the video directly on YouTube: https://www.youtube.com/watch?v=qi5GrbZ9U0M
© 2026 Derek Molloy, Dublin City University. All rights reserved.