Beruflich Dokumente
Kultur Dokumente
CProgrammingVariablesandConstants
CProgrammingVariablesandConstants
In this tutorial, you will learn about variables, rules for naming a variable, constants
and different type of constants in C programming.
Variables
Inprogramming,avariableisacontainer(storagearea)toholddata.
Toindicatethestoragearea,eachvariableshouldbegivenauniquename(identifier).
Variablenamesarejustthesymbolicrepresentationofamemorylocation.Forexample:
intplayerScore=95;
Here,playerScoreisavariableofintegertype.Thevariableisholding95intheabovecode.
http://www.programiz.com/cprogramming/cvariablesconstants
1/5
8/4/2016
CProgrammingVariablesandConstants
Thevalueofavariablecanbechanged,hencethename'variable'.
InCprogramming,youhavetodeclareavariablebeforeyoucanuseit.
RulesforwritingvariablenameinC
1.Avariablenamecanhaveletters(bothuppercaseandlowercaseletters),digitsand
underscoreonly.
2.Thefirstletterofavariableshouldbeeitheraletteroranunderscore.However,itis
discouragedtostartvariablenamewithanunderscore.Itisbecausevariablenamethat
startswithanunderscorecanconflictwithsystemnameandmaycauseerror.
3.Thereisnoruleonhowlongavariablecanbe.However,thefirst31charactersofa
variablearediscriminatedbythecompiler.So,thefirst31lettersoftwovariablesina
programshouldbedifferent.
Cisastronglytypedlanguage.Whatthismeansitthat,thetypeofavariablecannotbe
changed.Suppose,youdeclaredanintegertypevariable.Youcannotstorecharacterora
decimalnumberinthatvariable.Visitthispagetolearnmoreaboutdifferenttypesofdataa
variablecanstore.
Constants/Literals
Aconstantisavalueoranidentifierwhosevaluecannotbealteredinaprogram.For
example:1,2.5,"Cprogrammingiseasy"etc.
Asmentioned,anidentifieralsocanbedefinedasaconstant.
constdoublePI=3.14
Here,PIisaconstant.Basicallywhatitmeansisthat,PIand3.14issameforthisprogram.
Integerconstants
Aintegerconstantisanumericconstant(associatedwithnumber)withoutanyfractionalor
exponentialpart.TherearethreetypesofintegerconstantsinCprogramming:
decimalconstant(base10)
octalconstant(base8)
hexadecimalconstant(base16)
http://www.programiz.com/cprogramming/cvariablesconstants
2/5
8/4/2016
CProgrammingVariablesandConstants
Forexample:
Decimalconstants:0,9,22etc
Octalconstants:021,077,033etc
Hexadecimalconstants:0x7f,0x2a,0x521etc
InCprogramming,octalconstantstartswitha0andhexadecimalconstantstartswitha0x.
Floatingpointconstants
Afloatingpointconstantisanumericconstantthathaseitherafractionalformoran
exponentform.Forexample:
2.0
0.0000234
0.22E5
Note:E5=10 5
Characterconstants
Acharacterconstantisaconstantwhichusessinglequotationaroundcharacters.For
example:'a','l','m','F'
EscapeSequences
Sometimes,itisnecessarytousecharacterswhichcannotbetypedorhasspecialmeaning
inCprogramming.Forexample:newline(enter),tab,questionmarketc.Inordertouse
thesecharacters,escapesequenceisused.
Forexample:\nisusedfornewline.Thebackslash(\)causes"escape"fromthenormal
waythecharactersareinterpretedbythecompiler.
EscapeSequences
EscapeSequences
Character
\b
Backspace
\f
Formfeed
http://www.programiz.com/cprogramming/cvariablesconstants
3/5
8/4/2016
CProgrammingVariablesandConstants
\n
Newline
\r
Return
\t
Horizontaltab
\v
Verticaltab
\\
Backslash
\'
Singlequotationmark
\"
Doublequotationmark
\?
Questionmark
\0
Nullcharacter
Stringconstants
Stringconstantsaretheconstantswhichareenclosedinapairofdoublequotemarks.For
example:
"good"//stringconstant
""//nullstringconstant
""//stringconstantofsixwhitespace
"x"//stringconstanthavingsinglecharacter.
"Earthisround\n"//printsstringwithnewline
Enumerationconstants
Keywordenumisusedtodefineenumerationtypes.Forexample:
enumcolor{yellow,green,black,white};
Here,colorisavariableandyellow,green,blackandwhitearetheenumerationconstants
havingvalue0,1,2and3respectively.Formoreinformation,visitpage:CEnumeration.
Checkouttheseexamplestolearnmore:
CProgramtoPrintanIntegerEnteredbytheUser
http://www.programiz.com/cprogramming/cvariablesconstants
4/5
8/4/2016
CProgrammingVariablesandConstants
CProgramtoAddTwoIntegers
CProgramtoMultiplytwoFloatingPointNumbers
PreviousPage
NextPage
CopyrightbyProgramiz|Allrightsreserved|PrivacyPolicy
http://www.programiz.com/cprogramming/cvariablesconstants
5/5