What is Data Type?
We store variables in the computer’s memory while programming, but the computers must know the type of data we wish to store in them. The data type tells the compiler what kind of data it is dealing with, whether an integer, a character or a floating-point number. What will the variable store, and what will the value range be?
Data types are variable declarations in C++. The type and size of data associated with variables are determined by a data type.
Consider the following simple example;
int marks = 427;
In the above example, marks
is a variable of type int
. It means that the variable marks
can only store integer values.
Fundamental Data Types in C++
The following table lists the most common data types, their definitions, and their sizes (in bytes):
Data Type | Name | Size | Range |
---|---|---|---|
Character | char | 1 Byte | Signed: -128 to 128 |
Integer | int | 4 Bytes | 2147483648 to 2147483647 |
Floating Point | float | 4 Bytes | 3.4 x 10 -38 to 3.4 x 10 38 |
Boolean | bool | 1 Byte | true (1) or false (0) |
Void (Empty) | void | "nothing" or "no value" | 0 |
You can also download a PDF of the C++ Data Types comprehensive and more detailed table.
The Data Types In C++ are divided into;
- Primitive Data Types
- Derived Data Types
- Abstract or User-Defined Data Types
In this class, we will only focus on Primitive Data Types.
Primitive Data Types
These data types are built-in or predefined data types that can be used to declare variables directly by the user. For example, int, char, float, and bool
. The following primitive data types are available in C++;
- Integer
- Character
- Floating Point
- Boolean
- Unsigned
- Void
#1. Integer Data Type;
Integer data types only store integer numbers such as 100, -200, 340
. It has the following subtypes:
- int
The keyword int represents integer data types. Integers have two bytes
memory (16 bits). A 16-bit number can range from -215
to 215 -1
, which means it can hold values in the range of -32768 to 32767
.
- short int data type
In C++, the short integer data type is declared with a short int
. The short int data type has a maximum size of 2 bytes (16 bits)
. It may range from-215 to 215 -1
, which means it can store values between -32768 and 32767
.
- long int data type
The long int data type in C++ is used to store more significant integer values. It has a storage capacity of 4 bytes (32 bits), allowing it to store values in the range of -2147483648 to 2147483647
.
#2. Character Data Type;
In C++, the char
keyword represents the character data type. A character data type is any character that belongs to the ASCII character set. The char
data type has an 8-bit
maximum capacity (1 byte). It has two different types: signed char and unsigned char.
Both of them consume the same amount of RAM. Consider the following line of code:
char x;
In the above example, x
is a character type variable that takes up 1 byte
of memory. Character constants are stored in variables of the char
data type. Character constants are indicated by single quotations.
#3. Floating Point Data Type;
Although integers are only intended to store whole numbers, we sometimes need to store huge numbers or values that include a decimal point. Floating-point data types are data types that are used to store such types of data.
The keyword float
is used to represent the floating-point data type. Real numbers, such as 4.0, 4.99, 3.911,
etc., are stored in floating-point variables. Floating point data can be divided into three categories:
- float
The float data type is used in C++ to declare floating-point types of variables for storing decimal values. To store values, it requires 4 bytes
(32 bits) of RAM.
- double data type
In C++, the keyword double is used to express double-precision floating-point values. The double data type takes up 8 bytes
of memory (64 bits). The double data type’s size is a rational integer in the same range as the long float, and it is stored as a floating-point representation with binary mantissa and exponent.
- long double data type
The long double data type is used in C++ to declare long double types of variables, which represent long double-precision floating-point values. It requires a total of 10 bytes
(80 bits) of memory in the RAM.
#4. Boolean data type;
The keyword bool
is used to represent the Boolean data type. It can only have one of two values: true 1
or false 0
. It takes up one byte of memory.
#5 – Unsigned data type;
The data types described so far are signed data types, which means that one bit
is designated for the value’s sign (i.e., +/-)
. Unsigned numbers are whole numbers that always have positive values from 0
to their most extensive range. The value is likewise represented by the sign bit
, implying that no bit is set aside for holding a sign (+/-)
. Unsigned numbers can be divided into four categories:
- unsigned char
- unsigned integer
- unsigned short integer
- unsigned long integer
#6. Void (Valueless);
The term "void"
refers to something that has no value
. The void data type represents a valueless entity. The void data type is used for functions that do not return
a value.
//Permitive Data Type Example; #include <iostream> #include <conio.h> using namespace std; int main() { int a= 5; float b= 1.53; double c= 3.14156; char ch= 'M'; bool y= false; cout<<"The value of a= "<<a<<endl; cout<<"The value of b= "<<b<<endl; cout<<"The value of c= "<<c<<endl; cout<<"The value of ch= "<<ch<<endl; cout<<"The value of y= "<<y<<endl; getch (); return 0; }
OUTPUT OF THE PROGRAM
The value of a= 5
The value of b= 1.53
The value of c= 3.14156
The value of ch= M
The value of y= 0