C Programming-Constant, Variable, and Data type in C

C Programming
Constant, Variable, and Data type in C
Topics Covered: 1. Identifiers 2. Constant a. Integer Constant b. Real Constant c. Single Character constant d. String constant 3. Variable 4. Data type 5. Pri ary Data type a. Integer data type b. !loat data type c. Do"ble data type d. Character data type #. $ser%defined data type a. typedef b. en" &. Declaration of 'ariable (. Initiali)ation of 'ariable *. Constant 'ariable 1+. Volatile 'ariable 11. ,'erflo- and $nderflo- error 12. Sy bolic constant 13. Scanf f"nction.

Identifiers: Identifiers refer to the na es of 'ariable. f"nctions and arrays. /hese are "ser%defined na es and consist of a se0"ence of alphabet. digits. and "nderscore. /he first letter of the identifier is "st be alphabet. Constants: Constants are fi1ed 'al"e in C. -hich yo" cannot change. C constants can be categori)ed into2 Constants

;" eric Constants

Character Constants

Integer Constants

Real Constants

Single Character Constants

String Constants

Deci al constant

,ctal constant

3e1adeci al constant Fig. 1 Types of t e Constant

Integer Constants: Integer constants can be three types2 Deci al. ,ctal. and 3e1adeci al. Deci al constants consist set of digits + to *. preceded by 4'e of 5'e sign. 6 bedded spaces. co as. and non%digit characters are not per itted in integer constant. 7e are "sing 8d to represent deci al n" ber in printf and scanf f"nction. 9y ---.sharebca.co Share 9C:.Co : place to learn and share all abo"t 9C:

C Programming-Constant, Variable, and Data type in C e.g. 123 1345#

,ctal constant contains the 'al"e in octal for at. It contains +%& digits preceded by +. In printf and scanf f"nction yo" can represent ,ctal constant by "sing 8o e.g. +5#& +234 : he1adeci al constant contains the 'al"e fro +%* and a%f or :%!. 3ere :%! or a%f indicates the n" ber fro 1+%15 respecti'ely. In printf and scanf f"nction yo" can refer 3e1adeci al constant by "sing 81 e.g +15#(: +145:bc +<abcdD23 !eal Constants: Real constant contains integer 'al"e as -ell as it also contains fractional n" ber. Real constant is consisting digits fro +%* and only one dot character. It also contains 5'e and 4'e sign. e.g. +.++(3 %+.&5 435.35 .#& =o" can also represent this n" ber into e1ponential >scientific? notation. =o" can represent the n" ber in follo-ing for at2 Mantissa e exponent @antissa part can be fractional or integer -hile e1ponent part is al-ays integer. @antissa and e1ponent is separated by A6B or AeB. e.g 2345.345 can be represented by 2.345345e3 or 2.34534563 "ingle C aracter constant: Single character constant contains any character fro e1pressed in bet-een single 0"ote >A?. e.g. A1B A5B ADB Ceyboard. Character constant can be

In abo'e e1a ple A5B is not n" ber b"t it is in character for at. In character constant yo" cannot -rite ABB in this -ay b"t it is AEBB by "sing escape character. 7e are "sing 8c to represent the single character 'al"e in printf and scanf state ent. "tring Constant: : string is a gro"p of character enclosed in do"ble 0"otes >F?. 7e are "sing 8s to represent the string 'al"e in printf and scanf state ent. e.g. FS-a inarayanG F1234G #ote: FaG is a string constant -hile AaB single character constant.

Variable: “A variable is a data name that may be used to store a value.” It -ill not re ain constant d"ring e1ec"tion. 7hen yo" gi'e the 'ariable na e in progra Cno-n as 'ariable declaration. it is

!ollo-ing are the r"les for declaring the 'ariable na e2 1. It contains only alphabet. digits. and "nderscore > H ? 2. It "st being -ith alphabet character 3. Its length of 'ariable na e is ini " one character and a1i " 32 characters long. 4. /here is a difference bet-een "ppercase and lo-ercase letter na e. e.g. /otal and total are t-o different 'ariable. 9y ---.sharebca.co Share 9C:.Co : place to learn and share all abo"t 9C:

C Programming-Constant, Variable, and Data type in C 5. #. /he 'ariable sho"ld not be Cey-ord. 7hite space is not allo-ed in the 'ariable na e. Space. tab. and enter Cey are the -hite space character.

#ote: /he sa e r"les are applied for the declaration of identifier na e as the 'ariable na e.

Data type: /he 'ariable is "sed to store the different type of data 'al"e s"ch as integer. fractional n" ber. character. string. etc. /his data 'al"e deter ines the type of 'ariable. -hich is Cno-n as data type. In C progra basically fo"r types of data type2 1. Pri ary >!"nda ental? data types 2. $ser%defined data type 3. Deri'ed data type 4. 6 pty data set

Primary Data type: In C lang"age pri ary data types are ainly fo"r2 int >Integer 'al"e?. float >single precision 'al"e?. do"ble >do"ble precision 'al"e?. char >Character 'al"e?. /here are also so e other data types b"t it is co bination of all these fo"r data types s"ch as long int. long float. "nsigned int. etc. Pri ary Data type

Integer Data /ype

!loat Data type

Character Data type

• • • •

Signed Integer >signed int or int? $nsigned Integer >"nsigned int? Short Integer >short int? Iong Integer >long int?

• • •

Single precision fractional 'al"e >float? Do"ble precision fractional 'al"e >do"ble? Iong do"ble >long do"ble?

• •

Signed Character >signed char or char? $nsigned Character >"nsigned char?

Fig. $ Types of Primary Data type Integer Data type: Integers are -hole n" bers. Integer data type 'ariable can be declared as int variablename In 1%-bit mac ine the si)e of integer type 'ariable is $ bytes that eans it can store the 'al"e fro 4215 to >215?%1 i.e. fro 432.&#( to 532.&#&. In &$-bit mac ine the si)e of integer type 'ariable is ' bytes that eans it can store the 'al"e fro 4231 to >231?%1 i.e. fro 4 2.14.&4.(3.#4( to 52.14.&4.(3.#4&. Integer data type also can be di'ided into different s"b type. I. "igned Integer: Signed integer is a n" ber. -hich contains either positi'e or negati'e sign. Signed integer 'ariable can be declared "sing

signed

int

variablename

9y defa"lt integer is signed type of 'ariable. 7e are "sing 8d to represent signed or si ple integer data 'al"e in printf and scanf state ent. 9y ---.sharebca.co Share 9C:.Co : place to learn and share all abo"t 9C:

C Programming-Constant, Variable, and Data type in C II. (nsigned Integer: /he "nsigned integer has not posses the si)e so yo" cannot represent sign -ith this n" ber. 9"t it pro'ides the facility to store higher 'al"e "sing sa e n" ber of bytes. In 1#%bit achine the si)e of "nsigned integer is $ bytes b"t it can store the 'al"e fro + to #5.535. 7e are "sing 8" to represent "nsigned integer 'al"e in printf and scanf state ent. unsigned III. int variablename

)ong Integer: If yo" ha'e to store higher 'al"e than the gi'en range then there is "se of long integer. In long integer for 1#%bit achine there is "se of ' bytes so -e can represent the 'al"e fro 42.14.&4.(3.#4( to 52.14.&4.(3.#4&. 7e are "sing 8ld to represent long integer 'al"e in printf and scanf state ent. long int variablename $nsigned long integer can represent the 'al"e fro + to 4.2*4.*#&.2*5. 7e are "sing 8l" to represent "nsigned long integer 'al"e in printf and scanf state ent. unsigned long int variablename eans yo"r can represent the

IV.

" ort integer: It occ"pies only 1 byte in 1#%bit achine. /hat n" ber fro 42( to >2(?%1 i.e. fro 412( to 512& short int variablename

It can also signed and "nsigned for . !or "nsigned integer the range is fro + to 255. 7e are "sing 8d to represent short integer 'al"e in printf and scanf state ent.

#ote: /he si)e of short integer is gi'en 1 byte in booC FC Progra ing by 9alag"r"sa yG b"t in /"rbo C55 si)e of short integer is 2 bytes. :lso FshortG and Fshort intG both are sa e. Fsinged intG and FintG both are sa e. Float Data type: !loat 'ariable can contain any real n" ber. It represents the 'al"e -ith single precision. In 1#%bit achine the si)e of float 'ariable is ' bytes. 7e can represent the 'al"e fro &.'*-&+ to &.'*,&+ by "sing float 'ariable. !loat 'ariable cannot be ha'e any signed or "nsigned characteristic. 7e are "sing 8f or 8g to represent float data 'al"e in printf and scanf state ent. 7e can "se 8e to represent the float 'al"e in scientific for at. 7e cannot "se signed and "nsigned for at for the float 'ariable. Do-ble Data type: Do"ble data type is "sed to declare the 'ariable for any real n" ber b"t it represents the 'al"e -ith do"ble precision. In 1#%bit achine the si)e of do"ble 'ariable is + bytes. 7e can represent the 'al"e fro 1..*-&/+ to 1..*,&/+ by "sing the do"ble data type. 7e are "sing 8lf to represent the do"ble 'al"e in scanf and printf state ent. /here is also another data type long do"ble in -hich 1/ bytes are re0"ired in e ory. /he range for the long do"ble 'ariable is &.'*-'0&$ to 1.1*,'0&$. 7e are "sing 8If to represent long do"ble 'al"e in printf and scanf state ent. 7e cannot "se signed and "nsigned for at for the float 'ariable. C aracter Data type: : single character can be defined as a character >char? data type. /he si)e of the 'ariable declared as char is 1 byte. 7e are "sing 8c to represent character 'al"e in printf and scanf state ent. 7e can "se t-o Cind of character 'ariable2

1. "igned c aracter: 7hene'er yo" not specify any 0"alifier by defa"lt character 'ariable is signed character. 7e can represent the 'al"e fro 412( to 512& by "sing singed character $. (nsigned c aracter: 9y ---.sharebca.co Share 9C:.Co : place to learn and share all abo"t 9C:

C Programming-Constant, Variable, and Data type in C 7hene'er yo" declare the character as the "nsigned character type yo" can represent the 'al"e for + to 255

(ser-defined data type: 3ere yo" can declare yo"r o-n data type by "sing facility of AtypedefB and Aen" B typedef 1Type definition2: ACB s"pports a feat"re Cno-n as Ftype definitionG that allo-s "ser to define and identifier that -o"ld represent an e1isting data type. Iater on the identifier declared by "ser can be "sed for 'ariable declaration. AtypedefB cannot create ne- type. /he ad'antage of typedef is that yo" can "se appropriate for at for 'ariable declaration -hich increase the readability of yo"r progra . /he for at of the Ftype definitionG is2 typedef data%type identifierL 3ere the Adata%typeB refers to basic data%type "sed in ACB progra na e gi'en by the "ser. e.g. typedef int marks; main() { marks sub1, sub2, sub3; …………………………… …………………………… } and AidentifierB is the

;o- yo" can "se the A arCsB to declare the 'ariable of integer type. ;o- yo" can "se AintB or A arCsB for integer type declaration. 3ere As"b1B. As"b2B. As"b3B are 'ariables of type int declared "sing typedef. en-m 1*n-merated Data-type2: It is another type of "ser%defined data type creation. /here is no ore "se of the en" e1cept to increase the readability of yo"r progra . /he for at of Aen" B is2 en-m identifier J'al"e%1. 'al"e%2. 'al"e%3. M.. . 'al"e%nNL 3ere the identifier is a "ser%defined en" erated data%type. -hich can be "sed to declare 'ariables that can ha'e one of the 'al"es fro the bracCet >AJA?. /he 'al"e p"t inside the closing brace is Cno-n as enumeration constant. 3ere the 'al"e of the en" eration constant is start al-ays fro + and incre ent by 1 for ne1t 'al"e. If yo" -ant to assign the 'al"e other than abo'e fi1 'al"e yo" ha'e to assign e1plicitly the 'al"e. e.g. enum yourchoice { !!"12, #$%, M$&, !'(}; then the :DD has assigned 'al"e 12. S$9 assigned 'al"e 13. @$I has assigned 'al"e 14. and DIV has assigned 'al"e 15. enum yourchoice { !!"3), #$%, M$&"*), !'(}; then the :DD has assigned 'al"e 3+. S$9 assigned 'al"e 31. @$I has assigned 'al"e 5+. and DIV has assigned 'al"e 51. :fter this definition -e can declare the 'ariables of ne- type by2 en-m identifier 'ariable%1. 'ariable%2. 'ariable%3L ;o- -e can "se2 'ariable%1K'al"e%1L 'ariable%2K'al"e%2L

9y ---.sharebca.co

Share 9C:.Co : place to learn and share all abo"t 9C:

C Programming-Constant, Variable, and Data type in C !ollo-ing progra sho-s the "se of en" eration data%type2

enum choice { !!, M$&, #$%, !'(}; main() { int a; a" !!; printf(+,d-,a); } In abo'e e1a ple -e ha'e assigned F:DDG to 'ariable FaG so it -ill a"to atically assign + to FaG beca"se the 'al"e of F:DDG is + in en" erated data type.

Declaration of variable: Declaration of 'ariable in progra perfor t-o tasC 1. It tells the co piler -hat is the 'ariable na e. 2. It specifies -hat type of data the 'ariable hold s"ch as integer. float. do"ble. or character e.g. int a; 3ere abo'e declaration state ent tells to co plier AaB is the 'ariable na e and it is of type integer. /he declaration of 'ariable is al-ays done before the 'ariable "sed in the progra . /he for at of 'ariable declaration is2 data-type1 'ariable1L data-type$ 'ariable2L data-type& 'ariable3L 3ere -e can declare the 'ariable of different data%type2 e.g. int a; f.oat b; char c; a operator2 int a,b,c,d;

=o" can declare the 'ariable of sa e data%type by separating -ith co e.g.

:lso in ACB progra declaration of 'ariable is perfor ed in the start of the f"nction. e.g. Variable declaration is perfor ed after starting the opening brace >AJA? of ain>? f"nction main() { int no1,no2; f.oat res; int choice; ……………………… ……………………… }

Initiali3ation of variable: F/he process of gi'ing initial 'al"e to yo"r 'ariable is Cno-n as initialization.G Variable initiali)ation also can be perfor ed d"ring the 'ariable declaration as "nder2 data-type 'ariable%na e K so e constant 'al"eL :lso yo" can perfor e.g. int f.oat the initiali)ation after declaration of 'ariable. 00 'nitia.i1ation durin2 dec.aration

tota./marks"1)); pi"3314;

9y ---.sharebca.co

Share 9C:.Co : place to learn and share all abo"t 9C:

C Programming-Constant, Variable, and Data type in C doub.e distance; distance"343244;

00 'nitia.i1ation after dec.aration

7hen yo" si ply declare the 'ariable and not assign any 'al"e then yo"r 'ariable contains so e rando 'al"e that is Cno-n as garbage value.

Constant Variable: F/he 'ariable -hich 'al"e does not change d"ring the e1ec"tion of progra variable.G 7e can declare constant 'ariable by2 const data%type 'ariable%na e K so e constant 'al"eL

is Cno-n as constant

3ere -e can assign the 'al"e only d"ring the e1ec"tion. If -e are going to change the 'al"e later on then it -ill gi'e co pilation error. e.g. const f.oat pi"3314; 3ere pi is going to be constant 'ariable -hich 'al"e cannot be changed.

Volatile Variable: FDeclaring a 'ariable as 'olatile 'ariable so e e1ternal so"rces.G 7e can declare 'olatile 'ariable by2

eans the 'al"e of 'ariable

ay change any ti e by

volatile data%type 'ariable%na eL e.g. 5o.ati.e date; 3ere the 'al"e of 'ariable data can be change any ti e by so e e1ternal so"rces.

4verflo5 and (nderflo5 error: 7hen the 'al"e of the 'ariable is bigger or s aller than allo-able range of the 'ariable then the 'al"e of 'ariable is not as per the re0"est of "ser b"t it has been changed that is Cno-n as o'erfloor "nderflo- error of data. ACB does not pro'ide any error. -arning or essage for the o'erflo- or "nderflo- error.

"ymbolic constant 1Constant Identifier2: ACB allo-s progra er to "se constant by "sing the preprocessor Odefine that is Cno-n as sy bolic constant or constant identifier. /he for at of the sy bolic constant is2 6define e.g. 6define 6define 6define sy bolicna e 787 &/M 9:# ;'9#7/<& ## > ##/<& ## constant'al"e 1)) =) 4)

!ollo-ing are the r"les "sed to define the sy bolic constant2 1. Sy bolic na e is si ilar to 'ariable b"t it is not 'ariable. 2. ;o blanC space is allo- bet-een AOB and AdefineB -ord. 3. AOB "st be first in the line. 4. : blanC space is re0"ire bet-een AOdefineB and Asy bolicna eB P bet-een Asy bolicna eB and Aconstant'al"eB 5. It is not state ent so it does not end -ith se icolon #. =o" cannot p"t assign ent operator AKB bet-een Asy bolicna eB and constant 'al"e. &. (. *. 1+. 11. 9y ---.sharebca.co Share 9C:.Co : place to learn and share all abo"t 9C:

C Programming-Constant, Variable, and Data type in C 12. 13. 14. 15. 1#. 1&. 1(. 1*. 2+. 21. 22. 23. 24. 25. 2#. 2&. 2(. 2*. 3+. 31. 32. 33. 34. 35. 3#. 3&. 3(. 3*.

=o" can p"t any-here in progra

b"t before it is "sed in the progra .

(se of symbolic constant: Sy bolic constant are "sed for basically t-o p"rpose2 1. To remove t e problem of t e modification of t e program S"ppose in o"r progra -e are "sing the 'al"e of ApiB as 3.14 as si ple constant and there are "ltiple state ents for the calc"lation. ;o- s"ppose later on I -ant to "se ore precise 'al"e 3.1415* then in e'ery state ent I ha'e to change this 'al"e. 9"t if I ha'e "sed the sy bolic constant then if I a changing the 'al"e in sy bolic constant then e'ery-here it -ill be change. To remove t e problem to -nderstand t e program In o"r progra s"ppose I ha'e "sed the si ple constant 5+ for total arCs of s"bQect. 7hene'er anybody is reading that progra he cannot "nderstand -hat is 5+ b"t s"ppose I ha'e "sed the sy bolic constant /,/:IH@:RRS then i ediately any one can "nderstand it is total arCs of the s"bQect

$.

"canf f-nction: Scanf is a f"nction -ritten in Astdio.hB file. It is "sed to read the 'al"e fro the Ceyboard. 7e can read any 'al"e integer. real. character. or string fro the Ceyboard by "sing the scanf f"nction. /he for at of scanf is2 scanf>Ftype0"alifierG. P 'ariablena e?L /he 'al"e of Atype0"alifierB is 8d for integer. 8f for float. 8c fro /here are also other 0"alifiers are "sed. e.g. main( ) { int a; …………………33 scanf(+,d-, ? a); …………………33 } 3ere AaB is the 'ariable na e and -e are reading the 'al"e of AaB fro Ceyboard "sing the scanf f"nction. :fter inp"tting one 'al"e. to inp"t another 'al"e -e ha'e to inp"ts enter or space Cey. character. and 8s for string.

9y ---.sharebca.co

Share 9C:.Co : place to learn and share all abo"t 9C:

C Programming-Constant, Variable, and Data type in C

#ote: 7e can read 'ariable na e. e.g.

"ltiple 'al"es by one scanf f"nction "sing

ore than one type 0"alifier and

scanf(+,d,d,f-, ?a, ?b, ?c); 3ere AaB and AbB are integer 'ariable -hile AcB is float 'ariable.

Problems 1. Indicate -hether follo-ing constants are 'alid or not. >I? 1235#& >II? 345.4# >III? +&345 >,ctal constant? >IV? +2344* >,ctal Constant? >V? +<34: >3e1adeci al constant? >VI? +13425a >3e1adeci al constant? >VII? +14354S >3e1adeci al constant? >VIII? +345#.334 >,ctal Constant? >I<? 345.344++ ><? 2343.333.5544 ><I? A5G ><II? AEtB ><III? ABB ><IV? AGB ><V? ATnB ><VI? APR:@$R3 S7:@IB ><VII? F334G ><VIII? FB3elloBG ><I<? FS-a iGnarayanG Indicate -hether follo-ing 'ariable declarations are 'alid or not. >I? int 3ello >II? float aL >III? char aHcL >IV? int a%cL >V? long float piL >VI? signed int aL >VII? int *piL >VIII? int d.g hL >I<? int /otal. totalL ><? float s" . s" L ><I? int ifL ><II? int thes" 'al"eoft-o'ariablesareasgi'neasinyo"rbooCpleasereferit ><III? long char yna eL 7hat is o"tp"t of follo-ing progra U >I? ain> ? J int aL printf>F8dG.a?L N >II? Oincl"de Vstdio.hW Odefine @SSS F;a astee QeeG ain> ? J printf>@SSS?L N >III? Oincl"de Vstdio.hW ain > ? J int a K 1#L printf>Fa K 8dEnG. a?L printf>Fa K 8oEnG.a?L printf>Fa K 81EnG.a?L N Share 9C:.Co : place to learn and share all abo"t 9C:

2.

3.

9y ---.sharebca.co

C Programming-Constant, Variable, and Data type in C >IV? Oincl"de Vstdio.hW ain> ? J int a K #5L printf>Fa K 8dEnG.a?L printf>Fa K 8cEnG.a?L N >V? Oincl"de Vstdio.hW ain> ? J char c K AaBL printf>Fc K 8cEnG.c?L printf>Fc K 8dEnG.c?L N >VI? Oincl"de Vstdio.hW ain> ? J int aL float f K 2+.5+L a K fL printf>Ff K 8fEnG.f?L printf>Fa K 8dEnG.a?L f K aL printf>Ff K 8fEnG.f?L printf>Fa K 8dEnG.a?L N 7hich is the correct ans-er for follo-ing if all of the are character constantU >I? AXay S-a inarayanB >II? A9hag'anB >III? FS-a inarayanG >IV? :ll the abo'e >V? ;one 7rite a progra to read t-o integers. one float. one character. one do"ble. one long integer. and one long do"ble 'al"e and print it. 7rite a progra to read yo"r age and gender fro Ceyboard and print it. >@ale candidate has to enter @ for gender and !e ale candidate has to enter !? 7rite a progra to s-ap a 'al"e of t-o 'ariable "sing third 'ariable. 7hat restriction applied to the "se of Cey-ordU 61plain the fo"r basic data types in C. 61plain the constant in C. 7e can declare the 'ariable at any-here in progra . >State tr"e or false. :lso gi'e reason for yo"r ans-er? !ind o"t error in follo-ing2 >I? Odefine PI 3.14 ain> ? J int R.CL float peri eterL float areaL CKPI RK5L Peri eterK 2+ Y C Y r L N 7hat -ill be the o"tp"t of the follo-ing state entU printf>Z8dZ.scanf>Z8d8d8dZ.Pi.PQ.PC??L :ns. Iess than or e0"al to 3 >Depends on the ho- any 'al"e yo"r co p"ter has read s"ccessf"lly?

4.

5. #. &. (. *. 1+. 11. 12.

13.

9y ---.sharebca.co

Share 9C:.Co : place to learn and share all abo"t 9C:

Sign up to vote on this title
UsefulNot useful