C++ Numeric Data Types

Welcome to The Coding College! In this tutorial, we’ll explore numeric data types in C++, which allow you to work with numbers in various forms—integers, floating-point values, and more. These data types are fundamental for performing arithmetic operations, scientific calculations, and data processing.

Numeric Data Types in C++

C++ provides several built-in numeric data types to handle numbers efficiently. They can be broadly categorized into:

  1. Integer Data Types
    • Used to store whole numbers (e.g., -10, 0, 42).
  2. Floating-Point Data Types
    • Used to store numbers with fractional parts (e.g., 3.14, -2.5).

Integer Data Types

Data TypeDescriptionExampleMemoryRange
intStandard integer424 bytes-2,147,483,648 to 2,147,483,647
shortShort integer1002 bytes-32,768 to 32,767
longLong integer1234567894 or 8 bytesPlatform-dependent
long longVery large integer92233720368547758078 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned intPositive integers only1004 bytes0 to 4,294,967,295

Example: Declaring Integer Variables

#include <iostream>
using namespace std;

int main() {
    int age = 25;              // Standard integer
    unsigned int score = 100;  // Only positive values
    long population = 7800000000; // Large number

    cout << "Age: " << age << endl;
    cout << "Score: " << score << endl;
    cout << "Population: " << population << endl;

    return 0;
}

Output:

Age: 25  
Score: 100  
Population: 7800000000  

Floating-Point Data Types

Data TypeDescriptionExampleMemoryPrecision
floatSingle-precision floating-point3.144 bytes~6 decimal places
doubleDouble-precision floating-point3.1415928 bytes~15 decimal places
long doubleExtended-precision floating-point3.1415926535912-16 bytesPlatform-dependent

Example: Declaring Floating-Point Variables

#include <iostream>
using namespace std;

int main() {
    float price = 19.99;       // Single-precision
    double pi = 3.14159265359; // Double-precision
    long double gravity = 9.80665; // Extended-precision

    cout << "Price: $" << price << endl;
    cout << "Value of Pi: " << pi << endl;
    cout << "Gravity: " << gravity << " m/s^2" << endl;

    return 0;
}

Output:

Price: $19.99  
Value of Pi: 3.14159265359  
Gravity: 9.80665 m/s^2  

Modifiers for Numeric Data Types

Modifiers like signed, unsigned, short, and long can be applied to integer and floating-point types to adjust their range and size.

Example: Using Modifiers

#include <iostream>
using namespace std;

int main() {
    short int smallNumber = 32767;     // Maximum value for short
    unsigned long int largeNumber = 1234567890;

    cout << "Small Number: " << smallNumber << endl;
    cout << "Large Number: " << largeNumber << endl;

    return 0;
}

Output:

Small Number: 32767  
Large Number: 1234567890  

Arithmetic with Numeric Data Types

C++ allows arithmetic operations like addition, subtraction, multiplication, and division on numeric data types.

Example: Basic Arithmetic

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 3;
    cout << "Addition: " << (a + b) << endl;
    cout << "Subtraction: " << (a - b) << endl;
    cout << "Multiplication: " << (a * b) << endl;
    cout << "Division: " << (a / b) << endl;

    return 0;
}

Output:

Addition: 13  
Subtraction: 7  
Multiplication: 30  
Division: 3  

💡 Note: Division of integers results in an integer. For precise division, use floating-point types.

Using sizeof() to Check Memory Size

The sizeof() operator helps determine the memory size of numeric data types on your system.

Example:

#include <iostream>
using namespace std;

int main() {
    cout << "Size of int: " << sizeof(int) << " bytes" << endl;
    cout << "Size of float: " << sizeof(float) << " bytes" << endl;
    cout << "Size of double: " << sizeof(double) << " bytes" << endl;

    return 0;
}

Output (Depends on Platform):

Size of int: 4 bytes  
Size of float: 4 bytes  
Size of double: 8 bytes  

Choosing the Right Numeric Data Type

  1. Use int for whole numbers unless you need very large or small values.
  2. Use float or double for numbers with fractional parts. Prefer double for high precision.
  3. Use unsigned types when negative values are not needed to save memory.

Best Practices

  • Always Initialize Variables: Uninitialized variables may contain garbage values.
  • Use double for Precision: Avoid float for scientific calculations due to precision limits.
  • Beware of Integer Overflow: Use long long or unsigned for large values.

Learn More with The Coding College

This tutorial covers the basics of numeric data types. To master more advanced topics like type conversions, numeric limits, and performance optimization, visit The Coding College.

What’s Next?

  1. Experiment with arithmetic operations using different data types.
  2. Learn about type casting and converting between numeric types.

Leave a Comment