C++ Data Types Examples

Welcome to The Coding College! Understanding and working with data types is essential in C++. In this tutorial, we’ll explore examples of C++ data types to help you see how each type is declared, initialized, and used in real-world scenarios.

What Are Data Types in C++?

Data types define the type of data a variable can hold. For example:

  • Integer types: Whole numbers (e.g., int, long).
  • Floating-point types: Numbers with decimal points (e.g., float, double).
  • Character types: Single characters (e.g., char).
  • Boolean types: Logical values (true or false).

Examples of Common Data Types

1. Integer (int)

Example: Declaring and Initializing an Integer

#include <iostream>
using namespace std;

int main() {
    int age = 25;  // Declare and initialize
    cout << "Age: " << age << endl;

    return 0;
}

Output:

Age: 25  

💡 Note: The size of int is typically 4 bytes, holding values from -2,147,483,648 to 2,147,483,647.

2. Floating-Point (float and double)

Example: Working with Decimals

#include <iostream>
using namespace std;

int main() {
    float temperature = 36.5;  // Float for single-precision
    double pi = 3.14159265359;  // Double for higher precision

    cout << "Temperature: " << temperature << endl;
    cout << "Value of Pi: " << pi << endl;

    return 0;
}

Output:

Temperature: 36.5  
Value of Pi: 3.14159  

💡 Tip: Use double for high-precision calculations and float for lower precision to save memory.

3. Character (char)

Example: Storing Single Characters

#include <iostream>
using namespace std;

int main() {
    char grade = 'A';  // Use single quotes for characters
    cout << "Grade: " << grade << endl;

    return 0;
}

Output:

Grade: A  

💡 Note: A char variable stores a single character or its ASCII equivalent in 1 byte.

4. Boolean (bool)

Example: Logical Values

#include <iostream>
using namespace std;

int main() {
    bool isCodingFun = true;  // true = 1, false = 0
    cout << "Is coding fun? " << isCodingFun << endl;

    return 0;
}

Output:

Is coding fun? 1  

💡 Tip: Booleans are commonly used in conditional statements like if and while.

5. String (string)

Example: Storing Text

#include <iostream>
#include <string>  // Required for string
using namespace std;

int main() {
    string name = "Alice";
    cout << "Name: " << name << endl;

    return 0;
}

Output:

Name: Alice  

💡 Tip: Use string for text instead of character arrays for more flexibility.

6. Constant (const)

Example: Declaring Constants

#include <iostream>
using namespace std;

int main() {
    const float PI = 3.14159;  // Constant value
    cout << "Value of Pi: " << PI << endl;

    return 0;
}

Output:

Value of Pi: 3.14159  

💡 Note: Use const to declare variables whose values should not change.

7. User Input with Data Types

Example: Getting Input for Different Data Types

#include <iostream>
#include <string>
using namespace std;

int main() {
    int age;
    float salary;
    string name;

    cout << "Enter your name: ";
    getline(cin, name);  // Input a full line for the name
    cout << "Enter your age: ";
    cin >> age;
    cout << "Enter your salary: ";
    cin >> salary;

    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
    cout << "Salary: $" << salary << endl;

    return 0;
}

Output:

Enter your name: John Doe  
Enter your age: 30  
Enter your salary: 50000.5  
Name: John Doe  
Age: 30  
Salary: $50000.5  

Type Modifiers Examples

C++ supports type modifiers to extend or restrict the range of data types.

ModifierDescriptionExample
signedAllows negative and positivesigned int x = -10;
unsignedOnly positive valuesunsigned int x = 10;
shortSmaller range of integersshort x = 100;
longLarger range of integerslong x = 100000L;

Example: Using Type Modifiers

#include <iostream>
using namespace std;

int main() {
    unsigned int positiveNumber = 100;
    signed int negativeNumber = -50;
    long largeNumber = 100000L;

    cout << "Positive Number: " << positiveNumber << endl;
    cout << "Negative Number: " << negativeNumber << endl;
    cout << "Large Number: " << largeNumber << endl;

    return 0;
}

Output:

Positive Number: 100  
Negative Number: -50  
Large Number: 100000  

Best Practices for Working with Data Types

  1. Choose Appropriate Data Types: Avoid wasting memory; use float instead of double or short instead of int when precision or range isn’t critical.
  2. Use Constants Where Needed: Use const for values that shouldn’t change during program execution.
  3. Initialize Variables: Always initialize variables to avoid undefined behavior.
  4. Check Ranges: Ensure the variable values stay within the range of the chosen data type.

Learn More at The Coding College

This tutorial covered examples of data types in C++. To practice and expand your knowledge, visit The Coding College for more hands-on exercises and guides.

What’s Next?

  • Dive into advanced topics like type casting, dynamic memory, and structs.

Leave a Comment