Data is the foundation of all software systems. In C++, data is handled using strongly typed variables to ensure that operations performed during runtime are valid. These data types define the nature of variables, dictate what values functions can return, determine memory usage, and specify allowable operations.
Integral data types store whole numbers in memory without any fractional or decimal components.
Integers (int
) are the most common way to represent whole numbers and typically occupy 4 bytes (32 bits) of memory. If all 32 bits are set to 1
, the value would be 4,294,967,296. However, because half of the values must be reserved for negative numbers, the actual representable range is -2,147,483,648 to 2,147,483,647.
C++ provides finer control over memory usage through additional integer types, also known as integer aliases. These allow for storage of integers using more or less memory:
short
type, which occupies 2 bytes, can store values from -32,768 to 32,767.long long
type, which occupies 8 bytes, can store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.Using short
is beneficial on memory-constrained systems, whereas long long
is useful when values exceed the range of int
.
Data Type | Storage | Value Range |
---|---|---|
short | 2 bytes | -32,768 to 32,767 |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
long long | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
If negative numbers are not needed, these data types can be declared as unsigned, allowing for an increased range of positive values:
Data Type | Storage | Value Range |
---|---|---|
unsigned short | 2 bytes | 0 to 65,535 |
unsigned int | 4 bytes | 0 to 4,294,967,295 |
unsigned long long | 8 bytes | 0 to 18,446,744,073,709,551,615 |
The bool
data type represents logical values: true
and false
. These are internally stored as 1
and 0
, respectively, making them integral values. Although a boolean could technically be stored in a single bit, C++ requires at least 1 byte (8 bits), as memory is typically addressed in byte-sized chunks.
Data Type | Storage | Values |
---|---|---|
bool | 1 byte | false (0) and true (1) |
The char
data type represents characters such as letters, digits, and symbols. Each char
variable occupies 1 byte (8 bits) in English-based ASCII encoding. Characters are enclosed in single quotes ('A'
, '7'
, '#'
). ASCII maps the range 0-255 to unique characters (See: ASCII Table).
Data Type | Storage | Values |
---|---|---|
char | 1 byte | 0 to 255 (ASCII Table) |
Floating-point data types store decimal numbers in memory. C++ provides two primary floating-point types: float
and double
. These numbers are stored using scientific notation:
Decimal Value | C++ Floating-Point Notation |
---|---|
123.456 | 1.23456e2 |
9.8765 | 9.8765e0 |
0.12 | 1.2e-1 |
0.00012 | 1.2e-4 |
-456.789 | -4.56789e2 |
A float
, also called single precision, uses 4 bytes (32 bits) of memory, allowing values in the range -3.4e38 to 3.4e38 with up to 6 significant digits.
A double
, or double precision, uses 8 bytes (64 bits) of memory, supporting values in the range -1.7e308 to 1.7e308 with up to 15 significant digits.
Data Type | Storage | Value Range |
---|---|---|
float | 4 bytes | -3.4e38 to 3.4e38 |
double | 8 bytes | -1.7e308 to 1.7e308 |
A string is a sequence of characters enclosed in double quotes ("Hello World"
). The amount of memory used by a string depends on its length. Strings can be empty (""
), a single character ("a"
), or as long as memory allows ("Lorem ipsum dolor sit amet..."
).
Some special characters cannot be directly typed within strings (e.g., "
or \
). To include these, escape sequences—a combination of characters starting with a backslash (\
)—are used.
Sequence | Name | Description |
---|---|---|
\\ | Backslash | Represents backslash |
\’ | Single Quote | Represents single quote |
\” | Double Quote | Represents double quote |
\? | Question Mark | Represents question mark |
\0 | Null Character | Represents the NULL character |
\t | Tab | Tabs the cursor over |
\n | New Line | Moves cursor to the next line |
\b | Backspace | Moves cursor back one place |
\f | Form Feed | Moves cursor to start of next page |
\r | Carriage Return | Moves cursor to start of current line |
Data Type – A classification specifying the type of data that a variable can store, determining its size, range, and operations that can be performed on it.
Integral Data – Any data type that stores whole numbers (without decimals).
Integer (int
) – A data type that represents whole numbers using 4 bytes (32 bits), with a range of -2,147,483,648 to 2,147,483,647.
Short (short
) – A smaller integer type that uses 2 bytes (16 bits), with a range of -32,768 to 32,767.
Long Long (long long
) – A larger integer type that uses 8 bytes (64 bits), with a range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Boolean (bool
) – A data type that represents two logical states: true (1
) and false (0
).
Character (char
) – A data type used to store a single character, represented in 1 byte (8 bits).
ASCII (American Standard Code for Information Interchange) – A character encoding standard where each character is mapped to a numerical value (0-255).
Floating-Point Data – Any data type that stores decimal numbers.
Float (float
) – A single-precision floating-point number that uses 4 bytes (32 bits), storing values with up to 6 significant digits.
Double (double
) – A double-precision floating-point number that uses 8 bytes (64 bits), storing values with up to 15 significant digits.
String – A sequence of characters enclosed in double quotes (" "
), varying in size based on the number of characters stored.
Escape Sequence – A combination of characters starting with a backslash (\
) used to represent special characters in a string.
What is a data type, and why is it important in C++?
What is the purpose of strong typing in C++?
What is an integral data type?
What are the memory sizes and value ranges of short
, int
, and long long
in C++?
How does an unsigned integer (unsigned int
) differ from a signed integer (int
)?
Why would you use short
instead of int
?
When should you use long long
instead of int
?
What is the minimum and maximum values that an short
can represent?
What is the maximum positive value that an unsigned short
can represent?
What is the minimum and maximum values that an int
can represent?
What is the maximum positive value that an unsigned int
can represent?
What is the minimum and maximum values that an long long
can represent?
What is the maximum positive value that an unsigned long long
can represent?
What values can a bool
variable store?
How much memory does a bool
data type use in C++?
Why does C++ store boolean values in at least 1 byte instead of a single bit?
What must a char
be surrounded with?
How many characters can go inside of single quotes?
How much memory does char
use in C++?
What is ASCII, and how does it relate to the char
data type?
What is the difference between float
and double
?
How much memory does a float
occupy?
How many significant digits can a float
store?
How much memory does a double
occupy?
How many significant digits can a double
store?
Why would you use a double
instead of a float
?
What must a string
be surrounded with?
What is the difference between a char
and a string
?
Can a string be empty? If so, how is it represented?
What is an escape sequence?
Why are escape sequences needed in strings?
What escape sequence would you use to include a double quote ("
) inside a string?
How do you represent a backslash (\
) inside a string?