C++ Data Types

Thu Feb 13 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

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

Integral data types store whole numbers in memory without any fractional or decimal components.

Integer (and Aliases)

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:

  • The short type, which occupies 2 bytes, can store values from -32,768 to 32,767.
  • The 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 TypeStorageValue Range
short2 bytes-32,768 to 32,767
int4 bytes-2,147,483,648 to 2,147,483,647
long long8 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 TypeStorageValue Range
unsigned short2 bytes0 to 65,535
unsigned int4 bytes0 to 4,294,967,295
unsigned long long8 bytes0 to 18,446,744,073,709,551,615

Boolean

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 TypeStorageValues
bool1 bytefalse (0) and true (1)

Character

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 TypeStorageValues
char1 byte0 to 255 (ASCII Table)

Floating-Point Data

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 ValueC++ Floating-Point Notation
123.4561.23456e2
9.87659.8765e0
0.121.2e-1
0.000121.2e-4
-456.789-4.56789e2

Float

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.

Double

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 TypeStorageValue Range
float4 bytes-3.4e38 to 3.4e38
double8 bytes-1.7e308 to 1.7e308

Strings

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...").

Escape Sequences

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.

SequenceNameDescription
\\BackslashRepresents backslash
\’Single QuoteRepresents single quote
\”Double QuoteRepresents double quote
\?Question MarkRepresents question mark
\0Null CharacterRepresents the NULL character
\tTabTabs the cursor over
\nNew LineMoves cursor to the next line
\bBackspaceMoves cursor back one place
\fForm FeedMoves cursor to start of next page
\rCarriage ReturnMoves cursor to start of current line

Terms

  1. 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.

  2. Integral Data – Any data type that stores whole numbers (without decimals).

  3. 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.

  4. Short (short) – A smaller integer type that uses 2 bytes (16 bits), with a range of -32,768 to 32,767.

  5. 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.

  6. Boolean (bool) – A data type that represents two logical states: true (1) and false (0).

  7. Character (char) – A data type used to store a single character, represented in 1 byte (8 bits).

  8. ASCII (American Standard Code for Information Interchange) – A character encoding standard where each character is mapped to a numerical value (0-255).

  9. Floating-Point Data – Any data type that stores decimal numbers.

  10. Float (float) – A single-precision floating-point number that uses 4 bytes (32 bits), storing values with up to 6 significant digits.

  11. Double (double) – A double-precision floating-point number that uses 8 bytes (64 bits), storing values with up to 15 significant digits.

  12. String – A sequence of characters enclosed in double quotes (" "), varying in size based on the number of characters stored.

  13. Escape Sequence – A combination of characters starting with a backslash (\) used to represent special characters in a string.

Questions

General Questions

  1. What is a data type, and why is it important in C++?

  2. What is the purpose of strong typing in C++?

Integral Data Types

  1. What is an integral data type?

  2. What are the memory sizes and value ranges of short, int, and long long in C++?

  3. How does an unsigned integer (unsigned int) differ from a signed integer (int)?

  4. Why would you use short instead of int?

  5. When should you use long long instead of int?

  6. What is the minimum and maximum values that an short can represent?

  7. What is the maximum positive value that an unsigned short can represent?

  8. What is the minimum and maximum values that an int can represent?

  9. What is the maximum positive value that an unsigned int can represent?

  10. What is the minimum and maximum values that an long long can represent?

  11. What is the maximum positive value that an unsigned long long can represent?

Boolean Data Type

  1. What values can a bool variable store?

  2. How much memory does a bool data type use in C++?

  3. Why does C++ store boolean values in at least 1 byte instead of a single bit?

Character Data Type

  1. What must a char be surrounded with?

  2. How many characters can go inside of single quotes?

  3. How much memory does char use in C++?

  4. What is ASCII, and how does it relate to the char data type?

Floating-Point Data Types

  1. What is the difference between float and double?

  2. How much memory does a float occupy?

  3. How many significant digits can a float store?

  4. How much memory does a double occupy?

  5. How many significant digits can a double store?

  6. Why would you use a double instead of a float?

Strings

  1. What must a string be surrounded with?

  2. What is the difference between a char and a string?

  3. Can a string be empty? If so, how is it represented?

Escape Sequences

  1. What is an escape sequence?

  2. Why are escape sequences needed in strings?

  3. What escape sequence would you use to include a double quote (") inside a string?

  4. How do you represent a backslash (\) inside a string?