0% found this document useful (0 votes)
121 views

5 - Handling Primitive Data Types

This document provides an overview of handling primitive data types in C++. It discusses variables and data types, explaining that variables act as boxes that store values, which are restricted by the declared data type. There are two classifications of data types: by value (explicit vs implicit) and by construct (primitive vs referenced). Primitive data types in C++ include integers, floating points, Booleans, and characters. Integers can be regular, long, or short, and can be signed or unsigned. Floating points support decimal precision. Booleans represent true or false values. Characters represent single characters while strings are arrays of characters.

Uploaded by

Joey Lazarte
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

5 - Handling Primitive Data Types

This document provides an overview of handling primitive data types in C++. It discusses variables and data types, explaining that variables act as boxes that store values, which are restricted by the declared data type. There are two classifications of data types: by value (explicit vs implicit) and by construct (primitive vs referenced). Primitive data types in C++ include integers, floating points, Booleans, and characters. Integers can be regular, long, or short, and can be signed or unsigned. Floating points support decimal precision. Booleans represent true or false values. Characters represent single characters while strings are arrays of characters.

Uploaded by

Joey Lazarte
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter 5 – Handling Primitive Data Types

"Beyond a certain point, the whole universe becomes a continuous process of initiation."

-Robert Anton Wilson.

By this time, we are already in the formal part of our programming adventure. Since all
applications handle data, both computed and entered, we need to know how to properly
handle them and determine whether to store them for future use or simply use them once and
forget about them. This will be the focus of this module, handling data types.

At the end of this module, you will be able to:


1. Differentiate variables from data types
2. Identify the classifications of variables
3. Identify the classifications of data types
4. Identify the different data types available in C++
5. Argue the practical data type to use

Again, researching beyond the coverage of this module is highly encouraged to supplement your
understanding of the topics covered. And as always, think and see beyond the box.

So what are we waiting for? Let us continue our exploration of the world of Computer
Programming.

Recall

In the previous module, we identified the essential components of our work environment,
namely:
a) Operating System
b) Integrated Development Environment
c) Compilers
d) Version Control
In addition, we learned how to configure our work environment through step-by-step
guide, namely:
a) Creating Github Account
b) Creating Github Repository
c) Downloading Visual Studio Code
d) Installing Visual Studio Code
e) Downloading Minimalist GNU for Windows
f) Installing MinGW Installation Manager
g) Installing MinGW Components
h) Configuring Microsoft Windows Path
i) Downloading Project Templates for Visual Studio
j) Adding C++ Extension to Visual Studio Code
Lastly, we worked on our very first C++ application, displaying “Hello World!".

Introduction

Handling data is similar as handling where we store and retrieve them. In computers, the
data is stored and retrieved in the RAM. Programming languages make this possible
through variables. Think of variables as a box where we can store "valuables" in them. In
this case, the "valuables" we store within variables are values.

Handling Variables

We handle variables like we handle real-world boxes. When we take a box in real-world
that we plan to use as storage for our valuables, in Programming, we declare the variable.
When we place our valuables in the box, in Programming, we assign a value to the variable.
When this assignment is done for the first time, we are initializing the variable. When we
take our valuables out of the box, in Programming, we are retrieving its value.

There is a slight difference, however, then we take our valuables of the box
compared to when we retrieve the values of the variable. When we take our
valuables out of the box, we take them permanently. We do not have that
valuable anymore within the box after taking them out. In Programming,
however, when we retrieve the value of the variable, the value is still within
the variable. What we "take out" of the variable is just a copy of the value.
Variables

In programming languages, when talking about variables, we have the so- called explicit, implicit
and anonymous variables.

Explicit variables are variables that we declare, usually at the start of our application before
any processing is done within. They are made is such a way that developers know they will be
using a variable within the context of the application. Most programming languages have
explicit variables; they require that variables are to be declared before they are used.
Implicit variables are variables that are used right away without the need of declaring them.
They are referenced using a name. They are made in such a way that they are only allocated
some memory when called. They are usable within the context where they are initially used.
Most programming languages used in Web Development have implicit variables.
Anonymous variables are variables that are used right away without the need of declaring
them. They, however, are not referenced using a name. Only the computer itself know how to
call them. They are typically declared and destroyed after use. Anonymous variables are used
during computations

Major declarations are done as the first line of the "public void main()"
function for the explicit variables. Throughout the course, you will be using
anonymous variables without initially knowing you are using them.

In C++, we have explicit and anonymous variables.

Data Types

We use variables in par with data types. Data Types are the restriction we place to variables
that limit them to handle only our specified type of data. Think of this like we are segregating
our valuables to label boxes. Box 1, labeled as "Tommy's Box", will only contain the valuables of
Tommy. Box 2, labeled as "Furniture", will only contain the furniture, and nothing else like food.
Variables behaves in the same way like labeled boxes, they are limited to their declared data
type. We have two (2] classifications for data types:

a] By Value
b) By Construct
Classification by Value

When data types are classified by value, we have explicit and implicit data types.
Explicit Data Types are the data types that are declared and defined. For example, we
declared a string variable, therefore we can only store string values to that variable. We
declared an integer variable, therefore we can only store integer values to that variable.

Explicit Data Types are often called "Strong-typed" in some other contexts.
This is mainly because of how the values to be stored are treated. In
addition, being called “Strong-typed" suggests that there exists a bound
between the data types, by extension including the variables, and the value
stored.
Implicit Data Types are data types that are taken depending on how they are stored or
used. They are not defined which often causes ambiguity on how the values are to be handles.

Implicit Data Types are often called "Variant" type. They are especially used
in Web Development wherein the values, like those which come from the
users, are initially treated like strings before they are converted to what they
are actually.

In C++, we only have Explicit Data types.

Classification by Construct

Data Types can also be classified by construct. By construct in Programming means by how
they are constructed. It also means by what they are composed of.
Primitive Data Types are data types of singularity by nature. They contain only the
values they are supposed to contain. They do not have any other components like properties
and internal functions. They are like folders for paper. They are used only to contain papers
and nothing else like writing pens, chips and cars.

C Programming Language only have Primitive Data Types. They have


"structures" but not full-bodied "classes" which are the fundamental
implementation of Referenced Data types.
Referenced Data Types are data types that are objective by nature. They do not only
contain the values they are supposed to contain, but also have other components like
properties and functions. They contain variables within them. They are like car garage. They
can contain anything within them, not only the cars which they are made for. They can
contain fuel containers, workbenches and spare tires. In addition, they also serve different
functionalities like they enable automatic closing of garage doors, internal lightings, and
vents.

In C++, we have both Primitive and Referenced Data Types.

Primitive Data Types

Primitive Data Types in C++ are classifiable in integers, floating points and characters. This
classification is based on what specific values are allowable. Integers, floating points and
Booleans are numerical values, whereas characters are textual values.

Integers

Integers are values that are numerical in nature. They do not have decimal precisions. Some
examples of these are counting numbers including zero (0).Under Integers, we have the
regular integer, long and short data types. Table 1 and 2 show the minimum and the
maximum values allowable for the Integer Values, both signed and unsigned integer values.

In addition to these data types, we have a singed and unsigned identifiers for integer data
types. When we do not specify any of the two identifiers, C++ implicitly interprets them as if you
have placed a singed identifier.
Exceeding the set boundaries for the Integer Values results to what we call
"overflow". Causing overflows results to unexpected outcomes which
typically results for the application to terminate.

Floating-points

Floating Points are values that are numerical in nature. They also sport decimal precisions.
Some examples are temperature, body-mass index and height. Table 3 shows the minimum and
maximum values allowable for floating-point values.

Booleans

Boolean Values are values classifiable to either a true or false. These are numerical in
nature because C++ stores them as zero (0) or zero [1], internally. True value for booleans
are considered as non-zero positive integer. False value for booleans are considered as
zero. They are usually used as flags for a specific scenario. They use bool identifier.
Characters

Characters are the only primitive data type that is textual in nature. Regular characters can
only represent one (1] character per variable. Strings can be represented as a series of
characters. That is why, in programming languages such as C++, a string is declared as an
array of characters. Characters use char identifier, whereas Strings use char
variable[ jsyntax.

To declare Strings in C++, we indicate the maximum number of characters to be used in the
string. For example, if we will need to allocate 50 empty characters to contain a String, we
will declare it using "char x[50]". If we need 555 empty characters, we declare "char
x[555]".

Data Types in Action

Now that we have covered all the supported Primitive Data Types in C++, we need to
discern the importance of having different data types and when we will generally use them
in real-world applications.

Data Structures

First thing we need to know is how the computer in general, the Operating System and the CPU
in particular handle variables. When we declare a variable, the computer allocates a specific
empty and unused region in the RAM where to store the supposed value. How big the allocation
will be depends on the maximum value of the data type. This is the main reason why we have
varying lengths for our integer and floating points.
Now, remember that the computer essentially only understands binary, a series of 0's and l's. If
we take unsigned short integers as our example, the maximum value allowable for them is
65,535. In binary that is sixteen (16) l’s - 1111 1111 1111 1111. Hence the computer will allocate
sixteen sectors if we declare one unsigned short integers.
The same thing applies for [signed] short integers. However, the least significant bit (the left-
most bit), symbolizes the parity of the sign. For the value 32,767, we will have 0111 1111 1111
1111 in binary. For the value - 32,768, we will have 0111 1111 1111 1111 in binary.
A Character data type occupy 8-bits. The value differs on how the compiler sets the value of the
characters. For example, Compiler A can store the character "A" as 0000 0000, while Compiler B
can store the character "A" as 0100 0000.
Booleans occupy only 1-bit, either 0 or 1. For true, it is 1. For false, it is 0. When integers are
used in Boolean expressions, a 0-valued integer is interpreted as false and a positive-valued
integer is interpreted as true. Negative-valued integers are not represented in Booleans, as such
they typically result in application crashes or errors during compilation.

The course about "Data Structures" will discuss in detail about binaries,
parities and the like which are relevant on how data are stored within
computers. This includes the complex operations the computer perform on
floating-points.

Real-world Application

Now that we understand how Data Types work technically and theoretically, we need to discern
what the most applicable data types per scenario is. Since there are an unlimited number of
possible scenarios, we will only tackle the most common and general of them.

First is when we handle any numeric values, we know to choose only between integers and
floating-points. What determines which of the two will be used relies on the need to store the
decimal values. When we count the number of occurrences of a specific object, we can use
integer values. They mainly differ on how large the value could go. For example we need to
count the number of cars in the garage, we can use short. If we take the overall populations in
the world, we need to use long.
One of the Internet giants, Google, recently changed the data type of their
"Thumbs Up" variable from "int" to due to a short commotion that occurred
in the official Psy's Gangnam Style video in YouTube. This is because the limit
of int was reached forcing YouTube to change the data type to Int64 -
essentially the long data type in C++.

For values needing decimal precision, we use floating points. If we are simply storing the
temperature of a given specimen, we can use float. However, if we take monetary values, we
need to use doubles. This is mainly because monetary values typically reach millions and
billions.
If we only need to check the parity of an object or state, we only need to use the Boolean. If we
need to know whether the user is, biologically speaking, a male. We can use "bool isMale;”
rather than any other data types like the storage heavy string. This is because if the "isMale"
results to a false, we automatically know the user is a female. There is no gender in between of
the two biologically speaking.

In these scenarios, we mainly base our decision in the maximum number we anticipate the
values could become. We should never take the data types for granted because the computing
and storing capabilities of computer are limited. Incorrect usage of these resources can cause
not only the application to fail, but also the entire operating system.

Glossary

Variable: A temporary storage of value.

Explicit [Variables]: Variables that are declared and are assigned a name.
Implicit [Variables]: Variables that are not declared but are assigned a name.
Anonymous [Variables]: Variables that are not declared and are not assigned a name.

Data Type: A means to limit the types of values to be stored in a variable.


Strong-typed [Data Type]: see Explicit Variables.

Variant [Data Types]: Data types that can store any type of values.
Primitive [Data Types]: Data types that directly store the values within them and are
retrieved within their own parameters.
Referenced [Data Types]: Data types that references to different sectors in the memory to
store and retrieve values. Values stored in Referenced Data Types are not stored and retrieved
within the variable, but are stored and retrieved to another address the computer implicitly
assigned it.
Decimal Precision: Allows values to contain decimal point and numbers at the right of the
decimal point.
Integer Values [Data Types]: Data types that are numerical in nature and does not have
decimal precision.
Floating Point [Data Types]: Data types that are numerical in nature and do have decimal
precision.
Booleans [Data Types]: Data types that are either true or false, but not both, in value.
Characters [Data Types]: Data types that are textual in nature. They represent only one
character of the text.

You might also like