Table of Contents
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 char , int , float, do uble 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 Type||Memory (ByteS)||Minimum Value||
|Bool||1||Logical Value T/F||Logical Value T/F|
Primitive Data types in C++
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
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.
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.
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.
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.
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’.