C++ Boolean Char String - Data Types

In this Chapter, We will talk about C++ Boolean, Char, String data types. So, our mission is to teach you like that, you will be able to understand these data types easily.

There are many data types in C++

We also teach you about Numeric Data type in the previous chapter series. But now, we talk about char data type.

Character Types

The char data type is using to store a single character. So, the character much is surrounded by a single quote, like ‘A’ or ‘b’.

So, see the Example:

#include <iostream>
using namespace std;

int main(){
    char myGrade = 'A';
    cout << myGrade;
    return 0;
}

Then, see the Output:

A

Remember: In c++, the char data type has deference between CAPITAL and small letters. So, use it carefully.

So, Alternatively, you can use ASCII values to display several characters.

Then, look at the Example:

#include <iostream>
using namespace std;

int main(){
    char a1 = 'A', b2 = 'b', c3 = 'c';
    cout << a1 << endl;
    cout << b2 << endl;
    cout << c3 << endl;
    return 0;
}

So, here is Output:

A
b
c

So, the C++ Boolean Type of C++ (Boolean Char String)

A boolean data type declares with bool keyword and you will get the return value is true or false. then the value is returns, true = 1, and false = 0.

So, see Example:

#include <iostream>
using namespace std;

int main(){
    bool isAnswerCorrect = true;
    bool isAnswerWrong = false;
    cout << isAnswerCorrect << endl;
    cout << isAnswerWrong  << endl;
    return 0;
}

Then, See Output:

1
0

So, 1 use for true and 0 use for false.

The Boolean values are using mostly for conditional testing, and you will learn more about it in the first paragraphs.

So, Here star the “C++ String” Topic

The string data type is using to store a set of or a sequence of characters (text). So, this type is not a built-in, but it behaves like a one in its most basic usability. String values is surrounded by double quotes, like see Example.

So, see the Example:

#include <iostream>
using namespace std;

int main(){
 	string myGreeting = "Hello Dear!";
 	cout << myGreeting;
    return 0;
}

Then, see the Output:

Hello Dear!

So, if you want to include additional header file <string>. then, Lets do this in Example.

Then. here is Example:

#include <iostream>
#include <string>  // Include the string library 
using namespace std;

int main(){
 	string Greeting = "Hello!"; 		// the Creation a string variable
 	cout << Greeting;				// the Output string variable
    return 0;
}

So, see the Output:

Hello!

So, peoples, you will learn more about string. In our next chapter C++ Strings.

Summary of Today Topic C++ Boolean Char String

So, Students! today we have covered the three data types of C++ Boolean, Char and String, and talk the Numeric data type in previously. and we hope you are happy with learning C++.

By Usama Arshad

It's me Usama Arshad working as a Lecturer C++. I am a student of BSIT.

Leave a Reply

Your email address will not be published. Required fields are marked *