Вы находитесь на странице: 1из 3

Home

References

AboutTP

Advertising

Advertisements

...
Web

ThisSite

LearningC
CProgrammingHOME
CBasicIntroduction
CProgramStructure
CReservedKeywords
CBasicDatatypes

&%DVLF'DWDW\SHV

CVariableTypes

CStorageClasses
CUsingConstants
COperatorTypes

Advertisements

CControlStatements
CInputandOutput
CPointingtoData
CUsingFunctions

Chasaconceptof'datatypes'whichareusedtodefineavariablebeforeitsuse.Thedefinition
ofavariablewillassignstorageforthevariableanddefinethetypeofdatathatwillbeheldin
thelocation.

CPlaywithStrings

Thevalueofavariablecanbechangedanytime.

CStructuredDatatypes

Chasthefollowingbasicbuiltindatatypes.

CWorkingwithFiles
CBitsManipulation

int

CPreProcessors

float

CUsefulConcepts

double

CFunctionReferences
CBuiltinFunctions

CUsefulResources
CUsefulResources

SelectedReading
ComputerGlossary
WhoisWho

char
Please note that there is not a boolean data type. C does not have the traditional view about
logicalcomparison,butthatsanotherstory.

LQWGDWDW\SH
intisusedtodefineintegernumbers.

^
LQW&RXQW
&RXQW 
`

IORDWGDWDW\SH
Copyright2014bytutorialspoint

floatisusedtodefinefloatingpointnumbers.

^
IORDW0LOHV
0LOHV 
`

GRXEOHGDWDW\SH
double is used to define BIG floating point numbers. It reserves twice the storage for the
number.OnPCsthisislikelytobe8bytes.

^
GRXEOH$WRPV
$WRPV 
`

FKDUGDWDW\SH
chardefinescharacters.

^
FKDU/HWWHU
/HWWHU 
[

`

0RGLILHUV
Thedatatypesexplainedabovehavethefollowingmodifiers.
short

long
signed
unsigned
The modifiers define the amount of storage allocated to the variable. The amount of storage
allocatedisnotcastinstone.ANSIhasthefollowingrules:

VKRUWLQW LQW ORQJLQW


IORDW GRXEOH ORQJGRXEOH
Whatthismeansisthata'shortint'shouldassignlessthanorthesameamountofstorageasan
'int'andthe'int'shouldbelessorthesamebytesthana'longint'.Whatthismeansinthereal
worldis:

7\SH%\WHV5DQJH

VKRUWLQW! NE
XQVLJQHGVKRUWLQW! .E
XQVLJQHGLQW! *E
LQW! *E
ORQJLQW! *E
VLJQHGFKDU!
XQVLJQHGFKDU!
IORDW
GRXEOH
ORQJGRXEOH
ThesefiguresonlyapplytotodaysgenerationofPCs.Mainframesandmidrangemachinescould
usedifferentfigures,butwouldstillcomplywiththeruleabove.
You can find out how much storage is allocated to a data type by using the sizeof operator
discussedinOperatorTypesSession.
Hereisanexampletochecksizeofmemorytakenbyvariousdatatypes.

LQW
PDLQ
^
SULQWI VL]HRI FKDU  G?QVL]HRI FKDU 
SULQWI VL]HRI VKRUW  G?QVL]HRI VKRUW 
SULQWI VL]HRI LQW  G?QVL]HRI LQW 
SULQWI VL]HRI ORQJ  G?QVL]HRI ORQJ 
SULQWI VL]HRI IORDW  G?QVL]HRI IORDW 
SULQWI VL]HRI GRXEOH  G?QVL]HRI GRXEOH 
SULQWI VL]HRI ORQJGRXEOH  G?QVL]HRI ORQJGRXEOH 
SULQWI VL]HRI ORQJORQJ  G?QVL]HRI ORQJORQJ 
UHWXUQ
`

4XDOLILHUV
A type qualifier is used to refine the declaration of a variable, a function, and parameters, by
specifyingwhether:
Thevalueofavariablecanbechanged.
Thevalueofavariablemustalwaysbereadfrommemoryratherthanfromaregister
StandardClanguagerecognizesthefollowingtwoqualifiers:
const
volatile
TheconstqualifierisusedtotellCthatthevariablevaluecannotchangeafterinitialisation.
constfloatpi=3.14159
Nowpicannotbechangedatalatertimewithintheprogram.
Anotherwaytodefineconstantsiswiththe#definepreprocessorwhichhastheadvantagethatit
doesnotuseanystorage
The volatile qualifier declares a data type that can have its value changed in ways outside the
control or detection of the compiler (such as a variable updated by the system clock or by
another program). This prevents the compiler from optimizing code referring to the object by
storing the object's value in a register and rereading it from there, rather than from memory,
whereitmayhavechanged.Youwillusethisqualifieronceyouwillbecomeexpertin"C".Sofor
nowjustproceed.

:KDWDUH$UUD\V
Wehaveseenallbaiscdatatypes.InClanguageitispossibletomakearrayswhoseelements
arebasictypes.Thuswecanmakeanarrayof10integerswiththedeclaration.

LQW[>@
The square brackets mean subscripting parentheses are used only for function references.
Arrayindexesbeginatzero,sotheelementsofxare:
ThusArrayarespecialtypeofvariableswhichcanbeusedtostoremultiplevaluesofsamedata
type.Thosevaluesarestoredandaccessedusingsubscriptorindex.
Arraysoccupyconsecutivememoryslotsinthecomputer'smemory.

[>@[>@[>@[>@
Ifanarrayhasnelements,thelargestsubscriptisn1.
Multipledimensionarraysareprovided.Thedeclarationanduselooklike:

LQWQDPH>@>@
Q QDPH>LM@>@QDPH>N@>@
Subscriptscanbearbitraryintegerexpressions.Multidimensionarraysarestoredbyrowsothe
rightmostsubscriptvariesfastest.Inaboveexamplenamehas10rowsand20columns.
Same way, arrays can be defined for any data type. Text is usually kept as an array of
characters.ByconventioninC,thelastcharacterinacharacterarrayshouldbea`\0'because
most programs that manipulate character arrays expect it. For example, printf uses the `\0' to
detecttheendofacharacterarraywhenprintingitoutwitha`%s'.
Here is a program which reads a line, stores it in a buffer, and prints its length (excluding the
newlineattheend).

PDLQ  ^
LQWQF
FKDUOLQH>@
Q 
ZKLOH  F JHWFKDU   
?Q
 ^
LI Q
OLQH>Q@ F
Q
`
SULQWI OHQJWK G?QQ 
`

$UUD\,QLWLDOL]DWLRQ
Aswithotherdeclarations,arraydeclarationscanincludeanoptionalinitialization
Scalarvariablesareinitializedwithasinglevalue
Arraysareinitializedwithalistofvalues
Thelistisenclosedincurlybraces

LQWDUUD\>@ ^`
Thenumberofinitializerscannotbemorethanthenumberofelementsinthearraybutitcanbe
less in which case, the remaining elements are initialized to 0.if you like, the array size can be
inferred from the number of initializers by leaving the square brackets empty so these are
identicaldeclarations:

LQWDUUD\>@ ^`
LQWDUUD\>@ ^`
Anarrayofcharactersiestringcanbeinitializedasfollows:

FKDUVWULQJ>@ +HOOR

Advertisements

Вам также может понравиться