Data types in C++

category: 

In computer programming, information is stored in a computer memory with different data types. We must know what is to be stored in a computer memory,whether it is a simple number, a letter or a very large number. As we also know, computer memory is organized in bytes, and for these variables with varying information a data type is associated. The minimum amount of memory in computer memory is a byte, that can store a small amount of data and managed easily. Every variable is declared with two entities, its type and its name. There are several data types available in C++. The basic built in data types are charint, float, double and bool. There is another data type void, which we will discuss some other time. C++ also allows user defined data types. One of them is classes, which we have discussed here.

Data types in C++
Data Type Memory (ByteS) Minimum Value

Maximum Value

Bool 1 Logical Value T/F Logical Value T/F
Char 1 -128 127
Unsigned Char 1 0 255
Short 2 -32768 32767
Unsigned Short 2 0 65535
int 2 -32768 32767
unsigned int 2 0 65535
Long 4 -2147483648 2147483647
unsigned long 4 0 4294967295
float 4 10-38 1038
double 8 10-308 10308
long double 10 10-4932 104932

 

Primitive Data types in C++

character

C++ offers a predefined data type that is one byte in size, which can hold exatly one character such as ‘a’ or ‘A’. To declare a variable of type char, we have

Char ch;

Suppose we want to store a character value ‘a’, in a char data type ch, it is enclosed within a single quote.

Ch = ‘a’;

Only a single character can be stored in a variable of type char. A comma must separate more than one variable in the same line. Using them in different lines, they must be preceded by the type.

integer

On most machines the size of int type is 2 bytes. C++ defines this type as consisting of the values ranging from -32768 to 32767. This range is for the small integer. If long integer is needed the type long or long int can be used. The range of long int is too big that is from -2147483648 to 2147483647, which occupies 4 bytes in memory.

Float

C++ defines the data type float as representing numbers that have fractional part. For example, 12.55 as opposed to integers which have no fractional part. Floating point variables can either be small or large. A variable with type float occupies 4 bytes in size and can hold numbers from 10-308 to 10—308 with about 15 digits of precision. There is a long double, also available, that can hold numbers from 10--4932 to 10-4932.

Bool

Unlike ‘C’, it is an additional data type for representing a Boolean value. A variable associated with a bool data type may be assigned an integer value 1 to the literal true or a value 0 to the literal false.

Additional Data types in C++

After well design when we wish to store data in a C++ program, the value is stored in variable the system memory. It is also important for the system to know the amount of storage. As discussed in the primitive data types that storing a whole number or a character does not occupy the same space in the computer memory. Each data type discussed below has some characteristics such as the range of storage values and the operations that can be performed on that specific data type associated with the variable. C++ data types are stored in different size of memory, depending on the size of the data type. The Char and int have unsigned versions like unsigned char, unsigned int and unsigned long. The use of unsigned changes the range. Let us take a look at unsigned int. It can hold numbers from 0 to 65,535 rather than -32,768 to 32767. The use of unsigned is quite frequent. The unsigned is called the modifier because we are modifying how the memory is used.

On some systems, short and int have the same size but different on others. Test the size of int and short on the system before they can be used or check the manual. A variable with type int and short can be preceded either by plus sign (+) or minus sign (-). Integer numbers are those, which contain no decimal point. Floating point numbers are different from integers because they are stored in two parts rather than one part. One part is called the Mantissa, and the other part is called the exponent. The mantissa is the value and the exponent si the power to which it is raised. The floating point numbers can also be represented by exponential notation or scientific notation. The exponential notation is number containing a decimal point followed by the letter E and an integer constant. Both forms may be preceded by an optional, ‘+’ or ‘_’ sign.

For example

+3.1476

-0.967

-0.628E4

0.527E2

The value after ‘E’ is the power of 10 by which the first value is to be multiplied. For example

+0.527E2 is equivalent to 52.7

-0.628E7 is equivalent to -6280000

1.0E-4 is equivalent to .0001

This notation is constantly used to express large numbers. The range of values a data type can store varies from system to system. It is 100% dependent on the system platform you are using. For example int, it is 4 bytes in 32-bits computer. On the other hand, it is 2 bytes on 16-bits computers. We can say that it is dependent on the compiler and the operating system. A character data type can also accommodate a numerical value. For example 65, this is ASCII value of ‘A’.