You are on page 1of 4

Intro

ThistutorialprovidesabriefintroductiontoC++datatypes.
Whatisadatatype?
WhenwewishtostoredatainaC++program,suchasawholenumberoracharacter,wehaveto
tellthecompilerwhichtypeofdatawewanttostore.Thedatatypewillhavecharacteristicssuchas
therangeofvaluesthatcanbestoredandtheoperationsthatcanbeperformedonvariablesofthat
type.
Fundamentaltypes
C++providesthefollowingfundamentalbuiltindatatypes:Boolean,character,integerand
floatingpoint.Italsoenablesustocreateourownuserdefineddatatypesusingenumerationsand
classes.
Foreachofthefundamentaldatatypestherangeofvaluesandtheoperationsthatcanbeperformed
onvariablesofthatdatatypearedeterminedbythecompiler.Eachcompilershouldprovidethe
sameoperationsforaparticulardatatypebuttherangeofvaluesmayvarybetweendifferent
compilers.
BooleanType
TheBooleantypecanhavethevaluetrueorfalse.Forexample:
boolisEven=false;
boolkeyFound=true;
IfaBooleanvalueisconvertedtoanintegervaluetruebecomes1andfalsebecomes0.
IfanintegervalueisconvertedtoaBooleanvalue0becomesfalseandnonzerobecomestrue.
CharacterType
ThecharactertypeisusedtostorecharacterstypicallyASCIIcharactersbutnotalways.For
example:
charmenuSelection='q';
charuserInput='3';
Notehowacharacterisenclosedwithinsinglequotes.Wecanalsoassignnumericvaluesto
variablesofcharactertype:
charchNumber=26;
Wecandeclaresignedandunsignedcharacters,wheresignedcharacterscanhavepositiveand
negativevalues,andunsignedcharacterscanonlycontainpositivevalues.
signedcharmyChar=100;
signedcharnewChar=43;
unsignedcharyourChar=200;

Notethatifweuseaplainchar,neithersignednorunsigned:
chardataValue=27;
itmaydifferbetweencompilersastowhetheritbehavesasasignedorunsignedcharactertype.On
somecompilersitmayacceptpositiveandnegativevalues,onothersitmayonlyacceptpositive
values.Refertoyourcompilerdocumentationtoseewhichapplies.
Acharisguaranteedtobeatleast8bitsinsize.C++alsoprovidesthedatatypewchar_t,awide
charactertypetypicallyusedforlargecharactersets.
AnarrayofcharacterscanbeusedtocontainaCstylestringinC++.Forexample:
charaString[]="ThisisaCstylestring";
NotethatC++alsoprovidesastringclassthathasadvantagesovertheuseofcharacterarrays.
IntegerTypes
Theintegertypeisusedforstoringwholenumbers.Wecanusesigned,unsignedorplaininteger
valuesasfollows:
signedintindex=41982;
signedinttemperature=32;
unsignedintcount=0;
intheight=100;
intbalance=67;
Likecharacters,signedintegerscanholdpositiveornegativevalues,andunsignedintegerscanhold
onlypositivevalues.However,plainintegercanalwaysholdpositiveornegativevalues,they're
alwayssigned.
Youcandeclaresignedandunsignedintegervaluesinashortenedform,withouttheintkeyword:
signedindex=41982;
unsignedcount=0;
Integervaluescomeinthreesizes,plainint,shortintandlongint.
intnormal=1000;
shortintsmallValue=100;
longintbigValue=10000;
Therangeofvaluesforthesetypeswillbedefinedbyyourcompiler.Typicallyaplainintcanholda
greaterrangethanashortint,alongintcanholdagreaterrangethanaplainint,althoughthismay
notalwaysbetrue.Whatwecanbesureofisthatplainintwillbeatleastasbigasshortintand
maybegreater,andlongintwillbeatleastasbigasplainintandmaybegreater.Ashortintegeris
guaranteedtobeatleast16bitsandalongintegeratleast32bits.
Youcandeclareshortandlongintegervaluesinashortenedform,withouttheintkeyword:
shortsmallValue=100;
longbigValue=10000;
Youcanhavelongandshortsignedandunsignedintegers,forexample:

unsignedlongbigPositiveValue=12345;
signedshortsmallSignedValue=7;
FloatingPointTypes
Floatingpointtypescancontaindecimalnumbers,forexample1.23,.087.Therearethreesizes,
float(singleprecision),double(doubleprecision)andlongdouble(extendedprecision).Some
examples:
floatcelsius=37.623;
doublefahrenheit=98.415;
longdoubleaccountBalance=1897.23;
Therangeofvaluesthatcanbestoredineachoftheseisdefinedbyyourcompiler.Typicallydouble
willholdagreaterrangethanfloatandlongdoublewillholdagreaterrangethandoublebutthis
maynotalwaysbetrue.However,wecanbesurethatdoublewillbeatleastasgreatasfloatand
maybegreater,andlongdoublewillbeatleastasgreatasdoubleandmaybegreater.
EnumerationType
Anenumerationtypeisauserdefinedtypethatenablestheusertodefinetherangeofvaluesforthe
type.Namedconstantsareusedtorepresentthevaluesofanenumeration,forexample:
enumweekday{monday,tuesday,wednesday,thursday,friday,saturday,sunday};
weekdaycurrentDay=wednesday;
if(currentDay==tuesday){
//dosomething
}
Thedefaultvaluesassignedtotheenumerationconstantsarezerobased,soinourexampleabove
monday==0,tuesday==1,andsoon.
Theusercanassignadifferentvaluetooneormoreoftheenumerationconstants,andsubsequent
valuesthatarenotassignedavaluewillbeincremented.Forexample:
enumfruit{apple=3,banana=7,orange,kiwi};
Here,orangewillhavethevalue8andkiwi9.
ClassType
Theclasstypeenablesustocreatesophisticateduserdefinedtypes.Weprovidedataitemsforthe
classtypeandtheoperationsthatcanbeperformedonthedata.Forexample,tocreateasquare
classthathasadataitemforsize,andprovidesdrawandresizeoperations:
classsquare{
intsize;
public:
square();
~square();
voiddraw();
boolresize(intnewSize);
};

[Pleaserefertoatutorialonclassesandobjectsforamoredetailedexplanationoftheclasstype,
whichisoutsidethescopeofthistutorial].
Bob@http://www.daniweb.com/techtalkforums/thread1767.html