CPP Data Types

C++ Tutorial

💻 C++ Data Types (CPP Data Types) – Complete Beginner to Interview Guide

Data types in C++ specify what type of data a variable can store.
They decide:

  • how much memory is used

  • what kind of values are allowed

  • what operations can be performed


1️⃣ What is a Data Type? ⭐

A data type tells the compiler the type and size of data a variable will hold.

Example

int age = 20;

📌 int is the data type of variable age.


2️⃣ Types of Data Types in C++ ⭐⭐

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

  1. Basic (Primitive) Data Types

  2. Derived Data Types

  3. User-Defined Data Types

  4. Void Data Type


3️⃣ Basic (Primitive) Data Types ⭐⭐⭐

int

Stores whole numbers.

int a = 10;

📌 Size: usually 4 bytes


float

Stores decimal numbers (less precision).

float marks = 85.5;

📌 Size: 4 bytes


double

Stores decimal numbers (more precision).

double pi = 3.141592;

📌 Size: 8 bytes


char

Stores a single character.

char grade = 'A';

📌 Size: 1 byte


bool

Stores true or false.

bool isPassed = true;

📌 Size: 1 byte


string

Stores text (sequence of characters).

string name = "Sanjit";

📌 Requires #include <string>


4️⃣ Derived Data Types ⭐⭐

Derived from basic data types.

 Array

int arr[5] = {1, 2, 3, 4, 5};

 Pointer

int x = 10;
int* p = &x;

 Reference

int x = 10;
int& ref = x;

5️⃣ User-Defined Data Types ⭐⭐

Created by the programmer.

struct

struct Student {
int roll;
float marks;
};

union

union Data {
int i;
float f;
};

enum

enum Day { Mon, Tue, Wed };

typedef / using

typedef int number;
// OR
using number = int;

6️⃣ Void Data Type ⭐

Represents no value.

void show() {
cout << "Hello";
}

📌 Used with functions that do not return anything.


7️⃣ Size of Data Types ⭐⭐

cout << sizeof(int) << endl;
cout << sizeof(float) << endl;
cout << sizeof(char);

📌 Size may vary depending on system/compiler.


8️⃣ Type Modifiers ⭐⭐

Used to modify basic data types.

  • short

  • long

  • signed

  • unsigned

Example

unsigned int x = 100;
long int y = 100000;

9️⃣ Common Data Type Errors ❌

❌ Using wrong data type
❌ Data overflow
❌ Forgetting to include <string>
❌ Assuming same size on all systems


📌 Interview Questions (CPP Data Types)

Q1. What is a data type?
👉 Specifies type of data a variable can store

Q2. Difference between float and double?
👉 Precision and size

Q3. What is void data type?
👉 Represents no value

Q4. Which data type stores true/false?
👉 bool


✅ Summary

✔ Data types define type & size of data
✔ Categories: Basic, Derived, User-defined, Void
✔ Choose correct data type for efficiency
✔ Important for memory & performance

You may also like...