Predefined Datatypes with C++

Hello and welcome back, Today I will be teaching you about the five different pre-defined datatypes in C++.

string text = "Coding with Ammar"; //string datatype

First of all, we have a string datatype. As you can see the string datatype is typed with double quotation marks which is very important. We can type whole sentences with strings!

char letter = 'A'; //character datatype

Second, we have the character datatype typed as char. As you can see the character datatype is typed with single quotation marks and only one letter can be valid for this datatype.

int num = 17; //integer datatype

The third datatype is an integer. It stores only whole numbers like 42,18 etc. We can perform mathematical operations with the integer datatype.

bool flag = true; //boolean datatype | only has two values true(1) or false(0)

Fourth we have the boolean datatype which can only store two values true(1) and false(0). True is denoted with 1 and False as 0.

float dec1 = 4.5; // float datatype
double dec2 = 4.52668; //same as float but with longer decimal point

Next, we have our final datatype float which can store numbers with decimal values. Only stores up to 6-7 decimal digits. Double is a variation of float. It can store decimal digits up to 15 and above.

Here are the output examples.

Coding with Ammar
A
17
1
4.5
4.52668

We can also set the precision of our decimal points during output. For eg, if I set the precision to 3 this will be the output. It will also round up the decimal digits.

// For decimals we can change position of decimals
cout.precision(3);
cout<<"3 decimal place \n";
cout<<dec1<<endl;
cout<<dec2<<endl;

Here is the output.

3 decimal place 
4.5
4.53

This is the whole code.

#include<iostream>
using namespace std;
int main(){
    string text = "Coding with Ammar"; //string datatype
    char letter = 'A';
    int num = 17; //integer datatype
    bool flag = true;//boolean datatype only has two values true(1) or false(0)
    float dec1 = 4.5; // float datatype
    double dec2 = 4.52668; //same as float but with longer decimal point 
    cout<<text<<endl;
    cout<<letter<<endl;
    cout<<num<<endl;
    cout<<flag<<endl;
    cout<<dec1<<endl;
    cout<<dec2<<endl;
    // For decimals we can change position of decimals
    cout.precision(3);
    cout<<"3 decimal place \n";
    cout<<dec1<<endl;
    cout<<dec2<<endl;
}

Here is the video on this topic.