Table of Contents
In this lesson, you will learn about Variables and different Data Types in C++ and their usages, characteristics, and best practices. Data Types are used when you want to declare a variable, depending on what you are trying to store in that variable. For example, If you are trying to hold a “Name” value in a variable, then the Data Type for your variable should be a string.
What is a variable in C++?
Variables are declared in a program to store information while the program is running. A variable is memory location to store a value, such as a name, date of birth, address, age, etc.
The compiler will reset a variable value to its default value when the program is terminated. For example, a variable
FirstName value is an empty string after the program execution is over. Thus, variables are only temporary elements to store information needed to run and execute a program the right way.
More like this:
Variable naming constraints in C++
The following are constraints when you declare a variable in C++:
- Variable names in C++ are case sensitive. For example,
FirstNameis not the same as
- A Variable name in C++ can range between 1 to 255 characters.
- A variable name must begin with a letter or an underscore; a variable in C++ cannot start with a number or any other special character.
- A variable in C++ can contain a number.
- A variable in C++ cannot contain spaces or special characters other than the underscore (_).
- You cannot use C++ Keywords or reserved as a variable name.
C++ variable naming best practice
Use meaningful names
When declaring a variable, use what makes it easier for a developer to understand what a variable is designated for. For example:
mySalary is a good example to store salary information. Whereas,
myS is bad.
Also, keep in mind that using one letter for a variable name is not a common practice unless you create a variable for an iterator, such as
k, which are commonly used among programmers as loops iterators.
Avoid using indexes in a set of variables, for example,
t3, which may create more difficulties in understanding the program. Indexes are ok if you have more than one value of the same things you are trying to store. For example:
char AddressLine1, AddressLine2;
is not considered to be a bad practice in variable naming.
Consistency in programming is an important indicator of good program design and clean code. Stay consistent as much as possible when naming variables or functions.
For example, using a camelCase or PascalCase to name your variables is a great idea. However, don’t mix the two cases in the same program or program scope.
int employeeSalary; string employeeName; char employeeDepartment; bool isRetired;
int EmployeeSalary; string EmployeeName; char EmployeeDepartment; bool IsRetired;
are good examples of consistency when naming variables.
However, mixing two or more styles when declaring variables will make it difficult to read.
int EmployeeSalary; string employeeName; char Employee_Department; bool ISRETIRED;
Use comments to describe your variable
Use the single-line comment to explain your variable in a few words if it is hard to recognize.
int medianValue ; // to store median value for two arrays
Group Variables together
In a long list of variables declaration, grouping related variables is an excellent practice to make your program easier to understand.
// Employee basic information string employeeFirstName; string employeeLastName; string employeeDOB; // Employee address information string employeeStreetAddress; string employeeCity; string employeeState; string employeeZipCode; // Employee contact information string employeePhone; string employeeEmail;
C++ Reserved Keywords
Reserved keywords, or identifiers, are used by the C++ compiler and thus cannot be used as variable names.
C++ Data Types
As mentioned earlier in this lesson, Data Types are used to identify the type of information that a variable will hold. For example, an int Data Type is to store numbers.
int numberOfDoors; // to store a number string LastName; // to store a text value
There are three Data Types categories in C++:
- Primitive Data Types
- Derived Data Types
- User-Defined Data Types
In this lesson, we will discuss the Primitive Data Type. The Derived and User-Defined Data Types will be addressed in detail later in this tutorial.
Primitive Data Types
Primitive Data Types are built-in types that are available for programmers in all C++ versions.
Data type modifiers in C++
A programmer can modify these types using modifiers to alter their capacity or switch between signed and unsigned values.
The following list describes the Data Types in C++, their sizes, and the value range.
|Data Type||Size (In Bytes)||Range|
|char||1||-128 to 127|
|unsigned char||1||0 to 255|
|short||2||-32,768 to 32,767|
|unsigned short||2||0 to 65,535|
|int||2||-32,768 to 32,767|
|unsigned int||2||0 to 65,535|
|long||4||-2,147,483,648 to 2,147,483,647|
|unsigned long||4||0 to 4,294,967,295|
|float||2||3.4e-038 to 3.4e+038|
|double||8||1.7e-308 to 1.7e+308|
|long double||10||1.7e-4932 to 1.7e+4932|
|bool||1||0 to 1|
|wchar_t||4||0 to 255|
bool or Boolean is used to store logical or Boolean value. For example, if you need a variable to store true/false, yes/no, or 0/1 type of values, use the Boolean Data Type to define it. The keyword
bool is used to define a Boolean variable. For Example:
bool isRetired = false;
char or character Data Type is used to store a single character. For example:
char flag ='A';
The char value is enclosed inside single quotes.
int or integer is used for numbers. The keyword to use when declaring a variable is
int. for Example:
int floorNumber = 15; int floorNumber, doorNumber;
C ++ float
float is used to store single-precision- point numbers. for example:
float perimeter = 6.39;
double is similar to float. The only difference is that float is a single-precision floating number ( 32 bit), while
double is a double-precision floating number ( 64 bit). For example:
double PI= 3.14159265359;
void indicates the lack of a Data Type. It’s used in functions and pointers and cannot be used to declare a variable.
wchar_t is the keyword to declare a Wide character variable. wchar_t is similar to char, except its size, which is 2 bytes. The wchar_t is mainly used to represent some characters that require more memory and size, such as representing Unicode values. For example
wchar_t w = L'Á';
Points to remember
- Variable names in C++ are case sensitive.
- A variable name must begin with a letter or an underscore.
- A variable in C++ cannot contain spaces
- C++ primitive Data Types are: bool, char, int, float, double, void, wchar_t