C++ Data Types
Data types are the type of data associated with the operation to handle it. The data type tells us about the size and type of the information that a varialbe can store.
Fundamental data types are those which are not composed of any other data types. There are five fundamental data types in C++ :
Data Types | Description | Example |
---|---|---|
int | Integers are the numbers without any fractional part. The size of int is 4 bytes. |
int dt = 10; |
float | A number with fractional part known as floating-point number. Sufficient for storing 7 decimal digits. The size of float is 4 bytes. |
float dt = 5.75; |
double | Double is also used to handle floating-point number. Sufficient for storing 15 decimal digits. The size of double is 8 bytes. |
double dt = 14.57; |
boolean | Boolean data type is used to stores either true or false values. The size of boolean is 1 byte. |
bool dt = true; |
char | Character data type is used to store a single character / letter / number, or ASCII values. The size of char is 1 byte. |
char dt = 'B'; |
Data Type Modifiers
The fundamental data types have various modifiers, preceding them. The list of modifiers are:
signed unsigned long short
Types | Approximate size | Minimal Range |
---|---|---|
char | 1 byte | -127 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -127 to 127 |
short int | 2 byte | -32768 to 32767 |
unsigned short int | 2 byte | 0 to 65,535 |
signed short int | 2 byte | -32768 to 32767 |
int | 4 byte | -2147483648 to 2147483647 |
unsigned int | 4 byte | 0 to 4294967295 |
signed int | 4 byte | -2147483648 to 2147483647 |
long int | 4 byte | -2,147,483,648 to 2,147,483,647 |
signed long int | 4 byte | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 4 byte | 0 to 4,294,967,295 |
float | 4 byte | +/- 3.4e +/- 38 (~7 digits) |
double | 8 byte | +/- 1.7e +/- 308 (~15 digits) |
long double | 8 byte | +/- 1.7e +/- 308 (~15 digits) |
wchar_t | 2 or 4 byte | 1 wide character |
Example
The example will show the correct size of the data type.
#include <iostream> using namespace std; int main() { cout << "Size of char : " << sizeof(char) << endl; cout << "Size of int : " << sizeof(int) << endl; cout << "Size of short int : " << sizeof(short int) << endl; cout << "Size of long int : " << sizeof(long int) << endl; cout << "Size of float : " << sizeof(float) << endl; cout << "Size of double : " << sizeof(double) << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; return 0; }
Output
The ouput of the following code is :
Size of char : 1 Size of int : 4 Size of short int : 2 Size of long int : 4 Size of float : 4 Size of double : 8 Size of wchar_t : 4
Exercise:-
1. What is the size of wchar_t in C++?
View Answer
2. Which datatype is used to represent the absence of parameters?
View Answer
Program :-
C++ Program to Check Whether Number is Even or Odd
#include <iostream> using namespace std; int main() { int n; cout << "Enter an integer: "; cin >> n; if ( n % 2 == 0) cout << n << " is even."; else cout << n << " is odd."; return 0; }
Output :
Enter an integer: 23 23 is odd.
Visit :
Discussion