An easy-to-understand diagram showing what a variable is. It's simple purpose is to hold your information and data, and can be easily swapped out at will! (Learn Programming Language Step By Step, 2012) |
There are a variety of data types for Variables, ranging from Boolean (true or false statements), to Integers (a number). Here's a table below as provided by (cplusplus, 2015) showing all of the data types for C++ and their functions:
Group | Type names* | Notes on size / precision |
---|---|---|
Character types | char | Exactly one byte in size. At least 8 bits. |
char16_t | Not smaller than char . At least 16 bits. | |
char32_t | Not smaller than char16_t . At least 32 bits. | |
wchar_t | Can represent the largest supported character set. | |
Integer types (signed) | signed char | Same size as char . At least 8 bits. |
signed short int | Not smaller than char . At least 16 bits. | |
signed int | Not smaller than short . At least 16 bits. | |
signed long int | Not smaller than int . At least 32 bits. | |
signed long long int | Not smaller than long . At least 64 bits. | |
Integer types (unsigned) | unsigned char | (same size as their signed counterparts) |
unsigned short int | ||
unsigned int | ||
unsigned long int | ||
unsigned long long int | ||
Floating-point types | float | |
double | Precision not less than float | |
long double | Precision not less than double | |
Boolean type | bool | |
Void type | void | no storage |
Null pointer | decltype(nullptr) |
It means the user should always pick the right Variable type for the job since they all have their uses for different scenarios. For example; if a game wanted the player character to open a door when they move near it, the Boolean Variable in the door for whether or not it's closed should be set to true, but when they move within range of its hitbox the Variable will then change to false, and thus he door will open. This will be until the player has left the range of the hitbox and aren't constantly colliding with it anymore, changing the value to true again and closing the door.
Each type has a specific purpose and aren't really interchangeable, which is why you must use certain ones. If you're counting a score in a game and keeping track of a specific number that's constantly changing as the player progresses, an int datatype is compulsory to use as "The int keyword denotes an integral type that stores values according to the size and range shown in the following table." (Msdn.microsoft a, 2015) So it would be unwise to use a float datatype instead, since they serve a different purpose and the code simply wouldn't work, because: "The float keyword signifies a simple type that stores 32-bit floating-point values. The following table shows the precision and approximate range for the float type". (Msdn.microsoft b, 2015)
No comments:
Post a Comment