C++ more types :Void , bool, decimal, octal, hexadecimal, etc.

Some of the data types which we have discussed:

int,float,char,string

,etc. are used explicitly in C++ program but there are some other types which we may or may not use explicitly in our C++ program.Some of these types which we discuss here will come in handy while writing a C++ program and some others will help you in understanding some of the programming and non-programming(but computer) stuff.

i)Void type
ii)Bool type
iii)Decimal type
iv)Octal type
v)Hexadecimal

Link :C++ built-in data type


Void type

When we say ‘void‘ it means no type.In C++ program if void is used as a type it means it does not belong to any of the basic built-in type or user-defined or any other types discussed here.So if you want to use a type but not supporting any value then use void.The keyword for such type is void.It is used while returning value from function(discuss in Chapter 2 ).


Bool type

Bool type supports only two values:true and false,that means any variable of ‘bool’ type will have value either true or false.When ‘true’ is converted to int type it is same as the integer value 1 and ‘false’ is same as the integer value 0.Note that whenever you assigned an integer value to the bool variable any integer other than 0 is interpreted as true.The keyword of bool type is bool itself.

bool b1=true, b2=false , 
 b3=10002 , b4=0 ;

cout<< b1 << endl
<< b2 << endl
<< b3 << endl
<< b4 << endl ; 

The output,

1
0
1
0

The value assigned to b3 is 10002 which is not 0 so it has the value 1(true).


Decimal type

Decimal type value are represented from the value 0 to 9,for instance 8549349-the number consist of digits only from 0 to 9.Note decimal value is same as int type value,so integer and decimal may be used interchangeably in C++.There is no specific keyword for decimal type,if you want to use a decimal type you can use the int type.


Octal type

Octal type number are represented from the digit 0 to 8,for instance 743812.We cannot include the integer 9 to represent the value of such type.For instance 398270 is an error.

There is no specific keyword for such type in C++ so they cannot be used explicitly in our program.You can Google for more information on how to use this type in numbering system.


Hexadecimal type

Hexadecimal type uses the integer value from 0 to 9 and a,b,c,d,e,f(can be upper case also:meaning capital letter) to represent it’s value.The a,b,c,d,e,f alphabet represent the value 11,12,13,14,15.To give you an example of hexadecimal number if we were to convert the number 84954 in hexadecimal it would be written as 0x14BDA‬ (or 0x14bda they are same) note the B,D and A letter.How to convert the integer to hexadecimal can be found in wikipedia Hexadecimal.

*Note this type is use to represent the memory address in our Computer.

There is no specific keyword for such type in C++ so they cannot be used explicitly in C++ program.However,that does not mean we cannot convert an integer to hexadecimal number.The program given below shows how to convert an integer to decimal,octal and hexadecimal type.

#include <iostream>

using namespace std;

int main( )
{
cout << dec << 547458 << endl //Prints out decimal value of 547458
    << hex << 1947898 << endl //Prints out hexadecimal value of 1947898
    << oct << 832679 ; //Prints out octal value of 832679

cin.get() ;
return 0 ;
}

The output is,

547458
1db8fa
3132247

The second output has no ‘0x’ in front but it is a hexadecimal value for sure.




Leave a Reply

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