Before discussing data types in C++, you should know the word datatype. So what is a data type? It is the type of data that a variable can store in the form of value. It can be of any kind, i.e., numerical data, textual data, character data, etc. I hope you got it. If you still have any queries related to datatypes, comment below. Now let’s discuss datatypes in C++.
There are several data types in C++ look below:
Data type | Size of Datatype | Description of data type |
---|---|---|
int | 2 or 4 bytes | Stores whole numbers, without decimals / positive or negative |
float | 4 bytes | Stores fractional numbers, containing one or more decimals. Sufficient for storing 6-7 decimal digits |
double | 8 bytes | Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits |
char | 1 byte | Stores a single character/letter/number, or ASCII values and it is surrounded by single comma like ‘a’ |
boolean | 1byte | Stores true or false values or on or off like |
Declaring Variable with data types in C++ with output
#include <iostream> #include <string> using namespace std; int main () { // Creating variables and storing value depend on the data type int myNum = 500; // Integer (whole number) float myFloatNum = 5.999; // Floating point number double myDoubleNum = 9.99998; // Floating point number char myLetter = 'A'; // Character bool myBoolean = true; // Boolean string myString = "Codelikechamp.com"; // String // Print variable values cout << "int: " << myNum << "\n"; cout << "float: " << myFloatNum << "\n"; cout << "double: " << myDoubleNum << "\n"; cout << "char: " << myLetter << "\n"; cout << "bool: " << myBoolean << "\n"; cout << "string: " << myString << "\n"; return 0; }
Output
int: 500 float: 5.999 double: 9.99998 char: A bool: 1 string: Codelikechamp.com
Link: https://Codelikechamp.com
Medium Link: Follow me on Medium
Linkedin Link: Follow me on Linkedin