Data Type: A set of values together with a set of operations.
C++ data types fall into 3 categories:
- Simple data type
- Structured data type
- Pointers
Simple Data Types
They are three types of Simple Data Types
- Integral – which is a data type that deals with integers.
- Floating-Point – which is a data type that deals with decimal numbers.
- Enumeration Type – it is a user-defined data type.
Integral Data type:
- char
- short
- int
- long
- bool
- unsigned char
- unsigned short
- unsigned int
- unsigned long
Every Data type has a different set of values associated with it.
Data Type | Values | Storage(in bytes) |
int | -2147483648 to 2147483647 | 1 |
bool | true or false | 1 |
char | -128 to 127 | 1 |
Type Modifiers
The range of the data types can be modified by using the type modifiers.
unsigned: target data type will be represented in the unsigned representation.
signed: target data type will be represented in the signed representation.
short: target data type will have a width of at least 16 bits.
long: target data type will have a width of at least 32 bits.
long long: target data type will have a width of at least 64 bits.
int DATA TYPE
Integers in C++, as in mathematics, are numbers such as 10,-12,45,98574,-765….
Code:
C++ Code
#include<iostream>
using namespace std;
int main() {
int studentId = 87;
cout << "StudentId is " << studentId;
//to store the integer values we use int dataType
return 0;
}
Output: StudentId is 87
bool DATA TYPE
The boolean data type has only two values: true or false.
Code:
C++ Code
#include<iostream>
using namespace std;
int main() {
bool isStudentPresent = true;
if (isStudentPresent) {
cout << "Student is present";
} else {
cout << "Student is absent";
}
return 0;
}
Output: Student is present
char DATA TYPE
The data type char is the smallest integral data type. The char data type is used to represent characters.
Characters such as ‘a’, ‘&’, ‘S’, ‘*’
Code:
C++ Code
#include<iostream>
using namespace std;
int main() {
char sectionNumber = 'C';
cout << "The Student belongs to Section " << sectionNumber;
return 0;
}
Output: The Student belongs to Section C
Floating – Point Data type
Floating point data type deals with the decimal numbers.
C++ provides three floating-point data types.
- float
- double
- long double
float: The data type float is used to represent any real number between -3.4E+38 and 3.4E+38.
The memory allocated is 4 bytes.
Code:
C++ Code
#include<iostream>
using namespace std;
int main() {
float avgPercentage = 92.3;
cout << "The average Percentage of the student is " << avgPercentage << "%";
return 0;
}
Output: The average Percentage of the student is 92.3%
double: The data type double is used to represent any real number between -1.7E+308 and 1.7E+308.The memory allocated is 8 bytes.
C++ Program to illustrate the usage of data types
Code:
C++ Code
#include<iostream>
using namespace std;
int main() {
int student_ID = 102; //to store integers we use int.
char section_NO = 'A'; // to store characters we use char.
bool isPresent = true; //to store the boolean values we use bool.
float examScoreAverage = 92.76; //to store the decimal numbers we use float.
double distanceToSchool = 12345.76; //double is same as float but with extra bytes of memory to store larger values.
cout << "The size of int is: " << sizeof(int) << " Bytes" << endl;
cout << "The size of char is: " << sizeof(char) << " Bytes" << endl;
cout << "The size of bool is: " << sizeof(bool) << " Bytes" << endl;
cout << "The size of float is: " << sizeof(float) << " Bytes" << endl;
cout << "The size of double is: " << sizeof(double) << " Bytes" << endl;
cout << "The size of long double is: " << sizeof(long double) << " Bytes" << endl;
}
Output:
The size of int is: 4 Bytes
The size of char is: 1 Bytes
The size of bool is: 1 Bytes
The size of float is: 4 Bytes
The size of double is: 8 Bytes
The size of long double is: 16 Bytes