Prev Next
What are Data types?
Data types are used to define the variables that the same of type data it can store in memory. The data types determine the type of data to be stored in memory. The data types are used to represent the different values to be stored in the variable. The variable is a name that refers the memory location which means whenever you create a variable you reserve the space in the memory and based on the data type of the variable the operating system allocates the memory.
For example :
1 2 3 |
int a; a= 5; // value of variable |
So, in the above example the variable name is ‘a’ and the data type assigned to this variable is integer which means the variable ‘a’ can store only the integer values.
Types of Data types
In C++ the data types are divided into two types.
Primitive Data types :
The primitive data types are built-in data types or you can say predefined data types and they can be used by user to declare variables.
The primitive data types are :
- Integer
- Character
- Floating Point
- Double Floating Point
- Boolean
- Void
Let us discuss these data types briefly.
Integer
For the integer data type ‘int’ keyword is used. This data type requires 4 bytes of memory space and it ranges from the minimum value is -2,147,483,648 and the maximum value is 2,147,483,647, both the values are inclusive.
Let us have a look at the example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> using namespace std; int main () { int x, y, res; x = 10; y = 20; res = x + 1; res = x - y; cout << res; return 0; } |
Character
The character data type is used to store characters or a single character. For character data type ‘char’ keyword is used. This data type requires 1 byte of memory space and it ranges from -128 to 127 or 0 to 255.
Let us have a look at the example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<iostream> using namespace std; int main() { char ch='a'; cout<<"\nCharacter entered is: "<<ch; return 0; } |
Floating Point
This data type is used for storing single precision floating point values or decimal values. For floating point data type ‘float’ keyword is used. This data type requires 4 byte memory space.
Let us have a look at the example :
#include<iostream.h>#include<conio.h>void main(){ clrscr(); float fl; cout<<“\n\nEnter a floating-point number: “; cin>>fl;getch();}
Double Floating Point
This double floating point data type is used to store double precision floating point values or decimal values. For double floating point data type ‘double’ keyword is used. This data type requires 8 byte of memory space.
Let us have a look at the example :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> using namespace std; int main() { double a = 400.00; cout << "The double precision floating point is:"<<a; return 0; } |
Boolean
This data type is used for storing Boolean or logical values. The Boolean variable can store either true or false value. For this data type ‘bool’ keyword is used.
Let us have a look at the example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include<iostream> using namespace std; int main() { int x = 10, y = 20, m = 2; bool a, b; a = x == y; // false b = x < y; // true cout << "Value of a is = " << a << "\n"; cout << "Value of b is = " << b << "\n"; bool c = true; if (c) cout << "Yes" << "\n"; else cout << "No" << "\n"; int z = false + 5 * m - c; cout << z; return 0; } |
Void
The void data type refer the set of empty values. This data type is used with those function which does not returns a value.
Let us have a look at the example :
#include<iostream.h>#include<conio.h>void main(){ clrscr(); float fl; cout<<“\n\nEnter a floating-point number: “; cin>>fl;getch();}
What are data type modifiers?
The data type modifiers are used with built-in data types to modify the length of data which a particular data type can hold.
Some of the data type modifiers available in C++ are :
- Signed
- Unsigned
- Short
- Long
With integer base data types you can use signed, unsigned, long and short modifiers. And with char you can use signed and unsigned whereas with double data type you can use long. The signed and unsigned modifiers can also be used as prefix to long or short modifiers.
Data Type / Size(in bytes) | Range |
short int / 2 | -32,768 to 32,767 |
unsigned short int / 2 | 0 to 65,535 |
unsigned int / 4 | 0 to 4,294,967,295 |
int / 4 | -2,147,483,648 to 2,147,483,647 |
long int / 4 | -2,147,483,648 to 2,147,483,647 |
unsigned long int / 4 | 0 to 4,294,967,295 |
signed char / 1 | -128 to 127 |
unsigned char / 1 | 0 to 255 |
float / 4 | |
double / 8 | |
long double / 12 | |
wchar_t / 2 or 4 | 1 wide character |
Let us have a look at the example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; int main() { cout << "The size of char : " << sizeof(char); cout << "\nThe size of int : " << sizeof(int); cout << "\nThe size of short int : " << sizeof(short int); cout << "\nThe size of long int : " << sizeof(long int); cout << "\nThe size of float : " << sizeof(float); cout << "\nThe size of double : " << sizeof(double); cout << "\nThe size of wchar_t : " << sizeof(wchar_t); return 0; } |