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:
- Integer Data Types
- Used to store whole numbers (e.g.,
-10, 0, 42
).
- Used to store whole numbers (e.g.,
- Floating-Point Data Types
- Used to store numbers with fractional parts (e.g.,
3.14, -2.5
).
- Used to store numbers with fractional parts (e.g.,
Integer Data Types
Data Type | Description | Example | Memory | Range |
---|---|---|---|---|
int | Standard integer | 42 | 4 bytes | -2,147,483,648 to 2,147,483,647 |
short | Short integer | 100 | 2 bytes | -32,768 to 32,767 |
long | Long integer | 123456789 | 4 or 8 bytes | Platform-dependent |
long long | Very large integer | 9223372036854775807 | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned int | Positive integers only | 100 | 4 bytes | 0 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 Type | Description | Example | Memory | Precision |
---|---|---|---|---|
float | Single-precision floating-point | 3.14 | 4 bytes | ~6 decimal places |
double | Double-precision floating-point | 3.141592 | 8 bytes | ~15 decimal places |
long double | Extended-precision floating-point | 3.14159265359 | 12-16 bytes | Platform-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
- Use
int
for whole numbers unless you need very large or small values. - Use
float
ordouble
for numbers with fractional parts. Preferdouble
for high precision. - 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: Avoidfloat
for scientific calculations due to precision limits. - Beware of Integer Overflow: Use
long long
orunsigned
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?
- Experiment with arithmetic operations using different data types.
- Learn about type casting and converting between numeric types.