Data types and Constants in C++

Posted on November 28, 2023 by Vishesh Namdev
Python C C++ Java
C++ Programming

Data type specifies what kind of information a variable can hold and how it can be manipulated. It helps the computer understand the nature of the data, like whether it's a whole number, a decimal, a character, etc. C++ has built-in data types for handling different types of information, such as integers, floating-point numbers, characters, booleans, and more.

Data types are mainly divided into following types

Data types can be broadly categorized into the following types:

Fundamental Data Types:

(a) Integer Types (int, short, long, long long)
(b) Floating-Point Types (float, double, long double)
(c) Character Types (char, wchar_t, char16_t, char32_t)
(d) Boolean Type (bool)
(e) Void Type (void)

Derived Data Types:

(a) Pointer Types (type*)
(b) Array Types (type arrayName[size])
(c) Reference Types (type&)
(d) Function Types (type functionName(parameters))

User-Defined Types:

(a) Structures (struct)
(b) Classes (class)
(c) Unions (union)

This table is of those data types which are most common and this table contains their size and their description.

Data types in C++

Simple example of Data type:-

#include<iostream>Copy Code

int main() {
    // Integer types
    int age = 25;

    // Floating-point types
    float pi = 3.14f;

    // Character types
    char letter = 'A';

    // Boolean types
    bool isTrue = true;
    bool isFalse = false;

    // Output values
    std::cout << "Age: " << age << std::endl;
    std::cout << "Pi: " << pi << std::endl;
    std::cout << "Letter: " << letter << std::endl;
    std::cout << "Is True? " << std::boolalpha << isTrue << std::endl;

    return 0;


A constant is like a box that holds a value, and once you put something inside that box, it's stuck there – you can't change it later. Constants are used for values that should stay the same throughout a program, making the code easier to understand and preventing accidental changes to important values.
The const keyword is used to declare a constant variable

Constant in C++
Example of Constant:
#include<iostream>Copy Code

int main() {
    const int MAX_VALUE = 100;
    const double PI = 3.14159;

    // Attempting to modify a constant will result in a compilation error
    // MAX_VALUE = 200; // Error: assignment of read-only variable

    std::cout << "Max Value: " << MAX_VALUE << std::endl;
    std::cout << "PI: " << PI << std::endl;

    return 0;
Max Value: 100
PI: 3.14159