LearnContact
Lesson 88 min read

Data Types

Different kinds of data require different amounts of memory. In this lesson, you will learn what data types are, the built-in data types in C++, and how to choose the right one for your variables.

Introduction

In the previous lessons, you learned about variables and constants. Variables store data in memory, but not all data is the same. An age such as 20 is a whole number, a percentage such as 92.5 contains a decimal part, a grade such as A is a single character, and a value such as true represents a logical condition. Because different kinds of data are stored and processed differently, C++ uses data types.

What is a Data Type?

A data type tells the compiler what kind of information a variable is intended to store. It determines how the value is represented, which values the object can represent, and which operations can be performed on it.

  • The kind of data a variable can store.
  • How the value is represented in memory.
  • The range of values that can be represented.
  • The operations that can be performed on the data.
Simple Definition

A data type tells the compiler what kind of information a variable will store and how that information should be interpreted.

Why Do We Need Data Types?

Without Data Types

  • The compiler would not know how values should be interpreted.
  • Different kinds of data could be mixed incorrectly.
  • Invalid operations would be harder to detect.
  • Memory and calculations could not be managed reliably.

With Data Types

  • Represent different kinds of information correctly.
  • Allow the compiler to detect many invalid operations.
  • Help programmers choose suitable representations for data.
  • Make programs clearer and easier to understand.

Real-World Analogy

Imagine a warehouse where different items require different kinds of storage. Books may be placed on shelves, milk requires refrigeration, money belongs in a secure locker, and clothes may be stored in a cupboard. In the same way, different kinds of information require suitable data types.

Warehouse Analogy
Warehouse

Books      → Shelf
Milk       → Refrigerator
Money      → Locker
Clothes    → Cupboard

Similarly:

int        → Whole Numbers
float      → Decimal Numbers
char       → Single Characters
bool       → True/False Values

Categories of Data Types

C++ provides fundamental types directly through the language and allows programmers to build more complex types from them. For beginner-level learning, data types are commonly introduced in the following broad categories.

Built-in Types

Fundamental types provided by the C++ language, such as int, float, double, char, bool, and void.

Derived Types

Types formed from other types, including arrays, pointers, references, and functions.

User-Defined Types

Types created or declared by programmers, including classes, structures, unions, and enumerations.

Beginner Focus

This lesson focuses mainly on the commonly used built-in data types. Derived and user-defined types will be covered in later lessons.

Built-in Data Types

Built-in data types are provided directly by the C++ language. The following types are among the most commonly used by beginners.

Data TypeCommon Purpose
intWhole numbers
floatSingle-precision floating-point numbers
doubleDouble-precision floating-point numbers
charCharacter values
boolLogical true or false values
voidAbsence of a value

Understanding Each Data Type

Integer (int)

The int type is commonly used to represent whole numbers such as 10, -25, 500, and 1000.

Floating Point (float)

The float type represents single-precision floating-point values. It is used when fractional values are required and float precision is sufficient.

Double (double)

The double type represents double-precision floating-point values. It generally provides greater precision and range than float.

Character (char)

The char type represents a character value. Character literals are written inside single quotation marks, such as 'A', '7', and '@'.

Boolean (bool)

The bool type represents one of two logical values: true or false. It is commonly used in conditions and decision-making.

Void (void)

The void type represents the absence of a value. It is commonly used as the return type of functions that do not return a value.

Examples
int age = 20;
float temperature = 36.5f;
double pi = 3.1415926535;
char grade = 'A';
bool passed = true;
Important Note

The exact size and range of many C++ fundamental types are implementation-dependent. Use sizeof when you need to inspect the size of a type on the current system.

Memory Representation

Suppose a program creates variables using different data types. Each variable stores a value according to the representation and requirements of its type.

Variables
int age = 20;
float percentage = 92.5f;
char grade = 'A';
bool passed = true;
Conceptual Memory View
Variable         Value

age              20
percentage       92.5
grade            A
passed           true

Each variable has its own type and stored value.

The actual addresses and byte sizes depend on
the compiler, platform, and implementation.

Example: Using Different Data Types

#include <iostream>

int main()
{
    int age = 20;
    float percentage = 92.5f;
    char grade = 'A';
    bool passed = true;

    std::cout << age << std::endl;
    std::cout << percentage << std::endl;
    std::cout << grade << std::endl;
    std::cout << passed;

    return 0;
}

Line-by-Line Explanation

int age = 20;

Creates an integer variable named age and initializes it with the whole number 20.

float percentage = 92.5f;

Creates a float variable named percentage. The f suffix makes 92.5f a float literal.

char grade = 'A';

Creates a character variable named grade. Character literals use single quotation marks.

bool passed = true;

Creates a Boolean variable named passed and initializes it with the logical value true.

std::cout Statements

Each output statement sends a stored value to the standard output stream. std::endl inserts a newline and flushes the stream.

Boolean Output

Without std::boolalpha, inserting a bool into a standard output stream normally displays 1 for true and 0 for false.

Output
20
92.5
A
1

Program Execution Flow

Program Starts
Create Variables
Initialize Values
Store Values According to Their Types
Display Values
Program Ends

Data Type Comparison

Data TypeExample ValueCommon Purpose
int100Whole numbers
float45.75fSingle-precision floating-point values
double3.1415926535Double-precision floating-point values
char'A'Character values
booltrueLogical values
voidAbsence of a value

Real-World Applications

Banking Software

Integer types may represent counts and identifiers, Boolean values may represent account states, and carefully chosen numeric representations are used for financial values.

Student Management

Integer types can store roll numbers, char can represent a single grade symbol, and floating-point types can represent percentages.

Gaming

Integer types can represent health points, bool can represent game states, and floating-point types can represent positions and movement.

Scientific Applications

Floating-point types, especially double, are commonly used for measurements and numerical calculations.

Financial Data

Binary floating-point types such as float and double cannot exactly represent every decimal value. Real financial software often uses integer minor units, fixed-point arithmetic, decimal types, or specialized libraries depending on its requirements.

Common Beginner Mistakes

Using int for Decimal Values

Converting a floating-point value to int discards the fractional part. Choose a suitable floating-point type when fractional values are required.

Using Double Quotes for Characters

A character literal uses single quotes, such as 'A'. Double quotes create a string literal, such as "A".

Using Single Quotes for Strings

Single quotes are for character literals. Text containing multiple characters uses double quotes.

Confusing float and double

Both represent floating-point values, but double generally provides greater precision and range than float.

Assuming Type Sizes Are Always the Same

The exact sizes of many fundamental types can vary between implementations. Do not assume every system uses identical sizes.

Integer and Decimal Values
// Fractional part is discarded during conversion
int price = 99.99;

// Better when a fractional value is required
double price = 99.99;
Characters and Strings
// ❌ Incorrect for a char
char grade = "A";

// ✅ Correct character literal
char grade = 'A';

// Character literal
'A'

// String literal
"Hello"

Best Practices

  • Choose a data type that correctly represents the values your program needs.
  • Do not select types based only on assumed memory savings without understanding the required range and behavior.
  • Use double as the usual default floating-point type unless there is a specific reason to use float.
  • Do not rely on binary floating-point for exact decimal arithmetic when exactness is required.
  • Use meaningful variable names such as studentAge instead of unclear abbreviations.
  • Use bool for true-or-false states instead of using arbitrary integer values.
  • Use sizeof when you need to inspect the size of a type on the current implementation.

Frequently Asked Questions

What is a data type?

A data type defines how a value is represented, which values it can represent, and which operations are valid for it.

Why are data types important?

They allow the compiler to interpret values correctly, enforce type rules, and detect many invalid operations.

Which data type stores whole numbers?

Integer types are used for whole numbers. int is the most commonly introduced integer type.

Which data types store decimal numbers?

Floating-point types such as float and double represent values with fractional parts.

Which data type stores true or false?

The bool type stores the logical values true and false.

What is the difference between float and double?

Both are floating-point types, but double generally provides greater precision and a wider range than float.

Does every data type have the same size on every computer?

No. The C++ language specifies minimum requirements and relationships for many types, but exact sizes can depend on the implementation and platform.

Key Takeaways

  • Data types define how values are represented and used.
  • C++ provides fundamental types and allows more complex types to be built from them.
  • int is commonly used for whole numbers.
  • float and double represent floating-point values.
  • double generally provides greater precision than float.
  • char represents character values.
  • bool represents true or false.
  • void represents the absence of a value.
  • The exact size of many fundamental types depends on the implementation.
  • Choosing an appropriate data type improves correctness, clarity, and maintainability.

Summary

Data types are a fundamental part of C++ because they define how values are represented, interpreted, and processed. By selecting suitable data types, programmers can write clearer and more reliable programs. Understanding commonly used types such as int, float, double, char, bool, and void provides the foundation for learning operators, expressions, functions, classes, and object-oriented programming.

Next Lesson →

Operators