The data types in C++ are-
1. Integer- The keyword used for integer data types is 'int'. Integers use data space of 2-4 bytes. If a number is too long, it is rounded off to the largest value possible. Integer data types support only numbers and can't be used for letters or special characters. For example-
#include <iostream> using namespace std; int main() { cout<<"Hello World! \n\n"; cout<<"Write an integer:"; int (x); cin>>x; cout<<"\nThe integer is:"<<x; }
2. Floating point numbers- Floating point numbers have the keyword 'float'. They are used to represent numbers with 6-7 decimal point. They generally require 4 bytes of data space. For example-
#include <iostream> using namespace std; int main() { cout<<"Hello World! \n"; cout<<"Write a decimal number:"; float (x); cin>>x; cout<<"\nThe floating point number is:"<<x; }
3. Void- A void data type represents no value. It uses the keyword 'void' and is used for those functions that do not return any value.
4. Boolean- Boolean data types store the values 'true' and 'false'. For example-
#include <iostream> using namespace std; int main() { cout << "Boolean data types.\n"; bool CppIsFun=true; cout<<CppIsFun; }
The output comes as 1 as true has the value 1 and false has the value 0.
5. Character- A character data type can be used to store numbers or letters. It has the keyword 'char' and uses 1 byte of memory space. A character data type needs to be surrounded by single quotes. For example-
#include <iostream> using namespace std; int main() { cout << "Characters.\n\n"; char (x); cout<<"Write a character:"; cin>>x; cout<<"The character is:"<<x; }
6. sizeof()- The sizeof() is an operator which is used to find the size of a particular data type or variable.For example-
#include <iostream> using namespace std; int main() { cout<<"sizeof() operator.\n\n"; cout<<"Size of an integer:"<<sizeof(int)<<" bytes"<<"\n"; cout<<"Size of a floating point number:"<<sizeof(float)<<" bytes"<<"\n"; cout<<"Size of a double floating point number:"<<sizeof(double)<<" bytes"<<"\n"; cout<<"Size of a character:"<<sizeof(char)<<" bytes"<<"\n"; cout<<"Size of a boolean:"<<sizeof(bool)<<" bytes"<<"\n"; }
That is all for this post. I shall be explaining about the other two categories of data types in the following posts. Follow this blog to learn more about coding.
Regards,
Aarav Iyer
References: