char data type : unsigned char type and signed char type

char data type,signed char type and unsigned char type

Character data type also known as char data type is a built-in data type in C++.This type allows the programmer to use characters (the English alphabets,numbers and special characters) in their programs. The keyword of this data type is char and it has a size of 8 bits.If you want to use this data type simply write the keyword and declare a variable for it.For various conveniences char type is also divided into three types:char ,signed char and unsigned char data type.A simple program is given below which uses the char type and also it print out it’s size.

#include <iostream>

using namespace std ;

int main( )
char c=’N’ , c1 ; ///Note the single quotation
c1=c ;
cout<< c << ” , ” << c1 << endl ;
cout << sizeof(c) ;

cin.get() ;
return 0 ;


The output is

Note to assign a character literal to a char variable a single quotation is used.And the size 1 above means 1 byte,so a char type occupy only one byte.Considering the size of the char type it is in fact one of the smallest -another is bool type- data type in C and C++.

Link:Difference between signed and unsigned char data type.

Why is char considered as the smallest int type?

char data type is considered as the smallest int/integer data type. char type has the same internal format as the integer type.Basically this means the format of storing the char bits and integer bits are the same;they follow the base-2 binary format.This enables the char type to further sub-divide it into two types :signed char and unsigned char data type(more information about them is provided here Differences and uses : Signed and unsigned char type.) like the integer type.

If we literally consider a char type as an integer type then it can represent only 256(2^8) integers,this in turn will mean a char type can represent only 256 characters because an 8 bits can represent only 256 unique values;each character for each unique value.In our program we can utilize as many unique integer value as we desire- although it shouldn’t be larger than the maximum value representable by long long type(8 bytes, 64 bits)- but the number of characters is limited to 256.Our understanding of char type as smallest integer type make every characters interchangeable to integer type;in the sense that char type can represent an integer value and vice versa.So this allows every character to map to a unique integer value but the vice versa is not necessarily true because there are thousands of integers but only 256 characters to map to.So we can proudly state that many integers can point to the same character,not yet convinced? Look at the program below.

Link:Integer data type

#include < iostream >

using namespace std;

int main( )
char c1=36 , c2=56 , c3=78 , cc=292 ; /*integer value assigned to char */
char c4=’*’ , c5=’/’ , c6=’1′ ;
int i1=c4 , i2=c5 , i3=c6 ; ///char assigned to int

cout<< c1 << ” , ” << c2 << ” , ” << c3 << “,” << cc endl
  << i1 <<” , ” << i2 << “,” << i3 ;

cin.get( ) ;
return 0 ;


The output is,

$ , 8 , N , $
42 , 47 , 49

If we look at the output c1 and cc are assigned different integer but they give the same character as the output,whereas in case of characters assigned to int type variable all the integer output are different.To know which integers will map to which character we must be acquaint with certain pattern the compiler utilized to map integers to a character .The pattern is:a character would repeat itself after every 255(since 0 is included it is not 256) integer value.For instance 90 , 256+90 , (256*2)+90 , (256*3)+90 , … will have the same character value.But how do I know the 256 characters used by the integer to map to.Worry not,the character and it’s corresponding integer value are given in a chart known as ASCII chart and the chart is shown below.

char type ASCII chart
Fig. ASCII chart

**Note:ASCII chart consist of only the first 127 characters.Here,the remaining 128 characters are also shown if in case you want to refer them in your program.

If we know the chart conversion between integer and char type becomes much simpler.For instance in the program below,using the chart we will write a program to print out “Hello World!” using only integer value.

#include <iostream>

using namespace std ;

int main( )
char double_quotation=34 ,H=72 , e=101 , l=108 , o=111 ,
W=87 , r=114 , d=100 , space=32 ;

cout<< double_quotation << H << e << l << l << o << space
  << W << o << r << l << d << double_quotation << endl ;

cin.get( ) ;
return 0 ;


Assigning float value to char variable

int and char type go along pretty well,but what about floating point type.What happen if we assign a floating point value to a char variable. In this case,if the value has a decimal point value it will be truncated and the corresponding character of the truncated value will be the output and also if a character is assigned to floating point type variable the integer value of that character is the output.Look at the code below.

Link:Floating point data type.

char c=12.234 , c1=12 , c2=54.45 , c3=’M’ ;
float f=c3 ;

cout << c << “,” << c1 << “,” << c2 << “,” << f << endl ;

If you run the program c and c1 gives the same output.So,if float/real number is assigned to char variable it is treated as an integer and the fractional value-if found any-is removed.

Some points to note about char type

i)It is the smallest int type.

ii)It can represent only 256 characters consisting of English alphabets , 0-9 integers , special characters like ?,.$@(* etc. and some other characters.Now this means it cannot represent other languages script (say Russian,Hindi,Japanese,etc.) .This problem will be solved by Unicode and wchar_t type.

The char data type can be considered as signed char type or unsigned char type depending on your machine and compiler.If you want to use a signed char type do not use char type as it may be interpreted as signed type in some machine and unsigned in some other.To avoid any unexpected result from your program when running in different machine use signed char type explicitly if you need the signed type,in this way there is no chance for the machine to interpret it differently.The range of char data type however remains the same in all the machine.

Related Link

->Differences and uses : Signed and unsigned char type.
->C++ String type and it’s differences with char type
->Represent int and char in binary format.
->Using wchar_t to print out Japanese,Chinese,Russian in Console.