Programming Fundamentals · Lesson 10
C++ variables and data types
Declare and initialise variables, choose suitable types, and display a small learner profile.
A variable gives a name to stored data. In C++, a declaration specifies its type and name. For example, int lessonsCompleted{2}; creates an integer named lessonsCompleted and starts it at 2. Initialising a variable gives it a known value before you use it.
Use int for whole numbers, double for approximate decimal values, char for a single character, and bool for true or false. std::string stores text and needs the <string> header. It is a standard-library type, rather than a built-in type.
Names are case-sensitive. Use meaningful names such as lessonsCompleted; avoid spaces, reserved words, or starting a name with a digit. camelCase is one common convention, used here to make several words readable.
A variable keeps its declared type when its value changes. After int lessonsCompleted{2};, the assignment lessonsCompleted = 3; stores a new integer. Declare a value const when it should not change: const int targetLessons{10};.
Read the example
#include <iostream>
#include <string>
int main() {
std::string learner{"Amina"};
int lessonsCompleted{2};
double studyHours{1.5};
char group{'A'};
bool readyToLearn{true};
const int targetLessons{10};
std::cout << "Learner: " << learner << '\n';
std::cout << "Lessons: " << lessonsCompleted << '\n';
std::cout << "Hours: " << studyHours << '\n';
std::cout << "Group: " << group << '\n';
std::cout << std::boolalpha << "Ready: " << readyToLearn << '\n';
std::cout << "Target: " << targetLessons << '\n';
return 0;
}
Learner: Amina Lessons: 2 Hours: 1.5 Group: A Ready: true Target: 10
The braces initialise each variable. Use single quotes for the char 'A' and double quotes for the string "Amina". std::boolalpha displays a bool as true or false; the default numeric display would show 1 or 0.
Brace initialisation helps detect narrowing: int hours{1.5}; is rejected because the fraction would be lost. Use double hours{1.5}; instead. Floating-point numbers approximate many decimal fractions, so exact equality is not always appropriate.
Built-in types have limited ranges. Those ranges can vary by implementation; do not assume int is always the same size. The small whole numbers used in this lesson fit comfortably.
The const declaration prevents assigning a new value to targetLessons. Try changing lessonsCompleted after its declaration, then printing it again. Do not read an uninitialised local integer.
Try it yourself
Create a learner profile for Bilal with 3 completed lessons, 2.5 study hours, group B, and readyToLearn set to false. Display the five values. Then change lessonsCompleted to 4 and display the updated count.
Show a worked solution
#include <iostream>
#include <string>
int main() {
std::string learner{"Bilal"};
int lessonsCompleted{3};
double studyHours{2.5};
char group{'B'};
bool readyToLearn{false};
std::cout << learner << '\n' << lessonsCompleted << '\n';
std::cout << studyHours << '\n' << group << '\n';
std::cout << std::boolalpha << readyToLearn << '\n';
lessonsCompleted = 4;
std::cout << "Updated lessons: " << lessonsCompleted << '\n';
return 0;
}
Further reading
Check your understanding
Your lesson quiz
Answer all five questions. Score at least 4 out of 5 (80%) to pass. You can retry as often as you like.
Quiz progress is saved in this browser. View certificates →
Learn together
Was this lesson helpful?
Comments and reactions are not open yet.
Join the conversation
Ask a question or share what you learned. Your display name and comment will be public after review. Please leave out private information. How feedback is stored