float
and double
: Both these data types store floating point values such as 1.56
, 2.87
, -4.7
, etc. The difference between these two data types is the size. We can store up to 8 bytes in a double
variable while only 4 in a float
variable.
float f = 5.65;
double d = 7.8987;
bool
: This is used to store two states: true
/1
or false
/0
. If we want to store a number with a more significant value, our choice would be double
.
bool b = true;
// or we can also declare bool as this
bool b = 0;
char
: This is used to store a single character value such as a
, A
, #
, etc.
char c = 'A';
string
: This is used to store text such as Welcome to Educative
, @Answers
, etc.
string s = "This is a string";
Except for the last data type, “string,” which is only accessible in the string
library, all the data types mentioned above are available in the iostream
library. String usage requires adding #include <string>
at the beginning of the code.
#include <iostream>#include <string>using namespace std;int main(){int number = 5;float decimal1 = 5.87;double decimal2 = 87.0987;bool state1 = true;bool state2 = 0;char letter = 'A';string text = "Welcome to Educative";cout << "The integer value is: " << number << endl;cout << "The float value is: " << decimal1 << endl;cout << "The double value is: " << decimal2 << endl;cout << "We can write boolean value in two forms as: " << state1 << " and " << state2 << endl;cout << "The character value is: " << letter << endl;cout << "The string value is: " << text << endl;return 0;}
Free Resources