Data Types in C++

Data types in C++ define what kind of value a variable can store and how the compiler should interpret that value. They control memory usage, numeric range, precision, allowed operations, and many of the safety checks performed by the compiler. If variables are named storage locations, data types are the rules that describe what may legally live in those locations.

This topic is fundamental because nearly every later concept in C++ depends on type information. Arithmetic expressions, comparisons, function parameters, return values, arrays, pointers, classes, templates, and standard library containers all rely on types. In this article, we will understand data types in C++, study their major categories, look at common sizes in bytes, learn how to check size with sizeof, and see practical examples and common mistakes.

What Are Data Types in C++?

A data type in C++ tells the compiler what kind of data is being stored and what operations are valid for that data. For example, an integer type stores whole numbers, a floating-point type stores decimal values, a character type stores character data, and a Boolean type stores either true or false.

QuestionAnswered by the data type
What kind of value is stored?Whole number, decimal, character, logical value, and so on
How much memory is needed?The type decides how much storage is reserved
What operations are allowed?Arithmetic, comparison, logical operations, bitwise use, and more
How is the value interpreted?As signed integer, floating-point number, character code, Boolean state, and so on

A data type is the compiler’s way of knowing what a value means and how it should be handled in memory and expressions.

Why Data Types Matter in C++

  • They decide how many bytes a value usually occupies.
  • They affect range and precision.
  • They help the compiler catch invalid operations.
  • They influence performance and memory usage.
  • They make code more readable and predictable.

For example, choosing int for a loop counter makes sense because it stores whole numbers. Choosing double for a quantity with decimals makes more sense than forcing that value into an integer type and losing precision. Type choice is not decorative. It directly affects correctness.

Main Categories of Data Types in C++

C++ data types are commonly discussed in a few broad groups. For beginners, the most important group is the fundamental built-in types, but it is useful to know the overall picture.

CategoryExamplesPurpose
Fundamental typesint, char, float, double, bool, voidCore built-in types provided by the language
Derived typesArrays, pointers, references, functionsBuilt from other types
User-defined typesstruct, class, enum, type aliasesCreated by the programmer

This article focuses mainly on the fundamental data types because they are the base on which later topics are built.

Fundamental Data Types in C++ with Sizes in Bytes

The exact size of some C++ types depends on the compiler and platform. That detail matters. C++ defines minimum rules and relationships between types, but not every type has one fixed size on every machine. The table below shows common sizes in bytes used on modern systems and also notes important guarantees.

TypePurposeTypical Size (bytes)Important Notes
boolLogical true or false1Stores Boolean values only
charSingle character or small integer code1Exactly 1 byte by language definition
wchar_tWide character type2 or 4Platform-dependent
shortShort integer2At least 16 bits
intStandard integer type4Usually 4 bytes on modern systems
longLong integer4 or 8Depends on system model
long longLarger integer type8At least 64 bits
floatSingle-precision decimal4Used for moderate precision
doubleDouble-precision decimal8More precise than float
long doubleExtended precision decimal8, 12, or 16Compiler-dependent
voidNo valueNo storage valueUsed for functions or generic pointers

One byte in C++ is defined as the size of char. So when the language says char is 1 byte, that part is exact. The types above it may vary in exact size, but their relative capabilities and minimum requirements are defined by the language standard.

Integer Data Types in C++

Integer types store whole numbers. They may be signed or unsigned. Signed integers can store both negative and positive values, while unsigned integers store only non-negative values and use the available bits for a larger positive range.

TypeTypical UseTypical Size (bytes)
shortSmall integer values2
intGeneral-purpose integer calculations4
longLarger integer range depending on platform4 or 8
long longLarge integer values8
int age = 25;
short year = 24;
long population = 900000L;
long long stars = 10000000000LL;

Using a type that is too small may cause overflow. Using a type that is much larger than needed is not always harmful, but type choice should still be intentional rather than random.

Floating-Point Data Types in C++

Floating-point types are used for decimal values. These types are not exact in the way integers are. They store values using binary floating-point representation, which means some decimal fractions cannot be represented perfectly.

TypeTypical Size (bytes)Use
float4Moderate precision decimal calculations
double8General-purpose decimal work with higher precision
long double8, 12, or 16Extended precision where supported
float temperature = 36.5f;
double pi = 3.141592653589793;
long double preciseValue = 2.718281828459045L;

In many everyday programs, double is the default practical choice when decimals are needed because it provides better precision than float while remaining widely supported.

Character and Boolean Types in C++

The char type stores a single character value such as 'A' or 'x'. Under the hood it stores the character code. The bool type stores either true or false and is used heavily in conditions, flags, and program state checks.

char grade = 'A';
bool isConnected = true;

Even though char is often introduced as a character type, it is still an integer type at the machine level and can participate in numeric operations when needed. That is why characters are closely related to character encoding and ASCII or Unicode values.

What Is void in C++?

The void type represents the absence of a value. It does not store normal data like int or double. You will commonly see it in function return types when a function performs work but does not return a value.

void showMessage()
{
    std::cout << "Hello" << std::endl;
}

You may also see void*, which is a pointer that can hold the address of data of different types, though using it safely requires care and usually comes later in learning.

How to Check Data Type Size in C++ Using sizeof

The safest way to know the size of a type on your own machine is to ask the compiler directly. C++ provides the sizeof operator for this purpose.

#include <iostream>

int main()
{
    std::cout << "bool: " << sizeof(bool) << " byte(s)" << std::endl;
    std::cout << "char: " << sizeof(char) << " byte(s)" << std::endl;
    std::cout << "int: " << sizeof(int) << " byte(s)" << std::endl;
    std::cout << "double: " << sizeof(double) << " byte(s)" << std::endl;
    return 0;
}

This is important because the bytes table in tutorials shows common values, not a promise that every compiler on every system will match those exact numbers. sizeof gives the real answer for your platform.

Signed and Unsigned Data Types in C++

Many integer types can be modified with signed and unsigned. A signed type can represent negative and positive values. An unsigned type drops negative values and uses the available range for zero and positive values instead.

ModifierEffectExample
signedAllows negative and positive valuessigned int temperature = -5;
unsignedStores only non-negative values with larger positive rangeunsigned int count = 200;

Unsigned types should be used only when the data is naturally non-negative and the choice is actually helpful. Using them without understanding the consequences can create tricky comparisons and arithmetic bugs.

Examples of Data Types in C++

#include <iostream>
#include <string>

int main()
{
    int age = 21;
    double salary = 45500.75;
    char grade = 'A';
    bool passed = true;
    std::string name = "Riya";

    std::cout << "Name: " << name << std::endl;
    std::cout << "Age: " << age << std::endl;
    std::cout << "Salary: " << salary << std::endl;
    std::cout << "Grade: " << grade << std::endl;
    std::cout << "Passed: " << passed << std::endl;

    return 0;
}

Notice that std::string appears here even though it is not a fundamental language type. It is a standard library type that programmers use constantly in real code, so it is worth seeing alongside the built-in types early.

Common Mistakes with Data Types in C++

MistakeWhy it is a problemBetter approach
Assuming every type has the same size on every machineSome type sizes are platform-dependentUse sizeof when exact local size matters
Using int for decimal valuesThe fractional part is lostUse float, double, or long double
Using a type with insufficient rangeCan cause overflow or incorrect resultsChoose a type based on realistic value range
Using unsigned types without understanding themMay lead to unexpected arithmetic behaviorUse signed integers unless unsigned is clearly needed
Confusing char with text stringschar stores only one characterUse std::string for text

Best Practices for Choosing Data Types in C++

  • Choose the type based on meaning, not guesswork.
  • Use double by default for many decimal calculations unless float is specifically needed.
  • Check sizes with sizeof instead of assuming.
  • Use const where values should not change.
  • Keep platform dependency in mind when exact byte layout matters.

FAQs

Is int always 4 bytes in C++?

No. It is commonly 4 bytes on many modern systems, but the language standard does not force that exact size on every platform.

Which data type should I use for decimal numbers in C++?

Use float, double, or long double depending on the required precision. In many normal cases, double is the practical default.

Why is char exactly 1 byte in C++?

Because the language defines one byte as the amount of storage needed for a char. Other types are measured relative to that unit.

What is the easiest way to know type size on my machine?

Use the sizeof operator in a small C++ program. That gives the actual size used by your compiler and platform.

Is std::string a fundamental data type in C++?

No. It is a standard library type, not a built-in fundamental language type, but it is still one of the most commonly used types in practical C++ code.