You are on page 1of 10

Dat

aType i
stheclassi
fi
cat
ionofthedatat
hati
stakenasi
nput
,pr
ocessed,
andr
esul
ts
i
nanoutput.I
tishowwecat egor
izedat
aaccor
dingtoi
tst
ype.
Ther
ear
ethr
eet
ypeofDat
aTy
pes:

 Pr
imarydataty
pes
 Secondar
ydatatypes
 User-
defi
neddatatypes

Thesedataty
pesaref
urt
hersubdi
vi
dedi
ntosev
eral
otherdat
aty
peswhi
chy
oucanf
ind
i
nthefigur
ebelow:

Dat
aty
pesi
nC

1.Pr
imar
yDat
aTy
pes
Pri
marydataty
pes,al
soknownas‘pri
miti
vedat
at y
pe’ 
or‘
fundamentaldat
atype’
,ar
e
thebui
l
t-i
ndataty
pesthatar
eprovi
dedbythe 
programminglanguage.I
tdefi
nest
he
mostbasicdat
ali
keint
,char
,fl
oat
,etc.
Primarydataty
pescoul dbeofsev er
altypesl i
kean intcanbeunsignedint
,shortint
,
unsignedlongi
nt ,
 
etc.Withsuchawi derangeofcl assif
icat
ionandvari
ety,
the
programmerhasgotmanydi ffer
entdatat y
pest ochoosef r
om aspertherequirement
andusei ti
ntheircodealongwi t
htheadv antageofthesecondarydatatypes. 
Her
ear
ethemostcommonl
yusedpr
imar
ydat
aty
pes:
 boolean
 byte
 char
 short
 i
nt
 l
ong
 fl
oat
 double


Void’
 
isanotherpri
marydatatypet
hatmeans‘
novalue’
.Iti
susual
lyusedt
odefi
nethe
t
ypeofretur
nv al
ueinafuncti
on.Afunct
ionwi
tha 
v d 
oi r
etur
ntypedoesnotr
etur
nany
v
alue.

1.
1.I
ntegerDat
aTy
pe
Int
egerdat aty pesareusedt odefi
nethev ari
ablestakingint
egerv alueswi thorwithout
constantvaluesgi ventothem.Themostcommonl yusedkey wordort hedatatypeused
todefinetheint egertypedatais 
‘i
nt’
.Therear eotherdataty
pes
l
ike ‘
short’
 
and  ‘l
ong’ 
usedt odefi
neintegervaluesbuttheyhav ediff
er entranges(we
usethem aspert herequirementofthepr ogram i
.e.ifwearesur ethatourr equi
rement
i
ssmal l
andi twi l
lnevergobey ondtherangeofsmal lweshalltakesmal l
andl i
kewise)
.
Inthetutori
al constants,wesawt hattherangeoft heconstantsdifferedfordiff
erent
compi l
ers.
Si
milar
ly,
thesi
zeofthe 
int
egerdat
aty
pe(
inby
tes)al
sodi
ff
ersf
ordi
ff
erentcompi
l
ers
asshowninthetabl
ebel
ow:

Si
zeof
i
nti
ndi
ff
erentcompi
l
ers

Not
ethatt
hesi
zeoft
hegi
vendat
aty
pesi
sinby
tes.
The shor

int
egercanbeusedi nplaceswher
esmal lv
aluesandlit
tl
estoragespacei
s
requir
ed.I
tcanboostupt herunt
imebecauseituseslessspace.I
tisdeclar
edbyusi
ng
thekeyword 
‘shor
t’
 or
 ‘
shorti
nt’
.
The l
ong i
ntegergi
vesusal ongr
angeorabi ggersi
zecomparedto‘short
’buti
tcan
causeourpr ogr
am tot
akemor eti
mef orexecuti
onbecauseoft
hest or
agesizei
toff
ers.
Iti
sdeclar
edbyusi ngthekeywor
d ‘
long’
 or
 ‘
longint
’.Ther
angeforalongint
egeri
s
–2147483648  t
o+2147483647.
Thi
sishowwedecl
aret
hei
ntegerv
ari
abl
es:

i
ntnum,
l
engt
h;
shor
tbreadth;
shor
tintheight
;
l
ongintvolume=0;
Wehav
eanot
hercl
assi
fi
cat
ionoft
hei
ntegerdat
aty
pe:
 
signed 
and 
unsi
gnedi
nt.
Incaseweneedt otakepositi
vev
aluesonly
,thenwecanuseunsignedi
nt.I
tsrangeis0
to4294967295.Thisisbecauset
heleft
mostbitisf
reeanddoesnotneedtostorethe
signoft
henumber .So,wegetmor est
oragespacei.
e.doubl
eontheposit
ivesi
de.
Bydefaul
t,t
hesignedtypei
sdeclar
edandwedonotneedt
ousesi
gned.
 Si
gnedi
nt
worksli
kean i
nt.I
tsrangei
sthesameas i
nt.

1.
2.Char
act
erDat
aTy
pe
Characterdat
at ypesareusedt
odefi
nevar
iabl
est
aki
ngonechar
acterasi
tsv
alue.The
keywordusedf orcharact
erdat
aty
peis 

char’
.Her
eishowwedeclar
echar
acter
var
iables:

charch,
ch1='
A'
,ch2=67;
Here,i
n ch1 v
ari
abl
e,westore 
‘A’
,i
.e.
,thebinaryequi
v al
entoftheASCIIval
ue
of 
A(=bi
naryofdecimal65)getsstor
ed.And ch2 var
iabl
est or
esthevalue 
67(
ASCI
I
for
 ‘
C’)
.Sot hesear
eactual
lytwoway sofinit
ial
izi
ngachar act
erval
ue.
Li
keint
egers,herewehav e 
signed and unsigned charact
erv alues.The 
signedchar
 i
s
equi
val
entto char.Forasignedchart herangeis -128 t
o +127.Wher eas,foran
unsi
gnedchart herangeisfrom  0 
to 255.Her e’
showwedecl aresigned(
declar
ed
as 
char 
above)andunsi gnedcharv alues:

unsi
gnedcharch;
charch1=128;
Surpr
isedt
oseewhyIputthevalueofch1as128?Here’
sanot
herthi
ngaboutthi
sdata
ty
pe.Asmenti
onedbefor
e char 
hasarangeof
 +127 
butwehaveputthev
alue 
128 her
e.
Whatisgoi
ngtobetheoutput?
Whathappenshereisthatoncewereachtheendoft herange,t
heothersi
deoft
he
rangei
saccessed.I
tgoesbackt o-
128again.Sointhiscase,wheni
tcomesacross
128itgoesbacktothebeginni
ngandaccessesthecharatt heASCI
I-128.

1.
3.Fl
oat
(andDoubl
e)Dat
aTy
pe
Fl
oatanddoubledatatypesareusedtodefi
nevari
abl
esthatt
akeupadeci
mal
val
ueor
anexponenti
alval
ue.Thekeywordusedforfl
oatanddoubl
edatat
ype
ar
e ‘
fl
oat’
 and 

double’
 r
espect
ivel
y.

 Fl
oat 
hasarangeof–3.4e38 t
o +3.4e38 anditssi
zei
s 4by
tes.
 Doubl
e hasarangeof
 -
1.7e308 t
o +1.7e308 andit
ssi
zeis 
8bytes.
 Anot
herdat
aty
pet
hati
sof
fer
edbypr
ogr
ammi
ngl
anguagesi
s ‘
l
ongdoubl
e’
 whi
chhasa
rangeof–1.7e4932 
to 
+1.
7e4932 
andi
tssi
zei
s 10by
tes.Her
eishowwedecl
aref
loatand
doublevar
iabl
es:

fl
oatl
ength,ar
ea=0.0;
doubl
eradius,
area=0.
0;

2.Secondar
yDat
aTy
pes
Secondar
ydatatypes 
arebasi
call
yder
ivedf
rom t
hepr
imar
ydat
aty
pes.Let
’shav
ea
l
ookataf ewsecondarydat
atypes:
2.
1.Ar
ray
s
An arr
ay  
i
sacol l
ect
ionofdat aoft
hesamedat atype.Thesearedecl ar
edunderthe
samev ar
iabl
eandar eaccessedusi
ngit
.Ifwedeclareani nt
egerarray,al
ltheval
uesin
thearrayhavetobei nt
egers.Si
mil
arl
y,f
ora char
acterarray,al
ltheelementsofthe
arr
ayar echaract
ersandt hesamegoesfordoubleandev eryotherdatatype.Anarr
ayis
declar
edasf oll
ows:

i
nta[
50]
;
 //
Decl
arat
ion
Thi
sar
rayhasani
ntegerdat
aty
peandcanst
ore50i
ntegerel
ement
s.
2.
2.Poi
nter
s
Apoi
ntercont
ainstheaddressofav
ari
abl
eint
hepr
ogr
am.
Wedeclar
ethepointeras:

i
nt*
ip;
//Decl
arat
ion
Apointerdecl
aredasi ntegert
ypestorest
headdressofthei
ntegert
ypevari
abl
e.
Simil
arl
y,apointerdeclaredascharty
pestor
estheaddressofthechar
actert
ype
vari
abl
eandsoon.
Wewi
l
ldi
scussabouteachoneoft
hem i
ndet
ail
inf
utur
epost
s.

3.User
-def
ineddat
aty
pes
Theuser
-def
ineddat
atypedef
inest
hedataint
hewayt hattheprogr
ammerchooses.
Let
’shav
eal ookatt
hesecommonlyuseduser
-def
ineddatatypes:
3.
1.St
ruct
ures
I
tisacoll
ect
ionofvar
iabl
esofdi f
fer
entdatatypesrepresent
edbyt hesamename.
Unl
ikeanarr
aywherewehadt ostor
eal l
thedataofthesamet ypeinthevar
iabl
e,here
onecanstor
edataofdiff
erentdatatypesunderthesamev ari
ablename.Iti
smost l
y
usedt
of or
mrecordswheredi
ff
erentspeci
fi
cat
ionsneedt
obest
oredundert
hesame
name.The 
str
uct
 keywor
disusedtodefineast
ructur
e.
3.
2.Uni
on
Anotherdatatypethatisver
ysimil
art
ost
ructur
es.Itallowstheprogr
ammert ostor
e
dataofdif
ferentdatatypesi
nthesamememor ylocation.Aunioncanhavemulti
ple
member sbutitcanst or
eonlyonememberataparticulart
ime.Thekeyword 
uni
on i
s
usedtodefineaUni on.
3.
3.Enum
Enum or
 Enumerat
ion 
isusedtodecl
arethevar
iabl
esandconsi
stsofi
ntegr
alconst
ant
s.
Thekeyword 
enum isusedtodef
inetheenumerati
ondat
atype.
Eg:enum i
dent
if
ier
{el
ement
1,el
ement2,
…….
,el
ementn}; 
Itassi
gnsthev
alue
fr
om 0 t
o n 
tot
heelement
spresenti
nsi
det
heident
if
iersequent
ial
l
y.

Dat
aTy
pesi
nC
Adatat
ypespeci
fi
est
het
ypeofdat
athatav
ari
abl
ecanst
oresuchasi
nteger
,fl
oat
ing,
char
act
er,
etc.

Ther
ear
ethef
oll
owi
ngdat
aty
pesi
nCl
anguage.

Ty
pes Dat
aTy
pes

Basi
cDat
aTy
pe i
nt,
char
,fl
oat
,doubl
e

Der
ivedDat
aTy
pe ar
ray
,poi
nter
,st
ruct
ure,
uni
on

Enumer
ati
onDat
aTy
pe enum

Voi
dDat
aTy
pe v
oid

Basi
cDat
aTy
pes
Thebasi
cdataty
pesareint
eger-basedandf
loat
ing-
poi
ntbased.Cl
anguagesuppor
ts
bot
hsignedandunsi
gnedli
ter
als.

Thememorysizeoft
hebasi
cdat
aty
pesmaychangeaccor
dingt
o32or64-
bit
oper
ati
ngsy
stem.

Let
'sseet
hebasi
cdat
aty
pes.I
tssi
zei
sgi
ven 
accor
dingt
o32-
bitar
chi
tect
ure.

Dat
aTy
pes Memor
ySi
ze Range

char 1by
te −128t
o127

si
gnedchar 1by
te −128t
o127

unsi
gnedchar 1by
te 0t
o255

shor
t 2by
te −32,
768t
o32,
767

si
gnedshor
t 2by
te −32,
768t
o32,
767

unsi
gnedshor
t 2by
te 0t
o65,
535

i
nt 2by
te −32,
768t
o32,
767

si
gnedi
nt 2by
te −32,
768t
o32,
767

unsi
gnedi
nt 2by
te 0t
o65,
535

shor
tint 2by
te −32,
768t
o32,
767

si
gnedshor
tint 2by
te −32,
768t
o32,
767

unsi
gnedshor
tint 2by
te 0t
o65,
535

l
ongi
nt 4by
te -
2,147,
483,
648t
o2,
147,
483,
647

si
gnedl
ongi
nt 4by
te -
2,147,
483,
648t
o2,
147,
483,
647

unsi
gnedl
ongi
nt 4by
te 0t
o4,
294,
967,
295

f
loat 4by
te
doubl
e 8by
te

l
ongdoubl
e 10by
te

Key
wor
dsi
nC
Akeywordisa r
eser
v edword.Youcannotuseitasavar
iabl
ename,
const
antname,
etc.Ther
ear
e
onl
y32reservedwords(key
wor ds)i
ntheClanguage.

Al
i
stof32key
wor
dsi
nthecl
anguagei
sgi
venbel
ow:

aut
o br
eak case char const cont
inue def
aul
t do

doubl
e el
se enum ext
ern f
loat f
or got
o i
f

i
nt l
ong r
egi
ster r
etur
n shor
t si
gned si
zeof st
ati
c

st
ruct swi
tch t
ypedef uni
on unsi
gned v
oid v
olat
il
e whi
l
e

Var
iabl
esi
nC
A v
ari
able 
isanameoft
hememor
ylocat
ion.I
tisusedt
ost
oredat
a.I
tsv
aluecanbechanged,
andi
t
canbereusedmanyt
imes.

I
tisawayt
orepr
esentmemor
ylocat
iont
hroughsy
mbol
sot
hati
tcanbeeasi
l
yident
if
ied.

Let
'sseet
hesy
ntaxt
odecl
areav
ari
abl
e:

1. t
ype 
var
iabl
e_l
i
st;
 
 

Theexampl
eofdecl
ari
ngt
hev
ari
abl
eisgi
venbel
ow:

1. i
nt 
a; 
 
2. f
loat
 b;
 
 
3. char
 c;
 
 

Her
e,a,
b,car
evar
iabl
es.Thei
nt,
float
,charar
ethedat
aty
pes.
Wecanal
sopr
ovi
dev
alueswhi
l
edecl
ari
ngt
hev
ari
abl
esasgi
venbel
ow:

1. i
nt 
a=10,
b=20;
//decl
ari
ng 
2 v
ari
abl
e of
 i
nteger
 t
ype 
 
2. f
loat
 f
=20.
8; 
 
3. char
 c='
A'

 

Rul
esf
ordef
ini
ngv
ari
abl
es
o Av
ari
abl
ecanhav
eal
phabet
s,di
git
s,andunder
scor
e.

o Av
ari
abl
enamecanst
artwi
tht
heal
phabet
,andunder
scor
eonl
y.I
tcan'
tst
artwi
thadi
git
.

o Nowhi
tespacei
sal
l
owedwi
thi
nthev
ari
abl
ename.

o Av
ari
abl
enamemustnotbeanyr
eser
vedwor
dorkey
wor
d,e.
g.i
nt,
float
,et
c.

Val
idv
ari
abl
enames:

1. i
nt 
a; 
 
2. i
nt 
_ab;
 
 
3. i
nt 
a30;
 
 

I
nval
idv
ari
abl
enames:

1. i
nt 
2; 
 
2. i
nt 
a b;
 
 
3. i
nt 
long;
 
 

Ty
pesofVar
iabl
esi
nC
Ther
ear
emanyt
ypesofv
ari
abl
esi
nc:

1. l
ocal
var
iabl
e

2. gl
obal
var
iabl
e

3. st
ati
cvar
iabl
e

4. aut
omat
icv
ari
abl
e

5. ext
ernal
var
iabl
e
Local
Var
iabl
e
Av
ari
abl
ethati
sdecl
aredi
nsi
det
hef
unct
ionorbl
ocki
scal
l
edal
ocal
var
iabl
e.

I
tmustbedecl
aredatt
hest
artoft
hebl
ock.

1. v
oid 
funct
ion1(
){ 
 
2. i
nt 
x=10;
//l
ocal
 
var
iabl
e  
3. }
  

Youmusthav
etoi
nit
ial
i
zet
hel
ocal
var
iabl
ebef
orei
tisused.

Gl
obal
Var
iabl
e
Av ar
iabl
ethatisdecl
aredoutsi
dethefunct
ionorbl
ocki
scall
edagl obal
var
iabl
e.Anyf
unct
ioncan
changethevalueoftheglobalv
ari
abl
e.Iti
savail
abl
etoal
lthefunct
ions.

I
tmustbedecl
aredatt
hest
artoft
hebl
ock.

1. i
nt 
val
ue=20;
//gl
obal
 
var
iabl
e  
2. v
oid 
funct
ion1(
){ 
 
3. i
nt 
x=10;
//l
ocal
 
var
iabl
e  
4. }
  

St
ati
cVar
iabl
e
Av
ari
abl
ethati
sdecl
aredwi
tht
hest
ati
ckey
wor
discal
l
edst
ati
cvar
iabl
e.

I
tret
ainsi
tsv
aluebet
weenmul
ti
plef
unct
ioncal
l
s.

1. v
oid 
funct
ion1(
){ 
 
2. i
nt 
x=10;
//l
ocal
 
var
iabl
e  
3. st
ati
c i
nt 
y=10;
//st
ati
c v
ari
abl
e  
4. x=x+1;
 
 
5. y
=y+1;
 
 
6. pr
int
f("
%d,
%d"
,x,
y);
 
 
7. }
  

Ifyoucallthi
sfunct
ionmanyti
mes,the 
localvar
iablewil
lpr
intt
hesamev
alue 
foreachfunct
ioncal
l,
e.g,11,
11,11andsoon.Butthe 
stat
icvari
ablewillpr
intt
heincr
ement
edval
ue 
ineachfuncti
oncall
,
e.g.11,12,13andsoon.
Aut
omat
icVar
iabl
e
Allv
ari
ablesi
nCthatar
edecl
aredi
nsi
dethebl
ock,ar
eautomat
icv
ari
abl
esbydef
aul
t.Wecan
expl
ici
tl
ydecl
areanaut
omati
cvari
abl
eusi
ng 
autokeywor
d.

1. v
oid 
mai
n()

 
2. i
nt 
x=10;
//l
ocal
 
var
iabl
e (
also 
aut
omat
ic)
  
3. aut
o i
nt 
y=20;
//aut
omat
ic 
var
iabl
e  
4. }
  

Ex
ter
nal
Var
iabl
e
Wecanshareav ar
iabl
einmult
ipl
eCsourcefi
l
esbyusi
nganext
ernal
var
iabl
e.Todecl
arean
ext
ernal
var
iabl
e,youneedtouse 
ext
ernkeyword.

my
fil
e.h

1. exter
n i
nt 
x=10;
//ext
ernal
 
var
iabl
e (
also 
global

 
progr
am1.c

1. #i
ncl
ude 
"my
fil
e.h"
  
2. #i
ncl
ude 
<st
dio.
h> 
 
3. v
oid 
pri
ntVal
ue(
){ 
 
4.  
  
 pr
int
f("
Global
 
var
iabl
e: 
%d"

global
_var
iabl
e);
 
 
5. }
  

You might also like