Your Ad Here

C++ DATA TYPES


In addition to specifying a name for a variable, you also need to specify a particular data type
for that variable. A variable’s data type dictates the amount of memory that is allocated for
the variable, the type of data that you can store in the variable, and the types of operations that
can be performed using the variable. There are many different kinds of data types, but in this
book we will focus on the most basic kind of data types, known as primitive data types.
There are five primitive data types in C++: int, float, double, char, and bool. Some of
these data types (such as int, double, and float) are used for variables that will store
numeric values, and are referred to as numeric data types. The others have specialized purposes.
For example, the bool data type is used to store a value of either true or false and the
char data type is used to store a single character.
You will not use all of C++’s primitive data types in the programs you write in this book.
Instead, you will focus on two of the numeric data types (int and double). The int data
type is used for values that are whole numbers. For example, you could use a variable with
the data type int to store someone’s age (for example, 25) or the number of students in a
class (for example, 35). A variable of the int data type consists of 32 bits (4 bytes) of space
in memory. You use the data type double to store a floating-point value (that is, a fractional
value), such as the price of an item in dollars and cents (2.95) or a measurement in
feet or inches (2.5). A variable of the double data type consists of 64 bits (8 bytes) of space
in memory. You will learn about using other data types as you continue to learn more about
C++ in subsequent courses.
The int and double data types will be adequate for all the numeric variables you will use in
this book. But what about when you need to store a group of characters (such as a person’s
name) in a variable? In programming, we refer to a group of one or more characters as a
string. An example of a string is the last name “Wallace” or a product type such as a “desk”.
There is no primitive data type in C++ for storing strings; instead, they are stored in an object
known as a string object. In addition to working with the int and double data types in this
book, you will also work with strings.

0 comments:

Post a Comment

Popular Posts

Recent posts