top of page

The Basics of C++ Data Types: A Tutorial with Pankaj's Code

Welcome to "Codes with Pankaj," your trusted companion on the journey through programming. In this tutorial, we'll unravel the world of C++ data types, exploring their intricacies through examples. Whether you're a coding enthusiast or a beginner taking your first steps into programming, understanding data types is pivotal for writing robust and efficient C++ code.

Let's embark on this learning adventure with Pankaj!

1. Integer Types: Integers represent whole numbers in C++. Let's explore various integer types, such as int, short, long, and long long, and understand their differences through practical examples.


#include <iostream>
int main() {
    int age = 25;
    short distance = 100;
    long population = 1500000000;
    long long bigNumber = 9223372036854775807;

    std::cout << "Age: " << age << std::endl;
    std::cout << "Distance: " << distance << std::endl;
    std::cout << "Population: " << population << std::endl;
    std::cout << "Big Number: " << bigNumber << std::endl;

    return 0;
}

2. Floating-Point Types: Floats and doubles handle fractional numbers. Explore float, double, and long double through examples showcasing their precision.


#include <iostream>
int main() {
    float price = 19.99f;
    double pi = 3.14159265359;
    long double largePi = 3.141592653589793238;

    std::cout << "Price: " << price << std::endl;
    std::cout << "Pi: " << pi << std::endl;
    std::cout << "Large Pi: " << largePi << std::endl;

    return 0;
}

3. Character Types: Characters are fundamental in C++. Explore char and wchar_t through examples that demonstrate their usage.


#include <iostream>
int main() {
    char grade = 'A';
    wchar_t wideCharacter = L'W';

    std::cout << "Grade: " << grade << std::endl;
    std::wcout << "Wide Character: " << wideCharacter << std::endl;

    return 0;
}

4. Boolean Type: Discover the simplicity of the bool type, representing true or false values.


#include <iostream>
int main() {
    bool isRaining = true;

    std::cout << "Is it raining? " << std::boolalpha << isRaining << std::endl;

    return 0;
}

5. Enumeration Types: Define your constants using enum. Learn how to create and use enumerated types for improved code readability.


#include <iostream>
enum Color { RED, GREEN, BLUE };

int main() {
    Color selectedColor = GREEN;

    std::cout << "Selected Color: " << selectedColor << std::endl;

    return 0;
}

6. Void Type: Dive into the world of the void type, understanding its role in functions that do not return a value.


#include <iostream>
void printMessage() {
    std::cout << "Hello, World!" << std::endl;
}

int main() {
    printMessage();

    return 0;
}

By the end of this tutorial, you'll have a solid understanding of C++ data types, supported by hands-on examples. Get ready for more coding adventures with Pankaj!

Related Posts

See All
bottom of page