What are Data Types in C++?

In C++, data types tell the compiler what kind of value a variable can store. This helps the compiler know how much memory to allocate and what operations can be performed on that value.

For example, numbers can be used in mathematical calculations, but text (characters or strings) cannot be directly added to numbers. Using the correct data type avoids errors and gives correct results.

Example:


int age = 18;           // Integer
float height = 5.6;     // Float
double balance = 12345.67; // Double
char grade = 'A';       // Character
bool isPassed = true;   // Boolean
        

Why Use Data Types?

  • To efficiently use memory.
  • To reduce errors in the program.
  • To perform correct operations (like adding numbers or joining strings).

Types of Data Types in C++

C++ data types are mainly divided into three categories:

  1. Basic Data Types
  2. Derived Data Types (arrays, pointers, functions, references)
  3. User-defined Data Types (struct, class, enum, typedef, union)

1. Basic Data Types

These are the fundamental building blocks of C++ programs. They are used to represent simple values such as numbers, characters, and logical results.

Basic Data Types in C++:

  • int – stores integers (whole numbers).
  • float – stores decimal numbers (single precision).
  • double – stores decimal numbers with double precision (more accurate).
  • char – stores a single character.
  • bool – stores logical values (true or false).
  • string – stores a sequence of characters (words or sentences).
  • void – represents “no value” (used in functions with no return type).

Memory Size and Range of Basic Data Types

Data Type Size (in Bytes) Range (Approximate) Example
int 4 -2,147,483,648 to 2,147,483,647 int age = 18;
float 4 ±3.4e−38 to ±3.4e+38 float marks = 85.5;
double 8 ±1.7e−308 to ±1.7e+308 double salary = 55000.75;
char 1 -128 to 127 char grade = 'A';
bool 1 true or false bool isLogin = true;
string Varies (1 byte per character) No fixed range string city = "Panipat";
void No data stored void show();

Example of Basic Data Types

#include <iostream>
#include <string>
using namespace std;

int main() {
    int rollNo = 101;
    float percentage = 91.8;
    double balance = 123456.789;
    char grade = 'A';
    bool isPassed = true;
    string studentName = "Angel";

    cout << "Student Name: " << studentName << endl;
    cout << "Roll No: " << rollNo << endl;
    cout << "Percentage: " << percentage << "%" << endl;
    cout << "Bank Balance: " << balance << endl;
    cout << "Grade: " << grade << endl;
    cout << "Passed (1=True, 0=False): " << isPassed << endl;

    return 0;
}

Output:

Student Name: Angel

Roll No: 101

Percentage: 91.8%

Bank Balance: 123456.789

Grade: A

Passed (1=True, 0=False): 1

Explanation:

  • int → stores whole numbers such as roll number.
  • float → stores percentage with decimals.
  • double → stores large decimal values with higher accuracy.
  • char → stores one character (like grade).
  • bool → stores result as true or false (pass/fail).
  • string → stores multiple characters like names or cities.

2. Derived Data Types (Overview)

These are data types derived from the basic types:

  • Arrays – collection of elements of the same type.
  • Pointers – store memory addresses of variables.
  • Functions – blocks of code performing specific tasks.
  • References – alternative names for existing variables.

Example:


int numbers[5] = {1, 2, 3, 4, 5};    // Array
int *ptr = &age;                     // Pointer
int &ref = age;                      // Reference
        

3. User-defined Data Types (Overview)

These are created by programmers to handle complex data. Examples include:

  • struct – groups different data types together.
  • class – used in Object-Oriented Programming (OOP).
  • enum – defines a set of named constants.
  • typedef/using – creates a new name for an existing type.
  • union – similar to struct but shares same memory location.

Example:

struct Student {
    int rollNo;
    char name[50];
    float marks;
};

enum Color {Red, Green, Blue};

typedef unsigned int uint;

Summary:

  • Data types define what type of data a variable will store.
  • Basic types are the foundation; others are built using them.
  • String is also a data type that stores text values.
  • Size and range may vary depending on the compiler.
  • Choosing the correct data type improves efficiency and accuracy.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar