Data types in C++

Similar to C, C++ also utilizes various data types to represent different kinds of data. Data types in C++ define the size, range, and operations allowed on the data stored in variables.

Here’s a breakdown of the main data types in C++:

Built-in:

  • Integer: intshort intlong intlong long int, etc. – Stores whole numbers.
int x = 10;
  • Floating-point: floatdoublelong double – Stores decimal numbers.
float y = 3.14;
  • Character: charwchar_t – Stores single characters.
char ch = 'A';
  • Boolean: bool – Stores true or false values.
bool isTrue = true;

Derived Data Types:

  • Arrays: Fixed-size collections of elements of the same type.
int numbers[5] = {1, 2, 3, 4, 5}; // Array of 5 integers
  • Pointers: Stores the memory address of another variable.
int age = 25;
int *agePtr = &age; // agePtr points to the memory address of variable 'age'
  • References: Alias to an existing variable.
int number = 10;
int &numberRef = number; // numberRef and number refer to the same memory location
  • Structures: Organize multiple variables of different types into a single unit.
struct Person {
  std::string name;
  int age;
};
  • Unions: Stores one of several variables of different types within the same memory location.
union Data {
  int number;
  char character;
};

union Data data;
data.number = 10; // Stores the integer 10
data.character = 'a'; // Stores the character 'a' (only one value can be stored at a time)
  • Classes: User-defined data types representing complex entities with attributes and methods.
class Dog {
public:
  std::string name;
  int age;

  void bark() {
    std::cout << "Woof!";
  }
};

Other Data Types:

  • void: Represents the absence of a value (used for functions that don’t return a value).
void printMessage() {
  // This function doesn't return a value
  std::cout << "This is a message";
}
  • enum: Enumerated types, defining a set of named integer constants.
enum Color { Red, Green, Blue };
Color favoriteColor = Green;
  • auto: Let the compiler deduce the data type automatically (C++11 and later).

Type Modifiers:

  • Signed: Denotes that an integer can be positive, negative, or zero.
int age = 25; // signed int, can be positive, negative, or zero
  • Unsigned: Denotes that an integer can only be positive or zero.
unsigned int score = 100; // unsigned int, can only be positive or zero
  • Short: Reduces the size of an integer type.
short int smallNumber = 10; // smaller memory footprint than int
  • Long: Increases the size of an integer type.
long int largeNumber = 1234567890; // larger range than int

Additional Types from C++11 and Later:

nullptr:

What it is:

  • A keyword introduced in C++11 to represent a null pointer value.
  • It’s a distinct type, different from any integral or pointer type.
  • It can be assigned to any pointer type, but not to other types.

Usage examples:

  • Assigning null to pointers:
int* ptr = nullptr;  // ptr now points to nothing
  • Checking for null pointers:
if (ptr == nullptr) {
    // handle the null case
}
  • Overloading functions based on null pointers:
void print(int* ptr) { ... }
void print(nullptr_t) { ... }  // Overload for null pointers

char16_t:

What it is:

  • A distinct character type introduced in C++11 to represent 16-bit characters.
  • It’s primarily intended for working with Unicode characters, specifically UTF-16 encoding.
  • It occupies 2 bytes of memory.

Key characteristics:

  • Size: 16 bits (2 bytes).
  • Encoding: UTF-16 (usually).
  • Literals: Denoted by prefixing a string literal with uu"Hello world".
  • Header: Requires #include <cuchar> header for functions and definitions.

Usage examples:

char16_t ch1 = u'A';  // Assign a single character
char16_t str[] = u"Hello";  // Create a string of char16_t characters

char32_t:

What it is:

  • A distinct character type introduced in C++11 to represent 32-bit characters.
  • It’s designed for working with large character sets, including Unicode characters.
  • It occupies 4 bytes of memory.

Key characteristics:

  • Size: 32 bits (4 bytes).
  • Encoding: Often used for UTF-32 encoding, but not strictly tied to it.
  • Literals: Denoted by prefixing a string literal with UU"Hello world".
  • Header: Requires #include <cuchar> header for functions and definitions.

Usage examples:

char32_t ch = U'\u20AC';  // Assign a single Euro symbol character
char32_t str[] = U"Hello \u263A";  // Create a string with a smiley face

long long int:

What it is:

  • An extended integer type introduced in C++98 to accommodate larger integer values than those supported by the standard int type.
  • It’s typically 8 bytes in size, but the exact size can vary across platforms.

Key characteristics:

  • Size: Usually 8 bytes (64 bits).
  • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (approximately).
  • Declaration: Uses the keyword long long int.
  • Literals: Suffixed with ll or LL42ll or 1234567890123456789LL.

Usage examples:

long long int population = 8000000000;  // Represents a large population
long long int age_of_universe = 13800000000000LL;  // Represents a very large number

Size of data types:

The size of data types in C++ can vary depending on the system and compiler being used. However, some standard sizes are typically followed:

Data TypeSize (bytes)
char1
wchar_t2 or 4
short int2
int4
long int4 or 8
long long int8
float4
double8
long double8 or 16
bool1
void0

Key Points:

  • Choose the appropriate data type based on the kind of data you need to store and the required precision.
  • Use modifiers to optimize memory usage and value ranges.
  • Derived data types offer more complex data structures.
  • C++11 and later introduced additional data types and features.

See also:

Data types in C

Share this post:

Newsletter Updates

Enter your email address below and subscribe to our newsletter