In C++ there are different types of variables. These variable types are known as data types, and they define what information a variable can contain.
Most (but not all) of the basic data types are built into the <iostream>
class, and therefore the <iostream>
class will need to be included into your code before using those data types:
#include <iostream>
#include <limits>
The following reserved keywords are used to define different types of commonly used variables:
int
: integers (whole numbers), without decimals. For example: 1234 or -1234float
: single-precision decimal number. For example: 98.6fdouble
: double-precision decimal number number. For example: 3.14159 or -53.289334char
: single characters, such as 'a' or 'B'. When assigning the Char value to the Char variable, its value should be surrounded by single quotesstring
: text containing zero or more characters. For example, "Hello World". When assigning the String value to the String variable, its value should be surrounded by double quotesbool
: boolean value with two states: true or falseThe following are examples of declaring and assigning values to some variables of different data types:
int myNum = 17 ; // Integer (whole number)
float myFloat = 98.6 ; // Floating Point Number (Single-Precision)
double myDouble = 3.14159 ; // Floating Point Number (Double-Precision)
char myChar = 'D' ; // Character
bool myBool = true ; // Boolean
string myText = "Hello World" ; // String
signed
data types mean that the value which a variable of that data type contains can be either positive or negative in value.unsigned
data types mean that the value which a variable of that data type contains are positive values only.All data types are signed
by default, and you do not need to do anything to explicitly specify that it is signed. You only need to specify if you want the variable of a data type to be unsigned
.
If you know that your instance will only use positive values (for example, in some for
loops), then you can use an unsigned variable, which allows you to use higher positive values than you can with normal signed variables.
unsigned
options.Specifying the data type when declaring a variable (as in the examples above) is an easy way to specify the size and type of information that variable will store.
The following table lists the size, range, macros, and descriptions of various commonly used data types:
Data Type | Size | Range | Macro/Limits | Description |
---|---|---|---|---|
int |
2 or 4 bytes | -2,147,483,648 to 2,147,483,647 | INT_MIN to INT_MAX |
Stores positive or negative whole numbers, without decimals (example: 42) |
unsigned int |
4 bytes | 0 to 4,294,967,295 | UINT_MAX |
Stores only positive whole numbers, without decimals |
float |
4 bytes | ~1.17549e-38 to ~3.40282e+38 | FLT_MIN to FLT_MAX |
Stores fractional numbers with ~6-7 digits of precision (example: 42.234567) |
double |
8 bytes | ~2.22507e-308 to ~1.79769e+308 | DBL_MIN to DBL_MAX |
Stores fractional numbers with ~15 digits of precision (example: 42.234567890123456) |
long double |
10 or more bytes | ~3.3621e-4932 to ~1.18973e+4932 | LDBL_MIN to LDBL_MAX |
Stores high precision fractional numbers; precision varies by compiler |
short |
2 bytes | -32,768 to 32,767 | SHRT_MIN to SHRT_MAX |
Stores smaller whole numbers; useful for memory-constrained applications |
unsigned short |
2 bytes | 0 to 65,535 | USHRT_MAX |
Stores only positive small whole numbers |
long |
4 or 8 bytes | -2,147,483,648 to 2,147,483,647 (32-bit) -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (64-bit) |
LONG_MIN to LONG_MAX |
Stores large whole numbers; size depends on system architecture |
unsigned long |
8 bytes | 0 to 18,446,744,073,709,551,615 | ULONG_MAX |
Stores large positive whole numbers |
long long |
8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | LLONG_MIN to LLONG_MAX |
Stores very large positive or negative whole numbers |
unsigned long long |
8 bytes | 0 to 18,446,744,073,709,551,615 | ULLONG_MAX |
Stores very large positive whole numbers |
char |
1 byte | -128 to 127 | CHAR_MIN to CHAR_MAX |
Stores a single character or ASCII value. Char values must be surrounded by single quotes. (example: 'A') |
unsigned char |
1 byte | 0 to 255 | UCHAR_MAX |
Stores a single character or ASCII value without negative values |
bool |
1 byte | - | Stores logical values: |
To access the value of or to use the macro range of a variables data type, you can use the <limits>
header and std::numeric_limits<T>::min()
/ max()
as shown below:
#include <iostream>
#include <limits>
int main() {
std::cout << "int range: "
<< std::numeric_limits<int >::min()
<< " to "
<< std::numeric_limits<int >::max()
<< std::endl;
}
When you declare a variable of a data type, then that variable will automatically be assigned and consume that data type's amount of memory (in bytes), no matter if that variable contains a value or not. Therefore, it is important to keep track of how many variables of which data types you create within your program, especially when dealing with Arrays, so that you can better track and estimate how much free memory your program will require and consume as the program executes during run-time.
For example, an int
data type can require 4 bytes of memory on most systems. If you create 200 individual int
variables, then it will require and consume 800 bytes (4 bytes * 200 int variables = 800 bytes), regardless of whether the variable is initialized with a value or not:
int x; // Allocates 4 bytes (on most systems), even if x is uninitialized
Another example, again using an int
data type that requires 4 bytes of memory. If you create one int
array of 100 elements, then that one array requires and consumes 400 bytes (4 bytes per int element * 100 elements = 400 bytes), even if every element in that array is empty or null
.
int myArray[100]; // Allocates 100 * 4 bytes = 400 bytes, even if elements are uninitialized
Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.
About | Contact Us | Privacy | Terms & Conditions | © 2025 - T&J Divisions, LLC, All Rights Reserved |