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 Type | Size | Description |
int | 2 or 4 bytes | Stores whole numbers, without decimals |
float | 4 bytes | Stores fractional numbers, containing one or more decimals. They require 4 bytes of memory space. |
double | 8 bytes | Stores fractional numbers, containing one or more decimals. They require 4 bytes of memory space. |
char | 1 byte | Stores a single character/letter/number, or ASCII values |
boolean | 1 byte | Stores 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'