In C++, primitive data types are the built-in types that form the backbone of programming. Like a chemical compound, where different elements combine to form a large complex structure, in programming different data types combine to form complex algorithms and structures. Understanding them is pivotal in mastering C++. Here are some of the common data types:
int: Used for storing an integer. For example, if you are tracking the number of transactions in a blockchain, you might use
int
.float: Used for storing decimal numbers. For example, when you need to store the price of a stock,
float
can be useful.bool: This is a boolean type and can hold either
true
orfalse
. It's often used in logical operations or loops.char: Used for storing a single character. This could be part of a piece of string data, like the name of a company for a finance application.
You can declare a variable by specifying its type, followed by the variable name. For instance, int count;
declares a variable named count
of type int
. The variable can then be assigned a value using the assignment operator (=)
. For example: count = 10;
.

xxxxxxxxxx
using namespace std;
int main() {
int transactions = 100;
float stock_price = 200.50;
bool isProfit = true;
char company_name_initial = 'A';
cout << transactions << endl;
cout << stock_price << endl;
cout << isProfit << endl;
cout << company_name_initial << endl;
}
Are you sure you're getting this? Click the correct answer from the options.
What type of data would be best to store the average monthly temperature in a year within C++?
Click the option that best answers the question.
- int
- float
- bool
- char
We've learned that we can assign values to a variable, whose type we've declared. We can modify this value as per our requirement; hence, it is called a variable in namesake.
The syntax is [data type] [variable name] = [value];
such as int stockPriceToday = 150;
. The value of stockPriceToday
will remain 150
until it's changed manually in the code. Similarly, stockPriceToday = 155;
changes its value to 155
.

C++ also allows us to declare constants using const
keyword. A constant is similar to a variable, but its value remains unchanged throughout the execution of a program. It's used when you want to safeguard the value and ensure it's not getting modified by code. An example is const int stockPriceYesterday = 145;
. It cannot be modified later in the code.
Every variable and constant in C++ has a certain scope and lifetime which determines its accessibility and when it's deallocated from the memory. In the next screen, we will delve deeper into the scope and lifetime of variables and constants.
xxxxxxxxxx
using namespace std;
int main() {
int stockPriceToday = 150; // declaration of variable
cout << "Yesterday's Stock Price: " << stockPriceToday << endl;
stockPriceToday = 155; // modification of variable
cout << "Today's Stock Price: " << stockPriceToday << endl;
const int stockPriceYesterday = 145; // declaration of a constant
cout << "The stock price cannot be changed: " << stockPriceYesterday << endl;
return 0;
}
Let's test your knowledge. Fill in the missing part by typing it in.
A variable can be modified, while a ___ in C++ has a value that remains unchanged throughout the execution of a program.
Write the missing line below.
Based on what we learned about C++ primitive data types, variables and constants, let's look at an example that could apply to a seasoned software engineer's life.
Assume we are organizing a series of learning sessions on C++, where sessionsConducted
is an integer representing the number of sessions held till date. avgRating
is a float representing the average participant rating for these sessions. To mark the start of the sessions, we've defined a character type initialLetter
starting with 'A'. Also, isLearningCpp
is a boolean indicating whether the user is currently learning C++ or not.
The C++ code snippet provided shows how to declare these variables, assign values, and print the variables using cout
. After running this code, you'll see printed details about these defined variables on the console. This gives you a hands-on understanding of working with different data types and variables in C++.
xxxxxxxxxx
using namespace std;
int main() {
// Declaring integer type variable
int sessionsConducted = 50;
// Declaring float type variable
float avgRating = 4.7;
// Declaring char type variable
char initialLetter = 'A';
// Declaring boolean type variable
bool isLearningCpp = true;
// Printing the variables
cout << "Sessions conducted: " << sessionsConducted << endl;
cout << "Average rating: " << avgRating << endl;
cout << "Initial letter: " << initialLetter << endl;
cout << "Learning C++: " << isLearningCpp << endl;
return 0;
}
Are you sure you're getting this? Click the correct answer from the options.
Based on the below example, choose the correct output:
1int sessionsConducted = 5;
2float avgRating = 4.6;
3char initialLetter = 'A';
4bool isLearningCpp = true;
5
6cout << sessionsConducted << endl;
7cout << avgRating << endl;
8cout << initialLetter << endl;
9cout << boolalpha << isLearningCpp << endl;
Which of the following will be printed to the console?
Click the option that best answers the question.
- 5 4.6 A 1
- 5 4.6 A True
- 5 4.6 A true
- 5 4.6 65 true
As we draw this lesson to a close, we've navigated the landscape of Data Types and Variables in C++, understanding their significance in the realm of programming. We've explored different primitive types in C++, such as int
, float
, bool
, char
, etc., learned how to declare, assign values to variables, and work with constants.

Think of data types like the underlying building blocks or fundamental components in crafting software applications. They allow us to model real-world scenarios effectively within our programs, much like our "C++ Learning Sessions" example which can be viewed akin to a finance portfolio management system if we further extrapolate this concept.
Cementing these concepts early in our journey can enhance our capacity to grasp further complexities. Like in web development where choosing the right data types and the correct utilization of variables can make your code more efficient, they plays a similar role in C++ programming.
As we move forward in our C++ journey, you'll find these topics repeating in different contexts proving their widespread relevance. Understanding them well will not only strengthen your coding foundation but also provide a strong platform to understand more advanced concepts. Well done on completing this important lesson!
Let's test your knowledge. Fill in the missing part by typing it in.
Data types are essentially the ___ blocks when crafting software applications in C++.
Write the missing line below.