C++ Data Types & Constants

Data Types in C++

Data types specify the kinds of data that a variable can store; for instance, an integer variable can store data of that kind, a character variable can store data of that type, etc.

In C++, there are three categories for data types:

Built-in data types

These pre-defined data types for a language could be used right away by the programmer.

Int, Float, Char, Double, and Boolean are a few examples.

User-defined data types

The user itself defines these data kinds. Class, Struct, Union, and Enum are some examples.

Derived data Type

The basic built-in data types are the ancestors of these data types.

Array, Pointer, and Function are some examples.

Some of the popular built-in data types and their applications are:

Data TypeSizeDescription
int2 or 4 bytesStores whole numbers, without decimals
float4 bytesStores fractional numbers, containing one or more decimals. They require 4 bytes of memory space.
double8 bytesStores fractional numbers, containing one or more decimals. They require 4 bytes of memory space.
char1 byteStores a single character/letter/number, or ASCII values
boolean1 byteStores true or false values

Constants in C++

Constants are immutable; once they are initialised in a program, a constant variable’s value cannot be changed.

#include <iostream>
using namespace std;
 
int main()
{
    const float PI = 3.14;
    cout << "The value of PI is " << PI << endl;
    PI = 3.00; //error, since changing a const variable is not allowed.
}

Output:

error: assignment of read-only variable 'PI'
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in