Lesson 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Module 4 - Variables, Data Types, and Constant

At the end of the lesson, you should be able to:


1. discuss variable, data types and constants.
2. select appropriate data type, and initial value for a memory location
3. write a C++ statement declaration

Variables and Data Types


Variables are important for storing information. For instance, if you wish to multiply two numbers, then you need two variables that
can stores these two values and then, another variable for storing the product. Each variable has a type and the types determines
the number of memory locations in the physical space the need to be reserved for that variable.

C++ Variables
In programming, a variable is a container (storage area) to hold data.

To indicate the storage area, each variable should be given a unique name (identifier). For example,

Here, age is a variable of the int data type, and we have assigned an integer value 14 to it.
Note: The int data type suggests that the variable can only hold integers. Similarly, we can use the double data type if we have to
store decimals and exponentials.
We will learn about all the data types in detail in the next tutorial.
The value of a variable can be changed, hence the name variable.

Rules for naming a variable


A variable name can only have alphabets, numbers, and the underscore _.
A variable name cannot begin with a number.
It is a preferred practice to begin variable names with a lowercase character. For example, name is preferable to Name.
A variable name cannot be a keyword. For example, int is a keyword that is used to denote integers.
A variable name can start with an underscore. However, it's not considered a good practice.
Note: We should try to give meaningful names to variables. For example, first_name is a better variable name than fn.
C++ Literals

Literals are data used for representing fixed values. They can be used directly in the code. For example: 1, 2.5, 'c' etc.
Here, 1, 2.5 and 'c' are literals. Why? You cannot assign different values to these terms.
Here's a list of different literals in C++ programming.
1. Integers
An integer is a numeric literal(associated with numbers) without any fractional or exponential part. There are three types
of integer literals in C programming:
• decimal (base 10)
• octal (base 8)
• hexadecimal (base 16)

2. Floating-point Literals
A floating-point literal is a numeric literal that has either a fractional form or an exponent form. For example:
• -2.0
• 0.0000234
• -0.22E-5 Note: E-5 = 10-5

3. Characters
A character literal is created by enclosing a single character inside single quotation marks. For
example: 'a', 'm', 'F', '2', '}' etc.

4. Escape Sequences
Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C++ programming. For
example, newline (enter), tab, question mark, etc.
In order to use these characters, escape sequences are used.
5. String Literals
A string literal is a sequence of characters enclosed in double-quote marks. For example:

C++ Constants
In C++, we can create variables whose value cannot be changed. For that, we use the const keyword. Here's an
example:

Here, we have used the keyword const to declare a constant named LIGHT_SPEED. If we try to change the value
of LIGHT_SPEED, we will get an error.
A constant can also be created using the #define preprocessor directive. We will learn about it in detail in the C++
Macros tutorial.

C++ Data Types


In C++, data types are declarations for variables. This determines the type and size of data associated with variables.
For example,
Here, age is a variable of type int. Meaning, the variable can only store integers of either 2 or 4 bytes.

C++ Fundamental Data Types


The table below shows the fundamental data types, their meaning, and their sizes (in bytes):

1. C++ int
The int keyword is used to indicate integers. Its size is usually 4 bytes. Meaning, it can store values from -2147483648
to 2147483647.
For example,

2. C++ float and double


float and double are used to store floating-point numbers (decimals and exponentials).
The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two times the precision of float.
For example,
3. C++ char
Keyword char is used for characters. Its size is 1 byte. Characters in C++ are enclosed inside single quotes ' '.
For example,

4. C++ wchar_t
Wide character wchar_t is similar to the char data type, except its size is 2 bytes instead of 1. It is used to represent
characters that require more memory to represent them than a single char.
For example,

Note: There are also two other fixed-size character types char16_t and char32_t introduced in C++11.

5. C++ bool
The bool data type has one of two possible values: true or false.
Booleans are used in conditional statements and loops (which we will learn in later chapters).
For example,
6. C++ void
The void keyword indicates an absence of data. It means "nothing" or "no value". We will use void when we learn about
functions and pointers.
Note: We cannot declare variables of the void type.

C++ Data Types


As explained in the Variables chapter, a variable in C++ must be a specified data type:
Example
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String

Basic Data Types


The data type specifies the size and type of information the variable will store:

DataType Size Description


boolean 1 byte Stores true or false values
char 1 byte Stores a single character/letter/number, or ASCII values
int 2 or 4 bytes Stores whole numbers, without decimals
float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient
for
storing 6-7 decimal digits
double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient
for
storing 15 decimal digits

C++ Numeric Data Types


Numeric Types
Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when you
need a floating point number (with decimals), like 9.99 or 3.14515.
FLOAT VS. DOUBLE
The precision of a floating point value indicates how many digits the value can have after the decimal point. The
precision of float is only six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore
it is safer to use double for most calculations.

Scientific Numbers
A floating point number can also be a scientific number with an "e" to indicate the power of 10:
Example
#include <iostream>
using namespace std;
int main () {
float f1 = 35e3;
double d1 = 12E4;
cout << f1 << "\n";
cout << d1;
return 0;
}

Boolean Types
A boolean data type is declared with the bool keyword and can only take the values true or false.
When the value is returned, true = 1 and false = 0.
Example
#include <iostream>
using namespace std;

int main() {
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun << "\n";
cout << isFishTasty;
return 0;
}
C++ Character Data Types
Character Types
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':
Example:

#include <iostream>
using namespace std;
int main () {
char a = 65, b = 66, c = 67;
cout << a;
cout << b;
cout << c;
return 0;
}

C++ String Data Types


String Types
The string type is used to store a sequence of characters (text). This is not a built-in type, but it behaves like one in its
most basic usage. String values must be surrounded by double quotes:
Example:
#include <iostream>
#include <string>
using namespace std;

int main() {
string greeting = "Hello, Merry Christmas";
cout << greeting;
return 0;
}

You might also like