Our interactions (inputs and outputs) of a program are treated in many languages as a stream of bytes. These bytes represent data that can be interpreted as representing values that we understand. Additionally, within a program we process this data in various ways such as adding them up or sorting them. This data comes in different forms. Examples include: yourname which is a string of characters; your age which is usually an integer; or the amount of money in your pocket which is usually a value measured in dollars and cents (something with a fractional part). A major part of understanding how to design and code programs in centered in understanding the types of data that we want to manipulate and how to manipulate that data.
“A type defines a set of values and a set of operations that can be applied on those values. The set of values for each type is known as the domain for that type.”1 The four major families of data include:
- User Defined Data Types (Array,String, Pointers)
When we wish to store data in a C++ program, such as a whole number or a character, we have to tell the compiler which type of data we want to store. The data type will have characteristics such as the range of values that can be stored and the operations that can be performed on variables of that type.
Five (5) Fundamental (Built-in or Primitive or Basic) data types:
- 1. void- represents no type (type less) and usually used as a return value of a function. Data type void is the odd one out since we cannot define a variable of the type void.
- 2. char- represents any single character from keyboard. Every computer has a character set. A PC has ASCII character set. ASCII character set has 256 characters. Data type char represent any character from ASCII character set.
- 3. int- represents zero, positive and negative integer values (whole numbers).
- 4. float- represents zero, positive and negative floating point values (real numbers).
- 5. double- represents zero, positive and negative floating point values (real numbers). Data type double is similar to float but with better precision.
String: represents sequence characters enclosed within a pair of double quotes (“). String is not a fundamental type. String is a derived data type. Derived data types will be discussed in details later.
The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++. A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255). In addition, the computer can manipulate more complex data types that come from grouping several bytes, such as long numbers or non-integer numbers.
|char||Character or small integer.||1byte||signed: -128 to 127 unsigned: 0 to 255|
|short int(short)||Short Integer.||2bytes||signed: -32768 to 32767 unsigned: 0 to 65535|
|int||Integer.||4bytes||signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295|
|long int(long)||Long integer.||4bytes||signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295|
|bool||Boolean value. It can take one of two values: true or false.||1byte||true or false|
|float||Floating point number.||4bytes||+/- 3.4e +/- 38 (~7 digits)|
|double||Double precision floating point number.||8bytes||+/- 1.7e +/- 308 (~15 digits)|
|long double||Long double precision floating point number.||8bytes||+/- 1.7e +/- 308 (~15 digits)|
Type Modifier: Type modifiers are used to change default type of the built-in data types. Type modifiers supported by C++ are long, short, signed and unsigned. Table given below shows the use of type modifiers with the built-in data types.
Data type void and float does not support any type modifiers
Data type int supports all the four type modifiers
Data type char supports signed and unsigned
Data type double supports only long
Type modifiers are used with fundamental data types to create a variable. But if a variable is created using only type modifier then the default data type for the variable is int
The following table shows the variable type, how much memory it takes to store the value memory, and what is maximum and minimum vaue which can be stored in such type of variables.
|Type||Typical Bit Width||Typical Range|
|char||1byte||-127 to 127 or 0 to 255|
|unsigned char||1byte||0 to 255|
|signed char||1byte||-127 to 127|
|int||4bytes||-2147483648 to 2147483647|
|unsigned int||4bytes||0 to 4294967295|
|signed int||4bytes||-2147483648 to 2147483647|
|short int||2bytes||-32768 to 32767|
|unsigned short int||Range||0 to 65,535|
|signed short int||Range||-32768 to 32767|
|long int||4bytes||-2,147,483,647 to 2,147,483,647|
|signed long int||4bytes||same as long int|
|unsigned long int||4bytes||0 to 4,294,967,295|
|float||4bytes||+/- 3.4e +/- 38 (~7 digits)|
|double||8bytes||+/- 1.7e +/- 308 (~15 digits)|
|long double||8bytes||+/- 1.7e +/- 308 (~15 digits)|
The sizes of variables might be different from those shown in the above table, depending on the compiler and the computer you are using.
// sizeof() function to get size of various data types.
// endl which inserts a new-line character after every line
cout << “Size of char : ” << sizeof(char) << endl;
cout << “Size of int : ” << sizeof(int) << endl;
cout << “Size of short int : ” << sizeof(short int) << endl;
cout << “Size of long int : ” << sizeof(long int) << endl;
cout << “Size of float : ” << sizeof(float) << endl;
cout << “Size of double : ” << sizeof(double) << endl;
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8