Data Types in C++

In C++, all the variables must be declared before use. A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.There are many different types of datas. Variables are classified according to their data type, which determines the kind of information that may be stored in them.

Data types in C++ are categorised in three groups: Built-in (Primitive), User-defined and Derived.

Built-in (Primitive) Data Types

In the realm of numeric information, for example, there are whole numbers and fractional numbers. There are negative numbers and positive numbers.Then there is textual information. Names and address, for instance, are stored as groups of characters. When you write a program, you must determine what types of information it will be likely to encounter.

Although C++ offers many data types, in the very broadest sense there are only two: numeric and character. Numeric dara types are broken into two additional categories: integer and floating point. Integers are whole numbers like 12, l57, -34, and 2. Floating-point numbers have a decimal point, like 23.7, 189.0231, and 0.987.

Integer Data Types

Integer variables can only hold whole numbers.

Integer Example


integerExample.cpp

NOTE:
  1. The data type sizes and ranges shown in Table 2-6 are typical on many systems. Depending on your operating system, the sizes and ranges may be different.
  2. Literals: Any constant value written in a program.
  3. The sizeof operator will report the number of bytes of memory used by any data type or variable.


Integer Examples

4 Bits
Unsigned Integer Signed Integer
Postive Integer Positive Integer Negative Integer
Binary Decimal Binary Binary Decimal Decimal
0000 0 0000 0 1000 -8
0001 1 0001 1 1001 -7
0010 2 0010 2 1010 -6
0011 3 0011 3 1011 -5
0100 4 0100 4 1100 -4
0101 5 0101 5 1101 -3
0110 6 0110 6 1110 -2
0111 7 0111 7 1111 -1
1000 8 0 indicates positive 1 indicates negative
1001 9
1010 10
1011 11
1100 12
1101 13
1110 14
1111 15


Because data types do have a minimum and a maximum range, you cannot represent or store every possible number in variables.

Overflow

Overflow is the situation where you try to store a number that exceeds the maximum range for the data type. When you try to store too large of a positive or negative number, the binary representation of the number is corrupted and you get a meaningless or erroneous result.

Underflow

Underflow is the situation where you try to store a number that exceeds the minimum range for the data type. When you try to store too small of a positive or negative number, the binary representation of the number is corrupted and you get a meaningless or erroneous result.

Floating-Point Data Type

Floating-point data types are used to define variables that can hold real numbers. In C++, there are three data types that can represent floating-point numbers.
  1. float
  2. double
  3. long double

Floating-Point Data Example


floatingExample.cpp
NOTE:
  1. There are no unsigned floating-point data types. On all machines, variables of the float, double, and long double data types can store positive or negative numbers.
  2. Computers typically use E notation to represent floating-point values. In E notation, the number 47,281.97 would be 4.728197E4. The part of the number before the E is the mantissa, and the part after rhe E is the power of 10. When a floating-point number is stored in memory, it is stored as the mantissa and the power of 10.

The Char Data Type

The char data type is used to store individual characters. A varaible of the char data type can hold one character at a time. Although the char data type is used for storing characters, it is actually an integer data type that typically uses 1 byte of memory. (The size is system dependent. On some systems, the char data type is larger than 1 byte.)

The reason an integer data type is used to store characters is because characters are internally represented by numbers. Each character is assigned a unique number. The most commonly used method for encoding characters is ASCII, which stands for the American Standard Code for Information Interchange.
When a character is srored in memory, it is actually the numeric code that is stored. When the computer is instructed to print the value on the screen, it displays the character that corresponds with the numeric code.

The Char Data Example


charExample.cpp
NOTE:
  1. In C++, character literals are encloded in single quotation marks.
  2. ASCII Table

The bool Data Type

Expressions that have a true or false value are called Boolean expressions. Boolean variables are set to either true or false

The bool Data Example


boolExample.cpp

NOTE:
  1. The value true is represented in memory by the number 1, and false is represented by 0.
  2. Boolean values are useful for evaluating conditions that are either true or false.
  3. When the boolalpha format flag is set, bool values are inserted/extracted by their textual representation: either true or false , instead of integral values.


Declaring Variables With the auto Key Word

* C++ 11 introduces an alternative way to define variables, using the auto key word and an initialization value. Here is an example:

       auto amount = 100; ← int

* The auto key word tells the compiler to determine the variable's data type from the initialization value:

       auto interestRate = 12.0; ← double

       auto stockCode = 'D'; ← char

      auto customerNum = 459L; ← long