Coming Soon

This lesson is currently being developed

Introduction to fundamental data types

Learn about the basic data types available in C++.

Fundamental Data Types
Chapter
Beginner
Difficulty
35min
Estimated Time

What to Expect

Comprehensive explanations with practical examples

Interactive coding exercises to practice concepts

Knowledge quiz to test your understanding

Step-by-step guidance for beginners

Development Status

In Progress

Content is being carefully crafted to provide the best learning experience

Preview

Early Preview Content

This content is still being developed and may change before publication.

4.1 — Introduction to fundamental data types

In this lesson, you'll learn about the basic building blocks of C++ programs: fundamental data types. These are the basic types that C++ provides to represent different kinds of data.

What are data types?

A data type tells the compiler:

  • How much memory to allocate for a variable
  • How to interpret the data stored in that memory
  • What operations can be performed on that data

Think of data types like containers with specific shapes and sizes - you can only put certain things in certain containers, and each container takes up a specific amount of space.

Categories of fundamental data types

C++ provides several categories of fundamental data types:

1. Integer types

Used to store whole numbers (positive, negative, or zero):

  • int - Standard integer
  • short - Short integer (smaller range)
  • long - Long integer (larger range)
  • long long - Extra long integer (even larger range)

2. Character types

Used to store single characters:

  • char - Single character (like 'A', 'b', '3', '@')

3. Floating-point types

Used to store numbers with decimal points:

  • float - Single precision floating-point
  • double - Double precision floating-point
  • long double - Extended precision floating-point

4. Boolean type

Used to store true/false values:

  • bool - Boolean (true or false)

5. Null pointer type

  • std::nullptr_t - Type of nullptr (advanced topic)

6. Void type

  • void - Represents "no type" or "no value"

Size and range of data types

Different data types use different amounts of memory and can store different ranges of values:

#include <iostream>
#include <climits>  // For integer limits
#include <cfloat>   // For floating-point limits

int main()
{
    std::cout << "Data Type Sizes:\n";
    std::cout << "char: " << sizeof(char) << " byte(s)\n";
    std::cout << "int: " << sizeof(int) << " byte(s)\n";
    std::cout << "float: " << sizeof(float) << " byte(s)\n";
    std::cout << "double: " << sizeof(double) << " byte(s)\n";
    std::cout << "bool: " << sizeof(bool) << " byte(s)\n";
    
    std::cout << "\nInteger Ranges:\n";
    std::cout << "int min: " << INT_MIN << "\n";
    std::cout << "int max: " << INT_MAX << "\n";
    
    return 0;
}

Typical Output: (may vary by system)

Data Type Sizes:
char: 1 byte(s)
int: 4 byte(s)
float: 4 byte(s)
double: 8 byte(s)
bool: 1 byte(s)

Integer Ranges:
int min: -2147483648
int max: 2147483647

Declaring variables with fundamental types

Here's how to create variables of different fundamental types:

#include <iostream>

int main()
{
    // Integer types
    int age = 25;
    short temperature = -10;
    long population = 1000000L;
    long long bigNumber = 9876543210LL;
    
    // Character type
    char grade = 'A';
    char symbol = '@';
    
    // Floating-point types
    float price = 19.99f;
    double pi = 3.141592653589793;
    long double precision = 3.141592653589793238L;
    
    // Boolean type
    bool isReady = true;
    bool isFinished = false;
    
    // Display the values
    std::cout << "Age: " << age << std::endl;
    std::cout << "Temperature: " << temperature << "°C" << std::endl;
    std::cout << "Population: " << population << std::endl;
    std::cout << "Big number: " << bigNumber << std::endl;
    std::cout << "Grade: " << grade << std::endl;
    std::cout << "Symbol: " << symbol << std::endl;
    std::cout << "Price: $" << price << std::endl;
    std::cout << "Pi: " << pi << std::endl;
    std::cout << "High precision pi: " << precision << std::endl;
    std::cout << "Is ready: " << isReady << std::endl;
    std::cout << "Is finished: " << isFinished << std::endl;
    
    return 0;
}

Output:

Age: 25
Temperature: -10°C
Population: 1000000
Big number: 9876543210
Grade: A
Symbol: @
Price: $19.99
Pi: 3.14159
High precision pi: 3.14159
Is ready: 1
Is finished: 0

Choosing the right data type

Selecting the appropriate data type is important for:

  • Memory efficiency: Don't waste space
  • Performance: Some operations are faster on certain types
  • Accuracy: Ensure your type can handle the range of values you need

Guidelines for choosing data types:

  1. For whole numbers: Use int unless you need a specific range

    int count = 42;           // Good for most counting
    long long distance = 93000000L;  // For very large numbers
    
  2. For decimal numbers: Use double for most cases

    double temperature = 98.6;    // Good precision
    float coordinate = 12.5f;     // When memory is tight
    
  3. For single characters: Use char

    char initial = 'J';
    char newline = '\n';
    
  4. For true/false values: Use bool

    bool isLoggedIn = false;
    bool hasPermission = true;
    

Common mistakes with data types

❌ Using the wrong type for the data

// Bad: Using int for decimal values
int price = 19.99;  // Truncated to 19!

// Bad: Using char for numbers
char age = 25;      // Confusing - is this a number or character?

✅ Using appropriate types

// Good: Use double for decimal values
double price = 19.99;

// Good: Use int for age
int age = 25;

❌ Ignoring value ranges

// Potentially bad: What if the number is too big for int?
int worldPopulation = 8000000000;  // May not fit in int!

✅ Consider the range needed

// Better: Use long long for very large numbers
long long worldPopulation = 8000000000LL;

Type safety in C++

C++ is a strongly typed language, which means:

  • Every variable must have a declared type
  • You cannot assign incompatible types without conversion
  • The compiler checks types at compile time
#include <iostream>

int main()
{
    int number = 42;
    double decimal = 3.14;
    char letter = 'X';
    bool flag = true;
    
    // These work - compatible assignments
    number = 100;           // int to int
    decimal = 2.71828;      // double to double
    
    // These require conversion (will be covered in later lessons)
    number = decimal;       // double to int (truncates)
    decimal = number;       // int to double (promotes)
    
    std::cout << "Number: " << number << std::endl;
    std::cout << "Decimal: " << decimal << std::endl;
    
    return 0;
}

The sizeof operator

You can check the size of any data type or variable using sizeof:

#include <iostream>

int main()
{
    int x = 42;
    double y = 3.14;
    
    std::cout << "Size of int: " << sizeof(int) << " bytes\n";
    std::cout << "Size of double: " << sizeof(double) << " bytes\n";
    std::cout << "Size of variable x: " << sizeof(x) << " bytes\n";
    std::cout << "Size of variable y: " << sizeof(y) << " bytes\n";
    
    return 0;
}

Summary

Fundamental data types are the building blocks of C++ programs:

  • Integer types (int, short, long, long long) store whole numbers
  • Character type (char) stores single characters
  • Floating-point types (float, double, long double) store decimal numbers
  • Boolean type (bool) stores true/false values
  • Void type (void) represents "no value"

Choose the right data type based on:

  • The range of values you need to store
  • The precision required
  • Memory efficiency considerations
  • Performance requirements

Quiz

  1. What are the five main categories of fundamental data types in C++?
  2. Which data type would you use to store a person's age?
  3. Which data type would you use to store a bank account balance?
  4. What happens if you try to store a decimal number in an int variable?
  5. How can you find out how much memory a data type uses?

Practice exercises

Try creating variables of different types:

  1. Declare variables for: student ID (integer), GPA (decimal), letter grade (character), and graduated status (true/false)
  2. Use the sizeof operator to find the size of each fundamental data type on your system
  3. Create a program that demonstrates the difference between float and double precision
  4. Experiment with assigning values outside the typical range and observe what happens

Continue Learning

Explore other available lessons while this one is being prepared.

View Course

Explore More Courses

Discover other available courses while this lesson is being prepared.

Browse Courses

Lesson Discussion

Share your thoughts and questions

💬

No comments yet. Be the first to share your thoughts!

Sign in to join the discussion