Home   |   About   |   Terms   |   Contact    
Read & Learn
 

Data Types, Character Data Types in C++

C++

☯ All C++ Articles     ☯ All Articles


A new product.





Popular Google Pages:




This article is regarding Data Types, Character Data Types in C++.
Last updated on: .



What is Data Type in C++?
Let a simple program:
#include <iostream>
using namespace std;
int flower = 6;
int wind = 3;
int bird;
float bud;
char nest = 'A';
bool result;
int main()
{
bird = flower + wind;
cout << "The total is:" << bird << endl;
return 0;
}

In the above example we have some statements like
int bird;
float bud;
char nest = 'a';
bool result;

Here the keywords int, float, char, bool are called Data Type.

In the int data type it will store integer values. Like 2 , 3, -2, -3 etc.

In the float data type it will store float values. Like 9.66, 7.23, 1.24 etc.

In the char data type it will store character values. Like a, A, B, c etc.

In the void data type it will store no values or empty values.

In the bool data type it will store only the value true or false. This variables are also called the Logical Variables.

So we can say that, the type of information that a variable can hold is called the Data Type.

Please Note:
As C++ is a case sensitive program hence we can not write the keywords int, float, char as Int, Float, Char or any other camel case character. We have to write this same as it is mentioned.

Character Data Types in C++

Details of Character Data Types in C++

We can declare a char variable with the statement either:
char nest = 'A';

Or

char nest('A');
Both has the same result.



◕ A character data type can store only One Byte of memory. In this one byte or memory range we can store either Integer or Character.
As a result we can say that, the char data type serves Dual Purpose.

Please Note:
we have use single quotation mark like ('A') to declare a character. Double quotation mark is only used for the string like "Beautiful Bird."

Another thing is that the character 'A' is represented in ASCII by the decimal value 65. Hence we can write the above statement as
char nest = 65;

All the following statements
char nest = 'A';
char nest('A');
char nest = 65;
will give us the same result.

Wide Character Type

We know that a char type can store only One Byte. But we can extend this range upto Two Bytes with the keyword wchar_t.

Let have an Example:
char nest = 'A';
wchar_t sea = L'S';




char nest = 'A';
Here the character nest will store only One Byte with the value range from 0 to 255 or -128 to 127.



wchar_t sea = L'S'
Here the character sea will store Two Bytes with the value range from 0 to 65,535.

Please note: There is a preceding character L before the character 'A'. This L tells the compiler that, this is a Wide Character Type and may store 2 Bytes of data.

We can write this Wide Character Type either of the following ways:
wchar_t sea = L'A';
wchar_t sea(L'A');

We can also declare this within the hexadecimal code like:
wchar_t sea(0x2A);





Popular Google Pages:





Top of the page

Amazon & Flipkart Special Products

   


Top of the page