CPP Numeric Data Types

C++ Tutorial

💻 C++ Numeric Data Types – Complete Beginner to Interview Guide

Numeric data types in C++ are used to store numbers.
They are mainly divided into Integer types and Floating-point types.


1️⃣ What are Numeric Data Types? ⭐

Numeric data types store numerical values that can be used in:

  • calculations

  • conditions

  • loops

  • mathematical operations


2️⃣ Categories of Numeric Data Types ⭐⭐

✅ 1. Integer Types (Whole Numbers)

✅ 2. Floating-Point Types (Decimal Numbers)


3️⃣ Integer Numeric Data Types ⭐⭐⭐

Used to store whole numbers (no decimals).

🔹 int

int a = 10;

📌 Size: 4 bytes
📌 Range: approx -2,147,483,648 to 2,147,483,647


🔹 short int

short int b = 100;

📌 Size: 2 bytes


🔹 long int

long int c = 100000;

📌 Size: 4 or 8 bytes (system dependent)


🔹 long long int

long long int d = 123456789;

📌 Size: 8 bytes


🔹 unsigned int

unsigned int e = 50;

📌 Stores only positive numbers
📌 Doubles positive range


4️⃣ Floating-Point Numeric Data Types ⭐⭐⭐

Used to store decimal values.

🔹 float

float x = 5.5;

📌 Size: 4 bytes
📌 Precision: ~6–7 digits


🔹 double

double y = 3.141592653;

📌 Size: 8 bytes
📌 Precision: ~15 digits


🔹 long double

long double z = 3.141592653589793;

📌 Size: 8, 12, or 16 bytes (compiler dependent)


5️⃣ Example Program (Numeric Data Types) ⭐⭐


 

Output

10
5.5
12.345

6️⃣ Size of Numeric Data Types ⭐⭐


📌 Size may vary based on system/compiler.


7️⃣ Integer vs Floating-Point ⚠️


Output

2

📌 Integer division removes decimals.

✔ Correct way:


Output:

2.5

8️⃣ Common Numeric Data Type Errors ❌

❌ Integer overflow
❌ Using int for decimal values
❌ Precision loss with float
❌ Assuming fixed size on all systems


📌 Interview Questions (Numeric Data Types)

Q1. Difference between int and float?
👉 int stores whole numbers, float stores decimals

Q2. Which data type has more precision: float or double?
👉 double

Q3. What is unsigned int?
👉 Stores only positive integers


✅ Summary

✔ Numeric data types store numbers
✔ Integer types → whole numbers
✔ Floating-point types → decimals
✔ Choose correct type for accuracy
✔ Important for exams & interviews

You may also like...