← SEARCHABLE ARCHIVE
C / C++ · 2026-05-12

C++ from Basics to Mastery — Part 5

What data types are there in variables?

INTERACTIVE READING MODE →

The articles written on this blog were created with the goal of teaching. Also, the information in the article may be incorrect, so please be careful.

Let’s learn about the data types of variables. It can be broadly divided into four categories.

Basic data type. These are the most basic types that are mapped directly to memory.

1-A. Special Purpose

void: has no size. It does not occupy memory directly and is an incomplete type.

std::nullptr_t: Type of null pointer literal nullptr.

void DoSomething() {} //Function with no return value
void* ptr = nullptr;  //untyped pointer
std::nullptr_t null_val = nullptr;

1-B. Arithmetic Type

1-B-1. Integer char, signed char, unsigned char: Store characters. There is also wchar_t for Unicode. short, int, long, long long: Stores signed integers. unsigned short, unsigned int, unsigned long, unsigned long long: Stores unsigned integers. Because it does not store negative numbers, the storage space for positive numbers is twice as large as signed. bool: Stores true or false. Uses 1 byte, not 1 bit.

char c = 'A';
int count = -10;
unsigned int u_count = 20;
bool is_valid = true;

1-B-2 floating point type

Types that follow IEEE 754, There are basically float, double, and long double, There are also std::float16t, std::float32t, std::float64t, std::float128t, and std::bfloat16_t. (C++23 extension)

float f_val = 3.14f;
double d_val = 3.1415926535;

2. Complex data type This is a type that defines a structure in memory by combining basic types or other complex types.

Pointer (T): Points to a memory address of another type T. Pointer-to-member (T::): A special pointer that points to the memory offset of a specific member variable or function within a class.

Reference: Lvalue reference (T&): An alias for an object (Lvalue) that has a name and substance in memory. Rvalue reference (T&&, C++11): Binds to temporary objects and is a core type of Move Semantics and Perfect Forwarding.

Array (Array, T[N] / T[]): Allocates data of the same type to consecutive memory blocks. Function: The signature itself, consisting of a return type and a parameter list, is also a unique type. (e.g. int(double, char))

Enumeration: enum: A basic enumeration with no scope specified (allows implicit integer conversion).

enum class (C++11): A strictly scoped enumeration (strong type checking and scope guarantees). User-defined type (Class / Struct / Union): A type that implements object-oriented and data structures by controlling memory padding and alignment rules.

int value = 10;
int* ptr = &value; //pointer
int& ref = value;  //lvalue reference
int&& r_ref = 20;  //Rvalue reference


struct MyStruct { int x; };
int MyStruct::* member_ptr = &MyStruct::x; //member pointer


int arr[5] = {1, 2, 3, 4, 5}; //arrangement
int(*func_ptr)(int, int);     //function pointer


enum class Color { Red, Green, Blue }; //strict enumeration

3. System architecture-specific alias These are core alias types whose sizes are dynamically determined depending on the compiler and OS architecture (32-bit/64-bit). (<cstddef> header)

std::sizet: Return type of the sizeof operator. It is an unsigned integer that can express the byte size of the largest object that can be created in the system. (standard unit of container indexing and memory allocation) std::ptrdifft: A signed integer representing the distance when two pointers are subtracted. Used when calculating the difference between memory addresses.

#include <cstddef>

int arr[10];
std::size_t arr_size = sizeof(arr); //Maximum size guaranteed according to system architecture
std::ptrdiff_t diff = &arr[5] - &arr[0]; //Calculate distance between pointers (5)

4. CV-Qualifiers In C++'s type system, if qualifiers are added to all of the above data types, they are treated as completely different types from the compiler's perspective and are overloaded. (e.g. int and const int are different types)

const: Prevents writing to the corresponding memory area and ensures immutability. volatile: Forcefully prevents the compiler's memory read/write optimization. When controlling hardware registers or communicating with MMIO (Memory-Mapped I/O), it is essentially used to inform the compiler that register values ​​can be changed by an external source.

const int MAX_VALUE = 100; //Read only, cannot be modified
//MAX_VALUE = 200; // Compilation error occurred

volatile int hardware_register; //Avoid compiler optimization
//External hardware can change the value, so each time it is read directly from memory rather than the cache