5.1: Integer Data Type
- Page ID
The integer data type has two meanings:
- The integer data type with its various modifiers that create different domains
- The integer family which also includes the Boolean and character data types
The integer data type basically represents whole numbers (no fractional parts). The integer values jump from one value to another. There is nothing between 6 and 7. It could be asked why not make all your numbers floating point which allow for fractional parts. The reason is twofold. First, some things in the real world are not fractional. A dog, even with only 3 legs, is still one (1) dog not ¾ of a dog. Second, integer data type is often used to control program flow by counting, thus the need for a data type that jumps from one value to another.
The integer data type has the same attributes and acts or behaves similarly in all programming languages. The most often used integer data type in C++ is the simple integer.
|C++ Reserved Word||int|
|Represent||Whole numbers (no fractional parts)|
|Size||Usually 4 bytes|
|Normal Signage||Signed (negative and positive values)|
|Domain (Values Allowed)||-2,147,483,648 to 2,147,483,648|
|C++ syntax rule||Do not start with a 0 (zero)|
|C++ syntax rule||No decimal point|
We have seen the table below in Lesson 4 - it summarizes the modified size and range of built-in integer when combined with the type modifiers. Notice that there are signed and unsigned data types. The unsigned types start at zero, and only contain posititve values, these types also have a higher range since they have an additional bit (since the sign bit is NOT used) to use to contain the value.
Within C++ there are various reserved words that can be used to modify the size or signage of an integer. They include: long, short, signed and unsigned. Signed is rarely used because integers are signed by default – you must specify unsigned if you want integers that are only positive. Possible combinations are:
|DATA TYPE||SIZE (IN BYTES)||VALUE RANGE|
|short int||2||-32,768 to 32,767|
|unsigned short int||2||0 to 65,535|
|unsigned int||4||0 to 4,294,967,295|
|int||4||-2,147,483,648 to 2,147,483,647|
|long int||4||-2,147,483,648 to 2,147,483,647|
|unsigned long int||4||0 to 4,294,967,295|
|long long int||8||-(2^63) to (2^63)-1|
|unsigned long long int||8||0 to 18,446,744,073,709,551,615|
The domain of each of the above data type options varies with the compiler being used and the computer. The domains vary because the byte size allocated to the data varies with the compiler and computer. This effect is known as being machine dependent. Additionally, there have been some size changes with upgrades to the language. In "C" the int data type was allocated 2 bytes of memory storage on an Intel compatible central processing unit (cpu) machine. In "C++" an int is allocated 4 bytes.
These variations of the integer data type are an annoyance in C++ for a beginning programmer. For a beginning programmer it is more important to understand the general attributes of the integer data type that apply to most programming languages.
- Machine Dependent
- An attribute of a programming language that changes depending on the computer's CPU.
"C++ Data Types" by Harsh Agarwal, Geeks for Geeks
"Integer Data Type" by Kenneth Leroy Busbee, (Download for free at http://cnx.org/contents/303800f3-07f...firstname.lastname@example.org) is licensed under CC BY 4.0