You are on page 1of 701

revision: r685, 2014-07-24

Programming inD
Learn to program in the
D programming language
Ali ehreli
D.ershane Series
Copyright 2009-2012 Ali ehreli
0.1
1C@ >JIO@ION JA OC@ ??DGD.JMB #JMPH <I? 4DFD K<B@N =@GJIB OJ <GG JA OC@ H@H=@MN.
!?DGD.JMB D>@MDBD =T AGD @CM@GD DN GD>@IN@? PI?@M < M@<ODQ@ JHHJIN AOOMD=PODJI-
+JI>JHH@M>D<G-0C<M@ AGDF@ 3.0 2IDO@? 0O<O@N )D>@IN@.
B<N@? JI < RJMF <O ?DBDO<GH<MN.>JH.
-@MHDNNDJIN =@TJI? OC@ N>JK@ JA OCDN GD>@IN@ H<T =@ <Q<DG<=G@ <O COOK://??DGD.JMB/
>JKTMDBCO.COHG.
The PDF version of "Programming in D"
0.2
1C@ G<O@NO -!# Q@MNDJI JA OC@ =JJF ><I =@ ?JRIGJ<?@? <O OC@ AJGGJRDIB <??M@NN:
COOK://??DGD.JMB/?@MN/?.@I/-MJBM<HHDIB;DI;!.K?A
The revisions of the PDF file 0.2.1
t M685, 2014-07-24: 1C@ '*@HJMT *<I<B@H@IO' >C<KO@M.
t M677, 2014-05-29: A?? '1TK@ OM<DON' OJ OC@ 'JI?DODJI<G >JHKDG<ODJI' >C<KO@M.
t M672, 2014-05-20: 1C@ '!<O< 0C<MDIB JI>PMM@I>T' >C<KO@M.
t M646, 2013-11-01: 1C@ '*JM@ #PI>ODJIN', '*DSDIN', <I? '*JM@ /<IB@N' >C<KO@MN.
t M619, 2013-09-19: 1C@ '1PKG@N' <I? '*JM@ 1@HKG<O@N' >C<KO@MN.
t M598, 2013-07-09: 1C@ >J?@ @S<HKG@N DI OC@ '/<IB@N' >C<KO@M <M@ >JINO->JMM@>O <I? OC@T
O<F@ <?Q<IO<B@ JA 2#0.
t M588, 2013-06-28: 1C@ ')<=@GN <I? BJOJ' >C<KO@M.
t M585, 2013-06-26: 1C@ '2IDJIN' >C<KO@M.
t M581, 2013-06-14: 1C@ 'AJM@<>C RDOC 0OMP>ON <I? G<NN@N' >C<KO@M.
t M577, 2013-06-09: 1C@ '#PI>ODJI -JDIO@MN, !@G@B<O@N, <I? )<H=?<N' >C<KO@M.
t M571, 2013-06-03: 1C@ 'DN "SKM@NNDJI' >C<KO@M.
t M566, 2013-06-03: 1C@ 'JI?DODJI<G JHKDG<ODJI' >C<KO@M.
t M559, 2013-05-21: 1C@ 'BDO ,K@M<ODJIN' >C<KO@M.
t M547, 2013-01-31: 1C@ '2IDQ@MN<G #PI>ODJI <GG 0TIO<S (2#0)', '-MJK@MOD@N', 'JIOM<>O
-MJBM<HHDIB AJM 0OMP>ON <I? G<NN@N', '<GD<N', '<GD<N OCDN', <I? '-JDIO@MN' >C<KO@MN.
t M519, 2012-11-09: /@KG<>@ OJ%<NC() <GBJMDOCHN RDOC HJM@ @AAD>D@IO JI@N.
t M514, 2012-11-02: 1C@ 'G<NN@N', '&IC@MDO<I>@', ',=E@>O', '&IO@MA<>@N', '?@NOMJT <I? N>JK@?',
'*J?PG@N <I? )D=M<MD@N', <I? '"I><KNPG<ODJI <I? -MJO@>ODJI AOOMD=PO@N' >C<KO@MN.
t M506, 2012-09-15: 1C@ ',K@M<OJM ,Q@MGJ<?DIB' >C<KO@M <I? KM@QDJPN >C<KO@MN.
1C@ PK?<O@N OJ OC@ =JJF ><I =@ N@@I <O OC@ ??DGD.JMB KMJE@>O K<B@.
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
2
Acknowledgments 1
& RJPG? GDF@ OJ OC<IF HT RDA@ <I? ?<PBCO@M, RCJ @I?PM@? OC@ GJIB CJPMN <N HP>C <N & ?D?
RCDG@ RMDODIB OC@ JMDBDI<G 1PMFDNC Q@MNDJI <N R@GG <N OC@ "IBGDNC OM<ING<ODJI JA OCDN =JJF.
1C@N@ >C<KO@MN C<Q@ JMDBDI<GGT =@@I M@QD@R@? =T OC@ H@H=@MN JA OC@ 1PMFDNC !?DGD #JMPH. &
<H BM<O@APG OJ OC@ 1PMFDNC ! >JHHPIDOT AJM F@@KDIB HT @S>DO@H@IO <I? HJODQ<ODJI CDBC.
*@MO AO<JG, 7<A@M Y@G@IF, <I? 0<GDC !DI\@M C<Q@ BDQ@I A@@?=<>F JI <GHJNO @Q@MT GDI@ JA OC@
=JJF. <I AGK<T YDAO\D <I? #<MPF "M?@H ZI>@G C<Q@ =@@I DINOMPH@IO<G DI OC@ ?@Q@GJKH@IO JA
=JOC OCDN =JJF <I? JA ??DGD.JMB.
1C<IFN OJ OC@ AJGGJRDIB K@JKG@ AJM OC@DM Q@MT Q<GP<=G@ >JMM@>ODJIN, M@>JHH@I?<ODJIN, <I?
D?@<N: "MBDI $`I@T, 'JM?D 0<TJG, !<QD? %@M=@MOC, AI?M@ 1<HKP=JGJI, $JPM-$<?<?C<M< !<N<,
/<KC<^G '<FN@, AI?M@E *DOMJQD>, 'JC<II@N -A<P, '@MJH@ 0ID<O@>FD, '<NJI A?<HN, AGD %.
Y<GdfF<I, -<PG 'PM>U<F, BMD<I /JBJAA, ilrklm jpoksqn, 'JN@KC /PNCOJI 4<F@GDIB, 1JQ@,
%PBJ #GJM@IODIJ, 0<OT< -JOC<HN@OOD, )P_N *<MLP@N, CMDNOJKC 4@I?G@M.
1CDN =JJF C<N =@@I KMJJAM@<? =T "MBDI $`I@T OJ =@ @G@Q<O@? OJ "IBGDNC AMJH HT &IBGDNC.
A5=@AI>769?7@FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
3
Introduction 2
1CDN =JJF <DHN OJ O@<>C OC@ ! G<IBP<B@ OJ M@<?@MN RCJ <M@ I@R OJ >JHKPO@M KMJBM<HHDIB.
AGOCJPBC C<QDIB @SK@MD@I>@ DI JOC@M KMJBM<HHDIB G<IBP<B@N RJPG? >@MO<DIGT =@ C@GKAPG, OCDN
=JJF NO<MON AMJH OC@ =<ND>N. &A TJP <M@ DIO@M@NO@? DI G@<MIDIB CJR OJ KMJBM<H, & CJK@ TJP RDGG
ADI? OCDN =JJF C@GKAPG.
&I JM?@M AJM OCDN =JJF OJ =@ PN@APG, TJP RDGG I@@? <I @IQDMJIH@IO OJ RMDO@, >JHKDG@, <I? MPI
TJPM ! KMJBM<HN. 1CDN 67H7>AB?7@F 7@H;DA@?7@F HPNO DI>GP?@ <O G@<NO OC@ AJGGJRDIB:
t < O@SO @?DOJM
t < ! >JHKDG@M
&INO@<? JA J=O<DIDIB <I? DINO<GGDIB OCJN@ ORJ N@K<M<O@GT, TJP H<T >JIND?@M PNDIB <I
;@F79D3F76 67H7>AB?7@F 7@H;DA@?7@F (&!"). 6JP ><I ADI? DIAJMH<ODJI <=JPO ! @?DOJMN <I?
&!"N <O OC@ "?DOJMN <I? &!"N K<B@N <O ?G<IB.JMB. 6JP ><IIJO G@<MI OJ KMJBM<H DI ! RDOCJPO <
O@SO @?DOJM JM < >JHKDG@M. 4@ RDGG N@@ CJR OJ DINO<GG OC@ ENE >JHKDG@M <I? CJR OJ >JHKDG@
KMJBM<HN NJJI DI G<O@M >C<KO@MN.
"<>C >C<KO@M JA OCDN =JJF DIOMJ?P>@N <N A@R I@R >JI>@KON <N KJNND=G@. *JNO >C<KO@MN C<Q@ <
IPH=@M JA @S@M>DN@N <O OC@ @I?. 1C@N@ @S@M>DN@N <M@ <>>JHK<ID@? =T OC@DM NJGPODJIN, NJ OC<O
TJP ><I >JHK<M@ TJPM NJGPODJIN OJ HDI@. ,I OC@ R@= Q@MNDJI JA OCDN =JJF, OC@ NJGPODJIN
<KK@<M DI < I@R =MJRN@M RDI?JR. ,I OC@ -!# Q@MNDJI, OC@T <M@ <GG <O OC@ @I? JA OC@ =JJF.
& NPBB@NO OC<O TJP ?J IJO NFDK >C<KO@MN. &A TJP >JH@ <>MJNN >C<KO@MN OC<O TJP ADI? OJ =@
K<MOD>PG<MGT ?DAAD>PGO, DO H<T =@ =@><PN@ OC@ =JJF A<DGN OJ DIOMJ?P>@ <GG JA OC@ I@>@NN<MT
>JI>@KON. -G@<N@ @-H<DG OC@ <POCJM <=JPO NP>C KMJ=G@HN OJ C@GK H<F@ OCDN =JJF HJM@ PN@APG.
1CDN =JJF ?J@N IJO >JQ@M 9D3B:;53> GE7D ;@F7D8357 ($2&) KMJBM<HHDIB. AGOCJPBC H<IT
KMJBM<HN <M@ HJM@ PN<=G@ RDOC < $2&, $2&N <M@ IJO ?DM@>OGT M@G<O@? OJ KMJBM<HHDIB
G<IBP<B@N. #PMOC@MHJM@, OC@ ?@NDBI >CJD>@N <I? KMJBM<HHDIB NOTG@N JA $2&N H<T DIO@MA@M@
RDOC OCJN@ JA OC@ KMJBM<HHDIB G<IBP<B@ <I? DON NO<I?<M? GD=M<MT, <I? H<F@ DO HJM@ ?DAAD>PGO
OJ G@<MI OC@ G<IBP<B@. #JM OC<O M@<NJI, OCDN =JJF DI>GP?@N JIGT 5A@EA>7 KMJBM<HN. ,I>@ TJP
G@<MI ! <I? DON NO<I?<M? GD=M<MT, -CJ=JN, TJP RDGG =@ <=G@ OJ PN@ <IT $2& GD=M<MT OC<O TJP GDF@.
1C@ >C<KO@MN JA OCDN =JJF <M@ H<?@ <Q<DG<=G@ JIGDI@ <N OC@T <M@ OM<ING<O@? AMJH OC@DM 1PMFDNC
JMDBDI<GN. 6JP ><I PN@ OC@ /00 A@@? OJ =@ IJODAD@? <=JPO OC@ <Q<DG<=DGDOT JA I@R >C<KO@MN.
)@<MIDIB OJ KMJBM<H DN HJM@ API RC@I NC<M@? RDOC JOC@MN. $J OJ OC@ !.G@<MI I@RNBMJPK OJ
AJGGJR ?DN>PNNDJIN <I? OJ <NF <I? <INR@M LP@NODJIN.
-G@<N@ @-H<DG OC@ <POCJM RDOC <IT >JHH@ION <I? >JMM@>ODJIN M@B<M?DIB OCDN =JJF. 1C<IF TJP.
Author: AGD Y@CM@GD
t AGD Y@CM@GD'N M]NPH]
t &IO@MQD@RN RDOC CDH <O @QDU.I@O <I? <O cUHDM'?@ 0<I<O
t 6JP ><I ADI? CDH <O @QDU.I@O'N <I? ++ AJMPHN, <O OC@ !?DGD #JMPH, <I? <O OC@ !
I@RNBMJPKN
t 6JP ><I @-H<DG CDH <O <>@CM@GD@T<CJJ.>JH
!@FDA6G5F;A@
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
4
Practice of Programming 3
&O DN NJH@RC<O ?@=<O<=G@ RC<O OC@ KM<>OD>@ JA KMJBM<HHDIB @S<>OGT DN, =PO OC@ 5D38F <NK@>O JA
DO DN Q@MT NOMJIB. 0JH@ OCJPBCON <=JPO KMJBM<HHDIB:
t &O DN OC@ O<NF JA >M@<ODIB KMJBM<HN OC<O H<F@ OC@ >JHKPO@M =@C<Q@ OC@ R<T R@ R<IO.
t AN DO M@LPDM@N OJJGN <I? OC@ PN@ JA OCJN@ OJJGN BPD?@? =T OC@ @SK@MD@I>@ JA H<NO@M
KMJBM<HH@MN, DO DN < >M<AO.
t AN DO DIQJGQ@N NJGQDIB KMJ=G@HN PI?@M >JINOM<DION, DO DN @IBDI@@MDIB.
t &O DN Q@MT API <I? N<ODNATDIB.
t &O DN IJO < ADI@ <MO, =PO <N HP>C <N DO'N KJNND=G@ DI <ITOCDIB OC<O CPH<IN ?J, KMJBM<HN ><I
=@ RJMFN JA <MO.
t &O DN IJO < N>D@I>@, =PO OC@ H@OCJ?N OC<O DO PN@N <M@ >M@<O@? =T >JHKPO@M N>D@I>@.
Programming can be very hard to learn and teach
3.1
-MJBM<HHDIB C<N =@@I O<PBCO NDI>@ OC@ 1950N. 1C@M@ C<NI'O =@@I <I @AAD>D@IO JM NP>>@NNAPG
O@<>CDIB H@OCJ? ?@Q@GJK@? T@O.
2IAJMOPI<O@GT, G@<MIDIB OJ KMJBM<H ><I =@ < ?DAAD>PGO O<NF AJM MJPBCGT C<GA JA <GG NOP?@ION.
A>>JM?DIB OJ < M@N@<M>C K<K@M, OC@ JI@N RCJ ><I G@<MI KMJBM<HHDIB @<NDGT <M@ OCJN@ RCJ <M@
<=G@ OJ >M@<O@ 5A@E;EF7@F ?A67>E OJ ?@N>MD=@ OC@ PIFIJRI NDOP<ODJIN OC<O OC@T A<>@.
0JH@ JA OC@ ?DAAD>PGOT DI KMJBM<HHDIB >JH@N AMJH OC@ <HJPIO JA O@>CID><G ?@O<DGN OC<O I@@?
OJ =@ G@<MI@?.
&D35F;57 A8 &DA9D3??;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
5
The Hello World Program 4
1C@ ADMNO KMJBM<H OJ NCJR DI HJNO KMJBM<HHDIB G<IBP<B@ =JJFN DN OC@ :7>>A IAD>6 KMJBM<H.
1CDN Q@MT NCJMO <I? NDHKG@ KMJBM<H H@M@GT RMDO@N "C@GGJ RJMG?" <I? ADIDNC@N. 1CDN KMJBM<H DN
DHKJMO<IO =@><PN@ DO DI>GP?@N NJH@ JA OC@ @NN@IOD<G >JI>@KON JA OC<O G<IBP<B@.
%@M@ DN < :7>>A IAD>6 KMJBM<H DI !:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFMO(")FMMP XPSME!");
^
1C@ EAGD57 5A67 <=JQ@ I@@?N OJ =@ >JHKDG@? =T < ! >JHKDG@M OJ KMJ?P>@ <I @S@>PO<=G@
KMJBM<H.
Compiler installation
4.1
AO OC@ ODH@ JA RMDODIB OCDN >C<KO@M, OC@M@ <M@ OCM@@ ! >JHKDG@MN OJ >CJJN@ AMJH: ENE, OC@
!DBDO<G *<MN >JHKDG@M; HED, OC@ ! >JHKDG@M JA $; <I? MED, OC@ ! >JHKDG@M OC<O O<MB@ON OC@
))3* >JHKDG@M DIAM<NOMP>OPM@.
ENE DN OC@ ! >JHKDG@M OC<O C<N =@@I PN@? ?PMDIB OC@ ?@NDBI <I? ?@Q@GJKH@IO JA OC@ G<IBP<B@
JQ@M OC@ T@<MN. AGG JA OC@ @S<HKG@N DI OCDN =JJF C<Q@ =@@I O@NO@? RDOC ENE. #JM OC<O M@<NJI, DO
RJPG? =@ OC@ @<ND@NO AJM TJP OJ NO<MO RDOC ENE <I? OMT JOC@M >JHKDG@MN JIGT DA TJP C<Q@ <
NK@>DAD> I@@? OJ.
1J DINO<GG OC@ G<O@NO Q@MNDJI JA ENE, BJ OJ OC@ ?JRIGJ<? K<B@ <O !DBDO<G *<MN <I? N@G@>O OC@
>JHKDG@M =PDG? OC<O H<O>C@N TJPM >JHKPO@M @IQDMJIH@IO. 6JP HPNO N@G@>O OC@ ENE =PDG? OC<O
DN AJM TJPM JK@M<ODIB NTNO@H <I? K<>F<B@ H<I<B@H@IO NTNO@H, <I? RC@OC@M TJP C<Q@ <
32-=DO JM < 64-=DO -2. !J IJO DINO<GG < !1 >JHKDG@M. 1CDN =JJF >JQ@MN JIGT D H7DE;A@ FIA.
1C@ DINO<GG<ODJI NO@KN <M@ ?DAA@M@IO JI ?DAA@M@IO @IQDMJIH@ION =PO DO NCJPG? =@ <N @<NT <N
AJGGJRDIB NDHKG@ JI-N>M@@I DINOMP>ODJIN <I? =T >GD>FDIB < >JPKG@ JA =POOJIN.
Source file
4.2
1C@ ADG@ OC<O OC@ KMJBM<HH@M RMDO@N AJM OC@ ! >JHKDG@M OJ >JHKDG@ DN ><GG@? OC@ EAGD57 8;>7.
0DI>@ ! DN < >JHKDG@? G<IBP<B@, OC@ NJPM>@ ADG@ DON@GA DN IJO <I @S@>PO<=G@ KMJBM<H. 1C@
NJPM>@ ADG@ HPNO =@ >JIQ@MO@? OJ <I @S@>PO<=G@ KMJBM<H =T OC@ >JHKDG@M.
AN RDOC <IT ADG@, OC@ NJPM>@ ADG@ HPNO C<Q@ < I<H@. AGOCJPBC OC@ I<H@ ><I =@ <ITOCDIB OC<O DN
G@B<G JI OC@ ADG@ NTNO@H, DO DN >PNOJH<MT OJ PN@ OC@ .E 8;>7 7JF7@E;A@ AJM ! NJPM>@ ADG@N =@><PN@
?@Q@GJKH@IO @IQDMJIH@ION, KMJBM<HHDIB OJJGN, <I? KMJBM<HH@MN <GG @SK@>O OCDN OJ =@ OC@
><N@. #JM @S<HKG@, UFTU.E, HBNF.E, JOWPJDF.E, @O>. <M@ <KKMJKMD<O@ ! NJPM>@ ADG@ I<H@N.
Compiling the hello world program
4.3
JKT JM OTK@ OC@ C@GGJ RJMG? KMJBM<H <=JQ@ DIOJ < O@SO ADG@ <I? N<Q@ DO PI?@M OC@ I<H@
IFMMP.E.
1C@ >JHKDG@M RDGG NJJI >C@>F OC<O OC@ NTIO<S JA OCDN NJPM>@ >J?@ DN >JMM@>O (D.@. DO DN Q<GD?
<>>JM?DIB OJ OC@ G<IBP<B@ MPG@N) <I? H<F@ < KMJBM<H JPO JA DO =T OM<ING<ODIB DO DIOJ H<>CDI@
>J?@. 1J >JHKDG@ OC@ KMJBM<H AJGGJR OC@N@ NO@KN:
):7 7>>A ,AD>6 &DA9D3?
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
6
1. ,K@I < >JINJG@ RDI?JR.
2. $J OJ OC@ ?DM@>OJMT RC@M@ TJP N<Q@? IFMMP.E.
3. "IO@M OC@ AJGGJRDIB >JHH<I?. (!J IJO OTK@ OC@ $ >C<M<>O@M; DO DN OC@M@ OJ DI?D><O@ OC@
>JHH<I? GDI@ KMJHKO.)
$ ENE IFMMP.E
&A TJP ?D? IJO H<F@ <IT HDNO<F@, TJP H<T OCDIF OC<O IJOCDIB C<Q@ C<KK@I@?. 1J OC@ >JIOM<MT,
DO H@<IN OC<O @Q@MTOCDIB R@IO R@GG. 1C@M@ NCJPG? =@ <I @S@>PO<=G@ ADG@ I<H@? IFMMP (JM
IFMMP.FYF PI?@M 4DI?JRN) OC<O C<N EPNO =@@I >M@<O@? =T OC@ >JHKDG@M.
&A OC@ >JHKDG@M C<N DINO@<? KMDIO@? NJH@ H@NN<B@N, TJP KMJ=<=GT C<Q@ H<?@ < HDNO<F@ RC@I
>JKTDIB OC@ KMJBM<H >J?@. 1MT OJ D?@IODAT OC@ HDNO<F@, >JMM@>O DO, <I? M@OMT >JHKDGDIB. 6JP
RDGG MJPODI@GT H<F@ H<IT HDNO<F@N RC@I KMJBM<HHDIB, NJ OC@ KMJ>@NN JA >JMM@>ODIB <I?
>JHKDGDIB RDGG =@>JH@ A<HDGD<M OJ TJP.
,I>@ OC@ KMJBM<H C<N =@@I >M@<O@? NP>>@NNAPGGT, OTK@ OC@ I<H@ JA OC@ @S@>PO<=G@ KMJBM<H OJ
MPI DO. 6JP NCJPG? N@@ OC<O OC@ KMJBM<H KMDION "%@GGJ RJMG?!":
$ ./IFMMP l SVOOJOH UIF QSPHSBN
)FMMP XPSME! l UIF NFTTBHF UIBU JU QSJOUT
JIBM<OPG<ODJIN! 6JPM ADMNO ! KMJBM<H RJMFN <N @SK@>O@?.
Compiler switches
4.4
1C@ >JHKDG@M C<N H<IT >JHH<I? GDI@ NRDO>C@N OC<O <M@ PN@? AJM DIAGP@I>DIB CJR DO >JHKDG@N
OC@ KMJBM<H. 1J N@@ < GDNO JA >JHKDG@M NRDO>C@N @IO@M JIGT OC@ I<H@ JA OC@ >JHKDG@M:
$ ENE l FOUFS POMZ UIF OBNF
%.%64 % $PNQJMFS W2.059
$PQZSJHIU (D) 1999-2012 CZ %JHJUBM .BST XSJUUFO CZ 8BMUFS #SJHIU
%PDVNFOUBUJPO: IUUQ://XXX.EMBOH.PSH/JOEFY.IUNM
6TBHF:
ENE GJMFT.E ... \ -TXJUDI ^
GJMFT.E % TPVSDF GJMFT
...
-VOJUUFTU DPNQJMF JO VOJU UFTUT
...
-X FOBCMF XBSOJOHT
...
1C@ <==M@QD<O@? JPOKPO <=JQ@ NCJRN JIGT OC@ >JHH<I? GDI@ NRDO>C@N OC<O & M@>JHH@I? OC<O
TJP <GR<TN PN@. AGOCJPBC DO H<F@N IJ ?DAA@M@I>@ RDOC OC@ C@GGJ RJMG? KMJBM<H DI OCDN >C<KO@M,
OC@ AJGGJRDIB >JHH<I? GDI@ RJPG? >JHKDG@ OC@ KMJBM<H =T @I<=GDIB R<MIDIBN <I? PIDO O@NON.
4@ RDGG N@@ OC@N@ <I? JOC@M NRDO>C@N DI HJM@ ?@O<DG DI G<O@M >C<KO@MN:
$ ENE IFMMP.E -X -VOJUUFTU
1C@ >JHKG@O@ GDNO JA ENE >JHH<I? GDI@ NRDO>C@N ><I =@ AJPI? <O !*! JHKDG@M
?J>PH@IO<ODJI.
):7 7>>A ,AD>6 &DA9D3?
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
7
,I@ JOC@M >JHH<I? GDI@ NRDO>C OC<O TJP H<T ADI? PN@APG DN -SVO. &O >JHKDG@N OC@ NJPM>@ >J?@,
KMJ?P>@N OC@ @S@>PO<=G@ KMJBM<H, <I? MPIN DO RDOC < NDIBG@ >JHH<I?:
$ ENE -SVO IFMMP.E -X -VOJUUFTU
)FMMP XPSME! l UIF QSPHSBN JT BVUPNBUJDBMMZ FYFDVUFE
IDE
4.5
&I <??DODJI OJ OC@ >JHKDG@M, TJP H<T <GNJ >JIND?@M DINO<GGDIB <I &!" (DIO@BM<O@? ?@Q@GJKH@IO
@IQDMJIH@IO). &!"N <M@ ?@NDBI@? OJ H<F@ KMJBM<H ?@Q@GJKH@IO @<ND@M =T NDHKGDATDIB OC@
NO@KN JA RMDODIB, >JHKDGDIB, <I? ?@=PBBDIB.
&A TJP ?J DINO<GG <I &!", >JHKDGDIB <I? MPIIDIB OC@ KMJBM<H RDGG =@ <N NDHKG@ <N KM@NNDIB <
F@T JM >GD>FDIB < =POOJI JI OC@ &!". & NODGG M@>JHH@I? OC<O TJP A<HDGD<MDU@ TJPMN@GA RDOC
>JHKDGDIB KMJBM<HN H<IP<GGT DI < >JINJG@ RDI?JR.
&A TJP ?@>D?@ OJ DINO<GG <I &!", BJ OJ OC@ &!"N K<B@ <O ?G<IB.JMB OJ N@@ < GDNO JA <Q<DG<=G@ &!"N.
Contents of the hello world program
4.6
%@M@ DN < LPD>F GDNO JA OC@ H<IT ! >JI>@KON OC<O C<Q@ <KK@<M@? DI OCDN NCJMO KMJBM<H:
Core feature: "Q@MT G<IBP<B@ ?@ADI@N DON NTIO<S, API?<H@IO<G OTK@N, F@TRJM?N, MPG@N, @O>. AGG
JA OC@N@ H<F@ OC@ 5AD7 873FGD7E JA OC<O G<IBP<B@. 1C@ K<M@IOC@N@N, N@HD>JGJIN, <I? RJM?N
GDF@ NBJO <I? WPJE <M@ <GG <>>JM?DIB OJ OC@ MPG@N JA !. 1C@N@ <M@ NDHDG<M OJ OC@ MPG@N JA
"IBGDNC: NP=E@>O, Q@M=, KPI>OP<ODJI, N@IO@I>@ NOMP>OPM@, @O>.
Keyword: 0K@>D<G RJM?N OC<O <M@ < K<MO JA OC@ >JM@ A@<OPM@N JA OC@ G<IBP<B@ <M@ =7KIAD6E.
1C@M@ <M@ ORJ F@TRJM?N DI OCDN KMJBM<H: JNQPSU, RCD>C DN PN@? OJ DIOMJ?P>@ < HJ?PG@ OJ OC@
KMJBM<H; <I? WPJE, RCD>C C@M@ H@<IN "IJO M@OPMIDIB <ITOCDIB".
Library and function: 1C@ >JM@ A@<OPM@N ?@ADI@ JIGT OC@ NOMP>OPM@ JA OC@ G<IBP<B@. 1C@T <M@
PN@? AJM ?@ADIDIB API>ODJIN <I? PN@M OTK@N, <I? OCJN@ DI OPMI <M@ PN@? AJM =PDG?DIB GD=M<MD@N.
)D=M<MD@N <M@ >JGG@>ODJIN JA M@PN<=G@ KMJBM<H K<MON OC<O B@O >;@=76 RDOC TJPM KMJBM<HN OJ
C@GK OC@H <>CD@Q@ OC@DM KPMKJN@N.
XSJUFMO <=JQ@ DN < 8G@5F;A@ DI !'N NO<I?<M? >;4D3DK. &O DN PN@? AJM KMDIODIB < GDI@ JA O@SO, <N DON
I<H@ NPBB@NON: RMDO@ GDI@.
Module: )D=M<MT >JIO@ION <M@ BMJPK@? =T OTK@N JA O<NFN OC<O OC@T DIO@I? OJ C@GK RDOC. 0P>C <
BMJPK DN ><GG@? < HJ?PG@. 1C@ JIGT HJ?PG@ OC<O OCDN KMJBM<H PN@N DN TUE.TUEJP, RCD>C
C<I?G@N ?<O< DIKPO <I? JPOKPO.
Character and string: "SKM@NNDJIN GDF@ ")FMMP XPSME!" <M@ ><GG@? EFD;@9E, <I? OC@
@G@H@ION JA NOMDIBN <M@ ><GG@? 5:3D35F7DE. 1C@ JIGT NOMDIB DI OCDN KMJBM<H >JIO<DIN >C<M<>O@MN
')', 'F', '!', <I? JOC@MN.
Order of operations: -MJBM<HN >JHKG@O@ OC@DM O<NFN =T @S@>PODIB JK@M<ODJIN DI < >@MO<DI
JM?@M. 1C@N@ O<NFN NO<MO RDOC OC@ JK@M<ODJIN OC<O <M@ RMDOO@I DI OC@ API>ODJI I<H@? NBJO. 1C@
JIGT JK@M<ODJI DI OCDN KMJBM<H RMDO@N "%@GGJ RJMG?!".
Significance of uppercase and lowercase letters: 6JP ><I >CJJN@ OJ OTK@ <IT >C<M<>O@M
DIND?@ NOMDIBN, =PO TJP HPNO OTK@ OC@ JOC@M >C<M<>O@MN @S<>OGT <N OC@T <KK@<M DI OC@ KMJBM<H.
1CDN DN =@><PN@ GJR@M><N@ QN. PKK@M><N@ DN NDBIDAD><IO DI ! KMJBM<HN. #JM @S<HKG@, XSJUFMO
<I? 8SJUFMO <M@ ORJ ?DAA@M@IO I<H@N.
4@ RDGG >JQ@M <GG JA OC@ A@<OPM@N JA ! DI HJM@ ?@O<DG DI OC@ AJGGJRDIB >C<KO@MN.
):7 7>>A ,AD>6 &DA9D3?
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
8
Exercises
4.7
1. *<F@ OC@ KMJBM<H JPOKPO NJH@OCDIB @GN@.
2. C<IB@ OC@ KMJBM<H OJ JPOKPO HJM@ OC<I JI@ GDI@.
3. 1MT OJ >JHKDG@ OC@ KMJBM<H <AO@M H<FDIB JOC@M >C<IB@N; @.B. M@HJQ@ OC@ N@HD>JGJI <O OC@
@I? JA OC@ GDI@ RDOC XSJUFMO <I? J=N@MQ@ < >JHKDG<ODJI @MMJM.
... F:7 EA>GF;A@E
):7 7>>A ,AD>6 &DA9D3?
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
9
XSJUFMO and XSJUF 5
&I OC@ KM@QDJPN >C<KO@M R@ C<Q@ N@@I OC<O XSJUFMO O<F@N < NOMDIB RDOCDI K<M@IOC@N@N <I?
KMDION OC@ NOMDIB.
1C@ K<MON JA KMJBM<HN OC<O <>OP<GGT ?J RJMF <M@ ><GG@? 8G@5F;A@E <I? OC@ DIAJMH<ODJI OC<O
OC@T I@@? OJ >JHKG@O@ OC@DM RJMF <M@ ><GG@? B3D3?7F7DE. 1C@ <>O JA BDQDIB NP>C DIAJMH<ODJI OJ
API>ODJIN DN ><GG@? B3EE;@9 B3D3?7F7D H3>G7E OJ OC@H. -<M<H@O@MN <M@ K<NN@? OJ API>ODJIN
RDOCDI K<M@IOC@N@N, N@K<M<O@? =T >JHH<N.
Note: ):7 IAD6 K<M<H@O@M 67E5D;47E F:7 ;@8AD?3F;A@ F:3F ;E B3EE76 FA 3 8G@5F;A@ 3F F:7
5A@57BFG3> >7H7>. ):7 5A@5D7F7 ;@8AD?3F;A@ F:3F ;E 35FG3>>K B3EE76 6GD;@9 F:7 7J75GF;A@ A8 F:7
BDA9D3? ;E 53>>76 3@ <MBPH@IO . A>F:AG9: ;@5ADD75F, F:7E7 F7D?E 3D7 EA?7F;?7E GE76
;@F7D5:3@934>K ;@ F:7 EA8FI3D7 ;@6GEFDK.
XSJUFMO ><I O<F@ HJM@ OC<I JI@ <MBPH@IO. &O KMDION OC@H JI@ <AO@M OC@ JOC@M JI OC@ N<H@
GDI@:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFMO(")FMMP XPSME!", ")FMMP GJTI!");
^
0JH@ODH@N, <GG JA OC@ DIAJMH<ODJI OC<O DN OJ =@ KMDIO@? JI OC@ N<H@ GDI@ H<T IJO =@ M@<?DGT
<Q<DG<=G@ OJ =@ K<NN@? OJ XSJUFMO. &I NP>C ><N@N, OC@ ADMNO K<MON JA OC@ GDI@ H<T =@ KMDIO@? =T
XSJUF <I? OC@ G<NO K<MO JA OC@ GDI@ H<T =@ KMDIO@? =T XSJUFMO.
XSJUFMO <?Q<I>@N OJ OC@ I@SO GDI@, XSJUF NO<TN JI OC@ N<H@ GDI@:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
// -FU'T GJSTU QSJOU XIBU XF IBWF BWBJMBCMF:
XSJUF(")FMMP");
// ... MFU'T BTTVNF NPSF PQFSBUJPOT BU UIJT QPJOU ...
XSJUF("XPSME!");
// ... BOE GJOBMMZ:
XSJUFMO();
^
<GGDIB XSJUFMO RDOCJPO <IT K<M<H@O@M H@M@GT >JHKG@O@N OC@ >PMM@IO GDI@.
)DI@N OC<O NO<MO RDOC // <M@ ><GG@? 5A??7@F >;@7E JM =MD@AGT 5A??7@FE. A >JHH@IO DN IJO <
K<MO JA OC@ KMJBM<H >J?@ DI OC@ N@IN@ OC<O DO ?J@NI'O <AA@>O OC@ =@C<QDJM JA OC@ KMJBM<H. &ON
JIGT KPMKJN@ DN OJ @SKG<DI RC<O OC@ >J?@ ?J@N DI OC<O K<MOD>PG<M N@>ODJI JA OC@ KMJBM<H. 1C@
<P?D@I>@ JA < >JHH@IO DN <IT=J?T RCJ H<T =@ M@<?DIB OC@ KMJBM<H >J?@ G<O@M, DI>GP?DIB OC@
KMJBM<HH@M RCJ RMJO@ OC@ >JHH@IO DI OC@ ADMNO KG<>@.
Exercises
5.1
1. BJOC JA OC@ KMJBM<HN DI OCDN >C<KO@M KMDIO OC@ NOMDIBN RDOCJPO <IT NK<>@N =@OR@@I OC@H.
C<IB@ OC@ KMJBM<HN NJ OC<O OC@M@ DN NK<>@ =@OR@@I OC@ <MBPH@ION <N DI "%@GGJ RJMG?!".
2. 1MT ><GGDIB XSJUF RDOC HJM@ OC<I JI@ K<M<H@O@M <N R@GG.
ID;F7>@ 3@6 ID;F7
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
10
... F:7 EA>GF;A@E
ID;F7>@ 3@6 ID;F7
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
11
Compiler 6
4@ C<Q@ N@@I OC<O OC@ ORJ OJJGN OC<O <M@ PN@? HJNO DI ! KMJBM<HHDIB <M@ F:7 F7JF 76;FAD <I?
F:7 5A?B;>7D. ! KMJBM<HN <M@ RMDOO@I DI O@SO @?DOJMN.
1C@ >JI>@KO JA >JHKDG<ODJI <I? OC@ API>ODJI JA OC@ >JHKDG@M HPNO <GNJ =@ PI?@MNOJJ? RC@I
PNDIB 5A?B;>76 G<IBP<B@N GDF@ !.
Machine code
6.1
1C@ =M<DI JA OC@ >JHKPO@M DN OC@ HD>MJKMJ>@NNJM (JM OC@ -2, NCJMO AJM 57@FD3> BDA57EE;@9
G@;F). 1@GGDIB OC@ -2 RC<O OJ ?J DN ><GG@? 5A6;@9, <I? OC@ DINOMP>ODJIN OC<O <M@ PN@? RC@I
?JDIB NJ <M@ ><GG@? ?35:;@7 5A67.
*JNO -2 <M>CDO@>OPM@N PN@ H<>CDI@ >J?@ NK@>DAD> OJ OC<O K<MOD>PG<M <M>CDO@>OPM@. 1C@N@
H<>CDI@ >J?@ DINOMP>ODJIN <M@ ?@O@MHDI@? PI?@M C<M?R<M@ >JINOM<DION ?PMDIB OC@ ?@NDBI
NO<B@ JA OC@ <M>CDO@>OPM@. AO OC@ GJR@NO G@Q@G OC@N@ H<>CDI@ >J?@ DINOMP>ODJIN <M@
DHKG@H@IO@? <N @G@>OMD><G NDBI<GN. B@><PN@ OC@ @<N@ JA >J?DIB DN IJO < KMDH<MT >JIND?@M<ODJI
<O OCDN G@Q@G, RMDODIB KMJBM<HN ?DM@>OGT DI OC@ AJMH JA OC@ H<>CDI@ >J?@ JA OC@ -2 DN < Q@MT
?DAAD>PGO O<NF.
1C@N@ H<>CDI@ >J?@ DINOMP>ODJIN <M@ NK@>D<G IPH=@MN, RCD>C M@KM@N@IO Q<MDJPN JK@M<ODJIN
NPKKJMO@? =T OC@ -2. #JM @S<HKG@, AJM <I DH<BDI<MT 8-=DO -2, OC@ IPH=@M 4 HDBCO
M@KM@N@IO OC@ JK@M<ODJI JA GJ<?DIB, OC@ IPH=@M 5 HDBCO M@KM@N@IO OC@ JK@M<ODJI JA NOJMDIB,
<I? OC@ IPH=@M 6 HDBCO M@KM@N@IO OC@ JK@M<ODJI JA DI>M@H@IODIB. ANNPHDIB OC<O OC@ G@AOHJNO
3 =DON <M@ OC@ JK@M<ODJI IPH=@M <I? OC@ MDBCOHJNO 5 =DON <M@ OC@ Q<GP@ OC<O DN PN@? DI OC<O
JK@M<ODJI, < N<HKG@ KMJBM<H DI H<>CDI@ >J?@ AJM OCDN -2 HDBCO GJJF GDF@ OC@ AJGGJRDIB:
0QFSBUJPO 7BMVF .FBOJOH
100 11110 -0"% 11110
101 10100 4503& 10100
110 10100 */$3&.&/5 10100
000 00000 1"64&
B@DIB NJ >GJN@ OJ C<M?R<M@, H<>CDI@ >J?@ DN IJO NPDO<=G@ AJM M@KM@N@IODIB CDBC@M G@Q@G
>JI>@KON GDF@ 3 B>3K;@9 53D6 JM 3 EFG67@F D75AD6.
Programming language
6.2
-MJBM<HHDIB G<IBP<B@N <M@ ?@NDBI@? <N @AAD>D@IO R<TN JA KMJBM<HHDIB < -2, ><K<=G@ JA
M@KM@N@IODIB CDBC@M-G@Q@G >JI>@KON. -MJBM<HHDIB G<IBP<B@N ?J IJO C<Q@ OJ ?@<G RDOC
C<M?R<M@ >JINOM<DION; OC@DM H<DI KPMKJN@N <M@ @<N@ JA PN@ <I? @SKM@NNDQ@I@NN. -MJBM<HHDIB
G<IBP<B@N <M@ @<ND@M AJM CPH<IN OJ PI?@MNO<I?, >GJN@M OJ I<OPM<G G<IBP<B@N:
JG (B@DBSE@IBT@CFFO@QMBZFE()) \
EJTQMBZ@UIF@DBSE();
^
%JR@Q@M, KMJBM<HHDIB G<IBP<B@N <?C@M@ OJ HP>C HJM@ NOMD>O <I? AJMH<G MPG@N OC<I <IT
NKJF@I G<IBP<B@.
CA?B;>7D
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
12
Compiled languages
6.3
&I NJH@ KMJBM<HHDIB G<IBP<B@N, OC@ KMJBM<H DINOMP>ODJIN HPNO =@ >JHKDG@? =@AJM@
=@>JHDIB < KMJBM<H OJ =@ @S@>PO@?. 0P>C G<IBP<B@N KMJ?P>@ A<NO @S@>PODIB KMJBM<HN =PO
OC@ ?@Q@GJKH@IO KMJ>@NN DIQJGQ@N ORJ H<DI NO@KN: RMDODIB OC@ KMJBM<H <I? >JHKDGDIB DO.
&I B@I@M<G, >JHKDG@? G<IBP<B@N C@GK RDOC ><O>CDIB KMJBM<HHDIB @MMJMN =@AJM@ OC@ KMJBM<H
@Q@I NO<MON MPIIDIB.
! DN < >JHKDG@? G<IBP<B@.
Interpreted languages
6.4
0JH@ KMJBM<HHDIB G<IBP<B@N ?JI'O M@LPDM@ >JHKDG<ODJI. 0P>C G<IBP<B@N <M@ ><GG@?
;@F7DBD7F76 >3@9G397E. -MJBM<HN ><I =@ @S@>PO@? ?DM@>OGT AMJH OC@ C<I?-RMDOO@I KMJBM<H.
0JH@ @S<HKG@N JA DIO@MKM@O@? G<IBP<B@N <M@ -TOCJI, /P=T, <I? -@MG. B@><PN@ OC@M@ DN IJ
>JHKDG<ODJI NO@K, KMJBM<H ?@Q@GJKH@IO ><I =@ @<ND@M AJM OC@N@ G<IBP<B@N. ,I OC@ JOC@M
C<I?, <N OC@ DINOMP>ODJIN JA OC@ KMJBM<H HPNO =@ K<MN@? OJ =@ DIO@MKM@O@? @Q@MT ODH@ OC@
KMJBM<H DN @S@>PO@?, KMJBM<HN OC<O <M@ RMDOO@I DI OC@N@ G<IBP<B@N <M@ NGJR@M OC<I OC@DM
@LPDQ<G@ION RMDOO@I DI >JHKDG@? G<IBP<B@N.
&I B@I@M<G, AJM <I DIO@MKM@O@? G<IBP<B@, H<IT OTK@N JA @MMJMN DI OC@ KMJBM<H ><IIJO =@
?DN>JQ@M@? PIODG OC@ KMJBM<H NO<MON MPIIDIB.
Compiler
6.5
1C@ KPMKJN@ JA < >JHKDG@M DN OM<ING<ODJI: &O OM<ING<O@N KMJBM<HN RMDOO@I DI < KMJBM<HHDIB
G<IBP<B@ DIOJ H<>CDI@ >J?@. 1CDN DN < OM<ING<ODJI AMJH OC@ G<IBP<B@ JA OC@ KMJBM<HH@M OJ
OC@ G<IBP<B@ JA OC@ -2. 1CDN OM<ING<ODJI DN ><GG@? 5A?B;>3F;A@. "Q@MT >JHKDG@M PI?@MNO<I?N <
K<MOD>PG<M KMJBM<HHDIB G<IBP<B@ <I? DN ?@N>MD=@? <N < >JHKDG@M JA OC<O G<IBP<B@, <N DI "< !
>JHKDG@M".
Compilation error
6.6
AN OC@ >JHKDG@M >JHKDG@N < KMJBM<H <>>JM?DIB OJ OC@ MPG@N JA OC@ G<IBP<B@, DO NOJKN OC@
>JHKDG<ODJI <N NJJI <N DO >JH@N <>MJNN ;>>793> DINOMP>ODJIN. &GG@B<G DINOMP>ODJIN <M@ OC@ JI@N
OC<O <M@ JPOND?@ OC@ NK@>DAD><ODJIN JA OC@ G<IBP<B@. -MJ=G@HN GDF@ < HDNH<O>C@? K<M@IOC@NDN,
< HDNNDIB N@HD>JGJI, < HDNNK@GG@? F@TRJM?, @O>. <GG ><PN@ >JHKDG<ODJI @MMJMN.
1C@ >JHKDG@M H<T <GNJ @HDO < 5A?B;>3F;A@ I3D@;@9 RC@I DO N@@N < NPNKD>DJPN KD@>@ JA >J?@
OC<O H<T ><PN@ >JI>@MI =PO IJO I@>@NN<MDGT <I @MMJM. %JR@Q@M, R<MIDIBN <GHJNO <GR<TN
DI?D><O@ <I <>OP<G @MMJM JM =<? NOTG@, NJ DO DN < >JHHJI KM<>OD>@ OJ >JIND?@M HJNO JM <GG
R<MIDIBN <N @MMJMN.
CA?B;>7D
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
13
Fundamental Types 7
4@ C<Q@ N@@I OC<O OC@ =M<DI JA < >JHKPO@M DN OC@ -2. *JNO JA OC@ O<NFN JA < KMJBM<H <M@
K@MAJMH@? =T OC@ -2 <I? OC@ M@NO <M@ ?DNK<O>C@? OJ JOC@M K<MON JA OC@ >JHKPO@M.
1C@ NH<GG@NO PIDO JA ?<O< DI < >JHKPO@M DN ><GG@? 3 4;F, RCD>C ><I C<Q@ OC@ Q<GP@ 0 JM 1.
0DI>@ < OTK@ JA ?<O< OC<O ><I CJG? JIGT OC@ Q<GP@N 0 <I? 1 RJPG? C<Q@ Q@MT GDHDO@? PN@, OC@
-2 ?@ADI@N G<MB@M ?<O< OTK@N OC<O <M@ >JH=DI<ODJIN JA HJM@ OC<I JI@ =DO. AN <I @S<HKG@, <
4KF7 >JINDNON JA 8 =DON. 1C@ HJNO @AAD>D@IO ?<O< OTK@ JA < -2 H<F@N OC<O -2 <I $-4;F C&*:
<N DI 32-=DO -2, 64-=DO -2, @O>.
1C@ ?<O< OTK@N OC<O OC@ -2 ?@ADI@N <M@ NODGG IJO NPAAD>D@IO: OC@T ><I'O M@KM@N@IO CDBC@M G@Q@G
>JI>@KON GDF@ @3?7 A8 3 EFG67@F JM 3 B>3K;@9 53D6. ! KMJQD?@N H<IT PN@APG ?<O< OTK@N, =PO
@Q@I OC@N@ OTK@N <M@ IJO NPAAD>D@IO OJ M@KM@N@IO H<IT CDBC@M G@Q@G >JI>@KON. 0P>C >JI>@KON
HPNO =@ ?@ADI@? =T OC@ KMJBM<HH@M <N EFDG5FE <I? 5>3EE7E, RCD>C R@ RDGG N@@ DI G<O@M
>C<KO@MN.
!'N 8G@63?7@F3> FKB7E <M@ Q@MT NDHDG<M OJ OC@ API?<H@IO<G OTK@N JA H<IT JOC@M G<IBP<B@N, <N
N@@I DI OC@ AJGGJRDIB O<=G@. 1C@ O@MHN OC<O <KK@<M DI OC@ O<=G@ <M@ @SKG<DI@? =@GJR:
%'T 'VOEBNFOUBM %BUB 5ZQFT
5ZQF %FGJOJUJPO *OJUJBM 7BMVF
CPPM #PPMFBO UZQF GBMTF
CZUF TJHOFE 8 CJUT 0
VCZUF VOTJHOFE 8 CJUT 0
TIPSU TJHOFE 16 CJUT 0
VTIPSU VOTJHOFE 16 CJUT 0
JOU TJHOFE 32 CJUT 0
VJOU VOTJHOFE 32 CJUT 0
MPOH TJHOFE 64 CJUT 0-
VMPOH VOTJHOFE 64 CJUT 0-
GMPBU 32-CJU GMPBUJOH QPJOU GMPBU.OBO
EPVCMF 64-CJU GMPBUJOH QPJOU EPVCMF.OBO
SFBM FJUIFS UIF MBSHFTU GMPBUJOH QPJOU UZQF UIBU UIF
IBSEXBSF TVQQPSUT, PS EPVCMF; XIJDIFWFS JT MBSHFS
SFBM.OBO
JGMPBU JNBHJOBSZ WBMVF UZQF PG GMPBU GMPBU.OBO * 1.0J
JEPVCMFJNBHJOBSZ WBMVF UZQF PG EPVCMF EPVCMF.OBO *
1.0J
JSFBM JNBHJOBSZ WBMVF UZQF PG SFBM SFBM.OBO * 1.0J
DGMPBU DPNQMFY OVNCFS UZQF NBEF PG UXP GMPBUT GMPBU.OBO +
GMPBU.OBO * 1.0J
DEPVCMFDPNQMFY OVNCFS UZQF NBEF PG UXP EPVCMFT EPVCMF.OBO +
EPVCMF.OBO *
1.0J
DSFBM DPNQMFY OVNCFS UZQF NBEF PG UXP SFBMT SFBM.OBO +
SFBM.OBO * 1.0J
DIBS 65'-8 DPEF VOJU 0Y''
XDIBS 65'-16 DPEF VOJU 0Y''''
EDIBS 65'-32 DPEF VOJU BOE 6OJDPEF DPEF QPJOU 0Y0000''''
FG@63?7@F3> )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
14
&I <??DODJI OJ OC@ <=JQ@, OC@ F@TRJM? WPJE M@KM@N@ION :3H;@9 @A FKB7. 1C@ F@TRJM?N DFOU
<I? VDFOU <M@ M@N@MQ@? AJM APOPM@ PN@ OJ M@KM@N@IO NDBI@? <I? PINDBI@? 128 =DO Q<GP@N.
2IG@NN OC@M@ DN < NK@>DAD> M@<NJI IJO OJ, TJP ><I PN@ JOU OJ M@KM@N@IO RCJG@ Q<GP@N. 1J
M@KM@N@IO >JI>@KON OC<O ><I C<Q@ AM<>ODJI<G Q<GP@N, >JIND?@M EPVCMF.
1C@ AJGGJRDIB <M@ OC@ O@MHN OC<O <KK@<M@? DI OC@ O<=G@:
Boolean: 1C@ OTK@ JA GJBD><G @SKM@NNDJIN, C<QDIB OC@ Q<GP@ USVF AJM OMPOC <I? GBMTF AJM
A<GNDOT.
Signed type: A OTK@ OC<O ><I C<Q@ I@B<ODQ@ <I? KJNDODQ@ Q<GP@N. #JM @S<HKG@ CZUF ><I C<Q@
Q<GP@N AMJH -128 OJ 127. 1C@ I<H@N JA OC@N@ OTK@N >JH@ AMJH OC@ I@B<ODQ@ E;9@.
Unsigned type: A OTK@ OC<O ><I C<Q@ JIGT KJNDODQ@ Q<GP@N. #JM @S<HKG@ VCZUF ><I C<Q@
Q<GP@N AMJH 0 OJ 255. 1C@ V <O OC@ =@BDIIDIB JA OC@ I<H@ JA OC@N@ OTK@N >JH@N AMJH
G@E;9@76.
Floating point: 1C@ OTK@ OC<O ><I M@KM@N@IO Q<GP@N RDOC ?@>DH<G AM<>ODJIN <N DI 1.25. 1C@
KM@>DNDJI JA AGJ<ODIB KJDIO ><G>PG<ODJIN <M@ ?DM@>OGT M@G<O@? OJ OC@ =DO >JPIO JA OC@ OTK@: CDBC@M
OC@ =DO >JPIO, HJM@ KM@>DN@ OC@ M@NPGON.
,IGT AGJ<ODIB KJDIO OTK@N ><I M@KM@N@IO ?@>DH<G AM<>ODJIN; DIO@B@M OTK@N GDF@ JOU ><I JIGT
M@KM@N@IO RCJG@ Q<GP@N GDF@ 1 <I? 2.
Complex number type: 1C@ OTK@ OC<O ><I M@KM@N@IO OC@ >JHKG@S IPH=@MN JA H<OC@H<OD>N.
Imaginary number type: 1C@ OTK@ OC<O M@KM@N@ION JIGT OC@ DH<BDI<MT K<MO JA >JHKG@S
IPH=@MN. 1C@ J OC<O <KK@<MN DI OC@ &IDOD<G 3<GP@ >JGPHI DN OC@ NLP<M@ MJJO JA -1 DI
H<OC@H<OD>N.
nan: 0CJMO AJM "IJO < IPH=@M", M@KM@N@IODIB ;@H3>;6 8>A3F;@9 BA;@F H3>G7.
Properties of types
7.1
! OTK@N C<Q@ BDAB7DF;7E. -MJK@MOD@N <M@ <>>@NN@? RDOC < ?JO <AO@M OC@ I<H@ JA OC@ OTK@. #JM
@S<HKG@ OC@ TJ[FPG KMJK@MOT JA JOU DN <>>@NN@? <N JOU.TJ[FPG. 4@ RDGG N@@ JIGT AJPM JA
OC@N@ <OOMD=PO@N DI OCDN >C<KO@M:
t TUSJOHPG DN OC@ I<H@ JA OC@ OTK@
t TJ[FPG DN OC@ G@IBOC JA OC@ OTK@ DI O@MHN JA =TO@N. (&I JM?@M OJ ?@O@MHDI@ OC@ =DO >JPIO,
OCDN Q<GP@ HPNO =@ HPGODKGD@? =T 8, OC@ IPH=@M JA =DON DI < =TO@.)
t NJO DN NCJMO AJM "HDIDHPH"; OCDN DN OC@ NH<GG@NO Q<GP@ OC<O OC@ OTK@ ><I C<Q@
t NBY DN NCJMO AJM "H<SDHPH"; OCDN DN OC@ G<MB@NO Q<GP@ OC<O OC@ OTK@ ><I C<Q@
%@M@ DN < KMJBM<H OC<O KMDION OC@N@ KMJK@MOD@N AJM JOU:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFMO("5ZQF : ", JOU.TUSJOHPG);
XSJUFMO("-FOHUI JO CZUFT: ", JOU.TJ[FPG);
XSJUFMO(".JOJNVN WBMVF : ", JOU.NJO);
XSJUFMO(".BYJNVN WBMVF : ", JOU.NBY);
^
FG@63?7@F3> )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
15
TJ[F@U
7.2
6JP RDGG >JH@ <>MJNN OC@ TJ[F@U OTK@ <N R@GG. TJ[F@U DN IJO < N@K<M<O@ OTK@ =PO <I <GD<N JA <I
@SDNODIB PINDBI@? OTK@. &ON I<H@ >JH@N AMJH "NDU@ OTK@". &O DN OC@ HJNO NPDO<=G@ OTK@ OJ
M@KM@N@IO >JI>@KON GDF@ E;L7 JM 5AG@F. TJ[F@U HPNO =@ G<MB@ @IJPBC OJ M@KM@N@IO OC@ IPH=@M
JA =TO@N JA OC@ H@HJMT OC<O < KMJBM<H ><I KJO@IOD<GGT =@ PNDIB. 0J, DON <>OP<G NDU@ ?@K@I?N JI
OC@ NTNO@H: VJOU JI < 32-=DO NTNO@H, VMPOH JI < 64-=DO NTNO@H, @O>.
6JP ><I PN@ OC@ .TUSJOHPG KMJK@MOT OJ N@@ RC<O TJ[F@U DN <I <GD<N JA JI TJPM NTNO@H:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFMO(TJ[F@U.TUSJOHPG);
^
1C@ JPOKPO JA OC@ KMJBM<H DN OC@ AJGGJRDIB JI HT NTNO@H:
VMPOH
Exercise
7.3
t -MDIO OC@ KMJK@MOD@N JA JOC@M OTK@N.
Note: .AG 53@'F GE7 F:7 D7E7DH76 FKB7E cent 3@6 ucent ;@ 3@K BDA9D3?; 3@6 3E 3@
7J57BF;A@, void 6A7E @AF :3H7 F:7 BDAB7DF;7E .min 3@6 .max.
... F:7 EA>GF;A@
FG@63?7@F3> )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
16
Assignment and Order of Evaluation 8
1C@ ADMNO ORJ ?DAAD>PGOD@N OC<O HJNO NOP?@ION A<>@ RC@I G@<MIDIB OJ KMJBM<H DIQJGQ@ OC@
<NNDBIH@IO JK@M<ODJI <I? OC@ JM?@M JA @Q<GP<ODJI.
The assignment operation
8.1
6JP RDGG N@@ GDI@N NDHDG<M OJ OC@ AJGGJRDIB DI <GHJNO @Q@MT KMJBM<H DI <GHJNO @Q@MT
KMJBM<HHDIB G<IBP<B@:
B = 10;
1C@ H@<IDIB JA OC<O GDI@ DN "H<F@ B'N Q<GP@ =@>JH@ 10". 0DHDG<MGT, OC@ AJGGJRDIB GDI@ H@<IN
"H<F@ C'N Q<GP@ =@>JH@ 20":
C = 20;
B<N@? JI OC<O DIAJMH<ODJI, RC<O ><I =@ N<D? <=JPO OC@ AJGGJRDIB GDI@?
B = C;
2IAJMOPI<O@GT, OC<O GDI@ DN IJO <=JPO OC@ @LP<GDOT >JI>@KO JA H<OC@H<OD>N OC<O & <NNPH@ R@ <GG
FIJR. 1C@ @SKM@NNDJI <=JQ@ does not H@<I "< DN @LP<G OJ ="! 4C@I R@ <KKGT OC@ N<H@ GJBD>
AMJH OC@ @<MGD@M ORJ GDI@N, OC@ @SKM@NNDJI <=JQ@ HPNO H@<I "H<F@ B'N Q<GP@ =@>JH@ C".
*<FDIB B'N Q<GP@ =@>JH@ C APMOC@M H@<IN "H<F@ B'N Q<GP@ =@>JH@ OC@ N<H@ <N C'N Q<GP@".
1C@ R@GG-FIJRI = NTH=JG JA H<OC@H<OD>N C<N < >JHKG@O@GT ?DAA@M@IO H@<IDIB DI
KMJBM<HHDIB: H<F@ OC@ G@AO ND?@'N Q<GP@ OC@ N<H@ <N OC@ MDBCO ND?@'N Q<GP@.
Order of evaluation
8.2
1C@ JK@M<ODJIN JA < KMJBM<H <M@ <KKGD@? NO@K =T NO@K DI < NK@>DAD> JM?@M. 4@ H<T N@@ OC@
KM@QDJPN OCM@@ @SKM@NNDJIN DI < KMJBM<H DI OC<O JM?@M:
B = 10;
C = 20;
B = C;
1C@ H@<IDIB JA OCJN@ OCM@@ GDI@N <GOJB@OC@M DN OCDN: "H<F@ B'N Q<GP@ =@>JH@ 10, F:7@ H<F@
C'N Q<GP@ =@>JH@ 20, F:7@ H<F@ B'N Q<GP@ =@>JH@ OC@ N<H@ <N C'N Q<GP@". A>>JM?DIBGT, <AO@M
OCJN@ OCM@@ JK@M<ODJIN <M@ K@MAJMH@?, =JOC B'N <I? C'N Q<GP@N RJPG? =@ 20.
Exercise
8.3
,=N@MQ@ OC<O OC@ AJGGJRDIB OCM@@ JK@M<ODJIN NR<K OC@ Q<GP@N JA B <I? C. &A <O OC@
=@BDIIDIB OC@DM Q<GP@N <M@ 1 <I? 2 M@NK@>ODQ@GT, <AO@M OC@ JK@M<ODJIN OC@ Q<GP@N =@>JH@ 2
<I? 1:
D = B;
B = C;
C = D;
... F:7 EA>GF;A@
AEE;9@?7@F 3@6 %D67D A8 EH3>G3F;A@
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
17
Variables 9
JI>M@O@ >JI>@KON OC<O <M@ M@KM@N@IO@? DI < KMJBM<H <M@ ><GG@? H3D;34>7E. A Q<GP@ GDF@ 3;D
F7?B7D3FGD7 JM < HJM@ >JHKGD><O@? J=E@>O GDF@ 3 53D 7@9;@7 ><I =@ Q<MD<=G@N JA < KMJBM<H.
"Q@MT Q<MD<=G@ DN JA < >@MO<DI OTK@ <I? C<N < >@MO<DI Q<GP@. *JNO Q<MD<=G@N C<Q@ I<H@N <N
R@GG, =PO NJH@ Q<MD<=G@N <M@ <IJITHJPN.
AN <I @S<HKG@ JA < Q<MD<=G@, R@ ><I OCDIF JA OC@ >JI>@KO JA F:7 @G?47D A8 EFG67@FE <O <
N>CJJG. 0DI>@ OC@ IPH=@M JA NOP?@ION DN < RCJG@ IPH=@M, JOU DN < NPDO<=G@ OTK@, <I?
TUVEFOU$PVOU RJPG? =@ < NPAAD>D@IOGT ?@N>MDKODQ@ I<H@.
A>>JM?DIB OJ !'N NTIO<S MPG@N, < Q<MD<=G@ DN DIOMJ?P>@? =T DON OTK@ AJGGJR@? =T DON I<H@.
&IOMJ?P>DIB < Q<MD<=G@ OJ OC@ KMJBM<H DN ><GG@? DON 678;@;F;A@. ,I>@ < Q<MD<=G@ DN ?@ADI@?, DON
I<H@ NO<MON M@KM@N@IODIB DON Q<GP@.
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
// 5IF EFGJOJUJPO PG UIF WBSJBCMF; UIJT EFGJOJUJPO
// TQFDJGJFT UIBU UIF UZQF PG TUVEFOU$PVOU JT JOU:
JOU TUVEFOU$PVOU;
// 5IF OBNF PG UIF WBSJBCMF CFDPNFT JUT WBMVF:
XSJUFMO("5IFSF BSF ", TUVEFOU$PVOU, " TUVEFOUT.");
^
1C@ JPOKPO JA OCDN KMJBM<H DN OC@ AJGGJRDIB:
5IFSF BSF 0 TUVEFOUT.
AN N@@I AMJH OC<O JPOKPO, OC@ Q<GP@ JA TUVEFOU$PVOU DN 0. 1CDN DN <>>JM?DIB OJ OC@
API?<H@IO<G OTK@N O<=G@ AMJH OC@ KM@QDJPN >C<KO@M: OC@ DIDOD<G Q<GP@ JA JOU DN 0.
+JO@ OC<O TUVEFOU$PVOU ?J@N IJO <KK@<M DI OC@ JPOKPO <N DON I<H@. &I JOC@M RJM?N, OC@
JPOKPO JA OC@ KMJBM<H DN IJO "1C@M@ <M@ NOP?@IOJPIO NOP?@ION".
1C@ Q<GP@N JA Q<MD<=G@N <M@ >C<IB@? =T OC@ = JK@M<OJM. 1C@ = JK@M<OJM <NNDBIN I@R Q<GP@N OJ
Q<MD<=G@N, <I? AJM OC<O M@<NJI DN ><GG@? OC@ 3EE;9@?7@F AB7D3FAD:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU TUVEFOU$PVOU;
XSJUFMO("5IFSF BSF ", TUVEFOU$PVOU, " TUVEFOUT.");
// "TTJHOJOH UIF WBMVF 200 UP UIF TUVEFOU$PVOU WBSJBCMF:
TUVEFOU$PVOU = 200;
XSJUFMO("5IFSF BSF OPX ", TUVEFOU$PVOU, " TUVEFOUT.");
^
5IFSF BSF 0 TUVEFOUT.
5IFSF BSF OPX 200 TUVEFOUT.
4C@I OC@ Q<GP@ JA < Q<MD<=G@ DN FIJRI <O OC@ ODH@ JA OC@ Q<MD<=G@'N ?@ADIDODJI, OC@ Q<MD<=G@ ><I
=@ ?@ADI@? <I? <NNDBI@? <O OC@ N<H@ ODH@. 1CDN DN <I DHKJMO<IO BPD?@GDI@; DO H<F@N DO
DHKJNND=G@ OJ PN@ < Q<MD<=G@ =@AJM@ <NNDBIDIB DON DIO@I?@? Q<GP@:
+3D;34>7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
18
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
// %FGJOJUJPO BOE BTTJHONFOU BU UIF TBNF UJNF:
JOU TUVEFOU$PVOU = 100;
XSJUFMO("5IFSF BSF ", TUVEFOU$PVOU, " TUVEFOUT.");
^
5IFSF BSF 100 TUVEFOUT.
Exercise
9.1
t !@ADI@ ORJ Q<MD<=G@N OJ KMDIO "& C<Q@ @S>C<IB@? 20 "PMJN <O OC@ M<O@ JA 2.11". 6JP ><I PN@
EPVCMF AJM OC@ AGJ<ODIB KJDIO Q<GP@.
... F:7 EA>GF;A@
+3D;34>7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
19
Standard Input and Output Streams 10
0J A<M, OC@ KMDIO@? JPOKPO JA JPM KMJBM<HN C<N =@@I <KK@<MDIB JI OC@ 5A@EA>7 I;@6AI (JM
E5D77@). AGOCJPBC OCDN DN <>OP<GGT OC@ ><N@ RDOC H<IT PN@N JA H<IT KMJBM<HN, DI M@<GDOT OC@
>C<M<>O@MN <M@ KMDIO@? OJ EF3@63D6 AGFBGF EFD73?E JA KMJBM<HN.
1C@ NO<I?<M? JPOKPO DN >C<M<>O@M =<N@?; @Q@MTOCDIB OJ =@ KMDIO@? <M@ ADMNO >JIQ@MO@? OJ OC@DM
>C<M<>O@M M@KM@N@IO<ODJIN <I? OC@I N@IO OJ OC@ JPOKPO JI@ =T JI@ <N >C<M<>O@MN. #JM @S<HKG@,
OC@ DIO@B@M Q<GP@ 100 OC<O R@'Q@ KMDIO@? DI OC@ G<NO >C<KO@M DN IJO N@IO OJ OC@ JPOKPO <N OC@
Q<GP@ 100, =PO <N OC@ OCM@@ >C<M<>O@MN 1, 0, <I? 0.
0DHDG<MGT, RC<O R@ IJMH<GGT K@M>@DQ@ <N OC@ =7K4A3D6 DN <>OP<GGT OC@ EF3@63D6 ;@BGF EFD73? JA
< KMJBM<H <I? DN <GNJ >C<M<>O@M =<N@?. 1C@ DIAJMH<ODJI <GR<TN >JH@N <N >C<M<>O@MN OJ =@
>JIQ@MO@? OJ ?<O<. #JM @S<HKG@ OC@ DIO@B@M Q<GP@ 42 <>OP<GGT >JH@N OCMJPBC OC@ NO<I?<M?
DIKPO <N OC@ >C<M<>O@MN 4 <I? 2.
1C@N@ >JIQ@MNDJIN C<KK@I <POJH<OD><GGT.
1CDN >JI>@KO JA >JIN@>PODQ@ >C<M<>O@MN DN ><GG@? < 5:3D35F7D EFD73?. AN !'N NO<I?<M? DIKPO
<I? NO<I?<M? JPOKPO ADO OCDN ?@N>MDKODJI, OC@T <M@ >C<M<>O@M NOM@<HN.
1C@ I<H@N JA OC@ NO<I?<M? DIKPO <I? JPOKPO NOM@<HN DI ! <M@ TUEJO <I? TUEPVU, M@NK@>ODQ@GT.
,K@M<ODJIN JI OC@N@ NOM@<HN IJMH<GGT M@LPDM@ OC@ I<H@ JA OC@ NOM@<H, < ?JO, <I? OC@
JK@M<ODJI; <N DI TUSFBN.PQFSBUJPO(). B@><PN@ TUEJO <I? TUEPVU <M@ PN@? Q@MT >JHHJIGT,
<N < >JIQ@ID@I>@, NO<I?<M? JK@M<ODJIN JI OC@H ><I =@ ><GG@? RDOCJPO OC@ I@@? JA OC@ I<H@
<I? OC@ ?JO, <N DI PQFSBUJPO().
XSJUFMO OC<O R@'Q@ =@@I PNDIB DI OC@ KM@QDJPN >C<KO@MN DN <>OP<GGT OC@ NCJMO AJM
TUEPVU.XSJUFMO. 0DHDG<MGT, XSJUF DN OC@ NCJMO AJM TUEPVU.XSJUF. A>>JM?DIBGT, OC@ :7>>A IAD>6
KMJBM<H ><I <GNJ =@ RMDOO@I <N OC@ AJGGJRDIB:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
TUEPVU.XSJUFMO(")FMMP XPSME!");
^
Exercise
10.1
t ,=N@MQ@ OC<O TUEPVU.XSJUF RJMFN OC@ N<H@ <N XSJUF.
... F:7 EA>GF;A@
(F3@63D6 !@BGF 3@6 %GFBGF (FD73?E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
20
Reading from the Standard Input 11
AIT ?<O< OC<O DN M@<? =T OC@ KMJBM<H HPNO ADMNO =@ NOJM@? DI < Q<MD<=G@. #JM @S<HKG@, <
KMJBM<H OC<O M@<?N OC@ IPH=@M JA NOP?@ION AMJH OC@ DIKPO HPNO NOJM@ OCDN DIAJMH<ODJI DI <
Q<MD<=G@. 1C@ OTK@ JA OCDN NK@>DAD> Q<MD<=G@ ><I =@ JOU.
AN R@'Q@ N@@I DI OC@ KM@QDJPN >C<KO@M, R@ ?JI'O I@@? OJ OTK@ TUEPVU RC@I KMDIODIB OJ OC@
JPOKPO, =@><PN@ DO DN DHKGD@?. #PMOC@M, RC<O DN OJ =@ KMDIO@? DN NK@>DAD@? <N OC@ <MBPH@IO. 0J,
XSJUF(TUVEFOU$PVOU) DN NPAAD>D@IO OJ KMDIO OC@ Q<GP@ JA TUVEFOU$PVOU. 1J NPHH<MDU@:
TUSFBN: TUEPVU
PQFSBUJPO: XSJUF
EBUB: UIF WBMVF PG UIF TUVEFOU$PVOU WBSJBCMF
UBSHFU: DPNNPOMZ UIF DPOTPMF XJOEPX
1C@ M@Q@MN@ JA XSJUF DN SFBEG; DO M@<?N AMJH OC@ NO<I?<M? DIKPO. 1C@ "A" DI DON I<H@ >JH@N
AMJH "AJMH<OO@?" <N RC<O DO M@<?N HPNO <GR<TN =@ KM@N@IO@? DI < >@MO<DI AJMH<O.
4@'Q@ <GNJ N@@I DI OC@ KM@QDJPN >C<KO@M OC<O OC@ NO<I?<M? DIKPO NOM@<H DN TUEJO.
&I OC@ ><N@ JA M@<?DIB, JI@ KD@>@ JA OC@ KPUUG@ DN NODGG HDNNDIB: RC@M@ OJ NOJM@ OC@ ?<O<. 1J
NPHH<MDU@:
TUSFBN: TUEJO
PQFSBUJPO: SFBEG
EBUB: TPNF JOGPSNBUJPO
UBSHFU:
1C@ GJ><ODJI JA RC@M@ OJ NOJM@ OC@ ?<O< DN NK@>DAD@? =T OC@ <??M@NN JA < Q<MD<=G@. 1C@ <??M@NN
JA < Q<MD<=G@ DN OC@ @S<>O GJ><ODJI DI OC@ >JHKPO@M'N H@HJMT RC@M@ DON Q<GP@ DN NOJM@?.
&I !, OC@ & >C<M<>O@M OC<O DN OTK@? =@AJM@ < I<H@ DN OC@ <??M@NN JA RC<O OC<O I<H@
M@KM@N@ION. #JM @S<HKG@, OC@ <??M@NN JA TUVEFOU$PVOU DN &TUVEFOU$PVOU. %@M@,
&TUVEFOU$PVOU ><I =@ M@<? <N "OC@ <??M@NN JA TUVEFOU$PVOU" <I? DN OC@ HDNNDIB KD@>@ OJ
M@KG<>@ OC@ LP@NODJI H<MF <=JQ@:
TUSFBN: TUEJO
PQFSBUJPO: SFBEG
EBUB: TPNF JOGPSNBUJPO
UBSHFU: UIF MPDBUJPO PG UIF TUVEFOU$PVOU WBSJBCMF
1TKDIB < & DI AMJIO JA < I<H@ H@<IN BA;@F;@9 <O RC<O OC<O I<H@ M@KM@N@ION. 1CDN >JI>@KO DN
OC@ AJPI?<ODJI JA M@A@M@I>@N <I? KJDIO@MN OC<O R@ RDGG N@@ DI G<O@M >C<KO@MN.
& RDGG G@<Q@ JI@ K@>PGD<MDOT <=JPO OC@ PN@ JA SFBEG AJM G<O@M; AJM IJR, G@O'N <>>@KO <N < MPG@ OC<O
OC@ ADMNO <MBPH@IO OJ SFBEG HPNO =@ "%T":
SFBEG("%T", &TUVEFOU$PVOU);
Note: AE ! 7JB>3;@ 47>AI, ;@ ?AEF 53E7E F:7D7 ?GEF 3>EA 47 3 EB357: " %s".
"%T" DI?D><O@N OC<O OC@ ?<O< NCJPG? <POJH<OD><GGT =@ >JIQ@MO@? DI < R<T OC<O DN NPDO<=G@ OJ OC@
OTK@ JA OC@ Q<MD<=G@. #JM @S<HKG@, RC@I OC@ '4' <I? '2' >C<M<>O@MN <M@ M@<? OJ < Q<MD<=G@ JA
OTK@ JOU, OC@T RJPG? =@ >JIQ@MO@? <N OC@ DIO@B@M Q<GP@ 42.
1C@ KMJBM<H =@GJR <NFN OC@ PN@M OJ @IO@M OC@ IPH=@M JA NOP?@ION. 6JP HPNO KM@NN OC@ "IO@M
F@T <AO@M OTKDIB OC@ DIKPO:
'736;@9 8DA? F:7 (F3@63D6 !@BGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
21
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUF(")PX NBOZ TUVEFOUT BSF UIFSF ");
/*
* 5IF EFGJOJUJPO PG UIF WBSJBCMF UIBU XJMM CF VTFE UP
* TUPSF UIF JOGPSNBUJPO UIBU JT SFBE GSPN UIF JOQVU.
*/
JOU TUVEFOU$PVOU;
// 4UPSJOH UIF JOQVU EBUB UP UIBU WBSJBCMF
SFBEG("%T", &TUVEFOU$PVOU);
XSJUFMO("(PU JU: 5IFSF BSF ", TUVEFOU$PVOU, " TUVEFOUT.");
^
Skipping the whitespace characters
11.1
"Q@I OC@ "IO@M F@T OC<O R@ KM@NN <AO@M OTKDIB OC@ ?<O< DN NOJM@? <N < NK@>D<G >J?@ <I? DN
KG<>@? DIOJ OC@ TUEJO NOM@<H. 1CDN DN PN@APG OJ OC@ KMJBM<HN OJ ?@O@>O RC@OC@M OC@
DIAJMH<ODJI C<N =@@I DIKPO JI < NDIBG@ GDI@ JM HPGODKG@ GDI@N.
AGOCJPBC NJH@ODH@N PN@APG, NP>C NK@>D<G >J?@N <M@ HJNOGT IJO DHKJMO<IO AJM OC@ KMJBM<H <I?
HPNO =@ ADGO@M@? JPO AMJH OC@ DIKPO. ,OC@MRDN@ OC@T 4>A5= OC@ DIKPO <I? KM@Q@IO M@<?DIB JOC@M
?<O<.
1J N@@ OCDN BDA4>7? DI < KMJBM<H, G@O'N M@<? <GNJ OC@ IPH=@M JA O@<>C@MN AMJH OC@ DIKPO:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUF(")PX NBOZ TUVEFOUT BSF UIFSF ");
JOU TUVEFOU$PVOU;
SFBEG("%T", &TUVEFOU$PVOU);
XSJUF(")PX NBOZ UFBDIFST BSF UIFSF ");
JOU UFBDIFS$PVOU;
SFBEG("%T", &UFBDIFS$PVOU);
XSJUFMO("(PU JU: 5IFSF BSF ", TUVEFOU$PVOU, " TUVEFOUT",
" BOE ", UFBDIFS$PVOU, " UFBDIFST.");
^
2IAJMOPI<O@GT, IJR OC@ KMJBM<H B@ON EFG5= RC@I M@<?DIB OC@ N@>JI? JOU:
)PX NBOZ TUVEFOUT BSF UIFSF 100
)PX NBOZ UFBDIFST BSF UIFSF 20
l 5IF QSPHSBN HFUT TUVDL IFSF
AGOCJPBC OC@ PN@M @IO@MN OC@ IPH=@M JA O@<>C@MN <N 20, OC@ NK@>D<G >J?@(N) OC<O M@KM@N@ION
OC@ "IO@M F@T OC<O C<N =@@I KM@NN@? RC@I @IO@MDIB OC@ KM@QDJPN 100 DN NODGG DI OC@ DIKPO
NOM@<H <I? DN =GJ>FDIB DO. 1C@ >C<M<>O@MN OC<O <KK@<M@? DI OC@ DIKPO NOM@<H <M@ NDHDG<M OJ OC@
AJGGJRDIB:
100<&OUFS$PEF>20<&OUFS$PEF>
& C<Q@ NCJRI OC@ "IO@M >J?@ OC<O DN =GJ>FDIB OC@ DIKPO DI M@?.
'736;@9 8DA? F:7 (F3@63D6 !@BGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
22
1C@ NJGPODJI DN OJ PN@ < NK<>@ >C<M<>O@M =@AJM@ %T OJ DI?D><O@ OC<O OC@ "IO@M >J?@ OC<O
<KK@<MN =@AJM@ M@<?DIB OC@ IPH=@M JA O@<>C@MN DN IJO DHKJMO<IO: " %T". 0K<>@N OC<O <M@ DI
OC@ AJMH<O NOMDIBN <M@ PN@? OJ M@<? <I? DBIJM@ U@MJ JM HJM@ DIQDND=G@ >C<M<>O@MN OC<O RJPG?
JOC@MRDN@ <KK@<M DI OC@ DIKPO. 0P>C >C<M<>O@MN DI>GP?@ OC@ <>OP<G NK<>@ >C<M<>O@M, OC@
>J?@(N) OC<O M@KM@N@IO OC@ "IO@M F@T, OC@ 1<= >C<M<>O@M, @O>. <I? <M@ ><GG@? OC@ I:;F7EB357
5:3D35F7DE.
AN < B@I@M<G MPG@, TJP ><I PN@ " %T" AJM @Q@MT ?<O< OC<O DN M@<? AMJH OC@ DIKPO. 1C@ KMJBM<H
<=JQ@ RJMFN <N @SK@>O@? RDOC OC@ AJGGJRDIB >C<IB@N:
// ...
SFBEG(" %T", &TUVEFOU$PVOU);
// ...
SFBEG(" %T", &UFBDIFS$PVOU);
// ...
1C@ JPOKPO:
)PX NBOZ TUVEFOUT BSF UIFSF 100
)PX NBOZ UFBDIFST BSF UIFSF 20
(PU JU: 5IFSF BSF 100 TUVEFOUT BOE 20 UFBDIFST.
Additional information
11.2
t )DI@N OC<O NO<MO RDOC // <M@ PN@APG AJM NDIBG@ GDI@N JA >JHH@ION. 1J RMDO@ HPGODKG@ GDI@N <N
< NDIBG@ >JHH@IO, @I>GJN@ OC@ GDI@N RDOCDI /* <I? */ H<MF@MN.
&I JM?@M OJ =@ <=G@ OJ >JHH@IO @Q@I JOC@M >JHH@ION, PN@ /+ <I? +/:
/+
// " TJOHMF MJOF PG DPNNFOU
/*
" DPNNFOU UIBU TQBOT
NVMUJQMF MJOFT
*/
" DPNNFOU CMPDL UIBU JODMVEFT PUIFS DPNNFOUT
+/
t *JNO JA OC@ RCDO@NK<>@ DI OC@ NJPM>@ >J?@ <M@ DINDBIDAD><IO. &O DN BJJ? KM<>OD>@ OJ RMDO@
GJIB@M @SKM@NNDJIN <N HPGODKG@ GDI@N JM <?? @SOM< RCDO@NK<>@ OJ H<F@ OC@ >J?@ HJM@
M@<?<=G@. 0ODGG, <N GJIB <N OC@ NTIO<S MPG@N JA OC@ G<IBP<B@ <M@ J=N@MQ@?, OC@ KMJBM<HN ><I
=@ RMDOO@I RDOCJPO <IT @SOM< RCDO@NK<>@:
JNQPSU TUE.TUEJP;WPJE NBJO()\XSJUFMO(")BSE UP SFBE!");^
&O DN C<M? OJ M@<? NJPM>@ >J?@ OC<O C<N NJ A@R RCDO@NK<>@.
Exercise
11.3
t "IO@M IJI-IPH@M<G >C<M<>O@MN RC@I OC@ KMJBM<H DN @SK@>ODIB DIO@B@M Q<GP@N <I? J=N@MQ@
OC<O OC@ KMJBM<H ?J@N IJO RJMF >JMM@>OGT.
... F:7 EA>GF;A@
'736;@9 8DA? F:7 (F3@63D6 !@BGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
23
Logical Expressions 12
A>OP<G RJMF OC<O < KMJBM<H K@MAJMHN <M@ <>>JHKGDNC@? =T 7JBD7EE;A@E. AIT K<MO JA <
KMJBM<H OC<O KMJ?P>@N < Q<GP@ JM < ND?@ @AA@>O DN ><GG@? <I @SKM@NNDJI. &O C<N < Q@MT RD?@
?@ADIDODJI, =@><PN@ @Q@I < >JINO<IO Q<GP@ GDF@ 42 <I? < NOMDIB GDF@ "IFMMP" <M@ @SKM@NNDJIN
=@><PN@ OC@T KMJ?P>@ OC@ M@NK@>ODQ@ >JINO<IO Q<GP@N 42 <I? "C@GGJ".
Note: DA@'F 5A@8GE7 BDA6G5;@9 3 H3>G7 I;F: 678;@;@9 3 H3D;34>7. +3>G7E @776 @AF 47
3EEA5;3F76 I;F: H3D;34>7E.
#PI>ODJI ><GGN GDF@ XSJUFMO <M@ @SKM@NNDJIN <N R@GG, =@><PN@ OC@T C<Q@ ND?@ @AA@>ON. &I OC@
><N@ JA XSJUFMO, OC@ @AA@>O DN JI OC@ JPOKPO NOM@<H =T OC@ KG<>@H@IO JA >C<M<>O@MN JI DO.
AIJOC@M @S<HKG@ AMJH OC@ KMJBM<HN OC<O R@ C<Q@ RMDOO@I NJ A<M RJPG? =@ OC@ <NNDBIH@IO
JK@M<ODJI, RCD>C <AA@>ON OC@ Q<MD<=G@ JI DON G@AO-C<I? ND?@.
B@><PN@ JA KMJ?P>DIB Q<GP@N, @SKM@NNDJIN ><I O<F@ K<MO DI JOC@M @SKM@NNDJIN. 1CDN <GGJRN PN
OJ AJMH HJM@ >JHKG@S @SKM@NNDJIN AMJH NDHKG@M JI@N. #JM @S<HKG@, <NNPHDIB OC<O OC@M@ DN <
API>ODJI I<H@? DVSSFOU5FNQFSBUVSF() OC<O KMJ?P>@N OC@ Q<GP@ JA OC@ >PMM@IO <DM
O@HK@M<OPM@, OC@ Q<GP@ OC<O DO KMJ?P>@N H<T ?DM@>OGT =@ PN@? DI < XSJUFMO @SKM@NNDJI:
XSJUFMO("*U'T ", DVSSFOU5FNQFSBUVSF(),
" EFHSFFT BU UIF NPNFOU.");
1C<O GDI@ >JINDNON JA AJPM @SKM@NNDJIN:
1. "*U'T "
2. DVSSFOU5FNQFSBUVSF()
3. " EFHSFFT BU UIF NPNFOU."
4. OC@ XSJUFMO @SKM@NNDJI OC<O H<F@N PN@ JA OC@ JOC@M OCM@@
&I OCDN >C<KO@M R@ RDGG >JQ@M OC@ K<MOD>PG<M OTK@ JA @SKM@NNDJI OC<O DN PN@? DI >JI?DODJI<G
NO<O@H@ION.
B@AJM@ BJDIB APMOC@M OCJPBC, & RJPG? GDF@ OJ M@K@<O OC@ <NNDBIH@IO JK@M<OJM JI>@ HJM@, OCDN
ODH@ @HKC<NDUDIB OC@ ORJ @SKM@NNDJIN OC<O <KK@<M JI DON G@AO <I? MDBCO ND?@N: OC@ <NNDBIH@IO
JK@M<OJM (=) <NNDBIN OC@ Q<GP@ JA OC@ @SKM@NNDJI JI DON MDBCO-C<I? ND?@ OJ OC@ @SKM@NNDJI JI DON
G@AO-C<I? ND?@ (@.B. OJ < Q<MD<=G@).
UFNQFSBUVSF = 23 // UFNQFSBUVSF'T WBMVF CFDPNFT 23
Logical Expressions
12.1
)JBD><G @SKM@NNDJIN <M@ OC@ @SKM@NNDJIN OC<O <M@ PN@? DI BJJG@<I <MDOCH@OD>. )JBD><G
@SKM@NNDJIN <M@ RC<O H<F@N >JHKPO@M KMJBM<HN H<F@ ?@>DNDJIN GDF@ "DA OC@ <INR@M DN T@N, &
RDGG N<Q@ OC@ ADG@".
)JBD><G @SKM@NNDJIN ><I C<Q@ JI@ JA JIGT ORJ Q<GP@N: GBMTF OC<O DI?D><O@N A<GNDOT, <I? USVF
OC<O DI?D><O@N OMPOC.
& RDGG PN@ XSJUFMO @SKM@NNDJIN DI OC@ AJGGJRDIB @S<HKG@N. &A < GDI@ C<N USVF KMDIO@? <O OC@
@I?, DO RDGG H@<I OC<O RC<O DN KMDIO@? JI OC@ GDI@ DN OMP@. 0DHDG<MGT, GBMTF RDGG H@<I OC<O RC<O
DN JI OC@ GDI@ DN A<GN@. #JM @S<HKG@, DA OC@ JPOKPO JA < KMJBM<H DN OC@ AJGGJRDIB,
5IFSF JT DPGGFF: USVF
OC@I DO RDGG H@<I OC<O "OC@M@ DN >JAA@@". 0DHDG<MGT,
"A9;53> EJBD7EE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
24
5IFSF JT DPGGFF: GBMTF
RDGG H@<I OC<O "OC@M@ DNI'O >JAA@@". +JO@ OC<O OC@ A<>O OC<O "DN" <KK@<MN JI OC@ G@AO-C<I? ND?@
?J@N IJO H@<I OC<O >JAA@@ @SDNON. & PN@ OC@ "... DN ...: A<GN@" >JINOMP>O OJ H@<I "DN IJO" JM "DN
A<GN@".
)JBD><G @SKM@NNDJIN <M@ PN@? @SO@INDQ@GT DI 5A@6;F;A@3> EF3F7?7@FE, >AABE, 8G@5F;A@
B3D3?7F7DE, @O>. &O DN @NN@IOD<G OJ PI?@MNO<I? CJR OC@T RJMF. )P>FDGT, GJBD><G @SKM@NNDJIN <M@
Q@MT @<NT OJ @SKG<DI <I? PN@.
1C@ GJBD><G JK@M<OJMN OC<O <M@ PN@? DI GJBD><G @SKM@NNDJIN <M@ OC@ AJGGJRDIB:
t 1C@ == JK@M<OJM <INR@MN OC@ LP@NODJI "DN @LP<G OJ?". JHK<M@N OC@ ORJ @SKM@NNDJIN JI DON
G@AO <I? MDBCO ND?@N <I? KMJ?P>@N USVF DA OC@T <M@ @LP<G <I? GBMTF DA OC@T <M@ IJO. BT
?@ADIDODJI, OC@ Q<GP@ OC<O == KMJ?P>@N DN < GJBD><G @SKM@NNDJI.
AN <I @S<HKG@, G@O'N <NNPH@ OC<O R@ C<Q@ OC@ AJGGJRDIB ORJ Q<MD<=G@N:
JOU EBZT*O8FFL = 7;
JOU NPOUIT*O:FBS = 12;
1C@ AJGGJRDIB <M@ ORJ GJBD><G @SKM@NNDJIN OC<O PN@ OCJN@ Q<GP@N:
EBZT*O8FFL == 7 // USVF
NPOUIT*O:FBS == 11 // GBMTF
t 1C@ != JK@M<OJM <INR@MN OC@ LP@NODJI "DN IJO @LP<G OJ?" JHK<M@N OC@ ORJ @SKM@NNDJIN JI
DON ND?@N <I? KMJ?P>@N OC@ JKKJNDO@ JA ==.
EBZT*O8FFL != 7 // GBMTF
NPOUIT*O:FBS != 11 // USVF
t 1C@ ]] JK@M<OJM H@<IN "JM", <I? KMJ?P>@N USVF DA <IT JI@ JA OC@ GJBD><G @SKM@NNDJIN DN
OMP@.
&A OC@ Q<GP@ JA OC@ G@AO-C<I? @SKM@NNDJI DN USVF, DO KMJ?P>@N USVF RDOCJPO @Q@I GJJFDIB
<O OC@ @SKM@NNDJI OC<O DN JI OC@ MDBCO-C<I? ND?@. &A OC@ G@AO-C<I? ND?@ DN GBMTF, OC@I DO
KMJ?P>@N OC@ Q<GP@ JA OC@ MDBCO-C<I? ND?@. 1CDN JK@M<OJM DN NDHDG<M OJ OC@ "JM" DI
"IBGDNC: DA OC@ G@AO JI@, OC@ MDBCO JI@, JM =JOC <M@ USVF, OC@I DO KMJ?P>@N USVF.
AGG JA OC@ KJNND=G@ Q<GP@N AJM =JOC ND?@N JA OCDN JK@M<OJM <I? DON M@NPGO <M@ <N DI OC@
AJGGJRDIB O<=G@:
-FGU FYQSFTTJPO 3JHIU FYQSFTTJPO 3FTVMU
GBMTF GBMTF GBMTF
GBMTF USVF USVF
USVF GBMTF (OPU FWBMVBUFE) USVF
USVF USVF (OPU FWBMVBUFE) USVF
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
// GBMTF NFBOT "OP", USVF NFBOT "ZFT"
CPPM FYJTUT$PGGFF = GBMTF;
CPPM FYJTUT5FB = USVF;
XSJUFMO("5IFSF JT XBSN ESJOL: ",
"A9;53> EJBD7EE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
25
FYJTUT$PGGFF ]] FYJTUT5FB);
^
B@><PN@ <O G@<NO JI@ JA OC@ ORJ @SKM@NNDJIN DN USVF, OC@ GJBD><G @SKM@NNDJI <=JQ@
KMJ?P>@N USVF.
t 1C@ && JK@M<OJM H@<IN "<I?", <I? KMJ?P>@N USVF DA =JOC JA OC@ @SKM@NNDJIN <M@ OMP@.
&A OC@ Q<GP@ JA OC@ G@AO-C<I? @SKM@NNDJI DN GBMTF, DO KMJ?P>@N GBMTF RDOCJPO @Q@I
GJJFDIB <O OC@ @SKM@NNDJI OC<O DN JI OC@ MDBCO-C<I? ND?@. &A OC@ G@AO-C<I? ND?@ DN USVF,
OC@I DO KMJ?P>@N OC@ Q<GP@ JA OC@ MDBCO-C<I? ND?@. 1CDN JK@M<OJM DN NDHDG<M OJ OC@ "<I?"
DI "IBGDNC: DA OC@ G@AO Q<GP@ <I? OC@ MDBCO Q<GP@ <M@ USVF, OC@I DO KMJ?P>@N USVF.
-FGU FYQSFTTJPO 3JHIU FYQSFTTJPO 3FTVMU
GBMTF GBMTF (OPU FWBMVBUFE) GBMTF
GBMTF USVF (OPU FWBMVBUFE) GBMTF
USVF GBMTF GBMTF
USVF USVF USVF
XSJUFMO("* XJMM ESJOL DPGGFF: ",
XBOU5P%SJOL$PGGFF && FYJTUT$PGGFF);
Note: ):7 835F F:3F F:7 || 3@6 && AB7D3FADE ?3K @AF 7H3>G3F7 F:7 D;9:F-:3@6
7JBD7EE;A@ ;E 53>>76 F:7;D NCJMO>PO =@C<QDJM . ):7 A@>K AF:7D AB7D3FAD F:3F :3E F:;E
47:3H;AD ;E F:7 F7D@3DK AB7D3FAD ?:, I:;5: I7 I;>> E77 ;@ 3 >3F7D 5:3BF7D. A>> A8 F:7
AF:7D AB7D3FADE 3>I3KE 7H3>G3F7 3@6 GE7 3>> A8 F:7;D 7JBD7EE;A@E.
t 1C@ ? JK@M<OJM <INR@MN OC@ LP@NODJI "DN JI@ JM OC@ JOC@M?" 1CDN JK@M<OJM KMJ?P>@N USVF DA
JIGT JI@ @SKM@NNDJI DN USVF, =PO IJO =JOC.
-FGU FYQSFTTJPO 3JHIU FYQSFTTJPO 3FTVMU
GBMTF GBMTF GBMTF
GBMTF USVF USVF
USVF GBMTF USVF
USVF USVF GBMTF
#JM @S<HKG@, OC@ GJBD> OC<O M@KM@N@ION HT KG<TDIB >C@NN DA A@>K A@7 JA HT ORJ AMD@I?N
NCJRN PK ><I =@ >J?@? GDF@ OCDN:
XSJUFMO("* XJMM QMBZ DIFTT: ", KJN4IPXFE6Q ? CPC4IPXFE6Q);
t 1C@ < JK@M<OJM <INR@MN OC@ LP@NODJI "DN G@NN OC<I?" (JM "?J@N >JH@ =@AJM@ DI NJMO
JM?@M?").
XSJUFMO("8F CFBU: ", UIFJS4DPSF < PVS4DPSF);
t 1C@ > JK@M<OJM <INR@MN OC@ LP@NODJI "DN BM@<O@M OC<I?" (JM "?J@N >JH@ <AO@M DI NJMO
JM?@M?").
XSJUFMO("5IFZ CFBU: ", UIFJS4DPSF > PVS4DPSF);
t 1C@ <= JK@M<OJM <INR@MN OC@ LP@NODJI "DN G@NN OC<I JM @LP<G OJ?" (JM "?J@N >JH@ =@AJM@ JM
OC@ N<H@ DI NJMO JM?@M?"). 1CDN JK@M<OJM DN OC@ JKKJNDO@ JA OC@ > JK@M<OJM.
XSJUFMO("8F XFSF OPU CFBUFO: ", UIFJS4DPSF <= PVS4DPSF);
"A9;53> EJBD7EE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
26
t 1C@ >= JK@M<OJM <INR@MN OC@ LP@NODJI "DN BM@<O@M OC<I JM @LP<G OJ?" (JM "?J@N >JH@ <AO@M
JM OC@ N<H@ DI NJMO JM?@M?"). 1CDN JK@M<OJM DN OC@ JKKJNDO@ JA OC@ < JK@M<OJM.
XSJUFMO("8F EJE OPU CFBU: ", UIFJS4DPSF >= PVS4DPSF);
t 1C@ ! JK@M<OJM H@<IN "OC@ JKKJNDO@ JA". !DAA@M@IO AMJH OC@ JOC@M GJBD><G JK@M<OJMN, DO
O<F@N EPNO JI@ @SKM@NNDJI <I? KMJ?P>@N USVF DA OC<O @SKM@NNDJI DN GBMTF, <I? GBMTF DA OC<O
@SKM@NNDJI DN USVF.
XSJUFMO("* XJMM XBML: ", !FYJTUT#JDZDMF);
Grouping expressions
12.2
1C@ JM?@M DI RCD>C OC@ @SKM@NNDJIN <M@ @Q<GP<O@? ><I =@ NK@>DAD@? =T PNDIB K<M@IOC@N@N OJ
BMJPK OC@H. 4C@I K<M@IOC@NDU@? @SKM@NNDJIN <KK@<M DI HJM@ >JHKG@S @SKM@NNDJIN, OC@
K<M@IOC@NDU@? @SKM@NNDJIN <M@ @Q<GP<O@? =@AJM@ OC@T ><I =@ PN@? DI OC@ @SKM@NNDJIN OC<O OC@T
<KK@<M DI. #JM @S<HKG@, OC@ @SKM@NNDJI "DA OC@M@ DN >JAA@@ JM O@<, <I? <GNJ >JJFD@ JM N>JI@;
OC@I & <H C<KKT" ><I =@ >J?@? GDF@ OC@ AJGGJRDIB:
XSJUFMO("* BN IBQQZ: ",
(FYJTUT$PGGFF ]] FYJTUT5FB) && (FYJTUT$PPLJF ]] FYJTUT4DPOF));
&A OC@ NP= @SKM@NNDJIN R@M@ IJO K<M@IOC@NDU@?, OC@ @SKM@NNDJIN RJPG? =@ @Q<GP<O@? <>>JM?DIB
OJ AB7D3FAD BD75767@57 MPG@N JA ! (RCD>C C<Q@ =@@I DIC@MDO@? AMJH OC@ G<IBP<B@). 0DI>@ DI
OC@N@ MPG@N && C<N < CDBC@M KM@>@?@I>@ OC<I ]], RMDODIB OC@ @SKM@NNDJI RDOCJPO K<M@IOC@N@N
RJPG? IJO =@ @Q<GP<O@? <N DIO@I?@?:
XSJUFMO("* BN IBQQZ: ",
FYJTUT$PGGFF ]] FYJTUT5FB && FYJTUT$PPLJF ]] FYJTUT4DPOF);
1C@ && JK@M<OJM RJPG? =@ @Q<GP<O@? ADMNO <I? OC@ RCJG@ @SKM@NNDJI RJPG? =@ OC@ N@H<IOD>
@LPDQ<G@IO JA OC@ AJGGJRDIB @SKM@NNDJI:
XSJUFMO("* BN IBQQZ: ",
FYJTUT$PGGFF ]] (FYJTUT5FB && FYJTUT$PPLJF) ]] FYJTUT4DPOF);
1C<O C<N < OJO<GGT ?DAA@M@IO H@<IDIB: "DA OC@M@ DN >JAA@@, JM O@< <I? >JJFD@, JM N>JI@; OC@I & <H
C<KKT".
Reading CPPM input
12.3
AGG JA OC@ CPPM Q<GP@N <=JQ@ <M@ <POJH<OD><GGT KMDIO@? <N "A<GN@" JM "OMP@". &O DN IJO OC@ ><N@ DI
OC@ JKKJNDO@ ?DM@>ODJI: OC@ NOMDIBN "A<GN@" <I? "OMP@" <M@ IJO <POJH<OD><GGT M@<? <N OC@ Q<GP@N
GBMTF <I? USVF. #JM OC<O M@<NJI, OC@ DIKPO HPNO =@ ADMNO M@<? <N < NOMDIB <I? OC@I =@
>JIQ@MO@? OJ < CPPM Q<GP@.
0DI>@ JI@ JA OC@ @S@M>DN@N =@GJR M@LPDM@N TJP OJ @IO@M "A<GN@" <I? "OMP@" AMJH OC@ NO<I?<M?
DIKPO, & C<Q@ =@@I AJM>@? OJ PN@ ! A@<OPM@N OC<O & C<Q@I'O @SKG<DI@? OJ TJP T@O. & RDGG C<Q@ OJ
?@ADI@ < API>ODJI =@GJR OC<O RDGG >JIQ@MO OC@ DIKPO NOMDIB OJ < CPPM Q<GP@. 1CDN API>ODJI RDGG
<>CD@Q@ DON O<NF =T ><GGDIB UP, RCD>C DN ?@ADI@? DI OC@ HJ?PG@ TUE.DPOW. (6JP H<T N@@
$POW&YDFQUJPO @MMJMN DA TJP @IO@M <ITOCDIB JOC@M OC<I "A<GN@" JM "OMP@".)
& <H CJKDIB OC<O <GG JA OC@ >J?@ OC<O <M@ DI OC@ NBJO() API>ODJIN JA OC@ AJGGJRDIB KMJBM<HN
<M@ >G@<M <O OCDN KJDIO. SFBE@CPPM() DN OC@ API>ODJI OC<O >JIO<DIN I@R A@<OPM@N. AGOCJPBC &
"A9;53> EJBD7EE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
27
C<Q@ DIN@MO@? >JHH@ION OJ @SKG<DI RC<O DO ?J@N, TJP ><I DBIJM@ OC<O API>ODJI AJM IJR. 0ODGG, DO
HPNO =@ < K<MO JA OC@ NJPM>@ >J?@ AJM OC@ KMJBM<H OJ >JHKDG@ <I? RJMF >JMM@>OGT.
Exercises
12.4
1. 4@'Q@ N@@I <=JQ@ OC<O OC@ < <I? OC@ > JK@M<OJMN <M@ PN@? OJ ?@O@MHDI@ RC@OC@M < Q<GP@ DN
G@NN OC<I JM BM@<O@M OC<I <IJOC@M Q<GP@; =PO OC@M@ DN IJ JK@M<OJM OC<O <INR@MN OC@
LP@NODJI "DN =@OR@@I?" OJ ?@O@MHDI@ RC@OC@M < Q<GP@ DN =@OR@@I ORJ JOC@M Q<GP@N.
)@O'N <NNPH@ OC<O < KMJBM<HH@M C<N RMDOO@I OC@ AJGGJRDIB >J?@ OJ ?@O@MHDI@ RC@OC@M
WBMVF DN =@OR@@I 10 <I? 20. ,=N@MQ@ OC<O OC@ KMJBM<H ><IIJO =@ >JHKDG@? <N
RMDOO@I:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU WBMVF = 15;
XSJUFMO("*T CFUXFFO: ",
10 < WBMVF < 20); // j DPNQJMBUJPO &3303
^
1MT PNDIB K<M@IOC@N@N <MJPI? OC@ RCJG@ @SKM@NNDJI:
XSJUFMO("*T CFUXFFO: ",
(10 < WBMVF < 20)); // j DPNQJMBUJPO &3303
,=N@MQ@ OC<O DO NODGG ><IIJO =@ >JHKDG@?.
2. 4CDG@ N@<M>CDIB AJM < NJGPODJI OJ OCDN KMJ=G@H, OC@ N<H@ KMJBM<HH@M ?DN>JQ@MN OC<O OC@
AJGGJRDIB PN@ JA K<M@IOC@N@N IJR @I<=G@N OC@ >J?@ OJ =@ >JHKDG@?:
XSJUFMO("*T CFUXFFO: ",
(10 < WBMVF) < 20); // j DPNQJMFT CVU 830/(
,=N@MQ@ OC<O OC@ KMJBM<H IJR RJMFN <N @SK@>O@? <I? KMDION "OMP@". 2IAJMOPI<O@GT
OC<O JPOKPO DN HDNG@<?DIB =@><PN@ OC@ KMJBM<H C<N < =PB. 1J N@@ OC@ @AA@>O JA OC<O
=PB, M@KG<>@ 15 RDOC < Q<GP@ BM@<O@M OC<I 20:
JOU WBMVF = 21;
,=N@MQ@ OC<O OC@ KMJBM<H NODGG KMDION "OMP@" @Q@I OCJPBC 21 DN IJO G@NN OC<I 20.
Hint: /@H@H=@M OC<O OC@ OTK@ JA < GJBD><G @SKM@NNDJI DN CPPM. &O NCJPG?I'O H<F@
N@IN@ RC@OC@M < CPPM Q<GP@ DN G@NN OC<I 20.
3. 1C@ GJBD><G @SKM@NNDJI OC<O <INR@MN OC@ LP@NODJI "DN =@OR@@I?" HPNO DINO@<? =@ >J?@? GDF@
OCDN: "DN BM@<O@M OC<I OC@ GJR@M Q<GP@ <I? G@NN OC<I OC@ PKK@M Q<GP@?"
C<IB@ OC@ @SKM@NNDJI DI OC@ KMJBM<H <>>JM?DIB OJ OC<O GJBD> <I? J=N@MQ@ OC<O DO IJR
KMDION "OMP@" <N @SK@>O@?. A??DODJI<GGT, O@NO OC<O OC@ GJBD><G @SKM@NNDJI RJMFN
>JMM@>OGT AJM JOC@M Q<GP@N <N R@GG: AJM @S<HKG@, RC@I WBMVF DN 50 JM 1, OC@ KMJBM<H
NCJPG? KMDIO "A<GN@"; <I? RC@I DO'N 12, OC@ KMJBM<H NCJPG? KMDIO "OMP@".
4. )@O'N <NNPH@ OC<O R@ ><I BJ OJ OC@ =@<>C RC@I JI@ JA OC@ AJGGJRDIB >JI?DODJIN DN OMP@:
w &A OC@ ?DNO<I>@ OJ OC@ =@<>C DN G@NN OC<I 10 <I? OC@M@ DN < =D>T>G@ AJM @Q@MTJI@
w &A OC@M@ DN G@NN OC<I 6 JA PN, <I? R@ C<Q@ < ><M, <I? JI@ JA PN C<N < ?MDQ@M GD>@IN@
"A9;53> EJBD7EE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
28
AN RMDOO@I, OC@ AJGGJRDIB KMJBM<H <GR<TN KMDION "OMP@". JINOMP>O < GJBD><G @SKM@NNDJI
OC<O RDGG KMDIO "OMP@" RC@I JI@ JA OC@ >JI?DODJIN <=JQ@ DN OMP@. (4C@I OMTDIB OC@
KMJBM<H, @IO@M "A<GN@" JM "OMP@" AJM LP@NODJIN OC<O NO<MO RDOC "&N OC@M@ <".). !JI'O
AJMB@O OJ DI>GP?@ OC@ SFBE@CPPM() API>ODJI RC@I O@NODIB OC@ AJGGJRDIB KMJBM<H:
JNQPSU TUE.TUEJP;
JNQPSU TUE.DPOW;
JNQPSU TUE.TUSJOH;
WPJE NBJO()
\
XSJUF(")PX NBOZ BSF XF ");
JOU QFSTPO$PVOU;
SFBEG(" %T", &QFSTPO$PVOU);
XSJUF(")PX NBOZ CJDZDMFT BSF UIFSF ");
JOU CJDZDMF$PVOU;
SFBEG(" %T", &CJDZDMF$PVOU);
XSJUF("8IBU JT UIF EJTUBODF UP UIF CFBDI ");
JOU EJTUBODF;
SFBEG(" %T", &EJTUBODF);
CPPM FYJTUT$BS = SFBE@CPPM("*T UIFSF B DBS ");
CPPM FYJTUT-JDFOTF =
SFBE@CPPM("*T UIFSF B ESJWFS MJDFOTF ");
/*
3FQMBDF UIF 'USVF' CFMPX XJUI B MPHJDBM FYQSFTTJPO UIBU
QSPEVDFT UIF WBMVF 'USVF' XIFO POF PG UIF DPOEJUJPOT
MJTUFE JO UIF RVFTUJPO JT TBUJTGJFE:
*/
XSJUFMO("8F BSF HPJOH UP UIF CFBDI: ", USVF);
^
/*
1MFBTF OPUF UIBU UIJT GVODUJPO JODMVEFT GFBUVSFT UIBU XJMM
CF FYQMBJOFE JO MBUFS DIBQUFST.
*/
CPPM SFBE@CPPM(TUSJOH NFTTBHF)
\
// 1SJOU UIF NFTTBHF
XSJUF(NFTTBHF, "(GBMTF PS USVF) ");
// 3FBE UIF MJOF BT B TUSJOH
TUSJOH JOQVU;
XIJMF (JOQVU.MFOHUI == 0) \
JOQVU = DIPNQ(SFBEMO());
^
// 1SPEVDF B 'CPPM' WBMVF GSPN UIBU TUSJOH
CPPM SFTVMU = UP!CPPM(JOQVU);
// 3FUVSO UIF SFTVMU UP UIF DBMMFS
SFUVSO SFTVMU;
^
"IO@M Q<MDJPN Q<GP@N <I? O@NO OC<O OC@ GJBD><G @SKM@NNDJI OC<O TJP RMJO@ RJMFN
>JMM@>OGT.
... F:7 EA>GF;A@E
"A9;53> EJBD7EE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
29
JG Statement 13
4@'Q@ G@<MI@? OC<O <>OP<G RJMF DI < KMJBM<H <M@ K@MAJMH@? =T @SKM@NNDJIN. AGG JA OC@
@SKM@NNDJIN JA <GG JA OC@ KMJBM<HN OC<O R@'Q@ N@@I NJ A<M C<Q@ NO<MO@? RDOC OC@ NBJO()
API>ODJI <I? R@M@ @S@>PO@? PIODG OC@ @I? JA NBJO.
(F3F7?7@FE JI OC@ JOC@M C<I?, <M@ A@<OPM@N OC<O <AA@>O OC@ @S@>PODJI JA @SKM@NNDJIN.
0O<O@H@ION ?JI'O KMJ?P>@ Q<GP@N <I? ?JI'O C<Q@ ND?@ @AA@>ON OC@HN@GQ@N. 1C@T ?@O@MHDI@
RC@OC@M <I? DI RC<O JM?@M OC@ @SKM@NNDJIN <M@ @S@>PO@?. 0O<O@H@ION NJH@ODH@N PN@ GJBD><G
@SKM@NNDJIN RC@I H<FDIB NP>C ?@>DNDJIN.
Note: %F:7D BDA9D3??;@9 >3@9G397E ?3K :3H7 6;887D7@F 678;@;F;A@E 8AD 7JBD7EE;A@ 3@6
EF3F7?7@F, I:;>7 EA?7 AF:7DE ?3K @AF :3H7 3 6;EF;@5F;A@ 3F 3>>.
The JG block and its scope
13.1
1C@ JG NO<O@H@IO ?@O@MHDI@N RC@OC@M JI@ JM HJM@ @SKM@NNDJIN RJPG? =@ @S@>PO@?. &O H<F@N
OCDN ?@>DNDJI =T @Q<GP<ODIB < GJBD><G @SKM@NNDJI. &O C<N OC@ N<H@ H@<IDIB <N OC@ "IBGDNC RJM?
"DA" <N DI OC@ KCM<N@ "DA OC@M@ DN >JAA@@ OC@I & RDGG ?MDIF >JAA@@".
JG O<F@N < GJBD><G @SKM@NNDJI DI K<M@OC@NDN. &A OC@ Q<GP@ JA OC<O GJBD><G @SKM@NNDJI DN USVF,
OC@I DO @S@>PO@N OC@ @SKM@NNDJIN OC<O <M@ RDOCDI OC@ AJGGJRDIB >PMGT =M<>F@ON. JIQ@MN@GT, DA
OC@ GJBD><G @SKM@NNDJI DN GBMTF, DO ?J@N IJO @S@>PO@ OC@ @SKM@NNDJIN RDOCDI OC@ >PMGT =M<>F@ON.
1C@ <M@< RDOCDI OC@ >PMGT =M<>F@ON DN ><GG@? < E5AB7 <I? <GG JA OC@ >J?@ OC<O DN DI OC<O N>JK@
<M@ ><GG@? < 4>A5= A8 5A67.
%@M@ DN OC@ NTIO<S JA OC@ JG NO<O@H@IO:
JG (B@MPHJDBM@FYQSFTTJPO)
\
// ... FYQSFTTJPO(T) UP FYFDVUF JG USVF
^
#JM @S<HKG@, OC@ KMJBM<H >JINOMP>O OC<O M@KM@N@ION "DA OC@M@ DN >JAA@@ OC@I ?MDIF >JAA@@ <I?
R<NC OC@ >PK" ><I =@ RMDOO@I <N DI OC@ AJGGJRDIB KMJBM<H:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
CPPM FYJTUT$PGGFF = USVF;
JG (FYJTUT$PGGFF) \
XSJUFMO("%SJOL DPGGFF");
XSJUFMO("8BTI UIF DVQ");
^
^
&A OC@ Q<GP@ JA FYJTUT$PGGFF DN GBMTF, OC@I OC@ @SKM@NNDJIN OC<O <M@ RDOCDI OC@ =GJ>F RJPG?
=@ NFDKK@? <I? OC@ KMJBM<H RJPG? IJO KMDIO <ITOCDIB.
AGNJ IJO@ OC<O DO DN < >JHHJI ! >J?DIB NOTG@ OJ OTK@ OC@ JK@IDIB >PMGT =M<>F@ON JI OC@ N<H@
GDI@ <N OC@ JG F@TRJM?.
;8 (F3F7?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
30
The FMTF block and its scope
13.2
0JH@ODH@N OC@M@ <M@ JK@M<ODJIN OJ @S@>PO@ AJM RC@I OC@ GJBD><G @SKM@NNDJI JA OC@ JG
NO<O@H@IO DN GBMTF. #JM @S<HKG@, OC@M@ DN <GR<TN <I JK@M<ODJI OJ @S@>PO@ DI < ?@>DNDJI GDF@ "DA
OC@M@ DN >JAA@@ & RDGG ?MDIF >JAA@@, @GN@ & RDGG ?MDIF O@<".
1C@ JK@M<ODJIN OJ @S@>PO@ DI OC@ GBMTF ><N@ <M@ KG<>@? DI < N>JK@ <AO@M OC@ FMTF F@TRJM?:
JG (B@MPHJDBM@FYQSFTTJPO)
\
// ... FYQSFTTJPO(T) UP FYFDVUF JG USVF
^
FMTF
\
// ... FYQSFTTJPO(T) UP FYFDVUF JG GBMTF
^
#JM @S<HKG@, PI?@M OC@ <NNPHKODJI OC<O OC@M@ DN <GR<TN O@<:
JG (FYJTUT$PGGFF) \
XSJUFMO("%SJOL DPGGFF");
^ FMTF \
XSJUFMO("%SJOL UFB");
^
&I OC<O @S<HKG@, @DOC@M OC@ ADMNO JM OC@ N@>JI? NOMDIB RJPG? =@ KMDIO@? ?@K@I?DIB JI OC@ Q<GP@
JA FYJTUT$PGGFF.
AGNJ IJO@ OC@ KG<>@H@IO JA OC@ >PMGT =M<>F@ON <MJPI? FMTF; OC<O DN < >JHHJI ! >J?DIB NOTG@
<N R@GG.
FMTF DON@GA DN IJO < NO<O@H@IO =PO <I JKODJI<G 5>3GE7 JA OC@ JG NO<O@H@IO; DO ><IIJO =@ PN@?
<GJI@.
Always use the scope curly brackets
13.3
&O DN IJO M@>JHH@I?@? =PO DN <>OP<GGT KJNND=G@ OJ JHDO OC@ >PMGT =M<>F@ON DA OC@M@ DN JIGT JI@
NO<O@H@IO RDOCDI < N>JK@. AN =JOC OC@ JG <I? OC@ FMTF N>JK@N C<Q@ EPNO JI@ NO<O@H@IO <=JQ@,
OC<O >J?@ ><I <GNJ =@ RMDOO@I <N OC@ AJGGJRDIB:
JG (FYJTUT$PGGFF)
XSJUFMO("%SJOL DPGGFF");
FMTF
XSJUFMO("%SJOL UFB");
*JNO @SK@MD@I>@? KMJBM<HH@MN PN@ >PMGT =M<>F@ON @Q@I AJM NDIBG@ NO<O@H@ION. (,I@ JA OC@
@S@M>DN@N JA OCDN >C<KO@M DN <=JPO JHDOODIB OC@H.) %<QDIB N<D? OC<O, & RDGG IJR NCJR OC@ JIGT
><N@ RC@M@ JHDOODIB OC@ >PMGT =M<>F@ON DN <>OP<GGT =@OO@M.
The "if, else if, else" chain
13.4
,I@ JA OC@ KJR@MN JA NO<O@H@ION <I? @SKM@NNDJIN DN OC@ <=DGDOT OJ PN@ OC@H DI HJM@ >JHKG@S
R<TN. &I <??DODJI OJ @SKM@NNDJIN, N>JK@N ><I >JIO<DI JOC@M NO<O@H@ION. #JM @S<HKG@, <I FMTF
N>JK@ ><I >JIO<DI <I JG NO<O@H@IO. JII@>ODIB NO<O@H@ION <I? @SKM@NNDJIN DI ?DAA@M@IO R<TN
<GGJRN PN OJ H<F@ KMJBM<HN =@C<Q@ DIO@GGDB@IOGT <>>JM?DIB OJ OC@DM KPMKJN@N.
;8 (F3F7?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
31
1C@ AJGGJRDIB DN < HJM@ >JHKG@S >J?@ RMDOO@I PI?@M OC@ <BM@@H@IO OC<O MD?DIB OJ < BJJ?
>JAA@@ NCJK DN KM@A@MM@? JQ@M R<GFDIB OJ < =<? JI@:
JG (FYJTUT$PGGFF) \
XSJUFMO("%SJOL DPGGFF BU IPNF");
^ FMTF \
JG (FYJTUT#JDZDMF) \
XSJUFMO("3JEF UP UIF HPPE QMBDF");
^ FMTF \
XSJUFMO("8BML UP UIF CBE QMBDF");
^
^
1C@ >J?@ <=JQ@ M@KM@N@ION OC@ KCM<N@ "DA OC@M@ DN >JAA@@, ?MDIF <O CJH@; @GN@ DA OC@M@ DN <
=D>T>G@, MD?@ OJ OC@ BJJ? KG<>@; JOC@MRDN@ R<GF OJ OC@ =<? KG<>@".
)@O'N >JHKGD><O@ OCDN ?@>DNDJI KMJ>@NN APMOC@M: DINO@<? JA C<QDIB OJ R<GF OJ OC@ =<? KG<>@, G@O'N
ADMNO OMT OC@ I@DBC=JM:
JG (FYJTUT$PGGFF) \
XSJUFMO("%SJOL DPGGFF BU IPNF");
^ FMTF \
JG (FYJTUT#JDZDMF) \
XSJUFMO("3JEF UP UIF HPPE QMBDF");
^ FMTF \
JG (OFJHICPS*T)PNF) \
XSJUFMO(")BWF DPGGFF BU UIF OFJHICPS");
^ FMTF \
XSJUFMO("8BML UP UIF CBE QMBDF");
^
^
^
0P>C ?@>DNDJIN GDF@ "DA OCDN ><N@, @GN@ DA OC<O JOC@M ><N@, @GN@ DA OC<O @Q@I JOC@M ><N@, @O>." <M@
>JHHJI DI KMJBM<HN. 2IAJMOPI<O@GT, RC@I OC@ BPD?@GDI@ JA <GR<TN PNDIB >PMGT =M<>F@ON DN
AJGGJR@? J=NODI<O@GT, OC@ >J?@ @I?N PK C<QDIB OJJ HP>C CJMDUJIO<G <I? Q@MOD><G NK<>@:
DBIJMDIB OC@ @HKOT GDI@N, OC@ 3 JG NO<O@H@ION <I? OC@ 4 XSJUFMO @SKM@NNDJIN <=JQ@ J>>PKT <
OJO<G JA 13 GDI@N.
&I JM?@M OJ RMDO@ NP>C >JINOMP>ON DI < HJM@ >JHK<>O R<T, RC@I <I FMTF N>JK@ >JIO<DIN JIGT
JI@ JG NO<O@H@IO, OC@I OC@ >PMGT =M<>F@ON JA OC<O FMTF N>JK@ <M@ JHDOO@? <N <I @S>@KODJI JA
OCDN BPD?@GDI@.
& <H G@<QDIB OC@ AJGGJRDIB >J?@ PIOD?T <N <I DIO@MH@?D<O@ NO@K =@AJM@ NCJRDIB OC@ =@OO@M
AJMH JA DO. +J >J?@ NCJPG? =@ RMDOO@I DI NP>C <I PIOD?T R<T. (& <H <GNJ G@<QDIB DO =G<>F <I?
RCDO@ OJ DI?D><O@ OC<O DO DN PI<>>@KO<=G@ <N RMDOO@I.)
1C@ AJGGJRDIB DN RC<O OC@ >J?@ GJJFN GDF@ <AO@M M@HJQDIB OC@ >PMGT =M<>F@ON JA OC@ ORJ FMTF
N>JK@N OC<O >JIO<DI EPNO < NDIBG@ JG NO<O@H@IO:
;8 (F3F7?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
32
JG (FYJTUT$PGGFF) \
XSJUFMO("%SJOL DPGGFF BU IPNF");
^ FMTF
JG (FYJTUT#JDZDMF) \
XSJUFMO("3JEF UP UIF HPPE QMBDF");
^ FMTF
JG (OFJHICPS*T)PNF) \
XSJUFMO(")BWF DPGGFF BU UIF OFJHICPS");
^ FMTF \
XSJUFMO("8BML UP UIF CBE QMBDF");
^
&A R@ IJR HJQ@ OCJN@ JG NO<O@H@ION PK OJ OC@ N<H@ GDI@N <N OC@DM @I>GJNDIB FMTF >G<PN@N <I?
OD?T PK OC@ >J?@, R@ @I? PK RDOC OC@ AJGGJRDIB HJM@ M@<?<=G@ AJMH<O:
JG (FYJTUT$PGGFF) \
XSJUFMO("%SJOL DPGGFF BU IPNF");
^ FMTF JG (FYJTUT#JDZDMF) \
XSJUFMO("3JEF UP UIF HPPE QMBDF");
^ FMTF JG (OFJHICPS*T)PNF) \
XSJUFMO(")BWF DPGGFF BU UIF OFJHICPS");
^ FMTF \
XSJUFMO("8BML UP UIF CBE QMBDF");
^
/@HJQDIB OC@ >PMGT =M<>F@ON <GGJRN OC@ >J?@ OJ =@ HJM@ >JHK<>O, <I? GDI@N PK <GG JA OC@
@SKM@NNDJIN AJM @<ND@M M@<?<=DGDOT. 1C@ GJBD><G @SKM@NNDJIN, OC@ JM?@M OC<O OC@T <M@ @Q<GP<O@?,
<I? OC@ JK@M<ODJIN OC<O <M@ @S@>PO@? RC@I OC@T <M@ OMP@ <M@ IJR @<ND@M OJ N@@ <O < BG<I>@.
1CDN >JHHJI KMJBM<HHDIB >JINOMP>O DN ><GG@? OC@ "DA, @GN@ DA, @GN@" >C<DI.
Exercises
13.5
1. 0DI>@ OC@ GJBD><G @SKM@NNDJI =@GJR DN USVF, R@ RJPG? @SK@>O OCDN KMJBM<H OJ 6D;@=
>7?A@367 3@6 I3E: F:7 5GB:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
CPPM FYJTUT-FNPOBEF = USVF;
JG (FYJTUT-FNPOBEF) \
XSJUFMO("%SJOLJOH MFNPOBEF");
XSJUFMO("8BTIJOH UIF DVQ");
^ FMTF
XSJUFMO("&BUJOH QJF");
XSJUFMO("8BTIJOH UIF QMBUF");
^
BPO RC@I TJP MPI OC<O KMJBM<H TJP RDGG N@@ OC<O DO I3E:7E F:7 B>3F7 <N R@GG:
;8 (F3F7?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
33
%SJOLJOH MFNPOBEF
8BTIJOH UIF DVQ
8BTIJOH UIF QMBUF
4CT? JMM@>O OC@ KMJBM<H OJ R<NC OC@ KG<O@ JIGT RC@I OC@ GJBD><G @SKM@NNDJI DN GBMTF.
2. 4MDO@ < KMJBM<H OC<O KG<TN < B<H@ RDOC OC@ PN@M (J=QDJPNGT RDOC OMPNO). 1C@ PN@M OCMJRN <
?D@ <I? @IO@MN DON Q<GP@. "DOC@M OC@ PN@M JM OC@ KMJBM<H RDIN <>>JM?DIB OJ OC@ Q<GP@ JA OC@
?D@:
7BMVF PG UIF EJF 0VUQVU PG UIF QSPHSBN
1 :PV XPO
2 :PV XPO
3 :PV XPO
4 * XPO
5 * XPO
6 * XPO
"OZ PUIFS WBMVF &3303: *OWBMJE WBMVF
BJIPN: %<Q@ OC@ KMJBM<H <GNJ H@IODJI OC@ Q<GP@ RC@I OC@ Q<GP@ DN DIQ<GD?. #JM @S<HKG@:
&3303: 7 JT JOWBMJE
3. )@O'N >C<IB@ OC@ B<H@ =T C<QDIB OC@ PN@M @IO@M < Q<GP@ AMJH 1 OJ 1000. +JR OC@ PN@M
RDIN RC@I OC@ Q<GP@ DN DI OC@ M<IB@ 1-500 <I? OC@ >JHKPO@M RDIN RC@I OC@ Q<GP@ DN DI OC@
M<IB@ 501-1000. <I OC@ KM@QDJPN KMJBM<H =@ @<NDGT HJ?DAD@? OJ RJMF DI OCDN R<T?
... F:7 EA>GF;A@E
;8 (F3F7?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
34
XIJMF Loop 14
1C@ XIJMF GJJK DN NDHDG<M OJ OC@ JG NO<O@H@IO <I? @NN@IOD<GGT RJMFN <N < M@K@<O@? JG
NO<O@H@IO. 'PNO GDF@ JG, XIJMF <GNJ O<F@N < GJBD><G @SKM@NNDJI <I? @Q<GP<O@N OC@ =GJ>F RC@I
OC@ GJBD><G @SKM@NNDJI DN USVF. 1C@ ?DAA@M@I>@ DN OC<O OC@ XIJMF NO<O@H@IO @Q<GP<O@N OC@
GJBD><G @SKM@NNDJI <I? @S@>PO@N OC@ @SKM@NNDJIN DI OC@ =GJ>F <B<DI, <N GJIB <N OC@ GJBD><G
@SKM@NNDJI DN USVF, IJO EPNO JI>@. /@K@<ODIB < =GJ>F JA >J?@ OCDN R<T DN ><GG@? >AAB;@9.
%@M@ DN OC@ NTIO<S JA OC@ XIJMF NO<O@H@IO:
XIJMF (B@MPHJDBM@FYQSFTTJPO)
\
// ... FYQSFTTJPO(T) UP FYFDVUF XIJMF USVF
^
#JM @S<HKG@, OC@ >J?@ OC<O M@KM@N@ION 73F 5AA=;7E 3E >A@9 3E F:7D7 ;E 5AA=;7 ><I =@ >J?@? GDF@
OCDN:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
CPPM FYJTUT$PPLJF = USVF;
XIJMF (FYJTUT$PPLJF) \
XSJUFMO("5BLF DPPLJF");
XSJUFMO("&BU DPPLJF");
^
^
1C<O KMJBM<H RJPG? >JIODIP@ M@K@<ODIB OC@ GJJK =@><PN@ OC@ Q<GP@ JA FYJTUT$PPLJF I@Q@M
>C<IB@N AMJH USVF.
XIJMF DN PN@APG RC@I OC@ Q<GP@ JA OC@ GJBD><G @SKM@NNDJI >C<IB@N ?PMDIB OC@ @S@>PODJI JA OC@
KMJBM<H. 1J N@@ OCDN, G@O'N RMDO@ < KMJBM<H OC<O O<F@N < IPH=@M AMJH OC@ PN@M <N GJIB <N OC<O
IPH=@M DN U@MJ JM BM@<O@M. /@H@H=@M OC<O OC@ DIDOD<G Q<GP@ JA JOU Q<MD<=G@N DN 0:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU OVNCFS;
XIJMF (OVNCFS >= 0) \
XSJUF("1MFBTF FOUFS B OVNCFS: ");
SFBEG(" %T", &OVNCFS);
XSJUFMO("5IBOL ZPV GPS ", OVNCFS);
^
XSJUFMO("&YJUFE UIF MPPQ");
^
1C@ KMJBM<H OC<IFN AJM OC@ KMJQD?@? IPH=@M <I? @SDON OC@ GJJK JIGT RC@I OC@ IPH=@M DN G@NN
OC<I U@MJ.
The DPOUJOVF statement
14.1
1CDN NO<O@H@IO NO<MON OC@ I@SO DO@M<ODJI JA OC@ GJJK MDBCO <R<T, DINO@<? JA @S@>PODIB OC@ M@NO JA
OC@ @SKM@NNDJIN JA OC@ =GJ>F.
I:;>7 "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
35
)@O'N HJ?DAT OC@ KMJBM<H <=JQ@ OJ =@ < GDOOG@ KD>FT: DINO@<? JA OC<IFDIB AJM <IT IPH=@M, G@O'N
IJO <>>@KO 13. 1C@ AJGGJRDIB KMJBM<H ?J@N IJO OC<IF AJM 13 =@><PN@ DI OC<O ><N@ OC@
DPOUJOVF NO<O@H@IO H<F@N OC@ KMJBM<H BJ OJ OC@ =@BDIIDIB JA OC@ GJJK OJ @Q<GP<O@ OC@
GJBD><G @SKM@NNDJI <B<DI:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU OVNCFS;
XIJMF (OVNCFS >= 0) \
XSJUF("1MFBTF FOUFS B OVNCFS: ");
SFBEG(" %T", &OVNCFS);
JG (OVNCFS == 13) \
XSJUFMO("4PSSZ, OPU BDDFQUJOH UIBU POF...");
DPOUJOVF;
^
XSJUFMO("5IBOL ZPV GPS ", OVNCFS);
^
XSJUFMO("&YJUFE UIF MPPQ");
^
4@ ><I ?@ADI@ OC@ =@C<QDJM JA OC<O KMJBM<H <N F3=7 @G?47DE 3E >A@9 3E F:7K 3D7 9D73F7D F:3@
AD 7CG3> FA 0 4GF E=;B 13.
The CSFBL statement
14.2
0JH@ODH@N DO =@>JH@N J=QDJPN OC<O OC@M@ DN IJ I@@? OJ NO<T DI OC@ XIJMF GJJK <IT GJIB@M.
CSFBL <GGJRN OC@ KMJBM<H OJ @SDO OC@ GJJK MDBCO <R<T. 1C@ AJGGJRDIB KMJBM<H @SDON OC@ GJJK <N
NJJI <N DO ADI?N < NK@>D<G IPH=@M:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU OVNCFS;
XIJMF (OVNCFS >= 0) \
XSJUF("1MFBTF FOUFS B OVNCFS: ");
SFBEG(" %T", &OVNCFS);
JG (OVNCFS == 42) \
XSJUFMO("'06/% *5!");
CSFBL;
^
XSJUFMO("5IBOL ZPV GPS ", OVNCFS);
^
XSJUFMO("&YJUFE UIF MPPQ");
^
4@ ><I NPHH<MDU@ OCDN =@C<QDJM <N F3=7 @G?47DE 3E >A@9 3E F:7K 3D7 9D73F7D F:3@ AD 7CG3> FA
0 G@F;> 3 @G?47D ;E 42.
I:;>7 "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
36
Infinite loop
14.3
0JH@ODH@N OC@ GJBD><G @SKM@NNDJI DN DIO@IOD<GGT H<?@ < >JINO<IO USVF. 1C@ CSFBL NO<O@H@IO DN
< >JHHJI R<T JA @SDODIB NP>C ;@8;@;F7 >AABE.
1C@ AJGGJRDIB KMJBM<H KMDION < H@IP DI <I DIADIDO@ GJJK; OC@ JIGT R<T JA @SDODIB OC@ GJJK DN <
CSFBL NO<O@H@IO:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
/* *OGJOJUF MPPQ, CFDBVTF UIF MPHJDBM FYQSFTTJPO JT BMXBZT
* USVF */
XIJMF (USVF) \
XSJUF("0:&YJU, 1:5VSLJTI, 2:&OHMJTI - :PVS DIPJDF ");
JOU DIPJDF;
SFBEG(" %T", &DIPJDF);
JG (DIPJDF == 0) \
XSJUFMO("4FF ZPV MBUFS...");
CSFBL; // 5IF POMZ FYJU PG UIJT MPPQ
^ FMTF JG (DIPJDF == 1) \
XSJUFMO(".FSIBCB!");
^ FMTF JG (DIPJDF == 2) \
XSJUFMO(")FMMP!");
^ FMTF \
XSJUFMO("* EPO'U LOPX UIBU... :/");
^
^
^
Note: "S>@KODJIN 53@ F7D?;@3F7 3@ ;@8;@;F7 >AAB 3E I7>>. ,7 I;>> E77 7J57BF;A@E ;@ 3 >3F7D
5:3BF7D.
Exercises
14.4
1. 1C@ AJGGJRDIB KMJBM<H DN ?@NDBI@? OJ NO<T DI OC@ GJJK <N GJIB <N OC@ DIKPO DN 3, =PO OC@M@ DN
< =PB: DO ?J@NI'O <NF AJM <IT DIKPO:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU OVNCFS;
XIJMF (OVNCFS == 3) \
XSJUF("/VNCFS ");
SFBEG(" %T", &OVNCFS);
^
^
#DS OC@ =PB. 1C@ KMJBM<H NCJPG? NO<T DI OC@ GJJK <N GJIB <N OC@ DIKPO DN 3.
2. *<F@ OC@ >JHKPO@M C@GK AII< <I? BDGG KG<T < B<H@. #DMNO, OC@ >JHKPO@M NCJPG? O<F@ <
IPH=@M AMJH AII< DI OC@ M<IB@ AMJH 1 OJ 10. 1C@ KMJBM<H NCJPG? IJO <>>@KO <IT JOC@M
IPH=@M; DO NCJPG? <NF <B<DI.
,I>@ OC@ KMJBM<H O<F@N < Q<GD? IPH=@M AMJH AII<, DO NCJPG? NO<MO O<FDIB IPH=@MN
AMJH BDGG PIODG C@ BP@NN@N AII<'N IPH=@M >JMM@>OGT.
I:;>7 "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
37
Note: ):7 @G?47DE F:3F A@@3 7@F7DE A4H;AGE>K EF3KE A@ F:7 5A@EA>7 3@6 53@ 47 E77@
4K B;>>. :A) "7F'E ;9@AD7 F:;E 835F 3@6 ID;F7 F:7 BDA9D3? 3E 3@ 7J7D5;E7 A8 F:7 while
EF3F7?7@F.
... F:7 EA>GF;A@E
I:;>7 "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
38
Integers and Arithmetic Operations 15
4@ C<Q@ N@@I OC<O OC@ JG <I? XIJMF NO<O@H@ION <GGJR KMJBM<HN OJ H<F@ ?@>DNDJIN =T PNDIB
OC@ CPPM OTK@ DI OC@ AJMH JA GJBD><G @SKM@NNDJIN. &I OCDN >C<KO@M, R@ RDGG N@@ <MDOCH@OD>
JK@M<ODJIN JI OC@ ;@F797D OTK@N JA !. 1C@N@ A@<OPM@N RDGG <GGJR PN OJ RMDO@ HP>C HJM@ PN@APG
KMJBM<HN.
AGOCJPBC <MDOCH@OD> JK@M<ODJIN <M@ < K<MO JA JPM ?<DGT GDQ@N <I? <M@ <>OP<GGT NDHKG@, OC@M@ <M@
Q@MT DHKJMO<IO >JI>@KON OC<O < KMJBM<HH@M HPNO =@ <R<M@ JA DI JM?@M OJ KMJ?P>@ >JMM@>O
KMJBM<HN: 4;F >7@9F: A8 3 FKB7, AH7D8>AI, G@67D8>AI, <I? FDG@53F;A@.
B@AJM@ BJDIB APMOC@M, & RJPG? GDF@ OJ NPHH<MDU@ OC@ <MDOCH@OD> JK@M<ODJIN DI OC@ AJGGJRDIB
O<=G@ <N < M@A@M@I>@:
0QFSBUPS &GGFDU 4BNQMF
++ JODSFNFOUT CZ POF ++WBSJBCMF
-- EFDSFNFOUT CZ POF --WBSJBCMF
+ UIF SFTVMU PG BEEJOH UXP WBMVFT GJSTU + TFDPOE
- UIF SFTVMU PG TVCUSBDUJOH 'TFDPOE' GSPN 'GJSTU' GJSTU - TFDPOE
* UIF SFTVMU PG NVMUJQMZJOH UXP WBMVFT GJSTU * TFDPOE
/ UIF SFTVMU PG EJWJEJOH 'GJSTU' CZ 'TFDPOE' GJSTU / TFDPOE
% UIF SFNBJOEFS PG EJWJEJOH 'GJSTU' CZ 'TFDPOE' GJSTU % TFDPOE
?? UIF SFTVMU PG SBJTJOH 'GJSTU' UP UIF QPXFS PG
'TFDPOE'
(NVMUJQMZJOH 'GJSTU' CZ JUTFMG 'TFDPOE' UJNFT)
GJSTU ?? TFDPOE
*JNO JA OCJN@ JK@M<OJMN C<Q@ >JPIO@MK<MON OC<O C<Q@ <I = NDBI <OO<>C@?: +=, -=, *=, /=, %=,
<I? ??=. 1C@ ?DAA@M@I>@ RDOC OC@N@ JK@M<OJMN DN OC<O OC@T <NNDBI OC@ M@NPGO OJ OC@ G@AO-C<I?
ND?@:
WBSJBCMF += 10;
1C<O @SKM@NNDJI <??N OC@ Q<GP@ JA WBSJBCMF <I? 10 <I? <NNDBIN OC@ M@NPGO OJ WBSJBCMF. &I OC@
@I?, OC@ Q<GP@ JA WBSJBCMF RJPG? =@ DI>M@<N@? =T 10. &O DN OC@ @LPDQ<G@IO JA OC@ AJGGJRDIB
@SKM@NNDJI:
WBSJBCMF = WBSJBCMF + 10;
& RJPG? GDF@ <GNJ OJ NPHH<MDU@ ORJ DHKJMO<IO >JI>@KON C@M@ =@AJM@ @G<=JM<ODIB JI OC@H
=@GJR.
Overflow: +JO <GG Q<GP@N ><I ADO DI < OTK@, DI RCD>C ><N@ F:7 H3>G7 AH7D8>AIE. #JM @S<HKG@, <
Q<MD<=G@ JA OTK@ VCZUF ><I C<Q@ Q<GP@N JIGT DI OC@ M<IB@ JA 0 OJ 255; NJ RC@I <NNDBI@? 260,
OC@ Q<GP@ JQ@MAGJRN <I? =@>JH@N 4.
Underflow: 0DHDG<MGT, Q<GP@N H<T IJO =@ G@NN OC<I OC@ HDIDHPH Q<GP@ OC<O < OTK@ ><I C<Q@.
Truncation: &IO@B@M OTK@N ><IIJO C<Q@ Q<GP@N RDOC AM<>ODJI<G K<MON. #JM @S<HKG@, OC@ Q<GP@
JA OC@ JOU @SKM@NNDJI 3/2 DN 1, IJO 1.5.
More Information
15.1
4@ @I>JPIO@M <MDOCH@OD> JK@M<ODJIN ?<DGT RDOCJPO HP>C NPMKMDN@N: DA < =<B@G DN $1, ORJ =<B@GN
<M@ $2; DA AJPM N<I?RD>C@N <M@ $15, JI@ N<I?RD>C DN $3.75, @O>.
!@F797DE 3@6 AD;F:?7F;5 %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
39
2IAJMOPI<O@GT OCDIBN <M@ IJO <N NDHKG@ RDOC <MDOCH@OD> JK@M<ODJIN DI >JHKPO@MN. &A R@ ?JI'O
PI?@MNO<I? CJR Q<GP@N <M@ NOJM@? DI < >JHKPO@M, R@ H<T =@ NPMKMDN@? OJ N@@ OC<O <
>JHK<IT'N ?@=O DN D76G576 OJ $1.7 =DGGDJIN RC@I DO =JMMJRN $3 =DGGDJIN HJM@ JI OJK JA DON
@SDNODIB ?@=O JA $3 =DGGDJIN! ,M RC@I < =JS JA D>@ >M@<H N@MQ@N 4 FD?N, <I <MDOCH@OD> JK@M<ODJI
H<T >G<DH OC<O 2 =JS@N RJPG? =@ NPAAD>D@IO AJM 11 FD?N!
-MJBM<HH@MN HPNO PI?@MNO<I? CJR DIO@B@MN <M@ NOJM@? DI >JHKPO@MN.
Integer types 15.1.1
&IO@B@M OTK@N <M@ OC@ OTK@N OC<O ><I C<Q@ JIGT RCJG@ Q<GP@N GDF@ -2, 0, 10, @O>. 1C@N@ OTK@N
><IIJO C<Q@ AM<>ODJI<G K<MON <N DI 2.5. AGG JA OC@ DIO@B@M OTK@N OC<O R@ C<Q@ N@@I DI OC@
#PI?<H@IO<G 1TK@N >C<KO@M <M@ OC@ AJGGJRDIB:
5ZQF
/VNCFS PG
#JUT
*OJUJBM
7BMVF
CZUF 8 0
VCZUF 8 0
TIPSU 16 0
VTIPSU 16 0
JOU 32 0
VJOU 32 0
MPOH 64 0-
VMPOH 64 0-
1C@ V <O OC@ =@BDIIDIB JA OC@ OTK@ I<H@N NO<I?N AJM "PINDBI@?" <I? DI?D><O@N OC<O NP>C OTK@N
><IIJO C<Q@ Q<GP@N G@NN OC<I U@MJ.
Number of bits of a type 15.1.2
&I OJ?<T'N >JHKPO@M NTNO@HN, OC@ NH<GG@NO PIDO JA DIAJMH<ODJI DN ><GG@? < 4;F. AO OC@ KCTND><G
G@Q@G, < =DO DN M@KM@N@IO@? =T @G@>OMD><G NDBI<GN <O >@MO<DI KJDION DI OC@ >DM>PDOMT JA < >JHKPO@M.
A =DO ><I =@ DI JI@ JA ORJ NO<O@N OC<O >JMM@NKJI? OJ OC@ KM@N@I>@ <I? <=N@I>@ JA @G@>OMD><G
NDBI<GN <O OC@ KJDIO OC<O ?@ADI@N OC<O K<MOD>PG<M =DO. 1C@N@ ORJ NO<O@N <M@ <M=DOM<MDGT ?@ADI@? OJ
C<Q@ OC@ Q<GP@N 0 <I? 1. AN < M@NPGO, < =DO ><I C<Q@ JI@ JA OC@N@ ORJ Q<GP@N.
AN OC@M@ <M@I'O H<IT >JI>@KON OC<O ><I =@ M@KM@N@IO@? =T EPNO ORJ NO<O@N, =DO DN IJO < Q@MT
PN@APG OTK@. &O ><I JIGT =@ PN@APG AJM >JI>@KON RDOC ORJ NO<O@N GDF@ C@<?N JM O<DGN JM RC@OC@M <
GDBCO NRDO>C DN JI JM JAA.
&A R@ >JIND?@M ORJ =DON <O < ODH@, OC@ OJO<G IPH=@M JA DIAJMH<ODJI OC<O ><I =@ M@KM@N@IO@?
HPGODKGD@N. B<N@? JI @<>C =DO C<QDIB < Q<GP@ JA 0 JM 1 DI?DQD?P<GGT, OC@M@ <M@ < OJO<G JA 4
KJNND=G@ NO<O@N. ANNPHDIB OC<O OC@ G@AO <I? MDBCO ?DBDON M@KM@N@IO OC@ ADMNO <I? N@>JI? =DO
M@NK@>ODQ@GT, OC@N@ NO<O@N <M@ 00, 01, 10, <I? 11. )@O'N <?? JI@ HJM@ =DO OJ N@@ OCDN @AA@>O
=@OO@M; OCM@@ =DON ><I =@ DI 8 ?DAA@M@IO NO<O@N: 000, 001, 010, 011, 100, 101, 110, 111. AN ><I
=@ N@@I, @<>C <??@? =DO ?JP=G@N OC@ OJO<G IPH=@M JA NO<O@N OC<O ><I =@ M@KM@N@IO@?.
1C@ Q<GP@N OJ RCD>C OC@N@ @DBCO NO<O@N >JMM@NKJI? OJ <M@ ?@ADI@? =T >JIQ@IODJIN. 1C@
AJGGJRDIB O<=G@ NCJRN OC@N@ Q<GP@N AJM OC@ NDBI@? <I? PINDBI@? M@KM@N@IO<ODJIN JA 3 =DON:
!@F797DE 3@6 AD;F:?7F;5 %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
40
#JU
4UBUF
6OTJHOFE
7BMVF
4JHOFE
7BMVF
000 0 0
001 1 1
010 2 2
011 3 3
100 4 -4
101 5 -3
110 6 -2
111 7 -1
4@ ><I KMJ?P>@ OC@ AJGGJRDIB O<=G@ =T <??DIB HJM@ =DON:
#JUT /VNCFS PG %JTUJODU 7BMVFT % 5ZQF .JOJNVN 7BMVF .BYJNVN 7BMVF
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256 CZUF
VCZUF
-128
0
127
255
... ...
16 65,536 TIPSU
VTIPSU
-32768
0
32767
65535
... ...
32 4,294,967,296 JOU
VJOU
-2147483648
0
2147483647
4294967295
... ...
64 18,446,744,073,709,551,616 MPOH
VMPOH
-9223372036854775808
0
9223372036854775807
18446744073709551615
... ...
& C<Q@ NFDKK@? H<IT MJRN DI OC@ O<=G@, <I? DI?D><O@? OC@ NDBI@? <I? PINDBI@? Q@MNDJIN JA OC@
! OTK@N OC<O C<Q@ OC@ N<H@ IPH=@M JA =DON JI OC@ N<H@ MJR (@.B. JOU <I? VJOU <M@ =JOC JI
OC@ 32-=DO MJR).
Choosing a type 15.1.3
0DI>@ < 3-=DO OTK@ ><I JIGT C<Q@ 8 ?DNODI>O Q<GP@N, DO ><I JIGT M@KM@N@IO >JI>@KON GDF@ OC@
Q<GP@ JA < ?D@ JM OC@ IPH=@M JA OC@ ?<T JA OC@ R@@F. (1CDN DN EPNO <I @S<HKG@; OC@M@ DNI'O <
3-=DO OTK@ DI !.)
,I OC@ JOC@M C<I?, <GOCJPBC VJOU DN < Q@MT G<MB@ OTK@, DO ><IIJO M@KM@N@IO OC@ >JI>@KO JA <I
&! IPH=@M AJM @<>C GDQDIB K@MNJI; <N DON H<SDHPH Q<GP@ DN G@NN OC<I OC@ RJMG? KJKPG<ODJI JA 7
=DGGDJI. MPOH <I? VMPOH RJPG? =@ HJM@ OC<I @IJPBC OJ M@KM@N@IO H<IT >JI>@KON.
AN < B@I@M<G MPG@, <N GJIB <N OC@M@ DN IJ NK@>DAD> M@<NJI IJO OJ, TJP ><I PN@ JOU AJM DIO@B@M
Q<GP@N.
!@F797DE 3@6 AD;F:?7F;5 %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
41
Overflow 15.1.4
1C@ A<>O OC<O OTK@N ><I C<Q@ < GDHDO@? M<IB@ JA Q<GP@N H<T ><PN@ PI@SK@>O@? M@NPGON. #JM
@S<HKG@, <GOCJPBC <??DIB ORJ VJOU Q<MD<=G@N RDOC Q<GP@N JA 3 =DGGDJI @<>C NCJPG? KMJ?P>@ 6
=DGGDJI, =@><PN@ OC<O NPH DN BM@<O@M OC<I OC@ H<SDHPH Q<GP@ OC<O < VJOU Q<MD<=G@ ><I CJG?
(<=JPO 4 =DGGDJI), OCDN NPH AH7D8>AIE. 4DOCJPO <IT R<MIDIB, JIGT OC@ ?DAA@M@I>@ JA 6 <I? 4
=DGGDJI B@ON NOJM@?. (A GDOOG@ HJM@ <>>PM<O@GT, 6 HDIPN 4.3 =DGGDJI.)
Truncation 15.1.5
0DI>@ DIO@B@MN ><IIJO C<Q@ Q<GP@N RDOC AM<>ODJI<G K<MON, OC@T GJN@ OC@ K<MO <AO@M OC@ ?@>DH<G
KJDIO. #JM @S<HKG@, <NNPHDIB OC<O < =JS JA D>@ >M@<H N@MQ@N 4 FD?N, <GOCJPBC 11 FD?N RJPG?
<>OP<GGT I@@? 2.75 =JS@N, 2.75 ><I JIGT =@ NOJM@? <N 2 DI <I DIO@B@M OTK@.
& RDGG NCJR GDHDO@? O@>CIDLP@N OJ C@GK M@?P>@ OC@ @AA@>ON JA JQ@MAGJR, PI?@MAGJR, <I?
OMPI><ODJI G<O@M DI OC@ >C<KO@M.
.NJO and .NBY 15.1.6
& RDGG O<F@ <?Q<IO<B@ JA OC@ .NJO <I? .NBY KMJK@MOD@N =@GJR, RCD>C R@ C<Q@ N@@I DI OC@
API?<H@IO<G OTK@N >C<KO@M. 1C@N@ KMJK@MOD@N KMJQD?@ OC@ HDIDHPH <I? H<SDHPH Q<GP@N OC<O
<I DIO@B@M OTK@ ><I C<Q@.
Increment: ++ 15.1.7
1CDN JK@M<OJM DN PN@? RDOC < NDIBG@ Q<MD<=G@ (HJM@ B@I@M<GGT, RDOC < NDIBG@ @SKM@NNDJI) <I? DN
RMDOO@I =@AJM@ OC@ I<H@ JA OC<O Q<MD<=G@. &O DI>M@H@ION OC@ Q<GP@ JA OC<O Q<MD<=G@ =T 1:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU OVNCFS = 10;
++OVNCFS;
XSJUFMO("/FX WBMVF: ", OVNCFS);
^
/FX WBMVF: 11
1C@ DI>M@H@IO JK@M<OJM DN OC@ @LPDQ<G@IO JA PNDIB OC@ 366-3@6-3EE;9@ JK@M<OJM RDOC OC@ Q<GP@
JA 1:
OVNCFS += 1; // TBNF BT ++OVNCFS
&A OC@ M@NPGO JA OC@ DI>M@H@IO JK@M<ODJI DN BM@<O@M OC<I OC@ H<SDHPH Q<GP@ JA OC<O OTK@, OC@
M@NPGO AH7D8>AIE <I? =@>JH@N OC@ HDIDHPH Q<GP@. 4@ ><I N@@ OCDN @AA@>O =T DI>M@H@IODIB <
Q<MD<=G@ OC<O DIDOD<GGT C<N OC@ Q<GP@ JOU.NBY:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFMO("NJOJNVN JOU WBMVF : ", JOU.NJO);
XSJUFMO("NBYJNVN JOU WBMVF : ", JOU.NBY);
JOU OVNCFS = JOU.NBY;
XSJUFMO("CFGPSF UIF JODSFNFOU: ", OVNCFS);
++OVNCFS;
!@F797DE 3@6 AD;F:?7F;5 %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
42
XSJUFMO("BGUFS UIF JODSFNFOU : ", OVNCFS);
^
1C@ Q<GP@ =@>JH@N JOU.NJO <AO@M OC@ DI>M@H@IO:
NJOJNVN JOU WBMVF : -2147483648
NBYJNVN JOU WBMVF : 2147483647
CFGPSF UIF JODSFNFOU: 2147483647
BGUFS UIF JODSFNFOU : -2147483648
1CDN DN < Q@MT DHKJMO<IO J=N@MQ<ODJI, =@><PN@ OC@ Q<GP@ >C<IB@N AMJH OC@ H<SDHPH OJ OC@
HDIDHPH <N < M@NPGO JA ;@5D7?7@F;@9 <I? RDOCJPO <IT R<MIDIB! 1CDN @AA@>O DN ><GG@? AH7D8>AI.
4@ RDGG N@@ NDHDG<M @AA@>ON RDOC JOC@M JK@M<ODJIN.
Decrement: -- 15.1.8
1CDN JK@M<OJM DN NDHDG<M OJ OC@ DI>M@H@IO JK@M<OJM; OC@ ?DAA@M@I>@ DN OC<O OC@ Q<GP@ DN
?@>M@<N@? =T 1:
--OVNCFS; // UIF WBMVF EFDSFBTFT CZ 1
1C@ ?@>M@H@IO JK@M<ODJI DN OC@ @LPDQ<G@IO JA PNDIB OC@ EG4FD35F-3@6-3EE;9@ JK@M<OJM RDOC OC@
Q<GP@ JA 1:
OVNCFS -= 1; // TBNF BT --OVNCFS
0DHDG<M OJ OC@ ++ JK@M<OJM, DA OC@ Q<GP@ DN OC@ HDIDHPH Q<GP@ OJ =@BDI RDOC, DO =@>JH@N OC@
H<SDHPH Q<GP@. 1CDN @AA@>O DN ><GG@? G@67D8>AI.
Addition: + 15.1.9
1CDN JK@M<OJM DN PN@? RDOC ORJ @SKM@NNDJIN <I? <??N OC@DM Q<GP@N:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU GJSTU = 12;
JOU TFDPOE = 100;
XSJUFMO("3FTVMU: ", GJSTU + TFDPOE);
XSJUFMO("8JUI B DPOTUBOU FYQSFTTJPO: ", 1000 + TFDPOE);
^
3FTVMU: 112
8JUI B DPOTUBOU FYQSFTTJPO: 1100
&A OC@ M@NPGO DN BM@<O@M OC<I OC@ NPH JA OC@ ORJ @SKM@NNDJIN, DO <B<DI JQ@MAGJRN <I? =@>JH@N <
Q<GP@ OC<O DN G@NN OC<I =JOC JA OC@ @SKM@NNDJIN:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
// 3 CJMMJPOT FBDI
VJOU GJSTU = 3000000000;
VJOU TFDPOE = 3000000000;
!@F797DE 3@6 AD;F:?7F;5 %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
43
XSJUFMO("NBYJNVN WBMVF PG VJOU: ", VJOU.NBY);
XSJUFMO(" GJSTU: ", GJSTU);
XSJUFMO(" TFDPOE: ", TFDPOE);
XSJUFMO(" TVN: ", GJSTU + TFDPOE);
XSJUFMO("07&3'-08! 5IF SFTVMU JT OPU 6 CJMMJPOT!");
^
NBYJNVN WBMVF PG VJOU: 4294967295
GJSTU: 3000000000
TFDPOE: 3000000000
TVN: 1705032704
07&3'-08! 5IF SFTVMU JT OPU 6 CJMMJPOT!
Subtraction: - 15.1.10
1CDN JK@M<OJM DN PN@? RDOC ORJ @SKM@NNDJIN <I? BDQ@N OC@ ?DAA@M@I>@ =@OR@@I OC@ ADMNO <I? OC@
N@>JI?:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU OVNCFS@1 = 10;
JOU OVNCFS@2 = 20;
XSJUFMO(OVNCFS@1 - OVNCFS@2);
XSJUFMO(OVNCFS@2 - OVNCFS@1);
^
-10
10
&O DN <B<DI NPMKMDNDIB DA OC@ <>OP<G M@NPGO DN G@NN OC<I U@MJ <I? DN NOJM@? DI <I PINDBI@? OTK@.
)@O'N M@RMDO@ OC@ KMJBM<H PNDIB OC@ VJOU OTK@:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
VJOU OVNCFS@1 = 10;
VJOU OVNCFS@2 = 20;
XSJUFMO("130#-&.! VJOU DBOOPU IBWF OFHBUJWF WBMVFT:");
XSJUFMO(OVNCFS@1 - OVNCFS@2);
XSJUFMO(OVNCFS@2 - OVNCFS@1);
^
130#-&.! VJOU DBOOPU IBWF OFHBUJWF WBMVFT:
4294967286
10
&O DN < BJJ? BPD?@GDI@ OJ PN@ NDBI@? OTK@N OJ M@KM@N@IO >JI>@KON OC<O H<T @Q@M =@ NP=OM<>O@?.
AN GJIB <N OC@M@ DN IJ NK@>DAD> M@<NJI IJO OJ, TJP ><I >CJJN@ JOU.
Multiplication: * 15.1.11
1CDN JK@M<OJM HPGODKGD@N OC@ Q<GP@N JA ORJ @SKM@NNDJIN; OC@ M@NPGO DN <B<DI NP=E@>O OJ JQ@MAGJR:
!@F797DE 3@6 AD;F:?7F;5 %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
44
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
VJOU OVNCFS@1 = 6;
VJOU OVNCFS@2 = 7;
XSJUFMO(OVNCFS@1 * OVNCFS@2);
^
42
Division: / 15.1.12
1CDN JK@M<OJM ?DQD?@N OC@ ADMNO @SKM@NNDJI =T OC@ N@>JI? @SKM@NNDJI. 0DI>@ DIO@B@M OTK@N
><IIJO C<Q@ AM<>ODJI<G Q<GP@N, OC@ AM<>ODJI<G K<MO JA OC@ Q<GP@ DN ?DN><M?@?. 1CDN @AA@>O DN
><GG@? FDG@53F;A@. AN < M@NPGO, OC@ AJGGJRDIB KMJBM<H KMDION 3, IJO 3.5:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFMO(7 / 2);
^
3
#JM ><G>PG<ODJIN RC@M@ AM<>ODJI<G K<MON H<OO@M, 8>A3F;@9 BA;@F FKB7E HPNO =@ PN@? DINO@<? JA
DIO@B@MN. 4@ RDGG N@@ AGJ<ODIB KJDIO OTK@N DI OC@ I@SO >C<KO@M.
Remainder: % 15.1.13
1CDN JK@M<OJM ?DQD?@N OC@ ADMNO @SKM@NNDJI =T OC@ N@>JI? @SKM@NNDJI <I? KMJ?P>@N OC@
M@H<DI?@M JA OC@ ?DQDNDJI:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFMO(10 % 6);
^
4
A >JHHJI <KKGD><ODJI JA OCDN JK@M<OJM DN OJ ?@O@MHDI@ RC@OC@M < Q<GP@ DN J?? JM @Q@I. 0DI>@
OC@ M@H<DI?@M JA ?DQD?DIB <I @Q@I IPH=@M =T 2 DN <GR<TN 0, >JHK<MDIB OC@ M@NPGO <B<DINO 0 DN
NPAAD>D@IO OJ H<F@ OC<O ?DNODI>ODJI:
JG ((OVNCFS % 2) == 0) \
XSJUFMO("FWFO OVNCFS");
^ FMTF \
XSJUFMO("PEE OVNCFS");
^
!@F797DE 3@6 AD;F:?7F;5 %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
45
Power: ^^ 15.1.14
1CDN JK@M<OJM M<DN@N OC@ ADMNO @SKM@NNDJI OJ OC@ KJR@M JA OC@ N@>JI? @SKM@NNDJI. #JM @S<HKG@,
M<DNDIB 3 OJ OC@ KJR@M JA 4 DN HPGODKGTDIB 3 =T DON@GA 4 ODH@N:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFMO(3 ?? 4);
^
81
Arithmetic operations with assignment 15.1.15
AGG JA OC@ JK@M<OJMN OC<O O<F@ ORJ @SKM@NNDJIN C<Q@ 3EE;9@?7@F >JPIO@MK<MON. 1C@N@ JK@M<OJMN
<NNDBI OC@ M@NPGO =<>F OJ OC@ @SKM@NNDJI OC<O DN JI OC@ G@AO-C<I? ND?@:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU OVNCFS = 10;
OVNCFS += 20; // TBNF BT OVNCFS = OVNCFS + 20; OPX 30
OVNCFS -= 5; // TBNF BT OVNCFS = OVNCFS - 5; OPX 25
OVNCFS *= 2; // TBNF BT OVNCFS = OVNCFS * 2; OPX 50
OVNCFS /= 3; // TBNF BT OVNCFS = OVNCFS / 3; OPX 16
OVNCFS %= 7; // TBNF BT OVNCFS = OVNCFS % 7; OPX 2
OVNCFS ??= 6; // TBNF BT OVNCFS = OVNCFS ?? 6; OPX 64
XSJUFMO(OVNCFS);
^
64
Negation: - 15.1.16
1CDN JK@M<OJM >JIQ@MON OC@ Q<GP@ JA OC@ @SKM@NNDJI AMJH I@B<ODQ@ OJ KJNDODQ@ JM KJNDODQ@ OJ
I@B<ODQ@:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU OVNCFS@1 = 1;
JOU OVNCFS@2 = -2;
XSJUFMO(-OVNCFS@1);
XSJUFMO(-OVNCFS@2);
^
-1
2
1C@ OTK@ JA OC@ M@NPGO JA OCDN JK@M<ODJI DN OC@ N<H@ <N OC@ OTK@ JA OC@ @SKM@NNDJI. 0DI>@
PINDBI@? OTK@N ><IIJO C<Q@ I@B<ODQ@ Q<GP@N, OC@ M@NPGO JA PNDIB OCDN JK@M<OJM RDOC PINDBI@?
OTK@N ><I =@ NPMKMDNDIB:
!@F797DE 3@6 AD;F:?7F;5 %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
46
VJOU OVNCFS = 1;
XSJUFMO("OFHBUJPO: ", -OVNCFS);
1C@ OTK@ JA -OVNCFS DN VJOU <N R@GG, RCD>C ><IIJO C<Q@ I@B<ODQ@ Q<GP@N:
OFHBUJPO: 4294967295
Plus sign: + 15.1.17
1CDN JK@M<OJM C<N IJ @AA@>O <I? @SDNON JIGT AJM NTHH@OMT RDOC OC@ I@B<ODJI JK@M<OJM. -JNDODQ@
Q<GP@N NO<T KJNDODQ@ <I? I@B<ODQ@ Q<GP@N NO<T I@B<ODQ@:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU OVNCFS@1 = 1;
JOU OVNCFS@2 = -2;
XSJUFMO(+OVNCFS@1);
XSJUFMO(+OVNCFS@2);
^
1
-2
Post-increment: ++ 15.1.18
Note: *@>7EE F:7D7 ;E 3 EFDA@9 D73EA@, 3>I3KE GE7 F:7 D79G>3D ;@5D7?7@F AB7D3FAD (I:;5: ;E
EA?7F;?7E 53>>76 F:7 BD7-;@5D7?7@F AB7D3FAD).
JIOM<MT OJ OC@ M@BPG<M DI>M@H@IO JK@M<OJM, DO DN RMDOO@I <AO@M OC@ @SKM@NNDJI <I? NODGG
DI>M@H@ION OC@ Q<GP@ JA OC@ @SKM@NNDJI =T 1. 1C@ ?DAA@M@I>@ DN, OC@ KJNO-DI>M@H@IO JK@M<ODJI
DON@GA KMJ?P>@N OC@ JG? Q<GP@ JA OC@ @SKM@NNDJI. 1J N@@ OCDN ?DAA@M@I>@, G@O'N >JHK<M@ DO RDOC
OC@ M@BPG<M DI>M@H@IO JK@M<OJM:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU JODSFNFOUFE@SFHVMBSMZ = 1;
XSJUFMO(++JODSFNFOUFE@SFHVMBSMZ); // QSJOUT 2
XSJUFMO(JODSFNFOUFE@SFHVMBSMZ); // QSJOUT 2
JOU QPTU@JODSFNFOUFE = 1;
// (FUT JODSFNFOUFE, CVU JUT PME WBMVF JT VTFE:
XSJUFMO(QPTU@JODSFNFOUFE++); // QSJOUT 1
XSJUFMO(QPTU@JODSFNFOUFE); // QSJOUT 2
^
2
2
1
2
1C@ XSJUFMO(QPTU@JODSFNFOUFE++); NO<O@H@IO <=JQ@ DN OC@ @LPDQ<G@IO JA OC@ AJGGJRDIB
>J?@:
!@F797DE 3@6 AD;F:?7F;5 %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
47
JOU PME@WBMVF = QPTU@JODSFNFOUFE;
++QPTU@JODSFNFOUFE;
XSJUFMO(PME@WBMVF); // QSJOUT 1
Post-decrement: -- 15.1.19
Note: *@>7EE F:7D7 ;E 3 EFDA@9 D73EA@, 3>I3KE GE7 F:7 D79G>3D 675D7?7@F AB7D3FAD (I:;5: ;E
EA?7F;?7E 53>>76 F:7 BD7-675D7?7@F AB7D3FAD).
B@C<Q@N OC@ N<H@ <N OC@ KJNO-DI>M@H@IO JK@M<OJM =PO ?@>M@H@ION.
Operator precedence 15.1.20
1C@ JK@M<OJMN R@'Q@ ?DN>PNN@? <=JQ@ C<Q@ <GG =@@I PN@? DI JK@M<ODJIN JI OC@DM JRI RDOC JIGT
JI@ JM ORJ @SKM@NNDJIN. %JR@Q@M, NDHDG<M OJ GJBD><G @SKM@NNDJIN, DO DN >JHHJI OJ >JH=DI@
OC@N@ JK@M<OJMN OJ AJMH HJM@ >JHKG@S <MDOCH@OD> @SKM@NNDJIN:
JOU WBMVF = 77;
JOU SFTVMU = (((WBMVF + 8) * 3) / (WBMVF - 1)) % 5;
AN RDOC GJBD><G JK@M<OJMN, <MDOCH@OD> JK@M<OJMN <GNJ J=@T JK@M<OJM KM@>@?@I>@ MPG@N. #JM
@S<HKG@, OC@ * JK@M<OJM C<N KM@>@?@I>@ JQ@M OC@ + JK@M<OJM. #JM OC<O M@<NJI, RC@I
K<M@IOC@N@N <M@ IJO PN@? (@.B. DI OC@ WBMVF + 8 * 3 @SKM@NNDJI), OC@ * JK@M<OJM DN @Q<GP<O@?
=@AJM@ OC@ + JK@M<OJM. AN < M@NPGO, OC<O @SKM@NNDJI =@>JH@N OC@ @LPDQ<G@IO JA WBMVF + 24,
RCD>C DN LPDO@ ?DAA@M@IO AMJH (WBMVF + 8) * 3.
2NDIB K<M@IOC@N@N DN PN@APG =JOC AJM @INPMDIB >JMM@>O M@NPGON <I? AJM >JHHPID><ODIB OC@
DIO@IO JA OC@ >J?@ OJ KMJBM<HH@MN RCJ H<T RJMF JI OC@ >J?@ DI OC@ APOPM@.
Potential remedy against overflow 15.1.21
&A OC@ M@NPGO JA <I JK@M<ODJI ><IIJO ADO DI OC@ OTK@ JA OC@ M@NPGO, OC@I OC@M@ DN IJOCDIB OC<O ><I
=@ ?JI@. 0JH@ODH@N, <GOCJPBC OC@ PGODH<O@ M@NPGO RJPG? ADO DI < >@MO<DI OTK@, OC@ DIO@MH@?D<O@
><G>PG<ODJIN H<T JQ@MAGJR <I? ><PN@ DI>JMM@>O M@NPGON.
AN <I @S<HKG@, G@O'N <NNPH@ OC<O R@ I@@? OJ KG<IO <I <KKG@ OM@@ K@M 1000 NLP<M@ H@O@MN JA <I
<M@< OC<O DN 40 =T 60 FDGJH@O@MN. %JR H<IT OM@@N <M@ I@@?@??
4C@I R@ NJGQ@ OCDN KMJ=G@H JI K<K@M, R@ N@@ OC<O OC@ M@NPGO DN 40000 ODH@N 60000 ?DQD?@? =T
1000, =@DIB @LP<G OJ 2.4 HDGGDJI OM@@N. )@O'N RMDO@ < KMJBM<H OC<O @S@>PO@N OCDN ><G>PG<ODJI:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU XJEUI = 40000;
JOU MFOHUI = 60000;
JOU BSFB1FS5SFF = 1000;
JOU USFFT/FFEFE = XJEUI * MFOHUI / BSFB1FS5SFF;
XSJUFMO("/VNCFS PG USFFT OFFEFE: ", USFFT/FFEFE);
^
/VNCFS PG USFFT OFFEFE: -1894967
!@F797DE 3@6 AD;F:?7F;5 %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
48
+JO OJ H@IODJI DO'N IJO @Q@I >GJN@, OC@ M@NPGO DN <GNJ G@NN OC<I U@MJ! &I OCDN ><N@, OC@
DIO@MH@?D<O@ ><G>PG<ODJI XJEUI * MFOHUI JQ@MAGJRN <I? OC@ NP=N@LP@IO ><G>PG<ODJI JA
/ BSFB1FS5SFF KMJ?P>@N <I DI>JMM@>O M@NPGO.
,I@ R<T JA <QJD?DIB OC@ JQ@MAGJR DI OCDN @S<HKG@ DN OJ >C<IB@ OC@ JM?@M JA JK@M<ODJIN:
JOU USFFT/FFEFE = XJEUI / BSFB1FS5SFF * MFOHUI ;
1C@ M@NPGO RJPG? IJR =@ >JMM@>O:
/VNCFS PG USFFT OFFEFE: 2400000
1C@ M@<NJI OCDN H@OCJ? RJMFN DN OC@ A<>O OC<O <GG JA OC@ NO@KN JA OC@ ><G>PG<ODJI IJR ADO OC@
JOU OTK@.
-G@<N@ IJO@ OC<O OCDN DN IJO < >JHKG@O@ NJGPODJI =@><PN@ OCDN ODH@ OC@ DIO@MH@?D<O@ Q<GP@ DN
KMJI@ OJ OMPI><ODJI, RCD>C H<T <AA@>O OC@ M@NPGO NDBIDAD><IOGT DI >@MO<DI JOC@M ><G>PG<ODJIN.
AIJOC@M NJGPODJI HDBCO =@ OJ PN@ < AGJ<ODIB KJDIO OTK@ DINO@<? JA <I DIO@B@M OTK@: GMPBU,
EPVCMF, JM SFBM.
Potential remedy against truncation 15.1.22
C<IBDIB OC@ JM?@M JA JK@M<ODJIN H<T =@ < NJGPODJI OJ OMPI><ODJI <N R@GG. AI DIO@M@NODIB
@S<HKG@ JA OMPI><ODJI ><I =@ N@@I =T ?DQD?DIB <I? HPGODKGTDIB < Q<GP@ RDOC OC@ N<H@ IPH=@M.
4@ RJPG? @SK@>O OC@ M@NPGO JA 10/9*9 OJ =@ 10, =PO DO >JH@N JPO <N 9:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFMO(10 / 9 * 9);
^
9
1C@ M@NPGO DN >JMM@>O RC@I OMPI><ODJI DN <QJD?@? =T >C<IBDIB OC@ JM?@M JA JK@M<ODJIN:
XSJUFMO(10 * 9 / 9);
10
1CDN OJJ DN IJO < >JHKG@O@ NJGPODJI: 1CDN ODH@ OC@ DIO@MH@?D<O@ ><G>PG<ODJI >JPG? =@ KMJI@ OJ
JQ@MAGJR. 2NDIB < AGJ<ODIB KJDIO OTK@ H<T =@ <IJOC@M NJGPODJI OJ OMPI><ODJI DI >@MO<DI
><G>PG<ODJIN.
Exercises
15.2
1. 4MDO@ < KMJBM<H OC<O O<F@N ORJ DIO@B@MN AMJH OC@ PN@M, KMDION OC@ DIO@B@M LPJOD@IO
M@NPGODIB AMJH OC@ ?DQDNDJI JA OC@ ADMNO =T OC@ N@>JI?, <I? <GNJ KMDION OC@ M@H<DI?@M. #JM
@S<HKG@, RC@I 7 <I? 3 <M@ @IO@M@?, C<Q@ OC@ KMJBM<H KMDIO OC@ AJGGJRDIB @LP<ODJI:
7 = 3 * 2 + 1
!@F797DE 3@6 AD;F:?7F;5 %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
49
2. *J?DAT OC@ KMJBM<H OJ KMDIO NCJMO@M JPOKPO RC@I OC@ M@H<DI?@M DN 0. #JM @S<HKG@, RC@I
10 <I? 5 <M@ @IO@M@?, DO NCJPG? IJO KMDIO "10 = 5 * 2 + 0" =PO EPNO OC@ AJGGJRDIB:
10 = 5 * 2
3. 4MDO@ < NDHKG@ ><G>PG<OJM OC<O NPKKJMON OC@ AJPM =<ND> <MDOCH@OD> JK@M<ODJIN. %<Q@ OC@
KMJBM<H G@O OC@ JK@M<ODJI OJ =@ N@G@>O@? AMJH < H@IP <I? <KKGT OC<O JK@M<ODJI OJ OC@ ORJ
Q<GP@N OC<O <M@ @IO@M@?. 6JP ><I DBIJM@ JQ@MAGJR <I? OMPI><ODJI DI OCDN KMJBM<H.
4. 4MDO@ < KMJBM<H OC<O KMDION OC@ Q<GP@N AMJH 1 OJ 10, @<>C JI < N@K<M<O@ GDI@, RDOC OC@
@S>@KODJI JA Q<GP@ 7. !J IJO PN@ M@K@<O@? GDI@N GDF@ OC@ AJGGJRDIB:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
// %P OPU EP UIJT!
XSJUFMO(1);
XSJUFMO(2);
XSJUFMO(3);
XSJUFMO(4);
XSJUFMO(5);
XSJUFMO(6);
XSJUFMO(8);
XSJUFMO(9);
XSJUFMO(10);
^
&INO@<?, DH<BDI@ < Q<MD<=G@ RCJN@ Q<GP@ DN DI>M@H@IO@? DI < GJJK. 6JP H<T I@@? OJ
O<F@ <?Q<IO<B@ JA OC@ ;E @AF 7CG3> FA JK@M<OJM != C@M@.
... F:7 EA>GF;A@E
!@F797DE 3@6 AD;F:?7F;5 %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
50
Floating Point Types 16
&I OC@ KM@QDJPN >C<KO@M R@ C<Q@ N@@I OC<O ?@NKDO@ OC@DM @<N@ JA PN@, <MDOCH@OD> JK@M<ODJIN JI
DIO@B@MN <M@ KMJI@ OJ KMJBM<HHDIB @MMJMN ?P@ OJ JQ@MAGJR, PI?@MAGJR, <I? OMPI><ODJI. 4@
C<Q@ <GNJ N@@I OC<O DIO@B@MN ><IIJO C<Q@ Q<GP@N RDOC AM<>ODJI<G K<MON <N DI 1.25.
#GJ<ODIB KJDIO OTK@N <M@ @NK@>D<GGT OJ NPKKJMO AM<>ODJI<G K<MON. 1C@ "KJDIO" DI OC@DM I<H@
>JH@N AMJH OC@ 675;?3> BA;@F <I? "AGJ<ODIB" M@A@MN OJ <I DHKG@H@IO<ODJI ?@O<DG JA CJR OC@N@
OTK@N <M@ DHKG@H@IO@?: OC@ ?@>DH<G KJDIO 8>A3FE G@AO <I? MDBCO <N <KKMJKMD<O@. (1CDN ?@O<DG DN
IJO DHKJMO<IO RC@I PNDIB OC@N@ OTK@N.)
4@ HPNO >JQ@M DHKJMO<IO ?@O<DGN DI OCDN >C<KO@M <N R@GG. B@AJM@ ?JDIB OC<O, & RJPG? GDF@ OJ
BDQ@ < GDNO JA NJH@ JA OC@ DIO@M@NODIB <NK@>ON JA AGJ<ODIB KJDIO OTK@N:
t A??DIB 0.001 < OCJPN<I? ODH@N DN IJO OC@ N<H@ <N <??DIB 1.
t 2NDIB OC@ GJBD><G JK@M<OJMN == <I? != RDOC AGJ<ODIB KJDIO OTK@N DN @MMJI@JPN DI HJNO ><N@N.
t 1C@ DIDOD<G Q<GP@N JA AGJ<ODIB KJDIO OTK@N DN .OBO, IJO 0. .OBO H<T IJO =@ PN@? DI
@SKM@NNDJIN. 4C@I PN@? DI >JHK<MDNJI JK@M<ODJIN, .OBO DN IJO G@NN OC<I IJM BM@<O@M OC<I
<IT Q<GP@.
t 1C@ JQ@MAGJR Q<GP@ DN .JOGJOJUZ <I? OC@ PI?@MAGJR Q<GP@ DN I@B<ODQ@ .JOGJOJUZ.
AGOCJPBC AGJ<ODIB KJDIO OTK@N <M@ HJM@ PN@APG DI NJH@ ><N@N, OC@T C<Q@ K@>PGD<MDOD@N OC<O
@Q@MT KMJBM<HH@M HPNO FIJR. JHK<M@? OJ DIO@B@MN, OC@T <M@ Q@MT BJJ? RDOC <QJD?DIB
OMPI><ODJI =@><PN@ OC@DM H<DI KPMKJN@ DN OJ NPKKJMO AM<>ODJI<G Q<GP@N. )DF@ <IT JOC@M OTK@,
=@DIB =<N@? JI >@MO<DI IPH=@MN JA =DON, OC@T OJJ <M@ KMJI@ OJ JQ@MAGJR <I? PI?@MAGJR, =PO
>JHK<M@? OJ DIO@B@MN, OC@ M<IB@ JA Q<GP@N OC<O OC@T ><I NPKKJMO DN Q<NO. A??DODJI<GGT, DINO@<?
JA =@DIB NDG@IO DI OC@ ><N@ JA JQ@MAGJR <I? PI?@MAGJR, OC@T B@O OC@ NK@>D<G Q<GP@N JA KJNDODQ@
<I? I@B<ODQ@ ;@8;@;FK.
AN < M@HDI?@M, <GG JA OC@ AGJ<ODIB KJDIO OTK@N <M@ OC@ AJGGJRDIB:
5ZQF /VNCFS PG #JUT *OJUJBM
7BMVF
GMPBU 32 GMPBU.OBO
EPVCMF 64 EPVCMF.OBO
SFBM BU MFBTU 64, NBZCF NPSF
(F.H. 80, EFQFOEJOH PO IBSEXBSF
TVQQPSU)
SFBM.OBO
Floating point type properties
16.1
#GJ<ODIB KJDIO OTK@N C<Q@ HJM@ KMJK@MOD@N OC<I JOC@M OTK@N:
t .TUSJOHPG DN OC@ I<H@ JA OC@ OTK@.
t .TJ[FPG DN OC@ G@IBOC JA OC@ OTK@ DI O@MHN JA =TO@N. (&I JM?@M OJ ?@O@MHDI@ OC@ =DO >JPIO,
OCDN Q<GP@ HPNO =@ HPGODKGD@? =T 8, OC@ IPH=@M JA =DON DI < =TO@.)
t .NBY DN OC@ NCJMO AJM "H<SDHPH" <I? DN OC@ H<SDHPH Q<GP@ OC<O OC@ OTK@ ><I C<Q@; OC@
I@B<ODQ@ JA OCDN Q<GP@ DN OC@ HDIDHPH Q<GP@ OC<O OC@ OTK@ ><I C<Q@.
t .NJO@OPSNBM DN OC@ NH<GG@NO IJMH<GDU@? Q<GP@ OC<O OCDN OTK@ ><I C<Q@ (OC@ OTK@ ><I C<Q@
NH<GG@M Q<GP@N OC<I .NJO@OPSNBM =PO OC@ KM@>DNDJI JA OCJN@ Q<GP@N <M@ G@NN OC<I OC@
IJMH<G KM@>DNDJI JA OC@ OTK@).
t .EJH DN OC@ NCJMO AJM "?DBDON" <I? NK@>DAD@N OC@ IPH=@M JA ?DBDON OC<O NDBIDAT OC@ KM@>DNDJI
JA OC@ OTK@.
t .JOGJOJUZ DN OC@ NK@>D<G Q<GP@ PN@? OJ ?@IJO@ JQ@MAGJR <I? PI?@MAGJR.
F>A3F;@9 &A;@F )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
51
+JO@ OC<O OC@ HDIDHPH Q<GP@ JA AGJ<ODIB KJDIO OTK@N DN IJO .NJO =PO OC@ I@B<ODQ@ JA .NBY, @.B.
OC@ HDIDHPH Q<GP@ JA EPVCMF DN -EPVCMF.NBY.
,OC@M KMJK@MOD@N JA AGJ<ODIB KJDIO OTK@N <M@ PN@? G@NN >JHHJIGT. 6JP ><I N@@ <GG JA OC@H <O
-MJK@MOD@N AJM #GJ<ODIB -JDIO 1TK@N.
1C@ KMJK@MOD@N JA AGJ<ODIB KJDIO OTK@N <I? OC@DM M@G<ODJIN ><I =@ NCJRI JI < IPH=@M GDI@ GDF@
OC@ AJGGJRDIB:
+ +-----------+------------+ .... + .... +----------+----------+ +
] -NBY -1 ] 0 ] 1 NBY ]
-JOGJOJUZ -NJO@OPSNBM NJO@OPSNBM JOGJOJUZ
1C@ ?<NC@? KJMODJIN JA OC@ GDI@ <=JQ@ <M@ OJ N><G@: OC@ IPH=@M JA Q<GP@N OC<O ><I =@
M@KM@N@IO@? =@OR@@I NJO@OPSNBM <I? 1 DN @LP<G OJ OC@ IPH=@M JA Q<GP@N OC<O ><I =@
M@KM@N@IO@? =@OR@@I 1 <I? NBY. 1CDN H@<IN OC<O OC@ KM@>DNDJI JA OC@ AM<>ODJI<G K<MON JA OC@
Q<GP@N OC<O <M@ =@OR@@I NJO@OPSNBM <I? 1 <M@ Q@MT CDBC. (1C@ N<H@ DN OMP@ AJM OC@ I@B<ODQ@
ND?@ <N R@GG.)
.nan
16.2
4@ C<Q@ <GM@<?T N@@I OC<O OCDN DN OC@ ?@A<PGO Q<GP@ JA AGJ<ODIB KJDIO Q<MD<=G@N. .OBO H<T
<KK@<M <N < M@NPGO JA H@<IDIBG@NN AGJ<ODIB KJDIO @SKM@NNDJIN <N R@GG. #JM @S<HKG@ OC@ AGJ<ODIB
KJDIO @SKM@NNDJIN DI OC@ AJGGJRDIB KMJBM<H <GG KMJ?P>@ EPVCMF.OBO:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
EPVCMF [FSP = 0;
EPVCMF JOGJOJUZ = EPVCMF.JOGJOJUZ;
XSJUFMO("BOZ FYQSFTTJPO XJUI OBO: ", EPVCMF.OBO + 1);
XSJUFMO("[FSP / [FSP : ", [FSP / [FSP);
XSJUFMO("[FSP * JOGJOJUZ : ", [FSP * JOGJOJUZ);
XSJUFMO("JOGJOJUZ / JOGJOJUZ : ", JOGJOJUZ / JOGJOJUZ);
XSJUFMO("JOGJOJUZ - JOGJOJUZ : ", JOGJOJUZ - JOGJOJUZ);
^
Specifying floating point values
16.3
#GJ<ODIB KJDIO Q<GP@N ><I NDHKGT =@ NK@>DAD@? <N DIO@B@MN RDOCJPO < ?@>DH<G KJDIO GDF@ 123, JM
RDOC < ?@>DH<G KJDIO GDF@ 12.3.
#GJ<ODIB KJDIO Q<GP@N ><I <GNJ =@ NK@>DAD@? RDOC OC@ NK@>D<G AGJ<ODIB KJDIO NTIO<S <N DI
1.23F+4. 1C@ F+ K<MO DI OC<O NTIO<S ><I =@ M@<? <N "ODH@N 10 OJ OC@ KJR@M JA". A>>JM?DIB OJ
OC<O M@<?DIB, OC@ KM@QDJPN Q<GP@ DN "1.23 ODH@N 10 OJ OC@ KJR@M JA 4", RCD>C DN OC@ N<H@ <N
"1.23 ODH@N 10
4
", RCD>C DI OPMI DN OC@ N<H@ <N 1.23S10000, =@DIB @LP<G OJ 12300.
&A OC@ Q<GP@ <AO@M F DN I@B<ODQ@ <N DI 5.67F-3, OC@I DO DN M@<? <N "?DQD?@? =T 10 OJ OC@ KJR@M
JA". A>>JM?DIBGT, OCDN @S<HKG@ DN "5.67 ?DQD?@? =T 10
3
", RCD>C DI OPMI DN OC@ N<H@ <N 5.67/
1000, =@DIB @LP<G OJ 0.00567.
1C@ Q<GP@N OC<O <M@ KMDIO@? =T OC@ AJGGJRDIB KMJBM<H <M@ <GG DI OC@ AGJ<ODIB KJDIO AJMH<O. 1C@
KMJBM<H KMDION OC@ KMJK@MOD@N JA OC@ OCM@@ OTK@N:
JNQPSU TUE.TUEJP;
WPJE NBJO()
F>A3F;@9 &A;@F )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
52
\
XSJUFMO("5ZQF : ", GMPBU.TUSJOHPG);
XSJUFMO("1SFDJTJPO : ", GMPBU.EJH);
XSJUFMO(".JOJNVN OPSNBMJ[FE WBMVF: ", GMPBU.NJO@OPSNBM);
XSJUFMO(".BYJNVN WBMVF : ", GMPBU.NBY);
XSJUFMO(".JOJNVN WBMVF : ", -GMPBU.NBY);
XSJUFMO();
XSJUFMO("5ZQF : ", EPVCMF.TUSJOHPG);
XSJUFMO("1SFDJTJPO : ", EPVCMF.EJH);
XSJUFMO(".JOJNVN OPSNBMJ[FE WBMVF: ", EPVCMF.NJO@OPSNBM);
XSJUFMO(".BYJNVN WBMVF : ", EPVCMF.NBY);
XSJUFMO(".JOJNVN WBMVF : ", -EPVCMF.NBY);
XSJUFMO();
XSJUFMO("5ZQF : ", SFBM.TUSJOHPG);
XSJUFMO("1SFDJTJPO : ", SFBM.EJH);
XSJUFMO(".JOJNVN OPSNBMJ[FE WBMVF: ", SFBM.NJO@OPSNBM);
XSJUFMO(".BYJNVN WBMVF : ", SFBM.NBY);
XSJUFMO(".JOJNVN WBMVF : ", -SFBM.NBY);
^
1C@ JPOKPO JA OC@ KMJBM<H DN OC@ AJGGJRDIB DI HT @IQDMJIH@IO. 0DI>@ SFBM ?@K@I?N JI OC@
C<M?R<M@, TJP H<T B@O < ?DAA@M@IO JPOKPO AJM DO:
5ZQF : GMPBU
1SFDJTJPO : 6
.JOJNVN OPSNBMJ[FE WBMVF: 1.17549F-38
.BYJNVN WBMVF : 3.40282F+38
.JOJNVN WBMVF : -3.40282F+38
5ZQF : EPVCMF
1SFDJTJPO : 15
.JOJNVN OPSNBMJ[FE WBMVF: 2.22507F-308
.BYJNVN WBMVF : 1.79769F+308
.JOJNVN WBMVF : -1.79769F+308
5ZQF : SFBM
1SFDJTJPO : 18
.JOJNVN OPSNBMJ[FE WBMVF: 3.3621F-4932
.BYJNVN WBMVF : 1.18973F+4932
.JOJNVN WBMVF : -1.18973F+4932
Observations 16.3.1
AN TJP RDGG M@H@H=@M AMJH OC@ KM@QDJPN >C<KO@M, OC@ H<SDHPH Q<GP@ JA VMPOH C<N 20 ?DBDON:
18,446,744,073,709,551,616. 1C<O Q<GP@ GJJFN NH<GG RC@I >JHK<M@? OJ @Q@I OC@ NH<GG@NO
AGJ<ODIB KJDIO OTK@: GMPBU ><I C<Q@ Q<GP@N PK OJ OC@ 10
38
M<IB@, @.B.
340,282,000,000,000,000,000,000,000,000,000,000,000.
1C@ H<SDHPH Q<GP@ JA SFBM DN DI OC@ M<IB@ 10
4932
, < Q<GP@ RDOC HJM@ OC<I 4900 ?DBDON!
AN <IJOC@M J=N@MQ<ODJI, G@O'N GJJF <O OC@ HDIDHPH Q<GP@ OC<O EPVCMF ><I M@KM@N@IO RDOC
15-?DBDO KM@>DNDJI: 0.000...F:7D7 3D7 300 ?AD7 L7DA7E :7D7...0000222507. (*JM@ KM@>DN@GT, OC@
G<NO K<MO DN ...222507385850720.)
Overflow and underflow are not ignored
16.4
!@NKDO@ =@DIB <=G@ OJ O<F@ Q@MT G<MB@ Q<GP@N, AGJ<ODIB KJDIO OTK@N OJJ <M@ KMJI@ OJ JQ@MAGJR
<I? PI?@MAGJR. 1C@ AGJ<ODIB KJDIO OTK@N <M@ N<A@M OC<I DIO@B@M OTK@N DI OCDN M@B<M? =@><PN@
F>A3F;@9 &A;@F )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
53
JQ@MAGJR <I? PI?@MAGJR <M@ IJO DBIJM@?. 1C@ Q<GP@N OC<O JQ@MAGJR =@>JH@ .JOGJOJUZ, <I?
OC@ Q<GP@N OC<O PI?@MAGJR =@>JH@ -.JOGJOJUZ. 1J N@@ OCDN, G@O'N DI>M@H@IO OC@ Q<GP@ JA .NBY
=T 10%. 0DI>@ OC@ Q<GP@ DN <GM@<?T <O OC@ H<SDHPH, DI>M@H@IODIB =T 10% RJPG? JQ@MAGJR:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
SFBM WBMVF = SFBM.NBY;
XSJUFMO("#FGPSF : ", WBMVF);
// .VMUJQMZJOH XJUI 1.1 JT UIF TBNF BT NBLJOH 110%
WBMVF *= 1.1;
XSJUFMO(""EEFE 10% : ", WBMVF);
// -FU'T USZ UP NBLF JU MFTT CZ EJWJEJOH JO IBMG
WBMVF /= 2;
XSJUFMO("%JWJEFE JO IBMG: ", WBMVF);
^
,I>@ OC@ Q<GP@ JQ@MAGJRN <I? =@>JH@N SFBM.JOGJOJUZ, DO M@H<DIN OC<O R<T @Q@I <AO@M =@DIB
?DQD?@? DI C<GA:
#FGPSF : 1.18973F+4932
"EEFE 10% : JOG
%JWJEFE JO IBMG: JOG
Precision
16.5
-M@>DNDJI DN < >JI>@KO OC<O R@ >JH@ <>MJNN DI ?<DGT GDA@ =PO IJO O<GF <=JPO HP>C. -M@>DNDJI DN
OC@ IPH=@M JA ?DBDON OC<O DN PN@? RC@I NK@>DATDIB < Q<GP@. #JM @S<HKG@, RC@I R@ N<T OC<O OC@
OCDM? JA 100 DN 33, OC@ KM@>DNDJI DN 2 =@><PN@ 33 C<N 2 ?DBDON. 4C@I OC@ Q<GP@ DN NK@>DAD@?
HJM@ KM@>DN@GT GDF@ 33.33, OC@I OC@ KM@>DNDJI DN 4 ?DBDON.
1C@ IPH=@M JA =DON OC<O @<>C AGJ<ODIB OTK@ C<N IJO JIGT @AA@>ON DON H<SDHPH Q<GP@, =PO <GNJ DON
KM@>DNDJI. 1C@ BM@<O@M OC@ IPH=@M JA =DON, OC@ HJM@ KM@>DN@ OC@ Q<GP@N.
There is no truncation in division
16.6
AN R@ C<Q@ N@@I DI OC@ KM@QDJPN >C<KO@M, DIO@B@M ?DQDNDJI ><IIJO KM@N@MQ@ OC@ AM<>ODJI<G K<MO
JA OC@ M@NPGO:
JOU GJSTU = 3;
JOU TFDPOE = 2;
XSJUFMO(GJSTU / TFDPOE);
,POKPO:
1
#GJ<ODIB KJDIO OTK@N ?JI'O C<Q@ OCDN FDG@53F;A@ KMJ=G@H; OC@T <M@ NK@>DAD><GGT AJM KM@N@MQDIB
OC@ AM<>ODJI<G K<MON:
EPVCMF GJSTU = 3;
EPVCMF TFDPOE = 2;
XSJUFMO(GJSTU / TFDPOE);
F>A3F;@9 &A;@F )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
54
,POKPO:
1.5
1C@ <>>PM<>T JA OC@ AM<>ODJI<G K<MO ?@K@I?N JI OC@ KM@>DNDJI JA OC@ OTK@: SFBM C<N OC@ CDBC@NO
KM@>DNDJI <I? GMPBU C<N OC@ GJR@NO KM@>DNDJI.
Which type to use
16.7
2IG@NN OC@M@ DN < NK@>DAD> M@<NJI JOC@MRDN@, TJP ><I >CJJN@ EPVCMF AJM AGJ<ODIB KJDIO Q<GP@N.
GMPBU C<N Q@MT GJR KM@>DNDJI =PO =@><PN@ JA =@DIB NH<GG@M OC<I OC@ JOC@M OTK@N, DO H<T =@
PN@APG RC@I NOJM<B@ NK<>@ DN GDHDO@?. ,I OC@ JOC@M C<I?, NDI>@ OC@ KM@>DNDJI JA SFBM DN CDBC@M
OC<I EPVCMF JI NJH@ C<M?R<M@, DO RJPG? =@ KM@A@M<=G@ AJM CDBC KM@>DNDJI ><G>PG<ODJIN.
Cannot represent all values
16.8
4@ ><IIJO M@KM@N@IO >@MO<DI Q<GP@N DI JPM ?<DGT GDQ@N. &I OC@ ?@>DH<G NTNO@H OC<O R@ PN@ ?<DGT,
OC@ ?DBDON =@AJM@ OC@ ?@>DH<G KJDIO M@KM@N@IO JI@N, O@IN, CPI?M@?N, @O>. <I? OC@ ?DBDON <AO@M
OC@ ?@>DH<G KJDIO M@KM@N@IO O@IOCN, CPI?M@OCN, OCJPN<I?OCN, @O>.
&A < Q<GP@ DN <I @S<>O >JH=DI<ODJI JA OC@N@ Q<GP@N, DO ><I =@ M@KM@N@IO@? KM@>DN@GT. #JM
@S<HKG@, =@><PN@ OC@ Q<GP@ 0.23 >JINDNON JA 2 O@IOCN <I? 3 CPI?M@OCN DO DN M@KM@N@IO@?
KM@>DN@GT. ,I OC@ JOC@M C<I?, OC@ Q<GP@ JA 1/3 ><IIJO =@ M@KM@N@IO@? DI OC@ ?@>DH<G NTNO@H
=@><PN@ DO DN I@Q@M NPAAD>D@IO IJ H<OO@M CJR H<IT ?DBDON <M@ NK@>DAD@?: 0.33333...
&O DN Q@MT NDHDG<M RDOC OC@ AGJ<ODIB KJDIO OTK@N. B@><PN@ OC@N@ OTK@N <M@ =<N@? JI >@MO<DI
IPH=@MN JA =DON, OC@T ><IIJO M@KM@N@IO @Q@MT Q<GP@.
1C@ ?DAA@M@I>@ RDOC OC@ =DI<MT NTNO@H OC<O OC@ >JHKPO@MN PN@ DN OC<O OC@ ?DBDON =@AJM@ OC@
?@>DH<G KJDIO <M@ JI@N, ORJN, AJPMN, @O>. <I? OC@ ?DBDON <AO@M OC@ ?@>DH<G KJDIO <M@ C<GQ@N,
LP<MO@MN, @DBCOCN, @O>. ,IGT OC@ Q<GP@N OC<O <M@ @S<>O >JH=DI<ODJIN JA OCJN@ ?DBDON ><I =@
M@KM@N@IO@? KM@>DN@GT.
A Q<GP@ OC<O ><IIJO =@ M@KM@N@IO@? DI >JHKPO@MN DN 0.1 <N DI 10 >@ION. AGOCJPBC OCDN Q<GP@
><I =@ M@KM@N@IO@? KM@>DN@GT DI OC@ ?@>DH<G NTNO@H, DON =DI<MT M@KM@N@IO<ODJI I@Q@M @I?N <I?
>JIODIPJPNGT M@K@<ON AJPM ?DBDON <N 0.0001100110011... (+JO@ OC<O DO DN RMDOO@I DI =DI<MT
NTNO@H, IJO ?@>DH<G.) &O DN <GR<TN DI<>>PM<O@ <O NJH@ G@Q@G ?@K@I?DIB JI OC@ KM@>DNDJI JA OC@
AGJ<ODIB KJDIO OTK@ OC<O DN PN@?.
1C@ AJGGJRDIB KMJBM<H ?@HJINOM<O@N OCDN KMJ=G@H. 1C@ Q<GP@ JA < Q<MD<=G@ DN =@DIB
DI>M@H@IO@? =T 0.001 < OCJPN<I? ODH@N DI < GJJK. 0PMKMDNDIBGT, OC@ M@NPGO DN IJO 1:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
GMPBU SFTVMU = 0;
// 8F XPVME FYQFDU UIF SFTVMU UP CF 1 BGUFS MPPQJOH 1000
// UJNFT:
XIJMF (SFTVMU < 1) \
SFTVMU += 0.001;
^
// -FU'T WBMJEBUF
JG (SFTVMU == 1) \
XSJUFMO(""T FYQFDUFE: 1");
^ FMTF \
F>A3F;@9 &A;@F )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
55
XSJUFMO("%*''&3&/5: ", SFTVMU);
^
^
%*''&3&/5: 1.00099
B@><PN@ 0.001 ><IIJO =@ M@KM@N@IO@? KM@>DN@GT, OC<O DI<>>PM<>T <AA@>ON OC@ M@NPGO HPGODKG@
ODH@N. 1C@ JPOKPO NPBB@NON OC<O OC@ GJJK C<N =@@I M@K@<O@? 1001 ODH@N.
Comparing floating point values
16.9
4@ C<Q@ N@@I OC@ AJGGJRDIB >JHK<MDNJI JK@M<ODJIN AJM DIO@B@MN: @LP<G OJ (==), IJO @LP<G OJ
(!=), G@NN OC<I (<), BM@<O@M OC<I (>), G@NN OC<I JM @LP<G OJ (<=), <I? BM@<O@M OC<I JM @LP<G OJ
(>=). #GJ<ODIB KJDIO OTK@N C<Q@ H<IT HJM@ >JHK<MDNJI JK@M<OJMN.
0DI>@ OC@ NK@>D<G Q<GP@ .OBO M@KM@N@ION DIQ<GD? AGJ<ODIB KJDIO Q<GP@N, DO H<T IJO =@ >JHK<M@?
RDOC <IT JOC@M Q<GP@. #JM @S<HKG@, DO DN H@<IDIBG@NN OJ <NF RC@OC@M .OBO JM 1 DN BM@<O@M.
#JM OC<O M@<NJI, AGJ<ODIB KJDIO Q<GP@N DIOMJ?P>@ <IJOC@M >JHK<MDNJI >JI>@KO: PIJM?@M@?.
B@DIB PIJM?@M@? H@<IN OC<O <O G@<NO JI@ JA OC@ Q<GP@N DN .OBO.
1C@ AJGGJRDIB O<=G@ DI>GP?@N <GG JA OC@ AGJ<ODIB KJDIO >JHK<MDNJI JK@M<OJMN. AGG JA OC@
JK@M<OJMN <M@ =DI<MT JK@M<OJMN (H@<IDIB OC<O OC@T O<F@ ORJ JK@M<I?N) <I? PN@? <N DI
MFGU == SJHIU. 1C@ >JGPHIN OC<O >JIO<DI GBMTF <I? USVF <M@ OC@ M@NPGON JA OC@ >JHK<MDNJI
JK@M<ODJIN.
1C@ G<NO >JGPHI DI?D><O@N RC@OC@M OC@ JK@M<ODJI DN H@<IDIBAPG DA JI@ JA OC@ JK@M<I?N DN .OBO.
#JM @S<HKG@, @Q@I OCJPBC OC@ M@NPGO JA OC@ @SKM@NNDJI 1.2 < SFBM.OBO DN GBMTF, OC<O M@NPGO
DN H@<IDIBG@NN =@><PN@ JI@ JA OC@ JK@M<I?N DN SFBM.OBO. 1C@ M@NPGO JA OC@ M@Q@MN@
>JHK<MDNJI SFBM.OBO < 1.2 RJPG? KMJ?P>@ GBMTF <N R@GG. 1C@ <=M@QD<ODJI GCN NO<I?N AJM
>78F-:3@6 E;67, DI?D><ODIB OC@ @SKM@NNDJI JI OC@ G@AO-C<I? ND?@ JA @<>C JK@M<OJM.
0QFSBUPS .FBOJOH
*G MIT
JT
HSFBUFS
*G
MIT
JT
MFTT
*G
CPUI
BSF
FRVBM
*G BU
MFBTU
POF JT
.OBO
.FBOJOHGVM
XJUI .OBO
== JT FRVBM UP GBMTF GBMTF USVF GBMTF ZFT
!= JT OPU FRVBM UP USVF USVF GBMTF USVF ZFT
> JT HSFBUFS UIBO USVF GBMTF GBMTF GBMTF OP
>= JT HSFBUFS UIBO PS FRVBM UP USVF GBMTF USVF GBMTF OP
< JT MFTT UIBO GBMTF USVF GBMTF GBMTF OP
<= JT MFTT UIBO PS FRVBM UP GBMTF USVF USVF GBMTF OP
!<>= JT OPU MFTT UIBO, OPU HSFBUFS
UIBO, OPS FRVBM UP
GBMTF GBMTF GBMTF USVF ZFT
<> JT MFTT UIBO PS HSFBUFS UIBO USVF USVF GBMTF GBMTF OP
<>= JT MFTT UIBO, HSFBUFS UIBO, PS
FRVBM UP
USVF USVF USVF GBMTF OP
!<= JT OPU MFTT UIBO OPS FRVBM UP USVF GBMTF GBMTF USVF ZFT
!< JT OPU MFTT UIBO USVF GBMTF USVF USVF ZFT
!>= JT OPU HSFBUFS UIBO OPS FRVBM UP GBMTF USVF GBMTF USVF ZFT
!> JT OPU HSFBUFS UIBO GBMTF USVF USVF USVF ZFT
!<> JT OPU MFTT UIBO OPS HSFBUFS
UIBO
GBMTF GBMTF USVF USVF ZFT
F>A3F;@9 &A;@F )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
56
+JO@ OC<O DO H<F@N N@IN@ OJ PN@ .OBO RDOC <IT JK@M<OJM OC<O >JIO<DIN ;E @AF DI DON H@<IDIB,
<I? OC@ M@NPGO DN <GR<TN USVF. .OBO IJO =@DIB < Q<GD? Q<GP@ H<F@N OC@ M@NPGON JA HJNO
>JHK<MDNJIN OJ C<Q@ @AF DI OC@DM H@<IDIBN.
JT/B/() for .OBO equality comparison 16.9.1
B@><PN@ OC@ M@NPGO RJPG? <GR<TN =@ GBMTF <>>JM?DIB OJ OC@ KM@QDJPN O<=G@, DO DN IJO KJNND=G@
OJ PN@ OC@ == JK@M<OJM OJ ?@O@MHDI@ RC@OC@M OC@ Q<GP@ JA < AGJ<ODIB KJDIO Q<MD<=G@ DN .OBO:
JG (WBSJBCMF == EPVCMF.OBO) \ // j 830/(
// ...
^
#JM OC<O M@<NJI, OC@ JT/B/() API>ODJI AMJH OC@ TUE.NBUI HJ?PG@ HPNO =@ PN@?:
JNQPSU TUE.NBUI;
// ...
JG (JT/B/(WBSJBCMF)) \ // j DPSSFDU
// ...
^
0DHDG<MGT, OJ ?@O@MHDI@ RC@OC@M < Q<GP@ DN @AF .OBO, JI@ HPNO PN@ !JT/B/(), =@><PN@
JOC@MRDN@ OC@ != JK@M<OJM RJPG? <GR<TN KMJ?P>@ USVF.
Exercises
16.10
1. *J?DAT OC@ ><G>PG<OJM AMJH OC@ KM@QDJPN >C<KO@M OJ NPKKJMO AGJ<ODIB KJDIO OTK@N. 1C@ I@R
><G>PG<OJM RJPG? RJMF HJM@ <>>PM<O@GT RDOC OC<O >C<IB@. 4C@I OMTDIB OC@ ><G>PG<OJM, TJP
><I @IO@M AGJ<ODIB KJDIO Q<GP@N DI Q<MDJPN AJMH<ON <N DI 1000, 1.23, <I? 1.23@4.
2. 4MDO@ < KMJBM<H OC<O M@<?N 5 AGJ<ODIB KJDIO Q<GP@N AMJH OC@ DIKPO. *<F@ OC@ KMJBM<H KMDIO
ADMNO ORD>@ JA OCJN@ Q<GP@N <I? OC@I JI@ ADAOC JA OCJN@ Q<GP@N.
1CDN @S@M>DN@ DN < KM@K<M<ODJI AJM OC@ <MM<T >JI>@KO JA OC@ I@SO >C<KO@M. &A TJP RMDO@
OCDN KMJBM<H RDOC RC<O TJP C<Q@ N@@I NJ A<M, TJP RDGG PI?@MNO<I? <MM<TN @<ND@M <I?
<KKM@>D<O@ OC@H HJM@.
... F:7 EA>GF;A@E
F>A3F;@9 &A;@F )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
57
Arrays 17
4@ C<Q@ ?@ADI@? ADQ@ Q<MD<=G@N DI JI@ JA OC@ @S@M>DN@N JA OC@ G<NO >C<KO@M, <I? PN@? OC@H DI
>@MO<DI ><G>PG<ODJIN. 1C@ ?@ADIDODJIN JA OCJN@ Q<MD<=G@N R@M@ OC@ AJGGJRDIB:
EPVCMF WBMVF@1;
EPVCMF WBMVF@2;
EPVCMF WBMVF@3;
EPVCMF WBMVF@4;
EPVCMF WBMVF@5;
1CDN H@OCJ? JA ?@ADIDIB Q<MD<=G@N DI?DQD?P<GGT ?J@N IJO N><G@ OJ ><N@N RC@M@ @Q@I HJM@
Q<MD<=G@N <M@ I@@?@?. &H<BDI@ I@@?DIB < OCJPN<I? Q<GP@N; DO DN <GHJNO DHKJNND=G@ OJ ?@ADI@ <
OCJPN<I? Q<MD<=G@N AMJH WBMVF@1 OJ WBMVF@1000.
AMM<TN <M@ PN@APG DI NP>C ><N@N: 1C@ <MM<T A@<OPM@ <GGJRN OC@ ?@ADIDODJI JA H<IT Q<GP@N
OJB@OC@M. AMM<TN <M@ <GNJ OC@ HJNO >JHHJI ?<O< NOMP>OPM@ RC@I HPGODKG@ Q<GP@N <M@ PN@?
OJB@OC@M <N < >JGG@>ODJI.
1CDN >C<KO@M >JQ@MN JIGT NJH@ JA OC@ A@<OPM@N JA <MM<TN. *JM@ A@<OPM@N RDGG =@ DIOMJ?P>@? DI <
G<O@M >C<KO@M.
Definition
17.1
1C@ ?@ADIDODJI JA <MM<TN DN Q@MT NDHDG<M OJ OC@ ?@ADIDODJI JA Q<MD<=G@N. 1C@ JIGT ?DAA@M@I>@ DN
OC<O OC@ IPH=@M JA Q<MD<=G@N OC<O <M@ =@DIB ?@ADI@? <O OC@ N<H@ ODH@ DN NK@>DAD@? DI NLP<M@
=M<>F@ON. 4@ ><I >JIOM<NO OC@ ORJ ?@ADIDODJIN <N AJGGJRN:
JOU TJOHMF7BSJBCMF;
JOU<10> BSSBZ0G5FO7BSJBCMFT;
1C@ ADMNO GDI@ <=JQ@ DN OC@ ?@ADIDODJI JA < NDIBG@ Q<MD<=G@, EPNO GDF@ OC@ Q<MD<=G@N OC<O R@ C<Q@
?@ADI@? NJ A<M. 1C@ N@>JI? GDI@ DN OC@ ?@ADIDODJI JA <I <MM<T >JINDNODIB JA 10 Q<MD<=G@N.
A>>JM?DIBGT, OC@ @LPDQ<G@IO JA OC@ ADQ@ N@K<M<O@ Q<MD<=G@N <=JQ@ ><I =@ ?@ADI@? <N <I <MM<T JA
ADQ@ Q<MD<=G@N PNDIB OC@ AJGGJRDIB NTIO<S:
EPVCMF<5> WBMVFT;
1C<O ?@ADIDODJI ><I =@ M@<? <N 5 6AG4>7 H3>G7E. +JO@ OC<O & C<Q@ >CJN@I OC@ I<H@ JA OC@ <MM<T
<N KGPM<G OJ <QJD? >JIAPNDIB DO RDOC < NDIBG@ Q<MD<=G@.
&I NPHH<MT, OC@ ?@ADIDODJI JA <I <MM<T >JINDNON JA OC@ OTK@ JA OC@ Q<MD<=G@N, OC@ IPH=@M JA
Q<MD<=G@N, <I? OC@ I<H@ JA OC@ <MM<T:
type_name<variable_count> array_name;
1C@ OTK@ JA OC@ Q<MD<=G@N ><I <GNJ =@ < PN@M-?@ADI@? OTK@. (4@ RDGG N@@ PN@M-?@ADI@? OTK@N
G<O@M.) #JM @S<HKG@:
// "O BSSBZ UIBU IPMET UIF XFBUIFS JOGPSNBUJPO PG BMM
// DJUJFT. )FSF, UIF CPPM WBMVFT NBZ NFBO
// GBMTF: PWFSDBTU
// USVF : TVOOZ
CPPM<DJUZ$PVOU> XFBUIFS$POEJUJPOT;
// "O BSSBZ UIBU IPMET UIF XFJHIUT PG B IVOESFE CPYFT
EPVCMF<100> CPY8FJHIUT;
ADD3KE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
58
// *OGPSNBUJPO BCPVU UIF TUVEFOUT PG B TDIPPM
4UVEFOU*OGPSNBUJPO<TUVEFOU$PVOU> TUVEFOU*OGPSNBUJPOT;
Containers and elements
17.2
!<O< NOMP>OPM@N OC<O =MDIB @G@H@ION JA < >@MO<DI OTK@ OJB@OC@M <M@ ><GG@? 5A@F3;@7DE.
A>>JM?DIB OJ OCDN ?@ADIDODJI, <MM<TN <M@ >JIO<DI@MN. #JM @S<HKG@, <I <MM<T OC<O CJG?N OC@ <DM
O@HK@M<OPM@N JA OC@ ?<TN DI 'PGT ><I =MDIB 31 EPVCMF Q<MD<=G@N OJB@OC@M <I? AJMH 3 5A@F3;@7D
A8 7>7?7@FE A8 FKB7 double.
1C@ Q<MD<=G@N JA < >JIO<DI@M <M@ ><GG@? 7>7?7@FE. 1C@ IPH=@M JA @G@H@ION JA <I <MM<T DN
><GG@? OC@ >7@9F: JA OC@ <MM<T.
Accessing the elements
17.3
&I JM?@M OJ ?DAA@M@IOD<O@ OC@ Q<MD<=G@N DI OC@ @S@M>DN@ JA OC@ KM@QDJPN >C<KO@M, R@ C<? OJ
<KK@I? <I PI?@MN>JM@ <I? < IPH=@M OJ OC@DM I<H@N <N DI WBMVF@1. 1CDN DN IJO KJNND=G@ IJM
I@>@NN<MT RC@I OC@ Q<MD<=G@N <M@ ?@ADI@? OJB@OC@M <N < NDIBG@ <MM<T PI?@M < NDIBG@ I<H@.
&INO@<?, OC@ @G@H@ION <M@ <>>@NN@? =T NK@>DATDIB OC@ 7>7?7@F @G?47D RDOCDI NLP<M@ =M<>F@ON:
WBMVFT<0>
1C<O @SKM@NNDJI ><I =@ M@<? <N F:7 7>7?7@F I;F: F:7 @G?47D 0 A8 F:7 3DD3K @3?76 H3>G7E. &I
JOC@M RJM?N, DINO@<? JA OTKDIB WBMVF@1 JI@ HPNO OTK@ WBMVFT<0> RDOC <MM<TN.
1C@M@ <M@ ORJ DHKJMO<IO KJDION RJMOC NOM@NNDIB C@M@:
t The numbers start with zero: AGOCJPBC CPH<IN <NNDBI IPH=@MN OJ DO@HN NO<MODIB RDOC
1, OC@ IPH=@MN DI <MM<TN NO<MO RDOC 0. 1C@ Q<GP@N OC<O R@ C<Q@ IPH=@M@? <N 1, 2, 3, 4, <I?
5 =@AJM@ <M@ IPH=@M@? <N 0, 1, 2, 3, <I? 4 DI OC@ <MM<T. 1CDN Q<MD<ODJI DN < >JHHJI ><PN@
JA KMJBM<HHDIB @MMJMN.
t Two different uses of the <> characters: !JI'O >JIAPN@ OC@ ORJ N@K<M<O@ PN@N JA OC@ <>
>C<M<>O@MN. 4C@I ?@ADIDIB <MM<TN, OC@ <> >C<M<>O@MN <M@ RMDOO@I <AO@M OC@ OTK@ JA OC@
@G@H@ION <I? NK@>DAT OC@ IPH=@M JA @G@H@ION. 4C@I <>>@NNDIB @G@H@ION, OC@ <>
>C<M<>O@MN <M@ RMDOO@I <AO@M OC@ I<H@ JA OC@ <MM<T <I? NK@>DAT OC@ IPH=@M JA OC@ @G@H@IO
OC<O DN =@DIB <>>@NN@?:
// 5IJT JT B EFGJOJUJPO. *U EFGJOFT BO BSSBZ UIBU DPOTJTUT
// PG 12 FMFNFOUT. 5IJT BSSBZ JT VTFE UP IPME UIF OVNCFS
// PG EBZT JO FBDI NPOUI.
JOU<12> NPOUI%BZT;
// 5IJT JT BO BDDFTT. *U BDDFTTFT UIF FMFNFOU UIBU
// DPSSFTQPOET UP %FDFNCFS BOE TFUT JUT WBMVF UP 31.
NPOUI%BZT<11> = 31;
// 5IJT JT BOPUIFS BDDFTT. *U BDDFTTFT UIF FMFNFOU UIBU
// DPSSFTQPOET UP +BOVBSZ, UIF WBMVF PG XIJDI JT QBTTFE UP
// XSJUFMO.
XSJUFMO("+BOVBSZ IBT ", NPOUI%BZT<0>, " EBZT.");
Reminder: 1C@ @G@H@IO IPH=@MN JA '<IP<MT <I? !@>@H=@M <M@ 0 <I? 11
M@NK@>ODQ@GT; IJO 1 <I? 12.
ADD3KE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
59
Index
17.4
1C@ IPH=@M JA <I @G@H@IO DN ><GG@? DON ;@67J <I? OC@ <>O JA <>>@NNDIB <I @G@H@IO DN ><GG@?
;@67J;@9.
AI DI?@S I@@? IJO =@ < >JINO<IO Q<GP@; OC@ Q<GP@ JA < Q<MD<=G@ ><I <GNJ =@ PN@? <N <I DI?@S,
H<FDIB <MM<TN @Q@I HJM@ PN@APG. #JM @S<HKG@, OC@ HJIOC DN ?@O@MHDI@? =T OC@ Q<GP@ JA OC@
NPOUI*OEFY Q<MD<=G@ =@GJR:
XSJUFMO("5IJT NPOUI IBT ", NPOUI%BZT<NPOUI*OEFY>, " EBZT.");
4C@I OC@ Q<GP@ JA NPOUI*OEFY DN 2, OC@ @SKM@NNDJI <=JQ@ RJPG? KMDIO OC@ Q<GP@ JA
NPOUI%BZT<2>, OC@ IPH=@M JA ?<TN DI *<M>C.
,IGT OC@ DI?@S Q<GP@N =@OR@@I U@MJ <I? JI@ G@NN OC<I OC@ G@IBOC JA OC@ <MM<T <M@ Q<GD?. #JM
@S<HKG@, OC@ Q<GD? DI?@S@N JA < OCM@@-@G@H@IO <MM<T <M@ 0, 1, <I? 2. A>>@NNDIB <I <MM<T RDOC
<I DIQ<GD? DI?@S ><PN@N OC@ KMJBM<H OJ =@ O@MHDI<O@? RDOC <I @MMJM.
AMM<TN <M@ >JIO<DI@MN RC@M@ OC@ @G@H@ION <M@ KG<>@? ND?@ =T ND?@ DI OC@ >JHKPO@M'N H@HJMT.
#JM @S<HKG@, OC@ @G@H@ION JA OC@ <MM<T CJG?DIB OC@ IPH=@M JA ?<TN DI @<>C HJIOC ><I =@
NCJRI GDF@ OC@ AJGGJRDIB (<NNPHDIB < T@<M RC@I #@=MP<MT C<N 28 ?<TN):
JOEFYFT l 0 1 2 3 4 5 6 7 8 9 10 11
FMFNFOUT l ] 31 ] 28 ] 31 ] 30 ] 31 ] 30 ] 31 ] 31 ] 30 ] 31 ] 30 ] 31 ]
1C@ @G@H@IO <O DI?@S 0 C<N OC@ Q<GP@ 31 (IPH=@M JA ?<TN DI '<IP<MT); OC@ @G@H@IO <O DI?@S 1
C<N OC@ Q<GP@ JA 28 (IPH=@M JA ?<TN DI #@=MP<MT), @O>.
Fixed-length arrays vs. dynamic arrays
17.5
4C@I OC@ G@IBOC JA <I <MM<T DN NK@>DAD@? RC@I OC@ KMJBM<H DN RMDOO@I, OC<O <MM<T DN < 8;J76-
>7@9F: 3DD3K. 4C@I OC@ G@IBOC ><I >C<IB@ ?PMDIB OC@ @S@>PODJI JA OC@ KMJBM<H, OC<O <MM<T DN
< 6K@3?;5 3DD3K.
BJOC JA OC@ <MM<TN OC<O R@ C<Q@ ?@ADI@? <=JQ@ <M@ ADS@?-G@IBOC <MM<TN =@><PN@ OC@DM @G@H@IO
>JPION <M@ NK@>DAD@? <N 5 <I? 12 <O OC@ ODH@ RC@I OC@ KMJBM<H DN RMDOO@I. 1C@ G@IBOCN JA
OCJN@ <MM<TN ><IIJO =@ >C<IB@? ?PMDIB OC@ @S@>PODJI JA OC@ KMJBM<H. 1J >C<IB@ OC@DM
G@IBOCN, OC@ NJPM>@ >J?@ HPNO =@ HJ?DAD@? <I? OC@ KMJBM<H HPNO =@ M@>JHKDG@?.
!@ADIDIB ?TI<HD> <MM<TN DN NDHKG@M OC<I ?@ADIDIB ADS@?-G@IBOC <MM<TN =@><PN@ JHDOODIB OC@
G@IBOC H<F@N < ?TI<HD> <MM<T:
JOU<> EZOBNJD"SSBZ;
1C@ G@IBOC JA NP>C <I <MM<T ><I DI>M@<N@ JM ?@>M@<N@ ?PMDIB OC@ @S@>PODJI JA OC@ KMJBM<H.
Array properties
17.6
AN R@GG <N < OTK@, <I <MM<T C<N NJH@ KMJK@MOD@N <N R@GG. 4@ RDGG N@@ JIGT OCM@@ JA OC@H C@M@.
+JO@ OC<O OC@ .MFOHUI KMJK@MOT JA ?TI<HD> <MM<TN DN HJM@ ><K<=G@ OC<I OC<O JA ADS@?-G@IBOC
<MM<TN:
t .MFOHUI: /@OPMIN OC@ IPH=@M JA @G@H@ION JA OC@ <MM<T:
XSJUFMO("5IF BSSBZ IBT ", BSSBZ.MFOHUI, " FMFNFOUT.");
ADD3KE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
60
A??DODJI<GGT, OC@ G@IBOC JA ?TI<HD> <MM<TN ><I =@ >C<IB@? =T <NNDBIDIB < Q<GP@ OJ OCDN
KMJK@MOT:
JOU<> BSSBZ; // JOJUJBMMZ FNQUZ
BSSBZ.MFOHUI = 5; // OPX IBT 5 FMFNFOUT
t .TPSU: 0JMON OC@ @G@H@ION (DI OC@ ><N@ JA DIO@B@MN, AMJH OC@ NH<GG@NO Q<GP@ OJ OC@ BM@<O@NO
Q<GP@):
BSSBZ.TPSU;
t .SFWFSTF: 0JMON OC@ @G@H@ION DI M@Q@MN@ JM?@M:
BSSBZ.SFWFSTF;
+JR G@O'N M@QDNDO OC@ @S@M>DN@ RDOC OC@ ADQ@ Q<GP@N <I? RMDO@ DO <B<DI =T PNDIB <I <MM<T:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
// 5IJT WBSJBCMF JT VTFE BT B MPPQ DPVOUFS
JOU DPVOUFS;
// 5IF EFGJOJUJPO PG B GJYFE-MFOHUI BSSBZ PG GJWF
// FMFNFOUT PG UZQF EPVCMF
EPVCMF<5> WBMVFT;
// 3FBEJOH UIF WBMVFT JO B MPPQ
XIJMF (DPVOUFS < WBMVFT.MFOHUI) \
XSJUF("7BMVF ", DPVOUFS + 1, ": ");
SFBEG(" %T", &WBMVFT<DPVOUFS>);
++DPVOUFS;
^
XSJUFMO("5XJDF UIF WBMVFT:");
DPVOUFS = 0;
XIJMF (DPVOUFS < WBMVFT.MFOHUI) \
XSJUFMO(WBMVFT<DPVOUFS> * 2);
++DPVOUFS;
^
// 5IF MPPQ UIBU DBMDVMBUFT UIF GJGUIT PG UIF WBMVFT XPVME
// CF XSJUUFO TJNJMBSMZ
^
Observations: 1C@ Q<GP@ JA DPVOUFS ?@O@MHDI@N CJR H<IT ODH@N OC@ GJJKN <M@ M@K@<O@?
(DO@M<O@?). &O@M<ODIB OC@ GJJK RCDG@ DON Q<GP@ DN G@NN OC<I BSSBZ.MFOHUI @INPM@N OC<O OC@ GJJKN
<M@ @S@>PO@? JI>@ K@M @G@H@IO. AN OC@ Q<GP@ JA OC<O Q<MD<=G@ DN DI>M@H@IO@? <O OC@ @I? JA
@<>C DO@M<ODJI, OC@ WBMVFT<DPVOUFS> @SKM@NNDJI M@A@MN OJ OC@ @G@H@ION JA OC@ <MM<T JI@ =T
JI@: WBMVFT<0>, WBMVFT<1>, @O>.
1J N@@ CJR OCDN KMJBM<H DN =@OO@M OC<I OC@ KM@QDJPN JI@, DH<BDI@ I@@?DIB OJ M@<? 20 Q<GP@N.
1C@ KMJBM<H <=JQ@ RJPG? M@LPDM@ < NDIBG@ >C<IB@: M@KG<>DIB 5 RDOC 20. ,I OC@ JOC@M C<I?,
OC@ KMJBM<H OC<O ?D? IJO PN@ <I <MM<T RJPG? C<Q@ OJ C<Q@ 15 HJM@ Q<MD<=G@ ?@ADIDODJIN <I?
OC@ GDI@N OC<O OC@T C<Q@ =@@I PN@? DI RJPG? C<Q@ OJ =@ M@K@<O@? 15 HJM@ ODH@N.
Initializing the elements
17.7
)DF@ @Q@MT Q<MD<=G@ DI !, OC@ @G@H@ION JA <MM<TN <M@ <POJH<OD><GGT DIDOD<GDU@?. 1C@ DIDOD<G Q<GP@
JA OC@ @G@H@ION ?@K@I?N JI OC@ OTK@ JA OC@ @G@H@ION: 0 AJM JOU, EPVCMF.OBO AJM EPVCMF, @O>.
ADD3KE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
61
AGG JA OC@ @G@H@ION JA OC@ WBMVFT <MM<T <=JQ@ <M@ DIDOD<GDU@? OJ EPVCMF.OBO:
EPVCMF<5> WBMVFT; // FMFNFOUT BSF BMM EPVCMF.OBO
,=QDJPNGT, OC@ Q<GP@N JA OC@ @G@H@ION ><I =@ >C<IB@? G<O@M DI OC@ KMJBM<H. 4@ C<Q@ <GM@<?T
N@@I OCDN <=JQ@ RC@I <NNDBIDIB OJ <I @G@H@IO JA <I <MM<T:
NPOUI%BZT<11> = 31;
AI? <GNJ RC@I M@<?DIB < Q<GP@ AMJH OC@ DIKPO:
SFBEG(" %T", &WBMVFT<DPVOUFS>);
0JH@ODH@N OC@ ?@NDM@? Q<GP@N JA OC@ @G@H@ION <M@ FIJRI <O OC@ ODH@ RC@I OC@ <MM<T DN
?@ADI@?. &I NP>C ><N@N, OC@ DIDOD<G Q<GP@N JA OC@ @G@H@ION ><I =@ NK@>DAD@? JI OC@ MDBCO-C<I?
ND?@ JA OC@ <NNDBIH@IO JK@M<OJM RDOCDI NLP<M@ =M<>F@ON. )@O'N N@@ OCDN DI < KMJBM<H OC<O
M@<?N OC@ IPH=@M JA OC@ HJIOC AMJH OC@ PN@M, <I? KMDION OC@ IPH=@M JA ?<TN DI OC<O HJIOC:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
// "TTVNJOH UIBU 'FCSVBSZ IBT 28 EBZT
JOU<12> NPOUI%BZT =
< 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 >;
XSJUF("1MFBTF FOUFS UIF OVNCFS PG UIF NPOUI: ");
JOU NPOUI/VNCFS;
SFBEG(" %T", &NPOUI/VNCFS);
JOU JOEFY = NPOUI/VNCFS - 1;
XSJUFMO(".POUI ", NPOUI/VNCFS, " IBT ",
NPOUI%BZT<JOEFY>, " EBZT.");
^
AN TJP N@@, OC@ NPOUI%BZT <MM<T DN ?@ADI@? <I? DIDOD<GDU@? <O OC@ N<H@ ODH@. AGNJ IJO@ OC<O OC@
IPH=@M JA OC@ HJIOC, RCD>C DN DI OC@ M<IB@ 1-12 DN >JIQ@MO@? OJ < Q<GD? <MM<T DI?@S DI OC@
M<IB@ 0-11. AIT Q<GP@ OC<O DN @IO@M@? JPOND?@ JA OC@ 1-12 M<IB@ RJPG? ><PN@ OC@ KMJBM<H OJ
=@ O@MHDI<O@? RDOC <I @MMJM.
4C@I DIDOD<GDUDIB <MM<TN, DO DN KJNND=G@ OJ PN@ < NDIBG@ Q<GP@ JI OC@ MDBCO-C<I? ND?@. &I OC<O
><N@ <GG JA OC@ @G@H@ION JA OC@ <MM<T <M@ DIDOD<GDU@? OJ OC<O Q<GP@:
JOU<10> BMM0OFT = 1; // "MM PG UIF FMFNFOUT BSF TFU UP 1
Basic array operations
17.8
AMM<TN KMJQD?@ >JIQ@ID@IO JK@M<ODJIN OC<O <KKGT OJ <GG JA OC@DM @G@H@ION.
Copying fixed-length arrays: 1C@ <NNDBIH@IO JK@M<OJM >JKD@N <GG JA OC@ @G@H@ION AMJH OC@
MDBCO-C<I? ND?@ OJ OC@ G@AO-C<I? ND?@:
JOU<5> TPVSDF = < 10, 20, 30, 40, 50 >;
JOU<5> EFTUJOBUJPO;
EFTUJOBUJPO = TPVSDF;
Note: ):7 ?73@;@9 A8 F:7 3EE;9@?7@F AB7D3F;A@ ;E 5A?B>7F7>K 6;887D7@F 8AD 6K@3?;5 3DD3KE.
,7 I;>> E77 F:;E ;@ 3 >3F7D 5:3BF7D.
ADD3KE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
62
Adding elements to dynamic arrays: 1C@ _= JK@M<OJM <??N < I@R @G@H@IO JM < I@R <MM<T
OJ OC@ @I? JA < ?TI<HD> <MM<T:
JOU<> BSSBZ; // FNQUZ
BSSBZ _= 7; // IBT B TJOHMF FMFNFOU
BSSBZ _= 360; // IBT UXP FMFNFOUT
BSSBZ _= < 30, 40 >; // IBT GPVS FMFNFOUT
&O DN IJO KJNND=G@ OJ <?? @G@H@ION OJ ADS@?-G@IBOC <MM<TN:
JOU<10> BSSBZ;
BSSBZ _= 7; // j DPNQJMBUJPO &3303
Combining arrays: 1C@ _ JK@M<OJM >M@<O@N < I@R <MM<T =T >JH=DIDIB ORJ <MM<TN. &ON _=
>JPIO@MK<MO >JH=DI@N OC@ ORJ <MM<TN <I? <NNDBIN OC@ M@NPGO =<>F OJ OC@ G@AO-C<I? ND?@ <MM<T:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU<10> GJSTU = 1;
JOU<10> TFDPOE = 2;
JOU<> SFTVMU;
SFTVMU = GJSTU _ TFDPOE;
XSJUFMO(SFTVMU.MFOHUI); // QSJOUT 20
SFTVMU _= GJSTU;
XSJUFMO(SFTVMU.MFOHUI); // QSJOUT 30
^
1C@ _= JK@M<OJM ><IIJO =@ PN@? RC@I OC@ G@AO-C<I? ND?@ <MM<T DN < ADS@?-G@IBOC <MM<T:
JOU<20> SFTVMU;
// ...
SFTVMU _= GJSTU; // j DPNQJMBUJPO &3303
&A OC@ G@AO-C<I? ND?@ <MM<T ?J@NI'O C<Q@ MJJH AJM OC@ M@NPGO, OC@ KMJBM<H DN O@MHDI<O@? RDOC <I
@MMJM ?PMDIB <NNDBIH@IO:
JOU<10> GJSTU = 1;
JOU<10> TFDPOE = 2;
JOU<21> SFTVMU;
SFTVMU = GJSTU _ TFDPOE;
PCKFDU.&YDFQUJPO: MFOHUIT EPO'U NBUDI GPS BSSBZ DPQZ
Exercises
17.9
1. 4MDO@ < KMJBM<H OC<O <NFN OC@ PN@M CJR H<IT Q<GP@N RDGG =@ @IO@M@? <I? OC@I M@<?N <GG JA
OC@H. %<Q@ OC@ KMJBM<H NJMO OC@ @G@H@ION PNDIB .TPSU <I? .SFWFSTF.
2. 4MDO@ < KMJBM<H OC<O M@<?N IPH=@MN AMJH OC@ DIKPO, <I? KMDION OC@ J?? <I? @Q@I JI@N
N@K<M<O@GT =PO DI JM?@M. 1M@<O OC@ Q<GP@ -1 NK@>D<GGT OJ ?@O@MHDI@ OC@ @I? JA OC@ IPH=@MN;
?J IJO KMJ>@NN OC<O Q<GP@.
#JM @S<HKG@, RC@I OC@ AJGGJRDIB IPH=@MN <M@ @IO@M@?,
1 4 7 2 3 8 11 -1
ADD3KE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
63
C<Q@ OC@ KMJBM<H KMDIO OC@ AJGGJRDIB:
1 3 7 11 2 4 8
Hint: 6JP H<T R<IO OJ KPO OC@ @G@H@ION DI N@K<M<O@ <MM<TN. 6JP ><I ?@O@MHDI@
RC@OC@M < IPH=@M DN J?? JM @Q@I PNDIB OC@ % (M@H<DI?@M) JK@M<OJM.
3. 1C@ AJGGJRDIB DN < KMJBM<H OC<O ?J@N IJO RJMF <N @SK@>O@?. 1C@ KMJBM<H DN RMDOO@I OJ
M@<? ADQ@ IPH=@MN AMJH OC@ DIKPO <I? OJ KG<>@ OC@ NLP<M@N JA OCJN@ IPH=@MN DIOJ <I <MM<T.
1C@ KMJBM<H OC@I <OO@HKON OJ KMDIO OC@ NLP<M@N OJ OC@ JPOKPO. &INO@<?, OC@ KMJBM<H
O@MHDI<O@N RDOC <I @MMJM.
#DS OC@ =PBN JA OCDN KMJBM<H <I? H<F@ DO RJMF <N @SK@>O@?:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU<5> TRVBSFT;
XSJUFMO("1MFBTF FOUFS 5 OVNCFST");
JOU J = 0;
XIJMF (J <= 5) \
JOU OVNCFS;
XSJUF("/VNCFS ", J + 1, ": ");
SFBEG(" %T", &OVNCFS);
TRVBSFT<J> = OVNCFS * OVNCFS;
++J;
^
XSJUFMO("=== 5IF TRVBSFT PG UIF OVNCFST ===");
XIJMF (J <= TRVBSFT.MFOHUI)
\
XSJUF(TRVBSFT<J>, " ");
++J;
^
XSJUFMO();
^
... F:7 EA>GF;A@E
ADD3KE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
64
Characters 18
C<M<>O@MN <M@ OC@ =PDG?DIB =GJ>FN JA NOMDIBN. AIT NTH=JG JA < RMDODIB NTNO@H DN ><GG@? <
>C<M<>O@M: G@OO@MN JA <GKC<=@ON, IPH@M<GN, KPI>OP<ODJI H<MFN, OC@ NK<>@ >C<M<>O@M, @O>.
JIAPNDIBGT, OC@ =PDG?DIB =GJ>FN JA >C<M<>O@MN OC@HN@GQ@N <M@ ><GG@? >C<M<>O@MN <N R@GG.
AMM<TN JA >C<M<>O@MN H<F@ PK EFD;@9E. 4@ C<Q@ N@@I <MM<TN DI OC@ KM@QDJPN >C<KO@M; NOMDIBN
RDGG =@ >JQ@M@? ORJ >C<KO@MN G<O@M.
)DF@ <IT JOC@M ?<O<, >C<M<>O@MN <M@ <GNJ M@KM@N@IO@? <N DIO@B@M Q<GP@N OC<O <M@ H<?@ PK JA
=DON. #JM @S<HKG@, OC@ DIO@B@M Q<GP@ JA OC@ GJR@M><N@ B DN 97 <I? OC@ DIO@B@M Q<GP@ JA OC@
IPH@M<G 1 DN 49. 1C@N@ Q<GP@N C<Q@ =@@I <NNDBI@? H@M@GT =T >JIQ@IODJIN RC@I OC@ A0&&
O<=G@ C<N =@@I ?@NDBI@?.
&I HJNO G<IBP<B@N, >C<M<>O@MN <M@ M@KM@N@IO@? =T OC@ DIBS OTK@, RCD>C ><I CJG? JIGT 256
?DNODI>O Q<GP@N. &A TJP <M@ A<HDGD<M RDOC OC@ DIBS OTK@ AMJH JOC@M G<IBP<B@N, TJP H<T <GM@<?T
FIJR OC<O DO DN IJO G<MB@ @IJPBC OJ NPKKJMO OC@ NTH=JGN JA H<IT RMDODIB NTNO@HN. B@AJM@
B@OODIB OJ OC@ OCM@@ ?DNODI>O >C<M<>O@M OTK@N JA !, G@O'N ADMNO O<F@ < GJJF <O OC@ CDNOJMT JA
>C<M<>O@MN DI >JHKPO@M NTNO@HN.
History
18.1
ASCII Table 18.1.1
1C@ A0&& O<=G@ R<N ?@NDBI@? <O < ODH@ RC@I >JHKPO@M C<M?R<M@ R<N Q@MT GDHDO@? >JHK<M@?
OJ HJ?@MI NTNO@HN. %<QDIB =@@I =<N@? JI 7 =DON, OC@ A0&& O<=G@ ><I C<Q@ 128 ?DNODI>O >J?@
Q<GP@N. 1C<O DN NPAAD>D@IO OJ M@KM@N@IO >C<M<>O@MN GDF@ OC@ GJR@M><N@ <I? PKK@M><N@ Q@MNDJIN
JA OC@ 26 G@OO@MN JA OC@ =<ND> )<ODI <GKC<=@O, IPH@M<GN, >JHHJIGT PN@? KPI>OP<ODJI H<MFN,
<I? NJH@ O@MHDI<G (>JINJG@) >JIOMJG >C<M<>O@MN.
AN <I @S<HKG@, OC@ A0&& >J?@N JA OC@ >C<M<>O@MN JA OC@ NOMDIB "C@GGJ" <M@ OC@ AJGGJRDIB (OC@
>JHH<N <M@ DIN@MO@? EPNO OJ H<F@ DO @<ND@M OJ M@<?):
104, 101, 108, 108, 111
"Q@MT >J?@ <=JQ@ M@KM@N@ION < NDIBG@ G@OO@M JA "C@GGJ". #JM @S<HKG@, OC@M@ <M@ ORJ 108 Q<GP@N
AJM OC@ ORJ 'G' G@OO@MN. (Note: ):7 35FG3> AD67D A8 F:7E7 5:3D35F7DE 67B7@6 A@ F:7 B>3F8AD? 3@6
7H7@ F:7 6A5G?7@F F:3F F:7E7 H3>G7E 3D7 3 B3DF A8. ):7 5A67E 34AH7 3D7 ;@ F:7 AD67D F:3F F:7K
3BB73D ;@ F:7 EFD;@9.)
1C@ >J?@N JA OC@ A0&& O<=G@ C<Q@ G<O@M =@@I DI>M@<N@? OJ 8 =DON OJ =@>JH@ OC@ "SO@I?@?
A0&& O<=G@. "SO@I?@? A0&& O<=G@ C<N 256 ?DNODI>O >J?@N.
IBM Code Pages 18.1.2
&B* JMKJM<ODJI C<N ?@ADI@? < N@O JA O<=G@N, @<>C JI@ JA RCD>C <NNDBI OC@ >J?@N JA OC@
"SO@I?@? A0&& O<=G@ AMJH 128 OJ 255 OJ JI@ JM HJM@ RMDODIB NTNO@HN. 1C@N@ >J?@ O<=G@N
<GGJR@? NPKKJMODIB OC@ G@OO@MN JA H<IT HJM@ <GKC<=@ON. #JM @S<HKG@, OC@ NK@>D<G G@OO@MN JA
OC@ 1PMFDNC <GKC<=@O <M@ < K<MO JA &B*'N >J?@ K<B@ 857.
!@NKDO@ =@DIB HP>C PN@APG OC<I A0&&, >J?@ K<B@N C<Q@ NJH@ KMJ=G@HN <I? GDHDO<ODJIN: &I
JM?@M OJ ?DNKG<T O@SO >JMM@>OGT, DO HPNO =@ FIJRI RC<O >J?@ K<B@ < BDQ@I O@SO C<N JMDBDI<GGT
=@@I RMDOO@I DI. 1CDN DN =@><PN@ OC@ N<H@ >J?@ >JMM@NKJI?N OJ < ?DAA@M@IO >C<M<>O@M DI HJNO
JOC@M O<=G@N. #JM @S<HKG@, OC@ >J?@ OC<O M@KM@N@ION a DI O<=G@ 857 >JMM@NKJI?N OJ X DI O<=G@
437.
C:3D35F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
65
AIJOC@M KMJ=G@H DN OC@ GDHDO<ODJI JA OC@ IPH=@M JA <GKC<=@ON OC<O ><I =@ NPKKJMO@? DI <
NDIBG@ ?J>PH@IO. A??DODJI<GGT, <GKC<=@ON OC<O C<Q@ HJM@ OC<I 128 IJI-A0&& >C<M<>O@MN
><IIJO =@ NPKKJMO@? =T <I &B* O<=G@.
ISO/IEC 8859 Code Pages 18.1.3
1C@N@ >J?@ K<B@N <M@ < M@NPGO JA DIO@MI<ODJI<G NO<I?<M?DU<ODJI @AAJMON. 1C@T <M@ NDHDG<M OJ
&B*'N >J?@ K<B@N DI CJR OC@T <NNDBI >J?@N OJ >C<M<>O@MN. AN <I @S<HKG@, OC@ NK@>D<G G@OO@MN
JA OC@ 1PMFDNC <GKC<=@O <KK@<M DI >J?@ K<B@ 8859-9. 1C@N@ O<=G@N C<Q@ OC@ N<H@ KMJ=G@HN
<I? GDHDO<ODJIN JA &B*'N O<=G@N. #JM @S<HKG@, OC@ !PO>C ?DBM<KC e ?J@N IJO <KK@<M DI <IT JA
OC@N@ O<=G@N.
Unicode 18.1.4
2ID>J?@ NJGQ@N <GG JA OC@ KMJ=G@HN <I? GDHDO<ODJIN JA KM@QDJPN NJGPODJIN. 2ID>J?@ DI>GP?@N
HJM@ OC<I 100 OCJPN<I? >C<M<>O@MN <I? NTH=JGN JA OC@ RMDODIB NTNO@HN JA H<IT CPH<I
G<IBP<B@N, >PMM@IO <I? JG?. (+@R JI@N <M@ >JINO<IGT PI?@M M@QD@R AJM <??DODJI OJ OC@ O<=G@.)
"<>C JA OC@N@ >C<M<>O@MN C<N < PIDLP@ >J?@. !J>PH@ION OC<O <M@ @I>J?@? DI 2ID>J?@ ><I
DI>GP?@ <GG JA OC@ >C<M<>O@MN JA N@K<M<O@ RMDODIB NTNO@HN <O OC@ N<H@ ODH@ RDOCJPO <IT
>JIAPNDJI JM GDHDO<ODJI.
Unicode encodings
18.2
2ID>J?@ <NNDBIN < PIDLP@ >J?@ AJM @<>C >C<M<>O@M. 0DI>@ OC@M@ <M@ HJM@ 2ID>J?@ >C<M<>O@MN
OC<I <I 8-=DO Q<GP@ ><I CJG?, NJH@ >C<M<>O@MN HPNO =@ M@KM@N@IO@? =T <O G@<NO ORJ 8-=DO
Q<GP@N. #JM @S<HKG@, OC@ 2ID>J?@ >C<M<>O@M >J?@ JA a (286) DN BM@<O@M OC<I OC@ H<SDHPH
Q<GP@ OC<O <I 8-=DO OTK@ ><I CJG? (255).
Note: ! I;>> 3D4;FD3D;>K GE7 1 3@6 30 3E F:7 H3>G7E A8 F:7 2 4KF7E F:3F D7BD7E7@F N 47>AI. ):7K
3D7 @AF H3>;6 ;@ 3@K *@;5A67 7@5A6;@9; 4GF 3D7 GE78G> I:7@ ;@FDA6G5;@9 *@;5A67 7@5A6;@9E.
):7 355GD3F7 7@5A6;@9 H3>G7E 3D7 AGF A8 F:7 E5AB7 A8 F:;E 5:3BF7D.
1C@ R<T >C<M<>O@MN <M@ M@KM@N@IO@? DI @G@>OMJID> H@?DPHN DN ><GG@? 7@5A6;@9. 4@ C<Q@ N@@I
<=JQ@ CJR OC@ NOMDIB "C@GGJ" DN @I>J?@? DI A0&&. 4@ RDGG IJR N@@ OCM@@ 2ID>J?@ @I>J?DIBN
OC<O >JMM@NKJI? OJ !'N >C<M<>O@M OTK@N.
UTF-32: 1CDN @I>J?DIB PN@N 32 =DON (4 =TO@N) AJM @Q@MT 2ID>J?@ >C<M<>O@M. 1C@ 21#-32
@I>J?DIB JA "C@GGJ" DN NDHDG<M OJ DON A0&& @I>J?DIB, =PO @Q@MT >C<M<>O@M DN M@KM@N@IO@? =T 4
=TO@N:
0, 0, 0, 104, 0, 0, 0, 101, 0, 0, 0, 108, 0, 0, 0, 108, 0, 0, 0, 111
AN <IJOC@M @S<HKG@, OC@ 21#-32 @I>J?DIB JA "<a" DN:
0, 0, 0, 97, 0, 0, 1, 30
Note: ):7 35FG3> H3>G7E A8 EA?7 A8 F:7E7 4KF7E 3D7 6;887D7@F, 3@6 F:7 35FG3> AD67D A8 F:7
4KF7E ?3K 47 6;887D7@F.
< <I? a <M@ M@KM@N@IO@? =T 1 <I? 2 NDBIDAD><IO =TO@N M@NK@>ODQ@GT, <I? OC@ Q<GP@N JA OC@ JOC@M
5 =TO@N <M@ <GG U@MJN. 1C@N@ U@MJN ><I =@ OCJPBCO JA <N ADGG@M =TO@N OJ H<F@ @Q@MT 2ID>J?@
>C<M<>O@M J>>PKT 4 =TO@N @<>C.
#JM ?J>PH@ION =<N@? JI OC@ =<ND> )<ODI <GKC<=@O, OCDN @I>J?DIB <GR<TN PN@N 4 ODH@N <N H<IT
=TO@N <N OC@ A0&& @I>J?DIB. 4C@I HJNO JA OC@ >C<M<>O@MN JA < BDQ@I ?J>PH@IO C<Q@ A0&&
C:3D35F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
66
@LPDQ<G@ION, OC@ 3 ADGG@M =TO@N AJM @<>C JA OCJN@ >C<M<>O@MN RJPG? H<F@ OCDN @I>J?DIB HJM@
R<NO@APG >JHK<M@? OJ JOC@M @I>J?DIBN.
,I OC@ JOC@M C<I?, OC@M@ <M@ =@I@ADON JA @Q@MT >C<M<>O@M =@DIB M@KM@N@IO@? =T <I @LP<G
IPH=@M JA =TO@N.
UTF-16: 1CDN @I>J?DIB PN@N 16 =DON (2 =TO@N) OJ M@KM@N@IO HJNO JA OC@ 2ID>J?@ >C<M<>O@MN.
0DI>@ 16 =DON ><I C<Q@ <=JPO 65 OCJPN<I? PIDLP@ Q<GP@N, OC@ JOC@M 35 OCJPN<I? 2ID>J?@
>C<M<>O@MN HPNO =@ M@KM@N@IO@? =T HJM@ =TO@N.
AN <I @S<HKG@, "<a" DN @I>J?@? =T 4 =TO@N DI 21#-16:
0, 97, 1, 30
Note: ):7 35FG3> H3>G7E A8 EA?7 A8 F:7E7 4KF7E 3D7 6;887D7@F, 3@6 F:7 35FG3> AD67D A8 F:7
4KF7E ?3K 47 6;887D7@F.
JHK<M@? OJ 21#-32, OCDN @I>J?DIB O<F@N G@NN NK<>@ AJM HJNO ?J>PH@ION, =PO =@><PN@ OC@M@
<M@ >C<M<>O@MN OC<O <M@ M@KM@N@IO@? =T HJM@ OC<I 2 =TO@N, 21#-16 DN HJM@ >JHKGD><O@? OJ
KMJ>@NN.
UTF-8: 1CDN @I>J?DIB PN@N 1 JM HJM@ =TO@N AJM @Q@MT >C<M<>O@M. &A < >C<M<>O@M C<N <I
@LPDQ<G@IO DI OC@ A0&& O<=G@, DO DN M@KM@N@IO@? =T 1 =TO@ <I? =T OC@ N<H@ >J?@ <N DI OC@
A0&& O<=G@. 1C@ M@NO JA OC@ 2ID>J?@ >C<M<>O@MN <M@ M@KM@N@IO@? =T 2, 3, JM 4 =TO@N. *JNO JA
OC@ NK@>D<G >C<M<>O@MN JA OC@ "PMJK@<I RMDODIB NTNO@HN <M@ <HJIB OC@ BMJPK JA >C<M<>O@MN
OC<O <M@ M@KM@N@IO@? =T 2 =TO@N.
#JM HJNO ?J>PH@ION, 21#-8 DN OC@ @I>J?DIB OC<O O<F@N OC@ G@<NO <HJPIO JA NK<>@. AIJOC@M
=@I@ADO JA 21#-8 DN OC<O OC@ ?J>PH@ION OC<O C<Q@ <GM@<?T =@@I @I>J?@? DI A0&& ?DM@>OGT
H<O>C OC@DM 21#-8 @I>J?DIBN. 21#-8 ?J@N IJO R<NO@ <IT NK<>@: @Q@MT >C<M<>O@M DN
M@KM@N@IO@? =T NDBIDAD><IO =TO@N. AN <I @S<HKG@, OC@ 21#-8 @I>J?DIB JA "<a" DN:
97, 1, 30
Note: ):7 35FG3> H3>G7E A8 EA?7 A8 F:7E7 4KF7E 3D7 6;887D7@F, 3@6 F:7 35FG3> AD67D A8 F:7
4KF7E ?3K 47 6;887D7@F.
The character types of D
18.3
1C@M@ <M@ OCM@@ ! OTK@N OJ M@KM@N@IO >C<M<>O@MN. 1C@N@ >C<M<>O@MN >JMM@NKJI? OJ OC@ OCM@@
2ID>J?@ @I>J?DIBN H@IODJI@? <=JQ@. AN TJP M@H@H=@M AMJH OC@ #PI?<H@IO<G 1TK@N
>C<KO@M:
5ZQF %FGJOJUJPO *OJUJBM 7BMVF
DIBS 65'-8 DPEF VOJU 0Y''
XDIBS 65'-16 DPEF VOJU 0Y''''
EDIBS 65'-32 DPEF VOJU BOE 6OJDPEF DPEF QPJOU 0Y0000''''
JHK<M@? OJ NJH@ JOC@M KMJBM<HHDIB G<IBP<B@N, >C<M<>O@MN DI ! H<T IJO >JINDNO JA @LP<G
IPH=@M JA =TO@N. #JM @S<HKG@, =@><PN@ a ><I =@ M@KM@N@IO@? =T <O G@<NO 2 =TO@N DI 2ID>J?@,
DO ?J@NI'O ADO DI < Q<MD<=G@ JA OTK@ DIBS. ,I OC@ JOC@M C<I?, =@><PN@ EDIBS >JINDNON JA 4 =TO@N,
DO ><I CJG? <IT 2ID>J?@ >C<M<>O@M.
!@NKDO@ KMJQD?DIB OC@N@ PN@APG OTK@N, ! ?J@N IJO NPKKJMO NJH@ @NJO@MD> A@<OPM@N JA 2ID>J?@. &
RDGG @SKG<DI OCDN =@GJR.
C:3D35F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
67
Character literals
18.4
)DO@M<GN <M@ >JINO<IO Q<GP@N OC<O <M@ OTK@? DI OC@ KMJBM<H <N < K<MO JA OC@ NJPM>@ >J?@. &I !,
>C<M<>O@M GDO@M<GN <M@ NK@>DAD@? RDOCDI NDIBG@ LPJO@N:
DIBS MFUUFS@B = 'B';
XDIBS MFUUFS@F@BDVUF = 'a';
!JP=G@ LPJO@N <M@ IJO Q<GD? AJM >C<M<>O@MN =@><PN@ ?JP=G@ LPJO@N <M@ PN@? RC@I NK@>DATDIB
EFD;@9E, RCD>C R@ RDGG N@@ ORJ >C<KO@MN G<O@M. 'B' DN < >C<M<>O@M GDO@M<G <I? "B" DN < NOMDIB
GDO@M<G OC<O >JINDNON JA JI@ >C<M<>O@M.
3<MD<=G@N JA OTK@ DIBS ><I JIGT CJG? G@OO@MN OC<O <M@ DI OC@ A0&& O<=G@.
1C@M@ <M@ H<IT R<TN JA DIN@MODIB >C<M<>O@MN DI >J?@:
t *JNO I<OPM<GGT, OTKDIB OC@H JI OC@ F@T=J<M?.
t JKTDIB AMJH <IJOC@M KMJBM<H JM <IJOC@M O@SO. #JM @S<HKG@, TJP ><I >JKT <I? K<NO@ AMJH
< R@= NDO@, JM AMJH < KMJBM<H OC<O DN NK@>DAD><GGT AJM ?DNKG<TDIB 2ID>J?@ >C<M<>O@MN. (,I@
NP>C KMJBM<H DI HJNO )DIPS @IQDMJIH@ION DN C:3D35F7D #3B, 5:3D?3B JI OC@ >JINJG@.)
t 2NDIB NCJMO I<H@N JA OC@ >C<M<>O@MN. 1C@ NTIO<S AJM OCDN DN =&character_name;. #JM
@S<HKG@, OC@ I<H@ JA OC@ "PMJ >C<M<>O@M DN FVSP <I? DO ><I =@ NK@>DAD@? DI OC@ KMJBM<H
GDF@ OC@ AJGGJRDIB:
XDIBS DVSSFODZ4ZNCPM = '=&FVSP;';
0@@ OC@ GDNO JA I<H@? >C<M<>O@MN AJM <GG JA OC@ >C<M<>O@MN OC<O ><I =@ NK@>DAD@? OCDN
R<T.
t 0K@>DATDIB >C<M<>O@MN =T OC@DM DIO@B@M 2ID>J?@ Q<GP@N:
DIBS B = 97;
XDIBS c = 286;
t 0K@>DATDIB OC@ >J?@N JA OC@ >C<M<>O@MN JA OC@ A0&& O<=G@ @DOC@M =T =value_in_octal JM
=Yvalue_in_hexadecimal NTIO<S:
DIBS RVFTUJPO.BSL0DUBM = '=77';
DIBS RVFTUJPO.BSL)FYBEFDJNBM = '=Y3G';
t 0K@>DATDIB OC@ 2ID>J?@ Q<GP@N JA OC@ >C<M<>O@MN =T PNDIB OC@ =Vfour_digit_value NTIO<S
AJM XDIBS, <I? OC@ =6eight_digit_value NTIO<S AJM EDIBS. (+JO@ V QN. 6.) 1C@ 2ID>J?@
Q<GP@N HPNO =@ NK@>DAD@? DI C@S<?@>DH<G:
XDIBS c@X = '=V011F';
EDIBS c@E = '=60000011F';
1C@N@ H@OCJ?N ><I =@ PN@? OJ NK@>DAT OC@ >C<M<>O@MN RDOCDI NOMDIBN <N R@GG. #JM @S<HKG@, OC@
AJGGJRDIB ORJ GDI@N C<Q@ OC@ N<H@ NOMDIB GDO@M<GN:
XSJUFMO("3aTVNa QSFQBSBUJPO: 10.25i");
XSJUFMO("=Y52=&FBDVUF;TVN=V00F9 QSFQBSBUJPO: 10.25=&FVSP;");
Control characters
18.5
0JH@ >C<M<>O@MN JIGT <AA@>O OC@ AJMH<O JA OC@ O@SO, OC@T ?JI'O C<Q@ < QDNP<G M@KM@N@IO<ODJI
OC@HN@GQ@N. #JM @S<HKG@, OC@ @7I->;@7 >C<M<>O@M, RCD>C NK@>DAD@N OC<O OC@ JPOKPO NCJPG?
C:3D35F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
68
>JIODIP@ JI < I@R GDI@, ?J@N IJO C<Q@ < QDNP<G M@KM@N@IO<ODJI. 0P>C >C<M<>O@MN <M@ ><GG@?
5A@FDA> 5:3D35F7DE. JIOMJG >C<M<>O@MN <M@ NK@>DAD@? RDOC OC@ =control_character NTIO<S.
4ZOUBY /BNF %FGJOJUJPO
=O OFX MJOF .PWFT UIF QSJOUJOH UP B OFX MJOF
=S DBSSJBHF SFUVSO.PWFT UIF QSJOUJOH UP UIF CFHJOOJOH PG UIF DVSSFOU
MJOF
=U UBC .PWFT UIF QSJOUJOH UP UIF OFYU UBC TUPQ
#JM @S<HKG@, OC@ XSJUF() API>ODJI, RCD>C ?J@N IJO NO<MO < I@R GDI@ <POJH<OD><GGT, RJPG? ?J
NJ AJM @Q@MT =O >C<M<>O@M. "Q@MT J>>PMM@I>@ JA OC@ =O >JIOMJG >C<M<>O@M RDOCDI OC@ AJGGJRDIB
GDO@M<G M@KM@N@ION OC@ NO<MO JA < I@R GDI@:
XSJUF("GJSTU MJOF=OTFDPOE MJOF=OUIJSE MJOF=O");
1C@ JPOKPO:
GJSTU MJOF
TFDPOE MJOF
UIJSE MJOF
Single quote and backslash
18.6
1C@ NDIBG@ LPJO@ >C<M<>O@M DON@GA ><IIJO =@ RMDOO@I RDOCDI NDIBG@ LPJO@N =@><PN@ OC@ >JHKDG@M
RJPG? O<F@ OC@ N@>JI? JI@ <N OC@ >GJNDIB >C<M<>O@M JA OC@ ADMNO JI@: '''. 1C@ ADMNO ORJ RJPG?
=@ OC@ JK@IDIB <I? >GJNDIB LPJO@N, <I? OC@ OCDM? JI@ RJPG? =@ G@AO <GJI@, ><PNDIB <
>JHKDG<ODJI @MMJM.
0DHDG<MGT, NDI>@ OC@ =<>FNG<NC >C<M<>O@M C<N < NK@>D<G H@<IDIB DI OC@ >JIOMJG >C<M<>O@M <I?
GDO@M<G NTIO<S@N, OC@ >JHKDG@M RJPG? O<F@ DO <N OC@ NO<MO JA NP>C < NTIO<S: ='. 1C@ >JHKDG@M
OC@I RJPG? =@ GJJFDIB AJM < >GJNDIB NDIBG@ LPJO@ >C<M<>O@M, IJO ADI?DIB JI@, <I? @HDOODIB <
>JHKDG<ODJI @MMJM.
1J <QJD? OC@N@ >JIAPNDJIN, OC@ NDIBG@ LPJO@ <I? OC@ =<>FNG<NC >C<M<>O@MN <M@ 7E53B76 =T <
KM@>@?DIB =<>FNG<NC >C<M<>O@M:
4ZOUBY /BNF %FGJOJUJPO
=' TJOHMF RVPUF "MMPXT TQFDJGZJOH UIF TJOHMF RVPUF DIBSBDUFS:'=''
== CBDLTMBTI "MMPXT TQFDJGZJOH UIF CBDLTMBTI DIBSBDUFS: '==' PS "=="
The std.uni module
18.7
1C@ TUE.VOJ HJ?PG@ DI>GP?@N API>ODJIN OC<O <M@ PN@APG RDOC 2ID>J?@ >C<M<>O@MN. 6JP ><I N@@
OC@N@ API>ODJIN <O DON ?J>PH@IO<ODJI.
1C@ API>ODJIN OC<O NO<MO RDOC JT <INR@M >@MO<DI LP@NODJIN <=JPO >C<M<>O@MN: OC@ M@NPGO DN
GBMTF JM USVF ?@K@I?DIB JI RC@OC@M OC@ <INR@M DN IJ JM T@N, M@NK@>ODQ@GT. 1C@N@ API>ODJIN
<M@ PN@APG DI GJBD><G @SKM@NNDJIN:
t JT-PXFS: DN < GJR@M><N@ >C<M<>O@M?
t JT6QQFS: DN <I PKK@M><N@ >C<M<>O@M?
t JT"MQIB: DN < 2ID>J?@ <GKC<=@OD> >C<M<>O@M?
t JT8IJUF: DN < RCDO@NK<>@ >C<M<>O@M?
C:3D35F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
69
1C@ API>ODJIN OC<O NO<MO RDOC UP KMJ?P>@ I@R >C<M<>O@MN AMJH @SDNODIB >C<M<>O@MN:
t UP-PXFS: KMJ?P>@N OC@ GJR@M><N@ JA OC@ BDQ@I >C<M<>O@M
t UP6QQFS: KMJ?P>@N OC@ PKK@M><N@ JA OC@ BDQ@I >C<M<>O@M
%@M@ DN < KMJBM<H OC<O PN@N <GG JA OC@N@ API>ODJIN:
JNQPSU TUE.TUEJP;
JNQPSU TUE.VOJ;
WPJE NBJO()
\
XSJUFMO("*T d MPXFSDBTF ", JT-PXFS('d'));
XSJUFMO("*T g MPXFSDBTF ", JT-PXFS('g'));
XSJUFMO("*T e VQQFSDBTF ", JT6QQFS('e'));
XSJUFMO("*T ` VQQFSDBTF ", JT6QQFS('`'));
XSJUFMO("*T [ BMQIBOVNFSJD ", JT"MQIB('['));
XSJUFMO("*T =&FVSP; BMQIBOVNFSJD ", JT"MQIB('=&FVSP;'));
XSJUFMO("*T OFX-MJOF XIJUFTQBDF ", JT8IJUF('=O'));
XSJUFMO("*T VOEFSMJOF XIJUFTQBDF ", JT8IJUF('@'));
XSJUFMO("5IF MPXFSDBTF PG c: ", UP-PXFS('c'));
XSJUFMO("5IF MPXFSDBTF PG e: ", UP-PXFS('e'));
XSJUFMO("5IF VQQFSDBTF PG h: ", UP6QQFS('h'));
XSJUFMO("5IF VQQFSDBTF PG f: ", UP6QQFS('f'));
^
1C@ JPOKPO:
*T e MPXFSDBTF USVF
*T h MPXFSDBTF GBMTF
*T f VQQFSDBTF USVF
*T a VQQFSDBTF GBMTF
*T [ BMQIBOVNFSJD USVF
*T k BMQIBOVNFSJD GBMTF
*T OFX-MJOF XIJUFTQBDF USVF
*T VOEFSMJOF XIJUFTQBDF GBMTF
5IF MPXFSDBTF PG d: e
5IF MPXFSDBTF PG f: J
5IF VQQFSDBTF PG i: h
5IF VQQFSDBTF PG g: *
Limited support for and i of the Turkish alphabet
18.8
1C@ GJR@M><N@ <I? PKK@M><N@ Q@MNDJIN JA OC@ G@OO@MN d <I? D <M@ >JINDNO@IOGT ?JOO@? JM
PI?JOO@? DI OC@ 1PMFDNC <GKC<=@O. *JNO JOC@M <KC<=@ON <M@ DI>JINDNO@IO DI OCDN M@B<M?: OC@
PKK@M><N@ JA OC@ ?JOO@? D DN PI?JOO@? &.
B@><PN@ OC@ >JHKPO@M NTNO@HN C<Q@ NO<MO@? RDOC OC@ A0&& O<=G@, OM<?DODJI<GGT OC@ PKK@M><N@
JA D DN &, <I? OC@ GJR@M><N@ JA & DN D. #JM OC<O M@<NJI, OC@N@ ORJ G@OO@MN I@@? NK@>D<G <OO@IODJI.
1C@ AJGGJRDIB KMJBM<H ?@HJINOM<O@N OCDN KMJ=G@H:
JNQPSU TUE.TUEJP;
JNQPSU TUE.VOJ;
WPJE NBJO()
C:3D35F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
70
\
XSJUFMO("5IF VQQFSDBTF PG J: ", UP6QQFS('J'));
XSJUFMO("5IF MPXFSDBTF PG *: ", UP-PXFS('*'));
^
1C@ JPOKPO DN <>>JM?DIB OJ OC@ =<ND> )<ODI <GKC<=@O:
5IF VQQFSDBTF PG J: *
5IF MPXFSDBTF PG *: J
Every alphabet has limited support 18.8.1
C<M<>O@MN <M@ >JIQ@MO@? =@OR@@I OC@DM PKK@M><N@ <I? GJR@M><N@ Q@MNDJIN IJMH<GGT =T OC@DM
2ID>J?@ >C<M<>O@M >J?@N. 1CDN H@OCJ? DN KMJ=G@H<OD> AJM H<IT <GKC<=@ON. #JM @S<HKG@, OC@
AU@MD <I? @GO <GKC<=@ON <M@ NP=E@>O OJ OC@ N<H@ KMJ=G@H JA KMJ?P>DIB OC@ GJR@M><N@ JA & <N D.
1C@M@ <M@ NDHDG<M KMJ=G@HN RDOC NJMODIB. )@OO@MN JA H<IT <GKC<=@ON, GDF@ OC@ 1PMFDNC b, <M@ <GG
NJMO@? <AO@M U. "Q@I <>>@IO@? >C<M<>O@MN GDF@ [ <M@ NJMO@? <AO@M U @Q@I AJM OC@ =<ND> )<ODI
<GKC<=@O.
The trileri library 18.8.2
A ! KMJE@>O =T OC@ H@H=@MN JA OC@ !?DGD #JMPH OC<O <??M@NN@N OC@N@ KMJ=G@HN DN OC@ OMDG@MD
GD=M<MT. AO OC@ ODH@ JA RMDODIB OCDN >C<KO@M, OMDG@MD NPKKJMON OC@ "IBGDNC, 1PMFDNC, AU@MD,
1PMFH@I, (dMdH 1<O<M, $<B<QPU, (PHPF, %<F<N, (PM?DNC, <I? @GO <GKC<=@ON. &O NJMON OC@
G@OO@MN JA OC@N@ <GKC<=@ON <I? >JIQ@MON OC@DM G@OO@MN OJ <I? AMJH PKK@M><N@ <I? GJR@M><N@
>JMM@>OGT.
6JP ><I O@NO OC@ OMDG@MD GD=M<MT <O OC@ AJGGJRDIB 2/):
COOK://RRR.??DGD.JMB/>BD-=DI/OMDG@MD;?@I@H@
Problems with reading characters
18.9
1C@ AG@SD=DGDOT <I? KJR@M JA !'N 2ID>J?@ >C<M<>O@MN H<T ><PN@ PI@SK@>O@? M@NPGON RC@I
M@<?DIB >C<M<>O@MN AMJH <I DIKPO NOM@<H. 1CDN >JIOM<?D>ODJI DN ?P@ OJ OC@ HPGODKG@ H@<IDIBN
JA OC@ O@MH 5:3D35F7D. B@AJM@ @SK<I?DIB JI OCDN APMOC@M, G@O'N GJJF <O < KMJBM<H OC<O C<N OCDN
KMJ=G@H:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
DIBS MFUUFS;
XSJUF("1MFBTF FOUFS B MFUUFS: ");
SFBEG(" %T", &MFUUFS);
XSJUFMO("5IF MFUUFS UIBU IBT CFFO SFBE: ", MFUUFS);
^
&A TJP OMT OC<O KMJBM<H DI <I @IQDMJIH@IO OC<O ?J@N IJO PN@ 2ID>J?@, TJP H<T N@@ OC<O @Q@I
OC@ IJI-A0&& >C<M<>O@MN <M@ M@<? <I? KMDIO@? >JMM@>OGT.
,I OC@ JOC@M C<I?, DA TJP NO<MO OC@ N<H@ KMJBM<H DI < 2ID>J?@ @IQDMJIH@IO, AJM @S<HKG@ DI <
)DIPS >JINJG@, TJP H<T N@@ OC<O OC@ >C<M<>O@M KMDIO@? JI OC@ JPOKPO DN IJO OC@ N<H@
>C<M<>O@M OC<O C<N =@@I @IO@M@?. 1J N@@ OCDN, G@O'N @IO@M < IJI-A0&& >C<M<>O@M DI < >JINJG@
OC<O PN@N OC@ 21#-8 @I>J?DIB (GDF@ HJNO )DIPS >JINJG@N):
C:3D35F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
71
1MFBTF FOUFS B MFUUFS: e
5IF MFUUFS UIBU IBT CFFO SFBE: l OP MFUUFS PO UIF PVUQVU
1C@ M@<NJI AJM OCDN KMJ=G@H DN OC<O OC@ IJI-A0&& >C<M<>O@MN GDF@ b <M@ M@KM@N@IO@? =T ORJ
>J?@N, <I? M@<?DIB < DIBS AMJH OC@ DIKPO M@<?N JIGT OC@ ADMNO JI@ JA OCJN@ >J?@N. 0DI>@ OC<O
NDIBG@ DIBS DN IJO NPAAD>D@IO OJ M@KM@N@IO OC@ RCJG@ 2ID>J?@ >C<M<>O@M, OC@ >JINJG@ ?J@N IJO
?DNKG<T OC@ ;@5A?B>7F7 >C<M<>O@M.
1J NCJR OC<O OC@ 21#-8 >J?@N OC<O H<F@ PK < >C<M<>O@M <M@ M@<? JI@ DIBS <O < ODH@, G@O'N
M@<? ORJ DIBS Q<MD<=G@N <I? KMDIO OC@H JI@ <AO@M OC@ JOC@M:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
DIBS GJSTU$PEF;
DIBS TFDPOE$PEF;
XSJUF("1MFBTF FOUFS B MFUUFS: ");
SFBEG(" %T", &GJSTU$PEF);
SFBEG(" %T", &TFDPOE$PEF);
XSJUFMO("5IF MFUUFS UIBU IBT CFFO SFBE: ",
GJSTU$PEF, TFDPOE$PEF);
^
1C@ KMJBM<H M@<?N ORJ DIBS Q<MD<=G@N AMJH OC@ DIKPO <I? KMDION OC@H DI OC@ N<H@ JM?@M OC<O
OC@T <M@ M@<?. 4C@I OCJN@ >J?@N <M@ N@IO OJ OC@ >JINJG@ DI OC<O N<H@ JM?@M, OC@T >JHKG@O@
OC@ 21#-8 @I>J?DIB JA OC@ 2ID>J?@ >C<M<>O@M JI OC@ >JINJG@ <I? OCDN ODH@ OC@ 2ID>J?@
>C<M<>O@M DN KMDIO@? >JMM@>OGT:
1MFBTF FOUFS B MFUUFS: e
5IF MFUUFS UIBU IBT CFFO SFBE: e l UIF 6OJDPEF DIBSBDUFS UIBU
DPOTJTUT PG UXP DIBS DPEFT
1C@N@ M@NPGON <M@ <GNJ M@G<O@? OJ OC@ A<>O OC<O OC@ NO<I?<M? DIKPON <I? JPOKPON JA KMJBM<HN
<M@ DIBS NOM@<HN.
4@ RDGG N@@ G<O@M DI OC@ 0OMDIBN >C<KO@M OC<O DO DN @<ND@M OJ M@<? >C<M<>O@MN <N NOMDIBN, DINO@<?
JA ?@<GDIB RDOC 21# >J?@N DI?DQD?P<GGT.
D's Unicode support
18.10
2ID>J?@ DN < G<MB@ <I? >JHKGD><O@? NO<I?<M?. ! NPKKJMON < Q@MT PN@APG NP=N@O JA DO.
A 2ID>J?@-@I>J?@? ?J>PH@IO >JINDNON JA OC@ AJGGJRDIB G@Q@GN JA >JI>@KON AMJH OC@ GJR@MHJNO
OJ OC@ PKK@MHJNO:
t Code unit: 1C@ Q<GP@N OC<O H<F@ PK OC@ 21# @I>J?DIBN <M@ ><GG@? >J?@ PIDON. !@K@I?DIB
JI OC@ @I>J?DIB <I? OC@ >C<M<>O@MN OC@HN@GQ@N, 2ID>J?@ >C<M<>O@MN <M@ H<?@ PK JA JI@
JM HJM@ >J?@ PIDON. #JM @S<HKG@, DI OC@ 21#-8 @I>J?DIB OC@ G@OO@M < DN H<?@ PK JA < NDIBG@
>J?@ PIDO <I? OC@ G@OO@M b DN H<?@ PK JA ORJ >J?@ PIDON.
!'N >C<M<>O@M OTK@N DIBS, XDIBS, <I? EDIBS >JMM@NKJI? OJ 21#-8, 21#-16, <I?
21#-32 >J?@ PIDON, M@NK@>ODQ@GT.
t Code point: "Q@MT G@OO@M, IPH@M<G, NTH=JG, @O>. OC<O OC@ 2ID>J?@ NO<I?<M? ?@ADI@N DN
><GG@? < >J?@ KJDIO. #JM @S<HKG@ OC@ 2ID>J?@ >J?@ Q<GP@N JA < <I? b <M@ ORJ ?DNODI>O >J?@
KJDION.
C:3D35F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
72
!@K@I?DIB JI OC@ @I>J?DIB, >J?@ KJDION <M@ M@KM@N@IO@? =T JI@ JM HJM@ >J?@ PIDON.
AN H@IODJI@? <=JQ@, DI OC@ 21#-8 @I>J?DIB < DN M@KM@N@IO@? =T < NDIBG@ >J?@ PIDO,
<I? b DN M@KM@N@IO@? =T ORJ >J?@ PIDON. ,I OC@ JOC@M C<I?, =JOC < <I? b <M@
M@KM@N@IO@? =T < NDIBG@ >J?@ PIDO DI =JOC 21#-16 <I? 21#-32 @I>J?DIBN.
1C@ ! OTK@ OC<O NPKKJMON >J?@ KJDION DN EDIBS. DIBS <I? XDIBS ><I JIGT =@ PN@? <N
>J?@ PIDON.
t Character: AIT NTH=JG OC<O OC@ 2ID>J?@ NO<I?<M? ?@ADI@N <I? RC<O R@ ><GG ">C<M<>O@M" DI
?<DGT O<GF DN < >C<M<>O@M.
1CDN ?@ADIDODJI JA >C<M<>O@M DN AG@SD=G@ DI 2ID>J?@, <I? OCDN =MDIBN < >JHKGD><ODJI.
0JH@ >C<M<>O@MN ><I =@ AJMH@? =T HJM@ OC<I JI@ >J?@ KJDIO. #JM @S<HKG@, OC@ G@OO@M
b ><I =@ NK@>DAD@? DI ORJ R<TN:
w <N OC@ NDIBG@ >J?@ KJDIO b
w <N OC@ ORJ >J?@ KJDION B <I? g (>JH=DIDIB =M@Q@)
! ?J@N IJO I<ODQ@GT NPKKJMO OC@ >C<M<>O@M >JI>@KO JA >JH=DI@? >J?@ KJDION. &I !, OC@
NDIBG@ >J?@ KJDIO b DN ?DAA@M@IO AMJH OC@ ORJ >JIN@>PODQ@ >J?@ KJDION B <I? g.
Summary
18.11
t 2ID>J?@ NPKKJMON <GG JA OC@ >C<M<>O@MN JA <GG JA OC@ RMDODIB NTNO@HN.
t DIBS DN AJM OC@ 21#-8 @I>J?DIB; <GOCJPBC DO DN IJO NPDO<=G@ OJ M@KM@N@IO >C<M<>O@MN DI
B@I@M<G, DO NPKKJMON OC@ A0&& O<=G@.
t XDIBS DN AJM OC@ 21#-16 @I>J?DIB; <GOCJPBC DO DN IJO NPDO<=G@ OJ M@KM@N@IO >C<M<>O@MN DI
B@I@M<G, DO ><I NPKKJMO G@OO@MN JA HPGODKG@ <GKC<=@ON.
t EDIBS DN AJM OC@ 21#-32 @I>J?DIB; =@><PN@ DO DN 32 =DON, DO ><I <GNJ =@ PN@? <N >J?@ KJDION.
C:3D35F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
73
Slices and Other Array Features 19
4@ C<Q@ N@@I DI OC@ AMM<TN >C<KO@M CJR @G@H@ION <M@ BMJPK@? <N < >JGG@>ODJI DI <I <MM<T.
1C<O >C<KO@M C<N DIO@IODJI<GGT =@@I =MD@A, G@<QDIB HJNO JA OC@ A@<OPM@N JA <MM<TN OJ OCDN
>C<KO@M.
B@AJM@ BJDIB <IT APMOC@M, C@M@ <M@ =MD@A ?@ADIDODJIN JA NJH@ JA OC@ O@MHN OC<O C<KK@I OJ =@
>GJN@ DI H@<IDIB:
t Array: 1C@ B@I@M<G >JI>@KO JA < BMJPK JA @G@H@ION OC<O <M@ GJ><O@? ND?@ =T ND?@ <I? <M@
<>>@NN@? =T DI?@S@N.
t Fixed-length array (static array): AMM<T RDOC < ADS@? IPH=@M JA @G@H@ION. 1CDN OTK@ JA
<MM<T JRIN DON @G@H@ION.
t Dynamic array: AMM<T OC<O ><I B<DI JM GJN@ @G@H@ION. 1CDN OTK@ JA <MM<T KMJQD?@N <>>@NN
OJ @G@H@ION OC<O <M@ JRI@? =T OC@ ! MPIODH@ @IQDMJIH@IO.
t Slice: AIJOC@M I<H@ AJM 6K@3?;5 3DD3K.
4C@I & RMDO@ E>;57 & RDGG NK@>DAD><GGT H@<I < NGD>@; <I? RC@I & RMDO@ 3DD3K, & RDGG H@<I @DOC@M <
NGD>@ JM < ADS@?-G@IBOC <MM<T, RDOC IJ ?DNODI>ODJI.
Slices
19.1
0GD>@N <M@ OC@ N<H@ A@<OPM@ <N ?TI<HD> <MM<TN. 1C@T <M@ ><GG@? 6K@3?;5 3DD3KE AJM =@DIB PN@?
GDF@ <MM<TN, <I? <M@ ><GG@? E>;57E AJM KMJQD?DIB <>>@NN OJ KJMODJIN JA JOC@M <MM<TN. 1C@T <GGJR
PNDIB OCJN@ KJMODJIN <N DA OC@T <M@ N@K<M<O@ <MM<TN.
0GD>@N <M@ ?@ADI@? =T OC@ @G?47D D3@97 NTIO<S OC<O >JMM@NKJI? OJ OC@ DI?@S@N OC<O NK@>DAT OC@
=@BDIIDIB <I? OC@ @I? JA OC@ M<IB@:
beginning_index .. one_beyond_the_end_index
&I OC@ IPH=@M M<IB@ NTIO<S, OC@ =@BDIIDIB DI?@S DN < K<MO JA OC@ M<IB@ =PO OC@ @I? DI?@S DN
JPOND?@ JA OC@ M<IB@:
/* ... */ = NPOUI%BZT<0 .. 3>; // 0, 1, BOE 2 BSF JODMVEFE; CVU OPU 3
Note: $G?47D D3@97E 3D7 6;887D7@F 8DA? &:A4AE D3@97E. &:A4AE D3@97E 3D7 34AGF EFDG5F 3@6
5>3EE ;@F7D8357E. ,7 I;>> E77 F:7E7 873FGD7E ;@ >3F7D 5:3BF7DE.
AN <I @S<HKG@, R@ ><I E>;57 OC@ NPOUI%BZT <MM<T OJ =@ <=G@ OJ PN@ DON K<MON <N AJPM NH<GG@M
<MM<TN:
JOU<12> NPOUI%BZT =
< 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 >;
JOU<> GJSTU2VBSUFS = NPOUI%BZT<0 .. 3>;
JOU<> TFDPOE2VBSUFS = NPOUI%BZT<3 .. 6>;
JOU<> UIJSE2VBSUFS = NPOUI%BZT<6 .. 9>;
JOU<> GPVSUI2VBSUFS = NPOUI%BZT<9 .. 12>;
1C@ AJPM Q<MD<=G@N DI OC@ >J?@ <=JQ@ <M@ NGD>@N; OC@T KMJQD?@ <>>@NN OJ AJPM K<MON JA <I
<GM@<?T @SDNODIB <MM<T. AI DHKJMO<IO KJDIO RJMOC NOM@NNDIB C@M@ DN OC<O OCJN@ NGD>@N ?J IJO
C<Q@ OC@DM JRI @G@H@ION. 1C@T H@M@GT KMJQD?@ <>>@NN OJ OC@ @G@H@ION JA OC@ <>OP<G <MM<T.
*J?DATDIB <I @G@H@IO JA < NGD>@ HJ?DAD@N OC@ @G@H@IO JA OC@ <>OP<G <MM<T. 1J N@@ OCDN, G@O'N
HJ?DAT OC@ ADMNO @G@H@ION JA @<>C NGD>@ <I? OC@I KMDIO OC@ <>OP<G <MM<T:
(>;57E 3@6 %F:7D ADD3K F73FGD7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
74
GJSTU2VBSUFS<0> = 1;
TFDPOE2VBSUFS<0> = 2;
UIJSE2VBSUFS<0> = 3;
GPVSUI2VBSUFS<0> = 4;
XSJUFMO(NPOUI%BZT);
1C@ JPOKPO:
<1, 28, 31, 2, 31, 30, 3, 31, 30, 4, 30, 31>
"<>C NGD>@ HJ?DAD@N DON ADMNO @G@H@IO, <I? OC@ >JMM@NKJI?DIB @G@H@IO JA OC@ <>OP<G <MM<T DN
<AA@>O@?.
4@ C<Q@ N@@I @<MGD@M OC<O Q<GD? <MM<T DI?@S@N <M@ AMJH 0 OJ JI@ G@NN OC<I OC@ G@IBOC JA OC@
<MM<T. #JM @S<HKG@, OC@ Q<GD? DI?@S@N JA < 3-@G@H@IO <MM<T <M@ 0, 1, <I? 2. 0DHDG<MGT, OC@ @I?
DI?@S DI OC@ NGD>@ NTIO<S NK@>DAD@N JI@ =@TJI? OC@ G<NO @G@H@IO OC<O OC@ NGD>@ RDGG =@ KMJQD?DIB
<>>@NN OJ. #JM OC<O M@<NJI, RC@I OC@ G<NO @G@H@IO JA <I <MM<T I@@?N OJ =@ DI>GP?@? DI < NGD>@,
OC@ G@IBOC JA OC@ <MM<T HPNO =@ NK@>DAD@? <N OC@ @I? DI?@S. #JM @S<HKG@, < NGD>@ JA <GG JA OC@
@G@H@ION JA < 3-@G@H@IO <MM<T RJPG? =@ BSSBZ<0..3>.
AI J=QDJPN GDHDO<ODJI DN OC<O OC@ =@BDIIDIB DI?@S ><IIJO =@ BM@<O@M OC<I OC@ @I? DI?@S:
JOU<3> BSSBZ = < 0, 1, 2 >;
JOU<> TMJDF = BSSBZ<2 .. 1>; // j SVO-UJNF &3303
&O DN G@B<G OJ C<Q@ OC@ =@BDIIDIB <I? OC@ @I? DI?@S@N OJ =@ @LP<G. &I OC<O ><N@ OC@ NGD>@ DN
@HKOT. ANNPHDIB OC<O JOEFY DN Q<GD?:
JOU<> TMJDF = BO"SSBZ<JOEFY .. JOEFY>;
XSJUFMO("5IF MFOHUI PG UIF TMJDF: ", TMJDF.MFOHUI);
1C@ JPOKPO:
5IF MFOHUI PG UIF TMJDF: 0
$, instead of BSSBZ.MFOHUI
19.2
4C@I DI?@SDIB, $ DN < NCJMOC<I? AJM OC@ G@IBOC JA OC@ <MM<T:
XSJUFMO(BSSBZ<BSSBZ.MFOHUI - 1>); // UIF MBTU FMFNFOU
XSJUFMO(BSSBZ<$ - 1>); // UIF TBNF UIJOH
.EVQ to copy
19.3
0CJMO AJM "?PKGD><O@", OC@ .EVQ KMJK@MOT H<F@N < I@R <MM<T AMJH OC@ >JKD@N JA OC@ @G@H@ION
JA <I @SDNODIB <MM<T:
EPVCMF<> BSSBZ = < 1.25, 3.75 >;
EPVCMF<> UIF$PQZ = BSSBZ.EVQ;
AN <I @S<HKG@, G@O'N ?@ADI@ <I <MM<T OC<O >JIO<DIN OC@ IPH=@M JA ?<TN JA OC@ HJIOCN JA < G@<K
T@<M. A H@OCJ? DN OJ O<F@ < >JKT JA OC@ IJI-G@<K-T@<M <MM<T <I? OC@I OJ DI>M@H@IO OC@ @G@H@IO
OC<O >JMM@NKJI?N OJ #@=MP<MT:
(>;57E 3@6 %F:7D ADD3K F73FGD7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
75
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU<12> NPOUI%BZT =
< 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 >;
JOU<> MFBQ:FBS = NPOUI%BZT.EVQ;
++MFBQ:FBS<1>; // JODSFNFOUT UIF EBZT JO 'FCSVBSZ
XSJUFMO("/PO-MFBQ ZFBS: ", NPOUI%BZT);
XSJUFMO("-FBQ ZFBS : ", MFBQ:FBS);
^
1C@ JPOKPO:
/PO-MFBQ ZFBS: <31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31>
-FBQ ZFBS : <31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31>
Assignment
19.4
4@ C<Q@ N@@I NJ A<M OC<O OC@ <NNDBIH@IO JK@M<OJM ?A6;8;7E Q<GP@N JA Q<MD<=G@N. &O DN OC@ N<H@
RDOC ADS@?-G@IBOC <MM<TN:
JOU<3> B = < 1, 1, 1 >;
JOU<3> C = < 2, 2, 2 >;
B = C; // UIF FMFNFOUT PG 'B' CFDPNF 2
XSJUFMO(B);
1C@ JPOKPO:
<2, 2, 2>
1C@ <NNDBIH@IO JK@M<ODJI C<N < >JHKG@O@GT ?DAA@M@IO H@<IDIB AJM NGD>@N: &O H<F@N OC@ NGD>@
NO<MO KMJQD?DIB <>>@NN OJ I@R @G@H@ION:
JOU<> PEET = < 1, 3, 5, 7, 9, 11 >;
JOU<> FWFOT = < 2, 4, 6, 8, 10 >;
JOU<> TMJDF; // OPU QSPWJEJOH BDDFTT UP BOZ FMFNFOUT ZFU
TMJDF = PEET<2 .. $ - 2>;
XSJUFMO(TMJDF);
TMJDF = FWFOT<1 .. $ - 1>;
XSJUFMO(TMJDF);
A=JQ@, TMJDF ?J@N IJO KMJQD?@ <>>@NN OJ <IT @G@H@ION RC@I DO DN ?@ADI@?. &O DN OC@I PN@? OJ
KMJQD?@ <>>@NN OJ NJH@ JA OC@ @G@H@ION JA PEET, <I? G<O@M OJ NJH@ JA OC@ @G@H@ION JA FWFOT:
<5, 7>
<4, 6, 8>
(>;57E 3@6 %F:7D ADD3K F73FGD7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
76
Making a slice longer may terminate sharing
19.5
0DI>@ OC@ G@IBOC JA < ADS@?-G@IBOC <MM<T ><IIJO =@ >C<IB@?, OC@ >JI>@KO JA F7D?;@3F;A@ A8
E:3D;@9 DN JIGT <=JPO NGD>@N.
&O DN KJNND=G@ OJ <>>@NN OC@ N<H@ @G@H@ION =T HJM@ OC<I JI@ NGD>@. #JM @S<HKG@, OC@ ADMNO ORJ
JA OC@ @DBCO @G@H@ION =@GJR <M@ =@DIB <>>@NN@? OCMJPBC OCM@@ NGD>@N:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU<> TMJDF = < 1, 3, 5, 7, 9, 11, 13, 15 >;
JOU<> IBMG = TMJDF<0 .. $ / 2>;
JOU<> RVBSUFS = TMJDF<0 .. $ / 4>;
RVBSUFS<1> = 0; // NPEJGZ UISPVHI POF TMJDF
XSJUFMO(RVBSUFS);
XSJUFMO(IBMG);
XSJUFMO(TMJDF);
^
1C@ @AA@>O JA OC@ HJ?DAD><ODJI OJ OC@ N@>JI? @G@H@IO JA RVBSUFS DN N@@I OCMJPBC <GG JA OC@
NGD>@N:
<1, 0>
<1, 0, 5, 7>
<1, 0, 5, 7, 9, 11, 13, 15>
4C@I QD@R@? OCDN R<T, NGD>@N KMJQD?@ E:3D76 <>>@NN OJ @G@H@ION. 1CDN NC<MDIB JK@IN OC@
LP@NODJI JA RC<O C<KK@IN RC@I < I@R @G@H@IO DN <??@? OJ JI@ JA OC@ NGD>@N. 0DI>@ OC@ NGD>@N
KMJQD?@ <>>@NN OJ OC@ N<H@ <>OP<G @G@H@ION, OC@M@ H<T IJO =@ MJJH OJ <?? @G@H@ION OJ < NGD>@
RDOCJPO ?DNOPM=DIB OC@ JOC@MN.
! <INR@MN OCDN LP@NODJI =T O@MHDI<ODIB OC@ NC<MDIB M@G<ODJINCDK DA OC@M@ DN IJ MJJH AJM OC@
I@R @G@H@IO: 1C@ NGD>@ OC<O C<N IJ MJJH OJ BMJR G@<Q@N OC@ NC<MDIB. 4C@I OCDN C<KK@IN, <GG
JA OC@ @SDNODIB @G@H@ION JA OC<O NGD>@ <M@ >JKD@? OJ < I@R KG<>@ <POJH<OD><GGT <I? OC@ NGD>@
NO<MON KMJQD?DIB <>>@NN OJ OC@N@ I@R @G@H@ION.
1J N@@ OCDN DI <>ODJI, G@O'N <?? <I @G@H@IO OJ RVBSUFS =@AJM@ HJ?DATDIB DON N@>JI? @G@H@IO:
RVBSUFS _= 42; // UIJT TMJDF MFBWFT UIF TIBSJOH CFDBVTF
// UIFSF JT OP SPPN GPS UIF OFX FMFNFOU
RVBSUFS<1> = 0; // GPS UIBU SFBTPO UIJT NPEJGJDBUJPO
// EPFT OPU BGGFDU UIF PUIFS TMJDFT
1C@ JPOKPO JA OC@ KMJBM<H NCJRN OC<O OC@ HJ?DAD><ODJI OJ OC@ RVBSUFS NGD>@ ?J@N IJO <AA@>O
OC@ JOC@MN:
<1, 0, 42>
<1, 3, 5, 7>
<1, 3, 5, 7, 9, 11, 13, 15>
"SKGD>DOGT DI>M@<NDIB OC@ G@IBOC JA < NGD>@ H<F@N DO G@<Q@ OC@ NC<MDIB <N R@GG:
++RVBSUFS.MFOHUI; // MFBWFT UIF TIBSJOH
(>;57E 3@6 %F:7D ADD3K F73FGD7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
77
JM
RVBSUFS.MFOHUI += 5; // MFBWFT UIF TIBSJOH
,I OC@ JOC@M C<I?, NCJMO@IDIB < NGD>@ ?J@N IJO <AA@>O NC<MDIB. 0CJMO@IDIB OC@ NGD>@ H@M@GT
H@<IN OC<O OC@ NGD>@ IJR KMJQD?@N <>>@NN OJ G@NN @G@H@ION:
JOU<> B = < 1, 11, 111 >;
JOU<> E = B;
E = E<1 .. $>; // TIPSUFOJOH GSPN UIF CFHJOOJOH
E<0> = 42; // NPEJGZJOH UIF FMFNFOU UISPVHI UIF TMJDF
XSJUFMO(B); // QSJOUJOH UIF PUIFS TMJDF
AN ><I =@ N@@I AMJH OC@ JPOKPO, OC@ HJ?DAD><ODJI OCMJPBC E DN N@@I OCMJPBC B; OC@ NC<MDIB DN
NODGG DI @AA@>O:
<1, 42, 111>
/@?P>DIB OC@ G@IBOC DI ?DAA@M@IO R<TN ?J@N IJO O@MHDI<O@ NC<MDIB @DOC@M:
E = E<0 .. $ - 1>; // TIPSUFOJOH GSPN UIF FOE
--E.MFOHUI; // TBNF UIJOH
E.MFOHUI = E.MFOHUI - 1; // TBNF UIJOH
0C<MDIB JA @G@H@ION DN NODGG DI @AA@>O.
DBQBDJUZ to determine whether sharing will be terminated 19.5.1
1C@M@ <M@ ><N@N RC@I NGD>@N >JIODIP@ NC<MDIB @G@H@ION @Q@I <AO@M <I @G@H@IO DN <??@? OJ JI@
JA OC@H. 1CDN C<KK@IN RC@I OC@ @G@H@IO DN <??@? OJ OC@ GJIB@NO NGD>@ <I? OC@M@ DN MJJH <O OC@
@I? JA DO:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU<> TMJDF = < 1, 3, 5, 7, 9, 11, 13, 15 >;
JOU<> IBMG = TMJDF<0 .. $ / 2>;
JOU<> RVBSUFS = TMJDF<0 .. $ / 4>;
TMJDF _= 42; // BEEJOH UP UIF MPOHFTU TMJDF ...
TMJDF<1> = 0; // ... BOE UIFO NPEJGZJOH BO FMFNFOU
XSJUFMO(RVBSUFS);
XSJUFMO(IBMG);
XSJUFMO(TMJDF);
^
AN N@@I DI OC@ JPOKPO, <GOCJPBC OC@ <??@? @G@H@IO DI>M@<N@N OC@ G@IBOC JA < NGD>@, OC@ NC<MDIB
C<N IJO =@@I O@MHDI<O@?, <I? OC@ HJ?DAD><ODJI DN N@@I OCMJPBC <GG JA OC@ NGD>@N:
<1, 0>
<1, 0, 5, 7>
<1, 0, 5, 7, 9, 11, 13, 15, 42>
(>;57E 3@6 %F:7D ADD3K F73FGD7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
78
1C@ DBQBDJUZ KMJK@MOT JA NGD>@N ?@O@MHDI@N RC@OC@M OC@ NC<MDIB RDGG =@ O@MHDI<O@? DA <I
@G@H@IO DN <??@? OJ < K<MOD>PG<M NGD>@. (DBQBDJUZ DN <>OP<GGT < API>ODJI =PO OCDN ?DNODI>ODJI
?J@N IJO C<Q@ <IT NDBIDAD><I>@ DI OCDN ?DN>PNNDJI.)
DBQBDJUZ C<N ORJ H@<IDIBN:
t 4C@I DON Q<GP@ DN 0, DO H@<IN OC<O OCDN DN IJO OC@ G<MB@NO JMDBDI<G NGD>@. &I OCDN ><N@, <??DIB
< I@R @G@H@IO RJPG? ?@ADIDO@GT M@GJ><O@ OC@ @G@H@ION JA OC@ NGD>@ <I? OC@ NC<MDIB RJPG?
O@MHDI<O@.
t 4C@I DON Q<GP@ DN IJI-U@MJ, DO H@<IN OC<O OCDN DN OC@ G<MB@NO JMDBDI<G NGD>@. &I OCDN ><N@,
DBQBDJUZ ?@IJO@N OC@ OJO<G IPH=@M JA @G@H@ION OC<O OCDN NGD>@ ><I CJG? RDOCJPO I@@?DIB OJ
=@ >JKD@?. 1C@ IPH=@M JA @7I 7>7?7@FE OC<O ><I =@ <??@? OJ OCDN NGD>@ =@AJM@ DO I@@?N OJ
=@ >JKD@? ><I =@ ><G>PG<O@? =T NP=OM<>ODIB OC@ <>OP<G G@IBOC JA OC@ NGD>@ AMJH OC@ ><K<>DOT
Q<GP@. 1C@M@ DN IJ MJJH AJM <IJOC@M @G@H@IO RC@I OC@ G@IBOC JA OC@ NGD>@ @LP<GN DON
><K<>DOT.
A>>JM?DIBGT, < KMJBM<H OC<O I@@?N OJ ?@O@MHDI@ RC@OC@M OC@ NC<MDIB RDGG O@MHDI<O@ NCJPG?
PN@ < GJBD> NDHDG<M OJ OC@ AJGGJRDIB:
JG (TMJDF.DBQBDJUZ == 0) \
/* *UT FMFNFOUT XPVME CF SFMPDBUFE JG POF NPSF FMFNFOU
* JT BEEFE UP UIJT TMJDF. */
// ...
^ FMTF \
/* 5IJT TMJDF NBZ IBWF SPPN GPS OFX FMFNFOUT CFGPSF
* OFFEJOH UP CF SFMPDBUFE. -FU'T DBMDVMBUF IPX
* NBOZ: */
BVUP IPX.BOZ/FX&MFNFOUT = TMJDF.DBQBDJUZ - TMJDF.MFOHUI;
// ...
^
Operations on all of the elements
19.6
1CDN A@<OPM@ DN AJM =JOC ADS@?-G@IBOC <MM<TN <I? NGD>@N.
1C@ <> >C<M<>O@MN RMDOO@I <AO@M OC@ I<H@ JA <I <MM<T H@<IN 3>> A8 F:7 7>7?7@FE. 1CDN A@<OPM@
NDHKGDAD@N OC@ KMJBM<H RC@I >@MO<DI JK@M<ODJIN I@@? OJ =@ <KKGD@? OJ <GG JA OC@ @G@H@ION JA <I
<MM<T.
Note: ):7 D 5A?B;>7D F:3F ! :3H7 GE76 I:7@ ID;F;@9 F:;E 5:3BF7D (6?6 2.056) 6A7E @AF
EGBBADF F:;E 873FGD7 8G>>K. FAD F:3F D73EA@ ! :3H7 GE76 A@>K 8;J76->7@9F: 3DD3KE ;@ EA?7 A8 F:7
8A>>AI;@9 7J3?B>7E.
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
EPVCMF<3> B = < 10, 20, 30 >;
EPVCMF<3> C = < 2, 3, 4 >;
EPVCMF<3> SFTVMU = B<> + C<>;
XSJUFMO(SFTVMU);
^
1C@ JPOKPO:
(>;57E 3@6 %F:7D ADD3K F73FGD7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
79
<12, 23, 34>
1C@ <??DODJI JK@M<ODJI DI OC<O KMJBM<H DN <KKGD@? OJ OC@ >JMM@NKJI?DIB @G@H@ION JA =JOC JA
OC@ <MM<TN DI JM?@M: #DMNO OC@ ADMNO @G@H@ION <M@ <??@?, OC@I OC@ N@>JI? @G@H@ION <M@ <??@?,
@O>. A I<OPM<G M@LPDM@H@IO DN OC<O OC@ G@IBOCN JA OC@ ORJ <MM<TN HPNO =@ @LP<G.
1C@ JK@M<OJM ><I =@ JI@ JA OC@ <MDOCH@OD> JK@M<OJMN +, -, *, /, %, <I? ??; JI@ JA OC@ =DI<MT
JK@M<OJMN ?, &, <I? ]; <N R@GG <N OC@ PI<MT JK@M<OJMN - <I? _ OC<O <M@ OTK@? DI AMJIO JA <I
<MM<T. 4@ RDGG N@@ NJH@ JA OC@N@ JK@M<OJMN DI G<O@M >C<KO@MN.
1C@ <NNDBIH@IO Q@MNDJIN JA OC@N@ JK@M<OJMN ><I <GNJ =@ PN@?: =, +=, -=, *=, /=, %=, ??=, ?=,
&=, <I? ]=.
1CDN A@<OPM@ DN IJO JIGT =@OR@@I ORJ <MM<TN; DI <??DODJI OJ <I <MM<T, <I @SKM@NNDJI OC<O DN
>JHK<OD=G@ RDOC OC@ @G@H@ION ><I <GNJ =@ PN@?. #JM @S<HKG@, OC@ AJGGJRDIB JK@M<ODJI ?DQD?@N
<GG JA OC@ @G@H@ION JA <I <MM<T =T AJPM:
EPVCMF<3> B = < 10, 20, 30 >;
B<> /= 4;
XSJUFMO(B);
1C@ JPOKPO:
<2.5, 5, 7.5>
1J <NNDBI <GG JA OC@ @G@H@ION OJ < NK@>DAD> Q<GP@:
B<> = 42;
XSJUFMO(B);
1C@ JPOKPO:
<42, 42, 42>
1CDN A@<OPM@ M@LPDM@N BM@<O <OO@IODJI RC@I PN@? RDOC NGD>@N. AGOCJPBC OC@M@ DN IJ <KK<M@IO
?DAA@M@I>@ DI @G@H@IO Q<GP@N, OC@ AJGGJRDIB ORJ @SKM@NNDJIN C<Q@ Q@MT ?DAA@M@IO H@<IDIBN:
TMJDF2 = TMJDF1; // j TMJDF2 TUBSUT QSPWJEJOH BDDFTT
// UP UIF TBNF FMFNFOUT UIBU
// TMJDF1 QSPWJEFT BDDFTT UP
TMJDF3<> = TMJDF1; // j UIF WBMVFT PG UIF FMFNFOUT PG
// TMJDF3 DIBOHF
1C@ <NNDBIH@IO JA TMJDF2 H<F@N DO NC<M@ OC@ N<H@ @G@H@ION <N TMJDF1. ,I OC@ JOC@M C<I?,
<N TMJDF3<> H@<IN 3>> 7>7?7@FE A8 slice3, OC@ Q<GP@N JA DON @G@H@ION =@>JH@ OC@ N<H@ <N
OC@ Q<GP@N JA OC@ @G@H@ION JA TMJDF1. 1C@ @AA@>O JA KM@N@I>@ JM <=N@I>@ JA OC@ <> >C<M<>O@MN
><IIJO =@ DBIJM@?.
4@ ><I N@@ <I @S<HKG@ JA OCDN ?DAA@M@I>@ DI OC@ AJGGJRDIB KMJBM<H:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
EPVCMF<> TMJDF1 = < 1, 1, 1 >;
EPVCMF<> TMJDF2 = < 2, 2, 2 >;
EPVCMF<> TMJDF3 = < 3, 3, 3 >;
(>;57E 3@6 %F:7D ADD3K F73FGD7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
80
TMJDF2 = TMJDF1; // j TMJDF2 TUBSUT QSPWJEJOH BDDFTT
// UP UIF TBNF FMFNFOUT UIBU
// TMJDF1 QSPWJEFT BDDFTT UP
TMJDF3<> = TMJDF1; // j UIF WBMVFT PG UIF FMFNFOUT PG
// TMJDF3 DIBOHF
XSJUFMO("TMJDF1 CFGPSF: ", TMJDF1);
XSJUFMO("TMJDF2 CFGPSF: ", TMJDF2);
XSJUFMO("TMJDF3 CFGPSF: ", TMJDF3);
TMJDF2<0> = 42; // j UIF WBMVF PG BO FMFNFOU UIBU
// JU TIBSFT XJUI TMJDF1 DIBOHFT
TMJDF3<0> = 43; // j UIF WBMVF PG BO FMFNFOU UIBU
// POMZ JU QSPWJEFT BDDFTT UP
// DIBOHFT
XSJUFMO("TMJDF1 BGUFS : ", TMJDF1);
XSJUFMO("TMJDF2 BGUFS : ", TMJDF2);
XSJUFMO("TMJDF3 BGUFS : ", TMJDF3);
^
1C@ HJ?DAD><ODJI OCMJPBC TMJDF2 <AA@>ON TMJDF1 OJJ:
TMJDF1 CFGPSF: <1, 1, 1>
TMJDF2 CFGPSF: <1, 1, 1>
TMJDF3 CFGPSF: <1, 1, 1>
TMJDF1 BGUFS : <42, 1, 1>
TMJDF2 BGUFS : <42, 1, 1>
TMJDF3 BGUFS : <43, 1, 1>
1C@ ?<IB@M C@M@ DN OC<O OC@ KJO@IOD<G =PB H<T IJO =@ IJOD>@? PIODG <AO@M OC@ Q<GP@ JA < NC<M@?
@G@H@IO DN >C<IB@?.
Multi-dimensional arrays
19.7
0J A<M R@ C<Q@ PN@? <MM<TN RDOC JIGT API?<H@IO<G OTK@N GDF@ JOU <I? EPVCMF. 1C@ @G@H@IO
OTK@ ><I <>OP<GGT =@ <IT JOC@M OTK@, DI>GP?DIB JOC@M <MM<TN. 1CDN @I<=G@N OC@ KMJBM<HH@M OJ
?@ADI@ >JHKG@S >JIO<DI@MN GDF@ 3DD3K A8 3DD3KE. AMM<TN JA <MM<TN <M@ ><GG@? ?G>F;-6;?7@E;A@3>
3DD3KE.
AGG JA OC@ @G@H@ION JA <GG JA OC@ <MM<TN OC<O R@ C<Q@ ?@ADI@? NJ A<M C<Q@ =@@I RMDOO@I DI OC@
NJPM>@ >J?@ AMJH G@AO OJ MDBCO. 1J C@GK PN PI?@MNO<I? OC@ >JI>@KO JA < ORJ-?DH@INDJI<G <MM<T,
G@O'N ?@ADI@ <I <MM<T AMJH OJK OJ =JOOJH OCDN ODH@:
JOU<> BSSBZ = <
10,
20,
30,
40
>;
AN TJP M@H@H=@M, HJNO NK<>@N DI OC@ NJPM>@ >J?@ <M@ OJ C@GK RDOC M@<?<=DGDOT <I? ?J IJO
>C<IB@ OC@ H@<IDIB JA OC@ >J?@. 1C@ <MM<T <=JQ@ >JPG? C<Q@ =@@I ?@ADI@? JI < NDIBG@ GDI@
<I? RJPG? C<Q@ OC@ N<H@ H@<IDIB.
)@O'N IJR M@KG<>@ @Q@MT @G@H@IO JA OC<O <MM<T RDOC <IJOC@M <MM<T:
(>;57E 3@6 %F:7D ADD3K F73FGD7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
81
/* ... */ BSSBZ = <
< 10, 11, 12 >,
< 20, 21, 22 >,
< 30, 31, 32 >,
< 40, 41, 42 >
>;
4@ C<Q@ M@KG<>@? @G@H@ION JA OTK@ JOU RDOC @G@H@ION JA OTK@ JOU<>. 1J H<F@ OC@ >J?@
>JIAJMH OJ <MM<T ?@ADIDODJI NTIO<S, R@ HPNO IJR NK@>DAT OC@ OTK@ JA OC@ @G@H@ION <N JOU<>
DINO@<? JA JOU:
JOU<><> BSSBZ = <
< 10, 11, 12 >,
< 20, 21, 22 >,
< 30, 31, 32 >,
< 40, 41, 42 >
>;
0P>C <MM<TN <M@ ><GG@? FIA-6;?7@E;A@3> 3DD3KE =@><PN@ OC@T ><I =@ N@@I <N C<QDIB MJRN <I?
>JGPHIN.
1RJ-?DH@INDJI<G <MM<TN <M@ PN@? OC@ N<H@ R<T <N <IT JOC@M <MM<T <N GJIB <N R@ M@H@H=@M
OC<O @<>C @G@H@IO DN <I <MM<T DON@GA <I? DN PN@? DI <MM<T JK@M<ODJIN:
BSSBZ _= < 50, 51 >; // BEET B OFX FMFNFOU (J.F. B TMJDF)
BSSBZ<0> _= 13; // BEET UP UIF GJSTU FMFNFOU
1C@ I@R NO<O@ JA OC@ <MM<T:
<<10, 11, 12, 13>, <20, 21, 22>, <30, 31, 32>, <40, 41, 42>, <50, 51>>
AMM<TN <I? @G@H@ION ><I =@ ADS@?-G@IBOC <N R@GG. 1C@ AJGGJRDIB DN < OCM@@-?DH@INDJI<G <MM<T
RC@M@ <GG ?DH@INDJIN <M@ ADS@?-G@IBOC:
JOU<2><3><4> BSSBZ; // 2 DPMVNOT, 3 SPXT, 4 QBHFT
1C@ ?@ADIDODJI <=JQ@ ><I =@ N@@I <N 8AGD B397E A8 F:D77 DAIE A8 FIA 5A>G?@E. AN <I @S<HKG@,
NP>C <I <MM<T ><I =@ PN@? OJ M@KM@N@IO < 4-NOJMT =PDG?DIB DI <I <?Q@IOPM@ B<H@, @<>C NOJMT
>JINDNODIB JA 2S3=6 MJJHN.
#JM @S<HKG@, OC@ IPH=@M JA DO@HN DI OC@ ADMNO MJJH JA OC@ N@>JI? AGJJM ><I =@ DI>M@H@IO@?
GDF@ OC@ AJGGJRDIB:
// 5IF JOEFY PG UIF TFDPOE GMPPS JT 1, BOE UIF GJSTU SPPN
// PG UIBU GMPPS JT BDDFTTFE CZ <0><0>
++JUFN$PVOUT<1><0><0>;
&I <??DODJI OJ OC@ NTIO<S <=JQ@, OC@ OFX @SKM@NNDJI ><I <GNJ =@ PN@? OJ >M@<O@ < E>;57 A8
E>;57E. 1C@ AJGGJRDIB @S<HKG@ PN@N JIGT ORJ ?DH@INDJIN:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU<><> T = OFX JOU<><>(2, 3);
XSJUFMO(T);
^
(>;57E 3@6 %F:7D ADD3K F73FGD7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
82
1C@ OFX @SKM@NNDJI <=JQ@ >M@<O@N 2 NGD>@N >JIO<DIDIB 3 @G@H@ION @<>C <I? M@OPMIN < NGD>@ OC<O
KMJQD?@N <>>@NN OJ OCJN@ NGD>@N <I? @G@H@ION. 1C@ JPOKPO:
<<0, 0, 0>, <0, 0, 0>>
Summary
19.8
t #DS@?-G@IBOC <MM<TN JRI OC@DM @G@H@ION; NGD>@N KMJQD?@ <>>@NN OJ @G@H@ION OC<O ?JI'O
=@GJIB OJ OC@H.
t 4DOCDI OC@ <> JK@M<OJM, $ DN OC@ @LPDQ<G@IO JA array_name.MFOHUI.
t .EVQ H<F@N < I@R <MM<T OC<O >JINDNON JA OC@ >JKD@N JA OC@ @G@H@ION JA <I @SDNODIB <MM<T.
t 4DOC ADS@?-G@IBOC <MM<TN, OC@ <NNDBIH@IO JK@M<ODJI >C<IB@N OC@ Q<GP@N JA @G@H@ION; RDOC
NGD>@N, DO H<F@N OC@ NGD>@N NO<MO KMJQD?DIB <>>@NN OJ JOC@M @G@H@ION.
t 0GD>@N OC<O B@O GJIB@M ?3K NOJK NC<MDIB @G@H@ION <I? NO<MO KMJQD?DIB <>>@NN OJ I@RGT
>JKD@? @G@H@ION. DBQBDJUZ ?@O@MHDI@N RC@OC@M OCDN RDGG =@ OC@ ><N@.
t 1C@ NTIO<S BSSBZ<> H@<IN 3>> A8 F:7 7>7?7@FE; OC@ JK@M<ODJI OC<O DN <KKGD@? OJ DO DN <KKGD@?
OJ @<>C @G@H@IO DI?DQD?P<GGT.
t AMM<TN JA <MM<TN <M@ ><GG@? HPGOD-?DH@INDJI<G <MM<TN.
Exercise
19.9
t &O@M<O@ JQ@M OC@ @G@H@ION JA <I <MM<T JA EPVCMFN <I? C<GQ@ OC@ JI@N OC<O <M@ BM@<O@M OC<I
10. #JM @S<HKG@, BDQ@I OC@ AJGGJRDIB <MM<T:
EPVCMF<> BSSBZ = < 1, 20, 2, 30, 7, 11 >;
*J?DAT DO <N OC@ AJGGJRDIB:
<1, 10, 2, 15, 7, 5.5>
AGOCJPBC OC@M@ <M@ H<IT NJGPODJIN JA OCDN KMJ=G@H, OMT OJ PN@ JIGT OC@ A@<OPM@N JA
NGD>@N. 6JP ><I NO<MO RDOC < NGD>@ OC<O KMJQD?@N <>>@NN OJ <GG JA OC@ @G@H@ION. 1C@I TJP
><I NCJMO@I OC@ NGD>@ AMJH OC@ =@BDIIDIB <I? <GR<TN PN@ OC@ ADMNO @G@H@IO.
1C@ AJGGJRDIB @SKM@NNDJI NCJMO@IN OC@ NGD>@ AMJH OC@ =@BDIIDIB:
TMJDF = TMJDF<1 .. $>;
... F:7 EA>GF;A@
(>;57E 3@6 %F:7D ADD3K F73FGD7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
83
Strings 20
4@ C<Q@ PN@? NOMDIBN DI H<IT KMJBM<HN OC<O R@ C<Q@ N@@I NJ A<M. 0OMDIBN <M@ < >JH=DI<ODJI
JA OC@ ORJ A@<OPM@N OC<O R@ C<Q@ >JQ@M@? DI OC@ G<NO OCM@@ >C<KO@MN: >C<M<>O@MN <I? <MM<TN. &I
OC@ NDHKG@NO ?@ADIDODJI, NOMDIBN <M@ IJOCDIB =PO <MM<TN JA >C<M<>O@MN. #JM @S<HKG@, DIBS<> DN <
OTK@ JA NOMDIB.
1CDN NDHKG@ ?@ADIDODJI H<T =@ HDNG@<?DIB. AN R@ C<Q@ N@@I DI OC@ C<M<>O@MN >C<KO@M, ! C<N
OCM@@ N@K<M<O@ >C<M<>O@M OTK@N. AMM<TN JA OC@N@ >C<M<>O@M OTK@N G@<? OJ OCM@@ N@K<M<O@ NOMDIB
OTK@N, NJH@ JA RCD>C H<T C<Q@ NPMKMDNDIB JPO>JH@N DI NJH@ NOMDIB JK@M<ODJIN.
SFBEMO and DIPNQ, instead of SFBEG
20.1
1C@M@ <M@ NPMKMDN@N @Q@I RC@I M@<?DIB NOMDIBN AMJH OC@ >JINJG@.
B@DIB >C<M<>O@M <MM<TN, NOMDIBN ><I >JIO<DI >JIOMJG >C<M<>O@MN GDF@ '=O' <N R@GG. 4C@I
M@<?DIB NOMDIBN AMJH OC@ DIKPO, OC@ >JIOMJG >C<M<>O@M OC<O >JMM@NKJI?N OJ OC@ "IO@M F@T OC<O DN
KM@NN@? <O OC@ @I? JA >JINJG@ DIKPO =@>JH@N < K<MO JA OC@ NOMDIB <N R@GG. #PMOC@M, =@><PN@
OC@M@ DN IJ R<T OJ O@GG SFBEG() CJR H<IT >C<M<>O@MN OJ M@<?, DO >JIODIP@N OJ M@<? PIODG OC@
@I? JA OC@ @IODM@ DIKPO. #JM OC@N@ M@<NJIN, SFBEG() ?J@N IJO RJMF <N DIO@I?@? RC@I M@<?DIB
NOMDIBN:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
DIBS<> OBNF;
XSJUF("8IBU JT ZPVS OBNF ");
SFBEG(" %T", &OBNF);
XSJUFMO(")FMMP ", OBNF, "!");
^
1C@ "IO@M F@T OC<O OC@ PN@M KM@NN@N <AO@M OC@ I<H@ ?J@N IJO O@MHDI<O@ OC@ DIKPO. SFBEG()
>JIODIP@N OJ R<DO AJM HJM@ >C<M<>O@MN OJ <?? OJ OC@ NOMDIB:
8IBU JT ZPVS OBNF .FSU
l 5IF JOQVU JT OPU UFSNJOBUFE BMUIPVHI &OUFS IBT CFFO QSFTTFE
l (-FU'T BTTVNF UIBU &OUFS JT QSFTTFE B TFDPOE UJNF IFSF)
,I@ R<T JA O@MHDI<ODIB OC@ NO<I?<M? DIKPO NOM@<H DI < >JINJG@ DN KM@NNDIB OMG-! PI?@M 2IDS-
=<N@? NTNO@HN <I? OMG-7 PI?@M 4DI?JRN NTNO@HN. &A OC@ PN@M @Q@IOP<GGT O@MHDI<O@N OC@ DIKPO
OC<O R<T, R@ N@@ OC<O OC@ I@R-GDI@ >C<M<>O@MN C<Q@ =@@I M@<? <N K<MON JA OC@ NOMDIB <N R@GG:
)FMMP .FSU
l OFX-MJOF DIBSBDUFS BGUFS UIF OBNF
! l (POF NPSF CFGPSF UIF FYDMBNBUJPO NBSL)
1C@ @S>G<H<ODJI H<MF <KK@<MN <AO@M OCJN@ >C<M<>O@MN DINO@<? JA =@DIB KMDIO@? MDBCO <AO@M OC@
I<H@.
SFBEMO() DN HJM@ NPDO<=G@ RC@I M@<?DIB NOMDIBN. 0CJMO AJM "M@<? GDI@", SFBEMO() M@<?N PIODG
OC@ @I? JA OC@ GDI@. &O DN PN@? ?DAA@M@IOGT =@><PN@ OC@ " %T" AJMH<O NOMDIB <I? OC@ & JK@M<OJM
<M@ IJO I@@?@?:
(FD;@9E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
84
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
DIBS<> OBNF;
XSJUF("8IBU JT ZPVS OBNF ");
SFBEMO(OBNF);
XSJUFMO(")FMMP ", OBNF, "!");
^
SFBEMO() NOJM@N OC@ I@R-GDI@ >C<M<>O@M <N R@GG. 1CDN DN NJ OC<O OC@ KMJBM<H C<N < R<T JA
?@O@MHDIDIB RC@OC@M OC@ DIKPO >JINDNO@? JA < >JHKG@O@ GDI@ JM RC@OC@M OC@ @I? JA DIKPO C<N
=@@I M@<>C@?:
8IBU JT ZPVS OBNF .FSU
)FMMP .FSU
! l OFX-MJOF DIBSBDUFS CFGPSF UIF FYDMBNBUJPO NBSL
0P>C >JIOMJG >C<M<>O@MN OC<O <M@ <O OC@ @I?N JA NOMDIBN ><I =@ M@HJQ@? =T
TUE.TUSJOH.DIPNQ:
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
WPJE NBJO()
\
DIBS<> OBNF;
XSJUF("8IBU JT ZPVS OBNF ");
SFBEMO(OBNF);
OBNF = DIPNQ(OBNF);
XSJUFMO(")FMMP ", OBNF, "!");
^
1C@ DIPNQ() @SKM@NNDJI <=JQ@ M@OPMIN < I@R NOMDIB OC<O ?J@N IJO >JIO<DI OC@ OM<DGDIB >JIOMJG
>C<M<>O@MN. ANNDBIDIB OC<O M@OPMI Q<GP@ =<>F OJ OBNF KMJ?P>@N OC@ DIO@I?@? JPOKPO:
8IBU JT ZPVS OBNF .FSU
)FMMP .FSU! l OP OFX-MJOF DIBSBDUFS
SFBEMO() ><I =@ PN@? RDOCJPO < K<M<H@O@M. &I OC<O ><N@ DO D7FGD@E OC@ GDI@ OC<O DO C<N EPNO
M@<?. C<DIDIB OC@ M@NPGO JA SFBEMO() OJ DIPNQ() @I<=G@N < NCJMO@M <I? HJM@ M@<?<=G@
NTIO<S:
TUSJOH OBNF = DIPNQ(SFBEMO());
& RDGG NO<MO PNDIB OC<O AJMH <AO@M DIOMJ?P>DIB OC@ TUSJOH OTK@ =@GJR.
Double quotes, not single quotes
20.2
4@ C<Q@ N@@I OC<O NDIBG@ LPJO@N <M@ PN@? OJ ?@ADI@ >C<M<>O@M GDO@M<GN. 0OMDIB GDO@M<GN <M@
?@ADI@? RDOC ?JP=G@ LPJO@N. 'B' DN < >C<M<>O@M; "B" DN < NOMDIB OC<O >JIO<DIN < NDIBG@
>C<M<>O@M.
(FD;@9E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
85
TUSJOH, XTUSJOH, and ETUSJOH are immutable
20.3
1C@M@ <M@ OCM@@ NOMDIB OTK@N OC<O >JMM@NKJI? OJ OC@ OCM@@ >C<M<>O@M OTK@N: DIBS<>, XDIBS<>,
<I? EDIBS<>.
1C@M@ <M@ OCM@@ 3>;3E7E JA OC@ ;??GF34>7 Q@MNDJIN JA OCJN@ OTK@N: TUSJOH, XTUSJOH, <I?
ETUSJOH. 1C@ >C<M<>O@MN JA OC@ Q<MD<=G@N OC<O <M@ ?@ADI@? =T OC@N@ <GD<N@N ><IIJO =@
HJ?DAD@?. #JM @S<HKG@, OC@ >C<M<>O@MN JA < XDIBS<> ><I =@ HJ?DAD@? =PO OC@ >C<M<>O@MN JA <
XTUSJOH ><IIJO =@ HJ?DAD@?. (4@ RDGG N@@ !'N ;??GF34;>;FK >JI>@KO DI G<O@M >C<KO@MN.)
#JM @S<HKG@, OC@ AJGGJRDIB >J?@ OC<O OMD@N OJ ><KDO<GDU@ OC@ ADMNO G@OO@M JA < TUSJOH RJPG?
><PN@ < >JHKDG<ODJI @MMJM:
TUSJOH DBO/PU#F.VUBUFE = "IFMMP";
DBO/PU#F.VUBUFE<0> = ')'; // j DPNQJMBUJPO &3303
4@ H<T OCDIF JA ?@ADIDIB OC@ Q<MD<=G@ <N < DIBS<> DINO@<? JA OC@ TUSJOH <GD<N =PO OC<O ><IIJO
=@ >JHKDG@? @DOC@M:
DIBS<> B@TMJDF = "IFMMP"; // j DPNQJMBUJPO &3303
1CDN ODH@ OC@ >JHKDG<ODJI @MMJM DN ?P@ OJ OC@ >JH=DI<ODJI JA ORJ A<>OJMN:
1. 1C@ OTK@ JA NOMDIB GDO@M<GN GDF@ "IFMMP" DN TUSJOH, IJO DIBS<>, NJ OC@T <M@ DHHPO<=G@.
2. 1C@ DIBS<> JI OC@ G@AO-C<I? ND?@ DN < NGD>@, RCD>C, DA OC@ >J?@ >JHKDG@?, RJPG? KMJQD?@
<>>@NN OJ <GG JA OC@ >C<M<>O@MN JA OC@ MDBCO-C<I? ND?@.
0DI>@ DIBS<> DN HPO<=G@ <I? TUSJOH DN IJO, OC@M@ DN < HDNH<O>C. 1C@ >JHKDG@M ?J@N IJO <GGJR
<>>@NNDIB >C<M<>O@MN JA <I DHHPO<=G@ <MM<T =T < HPO<=G@ NGD>@.
1C@ NJGPODJI C@M@ DN OJ O<F@ < >JKT JA OC@ DHHPO<=G@ NOMDIB =T OC@ .EVQ KMJK@MOT:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
DIBS<> T = "IFMMP".EVQ;
T<0> = ')';
XSJUFMO(T);
^
1C@ KMJBM<H ><I IJR =@ >JHKDG@? <I? KMDIO OC@ HJ?DAD@? NOMDIB:
)FMMP
0DHDG<MGT, DIBS<> ><IIJO =@ PN@? RC@M@ < TUSJOH DN I@@?@?. &I NP>C ><N@N, OC@ .JEVQ
KMJK@MOT ><I =@ PN@? AJM KMJ?P>DIB <I DHHPO<=G@ TUSJOH Q<MD<=G@ AMJH < HPO<=G@ DIBS<>
Q<MD<=G@. #JM @S<HKG@, DA T DN < Q<MD<=G@ JA OTK@ DIBS<>, OC@ AJGGJRDIB GDI@ ><IIJO =@ >JHKDG@?:
TUSJOH SFTVMU = T _ '.'; // j DPNQJMBUJPO &3303
4C@I OC@ OTK@ JA T DN DIBS<>, OC@ OTK@ JA OC@ @SKM@NNDJI JI OC@ MDBCO-C<I? ND?@ JA OC@
<NNDBIH@IO <=JQ@ DN DIBS<> <N R@GG. .JEVQ DN PN@? AJM KMJ?P>DIB DHHPO<=G@ NOMDIBN AMJH
@SDNODIB NOMDIBN:
TUSJOH SFTVMU = (T _ '.').JEVQ; // j OPX DPNQJMFT
(FD;@9E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
86
Potentially confusing length of strings
20.4
4@ C<Q@ N@@I OC<O NJH@ 2ID>J?@ >C<M<>O@MN <M@ M@KM@N@IO@? =T HJM@ OC<I JI@ =TO@. #JM
@S<HKG@, OC@ G@OO@M ] DN M@KM@N@IO@? =T ORJ =TO@N. 1CDN A<>O DN M@AG@>O@? DI OC@ .MFOHUI
KMJK@MOT JA NOMDIBN:
XSJUFMO("SaTVNa".MFOHUI);
AGOCJPBC "M]NPH]" >JIO<DIN NDS >7FF7DE, OC@ G@IBOC JA OC@ NOMDIB DN OC@ IPH=@M JA 5:3D35F7DE
OC<O DO >JIO<DIN:
8
1C@ OTK@ JA OC@ @G@H@ION JA NOMDIB GDO@M<GN GDF@ "IFMMP" DN DIBS <I? DIBS M@KM@N@ION < 21#-8
>J?@ PIDO. A KMJ=G@H OC<O OCDN H<T ><PN@ DN RC@I R@ OMT OJ M@KG<>@ < ORJ->J?@-PIDO G@OO@M RDOC
< NDIBG@->J?@-PIDO G@OO@M:
DIBS<> T = "SaTVNa".EVQ;
XSJUFMO("#FGPSF: ", T);
T<1> = 'F';
T<5> = 'F';
XSJUFMO(""GUFS : ", T);
1C@ ORJ '@' >C<M<>O@MN ?J IJO M@KG<>@ OC@ ORJ G@OO@MN ]; OC@T M@KG<>@ NDIBG@ >J?@ PIDON,
M@NPGODIB DI <I DI>JMM@>O 21#-8 @I>J?DIB:
#FGPSF: SbTVNb
"GUFS : SF{TVFb l */$033&$5
4C@I ?@<GDIB RDOC G@OO@MN, NTH=JGN, <I? JOC@M 2ID>J?@ >C<M<>O@MN ?DM@>OGT <N DI OC@ >J?@
<=JQ@, OC@ >JMM@>O OTK@ OJ PN@ DN EDIBS:
EDIBS<> T = "SaTVNa"E.EVQ;
XSJUFMO("#FGPSF: ", T);
T<1> = 'F';
T<5> = 'F';
XSJUFMO(""GUFS : ", T);
1C@ JPOKPO:
#FGPSF: SbTVNb
"GUFS : SFTVNF
-G@<N@ IJO@ OC@ ORJ ?DAA@M@I>@N DI OC@ I@R >J?@:
1. 1C@ OTK@ JA OC@ NOMDIB DN EDIBS<>.
2. 1C@M@ DN < E <O OC@ @I? JA OC@ GDO@M<G "SaTVNa"E, NK@>DATDIB DON OTK@ <N <I <MM<T JA EDIBSN.
String literals
20.5
1C@ JKODJI<G >C<M<>O@M OC<O DN NK@>DAD@? <AO@M NOMDIB GDO@M<GN ?@O@MHDI@N OC@ OTK@ JA OC@
@G@H@ION JA OC@ NOMDIB:
JNQPSU TUE.TUEJP;
WPJE NBJO()
(FD;@9E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
87
\
TUSJOH T = "SaTVNa"D; // TBNF BT "SaTVNa"
XTUSJOH X = "SaTVNa"X;
ETUSJOH E = "SaTVNa"E;
XSJUFMO(T.MFOHUI);
XSJUFMO(X.MFOHUI);
XSJUFMO(E.MFOHUI);
^
1C@ JPOKPO:
8
6
6
B@><PN@ <GG JA OC@ G@OO@MN JA "M]NPH]" ><I =@ M@KM@N@IO@? =T < NDIBG@ XDIBS JM EDIBS, OC@ G<NO
ORJ G@IBOCN <M@ @LP<G OJ OC@ IPH=@M JA G@OO@MN.
String concatenation
20.6
0DI>@ OC@T <M@ <>OP<GGT <MM<TN, <GG JA OC@ <MM<T JK@M<ODJIN ><I =@ <KKGD@? OJ NOMDIBN <N R@GG. _
>JI><O@I<O@N ORJ NOMDIBN <I? _= <KK@I?N OJ <I @SDNODIB NOMDIB:
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
WPJE NBJO()
\
XSJUF("8IBU JT ZPVS OBNF ");
TUSJOH OBNF = DIPNQ(SFBEMO());
// $PODBUFOBUF:
TUSJOH HSFFUJOH = ")FMMP " _ OBNF;
// "QQFOE:
HSFFUJOH _= "! 8FMDPNF...";
XSJUFMO(HSFFUJOH);
^
1C@ JPOKPO:
8IBU JT ZPVS OBNF $BO
)FMMP $BO! 8FMDPNF...
Comparing strings
20.7
Note: *@;5A67 6A7E @AF 678;@7 :AI F:7 5:3D35F7DE 3D7 AD67D76 AF:7D F:3@ F:7;D *@;5A67
5A67E. FAD F:3F D73EA@, KAG ?3K 97F D7EG>FE F:3F 6A@'F ?3F5: I;F: KAGD 7JB75F3F;A@E 47>AI.
,:7@ 3>B:347F;53> AD67D;@9 ;E ;?BADF3@F, KAG ?3K I3@F FA GE7 3 >;4D3DK >;=7 FD;>7D; F:3F
EGBBADFE F:7 5A@57BF A8 3@ 3>B:347F.
4@ C<Q@ PN@? >JHK<MDNJI JK@M<OJMN <, >=, @O>. RDOC DIO@B@M <I? AGJ<ODIB KJDIO Q<GP@N =@AJM@.
1C@ N<H@ JK@M<OJMN ><I =@ PN@? RDOC NOMDIBN <N R@GG, =PO RDOC < ?DAA@M@IO H@<IDIB: NOMDIBN
<M@ JM?@M@? >7J;5A9D3B:;53>>K. 1CDN JM?@MDIB O<F@N @<>C >C<M<>O@M'N 2ID>J?@ >J?@ OJ =@ DON
KG<>@ DI < CTKJOC@OD><G BM<I? 2ID>J?@ <GKC<=@O. 1C@ >JI>@KON JA >7EE <I? 9D73F7D <M@
M@KG<>@? RDOC 478AD7 <I? 38F7D DI OCDN CTKJOC@OD><G <GKC<=@O:
(FD;@9E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
88
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
WPJE NBJO()
\
XSJUF(" &OUFS B TUSJOH: ");
TUSJOH T1 = DIPNQ(SFBEMO());
XSJUF("&OUFS BOPUIFS TUSJOH: ");
TUSJOH T2 = DIPNQ(SFBEMO());
JG (T1 == T2) \
XSJUFMO("5IFZ BSF UIF TBNF!");
^ FMTF \
TUSJOH GPSNFS;
TUSJOH MBUUFS;
JG (T1 < T2) \
GPSNFS = T1;
MBUUFS = T2;
^ FMTF \
GPSNFS = T2;
MBUUFS = T1;
^
XSJUFMO("'", GPSNFS, "' DPNFT CFGPSF '", MBUUFS, "'.");
^
^
B@><PN@ 2ID>J?@ <?JKON OC@ G@OO@MN JA OC@ =<ND> )<ODI <GKC<=@O AMJH OC@ A0&& O<=G@, OC@
NOMDIBN OC<O >JIO<DI JIGT OC@ G@OO@MN JA OC@ A0&& O<=G@ <KK@<M OJ =@ JM?@M@? >JMM@>OGT.
Lowercase and uppercase are different
20.8
B@><PN@ @<>C G@OO@M C<N < PIDLP@ >J?@, @Q@MT G@OO@M DN ?DAA@M@IO AMJH @Q@MT JOC@M. #JM
@S<HKG@, 'A' <I? '<' <M@ ?DAA@M@IO G@OO@MN.
A??DODJI<GGT, <N < >JIN@LP@I>@ JA OC@DM A0&& >J?@ Q<GP@N, <GG JA OC@ PKK@M><N@ G@OO@MN <M@
NJMO@? =@AJM@ <GG JA OC@ GJR@M><N@ G@OO@MN. #JM @S<HKG@, 'B' DN =@AJM@ '<'. 1C@ JDNQ() API>ODJI
JA OC@ TUE.TUSJOH HJ?PG@ ><I =@ PN@? RC@I NOMDIBN I@@? OJ =@ >JHK<M@? M@B<M?G@NN JA
GJR@M><N@ <I? PKK@M><N@. 6JP ><I N@@ OC@ API>ODJIN JA OCDN HJ?PG@ <O DON JIGDI@
?J>PH@IO<ODJI.
B@><PN@ NOMDIBN <M@ <MM<TN (<I? <N < >JMJGG<MT, D3@97E), OC@ API>ODJIN JA OC@ TUE.BSSBZ,
TUE.BMHPSJUIN, <I? TUE.SBOHF HJ?PG@N <M@ Q@MT PN@APG RDOC NOMDIBN <N R@GG.
Exercises
20.9
1. BMJRN@ OC@ ?J>PH@IO<ODJIN JA OC@ TUE.TUSJOH, TUE.BSSBZ, TUE.BMHPSJUIN, <I?
TUE.SBOHF HJ?PG@N.
2. 4MDO@ < KMJBM<H OC<O H<F@N PN@ JA OC@ _ JK@M<OJM: 1C@ PN@M @IO@MN OC@ ADMNO I<H@ <I? OC@
G<NO I<H@, <GG DI GJR@M><N@ G@OO@MN. -MJ?P>@ OC@ APGG I<H@ OC<O >JIO<DIN OC@ KMJK@M
><KDO<GDU<ODJI JA OC@ ADMNO <I? G<NO I<H@N. #JM @S<HKG@, RC@I OC@ NOMDIBN <M@ "@=MP" <I?
"?JH<O@N" OC@ KMJBM<H NCJPG? KMDIO ""=MP !JH<O@N".
3. /@<? < GDI@ AMJH OC@ DIKPO <I? KMDIO OC@ K<MO =@OR@@I OC@ ADMNO <I? G<NO '@' G@OO@MN JA OC@
GDI@. #JM @S<HKG@, RC@I OC@ GDI@ DN "OCDN GDI@ C<N ADQ@ RJM?N" OC@ KMJBM<H NCJPG? KMDIO "@
C<N ADQ@".
(FD;@9E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
89
6JP H<T ADI? OC@ JOEFY0G() <I? MBTU*OEFY0G() API>ODJIN PN@APG OJ B@O ORJ DI?@S@N
OJ KMJ?P>@ < NGD>@.
AN DO DN DI?D><O@? DI OC@DM ?J>PH@IO<ODJI, OC@ M@OPMI OTK@N JA JOEFY0G() <I?
MBTU*OEFY0G() <M@ IJO JOU IJM TJ[F@U, =PO TJ[FEJGG@U. 6JP H<T C<Q@ OJ ?@ADI@
Q<MD<=G@N JA OC<O @S<>O OTK@:
TJ[FEJGG@U GJSTU@F = JOEFY0G(MJOF, 'F');
&O DN KJNND=G@ OJ ?@ADI@ Q<MD<=G@N NCJMO@M RDOC OC@ BVUP F@TRJM?, RCD>C R@ RDGG N@@ DI
< G<O@M >C<KO@M:
BVUP GJSTU@F = JOEFY0G(MJOF, 'F');
... F:7 EA>GF;A@E
(FD;@9E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
90
Redirecting Standard Input and Output Streams 21
AGG JA OC@ KMJBM<HN OC<O R@ C<Q@ N@@I NJ A<M C<Q@ DIO@M<>O@? OCMJPBC TUEJO <I? TUEPVU, OC@
NO<I?<M? DIKPO <I? JPOKPO NOM@<HN. &IKPO <I? JPOKPO API>ODJIN GDF@ SFBEG <I? XSJUFMO
JK@M<O@ JI OC@N@ NOM@<HN =T ?@A<PGO.
4CDG@ PNDIB OC@N@ NOM@<HN, R@ C<Q@ H<?@ <I <NNPHKODJI OC<O OC@ NO<I?<M? DIKPO DN AMJH OC@
F@T=J<M? <I? OC@ NO<I?<M? JPOKPO DN OJ OC@ N>M@@I.
4@ RDGG NO<MO RMDODIB KMJBM<HN OC<O ?@<G RDOC ADG@N DI G<O@M >C<KO@MN. 4@ RDGG N@@ OC<O EPNO GDF@
NO<I?<M? DIKPO <I? JPOKPO NOM@<HN, ADG@N <M@ >C<M<>O@M NOM@<HN <N R@GG; NJ OC@T <M@ PN@? DI
<GHJNO OC@ N<H@ R<T <N TUEJO <I? TUEPVU.
B@AJM@ N@@DIB CJR ADG@N <M@ <>>@NN@? AMJH RDOCDI KMJBM<HN, & RJPG? GDF@ OJ NCJR CJR
NO<I?<M? DIKPON <I? JPOKPON JA KMJBM<HN ><I =@ M@?DM@>O@? OJ ADG@N, <I? ><I =@ KDK@? OJ JOC@M
KMJBM<HN. -MJBM<HN ><I KMDIO OJ ADG@N DINO@<? JA OC@ N>M@@I, <I? M@<? AMJH ADG@N DINO@<? JA OC@
F@T=J<M?. AGOCJPBC OC@N@ A@<OPM@N <M@ IJO ?DM@>OGT M@G<O@? OJ KMJBM<HHDIB G<IBP<B@N, OC@T
<M@ PN@APG OJJGN OC<O <M@ <Q<DG<=G@ DI <GG HJ?@MI >JINJG@N.
Redirecting standard output to a file with >
21.1
4C@I NO<MODIB OC@ KMJBM<H AMJH OC@ >JINJG@, OTKDIB < > >C<M<>O@M <I? < ADG@ I<H@ <O OC@ @I?
JA OC@ >JHH<I? GDI@ M@?DM@>ON OC@ NO<I?<M? JPOKPO JA OC<O KMJBM<H OJ OC@ NK@>DAD@? ADG@.
"Q@MTOCDIB OC<O OC@ KMJBM<H KMDION OJ DON NO<I?<M? JPOKPO RJPG? =@ RMDOO@I OJ OC<O ADG@
DINO@<?.
)@O'N O@NO OCDN RDOC < KMJBM<H OC<O M@<?N < AGJ<ODIB KJDIO IPH=@M AMJH DON DIKPO, HPGODKGD@N
OC<O IPH=@M =T ORJ, <I? KMDION OC@ M@NPGO OJ DON NO<I?<M? JPOKPO:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
EPVCMF OVNCFS;
SFBEG(" %T", &OVNCFS);
XSJUFMO(OVNCFS * 2);
^
&A OC@ I<H@ JA OC@ KMJBM<H DN CZ@UXP, DON JPOKPO RJPG? =@ RMDOO@I OJ < ADG@ I<H@?
CZ@UXP@SFTVMU.UYU RC@I OC@ KMJBM<H DN NO<MO@? JI OC@ >JHH<I? GDI@ <N DI OC@ AJGGJRDIB
GDI@:
./CZ@UXP > CZ@UXP@SFTVMU.UYU
#JM @S<HKG@, DA R@ @IO@M 1.2 <O OC@ >JINJG@, OC@ M@NPGO 2.4 RJPG? <KK@<M DI
CZ@UXP@SFTVMU.UYU. (Note: A>F:AG9: F:7 BDA9D3? 6A7E @AF 6;EB>3K 3 BDA?BF >;=7 "&>73E7
7@F7D 3 @G?47D", ;F EF;>> 7JB75FE 3 @G?47D FA 47 7@F7D76.)
Redirecting standard input from a file with <
21.2
0DHDG<M OJ M@?DM@>ODIB NO<I?<M? JPOKPO =T >, NO<I?<M? DIKPO ><I =@ M@?DM@>O@? AMJH < ADG@ =T <.
&I OCDN ><N@, OC@ KMJBM<H M@<?N AMJH OC@ NK@>DAD@? ADG@ DINO@<? JA AMJH OC@ F@T=J<M?.
1J O@NO OCDN, G@O'N PN@ < KMJBM<H OC<O ><G>PG<O@N JI@ O@IOC JA < IPH=@M:
'76;D75F;@9 (F3@63D6 !@BGF 3@6 %GFBGF (FD73?E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
91
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
EPVCMF OVNCFS;
SFBEG(" %T", &OVNCFS);
XSJUFMO(OVNCFS / 10);
^
ANNPHDIB OC<O OC@ ADG@ CZ@UXP@SFTVMU.UYU NODGG @SDNON <I? >JIO<DIN 2.4 AMJH OC@ KM@QDJPN
JPOKPO, <I? OC<O OC@ I<H@ JA OC@ I@R KMJBM<H DN POF@UFOUI, R@ ><I M@?DM@>O OC@ I@R
KMJBM<H'N NO<I?<M? DIKPO AMJH OC<O ADG@ <N DI OC@ AJGGJRDIB GDI@:
./POF@UFOUI < CZ@UXP@SFTVMU.UYU
1CDN ODH@ OC@ KMJBM<H RJPG? M@<? AMJH CZ@UXP@SFTVMU.UYU <I? KMDIO OC@ M@NPGO OJ OC@
>JINJG@ <N 0.24.
Redirecting both standard streams
21.3
> <I? < ><I =@ PN@? <O OC@ N<H@ ODH@:
./POF@UFOUI < CZ@UXP@SFTVMU.UYU > POF@UFOUI@SFTVMU.UYU
1CDN ODH@ OC@ NO<I?<M? DIKPO RJPG? =@ M@<? AMJH CZ@UXP@SFTVMU.UYU <I? OC@ NO<I?<M?
JPOKPO RJPG? =@ RMDOO@I OJ POF@UFOUI@SFTVMU.UYU.
Piping programs with ]
21.4
+JO@ OC<O CZ@UXP@SFTVMU.UYU DN <I DIO@MH@?D<MT =@OR@@I OC@ ORJ KMJBM<HN; CZ@UXP RMDO@N
OJ DO <I? POF@UFOUI M@<?N AMJH DO.
1C@ ] >C<M<>O@M KDK@N OC@ NO<I?<M? JPOKPO JA OC@ KMJBM<H OC<O DN JI DON G@AO-C<I? ND?@ OJ OC@
NO<I?<M? DIKPO JA OC@ KMJBM<H OC<O DN JI DON MDBCO-C<I? ND?@ RDOCJPO OC@ I@@? AJM <I
DIO@MH@?D<MT ADG@. #JM @S<HKG@, RC@I OC@ ORJ KMJBM<HN <=JQ@ <M@ KDK@? OJB@OC@M <N DI OC@
AJGGJRDIB GDI@, OC@T >JGG@>ODQ@GT ><G>PG<O@ A@7 8;8F: JA OC@ DIKPO:
./CZ@UXP ] ./POF@UFOUI
#DMNO CZ@UXP MPIN <I? M@<?N < IPH=@M AMJH DON DIKPO. (/@H@H=@M OC<O <GOCJPBC DO ?J@N IJO
KMJHKO AJM JI@, DO NODGG R<DON AJM < IPH=@M.)
1C@ M@NPGO JA CZ@UXP RJPG? <KK@<M <O OC@ NO<I?<M? DIKPO JA POF@UFOUI, RCD>C DI OPMI RJPG?
><G>PG<O@ <I? KMDIO JI@ O@IOC JA OC<O M@NPGO.
Exercise
21.5
t -DK@ HJM@ OC<I JI@ KMJBM<H:
./POF ] ./UXP ] ./UISFF
... F:7 EA>GF;A@
'76;D75F;@9 (F3@63D6 !@BGF 3@6 %GFBGF (FD73?E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
92
Files 22
4@ C<Q@ N@@I DI OC@ KM@QDJPN >C<KO@M OC<O NO<I?<M? DIKPO <I? JPOKPO NOM@<HN ><I =@
M@?DM@>O@? OJ <I? AMJH ADG@N <I? JOC@M KMJBM<HN RDOC OC@ >, <, <I? ] JK@M<OJMN JI OC@
>JINJG@. !@NKDO@ =@DIB Q@MT KJR@MAPG, OC@N@ OJJGN <M@ IJO NPDO<=G@ DI @Q@MT NDOP<ODJI =@><PN@
DI H<IT ><N@N KMJBM<HN ><I IJO >JHKG@O@ OC@DM O<NFN =T M@<?DIB AMJH OC@DM DIKPO <I? RMDODIB
OJ OC@DM JPOKPO GDF@ < NDHKG@ KDK@.
#JM @S<HKG@, < KMJBM<H OC<O ?@<GN RDOC NOP?@IO M@>JM?N H<T PN@ DON NO<I?<M? JPOKPO OJ
?DNKG<T OC@ KMJBM<H H@IP. 0P>C < KMJBM<H RJPG? I@@? OJ RMDO@ OC@ NOP?@IO M@>JM?N OJ <I
<>OP<G ADG@ DINO@<? JA TUEPVU.
&I OCDN >C<KO@M, R@ RDGG >JQ@M M@<?DIB AMJH <I? RMDODIB OJ ADG@N JA ADG@ NTNO@HN.
Fundamental concepts
22.1
#DG@N <M@ M@KM@N@IO@? =T OC@ 'JMF EFDG5F JA OC@ TUE.TUEJP HJ?PG@. 0DI>@ & C<Q@I'O DIOMJ?P>@?
NOMP>ON T@O, R@ RDGG C<Q@ OJ <>>@KO OC@ NTIO<S JA NOMP>O >JINOMP>ODJI <N-DN AJM IJR.
B@AJM@ B@OODIB OJ >J?@ N<HKG@N R@ C<Q@ OJ BJ OCMJPBC API?<H@IO<G >JI>@KON <=JPO ADG@N.
The producer and the consumer 22.1.1
#DG@N OC<O <M@ >M@<O@? JI JI@ KG<OAJMH H<T IJO =@ M@<?DGT PN<=G@ JI JOC@M KG<OAJMHN. *@M@GT
JK@IDIB < ADG@ <I? RMDODIB ?<O< DI DO H<T IJO =@ NPAAD>D@IO AJM OC<O ?<O< OJ =@ <Q<DG<=G@ JI OC@
>JINPH@M'N ND?@. 1C@ KMJ?P>@M <I? OC@ >JINPH@M JA OC@ ?<O< HPNO C<Q@ <GM@<?T <BM@@? JI
OC@ AJMH<O JA OC@ ?<O< OC<O DN DI OC@ ADG@. #JM @S<HKG@, DA OC@ KMJ?P>@M C<N RMDOO@I OC@ D? <I?
OC@ I<H@ JA OC@ NOP?@IO M@>JM?N DI < >@MO<DI JM?@M, OC@ >JINPH@M HPNO M@<? OC@ ?<O< =<>F DI
OC@ N<H@ JM?@M.
A??DODJI<GGT, OC@ N<HKG@ >J?@N =@GJR ?J IJO RMDO@ B,* OJ OC@ =@BDIIDIB JA OC@ ADG@. 1CDN H<T
H<F@ TJPM ADG@N DI>JHK<OD=G@ RDOC NTNO@HN OC<O M@LPDM@ B,*. (B,* DN NCJMO AJM 4KF7 AD67D
?3D=. &O NK@>DAD@N DI RC<O JM?@M OC@ 21# >J?@ PIDON JA >C<M<>O@MN <M@ <MM<IB@? DI < ADG@.)
Access rights 22.1.2
#DG@ NTNO@HN KM@N@IO ADG@N OJ KMJBM<HN PI?@M >@MO<DI <>>@NN MDBCON. A>>@NN MDBCON <M@
DHKJMO<IO AJM =JOC ?<O< DIO@BMDOT <I? K@MAJMH<I>@.
4C@I DO DN <=JPO M@<?DIB, <GGJRDIB HPGODKG@ KMJBM<HN M@<? AMJH OC@ N<H@ ADG@ RJPG? DHKMJQ@
K@MAJMH<I>@ =@><PN@ IJ KMJBM<H RJPG? =@ R<DODIB AJM <IT JOC@M OJ ADIDNC OC@DM M@<?DIB. ,I
OC@ JOC@M C<I?, RC@I DO DN <=JPO RMDODIB, JIGT JI@ KMJBM<H NCJPG? =@ <GGJR@? OJ RMDO@ OJ <
ADG@, JOC@MRDN@ KMJBM<HN H<T JQ@MRMDO@ @<>C JOC@M'N ?<O<.
Opening a file 22.1.3
1C@ NO<I?<M? DIKPO <I? JPOKPO NOM@<HN TUEJO <I? TUEPVU <M@ <GM@<?T AB7@ RC@I KMJBM<HN
NO<MO MPIIDIB. 1C@T <M@ M@<?T OJ =@ PN@?.
,I OC@ JOC@M C<I?, ADG@N HPNO ADMNO =@ JK@I@? =T NK@>DATDIB OC@ I<H@ JA OC@ ADG@ <I? OC@
<>>@NN MDBCON OC<O <M@ I@@?@?.
F;>7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
93
Closing a file 22.1.4
AIT ADG@ OC<O C<N =@@I JK@I@? =T < KMJBM<H HPNO =@ >GJN@? RC@I OC@ KMJBM<H ADIDNC@N PNDIB
OC<O ADG@. &I HJNO ><N@N OC@ ADG@N I@@? IJO =@ >GJN@? @SKGD>DOGT; OC@T <M@ >GJN@? <POJH<OD><GGT
RC@I 'JMF J=E@>ON <M@ O@MHDI<O@? <POJH<OD><GGT:
JG (B$POEJUJPO) \
// "TTVNF B 'JMF PCKFDU IBT CFFO DSFBUFE BOE VTFE IFSF.
// ...
^ // j 5IF BDUVBM GJMF XPVME CF DMPTFE BVUPNBUJDBMMZ IFSF
// XIFO MFBWJOH UIJT TDPQF. /P OFFE UP DMPTF FYQMJDJUMZ.
&I NJH@ ><N@N < ADG@ J=E@>O H<T I@@? OJ =@ M@-JK@I@? OJ <>>@NN < ?DAA@M@IO ADG@ JM OC@ N<H@ ADG@
RDOC ?DAA@M@IO <>>@NN MDBCON. &I NP>C ><N@N OC@ ADG@ HPNO =@ >GJN@? <I? M@-JK@I@?:
GJMF.DMPTF();
GJMF.PQFO("TUVEFOU@SFDPSET", "S");
Writing to and reading from files 22.1.5
0DI>@ ADG@N <M@ >C<M<>O@M NOM@<HN, DIKPO <I? JPOKPO API>ODJIN XSJUFMO, SFBEG, @O>. <M@ PN@?
@S<>OGT OC@ N<H@ R<T RDOC OC@H. 1C@ JIGT ?DAA@M@I>@ DN OC<O OC@ I<H@ JA OC@ 'JMF Q<MD<=G@
<I? < ?JO HPNO =@ OTK@?:
XSJUFMO("IFMMP"); // XSJUFT UP TUBOEBSE PVUQVU
TUEPVU.XSJUFMO("IFMMP"); // TBNF BT BCPWF
GJMF.XSJUFMO("IFMMP"); // XSJUFT UP UIF TQFDJGJFE GJMF
FPG() to determine the end of a file 22.1.6
1C@ FPG() H@H=@M API>ODJI ?@O@MHDI@N RC@OC@M OC@ @I? JA < ADG@ C<N =@@I M@<>C@? RCDG@
M@<?DIB AMJH < ADG@. &O M@OPMIN USVF DA OC@ @I? JA OC@ ADG@ C<N =@@I M@<>C@?.
#JM @S<HKG@, OC@ AJGGJRDIB GJJK RDGG =@ <>ODQ@ PIODG OC@ @I? JA OC@ ADG@:
XIJMF (!GJMF.FPG()) \
/* ... */
^
The TUE.GJMF module 22.1.7
1C@ TUE.GJMF HJ?PG@ >JIO<DIN API>ODJIN <I? OTK@N OC<O <M@ PN@APG RC@I RJMFDIB RDOC
>JIO@ION JA ?DM@>OJMD@N. #JM @S<HKG@, FYJTUT ><I =@ PN@? OJ ?@O@MHDI@ RC@OC@M < ADG@ JM <
?DM@>OJMT @SDNON JI OC@ ADG@ NTNO@HN:
JNQPSU TUE.GJMF;
// ...
JG (FYJTUT(GJMF/BNF)) \
// UIFSF JT B GJMF PS EJSFDUPSZ VOEFS UIBU OBNF
^ FMTF \
// OP GJMF PS EJSFDUPSZ VOEFS UIBU OBNF
^
F;>7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
94
TUE.TUEJP.'JMF struct
22.2
'JMF PN@N OC@ N<H@ HJ?@ >C<M<>O@MN OC<O <M@ PN@? =T GPQFO JA OC@ KMJBM<HHDIB G<IBP<B@:
.PEF %FGJOJUJPO
S SFBE BDDFTT
UIF GJMF JT PQFOFE UP CF SFBE GSPN UIF CFHJOOJOH
S+ SFBE BOE XSJUF BDDFTT
UIF GJMF JT PQFOFE UP CF SFBE GSPN BOE XSJUUFO BU UIF CFHJOOJOH
X XSJUF BDDFTT
JG UIF GJMF EPFT OPU FYJTU, JU JT DSFBUFE BT FNQUZ
JG UIF GJMF BMSFBEZ FYJTUT, JUT DPOUFOUT BSF DMFBSFE
X+ SFBE BOE XSJUF BDDFTT
JG UIF GJMF EPFT OPU FYJTU, JU JT DSFBUFE BT FNQUZ
JG UIF GJMF BMSFBEZ FYJTUT, JUT DPOUFOUT BSF DMFBSFE
B BQQFOE BDDFTT
JG UIF GJMF EPFT OPU FYJTU, JU JT DSFBUFE BT FNQUZ
JG UIF GJMF BMSFBEZ FYJTUT, JUT DPOUFOUT BSF QSFTFSWFE BOE JU JT PQFOFE
UP CF XSJUUFO BU UIF FOE
B+ SFBE BOE BQQFOE BDDFTT
JG UIF GJMF EPFT OPU FYJTU, JU JT DSFBUFE BT FNQUZ
JG UIF GJMF BMSFBEZ FYJTUT, JUT DPOUFOUT BSF QSFTFSWFE BOE UIF GJMF JT
PQFOFE UP CF SFBE GSPN UIF CFHJOOJOH BOE XSJUUFO BU UIF FOE
A '=' >C<M<>O@M H<T =@ <??@? OJ OC@ HJ?@ NOMDIB <N DI "M=". 1CDN H<T C<Q@ <I @AA@>O JI
KG<OAJMHN OC<O NPKKJMO OC@ 4;@3DK ?A67, =PO DO DN DBIJM@? JI <GG -,0&5 NTNO@HN.
1C@ 'JMF NOMP>O DN DI>GP?@? DI OC@ TUE.TUEJP HJ?PG@.
Writing to a file 22.2.1
1C@ ADG@ HPNO C<Q@ =@@I JK@I@? DI JI@ JA OC@ RMDO@ HJ?@N ADMNO:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
'JMF GJMF = 'JMF("TUVEFOU@SFDPSET", "X");
GJMF.XSJUFMO("/BNF : ", ";BGFS");
GJMF.XSJUFMO("/VNCFS: ", 123);
GJMF.XSJUFMO("$MBTT : ", "1"");
^
AN TJP M@H@H=@M AMJH OC@ 0OMDIBN >C<KO@M, OC@ OTK@ JA GDO@M<GN GDF@ "TUVEFOU@SFDPSET" DN
TUSJOH, >JINDNODIB JA DHHPO<=G@ >C<M<>O@MN. #JM OC<O M@<NJI, DO DN IJO KJNND=G@ OJ >JINOMP>O
'JMF J=E@>ON =T < HPO<=G@ ADG@ I<H@ (@.B. DIBS<>). 4C@I I@@?@?, DO RJPG? =@ I@>@NN<MT OJ
><GG OC@ .JEVQ KMJK@MOT JA OC@ HPO<=G@ NOMDIB.
1C@ KMJBM<H <=JQ@ >M@<O@N JM JQ@MRMDO@N OC@ >JIO@ION JA < ADG@ I<H@? TUVEFOU@SFDPSET DI
OC@ ?DM@>OJMT OC<O DO C<N =@@I NO<MO@? PI?@M (DI OC@ KMJBM<H'N IAD=;@9 6;D75FADK).
Note: F;>7 @3?7E 53@ 5A@F3;@ 3@K 5:3D35F7D F:3F ;E >793> 8AD F:3F 8;>7 EKEF7?. )A 47 BADF34>7, !
I;>> GE7 A@>K A(C!! 5:3D35F7DE.
F;>7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
95
Reading from a file 22.2.2
1C@ ADG@ HPNO C<Q@ =@@I JK@I@? DI JI@ JA OC@ M@<? HJ?@N ADMNO:
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
WPJE NBJO()
\
'JMF GJMF = 'JMF("TUVEFOU@SFDPSET", "S");
XIJMF (!GJMF.FPG()) \
TUSJOH MJOF = DIPNQ(GJMF.SFBEMO());
XSJUFMO("SFBE MJOF -> ]", MJOF);
^
^
1C@ KMJBM<H <=JQ@ M@<?N <GG JA OC@ GDI@N JA OC@ ADG@ I<H@? TUVEFOU@SFDPSET <I? KMDION OCJN@
GDI@N OJ DON NO<I?<M? JPOKPO.
Exercise
22.3
t 4MDO@ < KMJBM<H OC<O O<F@N < ADG@ I<H@ AMJH OC@ PN@M, JK@IN OC<O ADG@, <I? RMDO@N <GG JA OC@
IJI-@HKOT GDI@N JA OC<O ADG@ OJ <IJOC@M ADG@. 1C@ I<H@ JA OC@ I@R ADG@ ><I =@ =<N@? JI OC@
I<H@ JA OC@ JMDBDI<G ADG@. #JM @S<HKG@, DA OC@ JMDBDI<G ADG@ DN GPP.UYU, OC@ I@R ADG@ ><I =@
GPP.UYU.PVU.
... F:7 EA>GF;A@
F;>7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
96
BVUP and UZQFPG 23
BVUP
23.1
4C@I ?@ADIDIB 'JMF Q<MD<=G@N DI OC@ KM@QDJPN >C<KO@M, R@ C<Q@ M@K@<O@? OC@ I<H@ JA OC@ OTK@
JI =JOC ND?@N JA OC@ = JK@M<OJM:
'JMF GJMF = 'JMF("TUVEFOU@SFDPSET", "X");
&O A@@GN M@?PI?<IO. &O RJPG? <GNJ =@ >PH=@MNJH@ <I? @MMJM-KMJI@ @NK@>D<GGT RC@I OC@ OTK@
I<H@ R@M@ GJIB@M:
7FSZ-POH5ZQF/BNF WBS = 7FSZ-POH5ZQF/BNF(/* ... */);
#JMOPI<O@GT, OC@ OTK@ I<H@ JI OC@ G@AO-C<I? ND?@ DN IJO I@>@NN<MT =@><PN@ OC@ >JHKDG@M ><I
DIA@M OC@ OTK@ JA OC@ G@AO-C<I? ND?@ AMJH OC@ @SKM@NNDJI JI OC@ MDBCO-C<I? ND?@. #JM OC@
>JHKDG@M OJ DIA@M OC@ OTK@, OC@ BVUP F@TRJM? ><I =@ PN@?:
BVUP WBS = 7FSZ-POH5ZQF/BNF(/* ... */);
BVUP ><I =@ PN@? RDOC <IT OTK@ @Q@I RC@I OC@ OTK@ DN IJO NK@GG@? JPO JI OC@ MDBCO-C<I? ND?@:
BVUP EVSBUJPO = 42;
BVUP EJTUBODF = 1.2;
BVUP HSFFUJOH = ")FMMP";
BVUP WFIJDMF = #FBVUJGVM#JDZDMF("CMVF");
AGOCJPBC "<POJ" DN OC@ <==M@QD<ODJI JA 3GFA?3F;5, DO ?J@N IJO >JH@ AMJH 3GFA?3F;5 FKB7
;@87D7@57. &O >JH@N AMJH 3GFA?3F;5 EFAD397 5>3EE, RCD>C DN < >JI>@KO <=JPO GDA@ ODH@N JA
Q<MD<=G@N. BVUP DN PN@? RC@I IJ JOC@M NK@>DAD@M DN <KKMJKMD<O@. #JM @S<HKG@, OC@ AJGGJRDIB
?@ADIDODJI ?J@N IJO PN@ BVUP:
JNNVUBCMF J = 42;
1C@ >JHKDG@M DIA@MN OC@ OTK@ JA J <N JNNVUBCMF JOU <=JQ@. (4@ RDGG N@@ JNNVUBCMF DI < G<O@M
>C<KO@M.)
UZQFPG
23.2
UZQFPG KMJQD?@N OC@ <>OP<G OTK@ JA @SKM@NNDJIN (DI>GP?DIB NDIBG@ Q<MD<=G@N, J=E@>ON, GDO@M<GN,
@O>.).
1C@ AJGGJRDIB DN <I @S<HKG@ JA CJR UZQFPG ><I =@ PN@? OJ NK@>DAT < OTK@ RDOCJPO @SKGD>DOGT
NK@GGDIB DO JPO:
JOU WBMVF = 100; // BMSFBEZ EFGJOFE BT 'JOU'
UZQFPG(WBMVF) WBMVF2; // NFBOT "UZQF PG WBMVF"
UZQFPG(100) WBMVF3; // NFBOT "UZQF PG MJUFSBM 100"
1C@ G<NO ORJ Q<MD<=G@ ?@ADIDODJIN <=JQ@ <M@ OC@ @LPDQ<G@IO JA OC@ AJGGJRDIB:
JOU WBMVF2;
JOU WBMVF3;
3GFA 3@6 FKB7A8
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
97
&O DN J=QDJPN OC<O UZQFPG DN IJO I@@?@? DI NDOP<ODJIN GDF@ <=JQ@ RC@I <>OP<G OTK@N <M@ FIJRI.
1CDN F@TRJM? DN @NK@>D<GGT PN@APG DI O@HKG<O@ ?@ADIDODJIN, RCD>C R@ RDGG N@@ DI G<O@M >C<KO@MN.
Exercise
23.3
t AN R@ C<Q@ N@@I <=JQ@, OC@ OTK@ JA GDO@M<GN GDF@ 100 DN JOU (<N JKKJN@? OJ TIPSU, MPOH, JM
<IT JOC@M OTK@). 4MDO@ < KMJBM<H OJ ?@O@MHDI@ OC@ OTK@ JA AGJ<ODIB KJDIO GDO@M<GN GDF@ 1.2.
UZQFPG <I? .TUSJOHPG RJPG? =@ PN@APG DI OCDN KMJBM<H.
... F:7 EA>GF;A@
3GFA 3@6 FKB7A8
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
98
Name Space 24
AIT I<H@ DN <>>@NND=G@ AMJH OC@ KJDIO OC<O DO C<N =@@I ?@ADI@? <O OJ OC@ KJDIO OC<O DON N>JK@
C<N =@@I @SDO@?, <N R@GG <N DI <GG JA OC@ N>JK@N OC<O DON N>JK@ DI>GP?@N. &I OCDN M@B<M?, @Q@MT
N>JK@ ?@ADI@N < @3?7 EB357.
+<H@N <M@ IJO <Q<DG<=G@ JI>@ OC@DM N>JK@ C<N =@@I @SDO@?:
WPJE NBJO()
\
JOU PVUFS;
JG (B$POEJUJPO) \ // j DVSMZ CSBDLFU TUBSUT B OFX TDPQF
JOU JOOFS = 1;
PVUFS = 2; // j XPSLT; 'PVUFS' JT BWBJMBCMF IFSF
^ // j 'JOOFS' JT OPU BWBJMBCMF CFZPOE UIJT QPJOU
JOOFS = 3; // j DPNQJMBUJPO &3303
// 'JOOFS' JT OPU BWBJMBCMF JO UIF PVUFS TDPQF
^
B@><PN@ JOOFS DN ?@ADI@? RDOCDI OC@ N>JK@ JA OC@ JG >JI?DODJI DO DN <Q<DG<=G@ JIGT DI OC<O
N>JK@. ,I OC@ JOC@M C<I?, PVUFS DN <Q<DG<=G@ DI =JOC OC@ JPO@M N>JK@ <I? OC@ DII@M N>JK@.
&O DN IJO G@B<G OJ ?@ADI@ OC@ N<H@ I<H@ DI <I DII@M N>JK@:
TJ[F@U MFOHUI = PEE/VNCFST.MFOHUI;
JG (B$POEJUJPO) \
TJ[F@U MFOHUI = QSJNF/VNCFST.MFOHUI; // j DPNQJMBUJPO &3303
^
Defining names closest to their first use
24.1
AN R@ C<Q@ =@@I ?JDIB DI <GG JA OC@ KMJBM<HN NJ A<M, Q<MD<=G@N HPNO =@ ?@ADI@? =@AJM@ OC@DM
ADMNO PN@:
XSJUFMO(OVNCFS); // j DPNQJMBUJPO &3303
// OVNCFS JT OPU LOPXO ZFU
JOU OVNCFS = 42;
#JM OC<O >J?@ OJ =@ <>>@KO<=G@ =T OC@ >JHKDG@M, OVNCFS HPNO =@ ?@ADI@? =@AJM@ DO DN PN@? RDOC
XSJUFMO. AGOCJPBC OC@M@ DN IJ M@NOMD>ODJI JI CJR H<IT GDI@N @<MGD@M DO NCJPG? =@ ?@ADI@?, DO DN
<>>@KO@? <N BJJ? KMJBM<HHDIB KM<>OD>@ OC<O Q<MD<=G@N =@ ?@ADI@? >GJN@NO OJ RC@M@ OC@T <M@
ADMNO PN@?.
)@O'N N@@ OCDN DI < KMJBM<H OC<O KMDION OC@ <Q@M<B@ JA OC@ IPH=@MN OC<O DO O<F@N AMJH OC@ PN@M.
-MJBM<HH@MN RCJ <M@ @SK@MD@I>@? DI NJH@ JOC@M KMJBM<HHDIB G<IBP<B@N H<T =@ PN@? OJ
?@ADIDIB Q<MD<=G@N <O OJKN JA N>JK@N:
JOU DPVOU; // j )&3&
JOU<> OVNCFST; // j )&3&
EPVCMF BWFSBHF7BMVF; // j )&3&
XSJUF(")PX NBOZ OVNCFST BSF UIFSF ");
SFBEG(" %T", &DPVOU);
JG (DPVOU >= 1) \
OVNCFST.MFOHUI = DPVOU;
$3?7 (B357
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
99
// ... BTTVNF UIF DBMDVMBUJPO JT IFSF ...
^ FMTF \
XSJUFMO("&3303: :PV NVTU FOUFS BU MFBTU POF OVNCFS!");
^
JIOM<NO OC@ >J?@ <=JQ@ OJ OC@ JI@ =@GJR OC<O ?@ADI@N OC@ Q<MD<=G@N G<O@M, <N @<>C Q<MD<=G@
<>OP<GGT NO<MON O<FDIB K<MO DI OC@ KMJBM<H:
XSJUF(")PX NBOZ OVNCFST BSF UIFSF ");
JOU DPVOU; // j )&3&
SFBEG(" %T", &DPVOU);
JG (DPVOU >= 1) \
JOU<> OVNCFST; // j )&3&
OVNCFST.MFOHUI = DPVOU;
EPVCMF BWFSBHF7BMVF; // j )&3&
// ... BTTVNF UIBU UIF DBMDVMBUJPO JT IFSF ...
^ FMTF \
XSJUFMO("&3303: :PV NVTU FOUFS BU MFBTU POF OVNCFS!");
^
AGOCJPBC ?@ADIDIB <GG JA OC@ Q<MD<=G@N <O OC@ OJK H<T GJJF =@OO@M NOMP>OPM<GGT, OC@M@ <M@ N@Q@M<G
=@I@ADON JA ?@ADIDIB OC@H <N G<O@ <N KJNND=G@:
t Speed: "Q@MT Q<MD<=G@ ?@ADIDODJI C<N < NK@@? >JNO DI OC@ KMJBM<H. AN @Q@MT Q<MD<=G@ DN
DIDOD<GDU@? DI !, ?@ADIDIB Q<MD<=G@N <O OC@ OJK RJPG? KJO@IOD<GGT R<NO@ OC@DM DIDOD<GDU<ODJI
>JNO DA OCJN@ Q<MD<=G@N RJPG? IJO <>OP<GGT =@ PN@? G<O@M.
t Risk of mistakes: "Q@MT GDI@ OC<O DN =@OR@@I OC@ ?@ADIDODJI <I? PN@ JA < Q<MD<=G@ ><MMD@N <
CDBC@M MDNF JA KMJBM<HHDIB HDNO<F@N. AN <I @S<HKG@ JA OCDN, >JIND?@M < Q<MD<=G@ I<H@? <N
>JHHJI <N MFOHUI. &O DN KJNND=G@ OJ PN@ OC<O Q<MD<=G@ DI<?Q@MO@IOGT AJM <IJOC@M G@IBOC
=@AJM@ DON DIO@I?@? GDI@. AO OC@ ODH@ DON DIO@I?@? GDI@ C<N =@@I M@<>C@?, OC@ Q<MD<=G@ H<T
C<Q@ <I PIDIO@I?@? Q<GP@.
t Readability: AN OC@ IPH=@M JA GDI@N DI < N>JK@ <M@ DI>M@<N@?, DO DN HJM@ GDF@GT OC<O OC@
?@ADIDODJI JA < Q<MD<=G@ H<T =@>JH@ OJJ A<M PK DI OC@ NJPM>@ >J?@, AJM>DIB OC@ KMJBM<HH@M
OJ N>MJGG =<>F DI JM?@M OJ GJJF <O DON ?@ADIDODJI.
t Code maintenance: 0JPM>@ >J?@ DN DI >JINO<IO HJ?DAD><ODJI <I? DHKMJQ@H@IO: I@R
A@<OPM@N <M@ <??@?, JG? A@<OPM@N <M@ M@HJQ@?, =PBN <M@ ADS@?, @O>. 1C@N@ >C<IB@N
NJH@ODH@N H<F@ DO I@>@NN<MT OJ HJQ@ < BMJPK JA GDI@N <GOJB@OC@M <N < I@R API>ODJI.
4C@I OC<O C<KK@IN, C<QDIB <GG JA OC@ Q<MD<=G@N ?@ADI@? >GJN@ OJ OC@ GDI@N OC<O PN@
OC@H H<F@N DO @<ND@M OJ HJQ@ OC@H <N < >JC@M@IO =PI>C.
#JM @S<HKG@, DI OC@ G<OO@M >J?@ <=JQ@ OC<O AJGGJR@? OCDN BPD?@GDI@, <GG JA OC@ GDI@N
RDOCDI OC@ JG NO<O@H@IO ><I =@ HJQ@? OJ < I@R < API>ODJI DI OC@ KMJBM<H.
,I OC@ JOC@M C<I?, RC@I OC@ Q<MD<=G@N <M@ <GR<TN ?@ADI@? <O OC@ OJK, DA OC@ GDI@N @Q@M
I@@? OJ =@ HJQ@?, OC@ Q<MD<=G@N OC<O <M@ PN@? DI OCJN@ GDI@N HPNO =@ D?@IODAD@? JI@ =T
JI@.
$3?7 (B357
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
100
GPS Loop 25
1C@ GPS GJJK DN AJM OC@ N<H@ KPMKJN@ <N OC@ XIJMF GJJK. GPS H<F@N DO KJNND=G@ OJ KPO OC@
?@ADIDODJIN <I? @SKM@NNDJIN <=JPO OC@ GJJK'N DO@M<ODJI JI OC@ N<H@ GDI@.
AGOCJPBC GPS DN PN@? HP>C G@NN OC<I GPSFBDI DI KM<>OD>@, DO DN DHKJMO<IO OJ PI?@MNO<I? OC@
GPS GJJK ADMNO. 4@ RDGG N@@ GPSFBDI DI < G<O@M >C<KO@M.
Sections of the XIJMF loop
25.1
1C@ XIJMF GJJK @Q<GP<O@N OC@ GJJK >JI?DODJI <I? >JIODIP@N @S@>PODIB OC@ GJJK <N GJIB <N OC<O
>JI?DODJI DN USVF. #JM @S<HKG@, < GJJK OJ KMDIO OC@ IPH=@MN =@OR@@I 1 <I? 11 H<T >C@>F OC@
>JI?DODJI >7EE F:3@ 11:
XIJMF (OVNCFS < 11)
!F7D3F;@9 OC@ GJJK ><I =@ <>CD@Q@? =T DI>M@H@IODIB OVNCFS <O OC@ @I? JA OC@ GJJK:
++OVNCFS;
1J =@ >JHKDG<=G@ <N ! >J?@, OVNCFS HPNO C<Q@ =@@I ?@ADI@? =@AJM@ DON ADMNO PN@:
JOU OVNCFS = 1;
#DI<GGT, OC@M@ DN OC@ <>OP<G RJMF RDOCDI OC@ GJJK =J?T:
XSJUFMO(OVNCFS);
1C@N@ AJPM N@>ODJIN ><I =@ ?@ADI@? <N DI OC@ >JHH@ION =@GJR:
JOU OVNCFS = 1; // j QSFQBSBUJPO
XIJMF (OVNCFS < 11) \ // j DPOEJUJPO DIFDL
XSJUFMO(OVNCFS); // j BDUVBM XPSL
++OVNCFS; // j JUFSBUJPO
^
1C@ N@>ODJIN JA OC@ XIJMF GJJK <M@ @S@>PO@? DI OC@ AJGGJRDIB JM?@M ?PMDIB OC@ DO@M<ODJI JA OC@
XIJMF GJJK:
QSFQBSBUJPO
DPOEJUJPO DIFDL
BDUVBM XPSL
JUFSBUJPO
DPOEJUJPO DIFDL
BDUVBM XPSL
JUFSBUJPO
...
A CSFBL NO<O@H@IO JM < OCMJRI @S>@KODJI ><I O@MHDI<O@ OC@ GJJK <N R@GG.
8AD "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
101
Sections of the GPS loop
25.2
GPS =MDIBN OCM@@ JA OC@N@ N@>ODJIN JI < NDIBG@ GDI@. 1C@T <M@ RMDOO@I RDOCDI OC@ K<M@IOC@N@N JA
OC@ GPS GJJK, N@K<M<O@? RDOC N@HD>JGJIN. 1C@ GJJK =J?T >JIO<DIN JIGT OC@ <>OP<G RJMF:
GPS (/* QSFQBSBUJPO */; /* DPOEJUJPO DIFDL */; /* JUFSBUJPO */) \
/* BDUVBM XPSL */
^
1C@ N<H@ >J?@ DN HJM@ >JC@M@IO RC@I RMDOO@I <N < GPS GJJK:
GPS (JOU OVNCFS = 1; OVNCFS < 11; ++OVNCFS) \
XSJUFMO(OVNCFS);
^
1C@ =@I@ADO JA OC@ GPS GJJK DN HJM@ J=QDJPN RC@I OC@ GJJK =J?T C<N < G<MB@ IPH=@M JA
NO<O@H@ION. 1C@ @SKM@NNDJI OC<O DI>M@H@ION OC@ GJJK Q<MD<=G@ DN QDND=G@ JI OC@ GPS GDI@
DINO@<? JA =@DIB HDS@? RDOC OC@ JOC@M NO<O@H@ION JA OC@ GJJK.
1C@ N@>ODJIN JA OC@ GPS GJJK <M@ @S@>PO@? DI OC@ N<H@ JM?@M <N OC@ XIJMF GJJK.
1C@ CSFBL <I? DPOUJOVF NO<O@H@ION RJMF @S<>OGT OC@ N<H@ DI OC@ GPS GJJK.
1C@ JIGT ?DAA@M@I>@ =@OR@@I OC@ XIJMF <I? GPS GJJKN DN OC@ I<H@ NK<>@ JA OC@ GJJK Q<MD<=G@.
1CDN DN @SKG<DI@? =@GJR.
AGOCJPBC Q@MT >JHHJI, OC@ DO@M<ODJI Q<MD<=G@ I@@? IJO =@ <I DIO@B@M, IJM DO DN HJ?DAD@? JIGT
=T DI>M@H@IODIB. #JM @S<HKG@, OC@ AJGGJRDIB GJJK DN PN@? OJ KMDIO OC@ C<GQ@N JA OC@ KM@QDJPN
AGJ<ODIB KJDIO Q<GP@N:
GPS (EPVCMF WBMVF = 1; WBMVF > 0.001; WBMVF /= 2) \
XSJUFMO(WBMVF);
^
Note: 1C@ DIAJMH<ODJI <=JQ@ PI?@M OCDN ODOG@ DN O@>CID><GGT DI>JMM@>O =PO ?J@N >JQ@M OC@
H<EJMDOT JA OC@ PN@ ><N@N JA OC@ GPS GJJK, @NK@>D<GGT =T KMJBM<HH@MN RCJ C<Q@ < JM ++
=<>FBMJPI?. &I M@<GDOT, !'N GPS GJJK ?J@N IJO C<Q@ F:D77 E75F;A@E F:3F 3D7 E7B3D3F76 4K
E7?;5A>A@E. &O C<N ORJ N@>ODJIN, ADMNO JA RCD>C >JIO<DIN OC@ KM@K<M<ODJI <I? OC@ GJJK >JI?DODJI
OJB@OC@M. 4DOCJPO B@OODIB DIOJ OC@ ?@O<DGN JA OCDN NTIO<S, C@M@ DN CJR OJ ?@ADI@ ORJ Q<MD<=G@N JA
?DAA@M@IO OTK@N DI OC@ KM@K<M<ODJI N@>ODJI:
GPS (\ JOU J = 0; EPVCMF E = 0.5; ^ J < 10; ++J) \
XSJUFMO("J: ", J, ", E: ", E);
E /= 2;
^
+JO@ OC<O OC@ KM@K<M<ODJI N@>ODJI DN OC@ <M@< RDOCDI OC@ T@GGJR >PMGT =M<>F@ON <I? OC<O OC@M@
DN IJO < N@HD>JGJI =@OR@@I OC@ KM@K<M<ODJI N@>ODJI <I? OC@ >JI?DODJI N@>ODJI.
The sections may be empty
25.3
AGG OCM@@ JA OC@ GPS GJJK N@>ODJIN H<T =@ G@AO @HKOT:
t 0JH@ODH@N < NK@>D<G GJJK Q<MD<=G@ DN IJO I@@?@?, KJNND=GT =@><PN@ <I <GM@<?T-?@ADI@?
Q<MD<=G@ RJPG? =@ PN@?.
t 0JH@ODH@N OC@ GJJK RJPG? =@ @SDO@? =T < CSFBL NO<O@H@IO DINO@<? JA =T M@GTDIB JI OC@
GJJK >JI?DODJI.
8AD "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
102
t 0JH@ODH@N OC@ DO@M<ODJI @SKM@NNDJIN ?@K@I? JI >@MO<DI >JI?DODJIN OC<O RJPG? =@ >C@>F@?
RDOCDI OC@ GJJK =J?T.
4C@I <GG JA OC@ N@>ODJIN <M@ @HOKT, OC@ GPS GJJK HJ?@GN 8AD7H7D:
GPS ( ; ; ) \
// ...
^
0P>C < GJJK H<T =@ ?@NDBI@? OJ I@Q@M @I? JM @I? RDOC < CSFBL NO<O@H@IO.
The name space of the loop variable
25.4
1C@ JIGT ?DAA@M@I>@ =@OR@@I OC@ GPS <I? XIJMF GJJK DN OC@ I<H@ NK<>@ JA OC@ Q<MD<=G@
?@ADI@? ?PMDIB GJJK KM@K<M<ODJI: 1C@ Q<MD<=G@ DN <>>@NND=G@ JIGT RDOCDI OC@ GPS GJJK, IJO
JPOND?@:
GPS (JOU J = 0; J < 5; ++J) \
// ...
^
XSJUFMO(J); // j DPNQJMBUJPO &3303
// J JT OPU BDDFTTJCMF IFSF
,I OC@ JOC@M C<I?, NDI>@ OC@ Q<MD<=G@ DN ?@ADI@? DI OC@ I<H@ NK<>@ OC<O >JIO<DIN OC@ XIJMF
GJJK, OC@ I<H@ DN <>>@NND=G@ @Q@I <AO@M OC@ GJJK:
JOU J = 0;
XIJMF (J < 5) \
// ...
++J;
^
XSJUFMO(J); // j XPSLT; J JT BDDFTTJCMF IFSF
4@ C<Q@ N@@I OC@ BPD?@GDI@ JA 678;@;@9 @3?7E 5>AE7EF FA F:7;D 8;DEF GE7 DI OC@ KM@QDJPN
>C<KO@M. 0DHDG<M OJ OC@ M<ODJI<G@ AJM OC<O BPD?@GDI@, OC@ NH<GG@M OC@ I<H@ NK<>@ JA < Q<MD<=G@
OC@ =@OO@M. &I OCDN M@B<M?, RC@I OC@ GJJK Q<MD<=G@ DN IJO I@@?@? JPOND?@ OC@ GJJK, GPS DN =@OO@M
OC<I XIJMF.
Exercises
25.5
1. -MDIO OC@ AJGGJRDIB 9S9 O<=G@ =T PNDIB ORJ GPS GJJKN, JI@ DIND?@ OC@ JOC@M:
0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8
1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8
2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7 2,8
3,0 3,1 3,2 3,3 3,4 3,5 3,6 3,7 3,8
4,0 4,1 4,2 4,3 4,4 4,5 4,6 4,7 4,8
5,0 5,1 5,2 5,3 5,4 5,5 5,6 5,7 5,8
6,0 6,1 6,2 6,3 6,4 6,5 6,6 6,7 6,8
7,0 7,1 7,2 7,3 7,4 7,5 7,6 7,7 7,8
8,0 8,1 8,2 8,3 8,4 8,5 8,6 8,7 8,8
2. 2N@ JI@ JM HJM@ GPS GJJKN OJ KMDIO OC@ * >C<M<>O@M <N I@@?@? OJ KMJ?P>@ B@JH@OMD><G
K<OO@MIN:
8AD "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
103
*
**
***
****
*****
******
*******
********
*********
********
********
********
********
********
********
********
********
********
@O>.
... F:7 EA>GF;A@E
8AD "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
104
Ternary Operator : 26
1C@ : JK@M<OJM RJMFN Q@MT NDHDG<MGT OJ <I JG-FMTF NO<O@H@IO:
JG (/* DPOEJUJPO DIFDL */) \
/* ... FYQSFTTJPO(T) UP FYFDVUF JG USVF */
^ FMTF \
/* ... FYQSFTTJPO(T) UP FYFDVUF JG GBMTF */
^
1C@ JG NO<O@H@IO @S@>PO@N @DOC@M OC@ =GJ>F AJM OC@ ><N@ JA USVF JM OC@ =GJ>F AJM OC@ ><N@ JA
GBMTF. AN TJP M@H@H=@M, =@DIB < NO<O@H@IO, DO ?J@N IJO C<Q@ < Q<GP@; JG H@M@GT <AA@>ON OC@
@S@>PODJI JA >J?@ =GJ>FN.
,I OC@ JOC@M C<I?, OC@ : JK@M<OJM DN <I @SKM@NNDJI. &I <??DODJI OJ RJMFDIB NDHDG<MT OJ OC@
JG-FMTF NO<O@H@IO, DO KMJ?P>@N < Q<GP@. 1C@ @LPDQ<G@IO JA OC@ <=JQ@ >J?@ DN OC@ AJGGJRDIB:
/* DPOEJUJPO */ /* USVUI FYQSFTTJPO */ : /* GBMTJUZ FYQSFTTJPO */
B@><PN@ DO PN@N OCM@@ @SKM@NNDJIN, OC@ : JK@M<OJM DN ><GG@? OC@ O@MI<MT JK@M<OJM.
1C@ Q<GP@ OC<O DN KMJ?P>@? =T OCDN JK@M<OJM DN @DOC@M OC@ Q<GP@ JA OC@ OMPOC @SKM@NNDJI JM OC@
Q<GP@ JA OC@ A<GNDOT @SKM@NNDJI. B@><PN@ DO DN <I @SKM@NNDJI, DO ><I =@ PN@? <ITRC@M@ OC<O
@SKM@NNDJIN ><I =@ PN@?.
1C@ AJGGJRDIB @S<HKG@N >JIOM<NO OC@ : JK@M<OJM OJ OC@ JG-FMTF NO<O@H@IO. 1C@ O@MI<MT
JK@M<OJM DN HJM@ >JI>DN@ AJM OC@ ><N@N OC<O <M@ NDHDG<M OJ OC@N@ @S<HKG@N.
t Initialization
1J DIDOD<GDU@ RDOC 366 DA G@<K T@<M, 365 JOC@MRDN@:
JOU EBZT = JT-FBQ:FBS 366 : 365;
4DOC JG, JI@ R<T DN OJ ?@ADI@ OC@ Q<MD<=G@ RDOCJPO <I @SKGD>DO DIDOD<G Q<GP@ <I? OC@I
<NNDBI OC@ DIO@I?@? Q<GP@:
JOU EBZT;
JG (JT-FBQ:FBS) \
EBZT = 366;
^ FMTF \
EBZT = 365;
^
AI <GO@MI<ODQ@ RDOC JG DN OJ DIDOD<GDU@ OC@ Q<MD<=G@ RDOC OC@ IJI-G@<K T@<M Q<GP@ <I?
OC@I DI>M@H@IO DO DA G@<K T@<M:
JOU EBZT = 365;
JG (JT-FBQ:FBS) \
++EBZT;
^
t Printing
-MDIODIB < K<MO JA < H@NN<B@ ?DAA@M@IOGT ?@K@I?DIB JI < >JI?DODJI:
)7D@3DK %B7D3FAD ?:
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
105
XSJUFMO("5IF HMBTT JT IBMG ",
JT0QUJNJTUJD "GVMM." : "FNQUZ.");
4DOC JG, OC@ ADMNO <I? G<NO K<MON JA OC@ H@NN<B@ H<T =@ KMDIO@? N@K<M<O@GT:
XSJUF("5IF HMBTT JT IBMG ");
JG (JT0QUJNJTUJD) \
XSJUFMO("GVMM.");
^ FMTF \
XSJUFMO("FNQUZ.");
^
AGO@MI<ODQ@GT, OC@ @IODM@ H@NN<B@ ><I =@ KMDIO@? N@K<M<O@GT:
JG (JT0QUJNJTUJD) \
XSJUFMO("5IF HMBTT JT IBMG GVMM.");
^ FMTF \
XSJUFMO("5IF HMBTT JT IBMG FNQUZ.");
^
t Calculation
&I>M@<NDIB OC@ N>JM@ JA OC@ RDII@M DI < =<>FB<HHJI B<H@ 2 KJDION JM 1 KJDIO
?@K@I?DIB JI RC@OC@M OC@ B<H@ C<N @I?@? RDOC B<HHJI;
TDPSF += JT(BNNPO 2 : 1;
0OM<DBCOAJMR<M? RDOC JG:
JG (JT(BNNPO) \
TDPSF += 2;
^ FMTF \
TDPSF += 1;
^
AI <GO@MI<ODQ@ RDOC JG DN OJ ADMNO DI>M@H@IO =T JI@ <I? OC@I DI>M@H@IO <B<DI DA
B<HHJI:
++TDPSF;
JG (JT(BNNPO) \
++TDPSF;
^
AN ><I =@ N@@I AMJH OC@ @S<HKG@N <=JQ@, OC@ >J?@ DN HJM@ >JI>DN@ <I? HJM@ >G@<M RDOC OC@
O@MI<MT JK@M<OJM DI >@MO<DI NDOP<ODJIN.
The types of the selection expressions must match
26.1
1C@ Q<GP@ JA OC@ : JK@M<OJM DN @DOC@M OC@ Q<GP@ JA OC@ OMPOC @SKM@NNDJI JM OC@ Q<GP@ JA OC@
A<GNDOT @SKM@NNDJI. 1C@ OTK@N JA OC@N@ ORJ @SKM@NNDJIN I@@? IJO =@ OC@ N<H@ =PO OC@T HPNO
H<O>C. #JM @S<HKG@, OC@T ><I =@ =JOC DIO@B@M OTK@N GDF@ JOU <I? MPOH =PO OC@T ><IIJO =@
HDNH<O>C@? OTK@N GDF@ JOU <I? TUSJOH.
#MJH OC@ @S<HKG@N <=JQ@, OC@ Q<GP@N OC<O RJPG? =@ N@G@>O@? ?@K@I?DIB JI OC@ Q<GP@ JA
JT-FBQ:FBS R@M@ 366 <I? 365. BJOC JA OCJN@ Q<GP@N <M@ JA OTK@ JOU <I? ?J H<O>C.
)7D@3DK %B7D3FAD ?:
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
106
1J N@@ < >JHKDG<ODJI @MMJM ?P@ OJ OC@ Q<GP@N JA OC@ @SKM@NNDJIN IJO H<O>CDIB, G@O'N GJJF <O
>JHKJNDIB < H@NN<B@ OC<O M@KJMON OC@ IPH=@M JA DO@HN OJ =@ NCDKK@?. )@O'N KMDIO "A ?JU@I"
RC@I OC@ Q<GP@ @LP<GN 12: "A ?JU@I DO@HN RDGG =@ NCDKK@?." ,OC@MRDN@, G@O'N C<Q@ OC@ H@NN<B@
DI>GP?@ OC@ @S<>O IPH=@M: "3 DO@HN RDGG =@ NCDKK@?."
,I@ HDBCO OCDIF OC<O OC@ Q<MTDIB K<MO JA OC@ H@NN<B@ ><I =@ N@G@>O@? RDOC OC@ : JK@M<OJM:
XSJUFMO((DPVOU == 12) "" EP[FO" : DPVOU, // j DPNQJMBUJPO &3303
" JUFNT XJMM CF TIJQQFE.");
1C@ @SKM@NNDJIN ?J IJO H<O>C =@><PN@ OC@ OTK@ JA "" EP[FO" DN TUSJOH <I? OC@ OTK@ JA DPVOU
DN JOU.
A NJGPODJI DN OJ >JIQ@MO DPVOU OJ TUSJOH <N R@GG. 1C@ API>ODJI UP!TUSJOH AMJH OC@ TUE.DPOW
HJ?PG@ KMJ?P>@N < TUSJOH Q<GP@ AMJH OC@ NK@>DAD@? K<M<H@O@M:
JNQPSU TUE.DPOW;
// ...
XSJUFMO((DPVOU == 12) "" EP[FO" : UP!TUSJOH(DPVOU),
" JUFNT XJMM CF TIJQQFE.");
+JR <N =JOC JA OC@ N@G@>ODJI @SKM@NNDJIN JA OC@ : JK@M<OJM <M@ JA TUSJOH OTK@, OC@ >J?@
>JHKDG@N <I? KMDION OC@ @SK@>O@? H@NN<B@.
Exercise
26.2
t %<Q@ OC@ KMJBM<H M@<? < NDIBG@ JOU Q<GP@ <N F:7 @7F 3?AG@F RC@M@ < KJNDODQ@ Q<GP@
M@KM@N@ION < B<DI <I? < I@B<ODQ@ Q<GP@ M@KM@N@ION < GJNN.
1C@ KMJBM<H NCJPG? KMDIO < H@NN<B@ OC<O >JIO<DIN "B<DI@?" JM "GJNO" ?@K@I?DIB JI
RC@OC@M OC@ <HJPIO DN KJNDODQ@ JM I@B<ODQ@. #JM @S<HKG@, "$100 GJNO" JM "$70 B<DI@?".
"Q@I OCJPBC DO H<T =@ HJM@ NPDO<=G@, ?J IJO PN@ OC@ JG NO<O@H@IO DI OCDN @S@M>DN@.
... F:7 EA>GF;A@
)7D@3DK %B7D3FAD ?:
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
107
Literals 27
-MJBM<HN <>CD@Q@ OC@DM O<NFN =T H<IDKPG<ODIB Q<GP@N JA Q<MD<=G@N <I? J=E@>ON. 1C@T KMJ?P>@
I@R Q<GP@N <I? I@R J=E@>ON =T PNDIB OC@H RDOC API>ODJIN <I? JK@M<OJMN.
0JH@ Q<GP@N I@@? IJO =@ KMJ?P>@? ?PMDIB OC@ @S@>PODJI JA OC@ KMJBM<H; OC@T <M@ RMDOO@I
?DM@>OGT DIOJ OC@ NJPM>@ >J?@. #JM @S<HKG@, OC@ AGJ<ODIB KJDIO Q<GP@ 0.75 <I? OC@ TUSJOH
Q<GP@ "5PUBM QSJDF: " =@GJR <M@ IJO ><G>PG<O@?:
EJTDPVOUFE1SJDF = BDUVBM1SJDF * 0.75;
UPUBM1SJDF += DPVOU * EJTDPVOUFE1SJDF;
XSJUFMO("5PUBM QSJDF: ", UPUBM1SJDF);
0P>C Q<GP@N OC<O <M@ OTK@? DIOJ OC@ NJPM>@ >J?@ GDO@M<GGT <M@ ><GG@? GDO@M<GN. 4@ C<Q@ PN@?
H<IT GDO@M<GN DI OC@ KMJBM<HN OC<O R@ C<Q@ RMDOO@I NJ A<M. 4@ RDGG >JQ@M <GG JA OC@ OTK@N JA
GDO@M<GN <I? OC@DM NTIO<S MPG@N.
Integer literals
27.1
&IO@B@M GDO@M<GN ><I =@ RMDOO@I DI JI@ JA AJPM R<TN: OC@ ?@>DH<G NTNO@H OC<O R@ PN@ DI JPM
?<DGT GDQ@N; OC@ C@S<?@>DH<G <I? =DI<MT NTNO@HN, RCD>C <M@ HJM@ NPDO<=G@ DI >@MO<DI
>JHKPODIB O<NFN; <I? OC@ J>O<G NTNO@H, RCD>C H<T =@ I@@?@? DI Q@MT M<M@ ><N@N.
&I JM?@M OJ H<F@ OC@ >J?@ HJM@ M@<?<=G@ JM AJM <IT JOC@M M@<NJI, DO DN KJNND=G@ OJ DIN@MO @
>C<M<>O@MN <ITRC@M@ <HJIB OC@ >C<M<>O@MN JA DIO@B@M GDO@M<GN. #JM @S<HKG@, OJ N@K<M<O@ OC@
IPH=@MN <O OCM@@ ?DBDO DIO@MQ<GN <N DI 1@234@567. 1C@N@ >C<M<>O@MN <M@ JKODJI<G; OC@T <M@
DBIJM@? =T OC@ >JHKDG@M.
In the decimal system: 1C@ GDO@M<GN <M@ NK@>DAD@? =T OC@ ?@>DH<G IPH@M<GN @S<>OGT OC@ N<H@
R<T <N R@ <M@ PN@? OJ DI JPM ?<DGT GDQ@N, <N DI 12. &I OCDN NTNO@H, OC@ ADMNO ?DBDO ><IIJO =@ 0 <N
OC<O ?DBDO DN M@N@MQ@? AJM DI?D><ODIB OC@ J>O<G NTNO@H DI ?AEF AF:7D >3@9G397E. &I !, DIO@B@M
GDO@M<GN ><IIJO NO<MO RDOC OC@ ?DBDO 0 OJ KM@Q@IO =PBN OC<O <M@ ><PN@? =T OCDN NP=OG@ ?DAA@M@I>@.
1CDN ?J@N IJO KM@>GP?@ 0 JI DON JRI: 0 DN U@MJ.
In the hexadecimal system: 1C@ GDO@M<GN NO<MO RDOC 0Y JM 09 <I? DI>GP?@ OC@ IPH@M<GN JA OC@
C@S<?@>DH<G NTNO@H: "0123456789<=>?@A" <I? "AB!"#" <N DI 0Y12BC00GF.
In the octal system: 1C@ GDO@M<GN <M@ NK@>DAD@? =T OC@ PDUBM O@HKG<O@ AMJH OC@ TUE.DPOW
HJ?PG@ <I? DI>GP?@ OC@ IPH@M<GN JA OC@ J>O<G NTNO@H: "01234567" <N DI PDUBM!576.
In the binary system: 1C@ GDO@M<GN NO<MO RDOC 0C JM 0# <I? DI>GP?@ OC@ IPH@M<GN JA OC@
=DI<MT NTNO@H: 0 <I? 1 <N DI 0C01100011.
The types of integer literals 27.1.1
'PNO GDF@ <IT JOC@M Q<GP@, @Q@MT GDO@M<G DN JA < >@MO<DI OTK@. 1C@ OTK@N JA GDO@M<GN <M@ IJO
NK@>DAD@? @SKGD>DOGT <N JOU, EPVCMF, @O>. 1C@ >JHKDG@M DIA@MN OC@ OTK@ AMJH OC@ Q<GP@ <I?
NTIO<S JA OC@ GDO@M<G DON@GA.
AGOCJPBC HJNO JA OC@ ODH@ OC@ OTK@N JA GDO@M<GN <M@ IJO DHKJMO<IO, NJH@ODH@N OC@ OTK@N H<T
IJO H<O>C OC@ @SKM@NNDJIN OC<O OC@T <M@ PN@? DI. &I NP>C ><N@N OC@ OTK@ HPNO =@ NK@>DAD@?
@SKGD>DOGT.
BT ?@A<PGO, DIO@B@M GDO@M<GN <M@ DIA@MM@? OJ =@ JA OTK@ JOU. 4C@I OC@ Q<GP@ C<KK@IN OJ =@ OJJ
G<MB@ OJ =@ M@KM@N@IO@? =T <I JOU, OC@ >JHKDG@M PN@N OC@ AJGGJRDIB GJBD> OJ ?@>D?@ JI OC@ OTK@
JA OC@ GDO@M<G:
";F7D3>E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
108
t &A OC@ Q<GP@ JA OC@ GDO@M<G ?J@N IJO ADO <I JOU <I? DO DN NK@>DAD@? DI OC@ ?@>DH<G NTNO@H OC@I
DON OTK@ DN MPOH.
t &O OC@ Q<GP@ JA OC@ GDO@M<G ?J@N IJO ADO <I JOU <I? DO DN NK@>DAD@? DI <IT JOC@M NTNO@H OC@I
OC@ OTK@ DN OMD@? OJ =@ VJOU, MPOH, <I? VMPOH, DI OC<O JM?@M, ?@K@I?DIB JI RCD>C OTK@ ><I
<>>JHJ?<O@ OC@ Q<GP@.
1J N@@ OCDN GJBD> DI <>ODJI, G@O'N OMT OC@ AJGGJRDIB KMJBM<H OC<O O<F@N <?Q<IO<B@ JA UZQFPG <I?
TUSJOHPG:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFMO("=O--- UIFTF BSF XSJUUFO JO EFDJNBM ---");
// GJUT BO JOU, TP UIF UZQF JT JOU
XSJUFMO( 2@147@483@647, "=U=U",
UZQFPG(2@147@483@647).TUSJOHPG);
// EPFT OPU GJU BO JOU BOE JT EFDJNBM, TP UIF UZQF JT MPOH
XSJUFMO( 2@147@483@648, "=U=U",
UZQFPG(2@147@483@648).TUSJOHPG);
XSJUFMO("=O--- UIFTF BSF /05 XSJUUFO JO EFDJNBM ---");
// GJUT BO JOU, TP UIF UZQF JT JOU
XSJUFMO( 0Y7'''@'''', "=U=U",
UZQFPG(0Y7'''@'''').TUSJOHPG);
// EPFT OPU GJU BO JOU BOE JT OPU EFDJNBM, TP UIF UZQF JT VJOU
XSJUFMO( 0Y8000@0000, "=U=U",
UZQFPG(0Y8000@0000).TUSJOHPG);
// EPFT OPU GJU B VJOU BOE JT OPU EFDJNBM, TP UIF UZQF JT MPOH
XSJUFMO( 0Y1@0000@0000, "=U=U",
UZQFPG(0Y1@0000@0000).TUSJOHPG);
// EPFT OPU GJU B MPOH BOE JT OPU EFDJNBM, TP UIF UZQF JT VMPOH
XSJUFMO( 0Y8000@0000@0000@0000, "=U=U",
UZQFPG(0Y8000@0000@0000@0000).TUSJOHPG);
^
1C@ JPOKPO:
--- UIFTF BSF XSJUUFO JO EFDJNBM ---
2147483647 JOU
2147483648 MPOH
--- UIFTF BSF /05 XSJUUFO JO EFDJNBM ---
2147483647 JOU
2147483648 VJOU
4294967296 MPOH
9223372036854775808 VMPOH
The - suffix 27.1.2
/@B<M?G@NN JA OC@ H<BIDOP?@ JA OC@ Q<GP@, DA DO @I?N RDOC - <N DI 10-, OC@ OTK@ DN MPOH.
The 6 suffix 27.1.3
/@B<M?G@NN JA OC@ H<BIDOP?@ JA OC@ Q<GP@, DA DO @I?N RDOC 6 <N DI 106, OC@ OTK@ DN VOTJHOFE.
)JR@M><N@ V ><I <GNJ =@ PN@?.
";F7D3>E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
109
1C@ - <I? 6 NK@>DAD@MN ><I =@ PN@? OJB@OC@M DI <IT JM?@M. #JM @S<HKG@, 76- <I? 8-6 <M@ =JOC
VMPOH.
Floating point literals
27.2
1C@ AGJ<ODIB KJDIO GDO@M<GN ><I =@ NK@>DAD@? DI @DOC@M OC@ ?@>DH<G NTNO@H <N DI 1.234 JM DI OC@
C@S<?@>DH<G NTNO@H <N DI 0Y9B.CD.
In the decimal system: AI @SKJI@IO H<T =@ <KK@I?@? <AO@M OC@ >C<M<>O@M F JM & H@<IDIB
"ODH@N 10 OJ OC@ KJR@M JA". #JM @S<HKG@, 3.4F5 H@<IN "3.4 ODH@N 10 OJ OC@ KJR@M JA 5". A +
>C<M<>O@M H<T <GNJ =@ NK@>DAD@? =@AJM@ OC@ Q<GP@ JA OC@ @SKJI@IO, =PO DO C<N IJ @AA@>O. #JM
@S<HKG@ 5.6F2 <I? 5.6F+2 <M@ OC@ N<H@.
1C@ - >C<M<>O@M OTK@? =@AJM@ OC@ Q<GP@ JA OC@ @SKJI@IO >C<IB@N OC@ H@<IDIB OJ =@ "?DQD?@?
=T 10 OJ OC@ KJR@M JA". #JM @S<HKG@, 7.8F-3 H@<IN "7.8 ?DQD?@? =T 10 OJ OC@ KJR@M JA 3".
In the hexadecimal system: 1C@ Q<GP@ NO<MON RDOC @DOC@M 0Y JM 09 <I? OC@ K<MON =@AJM@ <I?
<AO@M OC@ KJDIO <M@ NK@>DAD@? DI OC@ IPH@M<GN JA OC@ C@S<?@>DH<G NTNO@H. 0DI>@ F <I? & <M@
Q<GD? IPH@M<GN DI OCDN NTNO@H, OC@ @SKJI@IO DN NK@>DAD@? =T Q JM 1.
AIJOC@M ?DAA@M@I>@ DN OC<O OC@ @SKJI@IO ?J@N IJO H@<I "10 OJ OC@ KJR@M JA", =PO "2 OJ OC@
KJR@M JA". #JM @S<HKG@, OC@ 14 K<MO DI 0YBCD.EFG14 H@<IN "2 OJ OC@ KJR@M JA 4".
#GJ<ODIB KJDIO GDO@M<GN <GHJNO <GR<TN C<Q@ < KJDIO =PO DO H<T =@ JHDOO@? DA OC@M@ DN <I
@SKJI@IO NK@>DAD@?. #JM @S<HKG@, 2F3 DN < AGJ<ODIB KJDIO GDO@M<G RDOC OC@ Q<GP@ 2000.
1C@ Q<GP@ =@AJM@ OC@ KJDIO H<T =@ JHDOO@? DA U@MJ. #JM @S<HKG@, .25 DN < GDO@M<G C<QDIB OC@
Q<GP@ "LP<MO@M".
1C@ JKODJI<G @ >C<M<>O@MN H<T =@ PN@? RDOC AGJ<ODIB KJDIO GDO@M<GN <N R@GG, <N DI 1@000.5.
The types of floating point literals 27.2.1
2IG@NN @SKGD>DOGT NK@>DAD@?, OC@ OTK@ JA < AGJ<ODIB KJDIO GDO@M<G DN EPVCMF. 1C@ G <I? '
NK@>DAD@MN H@<I GMPBU, <I? OC@ - NK@>DAD@M H@<IN SFBM. #JM @S<HKG@; 1.2 DN EPVCMF, 3.4G DN
GMPBU, <I? 5.6- DN SFBM.
Character literals
27.3
C<M<>O@M GDO@M<GN <M@ NK@>DAD@? RDOCDI NDIBG@ LPJO@N <N DI 'B', '=O', '=Y21', @O>.
As the character itself: 1C@ >C<M<>O@M H<T =@ OTK@? ?DM@>OGT =T OC@ F@T=J<M? JM >JKD@?
AMJH < N@K<M<O@ O@SO: '<', 'f', @O>.
As the character specifier: 1C@ >C<M<>O@M GDO@M<G H<T =@ NK@>DAD@? =T < =<>FNG<NC >C<M<>O@M
AJGGJR@? =T < NK@>D<G G@OO@M. #JM @S<HKG@, OC@ =<>FNG<NC >C<M<>O@M DON@GA ><I =@ NK@>DAD@? =T
'=='. C<M<>O@M NK@>DAD@M G@OO@MN <M@ OC@ AJGGJRDIB:
";F7D3>E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
110
4ZOUBY %FGJOJUJPO
=' TJOHMF RVPUF
=" EPVCMF RVPUF
= RVFTUJPO NBSL
== CBDLTMBTI
=B BMFSU (CFMM TPVOE PO TPNF UFSNJOBMT)
=C EFMFUF DIBSBDUFS
=G OFX QBHF
=O OFX-MJOF
=S DBSSJBHF SFUVSO
=U UBC
=W WFSUJDBM UBC
As the extended ASCII character code: C<M<>O@M GDO@M<GN ><I =@ NK@>DAD@? ?DM@>OGT =T
OC@DM >J?@N. 1C@ >J?@N ><I =@ NK@>DAD@? @DOC@M DI OC@ C@S<?@>DH<G NTNO@H JM DI OC@ J>O<G
NTNO@H. 4C@I PNDIB OC@ C@S<?@>DH<G NTNO@H, OC@ GDO@M<G HPNO NO<MO RDOC =Y <I? HPNO PN@ ORJ
?DBDON AJM OC@ >J?@, <I? RC@I PNDIB OC@ J>O<G NTNO@H OC@ GDO@M<G HPNO NO<MO RDOC = <I? C<Q@ PK
OJ OCM@@ ?DBDON. #JM @S<HKG@, OC@ GDO@M<GN '=Y21' <I? '=41' <M@ =JOC OC@ @S>G<H<ODJI KJDIO.
As the Unicode character code: 4C@I OC@ GDO@M<G DN NK@>DAD@? RDOC V AJGGJR@? =T 4
C@S<?@>DH<G ?DBDON, OC@I DON OTK@ DN XDIBS. 4C@I DO'N NK@>DAD@? RDOC 6 AJGGJR@? =T 8
C@S<?@>DH<G ?DBDON, OC@I DON OTK@ DN EDIBS. #JM @S<HKG@, '=V011F' <I? '=60000011F' <M@
=JOC OC@ a >C<M<>O@M, C<QDIB OC@ OTK@ XDIBS <I? EDIBS, M@NK@>ODQ@GT.
As named character entity: C<M<>O@MN OC<O C<Q@ @IODOT I<H@N ><I =@ NK@>DAD@? =T OC<O
I<H@ =T OC@ NTIO<S '=&name;'. (0@@ OC@ +<H@? C<M<>O@M "IODOD@N O<=G@.) #JM @S<HKG@,
'=&FVSP;' DN u, '=&IFBSUT;' DN |, <I? '=&DPQZ;' DN W.
String literals
27.4
0OMDIB GDO@M<GN <M@ < >JH=DI<ODJI JA >C<M<>O@M GDO@M<GN <I? ><I =@ NK@>DAD@? DI < Q<MD@OT JA
R<TN.
Double-quoted string literals 27.4.1
1C@ HJNO >JHHJI R<T JA NK@>DATDIB NOMDIB GDO@M<GN DN OTKDIB OC@DM >C<M<>O@MN RDOCDI ?JP=G@
LPJO@N <N DI "IFMMP". &I?DQD?P<G >C<M<>O@MN JA NOMDIB GDO@M<GN AJGGJR OC@ MPG@N JA >C<M<>O@M
GDO@M<GN. #JM @S<HKG@, OC@ GDO@M<G ""4 LB=V011GgU: 3=&GSBD12;5-" DN OC@ N<H@ <N
""4 LBegU: 3`5-".
Wysiwyg string literals 27.4.2
4C@I NOMDIB GDO@M<GN <M@ NK@>DAD@? RDOCDI =<>F-LPJO@N, OC@ DI?DQD?P<G >C<M<>O@MN JA OC@ NOMDIB
?J IJO J=@T OC@ NK@>D<G NTIO<S MPG@N JA >C<M<>O@M GDO@M<GN. #JM @S<HKG@, OC@ GDO@M<G
AD:=OVSUFOA ><I =@ < ?DM@>OJMT I<H@ JI OC@ 4DI?JRN JK@M<ODJIB NTNO@H. &A DO R@M@ RMDOO@I
RDOCDI ?JP=G@ LPJO@N, OC@ '=O' K<MO RJPG? H@<I OC@ @7I->;@7 >C<M<>O@M:
XSJUFMO(AD:=OVSUFOA);
XSJUFMO("D:=OVSUFO");
";F7D3>E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
111
D:=OVSUFO l XZTJXZH (XIBU ZPV TFF JT XIBU ZPV HFU)
D: l UIF DIBSBDUFS MJUFSBM JT UBLFO BT OFX-MJOF
VSUFO
4TNDRTB NOMDIB GDO@M<GN ><I <GO@MI<ODQ@GT =@ NK@>DAD@? RDOCDI ?JP=G@ LPJO@N =PO KM@K@I?@?
RDOC OC@ S >C<M<>O@M: S"D:=OVSUFO" DN <GNJ < RTNDRTB NOMDIB GDO@M<G.
Hexadecimal string literals 27.4.3
&I NDOP<ODJIN RC@M@ @Q@MT >C<M<>O@M DI < NOMDIB I@@?N OJ =@ NK@>DAD@? DI C@S<?@>DH<G NTNO@H,
DINO@<? JA OTKDIB =Y =@AJM@ @Q@MT JI@ JA OC@H, < NDIBG@ Y >C<M<>O@M H<T =@ OTK@? =@AJM@ OC@
JK@IDIB ?JP=G@ LPJO@. &I OC<O ><N@, @Q@MT >C<M<>O@M DI OC@ NOMDIB GDO@M<G DN O<F@I OJ =@
C@S<?@>DH<G. A??DODJI<GGT, OC@ NOMDIB GDO@M<G H<T >JIO<DI NK<>@N, RCD>C <M@ DBIJM@? =T OC@
>JHKDG@M. #JM @S<HKG@, "=Y44=Y64=Y69=Y6D=Y69" <I? Y"44 64 69 6D 69" <M@ OC@ N<H@
NOMDIB GDO@M<G.
Delimited string literals 27.4.4
1C@ NOMDIB GDO@M<G H<T >JIO<DI ?@GDHDO@MN OC<O <M@ OTK@? MDBCO DIND?@ OC@ ?JP=G@ LPJO@N. 1C@N@
?@GDHDO@MN <M@ IJO >JIND?@M@? OJ =@ K<MON JA OC@ Q<GP@ JA OC@ GDO@M<G. !@GDHDO@? NOMDIB GDO@M<GN
NO<MO RDOC < R =@AJM@ OC@ JK@IDIB ?JP=G@ LPJO@. #JM @S<HKG@, OC@ Q<GP@ JA R".IFMMP." DN
"C@GGJ"; OC@ ?JON <M@ IJO K<MON JA OC@ Q<GP@. AN GJIB <N DO @I?N RDOC < I@R-GDI@, OC@ ?@GDHDO@M
><I C<Q@ HJM@ OC<I JI@ >C<M<>O@M:
XSJUFMO(R".:@%&-*.*5&3
GJSTU MJOF
TFDPOE MJOF
.:@%&-*.*5&3");
*6;!")&*&1"/ DN IJO < K<MO JA OC@ Q<GP@:
GJSTU MJOF
TFDPOE MJOF
Token string literals 27.4.5
0OMDIB GDO@M<GN OC<O NO<MO RDOC R <I? OC<O PN@ \ <I? ^ <N ?@GDHDO@MN ><I >JIO<DI JIGT G@B<G !
>J?@:
BVUP TUS = R\JOU OVNCFS = 42; ++OVNCFS;^;
XSJUFMO(TUS);
1C@ JPOKPO:
JOU OVNCFS = 42; ++OVNCFS;
1CDN A@<OPM@ DN K<MOD>PG<MGT PN@APG OJ C@GK O@SO @?DOJMN CDBCGDBCO OC@ >J?@ DIND?@ OC@ NOMDIB <N !
>J?@.
Types of string literals 27.4.6
BT ?@A<PGO OC@ OTK@ JA < NOMDIB GDO@M<G DN JNNVUBCMF(DIBS)<>. AI <KK@I?@? D, X, JM E
>C<M<>O@M NK@>DAD@N OC@ OTK@ JA OC@ NOMDIB @SKGD>DOGT <N JNNVUBCMF(DIBS)<>,
";F7D3>E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
112
JNNVUBCMF(XDIBS)<>, JM JNNVUBCMF(EDIBS)<>, M@NK@>ODQ@GT. #JM @S<HKG@, OC@ >C<M<>O@MN JA
"IFMMP"E <M@ JA OTK@ JNNVUBCMF(EDIBS).
4@ C<Q@ N@@I DI OC@ 0OMDIBN >C<KO@M OC<O OC@N@ OCM@@ NOMDIB OTK@N <M@ <GD<N@? <N TUSJOH,
XTUSJOH, <I? ETUSJOH M@NK@>ODQ@GT.
Literals are calculated at compile time
27.5
&O DN KJNND=G@ OJ NK@>DAT GDO@M<GN <N @SKM@NNDJIN. #JM @S<HKG@, DINO@<? JA RMDODIB OC@ OJO<G
IPH=@M JA N@>JI?N DI '<IP<MT <N 2678400 JM 2@678@400, DO DN KJNND=G@ OJ NK@>DAT DO =T OC@
O@MHN OC<O H<F@ PK OC<O Q<GP@ <N 60 * 60 * 24 * 31. 1C@ HPGODKGD><ODJI JK@M<ODJIN DI OC<O
@SKM@NNDJI ?J IJO <AA@>O OC@ MPI-ODH@ NK@@? JA OC@ KMJBM<H, <N OC@ KMJBM<H RJPG? =@
>JHKDG@? <N DA 2678400 R@M@ RMDOO@I DINO@<?.
1C@ N<H@ <KKGD@N OJ NOMDIB GDO@M<GN. #JM @S<HKG@, OC@ >JI><O@I<ODJI JK@M<ODJI DI
"IFMMP " _ "XPSME" RJPG? =@ @S@>PO@? <O >JHKDG@ ODH@, IJO <O MPI ODH@. 1C@ KMJBM<H
RJPG? =@ >JHKDG@? <N DA OC@ >J?@ >JIO<DIN OC@ NDIBG@ NOMDIB GDO@M<G "IFMMP XPSME".
Exercises
27.6
1. 1C@ AJGGJRDIB GDI@ ><PN@N < >JHKDG<ODJI @MMJM:
JOU BNPVOU = 10@000@000@000; // j DPNQJMBUJPO &3303
C<IB@ OC@ KMJBM<H NJ OC<O OC@ GDI@ ><I =@ >JHKDG@? <I? OC<O BNPVOU @LP<GN O@I
=DGGDJIN.
2. 4MDO@ < KMJBM<H OC<O DI>M@<N@N OC@ Q<GP@ JA < Q<MD<=G@ <I? KMDION DO DI <I DIADIDO@ GJJK.
*<F@ OC@ Q<GP@ =@ KMDIO@? JI OC@ N<H@ GDI@:
/VNCFS: 25774 l BMXBZT PO UIF TBNF MJOF
A NK@>D<G >C<M<>O@M GDO@M<G JOC@M OC<I '=O' H<T =@ PN@APG C@M@.
... F:7 EA>GF;A@E
";F7D3>E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
113
Formatted Output 28
1CDN >C<KO@M DN <=JPO A@<OPM@N JA -CJ=JN'N TUE.GPSNBU HJ?PG@, IJO <=JPO OC@ >JM@ A@<OPM@N JA
OC@ ! G<IBP<B@.
!'N DIKPO <I? JPOKPO AJMH<O NK@>DAD@MN <M@ NDHDG<M OJ OC@ JI@N DI OC@ G<IBP<B@.
B@AJM@ BJDIB APMOC@M, & RJPG? GDF@ OJ NPHH<MDU@ OC@ AJMH<O NK@>DAD@MN <I? AG<BN <N <
M@A@M@I>@:
'MBHT (DBO CF VTFE UPHFUIFS)
- GMVTI MFGU
+ QSJOU TJHO
# QSJOU JO UIF BMUFSOBUJWF XBZ
0 QSJOU [FSP-GJMMFE
space QSJOU TQBDF-GJMMFE
'PSNBU 4QFDJGJFST
T EFGBVMU
C CJOBSZ
E EFDJNBM
P PDUBM
Y,9 IFYBEFDJNBM
F,& TDJFOUJGJD GMPBUJOH QPJOU
G,' EPUUFE GMPBUJOH QPJOU
H,( BT F PS G
B," IFYBEFDJNBM GMPBUJOH QPJOU
4@ C<Q@ =@@I PNDIB API>ODJIN GDF@ XSJUFMO RDOC HPGODKG@ K<M<H@O@MN <N I@>@NN<MT. 1C@
K<M<H@O@MN RJPG? =@ >JIQ@MO@? OJ OC@DM NOMDIB M@KM@N@IO<ODJIN <I? OC@I N@IO OJ OC@ JPOKPO.
0JH@ODH@N OCDN DN IJO NPAAD>D@IO. 1C@ JPOKPO H<T C<Q@ OJ =@ DI < >@MO<DI AJMH<O. )@O'N GJJF <O
OC@ AJGGJRDIB >J?@ OC<O DN PN@? OJ KMDIO DO@HN JA <I DIQJD>@:
JUFNT _= 1.23;
JUFNT _= 45.6;
GPS (JOU J = 0; J != JUFNT.MFOHUI; ++J) \
XSJUFMO("*UFN ", J + 1, ": ", JUFNT<J>);
^
1C@ JPOKPO:
*UFN 1: 1.23
*UFN 2: 45.6
!@NKDO@ OC@ DIAJMH<ODJI =@DIB >JMM@>O, DO H<T =@ M@LPDM@? OJ =@ KMDIO@? DI < >@MO<DI AJMH<O.
#JM @S<HKG@ OC@ ?JON H<T C<Q@ OJ =@ GDI@? PK <I? OC<O OC@M@ HPNO <GR<TN =@ ORJ ?DBDON <AO@M
OC@ ?JO <N DI OC@ AJGGJRDIB JPOKPO:
*UFN 1: 1.23
*UFN 2: 45.60
#JMH<OO@? JPOKPO DN PN@APG DI NP>C ><N@N. 1C@ JPOKPO API>ODJIN OC<O R@ C<Q@ =@@I PNDIB NJ A<M
C<N >JPIO@MK<MON OC<O >JIO<DI OC@ G@OO@M G DI OC@DM I<H@N: XSJUFG() <I? XSJUFGMO(). 1C@
G@OO@M G DN NCJMO AJM 8AD?3FF76. 1C@ ADMNO K<M<H@O@M JA OC@N@ API>ODJIN DN < 8AD?3F EFD;@9 OC<O
?@N>MD=@N CJR OC@ JOC@M K<M<H@O@MN NCJPG? =@ KMDIO@?.
#JM @S<HKG@, OC@ AJMH<O NOMDIB OC<O RJPG? C<Q@ XSJUFGMO() KMJ?P>@ OC@ ?@NDM@? JPOKPO
<=JQ@ DN OC@ AJGGJRDIB:
FAD?3FF76 %GFBGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
114
XSJUFGMO("*UFN %E:%9.02G", J + 1, JUFNT<J>);
1C@ AJMH<O NOMDIB >JIO<DIN M@BPG<M >C<M<>O@MN OC<O <M@ K<NN@? OJ OC@ JPOKPO <N-DN, <N R@GG <N
NK@>D<G AJMH<O NK@>DAD@MN OC<O >JMM@NKJI? OJ @<>C K<M<H@O@M OC<O DN OJ =@ KMDIO@?. #JMH<O
NK@>DAD@MN NO<MO RDOC OC@ % >C<M<>O@M <I? @I?N RDOC < 8AD?3F 5:3D35F7D. 1C@ AJMH<O NOMDIB
<=JQ@ C<N ORJ AJMH<O NK@>DAD@MN: %E <I? %9.02G.
"Q@MT NK@>DAD@M DN <NNJ>D<O@? OJ OC@ AJGGJRDIB K<M<H@O@MN DI JM?@M. #JM @S<HKG@, %E DN
<NNJ>D<O@? RDOC J + 1 <I? %9.02G DN <NNJ>D<O@? RDOC JUFNT<J>. "Q@MT NK@>DAD@M NK@>DAD@N OC@
AJMH<O JA OC@ K<M<H@O@M OC<O DO >JMM@NKJI?N OJ. (#JMH<O NK@>DAD@MN H<T C<Q@ K<M<H@O@M
IPH=@MN <N R@GG. 1CDN RDGG =@ @SKG<DI@? G<O@M DI OC@ >C<KO@M.)
AGG JA OC@ JOC@M >C<M<>O@MN JA OC@ AJMH<O NOMDIB OC<O <M@ IJO K<MON JA AJMH<O NK@>DAD@MN <M@
KMDIO@? <N-DN. 0P>C D79G>3D >C<M<>O@MN JA OC@ AJMH<O NK@>DAD@M <=JQ@ <M@ CDBCGDBCO@? <N M@? <N
DI "*UFN %E:%9.02G".
#JMH<O NK@>DAD@MN >JINDNO JA NDS K<MON HJNO JA RCD>C <M@ JKODJI<G. 1C@ K<MO I<H@? BAE;F;A@ RDGG
=@ @SKG<DI@? G<O@M =@GJR. 1C@ JOC@M ADQ@ <M@ OC@ AJGGJRDIB (Note: ):7 EB357E 47FI77@ F:7E7
B3DFE 3D7 ;@E7DF76 :7D7 FA :7>B I;F: D73634;>;FK; F:7K 3D7 @AF B3DFE A8 F:7 EB75;8;7DE.):
% flags width precision format_character
1C@ % >C<M<>O@M <O OC@ =@BDIIDIB <I? OC@ AJMH<O >C<M<>O@M <O OC@ @I? <M@ M@LPDM@?; OC@
JOC@MN <M@ JKODJI<G.
B@><PN@ % C<N < NK@>D<G H@<IDIB DI AJMH<O NOMDIBN, RC@I < % I@@?N OJ =@ KMDIO@? <N < M@BPG<M
>C<M<>O@M, DO HPNO =@ OTK@? <N %%.
format_character
28.1
C: &IO@B@M K<M<H@O@M DN KMDIO@? DI OC@ =DI<MT NTNO@H.
P: &IO@B@M K<M<H@O@M DN KMDIO@? DI OC@ J>O<G NTNO@H.
Y <I? 9: &IO@B@M K<M<H@O@M DN KMDIO@? DI OC@ C@S<?@>DH<G NTNO@H; RDOC GJR@M><N@ G@OO@MN AJM Y
<I? PKK@M><N@ G@OO@MN AJM 9.
E: &IO@B@M K<M<H@O@M DN KMDIO@? DI ?@>DH<G NTNO@H; < I@B<ODQ@ NDBI DN <GNJ KMDIO@? DA DO DN <
NDBI@? OTK@ <I? OC@ Q<GP@ DN G@NN OC<I U@MJ.
XSJUFGMO("#JOBSZ : %C", WBMVF);
XSJUFGMO("0DUBM : %P", WBMVF);
XSJUFGMO(")FYBEFDJNBM: %Y", WBMVF);
XSJUFGMO("%FDJNBM : %E", WBMVF);
#JOBSZ : 1100
0DUBM : 14
)FYBEFDJNBM: D
%FDJNBM : 12
F: #GJ<ODIB KJDIO K<M<H@O@M DN KMDIO@? <>>JM?DIB OJ OC@ AJGGJRDIB MPG@N.
t NDIBG@ ?DBDO =@AJM@ OC@ ?JO
t ?JO DA BD75;E;A@ DN IJI-U@MJ
t ?DBDON <AO@M OC@ ?JO, IPH=@M JA RCD>C DN ?@O@MHDI@? =T BD75;E;A@ (?@A<PGO KM@>DNDJI DN 6)
t OC@ F >C<M<>O@M (H@<IDIB "10 OJ OC@ KJR@M JA")
FAD?3FF76 %GFBGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
115
t OC@ - JM + >C<M<>O@M ?@K@I?DIB JI RC@OC@M OC@ @SKJI@IO DN G@NN OC<I JM BM@<O@M OC<I U@MJ
t OC@ @SKJI@IO >JINDNODIB JA <O G@<NO ORJ ?DBDON
&: 0<H@ <N DI F RDOC OC@ @S>@KODJI JA OC@ & >C<M<>O@M <O OC@ JPOKPO DINO@<? JA F.
G <I? ': #GJ<ODIB KJDIO K<M<H@O@M DN KMDIO@? DI OC@ ?@>DH<G NTNO@H; OC@M@ DN <O G@<NO JI@ ?DBDO
=@AJM@ OC@ ?JO <I? OC@ ?@A<PGO KM@>DNDJI DN 6.
H: 0<H@ <N DI G DA OC@ @SKJI@IO DN =@OR@@I -5 <I? BD75;E;A@; JOC@MRDN@ <N DI F. BD75;E;A@ ?J@N
IJO NK@>DAT OC@ IPH=@M JA ?DBDON <AO@M OC@ ?JO, =PO OC@ NDBIDAD><IO ?DBDON JA OC@ @IODM@ Q<GP@. &A
OC@M@ <M@ IJ NDBIDAD><IO ?DBDON <AO@M OC@ ?JO, OC@I OC@ ?JO DN IJO KMDIO@?. 1C@ MDBCOHJNO U@MJN
<AO@M OC@ ?JO <M@ IJO KMDIO@?.
(: 0<H@ <N DI H RDOC OC@ @S>@KODJI JA & JM ' <O OC@ JPOKPO.
B: #GJ<ODIB KJDIO K<M<H@O@M DN KMDIO@? DI OC@ C@S<?@>DH<G AGJ<ODIB AJMH<O:
t OC@ >C<M<>O@MN 0Y
t NDIBG@ C@S<?@>DH<G ?DBDO
t ?JO DA BD75;E;A@ DN IJI-U@MJ
t ?DBDON <AO@M OC@ ?JO, IPH=@M JA RCD>C DN ?@O@MHDI@? =T BD75;E;A@; DA IJ BD75;E;A@ DN
NK@>DAD@?, OC@I <N H<IT ?DBDON <N I@>@NN<MT
t OC@ Q >C<M<>O@M (H@<IDIB "2 OJ OC@ KJR@M JA")
t OC@ - JM + >C<M<>O@M ?@K@I?DIB JI RC@OC@M OC@ @SKJI@IO DN G@NN OC<I JM BM@<O@M OC<I U@MJ
t OC@ @SKJI@IO >JINDNODIB JA <O G@<NO JI@ ?DBDO (OC@ @SKJI@IO JA OC@ Q<GP@ 0 DN 0)
": 0<H@ <N DI B RDOC OC@ @S>@KODJI JA OC@ 09 <I? 1 >C<M<>O@MN <O OC@ JPOKPO.
EPVCMF WBMVF = 123.456789;
XSJUFGMO("XJUI F: %F", WBMVF);
XSJUFGMO("XJUI G: %G", WBMVF);
XSJUFGMO("XJUI H: %H", WBMVF);
XSJUFGMO("XJUI B: %B", WBMVF);
XJUI F: 1.234568F+02
XJUI G: 123.456789
XJUI H: 123.457
XJUI B: 0Y1.FEE3D07FF0C0CQ+6
T: 1C@ Q<GP@ DN KMDIO@? DI OC@ N<H@ R<T <N DI IJI-AJMH<OO@? JPOKPO, <>>JM?DIB OJ OC@ OTK@ JA
OC@ K<M<H@O@M:
t CPPM Q<GP@N <N USVF JM GBMTF
t DIO@B@M Q<GP@N N<H@ <N %E
t AGJ<ODIB KJDIO Q<GP@N N<H@ <N %H
t NOMDIBN DI 21#-8 @I>J?DIB; BD75;E;A@ ?@O@MHDI@N OC@ H<SDHPH IPH=@M JA =TO@N OJ PN@
(M@H@H=@M OC<O DI 21#-8 @I>J?DIB, OC@ IPH=@M JA =TO@N DN IJO OC@ N<H@ <N OC@ IPH=@M JA
>C<M<>O@MN; AJM @S<HKG@ OC@ NOMDIB "<b" C<N 2 >C<M<>O@MN, >JINDNODIB < OJO<G JA 3 =TO@N)
t NOMP>O <I? >G<NN J=E@>ON <N OC@ M@OPMI Q<GP@ JA OC@ UP4USJOH() H@H=@M API>ODJIN JA OC@DM
OTK@N; BD75;E;A@ ?@O@MHDI@N OC@ H<SDHPH IPH=@M JA =TO@N OJ PN@
t <MM<TN <N OC@DM @G@H@IO Q<GP@N ND?@ =T ND?@
CPPM C = USVF;
JOU J = 365;
EPVCMF E = 9.87;
TUSJOH T = "GPSNBUUFE";
BVUP P = 'JMF("UFTU@GJMF", "S");
FAD?3FF76 %GFBGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
116
JOU<> B = < 2, 4, 6, 8 >;
XSJUFGMO("CPPM : %T", C);
XSJUFGMO("JOU : %T", J);
XSJUFGMO("EPVCMF: %T", E);
XSJUFGMO("TUSJOH: %T", T);
XSJUFGMO("PCKFDU: %T", P);
XSJUFGMO("BSSBZ : %T", B);
CPPM : USVF
JOU : 365
EPVCMF: 9.87
TUSJOH: GPSNBUUFE
PCKFDU: 'JMF(55738'"0)
BSSBZ : <2, 4, 6, 8>
width
28.2
1CDN K<MO ?@O@MHDI@N OC@ RD?OC JA OC@ AD@G? OC<O OC@ K<M<H@O@M DN KMDIO@? DI. &A OC@ RD?OC DN
NK@>DAD@? <N OC@ >C<M<>O@M *, OC@I OC@ <>OP<G RD?OC Q<GP@ DN M@<? AMJH OC@ I@SO K<M<H@O@M. &A
RD?OC DN < I@B<ODQ@ Q<GP@, OC@I OC@ - AG<B DN <NNPH@?.
JOU WBMVF = 100;
XSJUFGMO("*O B GJFME PG 10 DIBSBDUFST:%10T", WBMVF);
XSJUFGMO("*O B GJFME PG 5 DIBSBDUFST :%5T", WBMVF);
*O B GJFME PG 10 DIBSBDUFST: 100
*O B GJFME PG 5 DIBSBDUFST : 100
precision
28.3
-M@>DNDJI DN NK@>DAD@? <AO@M < ?JO DI OC@ AJMH<O NK@>DAD@M. #JM AGJ<ODIB KJDIO OTK@N, DO
?@O@MHDI@N OC@ KM@>DNDJI JA OC@ KMDIO@? M@KM@N@IO<ODJI JA OC@ Q<GP@N. &A OC@ KM@>DNDJI DN
NK@>DAD@? <N OC@ >C<M<>O@M *, OC@I OC@ <>OP<G KM@>DNDJI DN M@<? AMJH OC@ I@SO K<M<H@O@M (OC<O
K<M<H@O@M HPNO =@ <I JOU). +@B<ODQ@ KM@>DNDJI Q<GP@N <M@ DBIJM@?.
EPVCMF WBMVF = 1234.56789;
XSJUFGMO("%.8H", WBMVF);
XSJUFGMO("%.3H", WBMVF);
XSJUFGMO("%.8G", WBMVF);
XSJUFGMO("%.3G", WBMVF);
1234.5679
1.23F+03
1234.56789000
1234.568
BVUP OVNCFS = 0.123456789;
XSJUFGMO("/VNCFS: %.*H", 4, OVNCFS);
/VNCFS: 0.1235
FAD?3FF76 %GFBGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
117
flags
28.4
*JM@ OC<I JI@ AG<B ><I =@ NK@>DAD@?.
-: OC@ Q<GP@ DN KMDIO@? G@AO-<GDBI@? DI DON AD@G?; OCDN AG<B ><I>@GN OC@ 0 AG<B
JOU WBMVF = 123;
XSJUFGMO("/PSNBMMZ SJHIU-BMJHOFE:]%10E]", WBMVF);
XSJUFGMO("-FGU-BMJHOFE :]%-10E]", WBMVF);
/PSNBMMZ SJHIU-BMJHOFE:] 123]
-FGU-BMJHOFE :]123 ]
+: DA OC@ Q<GP@ DN KJNDODQ@, DO DN KM@K@I?@? RDOC OC@ + >C<M<>O@M; OCDN AG<B ><I>@GN OC@ EB357 AG<B
XSJUFGMO("/P FGGFDU GPS OFHBUJWF WBMVFT : %+E", -50);
XSJUFGMO("1PTJUJWF WBMVF XJUI UIF + GMBH : %+E", 50);
XSJUFGMO("1PTJUJWF WBMVF XJUIPVU UIF + GMBH: %E", 50);
/P FGGFDU GPS OFHBUJWF WBMVFT : -50
1PTJUJWF WBMVF XJUI UIF + GMBH : +50
1PTJUJWF WBMVF XJUIPVU UIF + GMBH: 50
#: KMDION OC@ Q<GP@ DI <I 3>F7D@3F7 AJMH ?@K@I?DIB JI OC@ 8AD?3F25:3D35F7D
t P: OC@ ADMNO >C<M<>O@M JA OC@ J>O<G Q<GP@ DN <GR<TN KMDIO@? <N 0
t Y <I? 9: DA OC@ Q<GP@ DN IJO U@MJ, DO DN KM@K@I?@? RDOC 0Y JM 09
t AGJ<ODIB KJDION: < ?JO DN KMDIO@? @Q@I DA OC@M@ <M@ IJ NDBIDAD><IO ?DBDON <AO@M OC@ ?JO
t H <I? (: @Q@I OC@ DINDBIDAD><IO U@MJ ?DBDON <AO@M OC@ ?JO <M@ KMDIO@?
XSJUFGMO("0DUBM TUBSUT XJUI 0 : %#P", 1000);
XSJUFGMO(")FYBEFDJNBM TUBSUT XJUI 0Y : %#Y", 1000);
XSJUFGMO("$POUBJOT EPU FWFO XIFO VOOFDFTTBSZ: %#H", 1G);
XSJUFGMO("3JHIUNPTU [FSPT BSF QSJOUFE : %#H", 1.2);
0DUBM TUBSUT XJUI 0 : 01750
)FYBEFDJNBM TUBSUT XJUI 0Y : 0Y3F8
$POUBJOT EPU FWFO XIFO VOOFDFTTBSZ: 1.00000
3JHIUNPTU [FSPT BSF QSJOUFE : 1.20000
0: OC@ AD@G? DN K<??@? RDOC U@MJN (PIG@NN OC@ Q<GP@ DN OBO JM JOGJOJUZ); DA BD75;E;A@ DN <GNJ
NK@>DAD@?, OCDN AG<B DN DBIJM@?
XSJUFGMO("*O B GJFME PG 8 DIBSBDUFST: %08E", 42);
*O B GJFME PG 8 DIBSBDUFST: 00000042
EB357 >C<M<>O@M: DA OC@ Q<GP@ DN KJNDODQ@, < NK<>@ >C<M<>O@M DN KM@K@I?@? OJ <GDBI OC@ I@B<ODQ@
<I? KJNDODQ@ Q<GP@N
XSJUFGMO("/P FGGFDU GPS OFHBUJWF WBMVFT: % E", -34);
XSJUFGMO("1PTJUJWF WBMVF XJUI TQBDF : % E", 56);
XSJUFGMO("1PTJUJWF WBMVF XJUIPVU TQBDF : %E", 56);
FAD?3FF76 %GFBGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
118
/P FGGFDU GPS OFHBUJWF WBMVFT: -34
1PTJUJWF WBMVF XJUI TQBDF : 56
1PTJUJWF WBMVF XJUIPVU TQBDF : 56
Positional parameters
28.5
4@ C<Q@ N@@I <=JQ@ OC<O OC@ K<M<H@O@MN <M@ <NNJ>D<O@? JI@ =T JI@ RDOC OC@ NK@>DAD@MN DI OC@
AJMH<O NOMDIB. &O DN KJNND=G@ OJ PN@ KJNDODJI IPH=@MN RDOCDI AJMH<O NK@>DAD@MN. 1CDN @I<=G@N
<NNJ>D<ODIB OC@ NK@>DAD@MN RDOC NK@>DAD> K<M<H@O@MN. -<M<H@O@MN <M@ IPH=@M@? DI DI>M@<NDIB
A<NCDJI, NO<MODIB RDOC 1. 1C@ K<M<H@O@M IPH=@MN <M@ NK@>DAD@? DHH@?D<O@GT <AO@M OC@ %
>C<M<>O@M, AJGGJR@? =T < $:
% position$ flags width precision format_character
AI <?Q<IO<B@ JA KJNDODJI<G K<M<H@O@MN DN =@DIB <=G@ OJ PN@ OC@ N<H@ K<M<H@O@M DI HJM@ OC<I
JI@ KG<>@N DI OC@ N<H@ AJMH<O NOMDIB:
XSJUFGMO("%1$E %1$Y %1$P %1$C", 42);
1C@ AJMH<O NOMDIB <=JQ@ PN@N OC@ K<M<H@O@M IPH=@M@? 1 RDOCDI AJPM NK@>DAD@MN OJ KMDIO DO DI
?@>DH<G, C@S<?@>DH<G, J>O<G, <I? =DI<MT AJMH<ON:
42 2B 52 101010
AIJOC@M <KKGD><ODJI JA KJNDODJI<G K<M<H@O@MN DN NPKKJMODIB HPGODKG@ CPH<I G<IBP<B@N. 4C@I
M@A@MM@? =T KJNDODJI IPH=@MN, K<M<H@O@MN ><I =@ HJQ@? <ITRC@M@ RDOCDI OC@ NK@>DAD> AJMH<O
NOMDIB AJM < BDQ@I CPH<I G<IBP<B@. #JM @S<HKG@, OC@ IPH=@M JA NOP?@ION JA < BDQ@I >G<NNMJJH
><I =@ KMDIO@? <N DI OC@ AJGGJRDIB:
XSJUFGMO("5IFSF BSF %T TUVEFOUT JO SPPN %T.", DPVOU, SPPN);
5IFSF BSF 20 TUVEFOUT JO SPPN 1".
)@O'N <NNPH@ OC<O OC@ KMJBM<H HPNO <GNJ NPKKJMO 1PMFDNC. &I OCDN ><N@ OC@ AJMH<O NOMDIB
I@@?N =@ N@G@>O@? <>>JM?DIB OJ OC@ <>ODQ@ G<IBP<B@. 1C@ AJGGJRDIB H@OCJ? O<F@N <?Q<IO<B@ JA
OC@ O@MI<MT JK@M<OJM:
BVUP GPSNBU = (MBOHVBHF == "FO"
"5IFSF BSF %T TUVEFOUT JO SPPN %T."
: "%T TfOfGfOEB %T bdSFODJ WBS.");
XSJUFGMO(GPSNBU, DPVOU, SPPN);
2IAJMOPI<O@GT, RC@I OC@ K<M<H@O@MN <M@ <NNJ>D<O@? JI@ =T JI@, OC@ >G<NNMJJH <I? NOP?@IO
>JPIO DIAJMH<ODJI <KK@<M DI M@Q@MN@ JM?@M DI OC@ 1PMFDNC H@NN<B@; OC@ MJJH DIAJMH<ODJI DN
RC@M@ OC@ >JPIO NCJPG? =@ <I? OC@ >JPIO DN RC@M@ OC@ MJJH NCJPG? =@:
20 TgOgGgOEB 1" ceSFODJ WBS. l 8SPOH: NFBOT "SPPN 20", BOE "1" TUVEFOUT"!
1J <QJD? OCDN, OC@ K<M<H@O@MN ><I =@ NK@>DAD@? =T IPH=@MN <N 1$ <I? 2$ OJ <NNJ>D<O@ @<>C
NK@>DAD@M RDOC OC@ @S<>O K<M<H@O@M:
FAD?3FF76 %GFBGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
119
BVUP GPSNBU = (MBOHVBHF == "FO"
"5IFSF BSF %1$T TUVEFOUT JO SPPN %2$T."
: "%2$T TfOfGfOEB %1$T bdSFODJ WBS.");
XSJUFGMO(GPSNBU, DPVOU, SPPN);
+JR OC@ K<M<H@O@MN <KK@<M DI OC@ KMJK@M JM?@M, M@B<M?G@NN JA OC@ G<IBP<B@ N@G@>O@?:
5IFSF BSF 20 TUVEFOUT JO SPPN 1".
1" TgOgGgOEB 20 ceSFODJ WBS.
GPSNBU
28.6
#JMH<OO@? JPOKPO DN <Q<DG<=G@ =T OC@ GPSNBU() API>ODJI JA OC@ TUE.TUSJOH HJ?PG@ <N R@GG.
GPSNBU() RJMFN OC@ N<H@ <N XSJUFG() =PO DO D7FGD@E OC@ M@NPGO <N < TUSJOH DINO@<? JA
KMDIODIB DO OJ OC@ JPOKPO:
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
WPJE NBJO()
\
XSJUF("8IBU JT ZPVS OBNF ");
BVUP OBNF = DIPNQ(SFBEMO());
BVUP SFTVMU = GPSNBU(")FMMP %T!", OBNF);
^
1C@ KMJBM<H ><I H<F@ PN@ JA OC<O M@NPGO DI G<O@M @SKM@NNDJIN.
Exercises
28.7
1. 4MDO@ < KMJBM<H OC<O M@<?N < Q<GP@ <I? KMDION DO DI OC@ C@S<?@>DH<G NTNO@H.
2. 4MDO@ < KMJBM<H OC<O M@<?N < AGJ<ODIB KJDIO Q<GP@ <I? KMDION DO <N K@M>@IO<B@ Q<GP@ RDOC
ORJ ?DBDON <AO@M OC@ ?JO. #JM @S<HKG@, DA OC@ Q<GP@ DN 1.2345, DO NCJPG? KMDIO %1.23.
... F:7 EA>GF;A@E
FAD?3FF76 %GFBGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
120
Formatted Input 29
&O DN KJNND=G@ OJ NK@>DAT OC@ AJMH<O JA OC@ ?<O< OC<O DN @SK@>O@? <O OC@ DIKPO. 1C@ AJMH<O
NK@>DAD@N =JOC OC@ ?<O< OC<O DN OJ =@ M@<? <I? OC@ >C<M<>O@MN OC<O NCJPG? =@ DBIJM@?.
!'N DIKPO AJMH<O NK@>DAD@MN <M@ NDHDG<M OJ OC@ JI@N OC<O <M@ DI OC@ G<IBP<B@.
AN R@ C<Q@ <GM@<?T =@@I PNDIB DI OC@ KM@QDJPN >C<KO@MN, OC@ AJMH<O NK@>DAD@M " %T" M@<?N OC@
?<O< <>>JM?DIB OJ OC@ OTK@ JA OC@ Q<MD<=G@. #JM @S<HKG@, <N OC@ OTK@ JA OC@ AJGGJRDIB Q<MD<=G@
DN EPVCMF, OC@ >C<M<>O@MN <O OC@ DIKPO RJPG? =@ M@<? <N AGJ<ODIB KJDIO:
EPVCMF OVNCFS;
SFBEG(" %T", &OVNCFS);
1C@ AJMH<O NOMDIB ><I >JIO<DI OCM@@ OTK@N JA DIAJMH<ODJI:
t The space character: &I?D><O@N L7DA JM HJM@ RCDO@NK<>@ >C<M<>O@MN <O OC@ DIKPO <I?
NK@>DAD@N OC<O <GG JA OCJN@ >C<M<>O@MN NCJPG? =@ M@<? <I? DBIJM@?.
t Format specifier: 0DHDG<M OJ OC@ JPOKPO AJMH<O NK@>DAD@MN, DIKPO AJMH<O NK@>DAD@MN NO<MO
RDOC OC@ % >C<M<>O@M <I? ?@O@MHDI@ OC@ AJMH<O JA OC@ ?<O< OC<O DN OJ =@ M@<?.
t Any other character: &I?D><O@N OC@ >C<M<>O@MN OC<O <M@ @SK@>O@? <O OC@ DIKPO <N-DN, RCD>C
NCJPG? =@ M@<? <I? DBIJM@?.
1C@ AJMH<O NOMDIB H<F@N DO KJNND=G@ OJ N@G@>O NK@>DAD> DIAJMH<ODJI AMJH OC@ DIKPO <I? DBIJM@
OC@ JOC@MN.
)@O'N C<Q@ < GJJF <O <I @S<HKG@ OC<O PN@N <GG JA OC@ OCM@@ OTK@N JA DIAJMH<ODJI DI OC@ AJMH<O
NOMDIB. )@O'N <NNPH@ OC<O OC@ NOP?@IO IPH=@M <I? OC@ BM<?@ <M@ @SK@>O@? OJ <KK@<M <O OC@
DIKPO DI OC@ AJGGJRDIB AJMH<O:
OVNCFS:123 HSBEF:90
)@O'N APMOC@M <NNPH@ OC<O OC@ O<BN OVNCFS: <I? HSBEF: HPNO =@ DBIJM@?. 1C@ AJGGJRDIB
AJMH<O NOMDIB RJPG? E7>75F OC@ Q<GP@N JA IPH=@M <I? BM<?@ <I? RJPG? DBIJM@ OC@ JOC@M
>C<M<>O@MN:
JOU OVNCFS;
JOU HSBEF;
SFBEG("OVNCFS:%T HSBEF:%T", &OVNCFS, &HSBEF);
1C@ >C<M<>O@MN OC<O <M@ DI?D><O@? DI JM<IB@ DI "OVNCFS:%T HSBEF:%T" HPNO <KK@<M <O OC@
DIKPO @S<>OGT <N NK@>DAD@?; SFBEG() M@<?N <I? DBIJM@N OC@H.
1C@ NDIBG@ NK<>@ >C<M<>O@M OC<O <KK@<MN DI OC@ AJMH<O NOMDIB <=JQ@ RJPG? ><PN@ <GG JA OC@
RCDO@NK<>@ >C<M<>O@MN OC<O <KK@<M @S<>OGT <O OC<O KJNDODJI OJ =@ M@<? <I? DBIJM@?.
AN OC@ % >C<M<>O@M C<N < NK@>D<G H@<IDIB DI AJMH<O NOMDIBN, RC@I OC<O >C<M<>O@M DON@GA I@@?N
OJ =@ M@<? <I? DBIJM@?, DO HPNO =@ RMDOO@I ORD>@ DI OC@ AJMH<O NOMDIB <N %%.
/@<?DIB < NDIBG@ GDI@ JA ?<O< AMJH OC@ DIKPO C<N =@@I M@>JHH@I?@? <N DIPNQ(SFBEMO()) DI
OC@ 0OMDIBN >C<KO@M. &INO@<? JA OC<O H@OCJ?, < =O >C<M<>O@M <O OC@ @I? JA OC@ AJMH<O NOMDIB
><I <>CD@Q@ < NDHDG<M BJ<G:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
FAD?3FF76 !@BGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
121
XSJUF("'JSTU OBNF: ");
TUSJOH GJSTU/BNF;
SFBEG(" %T=O", &GJSTU/BNF); // j =O BU UIF FOE
XSJUF("-BTU OBNF : ");
TUSJOH MBTU/BNF;
SFBEG(" %T=O", &MBTU/BNF); // j =O BU UIF FOE
XSJUF(""HF : ");
JOU BHF;
SFBEG(" %T", &BHF);
XSJUFGMO("%T %T (%T)", GJSTU/BNF, MBTU/BNF, BHF);
^
1C@ =O >C<M<>O@MN <O OC@ @I?N JA OC@ AJMH<O NOMDIBN RC@I M@<?DIB GJSTU/BNF <I? MBTU/BNF
RJPG? ><PN@ OC@ I@R-GDI@ >C<M<>O@MN OJ =@ M@<? AMJH OC@ DIKPO <I? OJ =@ DBIJM@?. %JR@Q@M,
KJO@IOD<G RCDO@NK<>@ >C<M<>O@MN <O OC@ @I?N JA OC@ NOMDIBN H<T NODGG I@@? OJ =@ M@HJQ@? =T
DIPNQ().
Format specifier characters
29.1
E: /@<? <I DIO@B@M DI OC@ ?@>DH<G NTNO@H.
P: /@<? <I DIO@B@M DI OC@ J>O<G NTNO@H.
Y: /@<? <I DIO@B@M DI OC@ C@S<?@>DH<G NTNO@H.
G: /@<? < AGJ<ODIB KJDIO IPH=@M.
T: /@<? <>>JM?DIB OJ OC@ OTK@ JA OC@ Q<MD<=G@.
D: /@<? < NDIBG@ >C<M<>O@M. 1CDN NK@>DAD@M <GGJRN M@<?DIB RCDO@NK<>@ >C<M<>O@MN <N R@GG (DO
><I>@GN OC@ DBIJM@ =@C<QDJM).
#JM @S<HKG@, DA OC@ DIKPO >JIO<DIN "23 23 23", OC@ Q<GP@N RJPG? =@ M@<? ?DAA@M@IOGT <>>JM?DIB
OJ OC@ ?DAA@M@IO AJMH<O NK@>DAD@MN:
JOU OVNCFS@E;
JOU OVNCFS@P;
JOU OVNCFS@Y;
SFBEG(" %E %P %Y", &OVNCFS@E, &OVNCFS@P, &OVNCFS@Y);
XSJUFMO("3FBE XJUI %E: ", OVNCFS@E);
XSJUFMO("3FBE XJUI %P: ", OVNCFS@P);
XSJUFMO("3FBE XJUI %Y: ", OVNCFS@Y);
AGOCJPBC OC@ DIKPO >JIO<DIN OCM@@ N@ON JA "23" >C<M<>O@MN, OC@ Q<GP@N JA OC@ Q<MD<=G@N <M@
?DAA@M@IO:
3FBE XJUI %E: 23
3FBE XJUI %P: 19
3FBE XJUI %Y: 35
Note: +7DK 4D;78>K, "23" ;E 7CG3> FA 2J8+3=19 ;@ F:7 A5F3> EKEF7? 3@6 FA 2J16+3=35 ;@ F:7
:7J3675;?3> EKEF7?.
FAD?3FF76 !@BGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
122
Exercise
29.2
t ANNPH@ OC<O OC@ DIKPO >JIO<DIN OC@ ?<O@ DI OC@ AJMH<O K73D.?A@F:.63K. 4MDO@ < KMJBM<H
OC<O KMDION OC@ IPH=@M JA OC@ HJIOC. #JM @S<HKG@, DA OC@ DIKPO DN 2009.09.30, OC@ JPOKPO
NCJPG? =@ 9.
... F:7 EA>GF;A@
FAD?3FF76 !@BGF
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
123
EP-XIJMF Loop 30
&I OC@ GPS )JJK >C<KO@M, R@ C<Q@ N@@I OC@ NO@KN DI RCD>C OC@ XIJMF GJJK DN @S@>PO@?:
QSFQBSBUJPO
DPOEJUJPO DIFDL
BDUVBM XPSL
JUFSBUJPO
DPOEJUJPO DIFDL
BDUVBM XPSL
JUFSBUJPO
...
1C@ EP-XIJMF GJJK DN Q@MT NDHDG<M OJ OC@ XIJMF GJJK. 1C@ ?DAA@M@I>@ DN OC<O OC@ 5A@6;F;A@
5:75= DN K@MAJMH@? <O OC@ @I? JA @<>C DO@M<ODJI JA OC@ EP-XIJMF GJJK, NJ OC<O OC@ 35FG3> IAD=
DN K@MAJMH@? <O G@<NO JI>@:
QSFQBSBUJPO
BDUVBM XPSL
JUFSBUJPO
DPOEJUJPO DIFDL j BU UIF FOE PG UIF JUFSBUJPO
BDUVBM XPSL
JUFSBUJPO
DPOEJUJPO DIFDL j BU UIF FOE PG UIF JUFSBUJPO
...
#JM @S<HKG@, EP-XIJMF H<T =@ HJM@ I<OPM<G DI OC@ AJGGJRDIB KMJBM<H RC@M@ OC@ PN@M
BP@NN@N < IPH=@M, <N OC@ PN@M HPNO BP@NN <O G@<NO JI>@ NJ OC<O OC@ IPH=@M ><I =@ >JHK<M@?:
JNQPSU TUE.TUEJP;
JNQPSU TUE.SBOEPN;
WPJE NBJO()
\
JOU OVNCFS = VOJGPSN(1, 101);
XSJUFMO("* BN UIJOLJOH PG B OVNCFS CFUXFFO 1 BOE 100.");
JOU HVFTT;
EP \
XSJUF("8IBU JT ZPVS HVFTT ");
SFBEG(" %T", &HVFTT);
JG (OVNCFS < HVFTT) \
XSJUF(".Z OVNCFS JT MFTT UIBO UIBU. ");
^ FMTF JG (OVNCFS > HVFTT) \
XSJUF(".Z OVNCFS JT HSFBUFS UIBO UIBU. ");
^
^ XIJMF (HVFTT != OVNCFS);
XSJUFMO("$PSSFDU!");
^
1C@ API>ODJI VOJGPSN() OC<O DN PN@? DI OC@ KMJBM<H DN < K<MO JA OC@ TUE.SBOEPN HJ?PG@. &O
M@OPMIN < M<I?JH IPH=@M DI OC@ NK@>DAD@? M<IB@. 1C@ R<T DO DN PN@? <=JQ@, OC@ N@>JI?
6A-I:;>7 "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
124
IPH=@M DN >JIND?@M@? OJ =@ JPOND?@ JA OC@ M<IB@. &I JOC@M RJM?N, VOJGPSN() RJPG? IJO M@OPMI
101 AJM OC<O ><GG.
Exercise
30.1
t 4MDO@ < KMJBM<H OC<O KG<TN OC@ N<H@ B<H@ =PO C<Q@ OC@ KMJBM<H ?J OC@ BP@NNDIB. &A OC@
KMJBM<H DN RMDOO@I >JMM@>OGT, DO NCJPG? BP@NN OC@ PN@M'N IPH=@M DI <O HJNO 7 OMD@N.
... F:7 EA>GF;A@
6A-I:;>7 "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
125
Associative Arrays 31
ANNJ>D<ODQ@ <MM<TN <M@ < A@<OPM@ OC<O DN AJPI? DI HJNO JA OC@ HJ?@MI CDBC-G@Q@G G<IBP<B@N.
1C@T <M@ Q@MT A<NO ?<O< NOMP>OPM@N OC<O RJMF GDF@ HDID ?<O<=<N@N <I? <M@ >JHHJIGT PN@? DI
H<IT KMJBM<HN.
4@ C<Q@ N@@I <MM<TN DI OC@ AMM<TN >C<KO@M <N >JIO<DI@MN OC<O NOJM@ OC@DM @G@H@ION ND?@-=T-
ND?@ <I? KMJQD?@ <>>@NN OJ OC@H =T DI?@S@N. AI <MM<T OC<O NOJM@N OC@ I<H@N JA OC@ ?<TN JA OC@
R@@F ><I =@ ?@ADI@? GDF@ OCDN:
TUSJOH<> EBZ/BNFT =
< ".POEBZ", "5VFTEBZ", "8FEOFTEBZ", "5IVSTEBZ",
"'SJEBZ", "4BUVSEBZ", "4VOEBZ" >;
1C@ I<H@ JA < NK@>DAD> ?<T ><I =@ <>>@NN@? =T DON DI?@S DI OC<O <MM<T:
XSJUFMO(EBZ/BNFT<1>); // QSJOUT "5VFTEBZ"
1C@ A<>O OC<O <MM<TN KMJQD?@ <>>@NN OJ @G@H@ION =T DI?@S Q<GP@N ><I =@ ?@N>MD=@? <N <I
3EEA5;3F;A@ JA DI?@S@N RDOC @G@H@ION. &I JOC@M RJM?N, <MM<TN H<K DI?@S@N OJ @G@H@ION. AMM<TN
><I PN@ JIGT DIO@B@MN <N DI?@S@N.
ANNJ>D<ODQ@ <MM<TN <GGJR DI?@SDIB IJO JIGT =T DIO@B@MN =PO <IT OTK@. 1C@T H<K OC@ Q<GP@N JA
JI@ OTK@ OJ OC@ Q<GP@N JA <IJOC@M OTK@. 1C@ Q<GP@N JA OC@ OTK@ OC<O <NNJ>D<ODQ@ <MM<TN ?3B
8DA? <M@ ><GG@? =7KE, M<OC@M OC<I DI?@S@N. 1C@T KMJQD?@ <>>@NN OJ @G@H@ION =T OC@ F@T JA @<>C
@G@H@IO.
Very fast but unordered
31.1
ANNJ>D<ODQ@ <MM<TN <M@ < :3E: F34>7 DHKG@H@IO<ODJI. %<NC O<=G@N <M@ <HJIB OC@ A<NO@NO
>JGG@>ODJIN AJM NOJMDIB <I? <>>@NNDIB @G@H@ION. ,OC@M OC<I M<M@ K<OCJGJBD><G ><N@N, OC@ ODH@ DO
O<F@N OJ NOJM@ <I @G@H@IO JM OJ <>>@NN <I @G@H@IO DN DI?@K@I?@IO JA OC@ IPH=@M JA @G@H@ION
OC<O <M@ DI OC@ <NNJ>D<ODQ@ <MM<T.
1C@ CDBC K@MAJMH<I>@ JA C<NC O<=G@N DN <O OC@ @SK@IN@ JA OC@DM NOJMDIB OC@ @G@H@ION DI <I
PIJM?@M@? R<T. 2IGDF@ <MM<TN, OC@ @G@H@ION JA C<NC O<=G@N <M@ IJO ND?@-=T-ND?@. 1C@T <M@ IJO
NJMO@? DI <IT R<T @DOC@M.
Definition
31.2
1C@ NTIO<S JA <NNJ>D<ODQ@ <MM<TN DN NDHDG<M OJ OC@ <MM<T NTIO<S. 1C@ ?DAA@M@I>@ DN OC<O DO DN OC@
OTK@ JA OC@ F@T OC<O DN NK@>DAD@? RDOCDI OC@ NLP<M@ =M<>F@ON, IJO OC@ G@IBOC JA OC@ <MM<T:
element_type<key_type> associative_array_name;
#JM @S<HKG@, <I <NNJ>D<ODQ@ <MM<T OC<O H<KN ?<T I<H@N JA OTK@ TUSJOH OJ ?<T IPH=@MN JA
OTK@ JOU ><I =@ ?@ADI@? GDF@ OCDN:
JOU<TUSJOH> EBZ/VNCFST;
1C@ EBZ/VNCFST Q<MD<=G@ <=JQ@ DN <I <NNJ>D<ODQ@ <MM<T OC<O ><I =@ PN@? <N < O<=G@ OC<O
KMJQD?@N < H<KKDIB AMJH ?<T I<H@N OJ ?<T IPH=@MN. &I JOC@M RJM?N, DO ><I =@ PN@? <N OC@
M@Q@MN@ JA OC@ EBZ/BNFT <MM<T AMJH OC@ =@BDIIDIB JA OCDN >C<KO@M. 4@ RDGG PN@ OC@ EBZ/VNCFST
<NNJ>D<ODQ@ <MM<T DI OC@ @S<HKG@N =@GJR.
AEEA5;3F;H7 ADD3KE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
126
1C@ F@TN JA <NNJ>D<ODQ@ <MM<TN ><I =@ JA <IT OTK@ DI>GP?DIB PN@M-?@ADI@? TUSVDU <I? DMBTT
OTK@N. 4@ RDGG N@@ PN@M-?@ADI@? OTK@N DI G<O@M >C<KO@MN.
1C@ G@IBOC JA <NNJ>D<ODQ@ <MM<TN ><IIJO =@ NK@>DAD@? RC@I ?@ADI@?. 1C@T BMJR <POJH<OD><GGT
<N @G@H@ION <M@ <??@?.
Adding elements
31.3
ANNDBIH@IO JK@M<OJM DN NPAAD>D@IO OJ =PDG? OC@ <NNJ>D<ODJI =@OR@@I < F@T <I? < Q<GP@:
// BTTPDJBUFT FMFNFOU 0 XJUI LFZ ".POEBZ"
EBZ/VNCFST<".POEBZ"> = 0;
// BTTPDJBUFT FMFNFOU 1 XJUI LFZ "5VFTEBZ"
EBZ/VNCFST<"5VFTEBZ"> = 1;
1C@ O<=G@ BMJRN <POJH<OD><GGT RDOC @<>C <NNJ>D<ODJI. #JM @S<HKG@, EBZ/VNCFST RJPG? C<Q@
ORJ @G@H@ION <AO@M OC@ JK@M<ODJIN <=JQ@. 1CDN ><I =@ ?@HJINOM<O@? =T KMDIODIB OC@ @IODM@
O<=G@:
XSJUFMO(EBZ/VNCFST);
1C@ JPOKPO DI?D><O@N OC<O OC@ @G@H@IO Q<GP@N 0 <I? 1 >JMM@NKJI? OJ F@T Q<GP@N "*JI?<T" <I?
"1P@N?<T", M@NK@>ODQ@GT:
<".POEBZ":0, "5VFTEBZ":1>
1C@M@ ><I =@ JIGT JI@ @G@H@IO K@M F@T. #JM OC<O M@<NJI, RC@I <NNDBI@? OJ <I @SDNODIB
@G@H@IO, OC@ O<=G@ ?J@N IJO BMJR; DINO@<?, OC@ Q<GP@ JA OC@ @SDNODIB @G@H@IO >C<IB@N:
EBZ/VNCFST<"5VFTEBZ"> = 222; // DIBOHJOH UIF FYJTUJOH FMFNFOU
XSJUFMO(EBZ/VNCFST);
1C@ JPOKPO:
<".POEBZ":0, "5VFTEBZ":222>
Initialization
31.4
0JH@ODH@N NJH@ JA OC@ H<KKDIBN =@OR@@I OC@ F@TN <I? OC@ Q<GP@N <M@ <GM@<?T FIJRI <O OC@
ODH@ JA OC@ ?@ADIDODJI JA OC@ <NNJ>D<ODQ@ <MM<T. ANNJ>D<ODQ@ <MM<TN <M@ DIDOD<GDU@? NDHDG<MGT OJ
<MM<TN, RDOC OC@ ?DAA@M@I>@ JA < >JGJI N@K<M<ODIB @<>C F@T AMJH DON @G@H@IO:
// LFZ : FMFNFOU
JOU<TUSJOH> EBZ/VNCFST =
< ".POEBZ" : 0, "5VFTEBZ" : 1, "8FEOFTEBZ" : 2,
"5IVSTEBZ" : 3, "'SJEBZ" : 4, "4BUVSEBZ" : 5,
"4VOEBZ" : 6 >;
XSJUFMO(EBZ/VNCFST<"5VFTEBZ">); // QSJOUT 1
Removing elements
31.5
1C@ @G@H@IO OC<O >JMM@NKJI?N OJ < F@T DN M@HJQ@? =T .SFNPWF():
AEEA5;3F;H7 ADD3KE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
127
EBZ/VNCFST.SFNPWF("5VFTEBZ");
XSJUFMO(EBZ/VNCFST<"5VFTEBZ">); // j SVO-UJNF &3303
1C@ ADMNO GDI@ <=JQ@ M@HJQ@N OC@ @G@H@IO AJM OC@ F@T "1P@N?<T". 0DI>@ OC<O @G@H@IO DN IJO DI
OC@ >JIO<DI@M <ITHJM@, OC@ N@>JI? GDI@ RJPG? ><PN@ <I @S>@KODJI OJ =@ OCMJRI <I? OC@
KMJBM<H OJ =@ O@MHDI<O@? DA OC<O @S>@KODJI DN IJO ><PBCO. 4@ RDGG N@@ @S>@KODJIN DI < G<O@M
>C<KO@M.
&O DN KJNND=G@ OJ M@HJQ@ <GG JA OC@ @G@H@ION <O JI>@ <N RDGG =@ N@@I DI OC@ ADMNO @S@M>DN@ =@GJR.
Determining the presence of an element
31.6
1C@ JO JK@M<OJM ?@O@MHDI@N RC@OC@M OC@ @G@H@IO AJM < BDQ@I F@T @SDNON DI OC@ <NNJ>D<ODQ@
<MM<T:
JOU<TUSJOH> DPMPS$PEFT = < /* ... */ >;
JG ("QVSQMF" JO DPMPS$PEFT) \
// UIFSF JT BO FMFNFOU GPS LFZ "QVSQMF"
^ FMTF \
// OP FMFNFOU GPS LFZ "QVSQMF"
^
0JH@ODH@N DO H<F@N N@IN@ OJ PN@ < ?@A<PGO @G@H@IO Q<GP@ AJM F@TN OC<O ?J IJO @SDNO DI OC@
<NNJ>D<ODQ@ <MM<T. #JM @S<HKG@, OC@ NK@>D<G Q<GP@ JA -1 ><I =@ PN@? <N OC@ >J?@ AJM >JGJMN OC<O
<M@ IJO DI DPMPS$PEFT. .HFU() DN PN@APG DI NP>C ><N@N: &O M@OPMIN OC@ Q<GP@ JA OC@ @G@H@IO DA
OC@ @G@H@IO AJM OC@ NK@>DAD@? F@T @SDNON, OC@ ?@A<PGO Q<GP@ JOC@MRDN@. 1C@ ?@A<PGO Q<GP@ DN
NK@>DAD@? <N OC@ N@>JI? K<M<H@O@M JA .HFU():
JOU<TUSJOH> DPMPS$PEFT = < "CMVF" : 10, "HSFFO" : 20 >;
XSJUFMO(DPMPS$PEFT.HFU("QVSQMF", -1));
0DI>@ OC@ <MM<T ?J@N IJO >JIO<DI <I @G@H@IO AJM OC@ F@T "QVSQMF", .HFU() M@OPMIN -1:
-1
Properties
31.7
t .MFOHUI M@OPMIN OC@ IPH=@M JA @G@H@ION DI OC@ <MM<T.
t .LFZT M@OPMIN >JKD@N JA <GG JA OC@ F@TN DI OC@ <NNJ>D<ODQ@ <MM<T <N < ?TI<HD> <MM<T.
t .CZ,FZ KMJQD?@N <>>@NN OJ OC@ F@TN DI OC@ <NNJ>D<ODQ@ <MM<T RDOCJPO >JKTDIB OC@H; R@ RDGG
N@@ CJR .CZ,FZ DN PN@? DI GPSFBDI GJJKN DI OC@ I@SO >C<KO@M.
t .WBMVFT M@OPMIN >JKD@N JA <GG JA OC@ Q<GP@N DI OC@ <NNJ>D<ODQ@ <MM<T <N < ?TI<HD> <MM<T.
t .CZ7BMVF KMJQD?@N <>>@NN OJ OC@ @G@H@ION JA OC@ <NNJ>D<ODQ@ <MM<T RDOCJPO >JKTDIB OC@H.
t .SFIBTI H<T H<F@ OC@ <MM<T HJM@ @AAD>D@IO DI NJH@ ><N@N GDF@ <AO@M DIN@MODIB < G<MB@
IPH=@M JA @G@H@ION <I? =@AJM@ <>OP<GGT NO<MODIB OJ PN@ OC@ <NNJ>D<ODQ@ <MM<T.
t .TJ[FPG DN OC@ NDU@ JA OC@ <MM<T D787D7@57 (DO C<N IJOCDIB OJ ?J RDOC OC@ IPH=@M JA
@G@H@ION DI OC@ O<=G@ <I? DN OC@ N<H@ Q<GP@ AJM <GG <NNJ>D<ODQ@ <MM<TN).
t .HFU M@OPMIN OC@ @G@H@IO DA DO @SDNON, OC@ ?@A<PGO Q<GP@ JOC@MRDN@.
t .SFNPWF M@HJQ@N OC@ NK@>DAD@? @G@H@IO AMJH OC@ <MM<T.
AEEA5;3F;H7 ADD3KE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
128
Example
31.8
%@M@ DN < KMJBM<H OC<O KMDION OC@ 1PMFDNC I<H@N JA >JGJMN OC<O <M@ NK@>DAD@? DI "IBGDNC:
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
WPJE NBJO()
\
TUSJOH<TUSJOH> DPMPST = < "CMBDL" : "TJZBI",
"XIJUF" : "CFZB[",
"SFE" : "LfSNf[f",
"HSFFO" : "ZFhJM",
"CMVF" : "NBWJ"
>;
XSJUFGMO("* LOPX UIF 5VSLJTI OBNFT PG UIFTF %T DPMPST: %T",
DPMPST.MFOHUI, DPMPST.LFZT);
XSJUF("1MFBTF BTL NF POF: ");
TUSJOH JO&OHMJTI = DIPNQ(SFBEMO());
JG (JO&OHMJTI JO DPMPST) \
XSJUFGMO("="%T=" JT ="%T=" JO 5VSLJTI.",
JO&OHMJTI, DPMPST<JO&OHMJTI>);
^ FMTF \
XSJUFMO("* EPO'U LOPX UIBU POF.");
^
^
Exercises
31.9
1. %JR ><I <GG JA OC@ @G@H@ION JA <I <NNJ>D<ODQ@ <MM<T =@ M@HJQ@?? 1C@M@ <M@ <O G@<NO OCM@@
H@OCJ?N:
w /@HJQDIB OC@ @G@H@ION JI@-=T-JI@ AMJH OC@ <NNJ>D<ODQ@ <MM<T.
w ANNDBIDIB <I @HKOT <NNJ>D<ODQ@ <MM<T.
w 0DHDG<M OJ OC@ KM@QDJPN H@OCJ?, <NNDBIDIB OC@ <MM<T'N .JOJU KMJK@MOT.
Note: ):7 .init BDAB7DFK A8 3@K H3D;34>7 AD FKB7 ;E F:7 ;@;F;3> H3>G7 A8 F:3F
FKB7:
OVNCFS = JOU.JOJU; // 0 GPS JOU
2. 'PNO GDF@ RDOC <MM<TN, OC@M@ ><I =@ JIGT JI@ Q<GP@ AJM @<>C F@T. 1CDN H<T =@ N@@I <N <
GDHDO<ODJI AJM NJH@ <KKGD><ODJIN.
ANNPH@ OC<O <I <NNJ>D<ODQ@ <MM<T DN PN@? AJM NOJMDIB NOP?@IO BM<?@N. #JM @S<HKG@,
G@O'N <NNPH@ OC<O OC@ BM<?@N 90, 85, 95, @O>. <M@ OJ =@ NOJM@? AJM OC@ NOP?@IO I<H@?
"@HM@".
ANNJ>D<ODQ@ <MM<TN H<F@ DO @<NT OJ <>>@NN OC@ BM<?@N =T OC@ I<H@ JA OC@ NOP?@IO <N DI
HSBEFT<"FNSF">. %JR@Q@M, OC@ BM<?@N ><IIJO =@ DIN@MO@? <N DI OC@ AJGGJRDIB >J?@
=@><PN@ @<>C BM<?@ RJPG? JQ@MRMDO@ OC@ KM@QDJPN JI@:
JOU<TUSJOH> HSBEFT;
HSBEFT<"FNSF"> = 90;
HSBEFT<"FNSF"> = 85; // j 0WFSXSJUFT UIF QSFWJPVT HSBEF!
%JR ><I TJP NJGQ@ OCDN KMJ=G@H? !@ADI@ <I <NNJ>D<ODQ@ <MM<T OC<O ><I NOJM@ HPGODKG@
BM<?@N K@M NOP?@IO.
AEEA5;3F;H7 ADD3KE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
129
... F:7 EA>GF;A@E
AEEA5;3F;H7 ADD3KE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
130
GPSFBDI Loop 32
,I@ JA OC@ HJNO >JHHJI NO<O@H@ION DI ! DN OC@ GPSFBDI GJJK. &O DN PN@? AJM <KKGTDIB OC@
N<H@ JK@M<ODJIN OJ @Q@MT @G@H@IO JA < >JIO<DI@M (JM < D3@97).
,K@M<ODJIN OC<O <M@ <KKGD@? OJ @G@H@ION JA >JIO<DI@MN <M@ Q@MT >JHHJI DI KMJBM<HHDIB. 4@
C<Q@ N@@I DI OC@ GPS )JJK >C<KO@M OC<O @G@H@ION JA <I <MM<T <M@ <>>@NN@? DI < GPS GJJK =T <I
DI?@S Q<GP@ OC<O DN DI>M@H@IO@? <O @<>C DO@M<ODJI:
GPS (JOU J = 0; J != BSSBZ.MFOHUI; ++J) \
XSJUFMO(BSSBZ<J>);
^
1C@ AJGGJRDIB <M@ OC@ NO@KN OC<O <M@ DIQJGQ@? DI DO@M<ODIB JQ@M <GG JA OC@ @G@H@ION:
t !@ADIDIB < Q<MD<=G@ <N < >JPIO@M, RCD>C DN >JIQ@IODJI<GGT I<H@? <N J
t &O@M<ODIB OC@ GJJK PK OJ OC@ Q<GP@ JA OC@ .MFOHUI KMJK@MOT JA OC@ <MM<T
t &I>M@H@IODIB J
t A>>@NNDIB OC@ @G@H@IO
GPSFBDI C<N @NN@IOD<GGT OC@ N<H@ =@C<QDJM =PO DO NDHKGDAD@N OC@ >J?@ =T C<I?GDIB OCJN@ NO@KN
<POJH<OD><GGT:
GPSFBDI (FMFNFOU; BSSBZ) \
XSJUFMO(FMFNFOU);
^
-<MO JA OC@ KJR@M JA GPSFBDI >JH@N AMJH OC@ A<>O OC<O DO ><I =@ PN@? OC@ N<H@ R<T
M@B<M?G@NN JA OC@ OTK@ JA OC@ >JIO<DI@M. AN R@ C<Q@ N@@I DI OC@ KM@QDJPN >C<KO@M, JI@ R<T JA
DO@M<ODIB JQ@M OC@ Q<GP@N JA <I <NNJ>D<ODQ@ <MM<T DI < GPS GJJK DN =T ADMNO ><GGDIB OC@ <MM<T'N
.WBMVFT KMJK@MOT:
BVUP WBMVFT = BB.WBMVFT;
GPS (JOU J = 0; J != WBMVFT.MFOHUI; ++J) \
XSJUFMO(WBMVFT<J>);
^
GPSFBDI ?J@N IJO M@LPDM@ <ITOCDIB NK@>D<G AJM <NNJ>D<ODQ@ <MM<TN; DO DN PN@? @S<>OGT OC@ N<H@
<N RDOC <MM<TN:
GPSFBDI (WBMVF; BB) \
XSJUFMO(WBMVF);
^
The GPSFBDI syntax
32.1
GPSFBDI >JINDNON JA OCM@@ N@>ODJIN:
GPSFBDI (names; container_or_range) \
operations
^
t container_or_range NK@>DAD@N RC@M@ OC@ @G@H@ION <M@.
t operations NK@>DAD@N OC@ JK@M<ODJIN OJ <KKGT OJ @<>C @G@H@IO.
t names NK@>DAD@N OC@ I<H@ JA OC@ @G@H@IO <I? KJO@IOD<GGT JOC@M Q<MD<=G@N ?@K@I?DIB JI
OC@ OTK@ JA OC@ >JIO<DI@M JM OC@ M<IB@. AGOCJPBC OC@ >CJD>@ JA I<H@N DN PK OJ OC@
8AD735: "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
131
KMJBM<HH@M, OC@ IPH=@M JA <I? OC@ OTK@N JA OC@N@ I<H@N ?@K@I? JI OC@ OTK@ JA OC@
>JIO<DI@M.
DPOUJOVF and CSFBL
32.2
1C@N@ F@TRJM?N C<Q@ OC@ N<H@ H@<IDIB <N OC@T C<Q@ RDOC OC@ GPS GJJK: DPOUJOVF HJQ@N OJ
OC@ I@SO DO@M<ODJI =@AJM@ >JHKG@ODIB OC@ M@NO JA OC@ JK@M<ODJIN AJM OC@ >PMM@IO @G@H@IO, <I?
CSFBL O@MHDI<O@N OC@ GJJK <GOJB@OC@M.
GPSFBDI with arrays
32.3
4C@I OC@M@ DN < NDIBG@ I<H@ NK@>DAD@? DI OC@ @3?7E N@>ODJI, DO DN OC@ Q<GP@ JA OC@ @G@H@IO <O
@<>C DO@M<ODJI:
GPSFBDI (FMFNFOU; BSSBZ) \
XSJUFMO(FMFNFOU);
^
4C@I ORJ I<H@N <M@ NK@>DAD@? DI OC@ @3?7E N@>ODJI, OC@T <M@ <I <POJH<OD> >JPIO@M <I? OC@
Q<GP@ JA OC@ @G@H@IO, M@NK@>ODQ@GT:
GPSFBDI (J, FMFNFOU; BSSBZ) \
XSJUFMO(J, ": ", FMFNFOU);
^
1C@ >JPIO@M DN DI>M@H@IO@? <POJH<OD><GGT =T GPSFBDI. &ON I<H@ DN PK OJ OC@ KMJBM<HH@M.
GPSFBDI with strings and TUE.SBOHF.TUSJEF
32.4
0DI>@ NOMDIBN <M@ <MM<TN JA >C<M<>O@MN, GPSFBDI RJMFN OC@ N<H@ <N DO RJMFN RDOC <MM<TN:
0DIBG@ I<H@ DN OC@ >C<M<>O@M, ORJ I<H@N <M@ OC@ >JPIO@M <I? OC@ >C<M<>O@M:
GPSFBDI (D; "IFMMP") \
XSJUFMO(D);
^
GPSFBDI (J, D; "IFMMP") \
XSJUFMO(J, ": ", D);
^
%JR@Q@M, =@DIB 21# >J?@ PIDON, DIBS <I? XDIBS DO@M<O@ JQ@M 21# >J?@ PIDON, IJO 2ID>J?@
>J?@ KJDION:
GPSFBDI (J, DPEF; "BCD`E") \
XSJUFMO(J, ": ", DPEF);
^
1C@ ORJ 21#-8 >J?@ PIDON OC<O H<F@ PK \ RJPG? =@ <>>@NN@? <N N@K<M<O@ @G@H@ION:
0: B
1: C
2: D
3:
4: {
5: E
8AD735: "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
132
,I@ R<T JA DO@M<ODIB JQ@M 2ID>J?@ >C<M<>O@MN JA NOMDIBN DI < GPSFBDI GJJK DN TUSJEF AMJH OC@
TUE.SBOHF HJ?PG@. TUSJEF KM@N@ION OC@ NOMDIB <N < >JIO<DI@M OC<O >JINDNON JA 2ID>J?@
>C<M<>O@MN. &O O<F@N OC@ G@IBOC JA DON EFD;67E JQ@M OC@ >C<M<>O@MN <N DON N@>JI? K<M<H@O@M:
JNQPSU TUE.SBOHF;
// ...
GPSFBDI (D; TUSJEF("BCD`E", 1)) \
XSJUFMO(D);
^
/@B<M?G@NN JA OC@ >C<M<>O@M OTK@ JA OC@ NOMDIB, TUSJEF <GR<TN KM@N@ION DON @G@H@ION <N
2ID>J?@ >C<M<>O@MN:
B
C
D
a
E
& RDGG @SKG<DI =@GJR RCT OCDN GJJK >JPG? IJO DI>GP?@ <I <POJH<OD> >JPIO@M.
GPSFBDI with associative arrays
32.5
A NDIBG@ I<H@ NK@>DAD@N OC@ Q<GP@, ORJ I<H@N NK@>DAT OC@ F@T <I? OC@ Q<GP@:
GPSFBDI (WBMVF; BB) \
XSJUFMO(WBMVF);
^
GPSFBDI (LFZ, WBMVF; BB) \
XSJUFMO(LFZ, ": ", WBMVF);
^
ANNJ>D<ODQ@ <MM<TN ><I KMJQD?@ OC@DM F@TN <I? Q<GP@N <N D3@97E <N R@GG. 4@ RDGG N@@ M<IB@N DI
< G<O@M >C<KO@M. .CZ,FZ() <I? .CZ7BMVF() M@OPMI @AAD>D@IO M<IB@ J=E@>ON OC<O <M@ <GNJ PN@APG
DI JOC@M >JIO@SON. .CZ7BMVF() ?J@N IJO =MDIB <IT =@I@ADO DI GPSFBDI GJJKN JQ@M OC@ M@BPG<M
Q<GP@ DO@M<ODJI <=JQ@. ,I OC@ JOC@M C<I?, .CZ,FZ() DN OC@ JIGT @AA@>ODQ@ R<T JA DO@M<ODIB JQ@M
EPNO OC@ F@TN JA <I <NNJ>D<ODQ@ <MM<T:
GPSFBDI (LFZ; BB.CZ,FZ()) \
XSJUFMO(LFZ);
^
GPSFBDI with number ranges
32.6
4@ C<Q@ N@@I IPH=@M M<IB@N DI OC@ 0GD>@N <I? ,OC@M AMM<T #@<OPM@N >C<KO@M =@AJM@. &O DN
KJNND=G@ OJ NK@>DAT < IPH=@M M<IB@ DI OC@ 5A@F3;@7D2AD2D3@97 N@>ODJI:
GPSFBDI (OVNCFS; 10..15) \
XSJUFMO(OVNCFS);
^
/@H@H=@M OC<O 10 RJPG? =@ DI>GP?@? DI OC@ M<IB@ =PO 15 RJPG? IJO =@.
8AD735: "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
133
GPSFBDI with structs, classes, and ranges
32.7
GPSFBDI ><I <GNJ =@ PN@? RDOC J=E@>ON JA PN@M OTK@N OC<O ?@ADI@ OC@DM JRI DO@M<ODJI DI
GPSFBDI GJJKN. AN OC@ OTK@ DON@GA ?@ADI@N DON DO@M<ODJI =@C<QDJM, DO DN IJO KJNND=G@ OJ N<T
<ITOCDIB C@M@. -MJBM<HH@MN HPNO M@A@M OJ OC@ ?J>PH@IO<ODJI JA OC@ K<MOD>PG<M OTK@.
0OMP>ON <I? >G<NN@N KMJQD?@ NPKKJMO AJM GPSFBDI DO@M<ODJI @DOC@M =T OC@DM PQ"QQMZ() H@H=@M
API>ODJIN, JM =T < N@O JA D3@97 H@H=@M API>ODJIN. 4@ RDGG N@@ OC@N@ A@<OPM@N DI G<O@M >C<KO@MN.
The counter is automatic only for arrays
32.8
1C@ <POJH<OD> >JPIO@M DN KMJQD?@? JIGT RC@I DO@M<ODIB JQ@M <MM<TN. 4C@I < >JPIO@M DN I@@?@?
RCDG@ DO@M<ODIB JQ@M JOC@M OTK@N JA >JIO<DI@MN, OC@ >JPIO@M HPNO =@ ?@ADI@? <I? DI>M@H@IO@?
@SKGD>DOGT:
JOU J;
GPSFBDI (FMFNFOU; DPOUBJOFS) \
// ...
++J;
^
0P>C < Q<MD<=G@ DN <GNJ I@@?@? RC@I >JPIODIB < NK@>DAD> >JI?DODJI. #JM @S<HKG@, OC@ AJGGJRDIB
>J?@ >JPION JIGT OC@ Q<GP@N OC<O <M@ ?DQDND=G@ =T 10:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
BVUP OVNCFST = < 1, 0, 15, 10, 3, 5, 20, 30 >;
JOU DPVOU;
GPSFBDI (OVNCFS; OVNCFST) \
JG ((OVNCFS % 10) == 0) \
++DPVOU;
XSJUF(DPVOU);
^ FMTF \
XSJUF(' ');
^
XSJUFMO(": ", OVNCFS);
^
^
1C@ JPOKPO:
: 1
1: 0
: 15
2: 10
: 3
: 5
3: 20
4: 30
8AD735: "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
134
The copy of the element, not the element itself
32.9
1C@ GPSFBDI GJJK IJMH<GGT KMJQD?@N < >JKT JA OC@ @G@H@IO, IJO OC@ <>OP<G @G@H@IO OC<O DN
NOJM@? DI OC@ >JIO<DI@M. 1CDN H<T =@ < ><PN@ JA =PBN.
1J N@@ <I @S<HKG@ JA OCDN, G@O'N C<Q@ < GJJF <O OC@ AJGGJRDIB KMJBM<H OC<O DN OMTDIB OJ ?JP=G@
OC@ Q<GP@N JA OC@ @G@H@ION JA <I <MM<T:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
EPVCMF<> OVNCFST = < 1.2, 3.4, 5.6 >;
XSJUFGMO("#FGPSF: %T", OVNCFST);
GPSFBDI (OVNCFS; OVNCFST) \
OVNCFS *= 2;
^
XSJUFGMO(""GUFS : %T", OVNCFST);
^
1C@ JPOKPO JA OC@ KMJBM<H DI?D><O@N OC<O OC@ <NNDBIH@IO H<?@ OJ @<>C @G@H@IO DIND?@ OC@
GPSFBDI =J?T ?J@N IJO C<Q@ <IT @AA@>O JI OC@ @G@H@ION JA OC@ >JIO<DI@M:
#FGPSF: <1.2, 3.4, 5.6>
"GUFS : <1.2, 3.4, 5.6>
1C<O DN =@><PN@ OVNCFS DN IJO <I <>OP<G @G@H@IO JA OC@ <MM<T, =PO < >JKT JA @<>C @G@H@IO.
4C@I OC@ <>OP<G @G@H@ION I@@? OJ =@ JK@M<O@? JI, OC@ I<H@ HPNO =@ ?@ADI@? <N < D787D7@57
JA OC@ <>OP<G @G@H@IO =T OC@ SFG F@TRJM?:
GPSFBDI (SFG OVNCFS; OVNCFST) \
OVNCFS *= 2;
^
1C@ I@R JPOKPO NCJRN OC<O OC@ <NNDBIH@ION IJR HJ?DAT OC@ <>OP<G @G@H@ION JA OC@ <MM<T:
#FGPSF: <1.2, 3.4, 5.6>
"GUFS : <2.4, 6.8, 11.2>
1C@ SFG F@TRJM? H<F@N OVNCFS <I 3>;3E JA OC@ <>OP<G @G@H@IO <O @<>C DO@M<ODJI. AN < M@NPGO,
OC@ HJ?DAD><ODJIN OCMJPBC OVNCFS HJ?DAD@N OC<O <>OP<G @G@H@IO JA OC@ >JIO<DI@M.
The integrity of the container must be preserved
32.10
AGOCJPBC DO DN ADI@ OJ HJ?DAT OC@ @G@H@ION JA OC@ >JIO<DI@M OCMJPBC SFG Q<MD<=G@N, OC@
NOMP>OPM@ JA OC@ >JIO<DI@M HPNO IJO =@ >C<IB@?. #JM @S<HKG@, @G@H@ION HPNO IJO =@ M@HJQ@?
IJM <??@? OJ OC@ >JIO<DI@M ?PMDIB < GPSFBDI GJJK.
0P>C HJ?DAD><ODJIN H<T >JIAPN@ OC@ DII@M RJMFDIBN JA OC@ GJJK DO@M<ODJI <I? M@NPGO DI
DI>JMM@>O KMJBM<H NO<O@N.
8AD735: "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
135
Exercise
32.11
t 4@ FIJR OC<O <NNJ>D<ODQ@ <MM<TN KMJQD?@ < H<KKDIB AMJH F@TN OJ Q<GP@N. 1CDN H<KKDIB DN
PID?DM@>ODJI<G: Q<GP@N <M@ <>>@NN@? =T F@TN =PO IJO OC@ JOC@M R<T <MJPI?.
ANNPH@ OC<O OC@M@ DN <GM@<?T OC@ AJGGJRDIB <NNJ>D<ODQ@ <MM<T:
TUSJOH<JOU> OBNFT = < 1:"POF", 7:"TFWFO", 20:"UXFOUZ" >;
2N@ OC<O <NNJ>D<ODQ@ <MM<T <I? < GPSFBDI GJJK OJ ADGG <IJOC@M <NNJ>D<ODQ@ <MM<T I<H@?
WBMVFT. 1C@ I@R <NNJ>D<ODQ@ <MM<T NCJPG? KMJQD?@ Q<GP@N OC<O >JMM@NKJI? OJ I<H@N.
#JM @S<HKG@, OC@ AJGGJRDIB GDI@ NCJPG? KMDIO 20:
XSJUFMO(WBMVFT<"UXFOUZ">);
... F:7 EA>GF;A@
8AD735: "AAB
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
136
TXJUDI and DBTF 33
TXJUDI DN < NO<O@H@IO OC<O <GGJRN >JHK<MDIB OC@ Q<GP@ JA <I @SKM@NNDJI <B<DINO HPGODKG@
NK@>D<G Q<GP@N. &O DN NDHDG<M OJ =PO IJO OC@ N<H@ <N <I "DA, @GN@ DA, @GN@" >C<DI. DBTF DN PN@? AJM
NK@>DATDIB OC@ Q<GP@N OC<O <M@ OJ =@ >JHK<M@? RDOC TXJUDI'N @SKM@NNDJI. DBTF DN < K<MO JA OC@
TXJUDI NO<O@H@IO, IJO < NO<O@H@IO DON@GA.
TXJUDI O<F@N <I @SKM@NNDJI RDOCDI K<M@IOC@N@N, >JHK<M@N OC@ Q<GP@ JA OC<O @SKM@NNDJI OJ OC@
DBTF Q<GP@N, <I? @S@>PO@N OC@ JK@M<ODJIN JA OC@ DBTF OC<O DN @LP<G OJ OC@ Q<GP@ JA OC@
@SKM@NNDJI. &ON NTIO<S >JINDNON JA < TXJUDI =GJ>F OC<O >JIO<DIN JI@ JM HJM@ DBTF N@>ODJIN <I?
< EFGBVMU N@>ODJI:
TXJUDI (expression) \
DBTF value_1:
// PQFSBUJPOT UP FYFDVUF JG UIF FYQSFTTJPO JT FRVBM UP WBMVF@1
// ...
CSFBL;
DBTF value_2:
// PQFSBUJPOT UP FYFDVUF JG UIF FYQSFTTJPO JT FRVBM UP WBMVF@2
// ...
CSFBL;
// ... PUIFS DBTFT ...
EFGBVMU:
// PQFSBUJPOT UP FYFDVUF JG UIF FYQSFTTJPO JT OPU FRVBM UP BOZ DBTF
// ...
CSFBL;
^
AGOCJPBC DO DN PN@? DI >JI?DODJI<G >C@>FN, OC@ @SKM@NNDJI OC<O TXJUDI O<F@N DN IJO PN@? <N <
GJBD><G @SKM@NNDJI. &O DN IJO @Q<GP<O@? <N "DA OCDN >JI?DODJI DN OMP@" <N DI <I JG NO<O@H@IO. 1C@
H3>G7 JA OC@ TXJUDI @SKM@NNDJI DN PN@? DI @LP<GDOT >JHK<MDNJIN RDOC OC@ DBTF Q<GP@N. &O DN
NDHDG<M OJ <I "DA, @GN@ DA, @GN@" >C<DI OC<O C<N JIGT @LP<GDOT >JHK<MDNJIN:
BVUP WBMVF = expression;
JG (WBMVF == value_1) \
// PQFSBUJPOT GPS WBMVF@1
// ...
^ FMTF JG (WBMVF == value_2) \
// PQFSBUJPOT GPS WBMVF@2
// ...
^
// ... PUIFS 'FMTF JG'T ...
^ FMTF \
// PQFSBUJPOT GPS PUIFS WBMVFT
// ...
^
%JR@Q@M, OC@ "DA, @GN@ DA, @GN@" <=JQ@ DN IJO OC@ @LPDQ<G@IO JA OC@ TXJUDI NO<O@H@IO. 1C@
M@<NJIN RDGG =@ @SKG<DI@? DI OC@ AJGGJRDIB N@>ODJIN.
&A < DBTF Q<GP@ H<O>C@N OC@ Q<GP@ JA OC@ TXJUDI @SKM@NNDJI, OC@I OC@ JK@M<ODJIN OC<O <M@
PI?@M OC@ DBTF <M@ @S@>PO@?. &A IJ Q<GP@ H<O>C@N, OC@I OC@ JK@M<ODJIN OC<O <M@ PI?@M OC@
EFGBVMU <M@ @S@>PO@?. 1C@ JK@M<ODJIN <M@ @S@>PO@? PIODG < CSFBL JM < HPUP NO<O@H@IO DN
M@<>C@?.
EI;F5: 3@6 53E7
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
137
The HPUP statement
33.1
0JH@ PN@N JA HPUP DN <?QDN@? <B<DINO DI HJNO KMJBM<HHDIB G<IBP<B@N DI B@I@M<G. %JR@Q@M,
HPUP DN PN@APG DI TXJUDI NO<O@H@ION DI M<M@ NDOP<ODJIN. 1C@ HPUP NO<O@H@IO RDGG =@ >JQ@M@? DI
HJM@ ?@O<DG DI < G<O@M >C<KO@M.
DBTF ?J@N IJO DIOMJ?P>@ < E5AB7 <N OC@ JG NO<O@H@IO ?J@N. ,I>@ OC@ JK@M<ODJIN RDOCDI <I JG
JM FMTF N>JK@ <M@ ADIDNC@?, OC@ @Q<GP<ODJI JA OC@ @IODM@ JG NO<O@H@IO DN <GNJ ADIDNC@?. &O DN IJO
OC@ N<H@ RDOC OC@ DBTF N@>ODJIN; JI>@ < H<O>CDIB DBTF DN AJPI?, OC@ @S@>PODJI JA OC@
KMJBM<H EPHKN OJ OC<O ><N@ <I? @S@>PO@N OC@ JK@M<ODJIN PI?@M OC<O DBTF. 4C@I I@@?@? DI
M<M@ NDOP<ODJIN, HPUP DBTF H<F@N OC@ KMJBM<H @S@>PODJI EPHK PI?@M OC@ I@SO DBTF:
TXJUDI (WBMVF) \
DBTF 5:
XSJUFMO("GJWF");
HPUP DBTF; // DPOUJOVFT XJUI UIF OFYU DBTF
DBTF 4:
XSJUFMO("GPVS");
CSFBL;
EFGBVMU:
XSJUFMO("VOLOPXO");
CSFBL;
^
&A WBMVF DN 5, OC@ @S@>PODJI >JIODIP@N PI?@M OC@ DBTF 5 GDI@ <I? OC@ KMJBM<H KMDION "ADQ@".
1C@I OC@ HPUP DBTF NO<O@H@IO ><PN@N OC@ @S@>PODJI OJ >JIODIP@ OJ OC@ I@SO DBTF, <I? <N <
M@NPGO "AJPM" DN <GNJ KMDIO@?:
GJWF
GPVS
HPUP ><I <KK@<M DI OCM@@ R<TN PI?@M DBTF N@>ODJIN:
t HPUP DBTF ><PN@N OC@ @S@>PODJI OJ >JIODIP@ OJ OC@ I@SO DBTF.
t HPUP EFGBVMU ><PN@N OC@ @S@>PODJI OJ >JIODIP@ OJ OC@ EFGBVMU N@>ODJI.
t HPUP DBTF expression ><PN@N OC@ @S@>PODJI OJ >JIODIP@ OJ OC@ DBTF OC<O H<O>C@N OC<O
@SKM@NNDJI.
1C@ AJGGJRDIB KMJBM<H ?@HJINOM<O@N OC@N@ OCM@@ PN@N =T O<FDIB <?Q<IO<B@ JA < GPSFBDI GJJK:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
GPSFBDI (WBMVF; < 1, 2, 3, 10, 20 >) \
XSJUFGMO("--- WBMVF: %T ---", WBMVF);
TXJUDI (WBMVF) \
DBTF 1:
XSJUFMO("DBTF 1");
HPUP DBTF;
DBTF 2:
XSJUFMO("DBTF 2");
HPUP DBTF 10;
DBTF 3:
XSJUFMO("DBTF 3");
EI;F5: 3@6 53E7
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
138
HPUP EFGBVMU;
DBTF 10:
XSJUFMO("DBTF 10");
CSFBL;
EFGBVMU:
XSJUFMO("EFGBVMU");
CSFBL;
^
^
^
1C@ JPOKPO:
--- WBMVF: 1 ---
DBTF 1
DBTF 2
DBTF 10
--- WBMVF: 2 ---
DBTF 2
DBTF 10
--- WBMVF: 3 ---
DBTF 3
EFGBVMU
--- WBMVF: 10 ---
DBTF 10
--- WBMVF: 20 ---
EFGBVMU
The expression type must be integer, string, or CPPM
33.2
AIT OTK@ ><I =@ PN@? DI @LP<GDOT >JHK<MDNJIN DI JG NO<O@H@ION. ,I OC@ JOC@M C<I?, OC@ OTK@
JA OC@ TXJUDI @SKM@NNDJI DN GDHDO@? OJ DIO@B@M OTK@N, NOMDIB OTK@N, <I? CPPM.
TUSJOH PQ = /* ... */;
// ...
TXJUDI (PQ) \
DBTF "BEE":
SFTVMU = GJSTU + TFDPOE;
CSFBL;
DBTF "TVCUSBDU":
SFTVMU = GJSTU - TFDPOE;
CSFBL;
DBTF "NVMUJQMZ":
SFTVMU = GJSTU * TFDPOE;
CSFBL;
DBTF "EJWJEF":
SFTVMU = GJSTU / TFDPOE;
CSFBL;
EFGBVMU:
UISPX OFX &YDFQUJPO(GPSNBU("6OLOPXO PQFSBUJPO: %T", PQ));
^
Note: ):7 5A67 34AH7 F:DAIE 3@ 7J57BF;A@ I:7@ F:7 AB7D3F;A@ ;E @AF D75A9@;L76 4K F:7
BDA9D3?. ,7 I;>> E77 7J57BF;A@E ;@ 3 >3F7D 5:3BF7D.
EI;F5: 3@6 53E7
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
139
AGOCJPBC DO DN KJNND=G@ OJ PN@ CPPM @SKM@NNDJIN <N R@GG, NDI>@ CPPM C<N JIGT ORJ Q<GP@N, DO H<T
=@ HJM@ NPDO<=G@ OJ PN@ <I JG NO<O@H@IO JM OC@ O@MI<MT JK@M<OJM (:) RDOC OC<O OTK@.
DBTF values must be known at compile time
33.3
4DOC OC@ JG NO<O@H@IO, OC@ Q<GP@N OC<O <M@ JI =JOC ND?@N JA OC@ @LP<GDOT JK@M<OJM ><I =@
><G>PG<O@? <O MPI ODH@. #JM @S<HKG@, OC@ AJGGJRDIB JG NO<O@H@IO H<T =@ PNDIB ORJ Q<GP@N, JI@
JA RCD>C DN ><G>PG<O@? =T OC@ KMJBM<H <I? OC@ JOC@M @IO@M@? =T OC@ PN@M:
JG (HVFTT == OVNCFS) \
XSJUFMO("$PSSFDU!");
^
,I OC@ JOC@M C<I?, <GOCJPBC OC@ Q<GP@ JA OC@ TXJUDI @SKM@NNDJI DN IJMH<GGT ><G>PG<O@? <O MPI
ODH@, OC@ DBTF Q<GP@N HPNO =@ FIJRI <O >JHKDG@ ODH@.
Value ranges
33.4
/<IB@N JA Q<GP@N ><I =@ NK@>DAD@? =T .. =@OR@@I DBTFN:
TXJUDI (EJF7BMVF) \
DBTF 1:
XSJUFMO(":PV XPO");
CSFBL;
DBTF 2: .. DBTF 5:
XSJUFMO("*U'T B ESBX");
CSFBL;
DBTF 6:
XSJUFMO("* XPO");
CSFBL;
EFGBVMU:
/* 5IF QSPHSBN TIPVME OFWFS HFU IFSF CFDBVTF UIF DBTFT
* BCPWF DPWFS UIF FOUJSF SBOHF PG WBMJE EJF WBMVFT.
* (4FF 'GJOBM TXJUDI' CFMPX.) */
CSFBL;
^
1C@ >J?@ <=JQ@ ?@O@MHDI@N OC<O OC@ B<H@ @I?N DI < ?M<R RC@I OC@ ?D@ Q<GP@ DN 2, 3, 4, JM 5.
Distinct values
33.5
)@O'N <NNPH@ OC<O DO DN < ?M<R AJM OC@ Q<GP@N 2 <I? 4, M<OC@M OC<I AJM OC@ Q<GP@N OC<O <M@ DI OC@
M<IB@ 82,5:. !DNODI>O Q<GP@N JA < DBTF <M@ N@K<M<O@? =T >JHH<N:
DBTF 2, 4:
XSJUFMO("*U'T B ESBX");
CSFBL;
The GJOBM TXJUDI statement
33.6
1CDN NO<O@H@IO RJMFN NDHDG<MGT OJ OC@ M@BPG<M TXJUDI NO<O@H@IO RDOC OC@ AJGGJRDIB
?DAA@M@I>@N:
EI;F5: 3@6 53E7
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
140
t &O ><IIJO C<Q@ < EFGBVMU N@>ODJI. +JO@ OC<O OCDN N@>ODJI DN H@<IDIBG@NN RC@I OC@ DBTF
N@>ODJIN >JQ@M OC@ @IODM@ M<IB@ JA Q<GP@N <ITR<T, <N C<N =@@I RDOC OC@ NDS Q<GP@N JA OC@
?D@ <=JQ@.
t 3<GP@ M<IB@N ><IIJO =@ PN@? RDOC DBTFN (?DNODI>O Q<GP@N ><I =@).
t &A OC@ @SKM@NNDJI DN JA <I FOVN OTK@, <GG JA OC@ Q<GP@N JA OC@ OTK@ HPNO =@ >JQ@M@? =T OC@
DBTFN (R@ RDGG N@@ FOVN OTK@N DI OC@ I@SO >C<KO@M).
JOU EJF7BMVF = 1;
GJOBM TXJUDI (EJF7BMVF) \
DBTF 1:
XSJUFMO(":PV XPO");
CSFBL;
DBTF 2, 3, 4, 5:
XSJUFMO("*U'T B ESBX");
CSFBL;
DBTF 6:
XSJUFMO("* XPO");
CSFBL;
^
When to use
33.7
TXJUDI DN NPDO<=G@ AJM >JHK<MDIB OC@ Q<GP@ JA <I @SKM@NNDJI <B<DINO < N@O JA Q<GP@N OC<O <M@
FIJRI <O >JHKDG@ ODH@.
4C@I OC@M@ <M@ JIGT ORJ Q<GP@N OJ >JHK<M@, <I JG NO<O@H@IO H<T H<F@ HJM@ N@IN@. #JM
@S<HKG@, OJ >C@>F RC@OC@M DO DN C@<?N JM O<DGN:
JG (IFBET5BJMT3FTVMU == IFBET) \
// ...
^ FMTF \
// ...
^
AN < B@I@M<G MPG@, TXJUDI DN HJM@ NPDO<=G@ RC@I OC@M@ <M@ OCM@@ JM HJM@ Q<GP@N OJ >JHK<M@.
Exercises
33.8
1. 4MDO@ < ><G>PG<OJM KMJBM<H OC<O NPKKJMON <MDOCH@OD> JK@M<ODJIN. %<Q@ OC@ KMJBM<H ADMNO
M@<? OC@ JK@M<ODJI <N < TUSJOH, OC@I ORJ Q<GP@N JA OTK@ EPVCMF AMJH OC@ DIKPO. 1C@
><G>PG<OJM NCJPG? KMDIO OC@ M@NPGO JA OC@ JK@M<ODJI. #JM @S<HKG@, RC@I OC@ JK@M<ODJI <I?
Q<GP@N <M@ "<??" <I? "5 7", M@NK@>ODQ@GT, OC@ KMJBM<H NCJPG? KMDIO 12.
1C@ DIKPO ><I =@ M@<? <N DI OC@ AJGGJRDIB >J?@:
TUSJOH PQ;
EPVCMF GJSTU;
EPVCMF TFDPOE;
// ...
PQ = DIPNQ(SFBEMO());
SFBEG(" %T %T", &GJSTU, &TFDPOE);
2. &HKMJQ@ OC@ ><G>PG<OJM OJ NPKKJMO JK@M<OJMN GDF@ "+" DI <??DODJI OJ RJM?N GDF@ "<??".
3. %<Q@ OC@ KMJBM<H OCMJR <I @S>@KODJI AJM PIFIJRI JK@M<OJMN.
EI;F5: 3@6 53E7
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
141
... F:7 EA>GF;A@E
EI;F5: 3@6 53E7
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
142
FOVN 34
FOVN DN OC@ A@<OPM@ OC<O @I<=G@N ?@ADIDIB I<H@? >JINO<IO Q<GP@N.
Effects of magic constants on code quality
34.1
1C@ AJGGJRDIB >J?@ <KK@<M@? DI OC@ @S@M>DN@ NJGPODJIN JA OC@ &IO@B@MN <I? AMDOCH@OD>
,K@M<ODJIN >C<KO@M:
JG (PQFSBUJPO == 1) \
SFTVMU = GJSTU + TFDPOE;
^ FMTF JG (PQFSBUJPO == 2) \
SFTVMU = GJSTU - TFDPOE;
^ FMTF JG (PQFSBUJPO == 3) \
SFTVMU = GJSTU * TFDPOE;
^ FMTF JG (PQFSBUJPO == 4) \
SFTVMU = GJSTU / TFDPOE;
^
1C@ DIO@B@M GDO@M<GN 1, 2, 3, <I? 4 DI OC<O KD@>@ JA >J?@ <M@ ><GG@? ?39;5 5A@EF3@FE. &O DN IJO
@<NT OJ ?@O@MHDI@ RC<O @<>C JA OCJN@ GDO@M<GN H@<IN DI OC@ KMJBM<H. ,I@ HPNO @S<HDI@ OC@
>J?@ DI @<>C N>JK@ OJ ?@O@MHDI@ OC<O 1 H@<IN 366;F;A@, 2 H@<IN EG4FD35F;A@, @O>. 1CDN O<NF DN
M@G<ODQ@GT @<NT AJM OC@ >J?@ <=JQ@ =@><PN@ <GG JA OC@ N>JK@N >JIO<DI EPNO < NDIBG@ GDI@. &O RJPG?
=@ >JIND?@M<=GT HJM@ ?DAAD>PGO OJ ?@>DKC@M OC@ H@<IDIBN JA H<BD> >JINO<ION DI HJNO JOC@M
KMJBM<HN.
*<BD> >JINO<ION HPNO =@ <QJD?@? =@><PN@ OC@T M@?P>@ ORJ HJNO DHKJMO<IO LP<GDOD@N JA
KMJBM<HN: M@<?<=DGDOT <I? H<DIO<DI<=DGDOT.
FOVN @I<=G@N BDQDIB I<H@N OJ NP>C >JINO<ION <I? <N < >JIN@LP@I>@ H<FDIB OC@ >J?@ HJM@
M@<?<=G@ <I? H<DIO<DI<=G@. "<>C >JI?DODJI RJPG? =@ M@<?DGT PI?@MNO<I?<=G@ RC@I OC@
AJGGJRDIB FOVN Q<GP@N R@M@ PN@?:
JG (PQFSBUJPO == 0QFSBUJPO.BEE) \
SFTVMU = GJSTU + TFDPOE;
^ FMTF JG (PQFSBUJPO == 0QFSBUJPO.TVCUSBDU) \
SFTVMU = GJSTU - TFDPOE;
^ FMTF JG (PQFSBUJPO == 0QFSBUJPO.NVMUJQMZ) \
SFTVMU = GJSTU * TFDPOE;
^ FMTF JG (PQFSBUJPO == 0QFSBUJPO.EJWJEF) \
SFTVMU = GJSTU / TFDPOE;
^
1C@ FOVN OTK@ 0QFSBUJPO <=JQ@ OC<O J=QD<O@N OC@ I@@? AJM H<BD> >JINO<ION 1, 2, 3, <I? 4 ><I
=@ ?@ADI@? GDF@ OCDN:
FOVN 0QFSBUJPO \ BEE = 1, TVCUSBDU, NVMUJQMZ, EJWJEF ^
The FOVN syntax
34.2
1C@ NDHKG@NO AJMH JA <I FOVN ?@ADIDODJI DN OC@ AJGGJRDIB:
FOVN TypeName \ ValueName_1, ValueName_2, /* FUD. */ ^
7@G?
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
143
0JH@ODH@N DO DN I@>@NN<MT OJ NK@>DAT OC@ <>OP<G OTK@ (OC@ 43E7 FKB7) JA OC@ Q<GP@N <N R@GG:
FOVN TypeName : base_type \ ValueName_1, ValueName_2, /* FUD. */ ^
4@ RDGG N@@ CJR OCDN DN PN@? DI OC@ I@SO N@>ODJI.
)KB7$3?7 ?@ADI@N RC<O OC@ Q<GP@N >JGG@>ODQ@GT H@<I. AGG JA OC@ Q<GP@N JA <I FOVN FKB7 <M@
GDNO@? RDOCDI >PMGT =M<>F@ON. %@M@ <M@ NJH@ @S<HKG@N:
FOVN )FBET0S5BJMT \ IFBET, UBJMT ^
FOVN 4VJU \ TQBEFT, IFBSUT, EJBNPOET, DMVCT ^
FOVN 'BSF \ SFHVMBS, DIJME, TUVEFOU, TFOJPS ^
"<>C N@O JA Q<GP@N =@>JH@N K<MO JA < N@K<M<O@ OTK@. #JM @S<HKG@, IFBET <I? UBJMT =@>JH@
Q<GP@N JA OC@ OTK@ )FBET0S5BJMT. 1C@ I@R OTK@ ><I =@ PN@? GDF@ JOC@M API?<H@IO<G OTK@N
RC@I ?@ADIDIB Q<MD<=G@N:
)FBET0S5BJMT SFTVMU; // EFGBVMU JOJUJBMJ[FE
BVUP IU = )FBET0S5BJMT.IFBET; // JOGFSSFE UZQF
AN C<N =@@I N@@I DI OC@ >J?@N <=JQ@, OC@ Q<GP@N JA FOVN OTK@N <M@ <GR<TN NK@>DAD@? =T OC@
I<H@ JA OC@DM FOVN OTK@:
JG (SFTVMU == )FBET0S5BJMT.IFBET) \
// ...
^
Actual values and base types
34.3
1C@ Q<GP@N JA FOVN OTK@N <M@ IJMH<GGT DHKG@H@IO@? DI OC@ =<>FBMJPI? <N JOU Q<GP@N. &I
JOC@M RJM?N, <GOCJPBC OC@T <KK@<M <N I<H@? Q<GP@N <N IFBET <I? UBJMT DI OC@ >J?@, OC@T <M@
<>OP<GGT JOU Q<GP@N. (Note: !F ;E BAEE;4>7 FA 5:AAE7 3 FKB7 AF:7D F:3@ int I:7@ @77676.).
2IG@NN @SKGD>DOGT NK@>DAD@? =T OC@ KMJBM<HH@M, OC@ JOU Q<GP@N NO<MO =T 0 <I? <M@ DI>M@H@IO@?
=T JI@ AJM @<>C FOVN Q<GP@. #JM @S<HKG@, OC@ ORJ Q<GP@N JA OC@ )FBET0S5BJMT C<Q@ OC@ Q<GP@N
0 <I? 1:
XSJUFMO("IFBET JT 0: ", ()FBET0S5BJMT.IFBET == 0));
XSJUFMO("UBJMT JT 1: ", ()FBET0S5BJMT.UBJMT == 1));
1C@ JPOKPO:
IFBET JT 0: USVF
UBJMT JT 1: USVF
&O DN KJNND=G@ OJ H<IP<GGT M@N@O OC@ Q<GP@N <O <IT KJDIO. 1C<O C<N =@@I OC@ ><N@ RC@I
NK@>DATDIB OC@ Q<GP@ JA 0QFSBUJPO.BEE <N 1 <=JQ@. 1C@ AJGGJRDIB @S<HKG@ M@N@ON OC@ Q<GP@N
ORD>@:
FOVN 5FTU \ B, C, D, ` = 100, E, F, G = 222, H, d ^
XSJUFGMO("%E %E %E", 5FTU.C, 5FTU.`, 5FTU.d);
1C@ JPOKPO:
1 100 224
7@G?
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
144
&A JOU DN IJO NPDO<=G@ <N OC@ =<N@ OTK@ JA OC@ FOVN Q<GP@N, OC@ =<N@ OTK@ ><I =@ NK@>DAD@?
@SKGD>DOGT <AO@M OC@ I<H@ JA OC@ FOVN:
FOVN /BUVSBM$POTUBOU : EPVCMF \ QJ = 3.14, F = 2.72 ^
FOVN 5FNQFSBUVSF6OJU : TUSJOH \ $ = "$FMTJVT", ' = "'BISFOIFJU" ^
FOVN values that are not of an FOVN type
34.4
4@ C<Q@ ?DN>PNN@? OC<O DO DN DHKJMO<IO OJ <QJD? H<BD> >JINO<ION <I? DINO@<? OJ O<F@
<?Q<IO<B@ JA OC@ FOVN A@<OPM@.
%JR@Q@M, NJH@ODH@N DO H<T IJO =@ I<OPM<G OJ >JH@ PK RDOC FOVN OTK@ I<H@N EPNO OJ PN@
I<H@? >JINO<ION. )@O'N <NNPH@ OC<O < I<H@? >JINO<IO DN I@@?@? OJ M@KM@N@IO OC@ IPH=@M JA
N@>JI?N K@M ?<T. &O NCJPG? IJO =@ I@>@NN<MT OJ <GNJ ?@ADI@ <I FOVN FKB7 AJM OCDN >JINO<IO
Q<GP@. AGG OC<O DN I@@?@? DN < >JINO<IO Q<GP@ OC<O ><I =@ M@A@MM@? OJ =T DON I<H@. &I NP>C
><N@N, OC@ OTK@ JA OC@ FOVN <I? OC@ Q<GP@ K<M@IOC@N@N <M@ IJO NK@>DAD@?:
FOVN TFDPOET1FS%BZ = 60 * 60 * 24;
1C@ OTK@ JA OC@ Q<GP@ ><I =@ NK@>DAD@? @SKGD>DOGT, RCD>C RJPG? =@ M@LPDM@? DA OC@ OTK@ ><IIJO
=@ DIA@MM@? AMJH OC@ MDBCO C<I? ND?@:
FOVN JOU TFDPOET1FS%BZ = 60 * 60 * 24;
0DI>@ OC@M@ DN IJ FOVN OTK@ OJ M@A@M OJ, NP>C I<H@? >JINO<ION ><I =@ PN@? DI >J?@ NDHKGT =T
OC@DM I<H@N:
UPUBM4FDPOET = UPUBM%BZT * TFDPOET1FS%BZ;
FOVN ><I =@ PN@? AJM ?@ADIDIB I<H@? >JINO<ION JA JOC@M OTK@N <N R@GG. #JM @S<HKG@, OC@ OTK@
JA OC@ AJGGJRDIB >JINO<IO RJPG? =@ TUSJOH:
FOVN GJMF/BNF = "MJTU.UYU";
Properties
34.5
1C@ .NJO <I? .NBY KMJK@MOD@N <M@ OC@ HDIDHPH <I? H<SDHPH Q<GP@N JA <I FOVN OTK@. 4C@I
OC@ Q<GP@N JA OC@ FOVN OTK@ <M@ >JIN@>PODQ@, OC@T ><I =@ DO@M<O@? JQ@M DI < GPS GJJK RDOCDI
OC@N@ GDHDON:
FOVN 4VJU \ TQBEFT, IFBSUT, EJBNPOET, DMVCT ^
GPS (BVUP TVJU = 4VJU.NJO; TVJU <= 4VJU.NBY; ++TVJU) \
XSJUFGMO("%T: %E", TVJU, TVJU);
^
#JMH<O NK@>DAD@MN "%T" <I? "%E" KMJ?P>@ ?DAA@M@IO JPOKPON:
TQBEFT: 0
IFBSUT: 1
EJBNPOET: 2
DMVCT: 3
+JO@ OC<O < GPSFBDI GJJK JQ@M OC<O M<IB@ RJPG? G@<Q@ OC@ .NBY Q<GP@ JPO JA OC@ DO@M<ODJI:
7@G?
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
145
GPSFBDI (TVJU; 4VJU.NJO .. 4VJU.NBY) \
XSJUFGMO("%T: %E", TVJU, TVJU);
^
1C@ JPOKPO:
TQBEFT: 0
IFBSUT: 1
EJBNPOET: 2
l DMVCT JT NJTTJOH
Converting from the base type
34.6
AN C<N =@@I N@@I DI OC@ AJMH<OO@? JPOKPON <=JQ@, <I FOVN Q<GP@ ><I <POJH<OD><GGT =@
>JIQ@MO@? OJ DON =<N@ OTK@ (@.B. OJ JOU). 1C@ M@Q@MN@ >JIQ@MNDJI DN IJO <POJH<OD>:
4VJU TVJU = 1; // j DPNQJMBUJPO &3303
1C@ M@<NJI AJM OCDN DN OJ <QJD? @I?DIB PK RDOC DIQ<GD? FOVN Q<GP@N:
TVJU = 100; // j XPVME CF BO JOWBMJE FOVN WBMVF
1C@ Q<GP@N OC<O <M@ FIJRI OJ >JMM@NKJI? OJ Q<GD? FOVN Q<GP@N JA < K<MOD>PG<M FOVN OTK@ ><I
NODGG =@ >JIQ@MO@? OJ OC<O OTK@ =T <I @SKGD>DO FKB7 53EF:
TVJU = DBTU(4VJU)1; // OPX IFBSUT
&O RJPG? =@ OC@ KMJBM<HH@M'N M@NKJIND=DGDOT OJ @INPM@ OC@ Q<GD?DOT JA OC@ Q<GP@N RC@I <I
@SKGD>DO ><NO DN PN@?. 4@ RDGG N@@ OTK@ ><NODIB <I? OTK@ >JIQ@MNDJIN DI G<O@M >C<KO@MN.
Exercise
34.7
t *J?DAT OC@ ><G>PG<OJM KMJBM<H AMJH OC@ @S@M>DN@N JA OC@ &IO@B@MN <I? AMDOCH@OD>
,K@M<ODJIN >C<KO@M =T C<QDIB OC@ PN@M N@G@>O OC@ <MDOCH@OD> JK@M<ODJI AMJH < H@IP.
1CDN KMJBM<H NCJPG? =@ ?DAA@M@IO AMJH OC@ KM@QDJPN JI@ <O G@<NO DI OC@ AJGGJRDIB
<M@<N:
w 2N@ FOVN Q<GP@N, IJO H<BD> >JINO<ION.
w 2N@ EPVCMF DINO@<? JA JOU.
w 2N@ < TXJUDI NO<O@H@IO DINO@<? JA <I "DA, @GN@ DA, @GN@" >C<DI.
... F:7 EA>GF;A@
7@G?
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
146
Functions 35
0DHDG<M OJ CJR API?<H@IO<G OTK@N <M@ =PDG?DIB =GJ>FN JA KMJBM<H ?<O<, API>ODJIN <M@ =PDG?DIB
=GJ>FN JA KMJBM<H =@C<QDJM.
#PI>ODJIN <M@ <GNJ >GJN@GT M@G<O@? OJ OC@ >M<AO <NK@>O JA KMJBM<HHDIB. 1C@ API>ODJIN OC<O <M@
RMDOO@I =T @SK@MD@I>@? KMJBM<HH@MN <M@ NP>>DI>O, NDHKG@, <I? >G@<M. 1CDN BJ@N =JOC R<TN:
1C@ H@M@ <>O JA OMTDIB OJ D?@IODAT <I? RMDO@ NH<GG@M =PDG?DIB =GJ>FN JA < KMJBM<H H<F@N <
=@OO@M KMJBM<HH@M.
4@ C<Q@ >JQ@M@? =<ND> NO<O@H@ION <I? @SKM@NNDJIN DI KM@QDJPN >C<KO@MN. AGOCJPBC OC@M@ RDGG
=@ H<IT HJM@ OC<O R@ RDGG N@@ DI G<O@M >C<KO@MN, RC<O R@ C<Q@ N@@I NJ A<M <M@ >JHHJIGT-
PN@? A@<OPM@N JA !. 0ODGG, OC@T <M@ IJO NPAAD>D@IO JI OC@DM JRI OJ RMDO@ G<MB@ KMJBM<HN. 1C@
KMJBM<HN OC<O R@ C<Q@ RMDOO@I NJ A<M C<Q@ <GG =@@I Q@MT NCJMO, @<>C ?@HJINOM<ODIB EPNO <
NDHKG@ A@<OPM@ JA OC@ G<IBP<B@. 1MTDIB OJ RMDO@ < KMJBM<H RDOC <IT G@Q@G JA >JHKG@SDOT
RDOCJPO API>ODJIN RJPG? =@ Q@MT ?DAAD>PGO <I? JK@I OJ =PBN.
#PI>ODJIN <M@ A@<OPM@N OC<O KPO NO<O@H@ION <I? @SKM@NNDJIN OJB@OC@M <N PIDON JA KMJBM<H
@S@>PODJI. 0P>C NO<O@H@ION <I? @SKM@NNDJI <M@ BDQ@I < I<H@ OC<O ?@N>MD=@N RC<O OC@T
>JGG@>ODQ@GT <>CD@Q@. 1C@T ><I OC@I =@ 53>>76 (JM 7J75GF76) =T OC<O I<H@.
1C@ >JI>@KO JA 9;H;@9 @3?7E FA 3 9DAGB A8 EF7BE DN >JHHJI DI ?<DGT GDQ@N. #JM @S<HKG@, OC@
<>O JA >JJFDIB <I JH@G@O ><I =@ ?@N>MD=@? DI NJH@ G@Q@G JA ?@O<DG =T OC@ AJGGJRDIB NO@KN:
t B@O OC@ K<I
t B@O =POO@M
t B@O OC@ @BB
t OPMI JI OC@ NOJQ@
t KPO OC@ K<I JI OC@ ADM@
t KPO =POO@M DIOJ OC@ K<I RC@I DO DN CJO
t KPO OC@ @BB DIOJ =POO@M RC@I DO DN H@GO@?
t M@HJQ@ OC@ K<I AMJH OC@ ADM@ RC@I OC@ @BB DN >JJF@?
t OPMI JAA OC@ NOJQ@
0DI>@ OC<O HP>C ?@O<DG DN J=QDJPNGT @S>@NNDQ@, NO@KN OC<O <M@ M@G<O@? OJB@OC@M RJPG? =@
>JH=DI@? PI?@M < NDIBG@ I<H@:
t H<F@ KM@K<M<ODJIN (B@O OC@ K<I, =POO@M, <I? OC@ @BB)
t OPMI JI OC@ NOJQ@
t >JJF OC@ @BB (KPO OC@ K<I JI OC@ ADM@, @O>.)
t OPMI JAA OC@ NOJQ@
$JDIB APMOC@M, OC@M@ ><I =@ < NDIBG@ I<H@ AJM <GG JA OC@ NO@KN:
t H<F@ < JI@-@BB JH@G@O (<GG JA OC@ NO@KN)
#PI>ODJIN <M@ =<N@? JI OC@ N<H@ >JI>@KO: 0O@KN OC<O ><I >JGG@>ODQ@GT =@ I<H@? <N < RCJG@
<M@ KPO OJB@OC@M OJ AJMH < API>ODJI. AN <I @S<HKG@, G@O'N NO<MO RDOC OC@ AJGGJRDIB GDI@N JA >J?@
OC<O <>CD@Q@ OC@ O<NF JA KMDIODIB < H@IP:
XSJUFMO(" 0 &YJU");
XSJUFMO(" 1 "EE");
XSJUFMO(" 2 4VCUSBDU");
XSJUFMO(" 3 .VMUJQMZ");
XSJUFMO(" 4 %JWJEF");
0DI>@ DO RJPG? H<F@ N@IN@ OJ I<H@ OCJN@ GDI@N <GOJB@OC@M <N QSJOU.FOV, OC@T ><I =@ KPO
OJB@OC@M OJ AJMH < API>ODJI =T OC@ AJGGJRDIB NTIO<S:
FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
147
WPJE QSJOU.FOV()
\
XSJUFMO(" 0 &YJU");
XSJUFMO(" 1 "EE");
XSJUFMO(" 2 4VCUSBDU");
XSJUFMO(" 3 .VMUJQMZ");
XSJUFMO(" 4 %JWJEF");
^
1C@ >JIO@ION JA OC<O API>ODJI ><I IJR =@ @S@>PO@? AMJH RDOCDI NBJO() NDHKGT =T DON I<H@:
WPJE NBJO()
\
QSJOU.FOV();
/* ... */
^
&O H<T =@ J=QDJPN AMJH OC@ NDHDG<MDOD@N JA OC@ ?@ADIDODJIN JA QSJOU.FOV() <I? NBJO() OC<O
NBJO() DN < API>ODJI <N R@GG. 1C@ @S@>PODJI JA < ! KMJBM<H NO<MON RDOC OC@ API>ODJI I<H@?
NBJO() <I? =M<I>C@N JPO OJ JOC@M API>ODJIN AMJH OC@M@.
Parameters
35.1
0JH@ JA OC@ KJR@MN JA API>ODJIN >JH@ AMJH OC@ A<>O OC<O OC@DM =@C<QDJMN <M@ <?EPNO<=G@
OCMJPBC K<M<H@O@MN.
)@O'N >JIODIP@ RDOC OC@ JH@G@O @S<HKG@ =T HJ?DATDIB DO OJ H<F@ <I JH@G@O JA ADQ@ @BBN
DINO@<? JA <GR<TN JI@. 1C@ NO@KN RJPG? @S<>OGT =@ OC@ N<H@, OC@ JIGT ?DAA@M@I>@ =@DIB OC@
IPH=@M JA @BBN OJ PN@. 4@ ><I >C<IB@ OC@ HJM@ B@I@M<G ?@N>MDKODJI <=JQ@ <>>JM?DIBGT:
t H<F@ KM@K<M<ODJIN (B@O OC@ K<I, =POO@M, <I? ADQ@ @BBN)
t OPMI JI OC@ NOJQ@
t >JJF OC@ @BBN (KPO OC@ K<I JI OC@ ADM@, @O>.)
t OPMI JAA OC@ NOJQ@
)DF@RDN@, OC@ HJNO B@I@M<G NDIBG@ NO@K RJPG? =@>JH@ OC@ AJGGJRDIB:
t H<F@ < ADQ@-@BB JH@G@O (<GG JA OC@ NO@KN)
1CDN ODH@ OC@M@ DN <I <??DODJI<G DIAJMH<ODJI OC<O >JI>@MIN NJH@ JA OC@ NO@KN: "B@O ADQ@ @BBN",
">JJF OC@ @BBN", <I? "H<F@ < ADQ@-@BB JH@G@O".
1C@ =@C<QDJMN JA API>ODJIN ><I =@ <?EPNO@? NDHDG<MGT OJ OC@ JH@G@O @S<HKG@. 1C@ DIAJMH<ODJI
OC<O API>ODJIN PN@ OJ <?EPNO OC@DM =@C<QDJM <M@ ><GG@? B3D3?7F7DE. -<M<H@O@MN <M@ NK@>DAD@? DI
OC@ 8G@5F;A@ B3D3?7F7D >;EF, N@K<M<O@? =T >JHH<N AMJH @<>C JOC@M. 1C@ K<M<H@O@M GDNO DN
DIND?@ JA OC@ K<M@IOC@N@N OC<O >JH@N <AO@M OC@ I<H@ JA OC@ API>ODJI.
1C@ QSJOU.FOV() API>ODJI <=JQ@ C<N =@@I ?@ADI@? RDOC <I @HKOT K<M<H@O@M GDNO =@><PN@ OC<O
API>ODJI <GR<TN KMDIO@? OC@ N<H@ H@IP. )@O'N <NNPH@ OC<O NJH@ODH@N OC@ H@IP RDGG I@@? OJ
=@ KMDIO@? ?DAA@M@IOGT DI ?DAA@M@IO >JIO@SON. #JM @S<HKG@, DO H<T H<F@ HJM@ N@IN@ OJ KMDIO OC@
ADMNO @IOMT <N "/@OPMI" DINO@<? JA ""SDO" ?@K@I?DIB JI OC@ K<MO JA OC@ KMJBM<H OC<O DN =@DIB
@S@>PO@? <O OC<O ODH@.
&I NP>C < ><N@, OC@ ADMNO @IOMT JA OC@ H@IP ><I =@ B3D3?7F7D;L76 =T C<QDIB =@@I ?@ADI@? DI
OC@ K<M<H@O@M GDNO. 1C@ API>ODJI OC@I PN@N OC@ Q<GP@ JA OC<O K<M<H@O@M DINO@<? JA OC@ GDO@M<G
"&YJU":
FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
148
WPJE QSJOU.FOV(TUSJOH GJSTU&OUSZ)
\
XSJUFMO(" 0 ", GJSTU&OUSZ);
XSJUFMO(" 1 "EE");
XSJUFMO(" 2 4VCUSBDU");
XSJUFMO(" 3 .VMUJQMZ");
XSJUFMO(" 4 %JWJEF");
^
+JOD>@ OC<O NDI>@ OC@ DIAJMH<ODJI OC<O OC@ GJSTU&OUSZ K<M<H@O@M >JIQ@TN DN < KD@>@ JA O@SO,
DON OTK@ C<N =@@I NK@>DAD@? <N TUSJOH DI OC@ K<M<H@O@M GDNO. 1CDN API>ODJI ><I IJR =@ 53>>76
RDOC ?DAA@M@IO K<M<H@O@M Q<GP@N OJ KMDIO H@IPN C<QDIB ?DAA@M@IO ADMNO @IOMD@N. AGG OC<O I@@?N OJ
=@ ?JI@ DN OJ PN@ <KKMJKMD<O@ TUSJOH Q<GP@N ?@K@I?DIB JI RC@M@ OC@ API>ODJI DN =@DIB ><GG@?
AMJH:
// "U TPNF QMBDF JO UIF QSPHSBN:
QSJOU.FOV("&YJU");
// ...
// "U TPNF PUIFS QMBDF JO UIF QSPHSBN:
QSJOU.FOV("3FUVSO");
Note: 4C@I TJP RMDO@ TJPM JRI API>ODJIN, TJP H<T @I>JPIO@M >JHKDG<ODJI @MMJMN RDOC
K<M<H@O@MN JA OTK@ TUSJOH. 1C@ QSJOU.FOV() API>ODJI <=JQ@ C<N NP>C < KMJ=G@H. AN
RMDOO@I, DO ><IIJO =@ ><GG@? =T K<M<H@O@M Q<GP@N JA OTK@ DIBS<>. #JM @S<HKG@, OC@ AJGGJRDIB
>J?@ RJPG? ><PN@ < >JHKDG<ODJI @MMJM:
DIBS<> BO&OUSZ;
BO&OUSZ _= "5BLF TRVBSF SPPU";
QSJOU.FOV(BO&OUSZ); // j DPNQJMBUJPO &3303
,I OC@ JOC@M C<I?, DA QSJOU.FOV() R@M@ ?@ADI@? OJ O<F@ DON K<M<H@O@M <N DIBS<>, OC@I DO
>JPG? IJO =@ ><GG@? RDOC TUSJOHN GDF@ "&YJU". 1CDN DN M@G<O@? OJ OC@ >JI>@KO JA DHHPO<=DGDOT
<I? OC@ JNNVUBCMF F@TRJM?, =JOC JA RCD>C RDGG =@ >JQ@M@? DI OC@ I@SO >C<KO@M.
)@O'N >JIODIP@ RDOC OC@ H@IP API>ODJI <I? <NNPH@ OC<O DO DN IJO <KKMJKMD<O@ OJ <GR<TN NO<MO
OC@ H@IP N@G@>ODJI IPH=@MN RDOC U@MJ. &I OC<O ><N@ OC@ NO<MODIB IPH=@M ><I <GNJ =@ K<NN@? OJ
OC@ API>ODJI <N DON N@>JI? K<M<H@O@M. 1C@ K<M<H@O@MN JA OC@ API>ODJI HPNO =@ N@K<M<O@? =T
>JHH<N:
WPJE QSJOU.FOV(TUSJOH GJSTU&OUSZ, JOU GJSTU/VNCFS)
\
XSJUFMO(' ', GJSTU/VNCFS, ' ', GJSTU&OUSZ);
XSJUFMO(' ', GJSTU/VNCFS + 1, " "EE");
XSJUFMO(' ', GJSTU/VNCFS + 2, " 4VCUSBDU");
XSJUFMO(' ', GJSTU/VNCFS + 3, " .VMUJQMZ");
XSJUFMO(' ', GJSTU/VNCFS + 4, " %JWJEF");
^
&O DN IJR KJNND=G@ OJ O@GG OC@ API>ODJI RC<O IPH=@M OJ NO<MO AMJH:
QSJOU.FOV("3FUVSO", 1);
Calling a function
35.2
0O<MODIB < API>ODJI NJ OC<O DO <>CD@Q@N DON O<NF DN ><GG@? 53>>;@9 3 8G@5F;A@. 1C@ API>ODJI ><GG
NTIO<S DN OC@ AJGGJRDIB:
function_name(parameter_values)
FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
149
1C@ <>OP<G K<M<H@O@M Q<GP@N OC<O <M@ K<NN@? OJ API>ODJIN <M@ ><GG@? 8G@5F;A@ 3D9G?7@FE.
AGOCJPBC OC@ O@MHN B3D3?7F7D <I? 3D9G?7@F <M@ NJH@ODH@N PN@? DIO@M>C<IB@<=GT DI OC@
GDO@M<OPM@, OC@T NDBIDAT ?DAA@M@IO >JI>@KON.
1C@ <MBPH@ION <M@ H<O>C@? OJ OC@ K<M<H@O@MN JI@ =T JI@ DI OC@ JM?@M OC<O OC@ K<M<H@O@MN
<M@ ?@ADI@?. #JM @S<HKG@, OC@ G<NO ><GG JA QSJOU.FOV() <=JQ@ PN@N OC@ 3D9G?7@FE "3FUVSO"
<I? 1, RCD>C >JMM@NKJI? OJ OC@ B3D3?7F7DE GJSTU&OUSZ <I? GJSTU/VNCFS, M@NK@>ODQ@GT.
1C@ OTK@ JA @<>C <MBPH@IO HPNO H<O>C OC@ OTK@ JA OC@ >JMM@NKJI?DIB K<M<H@O@M.
Doing work
35.3
&I KM@QDJPN >C<KO@MN, R@ C<Q@ ?@ADI@? @SKM@NNDJIN <N @IODOD@N OC<O ?J RJMF. #PI>ODJI ><GGN <M@
@SKM@NNDJIN <N R@GG: OC@T ?J NJH@ RJMF. !JDIB RJMF ><I H@<I < >JH=DI<ODJI JA ORJ OCDIBN:
t Having side effects: 0D?@ @AA@>ON <M@ <IT >C<IB@ DI OC@ NO<O@ JA OC@ KMJBM<H JM DON
@IQDMJIH@IO. 0JH@ JK@M<ODJIN C<Q@ JIGT ND?@ @AA@>ON. AI @S<HKG@ DN CJR OC@ QSJOU.FOV()
API>ODJI <=JQ@ >C<IB@N TUEPVU =T KMDIODIB OJ DO. AN <IJOC@M @S<HKG@, < API>ODJI OC<O <??N
< 4UVEFOU J=E@>O OJ < NOP?@IO >JIO<DI@M RJPG? <GNJ C<Q@ < ND?@ @AA@>O: DO RJPG? =@ ><PNDIB
OC@ >JIO<DI@M OJ BMJR.
&I NPHH<MT, JK@M<ODJIN OC<O ><PN@ < >C<IB@ DI OC@ NO<O@ JA OC@ KMJBM<H C<Q@ ND?@
@AA@>ON.
t Producing a value: 0JH@ JK@M<ODJIN JIGT KMJ?P>@ Q<GP@N. #JM @S<HKG@, < API>ODJI OC<O
<??N IPH=@MN RJPG? =@ KMJ?P>DIB OC@ M@NPGO JA OC<O <??DODJI. AN <IJOC@M @S<HKG@, <
API>ODJI OC<O H<F@N < 4UVEFOU J=E@>O =T PNDIB OC@ NOP?@IO'N I<H@ <I? <??M@NN RJPG? =@
KMJ?P>DIB < 4UVEFOU J=E@>O.
t Having side effects and producing a value: 0JH@ JK@M<ODJIN ?J =JOC. #JM @S<HKG@, <
API>ODJI OC<O M@<?N ORJ Q<GP@N AMJH TUEJO <I? ><G>PG<O@N OC@DM NPH RJPG? =@ C<QDIB ND?@
@AA@>ON ?P@ OJ >C<IBDIB OC@ NO<O@ JA TUEJO <I? <GNJ KMJ?P>DIB OC@ NPH JA OC@ ORJ Q<GP@N.
t No operation: AGOCJPBC @Q@MT API>ODJI DN JA JI@ JA OC@ OCM@@ ><O@BJMD@N <=JQ@, NJH@ODH@N
NJH@ API>ODJIN @I? PK ?JDIB IJ RJMF <O <GG, ?@K@I?DIB JI >@MO<DI >JI?DODJIN <O >JHKDG@
ODH@ JM <O MPI ODH@.
The return value
35.4
1C@ Q<GP@ OC<O < API>ODJI KMJ?P>@N <N < M@NPGO JA DON RJMF DN ><GG@? DON D7FGD@ H3>G7. 1CDN O@MH
>JH@N AMJH OC@ J=N@MQ<ODJI OC<O JI>@ OC@ KMJBM<H @S@>PODJI =M<I>C@N DIOJ < API>ODJI, DO
@Q@IOP<GGT D7FGD@E =<>F OJ RC@M@ OC@ API>ODJI C<N =@@I ><GG@?. #PI>ODJIN B@O 53>>76 <I? OC@T
D7FGD@ Q<GP@N.
'PNO GDF@ <IT JOC@M Q<GP@, M@OPMI Q<GP@N C<Q@ OTK@N. 1C@ OTK@ JA OC@ M@OPMI Q<GP@ DN NK@>DAD@?
MDBCO =@AJM@ OC@ I<H@ JA OC@ API>ODJI <O OC@ KJDIO RC@M@ OC@ API>ODJI DN ?@ADI@?. #JM @S<HKG@,
< API>ODJI OC<O <??N ORJ Q<GP@N JA JOU <I? M@OPMIN OC@DM NPH <GNJ <N JOU RJPG? =@ ?@ADI@?
GDF@ OC@ AJGGJRDIB:
JOU BEE(JOU GJSTU, JOU TFDPOE)
\
// ... UIF BDUVBM XPSL PG UIF GVODUJPO ...
^
1C@ Q<GP@ OC<O < API>ODJI M@OPMIN O<F@N KG<>@ JA OC@ API>ODJI ><GG DON@GA. #JM @S<HKG@,
<NNPHDIB OC<O OC@ API>ODJI ><GG BEE(5, 7) KMJ?P>@N OC@ Q<GP@ 12, OC@I OC@ AJGGJRDIB ORJ
GDI@N RJPG? =@ @LPDQ<G@IO:
FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
150
XSJUFMO("3FTVMU: ", BEE(5, 7));
XSJUFMO("3FTVMU: ", 12);
AO OC@ ADMNO GDI@ <=JQ@, OC@ BEE() API>ODJI DN ><GG@? RDOC OC@ <MBPH@ION 5 <I? 7 478AD7
XSJUFMO() B@ON ><GG@?. 1C@ Q<GP@ 12 OC<O OC@ API>ODJI M@OPMIN DN DI OPMI K<NN@? OJ XSJUFMO()
<N DON N@>JI? <MBPH@IO.
1CDN <GGJRN K<NNDIB OC@ M@OPMI Q<GP@N JA API>ODJIN OJ JOC@M API>ODJIN OJ AJMH >JHKG@S
@SKM@NNDJIN:
XSJUFMO("3FTVMU: ", BEE(5, EJWJEF(100, TUVEFOU$PVOU())));
&I OC@ GDI@ <=JQ@, OC@ M@OPMI Q<GP@ JA TUVEFOU$PVOU() DN K<NN@? OJ EJWJEF() <N DON N@>JI?
<MBPH@IO, OC@ M@OPMI Q<GP@ JA EJWJEF() DN K<NN@? OJ BEE() <N DON N@>JI? <MBPH@IO, <I?
@Q@IOP<GGT OC@ M@OPMI Q<GP@ JA BEE() DN K<NN@? OJ XSJUFMO() <N DON N@>JI? <MBPH@IO.
The SFUVSO statement
35.5
1C@ M@OPMI Q<GP@ JA < API>ODJI DN NK@>DAD@? =T OC@ SFUVSO F@TRJM?:
JOU BEE(JOU GJSTU, JOU TFDPOE)
\
JOU SFTVMU = GJSTU + TFDPOE;
SFUVSO SFTVMU;
^
A API>ODJI KMJ?P>@N DON M@OPMI Q<GP@ =T O<FDIB <?Q<IO<B@ JA NO<O@H@ION, @SKM@NNDJIN, <I?
KJO@IOD<GGT ><GGDIB JOC@M API>ODJIN. 1C@ API>ODJI RJPG? OC@I M@OPMI OC<O Q<GP@ =T OC@ SFUVSO
F@TRJM?, <O RCD>C KJDIO OC@ @S@>PODJI JA OC@ API>ODJI @I?N.
&O DN KJNND=G@ OJ C<Q@ HJM@ OC<I JI@ SFUVSO NO<O@H@IO DI < API>ODJI. 1C@ Q<GP@ JA OC@ ADMNO
SFUVSO NO<O@H@IO OC<O B@ON @S@>PO@? ?@O@MHDI@N OC@ M@OPMI Q<GP@ JA OC@ API>ODJI AJM <
K<MOD>PG<M ><GG:
JOU DPNQMFY$BMDVMBUJPO(JOU B1BSBNFUFS, JOU BOPUIFS1BSBNFUFS)
\
JG (B1BSBNFUFS == BOPUIFS1BSBNFUFS) \
SFUVSO 0;
^
SFUVSO B1BSBNFUFS * BOPUIFS1BSBNFUFS;
^
1C@ API>ODJI <=JQ@ M@OPMIN 0 RC@I OC@ ORJ K<M<H@O@MN <M@ @LP<G, <I? OC@ KMJ?P>O JA OC@DM
Q<GP@N RC@I OC@T <M@ ?DAA@M@IO.
WPJE functions
35.6
1C@ M@OPMI OTK@N JA API>ODJIN OC<O ?J IJO KMJ?P>@ Q<GP@N <M@ NK@>DAD@? <N WPJE. 4@ C<Q@ N@@I
OCDN H<IT ODH@N RDOC OC@ NBJO() API>ODJI NJ A<M, <N R@GG <N OC@ QSJOU.FOV() API>ODJI <=JQ@.
0DI>@ OC@T ?J IJO M@OPMI <IT Q<GP@ OJ OC@ ><GG@M, OC@DM M@OPMI Q<GP@N C<Q@ =@@I ?@ADI@? <N
WPJE. (Note: main() 53@ 3>EA 47 678;@76 3E D7FGD@;@9 int. ,7 I;>> E77 F:;E ;@ 3 >3F7D 5:3BF7D.)
FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
151
The name of the function
35.7
1C@ I<H@ JA < API>ODJI HPNO =@ >CJN@I OJ >JHHPID><O@ OC@ KPMKJN@ JA OC@ API>ODJI >G@<MGT.
#JM @S<HKG@, OC@ I<H@N BEE <I? QSJOU.FOV C<N =@@I <KKMJKMD<O@ =@><PN@ OC@DM KPMKJN@N
C<Q@ =@@I OJ <?? ORJ Q<GP@N, <I? OJ KMDIO < H@IP, M@NK@>ODQ@GT.
A >JHHJI BPD?@GDI@ AJM API>ODJI I<H@N DN OC<O OC@T >JIO<DI < Q@M= GDF@ 366 JM BD;@F.
A>>JM?DIB OJ OCDN BPD?@GDI@ I<H@N GDF@ BEEJUJPO() <I? NFOV() RJPG? =@ G@NN OC<I D?@<G.
%JR@Q@M, DO DN <>>@KO<=G@ OJ I<H@ API>ODJIN NDHKGT <N IJPIN, DA OCJN@ API>ODJIN ?J IJO C<Q@
<IT ND?@ @AA@>ON. #JM @S<HKG@, < API>ODJI OC<O M@OPMIN OC@ >PMM@IO O@HK@M<OPM@ ><I =@ I<H@?
<N DVSSFOU5FNQFSBUVSF() DINO@<? JA HFU$VSSFOU5FNQFSBUVSF().
JHDIB PK RDOC I<H@N OC<O <M@ >G@<M, NCJMO, <I? >JINDNO@IO DN HJM@ JI OC@ <MO ND?@ JA
KMJBM<HHDIB.
Code quality through functions
35.8
#PI>ODJIN ><I DHKMJQ@ OC@ LP<GDOT JA >J?@. 0H<GG@M API>ODJIN RDOC A@R@M M@NKJIND=DGDOD@N G@<?
OJ KMJBM<HN OC<O <M@ @<ND@M OJ H<DIO<DI.
Code duplication is harmful 35.8.1
,I@ JA OC@ KM<>OD>@N OC<O DN CDBCGT ?@OMDH@IO<G OJ KMJBM<H LP<GDOT DN >J?@ ?PKGD><ODJI. J?@
?PKGD><ODJI DN RC@I OC@M@ <M@ HJM@ OC<I JI@ KD@>@ JA >J?@ DI OC@ KMJBM<H OC<O <>CD@Q@ OC@
N<H@ O<NF.
AGOCJPBC OCDN NJH@ODH@N C<KK@IN =T >JKTDIB GDI@N JA >J?@ OJ < I@R KG<>@, DO H<T <GNJ C<KK@I
=T @I?DIB PK RDOC NDHDG<M GDI@N JA >J?@ PIDIO@IODJI<GGT.
,I@ JA OC@ KMJ=G@HN RDOC >J?@ ?PKGD><ODJI DN OC<O KJO@IOD<G =PBN RJPG? I<OPM<GGT J>>PM DI <GG
JA OC@ >JKD@N, M@LPDMDIB OJ =@ ADS@? DI HJM@ OC<I JI@ KG<>@. JIQ@MN@GT, RC@I OC@ >J?@
<KK@<MN DI JIGT JI@ KG<>@ DI OC@ KMJBM<H, OC@I ADSDIB DO <O OC<O JI@ KG<>@ RJPG? B@O MD? JA OC@
=PB JI>@ <I? AJM <GG.
AN & C<Q@ H@IODJI@? <=JQ@, API>ODJIN <M@ >GJN@GT M@G<O@? OJ OC@ >M<AO <NK@>O JA KMJBM<HHDIB.
"SK@MD@I>@? KMJBM<HH@MN <M@ <GR<TN JI OC@ GJJFJPO AJM >J?@ ?PKGD><ODJI. 1C@T <GR<TN OMT OJ
D?@IODAT >JHHJI<GDOD@N DI >J?@ <I? HJQ@ >JHHJI KD@>@N JA >J?@ OJ N@K<M<O@ API>ODJIN (JM OJ
>JHHJI NOMP>ON, >G<NN@N, O@HKG<O@N, @O>. <N R@ RDGG N@@ DI G<O@M >C<KO@MN).
)@O'N NO<MO RDOC < KMJBM<H OC<O >JIO<DIN NJH@ >J?@ ?PKGD><ODJI. )@O'N N@@ CJR OC<O
?PKGD><ODJI ><I =@ M@HJQ@? =T HJQDIB >J?@ DIOJ API>ODJIN (D.@. =T D7835FAD;@9 OC@ >J?@). 1C@
AJGGJRDIB KMJBM<H M@<?N IPH=@MN AMJH OC@ DIKPO <I? KMDION OC@H ADMNO DI OC@ JM?@M OC<O OC@T
C<Q@ <MMDQ@? <I? OC@I DI IPH@MD><G JM?@M:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU<> OVNCFST;
JOU DPVOU;
XSJUF(")PX NBOZ OVNCFST BSF ZPV HPJOH UP FOUFS ");
SFBEG(" %T", &DPVOU);
// 3FBE UIF OVNCFST
GPSFBDI (J; 0 .. DPVOU) \
JOU OVNCFS;
XSJUF("/VNCFS ", J, " ");
SFBEG(" %T", &OVNCFS);
FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
152
OVNCFST _= OVNCFS;
^
// 1SJOU UIF OVNCFST
XSJUFMO("#FGPSF TPSUJOH:");
GPSFBDI (J, OVNCFS; OVNCFST) \
XSJUFGMO("%3E:%5E", J, OVNCFS);
^
OVNCFST.TPSU;
// 1SJOU UIF OVNCFST
XSJUFMO(""GUFS TPSUJOH:");
GPSFBDI (J, OVNCFS; OVNCFST) \
XSJUFGMO("%3E:%5E", J, OVNCFS);
^
^
0JH@ JA OC@ ?PKGD><O@? GDI@N JA >J?@ <M@ J=QDJPN DI OC<O KMJBM<H. 1C@ G<NO ORJ GPSFBDI GJJKN
OC<O <M@ PN@? AJM KMDIODIB OC@ IPH=@MN <M@ @S<>OGT OC@ N<H@. !@ADIDIB < API>ODJI OC<O HDBCO
<KKMJKMD<O@GT =@ I<H@? <N QSJOU() RJPG? M@HJQ@ OC<O ?PKGD><ODJI. 1C@ API>ODJI >JPG? O<F@ <
NGD>@ <N < K<M<H@O@M <I? KMDIO DO:
WPJE QSJOU(JOU<> TMJDF)
\
GPSFBDI (J, FMFNFOU; TMJDF) \
XSJUFGMO("%3T:%5T", J, FMFNFOU);
^
^
+JOD>@ OC<O OC@ I<H@ JA OC@ K<M<H@O@M DN ?@ADI@? OJ =@ HJM@ B@I@M<GGT <N TMJDF DINO@<? JA
OVNCFST. 1C@ M@<NJI AJM OC<O DN OC@ A<>O OC<O OC@ API>ODJI RJPG? IJO FIJR RC<O OC@ @G@H@ION
JA OC@ NGD>@ RJPG? NK@>DAD><GGT M@KM@N@IO. 1C<O ><I JIGT =@ FIJRI <O OC@ KG<>@ RC@M@ OC@
API>ODJI C<N =@@I ><GG@? AMJH. 1C@ @G@H@ION H<T =@ NOP?@IO &!N, K<MON JA < K<NNRJM?, @O>.
0DI>@ OC<O ><IIJO =@ FIJRI DI OC@ QSJOU() API>ODJI, B@I@M<G I<H@N GDF@ TMJDF <I? FMFNFOU
<M@ PN@? DI DON DHKG@H@IO<ODJI.
1C@ I@R API>ODJI ><I =@ ><GG@? AMJH OC@ ORJ KG<>@N RC@M@ OC@ NGD>@ I@@?N OJ =@ KMDIO@?:
JNQPSU TUE.TUEJP;
WPJE QSJOU(JOU<> TMJDF)
\
GPSFBDI (J, FMFNFOU; TMJDF) \
XSJUFGMO("%3T:%5T", J, FMFNFOU);
^
^
WPJE NBJO()
\
JOU<> OVNCFST;
JOU DPVOU;
XSJUF(")PX NBOZ OVNCFST BSF ZPV HPJOH UP FOUFS ");
SFBEG(" %T", &DPVOU);
// 3FBE UIF OVNCFST
GPSFBDI (J; 0 .. DPVOU) \
JOU OVNCFS;
XSJUF("/VNCFS ", J, " ");
SFBEG(" %T", &OVNCFS);
OVNCFST _= OVNCFS;
^
FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
153
// 1SJOU UIF OVNCFST
XSJUFMO("#FGPSF TPSUJOH:");
QSJOU(OVNCFST);
OVNCFST.TPSU;
// 1SJOU UIF OVNCFST
XSJUFMO(""GUFS TPSUJOH:");
QSJOU(OVNCFST);
^
1C@M@ DN HJM@ OJ ?J. +JOD>@ OC<O OC@M@ DN <GR<TN < ODOG@ GDI@ KMDIO@? MDBCO =@AJM@ KMDIODIB OC@
@G@H@ION JA OC@ NGD>@. AGOCJPBC OC@ ODOG@ DN ?DAA@M@IO, OC@ O<NF DN OC@ N<H@. &A KMDIODIB OC@ ODOG@
><I =@ N@@I <N < K<MO JA KMDIODIB OC@ NGD>@, OC@ ODOG@ OJJ ><I =@ K<NN@? <N < K<M<H@O@M. %@M@
<M@ OC@ I@R >C<IB@N:
WPJE QSJOU(TUSJOH UJUMF, JOU<> TMJDF)
\
XSJUFMO(UJUMF, ":");
GPSFBDI (J, FMFNFOU; TMJDF) \
XSJUFGMO("%3T:%5T", J, FMFNFOU);
^
^
// ...
// 1SJOU UIF OVNCFST
QSJOU("#FGPSF TPSUJOH", OVNCFST);
// ...
// 1SJOU UIF OVNCFST
QSJOU(""GUFS TPSUJOH", OVNCFST);
1CDN NO@K C<N OC@ <??@? =@I@ADO JA J=QD<ODIB OC@ >JHH@ION OC<O <KK@<M MDBCO =@AJM@ OC@ ORJ
QSJOU() ><GGN. 0DI>@ OC@ I<H@ JA OC@ API>ODJI <GM@<?T >G@<MGT >JHHPID><O@N RC<O DO ?J@N,
OCJN@ >JHH@ION <M@ PII@>@NN<MT:
QSJOU("#FGPSF TPSUJOH", OVNCFST);
OVNCFST.TPSU;
QSJOU(""GUFS TPSUJOH", OVNCFST);
AGOCJPBC NP=OG@, OC@M@ DN HJM@ >J?@ ?PKGD><ODJI DI OCDN KMJBM<H: 1C@ Q<GP@N JA DPVOU <I?
OVNCFS <M@ M@<? DI @S<>OGT OC@ N<H@ R<T. 1C@ JIGT ?DAA@M@I>@ DN OC@ H@NN<B@ OC<O DN KMDIO@? OJ
OC@ PN@M <I? OC@ I<H@ JA OC@ Q<MD<=G@:
JOU DPVOU;
XSJUF(")PX NBOZ OVNCFST BSF ZPV HPJOH UP FOUFS ");
SFBEG(" %T", &DPVOU);
// ...
JOU OVNCFS;
XSJUF("/VNCFS ", J, " ");
SFBEG(" %T", &OVNCFS);
1C@ >J?@ RJPG? =@>JH@ @Q@I =@OO@M DA DO OJJF <?Q<IO<B@ JA < I@R API>ODJI OC<O HDBCO =@
I<H@? <KKMJKMD<O@GT <N SFBE@JOU(). 1C@ I@R API>ODJI ><I O<F@ OC@ H@NN<B@ <N < K<M<H@O@M,
KMDIO OC<O H@NN<B@, M@<? <I JOU AMJH OC@ DIKPO, <I? M@OPMI OC<O JOU:
FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
154
JOU SFBE@JOU(TUSJOH NFTTBHF)
\
JOU SFTVMU;
XSJUF(NFTTBHF, " ");
SFBEG(" %T", &SFTVMU);
SFUVSO SFTVMU;
^
DPVOU ><I IJR =@ DIDOD<GDU@? ?DM@>OGT =T OC@ M@OPMI Q<GP@ JA < ><GG OJ OCDN I@R API>ODJI:
JOU DPVOU = SFBE@JOU(")PX NBOZ OVNCFST BSF ZPV HPJOH UP FOUFS");
OVNCFS ><IIJO =@ DIDOD<GDU@? DI <N NOM<DBCOAJMR<M? < R<T =@><PN@ OC@ GJJK >JPIO@M J C<KK@IN
OJ =@ < K<MO JA OC@ H@NN<B@ OC<O DN ?DNKG<T@? RC@I M@<?DIB OVNCFS. 1CDN ><I =@ JQ@M>JH@ =T
O<FDIB <?Q<IO<B@ JA GPSNBU:
JNQPSU TUE.TUSJOH;
// ...
JOU OVNCFS = SFBE@JOU(GPSNBU("/VNCFS %T", J));
#PMOC@M, NDI>@ OVNCFS DN PN@? DI JIGT JI@ KG<>@ DI OC@ GPSFBDI GJJK, DON ?@ADIDODJI ><I =@
@GDHDI<O@? <GOJB@OC@M <I? OC@ M@OPMI Q<GP@ JA SFBE@JOU() ><I ?DM@>OGT =@ PN@? DI DON KG<>@:
GPSFBDI (J; 0 .. DPVOU) \
OVNCFST _= SFBE@JOU(GPSNBU("/VNCFS %T", J));
^
)@O'N H<F@ < ADI<G HJ?DAD><ODJI OJ OCDN KMJBM<H =T HJQDIB OC@ GDI@N OC<O M@<? OC@ IPH=@MN OJ
< N@K<M<O@ API>ODJI. 1CDN RJPG? <GNJ @GDHDI<O@ OC@ I@@? AJM OC@ "/@<? OC@ IPH=@MN" >JHH@IO
=@><PN@ OC@ I<H@ JA OC@ I@R API>ODJI RJPG? <GM@<?T ><MMT OC<O DIAJMH<ODJI.
1C@ I@R SFBE/VNCFST() API>ODJI ?J@N IJO I@@? <IT K<M<H@O@M OJ >JHKG@O@ DON O<NF. &O M@<?N
NJH@ IPH=@MN <I? M@OPMIN OC@H <N < NGD>@. 1C@ AJGGJRDIB DN OC@ ADI<G Q@MNDJI JA OC@ KMJBM<H:
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
WPJE QSJOU(TUSJOH UJUMF, JOU<> TMJDF)
\
XSJUFMO(UJUMF, ":");
GPSFBDI (J, FMFNFOU; TMJDF) \
XSJUFGMO("%3T:%5T", J, FMFNFOU);
^
^
JOU SFBE@JOU(TUSJOH NFTTBHF)
\
JOU SFTVMU;
XSJUF(NFTTBHF, " ");
SFBEG(" %T", &SFTVMU);
SFUVSO SFTVMU;
^
JOU<> SFBE/VNCFST()
\
JOU<> SFTVMU;
JOU DPVOU = SFBE@JOU(")PX NBOZ OVNCFST BSF ZPV HPJOH UP FOUFS");
GPSFBDI (J; 0 .. DPVOU) \
SFTVMU _= SFBE@JOU(GPSNBU("/VNCFS %T", J));
^
FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
155
SFUVSO SFTVMU;
^
WPJE NBJO()
\
JOU<> OVNCFST = SFBE/VNCFST();
QSJOU("#FGPSF TPSUJOH", OVNCFST);
OVNCFST.TPSU;
QSJOU(""GUFS TPSUJOH", OVNCFST);
^
JHK<M@ OCDN Q@MNDJI JA OC@ KMJBM<H OJ OC@ ADMNO JI@. 1C@ H<EJM NO@KN JA OC@ KMJBM<H <M@
Q@MT >G@<M DI OC@ NBJO() API>ODJI JA OC@ I@R KMJBM<H. &I >JIOM<NO, OC@ NBJO() API>ODJI JA OC@
ADMNO KMJBM<H C<? OJ =@ @S<HDI@? OJ PI?@MNO<I? OC@ KPMKJN@ JA OC<O KMJBM<H.
AGOCJPBC OC@ OJO<G IPH=@MN JA IJIOMDQD<G GDI@N JA OC@ ORJ Q@MNDJIN JA OC@ KMJBM<H @I?@? PK
=@DIB @LP<G, API>ODJIN H<F@ KMJBM<HN NCJMO@M DI B@I@M<G. 1CDN @AA@>O DN IJO <KK<M@IO DI OCDN
NDHKG@ KMJBM<H. #JM @S<HKG@, =@AJM@ OC@ SFBE@JOU() API>ODJI C<N =@@I ?@ADI@?, M@<?DIB <I
JOU AMJH OC@ DIKPO DIQJGQ@? OCM@@ GDI@N JA >J?@. AAO@M OC@ ?@ADIDODJI JA SFBE@JOU(), OC@ N<H@
BJ<G DN <>CD@Q@? =T < NDIBG@ GDI@ JA >J?@. #PMOC@M, OC@ ?@ADIDODJI JA SFBE@JOU() <GGJR@?
M@HJQDIB OC@ ?@ADIDODJI JA OC@ Q<MD<=G@ OVNCFS <GOJB@OC@M.
Commented lines of code as functions 35.8.2
0JH@ODH@N OC@ I@@? OJ RMDO@ < >JHH@IO OJ ?@N>MD=@ OC@ KPMKJN@ JA < BMJPK JA GDI@N JA >J?@ DN
<I DI?D><ODJI OC<O OCJN@ GDI@N >JPG? =@OO@M =@ HJQ@? OJ < I@RGT ?@ADI@? API>ODJI. &A OC@ I<H@
JA OC@ API>ODJI DN ?@N>MDKODQ@ @IJPBC OC@I OC@M@ RJPG? =@ IJ I@@? AJM OC@ >JHH@IO @DOC@M.
1C@ OCM@@ >JHH@IO@? BMJPKN JA GDI@N JA OC@ ADMNO Q@MNDJI JA OC@ KMJBM<H C<Q@ =@@I PN@? AJM
?@ADIDIB I@R API>ODJIN OC<O <>CD@Q@? OC@ N<H@ O<NFN.
AIJOC@M DHKJMO<IO =@I@ADO JA M@HJQDIB >JHH@IO GDI@N DN OC<O >JHH@ION O@I? OJ =@>JH@
JPO?<O@? <N OC@ >J?@ B@ON HJ?DAD@? JQ@M OC@ ODH@. JHH@ION <M@ NJH@ODH@N AJMBJOO@I OJ =@
PK?<O@? <GJIB RDOC OC@ >J?@ <I? =@>JH@ @DOC@M PN@G@NN JM @Q@I RJMN@, HDNG@<?DIB. #JM OC<O
M@<NJI, DO DN =@I@AD>D<G OJ OMT OJ RMDO@ KMJBM<HN RDOCJPO OC@ I@@? AJM >JHH@ION.
Exercises
35.9
1. *J?DAT OC@ QSJOU.FOV() API>ODJI OJ O<F@ OC@ @IODM@ N@O JA H@IP DO@HN <N < K<M<H@O@M. #JM
@S<HKG@, OC@ H@IP DO@HN ><I =@ K<NN@? OJ OC@ API>ODJI <N DI OC@ AJGGJRDIB >J?@:
TUSJOH<> JUFNT =
< "#MBDL", "3FE", "(SFFO", "#MVF", "8IJUF" >;
QSJOU.FOV(JUFNT, 1);
%<Q@ OC@ KMJBM<H KMJ?P>@ OC@ AJGGJRDIB JPOKPO:
1 #MBDL
2 3FE
3 (SFFO
4 #MVF
5 8IJUF
2. 1C@ AJGGJRDIB KMJBM<H PN@N < ORJ ?DH@INDJI<G <MM<T <N < ><IQ<N. 0O<MO RDOC OC<O KMJBM<H
<I? DHKMJQ@ DO =T <??DIB HJM@ API>ODJI<GDOT OJ DO:
FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
156
JNQPSU TUE.TUEJP;
FOVN UPUBM-JOFT = 20;
FOVN UPUBM$PMVNOT = 60;
/*
* 5IF 'BMJBT' JO UIF OFYU MJOF NBLFT '-JOF' BO BMJBT PG
* EDIBS<UPUBM$PMVNOT>. &WFSZ '-JOF' UIBU JT VTFE JO UIF SFTU
* PG UIF QSPHSBN XJMM NFBO EDIBS<UPUBM$PMVNOT> GSPN UIJT
* QPJOU PO.
*
* "MTP OPUF UIBU '-JOF' JT B GJYFE-MFOHUI BSSBZ.
*/
BMJBT -JOF = EDIBS<UPUBM$PMVNOT>;
/*
* " EZOBNJD BSSBZ PG -JOFT JT CFJOH BMJBTFE BT '$BOWBT'.
*/
BMJBT $BOWBT = -JOF<>;
/*
* 1SJOUT UIF DBOWBT MJOF CZ MJOF.
*/
WPJE QSJOU($BOWBT DBOWBT)
\
GPSFBDI (MJOF; DBOWBT) \
XSJUFMO(MJOF);
^
^
/*
* 1MBDFT B EPU BU UIF TQFDJGJFE MPDBUJPO PO UIF DBOWBT. *O B
* TFOTF, "QBJOUT" UIF DBOWBT.
*/
WPJE QVU%PU($BOWBT DBOWBT, JOU MJOF, JOU DPMVNO)
\
DBOWBT<MJOF><DPMVNO> = '#';
^
/*
* %SBXT B WFSUJDBM MJOF PG UIF TQFDJGJFE MFOHUI GSPN UIF
* TQFDJGJFE QPTJUJPO.
*/
WPJE ESBX7FSUJDBM-JOF($BOWBT DBOWBT,
JOU MJOF,
JOU DPMVNO,
JOU MFOHUI)
\
GPSFBDI (MJOF5P1BJOU; MJOF .. MJOF + MFOHUI) \
QVU%PU(DBOWBT, MJOF5P1BJOU, DPMVNO);
^
^
WPJE NBJO()
\
-JOF FNQUZ-JOF = '.';
/* "O FNQUZ DBOWBT */
$BOWBT DBOWBT;
/* $POTUSVDUJOH UIF DBOWBT CZ BEEJOH FNQUZ MJOFT */
GPSFBDI (J; 0 .. UPUBM-JOFT) \
DBOWBT _= FNQUZ-JOF;
^
/* 6TJOH UIF DBOWBT */
QVU%PU(DBOWBT, 7, 30);
ESBX7FSUJDBM-JOF(DBOWBT, 5, 10, 4);
QSJOU(DBOWBT);
^
FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
157
... F:7 EA>GF;A@E
FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
158
Immutability 36
4@ C<Q@ N@@I OC<O Q<MD<=G@N M@KM@N@IO >JI>@KON DI KMJBM<HN. 1C@ DIO@M<>ODJIN JA OC@N@
>JI>@KON <M@ <>CD@Q@? =T @SKM@NNDJIN OC<O >C<IB@ OC@ Q<GP@N JA OCJN@ Q<MD<=G@N:
// 1BZ UIF CJMM
UPUBM1SJDF = DBMDVMBUF"NPVOU(JUFN1SJDFT);
NPOFZ*O8BMMFU -= UPUBM1SJDF;
NPOFZ"U.FSDIBOU += UPUBM1SJDF;
*J?DATDIB < Q<MD<=G@ DN ><GG@? ?GF3F;@9 OC<O Q<MD<=G@. 1C@ >JI>@KO JA HPO<=DGDOT DN @NN@IOD<G
AJM HJNO O<NFN. %JR@Q@M, OC@M@ <M@ NJH@ ><N@N RC@M@ HPO<=DGDOT DN IJO NPDO<=G@:
t 0JH@ >JI>@KON <M@ DHHPO<=G@ =T ?@ADIDODJI. #JM @S<HKG@, OC@M@ <M@ <GR<TN N@Q@I ?<TN DI <
R@@F, OC@ H<OC >JINO<IO B; (h) DN >JINO<IO, < KMJBM<H H<T =@ NPKKJMODIB JIGT < NCJMO GDNO
JA CPH<I G<IBP<B@N (@.B. JIGT "IBGDNC <I? 1PMFDNC), @O>.
t &A @Q@MT Q<MD<=G@ R@M@ HJ?DAD<=G@ <N R@ C<Q@ N@@I NJ A<M, OC@I @Q@MT KD@>@ JA >J?@ OC<O
PN@? OC<O Q<MD<=G@ >JPG? KJO@IOD<GGT C<Q@ HJ?DAD@? DO. "Q@I DA OC@M@ R<N IJ M@<NJI OJ
HJ?DAT < Q<MD<=G@ DI <I JK@M<ODJI, OC@M@ RJPG? =@ IJ BP<M<IO@@ OC<O OCDN RJPG? IJO C<KK@I
@DOC@M. -MJBM<HN <M@ ?DAAD>PGO OJ M@<? JM H<DIO<DI RC@I OC@M@ DN IJ BP<M<IO@@ JA
DHHPO<=DGDOT.
#JM @S<HKG@, DO H<T =@ >G@<M OC<O OC@ API>ODJI ><GG SFUJSF(PGGJDF, XPSLFS) RJPG?
M@ODM@ < RJMF@M JA <I JAAD>@. &A @Q@MT Q<MD<=G@ R@M@ HPO<=G@, DO RJPG? IJO =@ >G@<M
RCD>C JA OC@ ORJ Q<MD<=G@N RJPG? =@ HJ?DAD@? <AO@M OC<O API>ODJI ><GG. &O H<T =@
@SK@>O@? OC<O OC@ IPH=@M JA <>ODQ@ @HKGJT@@N JA PGGJDF RJPG? =@ ?@>M@<N@?, =PO
RJPG? OC@ API>ODJI ><GG <GNJ HJ?DAT XPSLFS DI NJH@ R<T?
1C@ >JI>@KO JA DHHPO<=DGDOT C@GKN RDOC PI?@MNO<I?DIB K<MON JA KMJBM<HN =T BP<M<IO@@DIB
OC<O >@MO<DI JK@M<ODJIN ?J IJO >C<IB@ >@MO<DI Q<MD<=G@N. &O <GNJ M@?P>@N OC@ MDNF JA >@MO<DI
OTK@N JA KMJBM<H @MMJMN.
1C@ ;??GF34;>;FK >JI>@KO JA ! DN M@KM@N@IO@? =T OC@ DPOTU <I? JNNVUBCMF F@TRJM?N.
AGOCJPBC OC@ ORJ RJM?N OC@HN@GQ@N <M@ >GJN@ DI H@<IDIB, OC@DM M@NKJIND=DGDOD@N DI KMJBM<HN
<M@ ?DAA@M@IO <I? OC@T <M@ NJH@ODH@N DI>JHK<OD=G@.
Immutable variables
36.1
BJOC JA OC@ O@MHN "DHHPO<=G@ Q<MD<=G@" <I? ">JINO<IO Q<MD<=G@" <M@ IJIN@IND><G RC@I OC@
RJM? "Q<MD<=G@" DN O<F@I GDO@M<GGT OJ H@<I EA?7F:;@9 F:3F 5:3@97E. 1C@ RJM? "Q<MD<=G@" H@<IN
<IT >JI>@KO JA < KMJBM<H RCD>C H<T =@ HPO<=G@ JA DHHPO<=G@.
1C@M@ <M@ OCM@@ R<TN JA ?@ADIDIB Q<MD<=G@N OC<O ><I I@Q@M =@ HPO<O@?.
FOVN constants 36.1.1
4@ C<Q@ N@@I @<MGD@M DI OC@ FOVN >C<KO@M OC<O FOVN ?@ADI@N I<H@? >JINO<IO Q<GP@N:
FOVN GJMF/BNF = "MJTU.UYU";
AN GJIB <N OC@DM Q<GP@N ><I =@ ?@O@MHDI@? <O >JHKDG@ ODH@, FOVN Q<MD<=G@N ><I =@ DIDOD<GDU@?
=T M@OPMI Q<GP@N JA API>ODJIN <N R@GG:
JOU UPUBM-JOFT()
\
SFUVSO 42;
^
!??GF34;>;FK
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
159
JOU UPUBM$PMVNOT()
\
SFUVSO 7;
^
TUSJOH OBNF()
\
SFUVSO "MJTU";
^
WPJE NBJO()
\
FOVN GJMF/BNF = OBNF() _ ".UYU";
FOVN UPUBM4RVBSFT = UPUBM-JOFT() * UPUBM$PMVNOT();
^
AN @SK@>O@?, Q<GP@N JA FOVN >JINO<ION ><IIJO =@ HJ?DAD@?:
++UPUBM4RVBSFT; // j DPNQJMBUJPO &3303
AGOCJPBC DO DN < Q@MT @AA@>ODQ@ R<T JA M@KM@N@IODIB DHHPO<=G@ Q<GP@N, FOVN ><I JIGT =@ PN@?
AJM >JHKDG@-ODH@ Q<GP@N.
AI FOVN >JINO<IO DN 3 ?3@;87EF 5A@EF3@F, H@<IDIB OC<O OC@ KMJBM<H DN >JHKDG@? <N DA DO C<N
=@@I M@KG<>@? =T DON Q<GP@. AN <I @S<HKG@, G@O'N >JIND?@M OC@ AJGGJRDIB FOVN ?@ADIDODJI <I?
OC@ ORJ @SKM@NNDJIN OC<O H<F@ PN@ JA DO:
FOVN J = 42;
XSJUFMO(J);
GPP(J);
1C@ >J?@ <=JQ@ RJPG? =@ OC@ N<H@ <N M@KG<>DIB J RDOC DON Q<GP@ 42:
XSJUFMO(42);
GPP(42);
AGOCJPBC OC<O M@KG<>@H@IO H<F@N N@IN@ AJM NDHKG@ OTK@N GDF@ JOU <I? H<F@N IJ ?DAA@M@I>@ DI
OC@ KMJBM<H, FOVN >JINO<ION =MDIB < CD??@I >JNO RC@I OC@T <M@ PN@? AJM <MM<TN JM <NNJ>D<ODQ@
<MM<TN:
FOVN B = < 42, 100 >;
XSJUFMO(B);
GPP(B);
AAO@M M@KG<>DIB B RDOC DON Q<GP@, OC@ @LPDQ<G@IO >J?@ OC<O OC@ >JHKDG@M RJPG? =@ >JHKDGDIB DN
OC@ AJGGJRDIB:
XSJUFMO(< 42, 100 >); // BO BSSBZ JT DSFBUFE BU SVO UJNF
GPP(< 42, 100 >); // BOPUIFS BSSBZ JT DSFBUFE BU SVO UJNF
1C@ CD??@I >JNO C@M@ DN OC<O OC@M@ RJPG? =@ ORJ N@K<M<O@ <MM<TN >M@<O@? AJM OC@ ORJ
@SKM@NNDJIN <=JQ@. #JM OC<O M@<NJI, DO H<T H<F@ HJM@ N@IN@ OJ ?@ADI@ <MM<TN <I? <NNJ>D<ODQ@
<MM<TN <N JNNVUBCMF Q<MD<=G@N DA OC@T <M@ BJDIB OJ =@ PN@? HJM@ OC<I JI>@ DI OC@ KMJBM<H.
!??GF34;>;FK
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
160
JNNVUBCMF variables 36.1.2
)DF@ FOVN, OCDN F@TRJM? NK@>DAD@N OC<O OC@ Q<GP@ JA < Q<MD<=G@ RDGG I@Q@M >C<IB@. &ON ?DAA@M@I>@
AMJH FOVN DN OC<O OC@ Q<GP@N JA JNNVUBCMF Q<MD<=G@N ><I =@ ><G>PG<O@? ?PMDIB OC@ @S@>PODJI JA
OC@ KMJBM<H.
1C@ AJGGJRDIB KMJBM<H >JHK<M@N OC@ PN@N JA FOVN <I? JNNVUBCMF. 1C@ KMJBM<H R<DON AJM OC@
PN@M OJ BP@NN < IPH=@M OC<O C<N =@@I KD>F@? M<I?JHGT. 0DI>@ OC@ M<I?JH IPH=@M ><IIJO =@
?@O@MHDI@? <O >JHKDG@ ODH@, DO ><IIJO =@ ?@ADI@? <N FOVN. 0ODGG, NDI>@ OC@ M<I?JHGT KD>F@?
Q<GP@ HPNO I@Q@M =@ >C<IB@? <AO@M C<QDIB =@DIB ?@>D?@?, DO DN NPDO<=G@ OJ NK@>DAT OC<O Q<MD<=G@
<N JNNVUBCMF.
1C@ KMJBM<H O<F@N <?Q<IO<B@ JA OC@ SFBE@JOU() API>ODJI OC<O C<N =@@I ?@ADI@? DI OC@
KM@QDJPN >C<KO@M:
JNQPSU TUE.TUEJP;
JNQPSU TUE.SBOEPN;
JOU SFBE@JOU(TUSJOH NFTTBHF)
\
JOU SFTVMU;
XSJUF(NFTTBHF, " ");
SFBEG(" %T", &SFTVMU);
SFUVSO SFTVMU;
^
WPJE NBJO()
\
FOVN NJO = 1;
FOVN NBY = 10;
JNNVUBCMF OVNCFS = VOJGPSN(NJO, NBY + 1);
XSJUFGMO("* BN UIJOLJOH PG B OVNCFS CFUXFFO %T BOE %T.",
NJO, NBY);
BVUP JT$PSSFDU = GBMTF;
XIJMF (!JT$PSSFDU) \
JNNVUBCMF HVFTT = SFBE@JOU("8IBU JT ZPVS HVFTT");
JT$PSSFDU = (HVFTT == OVNCFS);
^
XSJUFMO("$PSSFDU!");
^
,=N@MQ<ODJIN:
t NJO <I? NBY <M@ DIO@BM<G K<MON JA OC@ =@C<QDJM JA OCDN KMJBM<H <I? OC@DM Q<GP@N <M@ FIJRI
<O >JHKDG@ ODH@. #JM OC<O M@<NJI OC@T <M@ ?@ADI@? <N FOVN >JINO<ION.
t OVNCFS DN NK@>DAD@? <N JNNVUBCMF =@><PN@ DO RJPG? IJO =@ <KKMJKMD<O@ OJ HJ?DAT DO ?PMDIB
OC@ @S@>PODJI JA OC@ KMJBM<H. &O DN OC@ N<H@ RDOC @<>C PN@M BP@NN: ,I>@ M@<?, OC@ BP@NN
NCJPG? IJO =@ HJ?DAD@?.
t ,=N@MQ@ OC<O OC@ OTK@N JA OCJN@ Q<MD<=G@N <M@ IJO NK@>DAD@? @SKGD>DOGT. AN RDOC BVUP, OC@
OTK@N JA FOVN <I? JNNVUBCMF Q<MD<=G@N ><I =@ DIA@MM@? AMJH OC@ @SKM@NNDJI JI OC@ MDBCO
C<I? ND?@.
AGOCJPBC DO DN IJO I@>@NN<MT OJ RMDO@ OC@ OTK@ APGGT <N @.B. JNNVUBCMF(JOU), JNNVUBCMF
IJMH<GGT O<F@N OC@ <>OP<G OTK@ RDOCDI K<M@IOC@N@N. 1C@ JPOKPO JA OC@ AJGGJRDIB KMJBM<H
?@HJINOM<O@N OC<O OC@ APGG I<H@N JA OC@ OTK@N JA OC@ OCM@@ Q<MD<=G@N <M@ DI A<>O OC@ N<H@:
JNQPSU TUE.TUEJP;
WPJE NBJO()
!??GF34;>;FK
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
161
\
JNNVUBCMF JOGFSSFE5ZQF = 0;
JNNVUBCMF JOU FYQMJDJU5ZQF = 1;
JNNVUBCMF(JOU) XIPMF5ZQF = 2;
XSJUFMO(UZQFPG(JOGFSSFE5ZQF).TUSJOHPG);
XSJUFMO(UZQFPG(FYQMJDJU5ZQF).TUSJOHPG);
XSJUFMO(UZQFPG(XIPMF5ZQF).TUSJOHPG);
^
1C@ <>OP<G I<H@ JA OC@ OTK@ DI>GP?@N JNNVUBCMF:
JNNVUBCMF(JOU)
JNNVUBCMF(JOU)
JNNVUBCMF(JOU)
1C@ OTK@ OC<O DN NK@>DAD@? RDOCDI OC@ K<M@IOC@N@N C<N NDBIDAD><I>@. 4@ RDGG N@@ OCDN =@GJR
RC@I ?DN>PNNDIB OC@ DHHPO<=DGDOT JA OC@ RCJG@ NGD>@ QN. DON @G@H@ION.
DPOTU variables 36.1.3
1CDN F@TRJM? C<N OC@ N<H@ @AA@>O <N JNNVUBCMF JI Q<MD<=G@N. DPOTU Q<MD<=G@N ><IIJO =@
HJ?DAD@?:
DPOTU IBMG = UPUBM / 2;
IBMG = 10; // j DPNQJMBUJPO &3303
& M@>JHH@I? OC<O TJP KM@A@M JNNVUBCMF JQ@M DPOTU RC@I ?@ADIDIB Q<MD<=G@N. 1C@ M@<NJI DN
OC<O JNNVUBCMF Q<MD<=G@N ><I =@ K<NN@? OJ API>ODJIN <N OC@DM JNNVUBCMF K<M<H@O@MN. 4@ RDGG
N@@ OCDN =@GJR.
Immutable parameters
36.2
&O DN KJNND=G@ AJM API>ODJIN OJ KMJHDN@ OC<O OC@T ?J IJO HJ?DAT >@MO<DI K<M<H@O@MN OC<O OC@T
O<F@. 1C@ >JHKDG@M BP<M<IO@@N OC<O OCDN KMJHDN@ DN @IAJM>@?. B@AJM@ N@@DIB CJR OCDN DN
<>CD@Q@?, G@O'N ADMNO N@@ OC<O API>ODJIN ><I DI?@@? HJ?DAT OC@ @G@H@ION JA NGD>@N OC<O <M@
K<NN@? <N <MBPH@ION OJ OCJN@ API>ODJI.
AN TJP RJPG? M@H@H=@M AMJH OC@ 0GD>@N <I? ,OC@M AMM<T #@<OPM@N >C<KO@M, NGD>@N ?J IJO JRI
@G@H@ION =PO KMJQD?@ <>>@NN OJ OC@H. 1C@M@ H<T =@ HJM@ OC<I JI@ NGD>@ <O < BDQ@I ODH@ OC<O
KMJQD?@ <>>@NN OJ OC@ N<H@ @G@H@ION.
AGOCJPBC OC@ @S<HKG@N DI OCDN N@>ODJI AJ>PN JIGT JI NGD>@N, OCDN OJKD> DN <KKGD><=G@ OJ
<NNJ>D<ODQ@ <MM<TN <I? >G<NN@N <N R@GG =@><PN@ OC@T OJJ <M@ D787D7@57 FKB7E.
A NGD>@ OC<O DN K<NN@? <N < API>ODJI <MBPH@IO DN IJO OC@ NGD>@ OC<O OC@ API>ODJI DN ><GG@? RDOC.
1C@ <MBPH@IO DN < >JKT JA OC@ <>OP<G NGD>@:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU<> TMJDF = < 10, 20, 30, 40 >; // 1
IBMWF(TMJDF);
XSJUFMO(TMJDF);
^
WPJE IBMWF(JOU<> OVNCFST) // 2
\
!??GF34;>;FK
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
162
GPSFBDI (SFG OVNCFS; OVNCFST) \
OVNCFS /= 2;
^
^
4C@I KMJBM<H @S@>PODJI @IO@MN OC@ IBMWF() API>ODJI, OC@M@ <M@ ORJ NGD>@N OC<O KMJQD?@
<>>@NN OJ OC@ N<H@ AJPM @G@H@ION:
1. 1C@ NGD>@ I<H@? TMJDF OC<O DN ?@ADI@? DI NBJO(), RCD>C DN K<NN@? OJ IBMWF() <N DON
<MBPH@IO
2. 1C@ NGD>@ I<H@? OVNCFST OC<O IBMWF() M@>@DQ@N <N DON <MBPH@IO, RCD>C KMJQD?@N <>>@NN OJ
OC@ N<H@ @G@H@ION <N TMJDF
AGNJ ?P@ OJ OC@ SFG F@TRJM? DI OC@ GPSFBDI GJJK, OC@ Q<GP@N JA OC@ JMDBDI<G (<I? OC@ JIGT)
@G@H@ION B@O C<GQ@?:
<5, 10, 15, 20>
&O DN PN@APG AJM API>ODJIN OJ =@ <=G@ OJ HJ?DAT OC@ @G@H@ION JA OC@ NGD>@N OC<O <M@ K<NN@? <N
<MBPH@ION. 0JH@ API>ODJIN @SDNO EPNO AJM OC<O KPMKJN@ <N C<N =@@I N@@I DI OCDN @S<HKG@.
1C@ >JHKDG@M ?J@N IJO <GGJR K<NNDIB JNNVUBCMF Q<MD<=G@N <N <MBPH@ION OJ NP>C API>ODJIN,
=@><PN@ DO DN DHKJNND=G@ OJ HJ?DAT <I DHHPO<=G@ Q<MD<=G@:
JNNVUBCMF JOU<> TMJDF = < 10, 20, 30, 40 >;
IBMWF(TMJDF); // j DPNQJMBUJPO &3303
1C@ >JHKDG<ODJI @MMJM DI?D><O@N OC<O < Q<MD<=G@ JA OTK@ JNNVUBCMF(JOU<>) ><IIJO =@ PN@? <N
<I <MBPH@IO JA OTK@ JOU<>:
&SSPS: GVODUJPO EFOFNF.IBMWF (JOU<> OVNCFST) JT OPU DBMMBCMF
VTJOH BSHVNFOU UZQFT (JNNVUBCMF(JOU<>))
DPOTU parameters 36.2.1
&O DN DHKJMO<IO <I? I<OPM<G OC<O JNNVUBCMF Q<MD<=G@N =@ KM@Q@IO@? AMJH =@DIB K<NN@? OJ
API>ODJIN GDF@ IBMWF(), RCD>C HJ?DAT OC@DM <MBPH@ION. %JR@Q@M, DO RJPG? =@ < GDHDO<ODJI DA
OC@T >JPG? IJO =@ K<NN@? OJ API>ODJIN OC<O ?J IJO HJ?DAT OC@DM <MBPH@ION DI <IT R<T:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JNNVUBCMF JOU<> TMJDF = < 10, 20, 30, 40 >;
QSJOU(TMJDF); // j DPNQJMBUJPO &3303
^
WPJE QSJOU(JOU<> TMJDF)
\
XSJUFGMO("%T FMFNFOUT: ", TMJDF.MFOHUI);
GPSFBDI (J, FMFNFOU; TMJDF) \
XSJUFGMO("%T: %T", J, FMFNFOU);
^
^
&O ?J@N IJO H<F@ N@IN@ <=JQ@ OC<O < NGD>@ DN KM@Q@IO@? AMJH =@DIB KMDIO@? EPNO =@><PN@ DO DN
JNNVUBCMF. 1C@ KMJK@M R<T JA ?@<GDIB RDOC OCDN NDOP<ODJI DN DPOTU K<M<H@O@MN.
!??GF34;>;FK
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
163
1C@ DPOTU F@TRJM? NK@>DAD@N OC<O < Q<MD<=G@ RJPG? IJO =@ HJ?DAD@? OCMJPBC F:3F B3DF;5G>3D
D787D7@57 (@.B. < NGD>@) JA OC<O Q<MD<=G@. 0K@>DATDIB < K<M<H@O@M <N DPOTU BP<M<IO@@N OC<O OC@
@G@H@ION JA OC@ NGD>@ RJPG? IJO =@ HJ?DAD@? DIND?@ OC@ API>ODJI. ,I>@ QSJOU() KMJQD?@N OCDN
BP<M<IO@@, OC@ KMJBM<H ><I IJR =@ >JHKDG@?:
QSJOU(TMJDF); // OPX DPNQJMFT
// ...
WPJE QSJOU(DPOTU JOU<> TMJDF)
1CDN BP<M<IO@@ <GGJRN K<NNDIB =JOC HPO<=G@ <I? JNNVUBCMF Q<MD<=G@N <N <MBPH@ION:
JNNVUBCMF JOU<> TMJDF = < 10, 20, 30, 40 >;
QSJOU(TMJDF); // DPNQJMFT
JOU<> NVUBCMF4MJDF = < 7, 8 >;
QSJOU(NVUBCMF4MJDF); // DPNQJMFT
A K<M<H@O@M OC<O DN IJO HJ?DAD@? DI < API>ODJI =PO DN IJO NK@>DAD@? <N DPOTU M@?P>@N OC@
PN<=DGDOT JA OC<O API>ODJI. A??DODJI<GGT, DPOTU K<M<H@O@MN KMJQD?@ PN@APG DIAJMH<ODJI OJ OC@
KMJBM<HH@M. (IJRDIB OC<O < Q<MD<=G@ RDGG IJO =@ HJ?DAD@? RC@I K<NN@? OJ < API>ODJI H<F@N
OC@ >J?@ @<ND@M OJ PI?@MNO<I?. &O <GNJ KM@Q@ION KJO@IOD<G @MMJMN =@><PN@ OC@ >JHKDG@M
KM@Q@ION HJ?DAD><ODJIN OJ DPOTU K<M<H@O@MN:
WPJE QSJOU(DPOTU JOU<> TMJDF)
\
TMJDF<0> = 42; // j DPNQJMBUJPO &3303
1C@ KMJBM<HH@M RJPG? @DOC@M M@<GDU@ OC@ HDNO<F@ DI OC@ API>ODJI JM RJPG? M@OCDIF OC@ ?@NDBI
<I? K@MC<KN M@HJQ@ OC@ DPOTU NK@>DAD@M.
1C@ A<>O OC<O DPOTU K<M<H@O@MN ><I <>>@KO =JOC HPO<=G@ <I? JNNVUBCMF Q<MD<=G@N C<N <I
DIO@M@NODIB >JIN@LP@I>@. 1CDN DN @SKG<DI@? DI OC@ "0CJPG? < K<M<H@O@M =@ DPOTU JM
JNNVUBCMF?" N@>ODJI =@GJR.
JNNVUBCMF parameters 36.2.2
AN R@ C<Q@ N@@I <=JQ@, =JOC HPO<=G@ <I? JNNVUBCMF Q<MD<=G@N ><I =@ K<NN@? OJ API>ODJIN <N
OC@DM DPOTU K<M<H@O@MN. &I < R<T, DPOTU K<M<H@O@MN <M@ R@G>JHDIB.
&I >JIOM<NO, JNNVUBCMF K<M<H@O@MN =MDIB < NOMJIB M@LPDM@H@IO: ,IGT JNNVUBCMF Q<MD<=G@N
><I =@ K<NN@? OJ API>ODJIN <N OC@DM JNNVUBCMF K<M<H@O@MN:
WPJE GVOD(JNNVUBCMF JOU<> TMJDF)
\
/* ... */
^
WPJE NBJO()
\
JNNVUBCMF JOU<> JNN4MJDF = < 1, 2 >;
JOU<> TMJDF = < 8, 9 >;
GVOD(JNN4MJDF); // DPNQJMFT
GVOD(TMJDF); // j DPNQJMBUJPO &3303
^
#JM OC<O M@<NJI, OC@ JNNVUBCMF NK@>DAD@M NCJPG? =@ PN@? JIGT RC@I OCDN M@LPDM@H@IO DN
I@>@NN<MT. 4@ C<Q@ DI?@@? =@@I PNDIB OC@ JNNVUBCMF NK@>DAD@M DI?DM@>OGT OCMJPBC >@MO<DI
NOMDIB OTK@N. 1CDN RDGG =@ >JQ@M@? =@GJR.
!??GF34;>;FK
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
164
4@ C<Q@ N@@I OC<O OC@ K<M<H@O@MN OC<O <M@ NK@>DAD@? <N DPOTU JM JNNVUBCMF KMJHDN@ IJO OJ
HJ?DAT F:7 35FG3> H3D;34>7 OC<O DN K<NN@? <N <I <MBPH@IO. 1CDN DN M@G@Q<IO JIGT AJM M@A@M@I>@
OTK@N =@><PN@ JIGT OC@I OC@M@ DN F:7 35FG3> H3D;34>7 OJ O<GF <=JPO OC@ DHHPO<=DGDOT JA.
'787D7@57 FKB7E <I? H3>G7 FKB7E RDGG =@ >JQ@M@? DI G<O@M >C<KO@MN. AHJIB OC@ OTK@N OC<O R@
C<Q@ N@@I NJ A<M, JIGT NGD>@N <I? <NNJ>D<ODQ@ <MM<TN <M@ M@A@M@I>@ OTK@N; OC@ JOC@MN <M@ Q<GP@
OTK@N.
Should a parameter be DPOTU or JNNVUBCMF? 36.2.3
1C@ ORJ N@>ODJIN <=JQ@ H<T BDQ@ OC@ DHKM@NNDJI OC<O =@DIB HJM@ AG@SD=G@, DPOTU K<M<H@O@MN
NCJPG? =@ KM@A@MM@? JQ@M JNNVUBCMF K<M<H@O@MN. 1CDN DN IJO <GR<TN OMP@.
DPOTU 7D3E7E OC@ DIAJMH<ODJI <=JPO RC@OC@M OC@ JMDBDI<G Q<MD<=G@ DN HPO<=G@ JM JNNVUBCMF.
1CDN DIAJMH<ODJI DN CD??@I @Q@I AMJH OC@ >JHKDG@M.
A >JIN@LP@I>@ JA OCDN A<>O DN OC<O DPOTU K<M<H@O@MN ><IIJO =@ K<NN@? <N <MBPH@ION OJ
API>ODJIN OC<O O<F@ JNNVUBCMF K<M<H@O@MN. #JM @S<HKG@, GPP() =@GJR ><IIJO K<NN DON DPOTU
K<M<H@O@M OJ CBS():
WPJE NBJO()
\
/* 5IF PSJHJOBM WBSJBCMF JT JNNVUBCMF */
JNNVUBCMF JOU<> TMJDF = < 10, 20, 30, 40 >;
GPP(TMJDF);
^
/* " GVODUJPO UIBU UBLFT JUT QBSBNFUFS BT DPOTU, JO PSEFS UP
* CF NPSF VTFGVM. */
WPJE GPP(DPOTU JOU<> TMJDF)
\
CBS(TMJDF); // j DPNQJMBUJPO &3303
^
/* " GVODUJPO UIBU UBLFT JUT QBSBNFUFS BT JNNVUBCMF GPS B
* QMBVTJCMF SFBTPO. */
WPJE CBS(JNNVUBCMF JOU<> TMJDF)
\
/* ... */
^
CBS() M@LPDM@N OC<O OC@ K<M<H@O@M DN JNNVUBCMF. %JR@Q@M, DO DN IJO FIJRI RC@OC@M OC@
JMDBDI<G Q<MD<=G@ OC<O GPP()'N DPOTU K<M<H@O@M M@A@M@I>@N DN JNNVUBCMF JM IJO.
Note: !F ;E 5>73D ;@ F:7 5A67 34AH7 F:3F F:7 AD;9;@3> H3D;34>7 ;@ main() ;E immutable.
AI7H7D, F:7 5A?B;>7D 5A?B;>7E 8G@5F;A@E ;@6;H;6G3>>K I;F:AGF D793D6 FA 3>> A8 F:7 B>357E F:3F
8G@5F;A@ ;E 53>>76 8DA?. )A F:7 5A?B;>7D, F:7 slice B3D3?7F7D A8 foo() ?3K 47 D787DD;@9 FA 3
?GF34>7 H3D;34>7 AD 3@ immutable A@7.
A NJGPODJI RJPG? =@ OJ ><GG CBS() RDOC <I DHHPO<=G@ >JKT JA OC@ K<M<H@O@M:
WPJE GPP(DPOTU JOU<> TMJDF)
\
CBS(TMJDF.JEVQ);
^
AGOCJPBC OC<O DN < N@IND=G@ NJGPODJI, DO ?J@N C<Q@ OC@ >JNO JA >JKTDIB, RCD>C RJPG? =@ R<NO@APG
DI OC@ ><N@ RC@M@ OC@ JMDBDI<G Q<MD<=G@ C<N =@@I JNNVUBCMF OJ =@BDI RDOC.
AAO@M OCDN <I<GTNDN, DO NCJPG? =@ >G@<M OC<O O<FDIB K<M<H@O@MN <GR<TN <N DPOTU ?J@N IJO N@@H
OJ =@ OC@ =@NO <KKMJ<>C DI @Q@MT NDOP<ODJI. AAO@M <GG, DA GPP()'N K<M<H@O@M C<N =@@I ?@ADI@? <N
JNNVUBCMF, OC@M@ RJPG? IJO =@ <IT I@@? OJ >JKT DO =@AJM@ ><GGDIB CBS():
!??GF34;>;FK
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
165
WPJE GPP(JNNVUBCMF JOU<> TMJDF) // 5IJT UJNF JNNVUBCMF
\
CBS(TMJDF); // $PQZJOH JT OPU OFFEFE BOZNPSF
^
AGOCJPBC OC@ >J?@ >JHKDG@N, ?@ADIDIB OC@ K<M<H@O@M <N JNNVUBCMF C<N < NDHDG<M >JNO: 1CDN
ODH@ <I DHHPO<=G@ >JKT JA OC@ JMDBDI<G Q<MD<=G@ DN I@@?@? RC@I ><GGDIB GPP(), DA OC<O Q<MD<=G@
R@M@ IJO DHHPO<=G@ OJ =@BDI RDOC:
GPP(NVUBCMF4MJDF.JEVQ);
1@HKG<O@N ><I KMJQD?@ C@GK. (4@ RDGG N@@ O@HKG<O@N DI G<O@M >C<KO@MN.) AGOCJPBC & ?JI'O @SK@>O
TJP OJ APGGT PI?@MNO<I? OC@ AJGGJRDIB API>ODJI <O OCDN KJDIO DI OC@ =JJF, & RDGG KM@N@IO DO <N <
NJGPODJI OJ OCDN KMJ=G@H. 1C@ AJGGJRDIB API>ODJI O@HKG<O@ GPP() ><I =@ ><GG@? =JOC =T
HPO<=G@ <I? =T JNNVUBCMF Q<MD<=G@N. 1C@ K<M<H@O@M RJPG? =@ >JKD@? JIGT DA OC@ JMDBDI<G
Q<MD<=G@ C<N =@@I HPO<=G@; IJ >JKTDIB RJPG? O<F@ KG<>@ DA DO C<N =@@I JNNVUBCMF:
JNQPSU TUE.DPOW;
// ...
/* #FDBVTF JU JT B UFNQMBUF, GPP() DBO CF DBMMFE CZ NVUBCMF
* BOE JNNVUBCMF WBSJBCMFT. */
WPJE GPP(5)(5<> TMJDF)
\
/* 'UP()' EPFT OPU NBLF B DPQZ JG UIF PSJHJOBM WBSJBCMF JT
* BMSFBEZ JNNVUBCMF. */
CBS(UP!(JNNVUBCMF 5<>)(TMJDF));
^
Immutability of the slice versus the elements
36.3
4@ C<Q@ N@@I <=JQ@ OC<O OC@ OTK@ JA <I JNNVUBCMF NGD>@ C<N =@@I KMDIO@? <N
JNNVUBCMF(JOU<>). AN OC@ K<M@IOC@N@N <AO@M JNNVUBCMF DI?D><O@, DO DN OC@ @IODM@ NGD>@ OC<O DN
JNNVUBCMF. 0P>C < NGD>@ ><IIJO =@ HJ?DAD@? DI <IT R<T: @G@H@ION H<T IJO =@ <??@? JM
M@HJQ@?, OC@DM Q<GP@N H<T IJO =@ HJ?DAD@?, <I? OC@ NGD>@ H<T IJO NO<MO KMJQD?DIB <>>@NN OJ <
?DAA@M@IO N@O JA @G@H@ION:
JNNVUBCMF JOU<> JNN4MJDF = < 1, 2 >;
JNN4MJDF _= 3; // j DPNQJMBUJPO &3303
JNN4MJDF<0> = 3; // j DPNQJMBUJPO &3303
JNN4MJDF.MFOHUI = 1; // j DPNQJMBUJPO &3303
JNNVUBCMF JOU<> JNN0UIFS4MJDF = < 10, 11 >;
JNN4MJDF = JNN0UIFS4MJDF; // j DPNQJMBUJPO &3303
1<FDIB DHHPO<=DGDOT OJ OC<O @SOM@H@ H<T IJO =@ NPDO<=G@ DI @Q@MT ><N@. &I HJNO ><N@N, RC<O DN
DHKJMO<IO DN OC@ DHHPO<=DGDOT JA EPNO OC@ @G@H@ION. 0DI>@ < NGD>@ DN EPNO <I <>>@NN OJJG OJ
@G@H@ION, DO NCJPG? IJO H<OO@M OJ H<F@ >C<IB@N OJ OC@ NGD>@ DON@GA <N GJIB <N OC@ @G@H@ION <M@
IJO HJ?DAD@?.
1J NK@>DAT OC<O JIGT OC@ @G@H@ION <M@ DHHPO<=G@, JIGT OC@ @G@H@IO OTK@ DN RMDOO@I RDOCDI
K<M@IOC@N@N. 4C@I OC@ >J?@ DN HJ?DAD@? <>>JM?DIBGT, IJR JIGT OC@ @G@H@ION <M@ DHHPO<=G@,
IJO OC@ NGD>@ DON@GA:
JNNVUBCMF(JOU)<> JNN4MJDF = < 1, 2 >;
JNN4MJDF _= 3; // DBO BEE FMFNFOUT
JNN4MJDF<0> = 3; // j DPNQJMBUJPO &3303
JNN4MJDF.MFOHUI = 1; // DBO MPTF FMFNFOUT
!??GF34;>;FK
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
166
JNNVUBCMF JOU<> JNN0UIFS4MJDF = < 10, 11 >;
JNN4MJDF = JNN0UIFS4MJDF; // DBO QSPWJEF BDDFTT UP PUIFS FMFNFOUT
AGOCJPBC OC@ ORJ NTIO<S@N <M@ Q@MT NDHDG<M, OC@T C<Q@ ?DAA@M@IO H@<IDIBN. 1J NPHH<MDU@:
JNNVUBCMF JOU<> B = <1>; /* /FJUIFS UIF FMFNFOUT OPS UIF
* TMJDF DBO CF NPEJGJFE */
JNNVUBCMF(JOU<>) C = <1>; /* 5IF TBNF NFBOJOH BT BCPWF */
JNNVUBCMF(JOU)<> D = <1>; /* 5IF FMFNFOUT DBOOPU CF
* NPEJGJFE CVU UIF TMJDF DBO CF */
1CDN ?DNODI>ODJI C<Q@ =@@I DI @AA@>O DI NJH@ JA OC@ KMJBM<HN OC<O R@ C<Q@ RMDOO@I NJ A<M. AN
TJP H<T M@H@H=@M, OC@ OCM@@ NOMDIB <GD<N@N DIQJGQ@ DHHPO<=DGDOT:
t TUSJOH DN <I <GD<N AJM JNNVUBCMF(DIBS)<>
t XTUSJOH DN <I <GD<N AJM JNNVUBCMF(XDIBS)<>
t ETUSJOH DN <I <GD<N AJM JNNVUBCMF(EDIBS)<>
)DF@RDN@, NOMDIB GDO@M<GN <M@ DHHPO<=G@ <N R@GG:
t 1C@ OTK@ JA GDO@M<G "IFMMP"D DN TUSJOH
t 1C@ OTK@ JA GDO@M<G "IFMMP"X DN XTUSJOH
t 1C@ OTK@ JA GDO@M<G "IFMMP"E DN ETUSJOH
A>>JM?DIB OJ OC@N@ ?@ADIDODJIN, ! NOMDIBN <M@ IJMH<GGT <MM<TN JA ;??GF34>7 5:3D35F7DE.
.EVQ and .JEVQ 36.3.1
1C@M@ H<T =@ HDNH<O>C@N DI DHHPO<=DGDOT RC@I NOMDIBN <M@ K<NN@? OJ API>ODJIN <N
K<M<H@O@MN. 1C@ .EVQ <I? .JEVQ KMJK@MOD@N H<F@ >JKD@N JA <MM<TN RDOC OC@ ?@NDM@?
DHHPO<=DGDOT:
t .EVQ H<F@N < HPO<=G@ >JKT JA OC@ <MM<T; DON I<H@ >JH@N AMJH "?PKGD><O@"
t .JEVQ H<F@N <I DHHPO<=G@ >JKT JA OC@ <MM<T
#JM @S<HKG@, < API>ODJI OC<O DINDNON JI OC@ DHHPO<=DGDOT JA < K<M<H@O@M H<T C<Q@ OJ =@ ><GG@?
RDOC <I DHHPO<=G@ >JKT JA < HPO<=G@ NOMDIB:
WPJE GPP(TUSJOH T)
\
// ...
^
WPJE NBJO()
\
DIBS<> TBMVUBUJPO;
GPP(TBMVUBUJPO); // j DPNQJMBUJPO &3303
GPP(TBMVUBUJPO.JEVQ); // j UIJT DPNQJMFT
^
How to use
36.4
t AN < B@I@M<G MPG@, KM@A@M DHHPO<=G@ Q<MD<=G@N JQ@M HPO<=G@ JI@N.
t !@ADI@ >JINO<IO Q<GP@N <N FOVN DA OC@DM Q<GP@N ><I =@ ><G>PG<O@? <O >JHKDG@ ODH@. #JM
@S<HKG@, OC@ >JINO<IO Q<GP@ JA E75A@6E B7D ?;@GF7 ><I =@ <I FOVN:
FOVN JOU TFDPOET1FS.JOVUF = 60;
!??GF34;>;FK
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
167
1C@M@ DN IJ I@@? OJ NK@>DAT OC@ OTK@ @SKGD>DOGT DA DO ><I =@ DIA@MM@? AMJH OC@ MDBCO C<I?
ND?@:
FOVN TFDPOET1FS.JOVUF = 60;
t JIND?@M OC@ CD??@I >JNO JA FOVN <MM<TN <I? FOVN <NNJ>D<ODQ@ <MM<TN. !@ADI@ OC@H <N
JNNVUBCMF Q<MD<=G@N DA OC@ <MM<TN <M@ G<MB@ <I? OC@T <M@ PN@? HJM@ OC<I JI>@ DI OC@
KMJBM<H.
t 0K@>DAT Q<MD<=G@N <N JNNVUBCMF DA OC@DM Q<GP@N RDGG I@Q@M >C<IB@ =PO ><IIJO =@ FIJRI <O
>JHKDG@ ODH@. AB<DI, OC@ OTK@ ><I =@ DIA@MM@?:
JNNVUBCMF HVFTT = SFBE@JOU("8IBU JT ZPVS HVFTT");
t &A < API>ODJI ?J@N IJO HJ?DAT < K<M<H@O@M, NK@>DAT OC<O K<M<H@O@M <N DPOTU. 1CDN RJPG?
<GGJR =JOC HPO<=G@ <I? JNNVUBCMF Q<MD<=G@N OJ =@ K<NN@? <N <MBPH@ION:
WPJE GPP(DPOTU DIBS<> T)
\
// ...
^
WPJE NBJO()
\
DIBS<> NVUBCMF4USJOH;
TUSJOH JNNVUBCMF4USJOH;
GPP(NVUBCMF4USJOH); // j DPNQJMFT
GPP(JNNVUBCMF4USJOH); // j DPNQJMFT
^
t #JGGJRDIB AMJH OC@ KM@QDJPN BPD?@GDI@, >JIND?@M OC<O DPOTU K<M<H@O@MN ><IIJO =@ K<NN@?
OJ API>ODJIN O<FDIB JNNVUBCMF. 0@@ OC@ N@>ODJI ODOG@? "0CJPG? < K<M<H@O@M =@ DPOTU JM
JNNVUBCMF?" <=JQ@.
t &A OC@ API>ODJI HJ?DAD@N < K<M<H@O@M, G@<Q@ OC<O K<M<H@O@M <N HPO<=G@ (DPOTU JM
JNNVUBCMF RJPG? IJO <GGJR HJ?DAD><ODJIN <ITR<T):
JNQPSU TUE.TUEJP;
WPJE SFWFSTF(EDIBS<> T)
\
GPSFBDI (J; 0 .. T.MFOHUI / 2) \
JNNVUBCMF UFNQ = T<J>;
T<J> = T<$ - 1 - J>;
T<$ - 1 - J> = UFNQ;
^
^
WPJE NBJO()
\
EDIBS<> TBMVUBUJPO = "IFMMP"E.EVQ;
SFWFSTF(TBMVUBUJPO);
XSJUFMO(TBMVUBUJPO);
^
1C@ JPOKPO:
PMMFI
!??GF34;>;FK
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
168
Summary
36.5
t FOVN Q<MD<=G@N M@KM@N@IO DHHPO<=G@ >JI>@KON OC<O <M@ FIJRI <O >JHKDG@ ODH@.
t JNNVUBCMF Q<MD<=G@N M@KM@N@IO DHHPO<=G@ >JI>@KON OC<O HPNO =@ ><G>PG<O@? <O MPI ODH@.
t DPOTU K<M<H@O@MN <M@ OC@ JI@N OC<O API>ODJIN ?J IJO HJ?DAT. BJOC HPO<=G@ <I? JNNVUBCMF
Q<MD<=G@N ><I =@ K<NN@? <N <MBPH@ION JA DPOTU K<M<H@O@MN.
t JNNVUBCMF K<M<H@O@MN <M@ OC@ JI@N OC<O API>ODJIN NK@>DAD><GGT M@LPDM@ OC@H OJ =@ NJ. ,IGT
JNNVUBCMF Q<MD<=G@N ><I =@ K<NN@? <N <MBPH@ION JA JNNVUBCMF K<M<H@O@MN.
t JNNVUBCMF(JOU<>) NK@>DAD@N OC<O I@DOC@M OC@ NGD>@ IJM DON @G@H@ION ><I =@ HJ?DAD@?.
t JNNVUBCMF(JOU)<> NK@>DAD@N OC<O JIGT OC@ @G@H@ION ><IIJO =@ HJ?DAD@?.
t &HHPO<=DGDOT DN < Q@MT KJR@MAPG OJJG DI KMJBM<HHDIB. &O DN C@GKAPG OJ FIJR OC<O Q<MD<=G@N
<I? K<M<H@O@MN ?J IJO >C<IB@ DI NK@>DAD> >JIO@SON <I? ?PMDIB NK@>DAD> JK@M<ODJIN.
!??GF34;>;FK
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
169
Function Parameters 37
1CDN >C<KO@M >JQ@MN ?DAA@M@IO R<TN JA ?@ADIDIB API>ODJI K<M<H@O@MN.
0JH@ JA OC@ >JI>@KON JA OCDN >C<KO@M C<Q@ <GM@<?T <KK@<M@? DI KM@QDJPN >C<KO@MN. #JM
@S<HKG@, OC@ SFG F@TRJM? OC<O R@ C<Q@ N@@I DI OC@ GPSFBDI )JJK >C<KO@M R<N H<FDIB <>OP<G
@G@H@ION <Q<DG<=G@ DI GPSFBDI GJJKN <N JKKJN@? OJ >JKD@N JA OCJN@ @G@H@ION.
A??DODJI<GGT, R@ C<Q@ >JQ@M@? OC@ DPOTU <I? JNNVUBCMF F@TRJM?N DI OC@ KM@QDJPN >C<KO@M.
4@ C<Q@ RMDOO@I API>ODJIN OC<O KMJ?P>@? M@NPGON =T H<FDIB PN@ JA OC@DM K<M<H@O@MN. #JM
@S<HKG@, OC@ AJGGJRDIB API>ODJI PN@N DON K<M<H@O@MN DI < ><G>PG<ODJI:
EPVCMF XFJHIUFE"WFSBHF(EPVCMF RVJ[(SBEF, EPVCMF GJOBM(SBEF)
\
SFUVSO RVJ[(SBEF * 0.4 + GJOBM(SBEF * 0.6;
^
1C<O API>ODJI ><G>PG<O@N OC@ <Q@M<B@ BM<?@ =T O<FDIB 40% JA OC@ LPDU BM<?@ <I? 60% JA OC@
ADI<G BM<?@. %@M@ DN CJR DO H<T =@ PN@?:
JOU RVJ[(SBEF = 76;
JOU GJOBM(SBEF = 80;
XSJUFGMO("8FJHUIFE BWFSBHF: %2.0G",
XFJHIUFE"WFSBHF(RVJ[(SBEF, GJOBM(SBEF));
Most parameters are copied
37.1
&I OC@ >J?@ <=JQ@, OC@ ORJ Q<MD<=G@N <M@ K<NN@? <N <MBPH@ION OJ XFJHIUFE"WFSBHF() <I? OC@
API>ODJI GE7E DON K<M<H@O@MN. 1CDN A<>O H<T BDQ@ OC@ A<GN@ DHKM@NNDJI OC<O OC@ API>ODJI PN@N
OC@ <>OP<G Q<MD<=G@N OC<O C<Q@ =@@I K<NN@? <N <MBPH@ION. &I M@<GDOT, RC<O OC@ API>ODJI PN@N
<M@ 5AB;7E JA OCJN@ Q<MD<=G@N.
1CDN ?DNODI>ODJI DN DHKJMO<IO =@><PN@ HJ?DATDIB < K<M<H@O@M >C<IB@N JIGT OC@ >JKT. 1CDN ><I
=@ N@@I DI OC@ AJGGJRDIB API>ODJI OC<O DN OMTDIB OJ HJ?DAT DON K<M<H@O@M (D.@. H<FDIB < ND?@
@AA@>O). )@O'N <NNPH@ OC<O OC@ AJGGJRDIB API>ODJI DN RMDOO@I AJM M@?P>DIB OC@ @I@MBT JA < B<H@
>C<M<>O@M:
WPJE SFEVDF&OFSHZ(EPVCMF FOFSHZ)
\
FOFSHZ /= 4;
^
%@M@ DN < KMJBM<H OC<O O@NON SFEVDF&OFSHZ():
JNQPSU TUE.TUEJP;
WPJE SFEVDF&OFSHZ(EPVCMF FOFSHZ)
\
FOFSHZ /= 4;
^
WPJE NBJO()
\
EPVCMF FOFSHZ = 100;
SFEVDF&OFSHZ(FOFSHZ);
XSJUFMO("/FX FOFSHZ: ", FOFSHZ);
^
FG@5F;A@ &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
170
1C@ JPOKPO:
/FX FOFSHZ: 100 l /PU DIBOHFE!
AGOCJPBC SFEVDF&OFSHZ() ?MJKN OC@ Q<GP@ JA DON K<M<H@O@M OJ < LP<MO@M JA DON JMDBDI<G Q<GP@,
OC@ Q<MD<=G@ FOFSHZ DI NBJO() ?J@N IJO >C<IB@. 1C@ M@<NJI AJM OCDN DN OC<O OC@ FOFSHZ
Q<MD<=G@ DI NBJO() <I? OC@ FOFSHZ K<M<H@O@M JA SFEVDF&OFSHZ() <M@ N@K<M<O@; OC@
K<M<H@O@M DN < >JKT JA OC@ Q<MD<=G@ DI NBJO().
1J J=N@MQ@ OCDN HJM@ >GJN@GT, G@O'N DIN@MO NJH@ XSJUFMO() @SKM@NNDJIN:
JNQPSU TUE.TUEJP;
WPJE SFEVDF&OFSHZ(EPVCMF FOFSHZ)
\
XSJUFMO("&OUFSFE UIF GVODUJPO : ", FOFSHZ);
FOFSHZ /= 4;
XSJUFMO("-FBWJOH UIF GVODUJPO : ", FOFSHZ);
^
WPJE NBJO()
\
EPVCMF FOFSHZ = 100;
XSJUFMO("$BMMJOH UIF GVODUJPO : ", FOFSHZ);
SFEVDF&OFSHZ(FOFSHZ);
XSJUFMO("3FUVSOFE GSPN UIF GVODUJPO: ", FOFSHZ);
^
1C@ JPOKPO:
$BMMJOH UIF GVODUJPO : 100
&OUFSFE UIF GVODUJPO : 100
-FBWJOH UIF GVODUJPO : 25 l UIF QBSBNFUFS DIBOHFT,
3FUVSOFE GSPN UIF GVODUJPO: 100 l UIF WBSJBCMF SFNBJOT UIF TBNF
Objects of reference types are not copied
37.2
"G@H@ION JA NGD>@N <I? <NNJ>D<ODQ@ <MM<TN, <I? >G<NN J=E@>ON <M@ IJO >JKD@? RC@I K<NN@? <N
K<M<H@O@MN. 0P>C Q<MD<=G@N <M@ K<NN@? OJ API>ODJIN <N D787D7@57E. &I @AA@>O, OC@ K<M<H@O@M
=@>JH@N < M@A@M@I>@ OJ OC@ <>OP<G J=E@>O; HJ?DAD><ODJIN H<?@ OCMJPBC OC@ M@A@M@I>@ HJ?DAD@N
OC@ <>OP<G J=E@>O.
B@DIB NGD>@N, NOMDIBN <M@ K<NN@? <N M@A@M@I>@N <N R@GG:
JNQPSU TUE.TUEJP;
WPJE NBLF'JSTU-FUUFS%PU(EDIBS<> TUS)
\
TUS<0> = '.';
^
WPJE NBJO()
\
EDIBS<> TUS = "BCD"E.EVQ;
NBLF'JSTU-FUUFS%PU(TUS);
XSJUFMO(TUS);
^
1C@ >C<IB@ H<?@ OJ OC@ ADMNO @G@H@IO JA OC@ K<M<H@O@M <AA@>ON OC@ <>OP<G @G@H@IO DI NBJO():
FG@5F;A@ &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
171
.CD
Parameter qualifiers
37.3
-<M<H@O@MN <M@ K<NN@? OJ API>ODJIN <>>JM?DIB OJ OC@ B@I@M<G MPG@N ?@N>MD=@? <=JQ@:
t 3<GP@ OTK@N <M@ >JKD@?
t /@A@M@I>@ OTK@N <M@ K<NN@? <N M@A@M@I>@N
1CJN@ <M@ OC@ ?@A<PGO MPG@N OC<O <M@ <KKGD@? RC@I K<M<H@O@M ?@ADIDODJIN C<Q@ IJ LP<GDAD@MN.
1C@ AJGGJRDIB LP<GDAD@MN >C<IB@ OC@ R<T K<M<H@O@MN <M@ K<NN@? <I? RC<O JK@M<ODJIN <M@
<GGJR@? JI OC@H.
JO 37.3.1
4@ C<Q@ N@@I OC<O API>ODJIN <M@ < A<>DGDOT OC<O KMJ?P>@N Q<GP@N <I? ><I C<Q@ ND?@ @AA@>ON. 1C@
JO F@TRJM? NK@>DAD@N OC<O < K<M<H@O@M DN BJDIB =@ PN@? JIGT <N <I DIKPO ?<O< =T OCDN A<>DGDOT.
0P>C K<M<H@O@MN ><IIJO =@ HJ?DAD@? =T OC@ API>ODJI:
JNQPSU TUE.TUEJP;
EPVCMF XFJHIUFE5PUBM(JO EPVCMF DVSSFOU5PUBM,
JO EPVCMF XFJHIU,
JO EPVCMF BEEFOE)
\
SFUVSO DVSSFOU5PUBM + (XFJHIU * BEEFOE);
^
WPJE NBJO()
\
XSJUFMO(XFJHIUFE5PUBM(1.23, 4.56, 7.89));
^
JO K<M<H@O@MN ><IIJO =@ HJ?DAD@?:
WPJE GPP(JO JOU WBMVF)
\
WBMVF = 1; // j DPNQJMBUJPO &3303
^
JO DN OC@ @LPDQ<G@IO JA DPOTU TDPQF.
PVU 37.3.2
4@ C<Q@ N@@I OC<O OC@ API>ODJIN M@OPMI OC@ Q<GP@N OC<O OC@T KMJ?P>@ <N OC@DM M@OPMI Q<GP@N.
1C@ A<>O OC<O OC@M@ DN JIGT JI@ M@OPMI Q<GP@ DN NJH@ODH@N GDHDODIB <N NJH@ API>ODJIN H<T I@@?
OJ KMJ?P>@ HJM@ OC<I JI@ M@NPGO. (Note: !F ;E 35FG3>>K BAEE;4>7 FA D7FGD@ ?AD7 F:3@ A@7 D7EG>F
4K 678;@;@9 F:7 D7FGD@ FKB7 3E 3 Tuple AD 3 struct. ,7 I;>> E77 F:7E7 873FGD7E ;@ >3F7D
5:3BF7DE.)
1C@ PVU F@TRJM? H<F@N DO KJNND=G@ AJM API>ODJIN OJ M@OPMI M@NPGON OCMJPBC OC@DM K<M<H@O@MN.
4C@I PVU K<M<H@O@MN <M@ HJ?DAD@? RDOCDI OC@ API>ODJI, OCJN@ HJ?DAD><ODJIN @AA@>O OC@ <>OP<G
Q<MD<=G@ OC<O C<N =@@I K<NN@? OJ OC@ API>ODJI. &I < N@IN@, OC@ <NNDBI@? Q<GP@ BJ@N AGF JA OC@
API>ODJI OCMJPBC PVU K<M<H@O@MN.
)@O'N C<Q@ < GJJF <O < API>ODJI OC<O ?DQD?@N ORJ IPH=@MN <I? KMJ?P>@N =JOC OC@ LPJOD@IO <I?
OC@ M@H<DI?@M. 1C@ M@OPMI Q<GP@ ><I =@ PN@? AJM OC@ LPJOD@IO <I? OC@ M@H<DI?@M ><I =@
M@OPMI@? OCMJPBC <I PVU K<M<H@O@M:
FG@5F;A@ &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
172
JNQPSU TUE.TUEJP;
JOU EJWJEF(JO JOU EJWJEFOE, JO JOU EJWJTPS, PVU JOU SFNBJOEFS)
\
SFNBJOEFS = EJWJEFOE % EJWJTPS;
SFUVSO EJWJEFOE / EJWJTPS;
^
WPJE NBJO()
\
JOU SFNBJOEFS;
JOU SFTVMU = EJWJEF(7, 3, SFNBJOEFS);
XSJUFMO("SFTVMU: ", SFTVMU, ", SFNBJOEFS: ", SFNBJOEFS);
^
*J?DATDIB OC@ SFNBJOEFS K<M<H@O@M JA OC@ API>ODJI HJ?DAD@N OC@ SFNBJOEFS Q<MD<=G@ DI
NBJO() (OC@DM I<H@N I@@? IJO =@ OC@ N<H@):
SFTVMU: 2, SFNBJOEFS: 1
/@B<M?G@NN JA OC@DM Q<GP@N <O OC@ ><GG NDO@, PVU K<M<H@O@MN <M@ ADMNO <POJH<OD><GGT <NNDBI@? OJ
OC@ .JOJU Q<GP@ JA OC@DM OTK@N:
JNQPSU TUE.TUEJP;
WPJE GPP(PVU JOU QBSBNFUFS)
\
XSJUFMO(""GUFS FOUFSJOH UIF GVODUJPO : ", QBSBNFUFS);
^
WPJE NBJO()
\
JOU WBSJBCMF = 100;
XSJUFMO("#FGPSF DBMMJOH UIF GVODUJPO : ", WBSJBCMF);
GPP(WBSJBCMF);
XSJUFMO(""GUFS SFUVSOJOH GSPN UIF GVODUJPO: ", WBSJBCMF);
^
"Q@I OCJPBC OC@M@ DN IJ @SKGD>DO <NNDBIH@IO OJ OC@ K<M<H@O@M DI OC@ API>ODJI, OC@ Q<GP@ JA OC@
K<M<H@O@M <POJH<OD><GGT =@>JH@N OC@ DIDOD<G Q<GP@ JA JOU, <AA@>ODIB OC@ Q<MD<=G@ DI NBJO():
#FGPSF DBMMJOH UIF GVODUJPO : 100
"GUFS FOUFSJOH UIF GVODUJPO : 0 l UIF WBMVF PG JOU.JOJU
"GUFS SFUVSOJOH GSPN UIF GVODUJPO: 0
AN OCDN ?@HJINOM<O@N, PVU K<M<H@O@MN ><IIJO K<NN Q<GP@N DIOJ API>ODJIN; OC@T <M@ NOMD>OGT AJM
K<NNDIB Q<GP@N JPO JA API>ODJIN.
4@ RDGG N@@ DI G<O@M >C<KO@MN OC<O M@OPMIDIB 5VQMF JM TUSVDU OTK@N H<T =@ =@OO@M <GO@MI<ODQ@N
OJ PVU K<M<H@O@MN.
DPOTU 37.3.3
AN R@ C<Q@ N@@I DI OC@ KM@QDJPN >C<KO@M, DPOTU BP<M<IO@@N OC<O OC@ K<M<H@O@M RDGG IJO =@
HJ?DAD@? DIND?@ OC@ API>ODJI. &O DN C@GKAPG AJM OC@ KMJBM<HH@MN OJ FIJR OC<O >@MO<DI Q<MD<=G@N
RDGG IJO =@ >C<IB@? =T OC@ API>ODJI. DPOTU <GNJ H<F@N API>ODJIN HJM@ PN@APG =T <GGJRDIB
?GF34>7, DPOTU <I? JNNVUBCMF Q<MD<=G@N OJ =@ K<NN@? <N K<M<H@O@MN:
FG@5F;A@ &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
173
JNQPSU TUE.TUEJP;
EDIBS MBTU-FUUFS(DPOTU EDIBS<> TUS)
\
SFUVSO TUS<$ - 1>;
^
WPJE NBJO()
\
XSJUFMO(MBTU-FUUFS("DPOTUBOU"));
^
JNNVUBCMF 37.3.4
AN R@ C<Q@ N@@I DI OC@ KM@QDJPN >C<KO@M, JNNVUBCMF H<F@N API>ODJIN M@LPDM@ OC<O >@MO<DI
<MBPH@ION HPNO >JINDNO JA JNNVUBCMF @G@H@ION. B@><PN@ JA NP>C < M@LPDM@H@IO, OC@
AJGGJRDIB API>ODJI ><I JIGT =@ ><GG@? RDOC JNNVUBCMF NOMDIBN (@.B. RDOC NOMDIB GDO@M<GN):
JNQPSU TUE.TUEJP;
EDIBS<> NJY(JNNVUBCMF EDIBS<> GJSTU,
JNNVUBCMF EDIBS<> TFDPOE)
\
EDIBS<> SFTVMU;
JOU J;
GPS (J = 0; (J < GJSTU.MFOHUI) && (J < TFDPOE.MFOHUI); ++J) \
SFTVMU _= GJSTU<J>;
SFTVMU _= TFDPOE<J>;
^
SFTVMU _= GJSTU<J..$>;
SFTVMU _= TFDPOE<J..$>;
SFUVSO SFTVMU;
^
WPJE NBJO()
\
XSJUFMO(NJY(")&--0", "XPSME"));
^
0DI>@ DO =MDIBN < M@LPDM@H@IO JI OC@ K<M<H@O@M, JNNVUBCMF K<M<H@O@MN NCJPG? =@ PN@? JIGT
RC@I M@<GGT I@>@NN<MT. B@><PN@ DO DN HJM@ R@G>JHDIB, DPOTU DN HJM@ PN@APG.
SFG 37.3.5
1CDN F@TRJM? <GGJRN K<NNDIB < K<M<H@O@M =T M@A@M@I>@ @Q@I OCJPBC DO RJPG? IJMH<GGT =@
K<NN@? <N < >JKT.
#JM OC@ SFEVDF&OFSHZ() API>ODJI <=JQ@ OJ =@ <=G@ OJ HJ?DAT OC@ <>OP<G Q<MD<=G@ OC<O DN
K<NN@? <N DON <MBPH@IO, DO HPNO O<F@ OC@ K<M<H@O@M <N SFG:
JNQPSU TUE.TUEJP;
WPJE SFEVDF&OFSHZ(SFG EPVCMF FOFSHZ)
\
FOFSHZ /= 4;
^
WPJE NBJO()
\
EPVCMF FOFSHZ = 100;
SFEVDF&OFSHZ(FOFSHZ);
FG@5F;A@ &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
174
XSJUFMO("/FX FOFSHZ: ", FOFSHZ);
^
1CDN ODH@, OC@ HJ?DAD><ODJI OC<O DN H<?@ OJ OC@ K<M<H@O@M >C<IB@N OC@ <>OP<G Q<MD<=G@ OC<O DN
K<NN@? OJ OC@ API>ODJI DI NBJO():
/FX FOFSHZ: 25
AN ><I =@ N@@I, SFG K<M<H@O@MN ><I =@ PN@? =JOC <N DIKPO <I? JPOKPO. SFG K<M<H@O@MN ><I
<GNJ =@ OCJPBCO JA <N <GD<N@N JA <>OP<G Q<MD<=G@N. 1C@ API>ODJI K<M<H@O@M FOFSHZ <=JQ@ DN <I
<GD<N JA OC@ Q<MD<=G@ FOFSHZ DI NBJO().
0DHDG<M OJ PVU K<M<H@O@MN, SFG K<M<H@O@MN <GGJR API>ODJIN H<F@ ND?@ @AA@>ON <N R@GG. &I A<>O,
SFEVDF&OFSHZ() ?J@N IJO M@OPMI < Q<GP@; DO JIGT H<F@N < ND?@ @AA@>O OCMJPBC DON JIGT
K<M<H@O@M.
1C@ KMJBM<HHDIB NOTG@ ><GG@? 8G@5F;A@3> BDA9D3??;@9 A<QJMN M@OPMI Q<GP@N JQ@M ND?@ @AA@>ON,
NJ HP>C NJ OC<O NJH@ API>ODJI<G KMJBM<HHDIB G<IBP<B@N ?J IJO <GGJR ND?@ @AA@>ON <O <GG. 1CDN
DN =@><PN@ API>ODJIN OC<O KMJ?P>@ M@NPGON BGD7>K OCMJPBC OC@DM M@OPMI Q<GP@N <M@ @<ND@M OJ
PI?@MNO<I?, OJ RMDO@ >JMM@>OGT, <I? OJ H<DIO<DI.
1C@ N<H@ API>ODJI ><I =@ RMDOO@I DI API>ODJI<G KMJBM<HHDIB NOTG@ =T M@OPMIDIB OC@ M@NPGO,
DINO@<? JA H<FDIB < ND?@ @AA@>O. 1C@ K<MON JA OC@ KMJBM<H OC<O C<N >C<IB@? <M@ CDBCGDBCO@? DI
T@GGJR:
JNQPSU TUE.TUEJP;
EPVCMF SFEVDFE&OFSHZ(EPVCMF FOFSHZ)
\
SFUVSO FOFSHZ / 4;
^
WPJE NBJO()
\
EPVCMF FOFSHZ = 100;
FOFSHZ = SFEVDFE&OFSHZ(FOFSHZ);
XSJUFMO("/FX FOFSHZ: ", FOFSHZ);
^
+JO@ OC@ >C<IB@ DI OC@ I<H@ JA OC@ API>ODJI <N R@GG. +JR DO DN < IJPI <N JKKJN@? OJ < Q@M=.
JOPVU 37.3.6
!@NKDO@ DON I<H@ >JINDNODIB JA JO <I? PVU, OCDN F@TRJM? ?J@N IJO H@<I =JOC DIKPO <I?
JPOKPO; R@ C<Q@ <GM@<?T N@@I OC<O OC@ F@TRJM? OC<O <>CD@Q@N =JOC DIKPO <I? JPOKPO DN SFG.
JOPVU ><MMD@N OC@ ?GF34;>;FK JA OC@ K<M<H@O@M OJ OC@ M@OPMI OTK@. &A OC@ K<M<H@O@M DN ?GF34>7,
DPOTU, JM JNNVUBCMF, OC@I OC@ M@OPMI Q<GP@ DN ?GF34>7, DPOTU, JM JNNVUBCMF, M@NK@>ODQ@GT.
1J N@@ CJR JOPVU C@GKN DI KMJBM<HN, G@O'N GJJF <O < API>ODJI OC<O M@OPMIN < NGD>@ C<QDIB JI@
G@NN @G@H@IO AMJH =JOC OC@ AMJIO <I? OC@ =<>F JA OC@ JMDBDI<G NGD>@:
JNQPSU TUE.TUEJP;
JOU<> USJNNFE(JOU<> TMJDF)
\
JG (TMJDF.MFOHUI) \
--TMJDF.MFOHUI; // USJN GSPN UIF FOE
JG (TMJDF.MFOHUI) \
FG@5F;A@ &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
175
TMJDF = TMJDF<1 .. $>; // USJN GSPN UIF CFHJOOJOH
^
^
SFUVSO TMJDF;
^
WPJE NBJO()
\
JOU<> OVNCFST = < 5, 6, 7, 8, 9 >;
XSJUFMO(USJNNFE(OVNCFST));
^
1C@ JPOKPO:
<6, 7, 8>
A>>JM?DIB OJ OC@ KM@QDJPN >C<KO@M, DI JM?@M AJM OC@ API>ODJI OJ =@ HJM@ PN@APG, DON K<M<H@O@M
NCJPG? =@ DPOTU(JOU)<> =@><PN@ OC@ K<M<H@O@M DN IJO =@DIB HJ?DAD@? DIND?@ OC@ API>ODJI.
(+JO@ OC<O OC@M@ DN IJ C<MH DI HJ?DATDIB OC@ K<M<H@O@M NGD>@ DON@GA <N DO DN < >JKT JA OC@
JMDBDI<G <MBPH@IO.)
%JR@Q@M, ?@ADIDIB OC@ API>ODJI OC<O R<T RJPG? ><PN@ < >JHKDG<ODJI @MMJM:
JOU<> USJNNFE(DPOTU(JOU)<> TMJDF)
\
// ...
SFUVSO TMJDF; // j DPNQJMBUJPO &3303
^
1C@ >JHKDG<ODJI @MMJM DI?D><O@N OC<O < NGD>@ JA DPOTU(JOU) ><IIJO =@ M@OPMI@? <N < NGD>@ JA
HPO<=G@ JOU:
&SSPS: DBOOPU JNQMJDJUMZ DPOWFSU FYQSFTTJPO (TMJDF) PG UZQF
DPOTU(JOU)<> UP JOU<>
,I@ H<T OCDIF OC<O NK@>DATDIB OC@ M@OPMI OTK@ <GNJ <N DPOTU(JOU)<> H<T =@ OC@ NJGPODJI:
DPOTU(JOU)<> USJNNFE(DPOTU(JOU)<> TMJDF)
\
// ...
SFUVSO TMJDF; // OPX DPNQJMFT
^
AGOCJPBC OC@ >J?@ ><I IJR =@ >JHKDG@?, DO =MDIBN < GDHDO<ODJI: "Q@I DA OC@ API>ODJI DN ><GG@?
RDOC < NGD>@ JA ?GF34>7 @G@H@ION, OC@ M@OPMI@? NGD>@ @I?N PK >JINDNODIB JA DPOTU @G@H@ION. 1J
N@@ CJR GDHDODIB OCDN RJPG? =@, G@O'N GJJF <O OC@ AJGGJRDIB >J?@ OC<O DN OMTDIB OJ HJ?DAT OC@
@G@H@ION JA < NGD>@ JOC@M OC<I OC@ JI@N <O OC@ AMJIO <I? <O OC@ =<>F:
JOU<> OVNCFST = < 5, 6, 7, 8, 9 >;
JOU<> NJEEMF = USJNNFE(OVNCFST); // j DPNQJMBUJPO &3303
NJEEMF<> *= 10;
&O RJPG? =@ @SK@>O@? OC<O OC@ M@OPMI@? NGD>@ JA OTK@ DPOTU(JOU)<> ><IIJO =@ <NNDBI@? OJ <
NGD>@ JA OTK@ JOU<>:
&SSPS: DBOOPU JNQMJDJUMZ DPOWFSU FYQSFTTJPO (USJNNFE(OVNCFST))
PG UZQF DPOTU(JOU)<> UP JOU<>
FG@5F;A@ &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
176
%JR@Q@M, NDI>@ OC@ JMDBDI<G NGD>@ DN JA HPO<=G@ @G@H@ION OJ =@BDI RDOC, OCDN GDHDO<ODJI ><I =@
N@@I <N <MODAD>D<G <I? PIAJMOPI<O@. JOPVU NJGQ@N OCDN HPO<=DGDOT KMJ=G@H <=JPO K<M<H@O@MN
<I? M@OPMI Q<GP@N. &O DN NK@>DAD@? =JOC JI OC@ K<M<H@O@M <I? JI OC@ M@OPMI OTK@ <I? ><MMD@N
OC@ HPO<=DGDOT JA OC@ AJMH@M OJ OC@ G<OO@M:
JOPVU(JOU)<> USJNNFE(JOPVU(JOU)<> TMJDF)
\
// ...
SFUVSO TMJDF;
^
4DOC OC<O >C<IB@, OC@ N<H@ API>ODJI ><I IJR =@ ><GG@? RDOC ?GF34>7, DPOTU, <I? JNNVUBCMF
NGD>@N:
\
JOU<> OVNCFST = < 5, 6, 7, 8, 9 >;
// 5IF SFUVSO UZQF JT TMJDF PG NVUBCMF FMFNFOUT
JOU<> NJEEMF = USJNNFE(OVNCFST);
NJEEMF<> *= 10;
XSJUFMO(NJEEMF);
^
\
JNNVUBCMF JOU<> OVNCFST = < 10, 11, 12 >;
// 5IF SFUVSO UZQF JT TMJDF PG JNNVUBCMF FMFNFOUT
JNNVUBCMF JOU<> NJEEMF = USJNNFE(OVNCFST);
XSJUFMO(NJEEMF);
^
\
DPOTU JOU<> OVNCFST = < 13, 14, 15, 16 >;
// 5IF SFUVSO UZQF JT TMJDF PG DPOTU FMFNFOUT
DPOTU JOU<> NJEEMF = USJNNFE(OVNCFST);
XSJUFMO(NJEEMF);
^
MB[Z 37.3.7
&O DN I<OPM<G OJ @SK@>O OC<O <MBPH@ION <M@ @Q<GP<O@? 478AD7 @IO@MDIB API>ODJIN OC<O PN@ OCJN@
<MBPH@ION. #JM @S<HKG@, OC@ API>ODJI BEE() =@GJR DN ><GG@? RDOC OC@ M@OPMI Q<GP@N JA ORJ
JOC@M API>ODJIN:
SFTVMU = BEE(BO"NPVOU(), BOPUIFS"NPVOU());
&I JM?@M AJM BEE() OJ =@ ><GG@?, ADMNO BO"NPVOU() <I? BOPUIFS"NPVOU() HPNO =@ ><GG@?.
,OC@MRDN@, OC@ Q<GP@N OC<O BEE() I@@?N RJPG? IJO =@ <Q<DG<=G@.
"Q<GP<ODIB <MBPH@ION =@AJM@ ><GGDIB < API>ODJI DN 7397D.
%JR@Q@M, NJH@ K<M<H@O@MN H<T IJO B@O < >C<I>@ OJ =@ PN@? DI OC@ API>ODJI <O <GG ?@K@I?DIB
JI >@MO<DI >JI?DODJIN. &I NP>C ><N@N, OC@ @<B@M @Q<GPODJIN JA OC@ <MBPH@ION RJPG? =@
R<NO@APG.
)@O'N GJJF <O NP>C < KMJBM<H OC<O PN@N JI@ JA DON K<M<H@O@MN JIGT RC@I I@@?@?. 1C@ AJGGJRDIB
API>ODJI OMD@N OJ B@O OC@ M@LPDM@? IPH=@M JA @BBN ADMNO AMJH OC@ M@AMDB@M<OJM. 4C@I OC@M@ DN
NPAAD>D@IO IPH=@M JA @BBN DI OC@M@, DO ?J@NI'O I@@? OJ FIJR OC@ IPH=@M JA @BBN OC<O OC@
I@DBC=JMN C<Q@:
WPJE NBLF0NFMFU(JO JOU SFRVJSFE&HHT,
JO JOU FHHT*O'SJEHF,
JO JOU FHHT"U/FJHICPST)
\
FG@5F;A@ &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
177
XSJUFGMO("/FFE UP NBLF B %T-FHH PNFMFU", SFRVJSFE&HHT);
JG (SFRVJSFE&HHT <= FHHT*O'SJEHF) \
XSJUFMO("5BLF BMM PG UIF FHHT GSPN UIF GSJEHF");
^ FMTF JG (SFRVJSFE&HHT <= (FHHT*O'SJEHF + FHHT"U/FJHICPST)) \
XSJUFGMO("5BLF %T FHHT GSPN UIF GSJEHF"
" BOE %T FHHT GSPN UIF OFJHICPST",
FHHT*O'SJEHF, SFRVJSFE&HHT - FHHT*O'SJEHF);
^ FMTF \
XSJUFGMO("$BOOPU NBLF B %T-PNFMFU", SFRVJSFE&HHT);
^
^
A??DODJI<GGT, G@O'N <NNPH@ OC<O OC@M@ DN < API>ODJI OC<O ><G>PG<O@N <I? M@OPMIN OC@ OJO<G IPH=@M
JA @BBN <Q<DG<=G@ <O OC@ I@DBC=JMN. #JM ?@HJINOM<ODJI KPMKJN@N, OC@ API>ODJI KMDION NJH@
DIAJMH<ODJI <N R@GG:
JOU DPVOU&HHT(JO JOU<TUSJOH> BWBJMBCMF&HHT)
\
JOU SFTVMU;
GPSFBDI (OFJHICPS, DPVOU; BWBJMBCMF&HHT) \
XSJUFMO(OFJHICPS, ": ", DPVOU, " FHHT");
SFTVMU += DPVOU;
^
XSJUFGMO("" UPUBM PG %T FHHT BWBJMBCMF BU UIF OFJHICPST",
SFTVMU);
SFUVSO SFTVMU;
^
1C<O API>ODJI DO@M<O@N JQ@M OC@ @G@H@ION JA <I <NNJ>D<ODQ@ <MM<T <I? <??N PK <GG JA OC@ @BB
>JPION.
1C@ NBLF0NFMFU() API>ODJI ><I =@ ><GG@? RDOC OC@ M@OPMI Q<GP@ JA DPVOU&HHT() <N DI OC@
AJGGJRDIB KMJBM<H:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU<TUSJOH> BU/FJHCPST = < "+BOF":5, "+JN":3, "#JMM":7 >;
NBLF0NFMFU(2, 5, DPVOU&HHT(BU/FJHCPST));
^
AN N@@I DI OC@ JPOKPO JA OC@ KMJBM<H, ADMNO DPVOU&HHT() API>ODJI DN @S@>PO@? <I? OC@I
NBLF0NFMFU() DN ><GG@?:
+BOF: 5 FHHT n
#JMM: 7 FHHT o DPVOUJOH UIF FHHT BU UIF OFJHICPST
+JN: 3 FHHT p
" UPUBM PG 15 FHHT BWBJMBCMF BU UIF OFJHICPST
/FFE UP NBLF B 2-FHH PNFMFU
5BLF BMM PG UIF FHHT GSPN UIF GSJEHF
AGOCJPBC DO DN KJNND=G@ OJ H<F@ OC@ ORJ-@BB JH@G@O RDOC JIGT OC@ @BBN DI OC@ AMD?B@, OC@ @BBN
<O OC@ I@DBC=JMN C<Q@ <GM@<?T =@@I >JPIO@? @<B@MGT.
FG@5F;A@ &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
178
1C@ MB[Z F@TRJM? NK@>DAD@N OC<O <I @SKM@NNDJI OC<O C<N =@@I K<NN@? OJ < API>ODJI <N <
K<M<H@O@M RDGG =@ @Q<GP<O@? JIGT DA <I? RC@I I@@?@?:
WPJE NBLF0NFMFU(JO JOU SFRVJSFE&HHT,
JO JOU FHHT*O'SJEHF,
MB[Z JOU FHHT"U/FJHICPST)
\
// ... UIF CPEZ PG UIF GVODUJPO JT UIF TBNF BT CFGPSF ...
^
AN N@@I DI OC@ I@R JPOKPO, RC@I OC@ IPH=@M JA @BBN DI OC@ AMD?B@ N<ODNAT OC@ M@LPDM@? @BBN,
OC@ >JPIODIB JA OC@ @BBN <O OC@ I@DBC=JMN ?J@N IJO C<KK@I <ITHJM@:
/FFE UP NBLF B 2-FHH PNFMFU
5BLF BMM PG UIF FHHT GSPN UIF GSJEHF
1C<O >JPIO RJPG? NODGG C<KK@I DA I@@?@?. #JM @S<HKG@, G@O'N O<F@ < GJJF <O < ><N@ RC@M@ OC@
M@LPDM@? @BBN <M@ HJM@ OC<I OC@ @BBN DI OC@ AMD?B@:
NBLF0NFMFU(9, 5, DPVOU&HHT(BU/FJHCPST));
1CDN ODH@ OC@ OJO<G IPH=@M JA @BBN <O OC@ I@DBC=JMN DN M@<GGT I@@?@?:
/FFE UP NBLF B 9-FHH PNFMFU
+BOF: 5 FHHT
#JMM: 7 FHHT
+JN: 3 FHHT
" UPUBM PG 15 FHHT BWBJMBCMF BU UIF OFJHICPST
5BLF 5 FHHT GSPN UIF GSJEHF BOE 4 FHHT GSPN UIF OFJHICPST
1C@ Q<GP@N JA MB[Z K<M<H@O@MN <M@ @Q<GP<O@? @Q@MT ODH@ OC<O OC@T <M@ PN@? DI OC@ API>ODJI.
#JM @S<HKG@, =@><PN@ OC@ MB[Z K<M<H@O@M JA OC@ AJGGJRDIB API>ODJI DN PN@? OCM@@ ODH@N DI OC@
API>ODJI, OC@ @SKM@NNDJI OC<O KMJQD?@N DON Q<GP@ DN @Q<GP<O@? OCM@@ ODH@N:
JNQPSU TUE.TUEJP;
JOU WBMVF0G"SHVNFOU()
\
XSJUFMO("$BMDVMBUJOH...");
SFUVSO 1;
^
WPJE GVODUJPO8JUI-B[Z1BSBNFUFS(MB[Z JOU WBMVF)
\
JOU SFTVMU = WBMVF + WBMVF + WBMVF;
XSJUFMO(SFTVMU);
^
WPJE NBJO()
\
GVODUJPO8JUI-B[Z1BSBNFUFS(WBMVF0G"SHVNFOU());
^
1C@ JPOKPO
FG@5F;A@ &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
179
$BMDVMBUJOH
$BMDVMBUJOH
$BMDVMBUJOH
3
TDPQF 37.3.8
1CDN F@TRJM? NK@>DAD@N OC<O < K<M<H@O@M RDGG IJO =@ PN@? =@TJI? OC@ N>JK@ JA OC@ API>ODJI:
JOU<> HMPCBM4MJDF;
JOU<> GPP(TDPQF JOU<> QBSBNFUFS)
\
HMPCBM4MJDF = QBSBNFUFS; // j DPNQJMBUJPO &3303
SFUVSO QBSBNFUFS; // j DPNQJMBUJPO &3303
^
WPJE NBJO()
\
JOU<> TMJDF = < 10, 20 >;
JOU<> SFTVMU = GPP(TMJDF);
^
1C<O API>ODJI =M@<F@N OC@ KMJHDN@ JA TDPQF DI ORJ KG<>@N: &O <NNDBIN OC@ K<M<H@O@M OJ <
BGJ=<G Q<MD<=G@ <I? M@OPMIN DO. BJOC JA OCJN@ RJPG? H<F@ DO KJNND=G@ AJM OC@ K<M<H@O@M OJ =@
<>>@NN@? <AO@M OC@ API>ODJI ADIDNC@N.
(Note: 6?6 2.062, F:7 5A?B;>7D F:3F :3E 477@ GE76 I:7@ 5A?B;>;@9 F:7 7J3?B>7E ;@ F:;E
5:3BF7D 6A7E @AF EGBBADF F:7 scope =7KIAD6. )
TIBSFE 37.3.9
1CDN F@TRJM? M@LPDM@N OC<O OC@ K<M<H@O@M DN NC<M<=G@ =@OR@@I OCM@<?N:
WPJE GPP(TIBSFE JOU<> J)
\
// ...
^
WPJE NBJO()
\
JOU<> OVNCFST = < 10, 20 >;
GPP(OVNCFST); // j DPNQJMBUJPO &3303
^
1C@ KMJBM<H <=JQ@ ><IIJO =@ >JHKDG@? =@><PN@ OC@ <MBPH@IO DN IJO TIBSFE. 1C@ KMJBM<H
><I =@ >JHKDG@? RDOC OC@ AJGGJRDIB >C<IB@:
TIBSFE JOU<> OVNCFST = < 10, 20 >;
GPP(OVNCFST); // OPX DPNQJMFT
4@ RDGG PN@ OC@ TIBSFE F@TRJM? G<O@M DI OC@ !<O< 0C<MDIB JI>PMM@I>T >C<KO@M.
Summary
37.4
t &3D3?7F7D DN RC<O OC@ API>ODJI O<F@N AMJH DON ><GG@M OJ <>>JHKGDNC DON O<NF.
t AD9G?7@F DN <I @SKM@NNDJI (@.B. < Q<MD<=G@) OC<O DN K<NN@? OJ < API>ODJI <N < K<M<H@O@M.
t AMBPH@ION JA Q<GP@ OTK@N <M@ K<NN@? =T->JKT; <MBPH@ION JA M@A@M@I>@ OTK@N <M@ K<NN@?
=T-M@A@M@I>@. (4@ RDGG N@@ OCDN OJKD> <B<DI DI G<O@M >C<KO@MN.)
FG@5F;A@ &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
180
t JO NK@>DAD@N OC<O OC@ K<M<H@O@M DN JIGT AJM ?<O< DIKPO.
t PVU NK@>DAD@N OC<O OC@ K<M<H@O@M DN JIGT AJM ?<O< JPOKPO.
t SFG NK@>DAD@N OC<O OC@ K<M<H@O@M DN AJM ?<O< DIKPO <I? ?<O< JPOKPO.
t DPOTU BP<M<IO@@N OC<O OC@ K<M<H@O@M DN IJO HJ?DAD@? DIND?@ OC@ API>ODJI.
t JNNVUBCMF M@LPDM@N OC<O OC@ <MBPH@IO DN JNNVUBCMF.
t JOPVU <KK@<MN =JOC <O OC@ K<M<H@O@M <I? OC@ M@OPMI OTK@, <I? OM<INA@MN OC@ ?GF34;>;FK JA
OC@ K<M<H@O@M OJ OC@ M@OPMI OTK@.
t MB[Z @Q<GP<O@N OC@ K<M<H@O@M RC@I (<I? @Q@MT ODH@) DON Q<GP@ DN <>OP<GGT PN@?.
t TDPQF BP<M<IO@@N OC<O IJ M@A@M@I>@ OJ OC@ K<M<H@O@M RDGG =@ G@<F@? AMJH OC@ API>ODJI.
t TIBSFE M@LPDM@N OC<O OC@ K<M<H@O@M DN TIBSFE.
Exercise
37.5
t 1C@ AJGGJRDIB KMJBM<H DN OMTDIB OJ NR<K OC@ Q<GP@N JA ORJ <MBPH@ION:
JNQPSU TUE.TUEJP;
WPJE TXBQ(JOU GJSTU, JOU TFDPOE)
\
JOU UFNQ = GJSTU;
GJSTU = TFDPOE;
TFDPOE = UFNQ;
^
WPJE NBJO()
\
JOU B = 1;
JOU C = 2;
TXBQ(B, C);
XSJUFMO(B, ' ', C);
^
1C@ KMJBM<H ?J@N IJO C<Q@ <IT @AA@>O JI B JM C:
1 2 l OPU TXBQQFE
#DS OC@ API>ODJI NJ OC<O OC@ Q<GP@N JA B <I? C <M@ NR<KK@?.
... F:7 EA>GF;A@
FG@5F;A@ &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
181
Lazy Operators 38
)<UT @Q<GP<ODJIN DN <=JPO ?@G<TDIB @S@>PODJIN JA @SKM@NNDJIN PIODG OC@ M@NPGON JA OCJN@
@SKM@NNDJIN <M@ I@@?@?. )<UT @Q<GP<ODJIN <M@ <HJIB OC@ API?<H@IO<G A@<OPM@N JA NJH@ JA OC@
KMJBM<HHDIB G<IBP<B@N.
+<OPM<GGT, ?@G<TDIB @S@>PODJIN JA @SKM@NNDJIN PIODG OC@DM M@NPGON <M@ M@<GGT I@>@NN<MT H<T
H<F@ KMJBM<HN MPI A<NO@M.
A >JI>@KO OC<O DN NDHDG<M OJ G<UT @Q<GP<ODJIN DN OC@ NCJMO>PO =@C<QDJM JA OCM@@ JK@M<OJMN:
t ]] (AD) JK@M<OJM: 1C@ N@>JI? @SKM@NNDJI DN @Q<GP<O@? JIGT DA OC@ ADMNO @SKM@NNDJI DN GBMTF.
JG (BO&YQSFTTJPO() ]] NBZ/PU#F&WBMVBUFE()) \
// ...
^
&A OC@ M@NPGO JA BO&YQSFTTJPO() DN USVF, OC@ M@NPGO JA OC@ ]] @SKM@NNDJI DN <GNJ USVF
<I? OC@ N@>JI? @SKM@NNDJI DN IJO @Q<GP<O@?.
t && (3@6) JK@M<OJM: 1C@ N@>JI? @SKM@NNDJI DN @Q<GP<O@? JIGT DA OC@ ADMNO @SKM@NNDJI DN USVF.
JG (BO&YQSFTTJPO() && NBZ/PU#F&WBMVBUFE()) \
// ...
^
&A OC@ M@NPGO JA BO&YQSFTTJPO() DN GBMTF, OC@ M@NPGO JA OC@ && @SKM@NNDJI DN <GNJ
GBMTF <I? OC@ N@>JI? @SKM@NNDJI DN IJO @Q<GP<O@?.
t : (F7D@3DK) JK@M<OJM: "DOC@M OC@ ADMNO JM OC@ N@>JI? @SKM@NNDJI DN @Q<GP<O@? ?@K@I?DIB JI
RC@OC@M OC@ >JI?DODJI DN USVF JM GBMTF, M@NK@>ODQ@GT.
JOU J = DPOEJUJPO() FJUIFS5IJT() : PS5IJT();
1C@ G<UDI@NN JA OC@N@ JK@M<OJMN DN IJO JIGT <=JPO K@MAJMH<I>@. 0JH@ODH@N, @Q<GP<ODIB JI@ JA
OC@ @SKM@NNDJIN ><I =@ <I @MMJM.
#JM @S<HKG@, OC@ ;8 F:7 8;DEF >7FF7D ;E A >JI?DODJI >C@>F =@GJR RJPG? =@ <I @MMJM RC@I OC@
NOMDIB DN @HKOT:
ETUSJOH T;
// ...
JG (T<0> == '"') \
// ...
^
&I JM?@M OJ <>>@NN OC@ U@MJ-DI?@S@? @G@H@IO JA T, DO HPNO ADMNO =@ @INPM@? OC<O OC@ NOMDIB ?J@N
C<Q@ NP>C <I @G@H@IO. #JM OC<O M@<NJI, OC@ AJGGJRDIB >JI?DODJI >C@>F HJQ@N OC<O KJO@IOD<GGT
@MMJI@JPN GJBD><G @SKM@NNDJI OJ OC@ MDBCO-C<I? ND?@ JA OC@ && JK@M<OJM OJ @INPM@ OC<O DO RDGG =@
@Q<GP<O@? JIGT RC@I DO DN N<A@ OJ ?J NJ:
ETUSJOH T;
// ...
JG ((T.MFOHUI >= 1) && (T<0> == '"')) \
// ...
^
)<UT @Q<GP<ODJIN ><I =@ <>CD@Q@? =T API>ODJI KJDIO@MN, ?@G@B<O@N, <I? M<IB@N <N R@GG. 4@ RDGG
N@@ OC@N@ DI G<O@M >C<KO@M.
"3LK %B7D3FADE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
182
Program Environment 39
4@ C<Q@ N@@I OC<O NBJO() DN < API>ODJI. -MJBM<H @S@>PODJI NO<MON RDOC NBJO() <I? =M<I>C@N
OJ JOC@M API>ODJIN AMJH OC@M@. 1C@ ?@ADIDODJI JA NBJO() OC<O R@ C<Q@ PN@? NJ A<M C<N =@@I OC@
AJGGJRDIB:
WPJE NBJO()
A>>JM?DIB OJ OC<O ?@ADIDODJI NBJO() ?J@N IJO O<F@ <IT K<M<H@O@MN <I? ?J@N IJO M@OPMI <
Q<GP@. &O DN <>OP<GGT IJO KJNND=G@ OJ IJO M@OPMI < Q<GP@, =@><PN@ @IQDMJIH@ION OC<O NO<MO
KMJBM<HN @SK@>O M@OPMI Q<GP@N AMJH OC@H. AGOCJPBC DO DN KJNND=G@ OJ NK@>DAT OC@ M@OPMI OTK@ JA
NBJO() <N WPJE, DO ?J@N DI?@@? M@OPMI < Q<GP@.
The return value of NBJO()
39.1
-MJBM<HN <M@ <GR<TN NO<MO@? =T <I @IODOT DI < K<MOD>PG<M @IQDMJIH@IO. 1C@ @IODOT OC<O NO<MON
OC@ KMJBM<H H<T =@ OC@ NC@GG RC@M@ OC@ PN@M OTK@N OC@ I<H@ JA OC@ KMJBM<H <I? KM@NN@N OC@
"IO@M F@T, JM DO H<T =@ < ?@Q@GJKH@IO @IQDMJIH@IO RC@M@ OC@ KMJBM<HH@M >GD>FN OC@ 8/PI:
=POOJI, @O>.
1C@ KMJBM<H >JHHPID><O@N DON @SDO NO<OPN OJ DON @IQDMJIH@IO =T OC@ M@OPMI Q<GP@ JA NBJO().
A M@OPMI Q<GP@ JA 0 H@<IN NP>>@NNAPG >JHKG@ODJI <I? <IT JOC@M Q<GP@ H@<IN NJH@ OTK@ JA
A<DGPM@. AGOCJPBC OC@ M@OPMI Q<GP@ DN PK OJ OC@ KMJBM<HH@M, =T >JIQ@IODJI OC@ Q<GP@ 0 <GR<TN
H@<IN NP>>@NN.
Note: %@>K H3>G7E ;@ F:7 D3@97 00,1271 3D7 BADF34>7; @AF 3>> 7@H;DA@?7@FE EGBBADF AF:7D
H3>G7E.
3<GP@N JOC@M OC<I 0 ><I C<Q@ ?DAA@M@IO H@<IDIBN ?@K@I?DIB JI @<>C KMJBM<H. #JM @S<HKG@,
OC@ >JHHJI 2IDS KMJBM<H MT, RCD>C DN PN@? AJM GDNODIB >JIO@ION JA ?DM@>OJMD@N, M@OPMIN 1 AJM
HDIJM @MMJMN <I? 2 AJM N@MDJPN JI@N. &I H<IT @IQDMJIH@ION, OC@ M@OPMI Q<GP@ JA OC@ KMJBM<H
OC<O C<N =@@I @S@>PO@? HJNO M@>@IOGT DI OC@ >JINJG@ ><I =@ N@@I OCMJPBC OC@ $ @IQDMJIH@IO
Q<MD<=G@. #JM @S<HKG@, RC@I R@ <NF MT OJ GDNO < ADG@ OC<O ?J@N IJO @SDNO, DON IJI-U@MJ M@OPMI
Q<GP@ ><I =@ J=N@MQ@? RDOC $ <N N@@I =@GJR.
Note: !@ F:7 5A??3@6 >;@7 ;@F7D35F;A@E 47>AI, F:7 >;@7E F:3F EF3DF I;F: # ;@6;53F7 F:7 >;@7E
F:3F F:7 GE7D FKB7E. !8 KAG I3@F FA FDK F:7 E3?7 5A??3@6E, KAG ?GEF 7@F7D F:7 5A@F7@FE A8
F:AE7 >;@7E 7J57BF F:7 # 5:3D35F7D. A>EA, F:7 5A??3@6E 47>AI EF3DF 3 BDA9D3? @3?76
deneme; D7B>357 F:3F @3?7 I;F: F:7 @3?7 A8 KAGD F7EF BDA9D3?.
A66;F;A@3>>K, 3>F:AG9: F:7 8A>>AI;@9 7J3?B>7E E:AI ;@F7D35F;A@E ;@ 3 ";@GJ 5A@EA>7, F:7K
IAG>6 47 E;?;>3D 4GF @AF 7J35F>K F:7 E3?7 ;@ 5A@EA>7E ;@ AF:7D AB7D3F;@9 EKEF7?E.
# MT B@GJMF@UIBU@EPFT@OPU@FYJTU
MT: DBOOPU BDDFTT B@GJMF@UIBU@EPFT@OPU@FYJTU: /P TVDI GJMF PS EJSFDUPSZ
# FDIP $
2 l UIF SFUVSO WBMVF PG MT
NBJO() always returns a value 39.1.1
0JH@ JA OC@ KMJBM<HN OC<O R@ C<Q@ RMDOO@I NJ A<M C<Q@ OCMJRI @S>@KODJIN RC@I OC@T >JPG?
IJO >JIODIP@ RDOC OC@DM O<NFN. AN HP>C <N R@ C<Q@ N@@I NJ A<M, RC@I <I @S>@KODJI DN OCMJRI,
OC@ KMJBM<H @I?N RDOC <I PCKFDU.&YDFQUJPO @MMJM H@NN<B@.
&DA9D3? E@H;DA@?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
183
4C@I OC<O C<KK@IN, @Q@I DA NBJO() C<N =@@I ?@ADI@? <N M@OPMIDIB WPJE, < Q<GP@ JA 1 DN
<POJH<OD><GGT M@OPMI@? OJ OC@ KMJBM<H'N @IQDMJIH@IO. )@O'N N@@ OCDN DI <>ODJI =T OC@ AJGGJRDIB
NDHKG@ KMJBM<H OC<O O@MHDI<O@N RDOC <I @S>@KODJI:
WPJE NBJO()
\
UISPX OFX &YDFQUJPO("5IFSF IBT CFFO BO FSSPS.");
^
AGOCJPBC OC@ M@OPMI OTK@ DN NK@>DAD@? <N WPJE, OC@ M@OPMI Q<GP@ DN 1:
# ./EFOFNF
PCKFDU.&YDFQUJPO: 5IFSF IBT CFFO BO FSSPS.
...
# FDIP $
1
0DHDG<MGT, WPJE NBJO() API>ODJIN OC<O O@MHDI<O@ NP>>@NNAPGGT <GNJ <POJH<OD><GGT M@OPMI 0 <N
OC@DM M@OPMI Q<GP@N. )@O'N N@@ OCDN RDOC OC@ AJGGJRDIB KMJBM<H OC<O O@MHDI<O@N EG557EE8G>>K:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFMO("%POF!");
^
1C@ KMJBM<H M@OPMIN 0:
# ./EFOFNF
%POF!
# FDIP $
0
Specifying the return value 39.1.2
/@OPMIDIB < NK@>DAD> Q<GP@ AMJH NBJO() DN OC@ N<H@ <N M@OPMIDIB < Q<GP@ AMJH <IT JOC@M
API>ODJI. 1C@ M@OPMI OTK@ HPNO =@ NK@>DAD@? <N JOU <I? OC@ Q<GP@ HPNO =@ M@OPMI@? =T OC@
SFUVSO NO<O@H@IO:
JNQPSU TUE.TUEJP;
JOU NBJO()
\
JOU OVNCFS;
XSJUF("1MFBTF FOUFS B OVNCFS CFUXFFO 3 BOE 6: ");
SFBEG(" %T", &OVNCFS);
JG ((OVNCFS < 3) ]] (OVNCFS > 6)) \
TUEFSS.XSJUFGMO("&3303: %T JT OPU WBMJE!", OVNCFS);
SFUVSO 111;
^
XSJUFGMO("5IBOL ZPV GPS %T.", OVNCFS);
SFUVSO 0;
^
4C@I OC@ @IO@M@? IPH=@M DN RDOCDI OC@ Q<GD? M<IB@, OC@ M@OPMI Q<GP@ JA OC@ KMJBM<H DN 0:
&DA9D3? E@H;DA@?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
184
# ./EFOFNF
1MFBTF FOUFS B OVNCFS CFUXFFO 3 BOE 6: 5
5IBOL ZPV GPS 5.
# FDIP $
0
4C@I OC@ IPH=@M DN JPOND?@ JA OC@ Q<GD? M<IB@, OC@ M@OPMI Q<GP@ JA OC@ KMJBM<H DN 111:
# ./EFOFNF
1MFBTF FOUFS B OVNCFS CFUXFFO 3 BOE 6: 10
&3303: 10 JT OPU WBMJE!
# FDIP $
111
1C@ Q<GP@ JA 111 DI OC@ <=JQ@ KMJBM<H DN <M=DOM<MT; IJMH<GGT 1 DN NPDO<=G@ <N OC@ A<DGPM@ >J?@.
Standard error stream TUEFSS
39.2
1C@ KMJBM<H <=JQ@ PN@N OC@ NOM@<H TUEFSS. 1C<O NOM@<H DN OC@ OCDM? JA OC@ NO<I?<M? NOM@<HN.
&O DN PN@? AJM RMDODIB @MMJM H@NN<B@N:
t TUEJO: NO<I?<M? DIKPO NOM@<H
t TUEPVU: NO<I?<M? JPOKPO NOM@<H
t TUEFSS: NO<I?<M? @MMJM NOM@<H
4C@I < KMJBM<H DN NO<MO@? DI < >JINJG@, IJMH<GGT OC@ H@NN<B@N OC<O <M@ RMDOO@I OJ TUEPVU
<I? TUEFSS =JOC <KK@<M JI OC@ >JINJG@. 4C@I I@@?@?, DO DN KJNND=G@ OJ M@?DM@>O OC@N@ JPOKPON
DI?DQD?P<GGT. 1CDN NP=E@>O DN JPOND?@ JA OC@ AJ>PN JA OCDN >C<KO@M <I? OC@ ?@O<DGN H<T Q<MT AJM
@<>C NC@GG KMJBM<H.
Parameters of NBJO()
39.3
&O DN >JHHJI AJM KMJBM<HN OJ O<F@ K<M<H@O@MN AMJH OC@ @IQDMJIH@IO OC<O NO<MO@? OC@H. #JM
@S<HKG@, R@ C<Q@ <GM@<?T K<NN@? < ADG@ I<H@ <N < >JHH<I? GDI@ JKODJI OJ MT <=JQ@. 1C@M@
<M@ ORJ >JHH<I? GDI@ JKODJIN DI OC@ AJGGJRDIB GDI@:
# MT -M EFOFNF
-SXYS-YS-Y 1 BDFISFMJ VTFST 460668 /PW 6 20:38 EFOFNF
1C@ N@O JA >JHH<I? GDI@ K<M<H@O@MN <I? OC@DM H@<IDIBN <M@ ?@ADI@? @IODM@GT =T OC@ KMJBM<H.
"Q@MT KMJBM<H ?J>PH@ION DON PN<B@, DI>GP?DIB RC<O @Q@MT K<M<H@O@M H@<IN.
1C@ <MBPH@ION OC<O <M@ PN@? RC@I NO<MODIB < ! KMJBM<H <M@ K<NN@? OJ OC<O KMJBM<H'N
NBJO() <N < NGD>@ JA TUSJOHN. !@ADIDIB NBJO() <N O<FDIB < K<M<H@O@M JA OTK@ TUSJOH<> DN
NPAAD>D@IO OJ C<Q@ <>>@NN OJ KMJBM<H <MBPH@ION. 1C@ I<H@ JA OCDN K<M<H@O@M DN >JHHJIGT
<==M@QD<O@? <N BSHT. 1C@ AJGGJRDIB KMJBM<H KMDION <GG JA OC@ <MBPH@ION OC<O OC@ KMJBM<H C<N
=@@I NO<MO@? RDOC:
JNQPSU TUE.TUEJP;
WPJE NBJO(TUSJOH<> BSHT)
\
GPSFBDI (J, BSH; BSHT) \
XSJUFGMO(""SHVNFOU %-3T: %T", J, BSH);
&DA9D3? E@H;DA@?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
185
^
^
)@O'N NO<MO OC@ KMJBM<H RDOC <M=DOM<MT <MBPH@ION:
# ./EFOFNF TPNF BSHVNFOUT PO UIF DPNNBOE MJOF 42 --BO-PQUJPO
"SHVNFOU 0 : ./EFOFNF
"SHVNFOU 1 : TPNF
"SHVNFOU 2 : BSHVNFOUT
"SHVNFOU 3 : PO
"SHVNFOU 4 : UIF
"SHVNFOU 5 : DPNNBOE
"SHVNFOU 6 : MJOF
"SHVNFOU 7 : 42
"SHVNFOU 8 : --BO-PQUJPO
1C@ ADMNO <MBPH@IO DN <GR<TN OC@ I<H@ JA OC@ KMJBM<H, DI OC@ R<T DO C<N =@@I @IO@M@? =T OC@
PN@M. 1C@ JOC@M <MBPH@ION <KK@<M DI OC@ N<H@ JM?@M OC<O OC@T C<Q@ =@@I @IO@M@?.
&O DN >JHKG@O@GT PK OJ OC@ KMJBM<H CJR DO H<F@N PN@ JA OC@ <MBPH@ION. 1C@ AJGGJRDIB KMJBM<H
KMDION DON ORJ <MBPH@ION DI M@Q@MN@ JM?@M:
JNQPSU TUE.TUEJP;
JOU NBJO(TUSJOH<> BSHT)
\
JG (BSHT.MFOHUI != 3) \
TUEFSS.XSJUFGMO("&3303! $PSSFDU VTBHF:=O"
" %T XPSE1 XPSE2", BSHT<0>);
SFUVSO 1;
^
XSJUFMO(BSHT<2>, ' ', BSHT<1>);
SFUVSO 0;
^
1C@ KMJBM<H <GNJ NCJRN DON >JMM@>O PN<B@ DA OC@M@ <M@ IJO ORJ RJM?N @S<>OGT:
# ./EFOFNF
&3303! $PSSFDU VTBHF:
./EFOFNF XPSE1 XPSE2
# ./EFOFNF XPSME IFMMP
IFMMP XPSME
Command line options and the TUE.HFUPQU module
39.4
1C<O DN <GG OC@M@ DN OJ FIJR <=JPO OC@ K<M<H@O@MN <I? OC@ M@OPMI Q<GP@ JA NBJO(). %JR@Q@M,
K<MNDIB OC@ <MBPH@ION DN < M@K@ODODQ@ O<NF. 1C@ TUE.HFUPQU HJ?PG@ DN ?@NDBI@? OJ C@GK RDOC
K<MNDIB OC@ >JHH<I? GDI@ JKODJIN JA KMJBM<HN.
0JH@ K<M<H@O@MN GDF@ "RJMG?" <I? "C@GGJ" <=JQ@ <M@ KPM@GT ?<O< AJM OC@ KMJBM<H OJ PN@.
,OC@M FDI?N JA K<M<H@O@MN <M@ ><GG@? 5A??3@6 >;@7 ABF;A@E, <I? <M@ AJM >C<IBDIB =@C<QDJMN
JA KMJBM<HN. AI @S<HKG@ JA < >JHH<I? GDI@ JKODJI C<N =@@I -M OC<O C<N =@@I K<NN@? OJ MT
<=JQ@.
&DA9D3? E@H;DA@?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
186
JHH<I? GDI@ JKODJIN H<F@ KMJBM<HN HJM@ PN@APG =T M@HJQDIB OC@ I@@? AJM < CPH<I PN@M OJ
DIO@M<>O RDOC OC@ KMJBM<H OJ C<Q@ DO =@C<Q@ DI < >@MO<DI R<T. 4DOC >JHH<I? GDI@ JKODJIN,
KMJBM<HN ><I =@ NO<MO@? AMJH N>MDKO KMJBM<HN <I? OC@DM =@C<QDJMN ><I =@ NK@>DAD@? OCMJPBC
>JHH<I? GDI@ JKODJIN.
AGOCJPBC OC@ NTIO<S <I? H@<IDIBN JA >JHH<I? GDI@ <MBPH@ION JA @Q@MT KMJBM<H DN NK@>DAD>
OJ OC<O KMJBM<H, OC@DM AJMH<O DN NJH@RC<O NO<I?<M?. #JM @S<HKG@, DI -,0&5, >JHH<I? GDI@
JKODJIN NO<MO RDOC -- AJGGJR@? =T OC@ I<H@ JA OC@ JKODJI, <I? Q<GP@N >JH@ <AO@M = >C<M<>O@MN:
# ./EFOFNF --BO-PQUJPO=17
1C@ TUE.HFUPQU HJ?PG@ NDHKGDAD@N K<MNDIB NP>C JKODJIN. &O C<N HJM@ ><K<=DGDOD@N OC<I RC<O DN
>JQ@M@? DI OCDN N@>ODJI.
)@O'N ?@NDBI < KMJBM<H OC<O KMDION M<I?JH IPH=@MN. )@O'N O<F@ OC@ HDIDHPH, H<SDHPH, <I?
OJO<G IPH=@M JA OC@N@ IPH=@MN <N KMJBM<H <MBPH@ION. )@O'N @SK@>O OC@ AJGGJRDIB NTIO<S OJ
B@O OC@N@ Q<GP@N AMJH OC@ >JHH<I? GDI@:
# ./EFOFNF --DPVOU=7 --NJOJNVN=10 --NBYJNVN=15
1C@ HFUPQU() API>ODJI K<MN@N <I? <NNDBIN OCJN@ Q<GP@N OJ Q<MD<=G@N. 0DHDG<M OJ SFBEG(), OC@
<??M@NN@N JA Q<MD<=G@N HPNO =@ NK@>DAD@? =T OC@ & JK@M<OJM:
JNQPSU TUE.TUEJP;
JNQPSU TUE.HFUPQU;
JNQPSU TUE.SBOEPN;
WPJE NBJO(TUSJOH<> BSHT)
\
JOU DPVOU;
JOU NJOJNVN;
JOU NBYJNVN;
HFUPQU(BSHT,
"DPVOU", &DPVOU,
"NJOJNVN", &NJOJNVN,
"NBYJNVN", &NBYJNVN);
GPSFBDI (J; 0 .. DPVOU) \
XSJUF(VOJGPSN(NJOJNVN, NBYJNVN + 1), ' ');
^
XSJUFMO();
^
# ./EFOFNF --DPVOU=7 --NJOJNVN=10 --NBYJNVN=15
11 11 13 11 14 15 10
*JNO >JHH<I? GDI@ JKODJIN JA HJNO KMJBM<HN C<Q@ NCJMO@M NTIO<S <N R@GG. #JM @S<HKG@, -D
H<T C<Q@ OC@ N<H@ H@<IDIB <N --DPVOU. 0P>C <GO@MI<ODQ@ NTIO<S AJM @<>C JKODJI DN NK@>DAD@?
<AO@M < ] >C<M<>O@M. 1C@M@ H<T =@ HJM@ OC<I JI@ NCJMO>PO AJM @<>C JKODJI:
HFUPQU(BSHT,
"DPVOU]D", &DPVOU,
"NJOJNVN]O", &NJOJNVN,
"NBYJNVN]Y", &NBYJNVN);
&O DN >JHHJI OJ PN@ < NDIBG@ ?<NC AJM OC@ NCJMO Q@MNDJIN <I? OC@ = >C<M<>O@M DN PNP<GGT
JHDOO@?:
&DA9D3? E@H;DA@?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
187
# ./EFOFNF -D7 -O10 -Y15
11 13 10 15 14 15 14
HFUPQU() >JIQ@MON OC@ <MBPH@ION AMJH TUSJOH OJ OC@ OTK@ JA @<>C Q<MD<=G@. #JM @S<HKG@,
NDI>@ DPVOU <=JQ@ DN JOU, HFUPQU() >JIQ@MON OC@ Q<GP@ NK@>DAD@? AJM OC@ --DPVOU <MBPH@IO OJ
JOU. 4C@I I@@?@?, NP>C >JIQ@MNDJIN H<T <GNJ =@ K@MAJMH@? @SKGD>DOGT =T UP.
0J A<M R@ C<Q@ PN@? TUE.DPOW.UP JIGT RC@I >JIQ@MODIB OJ TUSJOH. UP ><I DI A<>O >JIQ@MO
AMJH <IT OTK@ OJ <IT OTK@ <N GJIB <N OC<O >JIQ@MNDJI DN KJNND=G@. #JM @S<HKG@, OC@ AJGGJRDIB
KMJBM<H O<F@N <?Q<IO<B@ JA UP RC@I >JIQ@MODIB DON <MBPH@IO OJ TJ[F@U:
JNQPSU TUE.TUEJP;
JNQPSU TUE.DPOW;
WPJE NBJO(TUSJOH<> BSHT)
\
// 5IF EFGBVMU DPVOU JT 10
TJ[F@U DPVOU = 10;
JG (BSHT.MFOHUI > 1) \
// 5IFSF JT BO BSHVNFOU
DPVOU = UP!TJ[F@U(BSHT<1>);
^
GPSFBDI (J; 0 .. DPVOU) \
XSJUF(J * 2, ' ');
^
XSJUFMO();
^
1C@ KMJBM<H KMJ?P>@N 10 IPH=@MN RC@I OC@M@ DN IJ <MBPH@IO NK@>DAD@?:
# ./EFOFNF
0 2 4 6 8 10 12 14 16 18
# ./EFOFNF 3
0 2 4
Environment variables
39.5
1C@ @IQDMJIH@IO OC<O < KMJBM<H DN NO<MO@? DI KMJQD?@N NJH@ Q<MD<=G@N OC<O OC@ KMJBM<H H<T
H<F@ PN@ JA. 1C@ @IQDMJIH@IO Q<MD<=G@N ><I =@ <>>@NN@? =T TUE.QSPDFTT.HFUFOW. 1C@
AJGGJRDIB KMJBM<H DN <>>@NNDIB <I? KMDIODIB OC@ 1"5) @IQDMJIH@IO Q<MD<=G@:
JNQPSU TUE.TUEJP;
JNQPSU TUE.QSPDFTT;
WPJE NBJO()
\
XSJUFMO(HFUFOW("1"5)"));
^
1C@ JPOKPO:
# ./EFOFNF
/VTS/MPDBM/CJO:/VTS/CJO:/IPNF/BDFISFMJ/ENE/MJOVY/CJO
TUE.QSPDFTT.FOWJSPONFOU KMJQD?@N <>>@NN OJ OC@ @IQDMJIH@IO Q<MD<=G@N OCMJPBC OC@
<NNJ>D<ODQ@ <MM<T NTIO<S:
&DA9D3? E@H;DA@?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
188
JNQPSU TUE.QSPDFTT;
// ...
XSJUFMO(FOWJSPONFOU<"1"5)">);
%JR@Q@M, FOWJSPONFOU DON@GA DN IJO <I <NNJ>D<ODQ@ <MM<T. 4C@I I@@?@?, OC@ @IQDMJIH@IO
Q<MD<=G@N ><I =@ >JIQ@MO@? OJ <I <NNJ>D<ODQ@ <MM<T =T UP""():
TUSJOH<TUSJOH> FOW7BST = FOWJSPONFOU.UP""();
Starting other programs
39.6
-MJBM<HN H<T NO<MO JOC@M KMJBM<HN <I? =@>JH@ OC@ 7@H;DA@?7@F AJM OCJN@ KMJBM<HN. 1RJ
API>ODJIN OC<O @I<=G@ OCDN <M@ TZTUFN <I? TIFMM AMJH OC@ TUE.QSPDFTT HJ?PG@.
TZTUFN @S@>PO@N DON K<M<H@O@M <N DA OC@ >JHH<I? C<N =@@I OTK@? <O OC@ >JINJG@. &O OC@I
M@OPMIN OC@ M@OPMI Q<GP@ JA OC<O >JHH<I?:
JNQPSU TUE.TUEJP;
JNQPSU TUE.QSPDFTT;
WPJE NBJO()
\
JNNVUBCMF SFU7BM = TZTUFN("MT -M EFOFNF");
XSJUFGMO("MT IBT SFUVSOFE %T.", SFU7BM);
^
1C@ JPOKPO DI>GP?@N OC@ JPOKPO JA MT AJGGJR@? =T OC@ GDI@ OC<O OCDN KMJBM<H KMDION:
# ./EFOFNF
-SXYS-YS-Y 1 BDFISFMJ VTFST 461107 /PW 7 17:33 EFOFNF
MT IBT SFUVSOFE 0.
TIFMM @S@>PO@N DON K<M<H@O@M DI < NC@GG @IQDMJIH@IO <I? M@OPMIN OC@ NO<I?<M? JPOKPO JA OC<O
>JHH<I?. &O OCMJRN <I @S>@KODJI DA OC@ KMJBM<H >JPG? IJO =@ NO<MO@? JM ADIDNC@? RDOC < IJI-
U@MJ @MMJM >J?@:
JNQPSU TUE.TUEJP;
JNQPSU TUE.QSPDFTT;
WPJE NBJO()
\
JNNVUBCMF PVUQVU = TIFMM("MT -M EFOFNF*");
XSJUFMO("5IF PVUQVU PG MT:");
XSJUFMO(PVUQVU);
^
1C@ JPOKPO:
# ./EFOFNF
5IF PVUQVU PG MT:
-SXYSXYS-Y 1 BDFISFMJ BDFISFMJ 923117 2012-03-25 11:19 EFOFNF
-SXYS-YS-Y 1 BDFISFMJ BDFISFMJ 1091303 2012-03-25 11:19 EFOFNF.E
&DA9D3? E@H;DA@?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
189
Summary
39.7
t "Q@I RC@I DO DN ?@ADI@? RDOC M@OPMI OTK@ WPJE, NBJO() <POJH<OD><GGT M@OPMIN 0 AJM NP>>@NN
<I? 1 AJM A<DGPM@.
t TUEFSS DN NPDO<=G@ OJ KMDIO @MMJM H@NN<B@N.
t NBJO ><I O<F@ K<M<H@O@MN <N TUSJOH<>.
t TUE.HFUPQU C@GKN RDOC K<MNDIB >JHH<I? GDI@ JKODJIN.
t TUE.QSPDFTT C@GKN RDOC <>>@NNDIB @IQDMJIH@IO Q<MD<=G@N <I? NO<MODIB JOC@M KMJBM<HN.
Exercises
39.8
1. 4MDO@ < ><G>PG<OJM KMJBM<H OC<O O<F@N <I JK@M<OJM <I? ORJ JK@M<I?N <N >JHH<I? GDI@
<MBPH@ION. %<Q@ OC@ KMJBM<H NPKKJMO OC@ AJGGJRDIB PN<B@:
# ./EFOFNF 3.4 Y 7.8
26.52
Note: B753GE7 F:7 * 5:3D35F7D :3E 3 EB75;3> ?73@;@9 A@ ?AEF 5A@EA>7E, ! :3H7 GE76
x. .AG ?3K EF;>> GE7 * 3E >A@9 3E ;F ;E 7E53B76 3E \*.
2. 4MDO@ < KMJBM<H OC<O <NFN OC@ PN@M RCD>C KMJBM<H OJ NO<MO, NO<MON OC<O KMJBM<H, <I?
M@OPMIN DON M@OPMI Q<GP@.
... F:7 EA>GF;A@E
&DA9D3? E@H;DA@?7@F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
190
Exceptions 40
2I@SK@>O@? NDOP<ODJIN <M@ K<MON JA KMJBM<HN: PN@M HDNO<F@N, KMJBM<HHDIB @MMJMN, >C<IB@N DI
OC@ KMJBM<H @IQDMJIH@IO, @O>. -MJBM<HN HPNO =@ RMDOO@I DI R<TN OJ <QJD? KMJ?P>DIB
DI>JMM@>O M@NPGON RC@I A<>@? RDOC NP>C 7J57BF;A@3> >JI?DODJIN.
0JH@ JA OC@N@ >JI?DODJIN H<T =@ N@Q@M@ @IJPBC OJ NOJK OC@ @S@>PODJI JA OC@ KMJBM<H. #JM
@S<HKG@, < M@LPDM@? KD@>@ JA DIAJMH<ODJI H<T =@ HDNNDIB JM DIQ<GD?, JM < ?@QD>@ H<T IJO =@
API>ODJIDIB >JMM@>OGT. 1C@ @S>@KODJI C<I?GDIB H@>C<IDNH JA ! C@GKN RDOC NOJKKDIB KMJBM<H
@S@>PODJI RC@I I@>@NN<MT, <I? OJ M@>JQ@M AMJH OC@ PI@SK@>O@? NDOP<ODJIN RC@I KJNND=G@.
AN <I @S<HKG@ JA < N@Q@M@ >JI?DODJI, R@ ><I OCDIF JA K<NNDIB <I PIFIJRI JK@M<OJM OJ <
API>ODJI OC<O FIJRN JIGT OC@ AJPM <MDOCH@OD> JK@M<OJMN, <N R@ C<Q@ N@@I DI OC@ @S@M>DN@N JA
OC@ KM@QDJPN >C<KO@M:
TXJUDI (PQFSBUPS) \
DBTF "+":
XSJUFMO(GJSTU + TFDPOE);
CSFBL;
DBTF "-":
XSJUFMO(GJSTU - TFDPOE);
CSFBL;
DBTF "Y":
XSJUFMO(GJSTU * TFDPOE);
CSFBL;
DBTF "/":
XSJUFMO(GJSTU / TFDPOE);
CSFBL;
EFGBVMU:
UISPX OFX &YDFQUJPO(GPSNBU("*OWBMJE PQFSBUPS: %T", PQFSBUPS));
^
1C@ TXJUDI NO<O@H@IO <=JQ@ ?J@N IJO FIJR RC<O OJ ?J RDOC JK@M<OJMN OC<O <M@ IJO GDNO@? JI
OC@ DBTF NO<O@H@ION; NJ OCMJRN <I @S>@KODJI.
1C@M@ <M@ H<IT @S<HKG@N JA OCMJRI @S>@KODJIN DI -CJ=JN. #JM @S<HKG@, UP!JOU, RCD>C ><I
=@ PN@? OJ >JIQ@MO < NOMDIB M@KM@N@IO<ODJI JA <I DIO@B@M OJ <I JOU Q<GP@ OCMJRN <I @S>@KODJI
RC@I OC<O M@KM@N@IO<ODJI DN IJO Q<GD?:
JNQPSU TUE.DPOW;
WPJE NBJO()
\
DPOTU JOU WBMVF = UP!JOU("IFMMP");
^
1C@ KMJBM<H O@MHDI<O@N RDOC <I @S>@KODJI OC<O DN OCMJRI =T UP!JOU:
TUE.DPOW.$POW&YDFQUJPO!TUE/DPOW.E(38): TUE.DPOW(1157): $BO'U
DPOWFSU WBMVF AIFMMP' PG UZQF DPOTU(DIBS)<> UP UZQF JOU
TUE.DPOW.$POW&YDFQUJPO <O OC@ =@BDIIDIB JA OC@ H@NN<B@ DN OC@ OTK@ JA OC@ OCMJRI
@S>@KODJI J=E@>O. 4@ ><I O@GG AMJH OC@ I<H@ OC<O OC@ OTK@ DN $POW&YDFQUJPO OC<O DN ?@ADI@? DI
OC@ TUE.DPOW HJ?PG@.
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
191
The UISPX statement to throw exceptions
40.1
4@'Q@ N@@I OC@ UISPX NO<O@H@IO =JOC DI OC@ @S<HKG@N <=JQ@ <I? DI OC@ KM@QDJPN >C<KO@MN.
UISPX OCMJRN <I 7J57BF;A@ A4<75F <I? OCDN O@MHDI<O@N OC@ >PMM@IO JK@M<ODJI JA OC@ KMJBM<H.
1C@ @SKM@NNDJIN <I? NO<O@H@ION OC<O <M@ RMDOO@I <AO@M OC@ UISPX NO<O@H@IO <M@ IJO @S@>PO@?.
1CDN =@C<QDJM DN <>>JM?DIB OJ OC@ I<OPM@ JA @S>@KODJIN: OC@T HPNO =@ OCMJRI RC@I OC@
KMJBM<H ><IIJO >JIODIP@ RDOC DON >PMM@IO O<NF.
JIQ@MN@GT, DA OC@ KMJBM<H >JPG? >JIODIP@ OC@I OC@ NDOP<ODJI RJPG? IJO R<MM<IO OCMJRDIB <I
@S>@KODJI. &I NP>C ><N@N OC@ API>ODJI RJPG? ADI? < R<T <I? >JIODIP@.
The exception types &YDFQUJPO and &SSPS 40.1.1
,IGT OC@ OTK@N OC<O <M@ DIC@MDO@? AMJH OC@ 5ISPXBCMF >G<NN ><I =@ OCMJRI. 5ISPXBCMF DN
<GHJNO I@Q@M PN@? ?DM@>OGT DI KMJBM<HN. 1C@ OTK@N OC<O <M@ <>OP<GGT OCMJRI <M@ OTK@N OC<O <M@
DIC@MDO@? AMJH &YDFQUJPO JM &SSPS, RCD>C OC@HN@GQ@N <M@ OC@ OTK@N OC<O <M@ DIC@MDO@? AMJH
5ISPXBCMF. #JM @S<HKG@, <GG JA OC@ @S>@KODJIN OC<O -CJ=JN OCMJRN <M@ DIC@MDO@? AMJH @DOC@M
&YDFQUJPO JM &SSPS.
&SSPS M@KM@N@ION PIM@>JQ@M<=G@ >JI?DODJIN <I? DN IJO M@>JHH@I?@? OJ =@ 53G9:F. #JM OC<O
M@<NJI, HJNO JA OC@ @S>@KODJIN OC<O < KMJBM<H OCMJRN <M@ OC@ OTK@N OC<O <M@ DIC@MDO@? AMJH
&YDFQUJPO. (Note: !@:7D;F3@57 ;E 3 FAB;5 D7>3F76 FA 5>3EE7E. ,7 I;>> E77 5>3EE7E ;@ 3 >3F7D
5:3BF7D.)
&YDFQUJPO J=E@>ON <M@ >JINOMP>O@? RDOC < TUSJOH Q<GP@ OC<O M@KM@N@ION <I @MMJM H@NN<B@.
6JP H<T ADI? DO @<NT OJ >M@<O@ OCDN H@NN<B@ RDOC OC@ GPSNBU() API>ODJI AMJH OC@ TUE.TUSJOH
HJ?PG@:
JNQPSU TUE.TUEJP;
JNQPSU TUE.SBOEPN;
JNQPSU TUE.TUSJOH;
JOU<> SBOEPN%JDF7BMVFT(JOU DPVOU)
\
JG (DPVOU < 0) \
UISPX OFX &YDFQUJPO(
GPSNBU("*OWBMJE EJDF DPVOU: %T", DPVOU));
^
JOU<> WBMVFT;
GPSFBDI (J; 0 .. DPVOU) \
WBMVFT _= VOJGPSN(1, 7);
^
SFUVSO WBMVFT;
^
WPJE NBJO()
\
XSJUFMO(SBOEPN%JDF7BMVFT(-5));
^
PCKFDU.&YDFQUJPO...: *OWBMJE EJDF DPVOU: -5
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
192
Thrown exception terminates all scopes 40.1.2
4@ C<Q@ N@@I OC<O OC@ KMJBM<H @S@>PODJI NO<MON AMJH OC@ NBJO API>ODJI <I? =M<I>C@N DIOJ
JOC@M API>ODJIN AMJH OC@M@. 1CDN G<T@M@? @S@>PODJI JA BJDIB ?@@K@M DIOJ API>ODJIN <I?
@Q@IOP<GGT M@OPMIDIB AMJH OC@H ><I =@ N@@I <N OC@ =M<I>C@N JA < OM@@.
#JM @S<HKG@, NBJO() H<T ><GG < API>ODJI I<H@? NBLF0NFMFU, RCD>C DI OPMI H<T ><GG <IJOC@M
API>ODJI I<H@? QSFQBSF"MM, RCD>C DI OPMI H<T ><GG <IJOC@M API>ODJI I<H@? QSFQBSF&HHT,
@O>. ANNPHDIB OC<O OC@ <MMJRN DI?D><O@ API>ODJI ><GGN, OC@ =M<I>CDIB JA NP>C < KMJBM<H ><I
=@ NCJRI <N DI OC@ AJGGJRDIB API>ODJI ><GG OM@@:
NBJO
]
+--r NBLF0NFMFU
] ]
] +--r QSFQBSF"MM
] ] ]
] ] +-r QSFQBSF&HHT
] ] +-r QSFQBSF#VUUFS
] ] +-r QSFQBSF1BO
] ]
] +--r DPPL&HHT
] +--r DMFBO"MM
]
+--r FBU0NFMFU
1C@ AJGGJRDIB KMJBM<H ?@HJINOM<O@N OC@ =M<I>CDIB <=JQ@ =T PNDIB ?DAA@M@IO G@Q@GN JA
DI?@IO<ODJI DI DON JPOKPO. 1C@ KMJBM<H ?J@NI'O ?J <ITOCDIB PN@APG JOC@M OC<I KMJ?P>DIB <I
JPOKPO NPDO<=G@ OJ JPM KPMKJN@N:
JNQPSU TUE.TUEJP;
WPJE JOEFOU(JO JOU MFWFM)
\
GPSFBDI (J; 0 .. MFWFM * 2) \
XSJUF(' ');
^
^
WPJE FOUFSJOH(JO DIBS<> GVODUJPO/BNF, JO JOU MFWFM)
\
JOEFOU(MFWFM);
XSJUFMO("r ", GVODUJPO/BNF, "'T GJSTU MJOF");
^
WPJE FYJUJOH(JO DIBS<> GVODUJPO/BNF, JO JOU MFWFM)
\
JOEFOU(MFWFM);
XSJUFMO("t ", GVODUJPO/BNF, "'T MBTU MJOF");
^
WPJE NBJO()
\
FOUFSJOH("NBJO", 0);
NBLF0NFMFU();
FBU0NFMFU();
FYJUJOH("NBJO", 0);
^
WPJE NBLF0NFMFU()
\
FOUFSJOH("NBLF0NFMFU", 1);
QSFQBSF"MM();
DPPL&HHT();
DMFBO"MM();
FYJUJOH("NBLF0NFMFU", 1);
^
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
193
WPJE FBU0NFMFU()
\
FOUFSJOH("FBU0NFMFU", 1);
FYJUJOH("FBU0NFMFU", 1);
^
WPJE QSFQBSF"MM()
\
FOUFSJOH("QSFQBSF"MM", 2);
QSFQBSF&HHT();
QSFQBSF#VUUFS();
QSFQBSF1BO();
FYJUJOH("QSFQBSF"MM", 2);
^
WPJE DPPL&HHT()
\
FOUFSJOH("DPPL&HHT", 2);
FYJUJOH("DPPL&HHT", 2);
^
WPJE DMFBO"MM()
\
FOUFSJOH("DMFBO"MM", 2);
FYJUJOH("DMFBO"MM", 2);
^
WPJE QSFQBSF&HHT()
\
FOUFSJOH("QSFQBSF&HHT", 3);
FYJUJOH("QSFQBSF&HHT", 3);
^
WPJE QSFQBSF#VUUFS()
\
FOUFSJOH("QSFQBSF#VUUFS", 3);
FYJUJOH("QSFQBSF#VUUFS", 3);
^
WPJE QSFQBSF1BO()
\
FOUFSJOH("QSFQBSF1BO", 3);
FYJUJOH("QSFQBSF1BO", 3);
^
1C@ KMJBM<H KMJ?P>@N OC@ AJGGJRDIB JPOKPO:
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
194
q NBJO, GJSTU MJOF
q NBLF0NFMFU, GJSTU MJOF
q QSFQBSF"MM, GJSTU MJOF
q QSFQBSF&HHT, GJSTU MJOF
r QSFQBSF&HHT, MBTU MJOF
q QSFQBSF#VUUFS, GJSTU MJOF
r QSFQBSF#VUUFS, MBTU MJOF
q QSFQBSF1BO, GJSTU MJOF
r QSFQBSF1BO, MBTU MJOF
r QSFQBSF"MM, MBTU MJOF
q DPPL&HHT, GJSTU MJOF
r DPPL&HHT, MBTU MJOF
q DMFBO"MM, GJSTU MJOF
r DMFBO"MM, MBTU MJOF
r NBLF0NFMFU, MBTU MJOF
q FBU0NFMFU, GJSTU MJOF
r FBU0NFMFU, MBTU MJOF
r NBJO, MBTU MJOF
1C@ API>ODJIN FOUFSJOH <I? FYJUJOH <M@ PN@? OJ DI?D><O@ OC@ ADMNO <I? G<NO GDI@N JA API>ODJIN
RDOC OC@ C@GK JA OC@ q <I? r >C<M<>O@MN. 1C@ KMJBM<H NO<MON RDOC OC@ ADMNO GDI@ JA NBJO(),
=M<I>C@N ?JRI OJ JOC@M API>ODJIN, <I? ADI<GGT @I?N RDOC OC@ G<NO GDI@ JA NBJO.
)@O'N HJ?DAT OC@ QSFQBSF&HHT API>ODJI OJ O<F@ OC@ IPH=@M JA @BBN <N < K<M<H@O@M. 0DI>@
>@MO<DI Q<GP@N JA OCDN K<M<H@O@M RJPG? =@ <I @MMJM, G@O'N C<Q@ OCDN API>ODJI OCMJR <I
@S>@KODJI RC@I OC@ IPH=@M JA @BBN DN G@NN OC<I JI@:
WPJE QSFQBSF&HHT(JOU DPVOU)
\
FOUFSJOH("QSFQBSF&HHT", 3);
JG (DPVOU < 1) \
UISPX OFX &YDFQUJPO(
GPSNBU("$BOOPU UBLF %T FHHT GSPN UIF GSJEHF", DPVOU));
^
FYJUJOH("QSFQBSF&HHT", 3);
^
&I JM?@M OJ =@ <=G@ OJ >JHKDG@ OC@ KMJBM<H, R@ HPNO HJ?DAT JOC@M GDI@N JA OC@ KMJBM<H OJ =@
>JHK<OD=G@ RDOC OCDN >C<IB@. 1C@ IPH=@M JA @BBN OJ O<F@ JPO JA OC@ AMD?B@ ><I =@ C<I?@?
?JRI AMJH API>ODJI OJ API>ODJI, NO<MODIB RDOC NBJO(). 1C@ K<MON JA OC@ KMJBM<H OC<O I@@? OJ
>C<IB@ <M@ OC@ AJGGJRDIB. 1C@ DIQ<GD? Q<GP@ JA -8 DN DIO@IODJI<G OJ NCJR CJR OC@ JPOKPO JA OC@
KMJBM<H RDGG =@ ?DAA@M@IO AMJH OC@ KM@QDJPN JPOKPO RC@I <I @S>@KODJI DN OCMJRI:
// ...
WPJE NBJO()
\
FOUFSJOH("NBJO", 0);
NBLF0NFMFU(-8);
FBU0NFMFU();
FYJUJOH("NBJO", 0);
^
WPJE NBLF0NFMFU(JOU FHH$PVOU)
\
FOUFSJOH("NBLF0NFMFU", 1);
QSFQBSF"MM(FHH$PVOU);
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
195
DPPL&HHT();
DMFBO"MM();
FYJUJOH("NBLF0NFMFU", 1);
^
// ...
WPJE QSFQBSF"MM(JOU FHH$PVOU)
\
FOUFSJOH("QSFQBSF"MM", 2);
QSFQBSF&HHT(FHH$PVOU);
QSFQBSF#VUUFS();
QSFQBSF1BO();
FYJUJOH("QSFQBSF"MM", 2);
^
// ...
4C@I R@ NO<MO OC@ KMJBM<H IJR, R@ N@@ OC<O OC@ GDI@N OC<O PN@? OJ =@ KMDIO@? <AO@M OC@ KJDIO
RC@M@ OC@ @S>@KODJI DN OCMJRI <M@ HDNNDIB:
q NBJO, GJSTU MJOF
q NBLF0NFMFU, GJSTU MJOF
q QSFQBSF"MM, GJSTU MJOF
q QSFQBSF&HHT, GJSTU MJOF
PCKFDU.&YDFQUJPO: $BOOPU UBLF -8 FHHT GSPN UIF GSJEHF
4C@I OC@ @S>@KODJI DN OCMJRI, OC@ KMJBM<H @S@>PODJI @SDON OC@ QSFQBSF&HHT, QSFQBSF"MM,
NBLF0NFMFU <I? NBJO() API>ODJIN DI OC<O JM?@M, AMJH OC@ =JOOJH G@Q@G OJ OC@ OJK G@Q@G. +J
<??DODJI<G NO@KN <M@ @S@>PO@? <N OC@ KMJBM<H @SDON OC@N@ API>ODJIN.
1C@ M<ODJI<G@ AJM NP>C < ?M<NOD> O@MHDI<ODJI DN OC<O < A<DGPM@ DI < GJR@M G@Q@G API>ODJI RJPG?
H@<I OC<O OC@ CDBC@M G@Q@G API>ODJIN OC<O I@@?@? DO NCJPG? <GNJ =@ >JIND?@M@? <N A<DG@?.
1C@ @S>@KODJI J=E@>O OC<O DN OCMJRI AMJH < GJR@M G@Q@G API>ODJI DN OM<INA@MM@? OJ OC@ CDBC@M
G@Q@G API>ODJIN JI@ G@Q@G <O < ODH@ <I? ><PN@N OC@ KMJBM<H OJ ADI<GGT @SDO OC@ NBJO() API>ODJI.
1C@ K<OC OC<O OC@ @S>@KODJI O<F@N ><I =@ NCJRI <N OC@ M@? K<OC DI OC@ AJGGJRDIB OM@@:
q
]
NBJO s---+
] ]
+--r NBLF0NFMFU s-+
] ] ]
] +--r QSFQBSF"MM s------------+
] ] ] ]FYDFQUJPO
] ] +-r QSFQBSF&HHT 9-+
] ] +-r QSFQBSF#VUUFS
] ] +-r QSFQBSF1BO
] ]
] +--r DPPL&HHT
] +--r DMFBO"MM
]
+--r FBU0NFMFU
1C@ KJDIO JA OC@ @S>@KODJI H@>C<IDNH DN KM@>DN@GT OCDN =@C<QDJM JA @SDODIB <GG JA OC@ G<T@MN JA
API>ODJI ><GGN MDBCO <R<T. 0JH@ODH@N DO H<F@N N@IN@ OJ 53F5: OC@ OCMJRI @S>@KODJI OJ ADI? <
R<T OJ >JIODIP@ OC@ @S@>PODJI JA OC@ KMJBM<H. & RDGG DIOMJ?P>@ OC@ DBUDI F@TRJM? =@GJR.
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
196
When to use UISPX 40.1.3
2N@ UISPX DI NDOP<ODJIN RC@I DO DN IJO KJNND=G@ OJ >JIODIP@. #JM @S<HKG@, < API>ODJI OC<O M@<?N
OC@ IPH=@M JA NOP?@ION AMJH < ADG@ H<T OCMJR <I @S>@KODJI DA OCDN DIAJMH<ODJI DN IJO <Q<DG<=G@
JM DI>JMM@>O.
,I OC@ JOC@M C<I?, DA OC@ KMJ=G@H DN ><PN@? =T NJH@ PN@M <>ODJI GDF@ @IO@MDIB DIQ<GD? ?<O<, DO
H<T H<F@ HJM@ N@IN@ OJ Q<GD?<O@ OC@ ?<O< DINO@<? JA OCMJRDIB <I @S>@KODJI. !DNKG<TDIB <I
@MMJM H@NN<B@ <I? <NFDIB OC@ PN@M OJ M@-@IO@M OC@ ?<O< DN HJM@ <KKMJKMD<O@ DI H<IT ><N@N.
The USZ-DBUDI statement to catch exceptions
40.2
AN R@'Q@ N@@I <=JQ@, < OCMJRI @S>@KODJI ><PN@N OC@ KMJBM<H @S@>PODJI OJ @SDO <GG API>ODJIN
<I? OCDN ADI<GGT O@MHDI<O@N OC@ RCJG@ KMJBM<H.
1C@ @S>@KODJI J=E@>O ><I =@ ><PBCO =T < USZ-DBUDI NO<O@H@IO <O <IT KJDIO JI DON K<OC <N DO
@SDON OC@ API>ODJIN. 1C@ USZ-DBUDI NO<O@H@IO HJ?@GN OC@ KCM<N@ "FDK OJ ?J NJH@OCDIB, <I?
53F5: @S>@KODJIN OC<O H<T =@ OCMJRI." %@M@ DN OC@ NTIO<S JA USZ-DBUDI:
USZ \
// UIF DPEF CMPDL UIBU JT CFJOH FYFDVUFE, XIFSF BO
// FYDFQUJPO NBZ CF UISPXO
^ DBUDI (an_exception_type) \
// FYQSFTTJPOT UP FYFDVUF JG BO FYDFQUJPO PG UIJT
// UZQF JT DBVHIU
^ DBUDI (another_exception_type) \
// FYQSFTTJPOT UP FYFDVUF JG BO FYDFQUJPO PG UIJT
// PUIFS UZQF JT DBVHIU
// ... NPSF DBUDI CMPDLT BT BQQSPQSJBUF ...
^ GJOBMMZ \
// FYQSFTTJPOT UP FYFDVUF SFHBSEMFTT PG XIFUIFS BO
// FYDFQUJPO JT UISPXO
^
)@O'N NO<MO RDOC OC@ AJGGJRDIB KMJBM<H OC<O ?J@N IJO PN@ < USZ-DBUDI NO<O@H@IO <O OCDN NO<O@.
1C@ KMJBM<H M@<?N OC@ Q<GP@ JA < ?D@ AMJH < ADG@ <I? KMDION DO OJ OC@ NO<I?<M? JPOKPO:
JNQPSU TUE.TUEJP;
JOU SFBE%JF'SPN'JMF()
\
BVUP GJMF = 'JMF("UIF@GJMF@UIBU@DPOUBJOT@UIF@WBMVF", "S");
JOU EJF;
GJMF.SFBEG(" %T", &EJF);
SFUVSO EJF;
^
WPJE NBJO()
\
DPOTU JOU EJF = SFBE%JF'SPN'JMF();
XSJUFMO("%JF WBMVF: ", EJF);
^
+JO@ OC<O OC@ SFBE%JF'SPN'JMF API>ODJI DN RMDOO@I DI < R<T OC<O DBIJM@N @MMJM >JI?DODJIN,
@SK@>ODIB OC<O OC@ ADG@ <I? OC@ Q<GP@ OC<O DO >JIO<DIN <M@ <Q<DG<=G@. &I JOC@M RJM?N, OC@
API>ODJI DN ?@<GDIB JIGT RDOC DON JRI O<NF DINO@<? JA K<TDIB <OO@IODJI OJ @MMJM >JI?DODJIN. 1CDN
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
197
DN < =@I@ADO JA @S>@KODJIN: H<IT API>ODJIN ><I =@ RMDOO@I DI R<TN OC<O AJ>PN JI OC@DM <>OP<G
O<NFN, M<OC@M OC<I AJ>PNDIB JI @MMJM >JI?DODJIN.
)@O'N NO<MO OC@ KMJBM<H RC@I UIF@GJMF@UIBU@DPOUBJOT@UIF@WBMVF DN HDNNDIB:
TUE.FYDFQUJPO.&SSOP&YDFQUJPO!TUE/TUEJP.E(286): $BOOPU PQFO
GJMF AUIF@GJMF@UIBU@DPOUBJOT@UIF@WBMVF' JO NPEF AS' (/P TVDI
GJMF PS EJSFDUPSZ)
AI @S>@KODJI JA OTK@ &SSOP&YDFQUJPO DN OCMJRI <I? OC@ KMJBM<H O@MHDI<O@N RDOCJPO KMDIODIB
"!D@ Q<GP@: ".
)@O'N <?? <I DIO@MH@?D<O@ API>ODJI OJ OC@ KMJBM<H OC<O ><GGN SFBE%JF'SPN'JMF AMJH RDOCDI <
USZ =GJ>F <I? G@O'N C<Q@ NBJO() ><GG OCDN I@R API>ODJI:
JNQPSU TUE.TUEJP;
JOU SFBE%JF'SPN'JMF()
\
BVUP GJMF = 'JMF("UIF@GJMF@UIBU@DPOUBJOT@UIF@WBMVF", "S");
JOU EJF;
GJMF.SFBEG(" %T", &EJF);
SFUVSO EJF;
^
JOU USZ3FBEJOH'SPN'JMF()
\
JOU EJF;
USZ \
EJF = SFBE%JF'SPN'JMF();
^ DBUDI (TUE.FYDFQUJPO.&SSOP&YDFQUJPO FYD) \
XSJUFMO("($PVME OPU SFBE GSPN GJMF; BTTVNJOH 1)");
EJF = 1;
^
SFUVSO EJF;
^
WPJE NBJO()
\
DPOTU JOU EJF = USZ3FBEJOH'SPN'JMF();
XSJUFMO("%JF WBMVF: ", EJF);
^
4C@I R@ NO<MO OC@ KMJBM<H <B<DI RC@I UIF@GJMF@UIBU@DPOUBJOT@UIF@WBMVF DN NODGG HDNNDIB,
OCDN ODH@ OC@ KMJBM<H ?J@N IJO O@MHDI<O@ RDOC <I @S>@KODJI:
($PVME OPU SFBE GSPN GJMF; BTTVNJOH 1)
%JF WBMVF: 1
1C@ I@R KMJBM<H FD;7E @S@>PODIB SFBE%JF'SPN'JMF DI < USZ =GJ>F. &A OC<O =GJ>F @S@>PO@N
NP>>@NNAPGGT, OC@ API>ODJI @I?N IJMH<GGT RDOC OC@ SFUVSO EJF; NO<O@H@IO. &A OC@ @S@>PODJI JA
OC@ USZ =GJ>F @I?N RDOC OC@ NK@>DAD@? TUE.FYDFQUJPO.&SSOP&YDFQUJPO, OC@I OC@ KMJBM<H
@S@>PODJI @IO@MN OC@ DBUDI =GJ>F.
1C@ AJGGJRDIB DN < NPHH<MT JA @Q@ION RC@I OC@ KMJBM<H DN NO<MO@? RC@I OC@ ADG@ DN HDNNDIB:
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
198
t GDF@ DI OC@ KM@QDJPN KMJBM<H, < TUE.FYDFQUJPO.&SSOP&YDFQUJPO J=E@>O DN OCMJRI (=T
'JMF(), IJO =T JPM >J?@),
t OCDN @S>@KODJI DN ><PBCO =T DBUDI,
t OC@ Q<GP@ JA 1 DN <NNPH@? ?PMDIB OC@ IJMH<G @S@>PODJI JA OC@ DBUDI =GJ>F,
t <I? OC@ KMJBM<H >JIODIP@N DON IJMH<G JK@M<ODJIN.
DBUDI DN OJ ><O>C OCMJRI @S>@KODJIN KM@NPH<=GT OJ ADI? < R<T OJ >JIODIP@ @S@>PODIB OC@
KMJBM<H.
AN <IJOC@M @S<HKG@, G@O'N BJ =<>F OJ OC@ JH@G@O KMJBM<H <I? <?? < USZ-DBUDI NO<O@H@IO OJ
DON NBJO() API>ODJI:
WPJE NBJO()
\
FOUFSJOH("NBJO", 0);
USZ \
NBLF0NFMFU(-8);
FBU0NFMFU();
^ DBUDI (&YDFQUJPO FYD) \
XSJUF("'BJMFE UP FBU PNFMFU: ");
XSJUFMO('"', FYD.NTH, '"');
XSJUFMO("4IBMM FBU BU UIF OFJHICPS'T...");
^
FYJUJOH("NBJO", 0);
^
1C<O USZ =GJ>F >JIO<DIN ORJ GDI@N JA >J?@. AIT @S>@KODJI OCMJRI AMJH @DOC@M JA OCJN@ GDI@N
RJPG? =@ ><PBCO =T OC@ DBUDI =GJ>F.
q NBJO, GJSTU MJOF
q NBLF0NFMFU, GJSTU MJOF
q QSFQBSF"MM, GJSTU MJOF
q QSFQBSF&HHT, GJSTU MJOF
'BJMFE UP FBU PNFMFU: "$BOOPU UBLF -8 FHHT GSPN UIF GSJEHF"
4IBMM FBU BU UIF OFJHICPS'T...
r NBJO, MBTU MJOF
AN ><I =@ N@@I AMJH OC@ JPOKPO, OC@ KMJBM<H ?J@NI'O O@MHDI<O@ =@><PN@ JA OC@ OCMJRI
@S>@KODJI <ITHJM@. &O M@>JQ@MN AMJH OC@ @MMJM >JI?DODJI <I? >JIODIP@N @S@>PODIB IJMH<GGT ODGG
OC@ @I? JA OC@ NBJO() API>ODJI.
DBUDI blocks are looked up sequentially 40.2.1
1C@ OTK@ &YDFQUJPO, RCD>C R@ C<Q@ PN@? NJ A<M DI OC@ @S<HKG@N DN < B@I@M<G @S>@KODJI OTK@.
1CDN OTK@ H@M@GT NK@>DAD@N OC<O <I @MMJM J>>PMM@? DI OC@ KMJBM<H. &O <GNJ >JIO<DIN < H@NN<B@
OC<O ><I @SKG<DI OC@ @MMJM APMOC@M, =PO DO ?J@N IJO >JIO<DI DIAJMH<ODJI <=JPO OC@ FKB7 JA OC@
@MMJM.
$POW&YDFQUJPO <I? &SSOP&YDFQUJPO OC<O R@ C<Q@ N@@I @<MGD@M DI OCDN >C<KO@M <M@ HJM@
NK@>DAD> @S>@KODJI OTK@N: OC@ AJMH@M DN <=JPO < >JIQ@MNDJI @MMJM, <I? OC@ G<OO@M DN <=JPO <
NTNO@H @MMJM. )DF@ H<IT JOC@M @S>@KODJI OTK@N DI -CJ=JN <I? <N OC@DM I<H@N NPBB@NO,
$POW&YDFQUJPO <I? &SSOP&YDFQUJPO <M@ =JOC DIC@MDO@? AMJH OC@ &YDFQUJPO >G<NN.
&YDFQUJPO <I? DON ND=GDIB &SSPS <M@ APMOC@M DIC@MDO@? AMJH 5ISPXBCMF, OC@ HJNO B@I@M<G
@S>@KODJI OTK@.
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
199
AGOCJPBC KJNND=G@, DO DN IJO M@>JHH@I?@? OJ ><O>C J=E@>ON JA OTK@ &SSPS <I? J=E@>ON JA OTK@N
OC<O <M@ DIC@MDO@? AMJH &SSPS. 0DI>@ DO DN HJM@ B@I@M<G OC<I &SSPS, DO DN IJO M@>JHH@I?@? OJ
><O>C 5ISPXBCMF @DOC@M. 4C<O NCJPG? IJMH<GGT =@ ><PBCO <M@ OC@ OTK@N OC<O <M@ PI?@M OC@
&YDFQUJPO CD@M<M>CT.
5ISPXBCMF (OPU SFDPNNFOEFE UP DBUDI)
o n
&YDFQUJPO &SSPS (OPU SFDPNNFOEFE UP DBUDI)
o n o n
... ... ... ...
Note: ! I;>> 7JB>3;@ F:7 :;7D3D5:K D7BD7E7@F3F;A@E >3F7D ;@ F:7 !@:7D;F3@57 5:3BF7D. ):7 FD77
34AH7 ;@6;53F7E F:3F Throwable ;E F:7 ?AEF 97@7D3> 3@6 Exception 3@6 Error 3D7 ?AD7
EB75;8;5.
&O DN KJNND=G@ OJ ><O>C @S>@KODJI J=E@>ON JA < K<MOD>PG<M OTK@. #JM @S<HKG@, DO DN KJNND=G@ OJ
><O>C <I &SSOP&YDFQUJPO J=E@>O NK@>DAD><GGT OJ ?@O@>O <I? C<I?G@ < NTNO@H @MMJM.
"S>@KODJIN <M@ ><PBCO JIGT DA OC@T H<O>C < OTK@ OC<O DN NK@>DAD@? DI < DBUDI =GJ>F. #JM
@S<HKG@, < ><O>C =GJ>F OC<O DN OMTDIB OJ ><O>C < 4QFDJBM&YDFQUJPO5ZQF RJPG? IJO ><O>C <I
&SSOP&YDFQUJPO.
1C@ OTK@ JA OC@ @S>@KODJI J=E@>O OC<O DN OCMJRI ?PMDIB OC@ @S@>PODJI JA < USZ =GJ>F DN
H<O>C@? OJ OC@ OTK@N OC<O <M@ NK@>DAD@? =T OC@ DBUDI =GJ>FN, DI OC@ JM?@M DI RCD>C OC@ DBUDI
=GJ>FN <M@ RMDOO@I. &A OC@ OTK@ JA OC@ J=E@>O H<O>C@N OC@ OTK@ JA OC@ DBUDI =GJ>F, OC@I OC@
@S>@KODJI DN >JIND?@M@? OJ =@ ><PBCO =T OC<O DBUDI =GJ>F, <I? OC@ >J?@ OC<O DN RDOCDI OC<O
=GJ>F DN @S@>PO@?. ,I>@ < H<O>C DN AJPI?, OC@ M@H<DIDIB DBUDI =GJ>FN <M@ DBIJM@?.
B@><PN@ OC@ DBUDI =GJ>FN <M@ H<O>C@? DI JM?@M AMJH OC@ ADMNO OJ OC@ G<NO, OC@ DBUDI =GJ>FN
HPNO =@ JM?@M@? AMJH OC@ HJNO NK@>DAD> @S>@KODJI OTK@N OJ OC@ HJNO B@I@M<G @S>@KODJI OTK@N.
A>>JM?DIBGT, <I? DA DO H<F@N N@IN@ OJ ><O>C OC<O OTK@ JA @S>@KODJIN, OC@ &YDFQUJPO OTK@ HPNO
=@ NK@>DAD@? <O OC@ G<NO DBUDI =GJ>F.
#JM @S<HKG@, < USZ-DBUDI NO<O@H@IO OC<O DN OMTDIB OJ ><O>C N@Q@M<G NK@>DAD> OTK@N JA
@S>@KODJIN <=JPO NOP?@IO M@>JM?N H<T JM?@M OC@ DBUDI =GJ>FN AMJH OC@ HJNO NK@>DAD> OJ OC@
HJNO B@I@M<G <N DI OC@ AJGGJRDIB >J?@:
USZ \
// PQFSBUJPOT BCPVU TUVEFOU SFDPSET UIBU NBZ UISPX ...
^ DBUDI (4UVEFOU*E%JHJU&YDFQUJPO FYD) \
// BO FYDFQUJPO UIBU JT TQFDJGJDBMMZ BCPVU FSSPST XJUI
// UIF EJHJUT PG TUVEFOU JET
^ DBUDI (4UVEFOU*E&YDFQUJPO FYD) \
// B NPSF HFOFSBM FYDFQUJPO BCPVU TUVEFOU JET CVU OPU
// OFDFTTBSJMZ BCPVU UIFJS EJHJUT
^ DBUDI (4UVEFOU3FDPSE&YDFQUJPO FYD) \
// FWFO NPSF HFOFSBM FYDFQUJPO BCPVU TUVEFOU SFDPSET
^ DBUDI (&YDFQUJPO FYD) \
// UIF NPTU HFOFSBM FYDFQUJPO UIBU NBZ OPU CF SFMBUFE
// UP TUVEFOU SFDPSET
^
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
200
The GJOBMMZ block 40.2.2
GJOBMMZ DN <I JKODJI<G =GJ>F JA OC@ USZ-DBUDI NO<O@H@IO. &O DI>GP?@N @SKM@NNDJIN OC<O NCJPG?
=@ @S@>PO@? M@B<M?G@NN JA RC@OC@M <I @S>@KODJI DN OCMJRI JM IJO.
1J N@@ CJR GJOBMMZ RJMFN, G@O'N GJJF <O < KMJBM<H OC<O OCMJRN <I @S>@KODJI 50% JA OC@ ODH@:
JNQPSU TUE.TUEJP;
JNQPSU TUE.SBOEPN;
WPJE UISPXT)BMG5IF5JNF()
\
JG (VOJGPSN(0, 2) == 1) \
UISPX OFX &YDFQUJPO("UIF FSSPS NFTTBHF");
^
^
WPJE GPP()
\
XSJUFMO("UIF GJSTU MJOF PG GPP()");
USZ \
XSJUFMO("UIF GJSTU MJOF PG UIF USZ CMPDL");
UISPXT)BMG5IF5JNF();
XSJUFMO("UIF MBTU MJOF PG UIF USZ CMPDL");
// ... UIFSF NBZ CF POF PS NPSF DBUDI CMPDLT IFSF ...
^ GJOBMMZ \
XSJUFMO("UIF CPEZ PG UIF GJOBMMZ CMPDL");
^
XSJUFMO("UIF MBTU MJOF PG GPP()");
^
WPJE NBJO()
\
GPP();
^
1C@ JPOKPO JA OC@ KMJBM<H DN OC@ AJGGJRDIB RC@I OC@ API>ODJI ?J@N IJO OCMJR:
UIF GJSTU MJOF PG GPP()
UIF GJSTU MJOF PG UIF USZ CMPDL
UIF MBTU MJOF PG UIF USZ CMPDL
UIF CPEZ PG UIF GJOBMMZ CMPDL
UIF MBTU MJOF PG GPP()
1C@ JPOKPO JA OC@ KMJBM<H DN OC@ AJGGJRDIB RC@I OC@ API>ODJI ?J@N OCMJR:
UIF GJSTU MJOF PG GPP()
UIF GJSTU MJOF PG UIF USZ CMPDL
UIF CPEZ PG UIF GJOBMMZ CMPDL
PCKFDU.&YDFQUJPO!EFOFNF.E: UIF FSSPS NFTTBHF
AN ><I =@ N@@I, <GOCJPBC "OC@ G<NO GDI@ JA OC@ OMT =GJ>F" <I? "OC@ G<NO GDI@ JA AJJ()" <M@ IJO
KMDIO@?, OC@ >JIO@IO JA OC@ GJOBMMZ =GJ>F DN NODGG @S@>PO@? RC@I <I @S>@KODJI DN OCMJRI.
When to use the USZ-DBUDI statement 40.2.3
1C@ USZ-DBUDI NO<O@H@IO DN PN@APG OJ ><O>C @S>@KODJIN OJ ?J NJH@OCDIB NK@>D<G <=JPO OC@H.
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
201
#JM OC<O M@<NJI, OC@ USZ-DBUDI NO<O@H@IO NCJPG? =@ PN@? JIGT RC@I OC@M@ DN NJH@OCDIB
NK@>D<G OJ =@ ?JI@. !J IJO ><O>C @S>@KODJIN JOC@MRDN@ <I? G@<Q@ OC@H OJ CDBC@M G@Q@G
API>ODJIN OC<O H<T R<IO OJ ><O>C OC@H.
Types of errors
40.3
4@ C<Q@ N@@I CJR PN@APG OC@ @S>@KODJI H@>C<IDNH DN. &O @I<=G@N =JOC OC@ GJR@M <I? CDBC@M
G@Q@G JK@M<ODJIN OJ =@ <=JMO@? MDBCO <R<T, DINO@<? JA OC@ KMJBM<H >JIODIPDIB RDOC DI>JMM@>O JM
HDNNDIB ?<O<, JM =@C<QDIB DI <IT JOC@M DI>JMM@>O R<T.
1CDN ?J@N IJO H@<I OC<O @Q@MT @MMJM >JI?DODJI R<MM<ION OCMJRDIB <I @S>@KODJI. 1C@M@ H<T =@
=@OO@M OCDIBN OJ ?J ?@K@I?DIB JI OC@ OTK@N JA @MMJMN.
User errors 40.3.1
0JH@ JA OC@ @MMJMN <M@ ><PN@? =T OC@ PN@M. AN R@ C<Q@ N@@I <=JQ@, OC@ PN@M H<T C<Q@
@IO@M@? < NOMDIB GDF@ "C@GGJ" @Q@I OCJPBC OC@ KMJBM<H C<N =@@I @SK@>ODIB < IPH=@M. &O H<T
=@ HJM@ <KKMJKMD<O@ OJ ?DNKG<T <I @MMJM H@NN<B@ <I? <NF OC@ PN@M OJ @IO@M <KKMJKMD<O@ ?<O<
<B<DI.
"Q@I NJ, DO H<T =@ ADI@ OJ <>>@KO <I? PN@ OC@ ?<O< ?DM@>OGT RDOCJPO Q<GD?<ODIB OC@ ?<O< PK
AMJIO; <N GJIB <N OC@ >J?@ OC<O PN@N OC@ ?<O< RJPG? OCMJR <ITR<T. 4C<O DN DHKJMO<IO DN OJ =@
<=G@ OJ IJODAT OC@ PN@M RCT OC@ ?<O< DN IJO NPDO<=G@.
#JM @S<HKG@, G@O'N GJJF <O < KMJBM<H OC<O O<F@N < ADG@ I<H@ AMJH OC@ PN@M. 1C@M@ <M@ <O G@<NO
ORJ R<TN JA ?@<GDIB RDOC KJO@IOD<GGT DIQ<GD? ADG@ I<H@N:
t Validating the data before use: 4@ ><I ?@O@MHDI@ RC@OC@M OC@ ADG@ RDOC OC@ BDQ@I I<H@
@SDNON =T ><GGDIB FYJTUT() JA OC@ TUE.GJMF HJ?PG@:
JG (FYJTUT(GJMF/BNF)) \
// ZFT, UIF GJMF FYJTUT
^ FMTF \
// OP, UIF GJMF EPFTO'U FYJTU
^
1CDN BDQ@N PN OC@ >C<I>@ OJ =@ <=G@ OJ JK@I OC@ ?<O< JIGT DA DO @SDNON. 2IAJMOPI<O@GT, DO
DN NODGG KJNND=G@ OC<O OC@ ADG@ ><IIJO =@ JK@I@? @Q@I DA FYJTUT() M@OPMIN USVF, DA AJM
@S<HKG@ <IJOC@M KMJ>@NN JI OC@ NTNO@H ?@G@O@N JM M@I<H@N OC@ ADG@ =@AJM@ OCDN
KMJBM<H <>OP<GGT JK@IN DO.
#JM OC<O M@<NJI, OC@ AJGGJRDIB H@OCJ? H<T =@ HJM@ PN@APG.
t Using the data without first validating it: 4@ ><I <NNPH@ OC<O OC@ ?<O< DN Q<GD? <I?
NO<MO PNDIB DO MDBCO <R<T, =@><PN@ 'JMF RJPG? OCMJR <I @S>@KODJI DA OC@ ADG@ ><IIJO =@
JK@I@? <ITR<T.
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
WPJE VTF5IF'JMF(TUSJOH GJMF/BNF)
\
BVUP GJMF = 'JMF(GJMF/BNF, "S");
// ...
^
TUSJOH SFBE@TUSJOH(JO DIBS<> QSPNQU)
\
XSJUF(QSPNQU, ": ");
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
202
SFUVSO DIPNQ(SFBEMO());
^
WPJE NBJO()
\
CPPM JT@GJMF6TFE = GBMTF;
XIJMF (!JT@GJMF6TFE) \
USZ \
VTF5IF'JMF(
SFBE@TUSJOH("1MFBTF FOUFS B GJMF OBNF"));
/*
* *G XF BSF BU UIJT MJOF, JU NFBOT UIBU
* VTF5IF'JMF() GVODUJPO IBT CFFO DPNQMFUFE
* TVDDFTTGVMMZ. 5IJT JOEJDBUFT UIBU UIF GJMF
* OBNF IBT CFFO WBMJE.
*
* 8F DBO OPX TFU UIF WBMVF PG UIF MPPQ GMBH UP
* UFSNJOBUF UIF XIJMF MPPQ.
*/
JT@GJMF6TFE = USVF;
XSJUFMO("5IF GJMF IBT CFFO VTFE TVDDFTTGVMMZ");
^ DBUDI (TUE.FYDFQUJPO.&SSOP&YDFQUJPO FYD) \
TUEFSS.XSJUFMO("5IJT GJMF DPVME OPU CF PQFOFE");
^
^
^
Programmer errors 40.3.2
0JH@ @MMJMN <M@ ><PN@? =T KMJBM<HH@M HDNO<F@N. #JM @S<HKG@ OC@ KMJBM<HH@M H<T OCDIF
OC<O < API>ODJI OC<O C<N EPNO =@@I RMDOO@I RDGG <GR<TN =@ ><GG@? RDOC < Q<GP@ BM@<O@M OC<I JM
@LP<G U@MJ, <I? OCDN H<T =@ OMP@ <>>JM?DIB OJ OC@ ?@NDBI JA OC@ KMJBM<H. 1C@ API>ODJI C<QDIB
NODGG =@@I ><GG@? RDOC < Q<GP@ G@NN OC<I U@MJ RJPG? DI?D><O@ @DOC@M < HDNO<F@ DI OC@ ?@NDBI JA
OC@ KMJBM<H JM DI OC@ DHKG@H@IO<ODJI JA OC<O ?@NDBI. BJOC JA OC@N@ ><I =@ OCJPBCO JA <N
KMJBM<HHDIB @MMJMN.
&O DN HJM@ <KKMJKMD<O@ OJ PN@ BTTFSU DINO@<? JA OC@ @S>@KODJI H@>C<IDNH AJM @MMJMN OC<O <M@
><PN@? =T KMJBM<HH@M HDNO<F@N. (Note: ,7 I;>> 5AH7D assert ;@ 3 >3F7D 5:3BF7D.)
WPJE QSPDFTT.FOV4FMFDUJPO(JOU TFMFDUJPO)
\
BTTFSU(TFMFDUJPO >= 0);
// ...
^
WPJE NBJO()
\
QSPDFTT.FOV4FMFDUJPO(-1);
^
1C@ KMJBM<H O@MHDI<O@N RDOC <I BTTFSU A<DGPM@:
DPSF.FYDFQUJPO."TTFSU&SSPS!EFOFNF.E(3): "TTFSUJPO GBJMVSF
BTTFSU Q<GD?<O@N KMJBM<H NO<O@ <I? KMDION OC@ ADG@ I<H@ <I? GDI@ IPH=@M JA OC@ Q<GD?<ODJI DA
DO A<DGN. 1C@ H@NN<B@ <=JQ@ DI?D><O@N OC<O OC@ <NN@MODJI <O GDI@ 3 JA ?@I@H@.? C<N A<DG@?.
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
203
Unexpected situations 40.3.3
#JM PI@SK@>O@? NDOP<ODJIN OC<O <M@ JPOND?@ JA OC@ ORJ B@I@M<G ><N@N <=JQ@, DO DN NODGG
<KKMJKMD<O@ OJ OCMJR @S>@KODJIN. &A OC@ KMJBM<H ><IIJO >JIODIP@ DON @S@>PODJI, OC@M@ DN
IJOCDIB @GN@ OJ ?J =PO OJ OCMJR.
&O DN PK OJ OC@ CDBC@M G<T@M API>ODJIN OC<O ><GG OCDN API>ODJI OJ ?@>D?@ RC<O OJ ?J RDOC OCMJRI
@S>@KODJIN. 1C@T H<T ><O>C OC@ @S>@KODJIN OC<O R@ OCMJR OJ M@H@?T OC@ NDOP<ODJI.
Summary
40.4
t 4C@I A<>@? RDOC < PN@M @MMJM @DOC@M R<MI OC@ PN@M MDBCO <R<T JM @INPM@ OC<O <I @S>@KODJI
DN OCMJRI; OC@ @S>@KODJI H<T =@ OCMJRI <ITR<T =T <IJOC@M API>ODJI RC@I PNDIB DI>JMM@>O
?<O<, JM TJP H<T OCMJR ?DM@>OGT.
t 2N@ BTTFSU OJ Q<GD?<O@ KMJBM<H GJBD> <I? DHKG@H@IO<ODJI. (Note: ! I;>> 7JB>3;@ assert ;@
3 >3F7D 5:3BF7D.)
t 4C@I DI ?JP=O, UISPX <I @S>@KODJI.
t <O>C @S>@KODJIN DA <I? JIGT DA TJP ><I ?J NJH@OCDIB PN@APG <=JPO OC<O @S>@KODJI.
,OC@MRDN@, ?J IJO @I><KNPG<O@ >J?@ RDOC < USZ-DBUDI NO<O@H@IO; DINO@<?, G@<Q@ OC@
@S>@KODJIN OJ CDBC@M G<T@MN JA OC@ >J?@ OC<O H<T ?J NJH@OCDIB <=JPO OC@H.
t ,M?@M OC@ DBUDI =GJ>FN AMJH OC@ HJNO NK@>DAD> OJ OC@ HJNO B@I@M<G.
t -PO OC@ @SKM@NNDJIN OC<O HPNO <GR<TN =@ @S@>PO@? RC@I G@<QDIB < N>JK@, DI GJOBMMZ
=GJ>FN.
EJ57BF;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
204
TDPQF 41
AN R@ C<Q@ N@@I, @SKM@NNDJIN OC<O HPNO <GR<TN =@ @S@>PO@? <M@ RMDOO@I DI OC@ GJOBMMZ =GJ>F,
<I? @SKM@NNDJIN OC<O HPNO =@ @S@>PO@? RC@I OC@M@ <M@ @MMJM >JI?DODJIN <M@ RMDOO@I DI DBUDI
=GJ>FN.
4@ ><I H<F@ OC@ AJGGJRDIB J=N@MQ<ODJIN <=JPO OC@ PN@ JA OC@N@ =GJ>FN:
t DBUDI <I? GJOBMMZ ><IIJO =@ PN@? RDOCJPO < USZ =GJ>F.
t 0JH@ JA OC@ Q<MD<=G@N OC<O OC@N@ =GJ>FN I@@? H<T IJO =@ <>>@NND=G@ RDOCDI OC@N@ =GJ>FN:
WPJE GPP(SFG JOU S)
\
USZ \
JOU BEEFOE = 42;
S += BEEFOE;
NBZ5ISPX();
^ DBUDI (&YDFQUJPO FYD) \
S -= BEEFOE; // j DPNQJMBUJPO &3303
^
^
1C<O API>ODJI ADMNO HJ?DAD@N OC@ M@A@M@I>@ K<M<H@O@M, <I? OC@I M@Q@MON OCDN
HJ?DAD><ODJI RC@I <I @S>@KODJI DN OCMJRI. 2IAJMOPI<O@GT, BEEFOE DN <>>@NND=G@ JIGT DI
OC@ USZ =GJ>F, RC@M@ DO DN ?@ADI@?. (Note: ):;E ;E D7>3F76 FA @3?7 EB357E; 3E I7>> 3E
A4<75F >;87F;?7E F:3F I;>> 47 7JB>3;@76 ;@ 3 >3F7D 5:3BF7D.)
t 4MDODIB <GG JA KJO@IOD<GGT PIM@G<O@? @SKM@NNDJIN DI OC@ NDIBG@ GJOBMMZ =GJ>F <O OC@ =JOOJH
N@K<M<O@N OCJN@ @SKM@NNDJIN AMJH OC@ <>OP<G >J?@ OC<O OC@T <M@ M@G<O@? OJ.
1C@ TDPQF NO<O@H@ION C<Q@ NDHDG<M API>ODJI<GDOT RDOC OC@ DBUDI <I? GJOBMMZ N>JK@N =PO OC@T
<M@ =@OO@M DI H<IT M@NK@>ON. 1C@ OCM@@ TDPQF NO<O@H@ION <M@ <GNJ <=JPO @SKM@NNDJIN OC<O
NCJPG? =@ @S@>PO@? RC@I G@<QDIB N>JK@N:
t TDPQF(FYJU): OC@ @SKM@NNDJI DN <GR<TN @S@>PO@? RC@I @SDODIB OC@ N>JK@, M@B<M?G@NN JA
RC@OC@M NP>>@NNAPGGT JM ?P@ OJ <I @S>@KODJI
t TDPQF(TVDDFTT): OC@ @SKM@NNDJI DN @S@>PO@? JIGT DA OC@ N>JK@ DN =@DIB @SDO@? NP>>@NNAPGGT
t TDPQF(GBJMVSF): OC@ @SKM@NNDJI DN @S@>PO@? JIGT DA OC@ N>JK@ DN =@DIB @SDO@? ?P@ OJ <I
@S>@KODJI
AGOCJPBC OC@N@ NO<O@H@ION <M@ >GJN@GT M@G<O@? OJ @S>@KODJIN, OC@T ><I =@ PN@? RDOCJPO < USZ-
DBUDI =GJ>F.
AN <I @S<HKG@, G@O'N RMDO@ OC@ API>ODJI <=JQ@ RDOC < TDPQF(GBJMVSF) NO<O@H@IO:
WPJE GPP(SFG JOU S)
\
JOU BEEFOE = 42;
S += BEEFOE;
TDPQF(GBJMVSF) S -= BEEFOE;
NBZ5ISPX();
^
1C@ TDPQF(GBJMVSF) NO<O@H@IO <=JQ@ @INPM@N OC<O OC@ S -= BEEFOE @SKM@NNDJI RDGG =@
@S@>PO@? DA OC@ API>ODJI'N N>JK@ DN @SDO@? ?P@ OJ <I @S>@KODJI. A =@I@ADO JA TDPQF(GBJMVSF) DN
OC@ A<>O OC<O OC@ @SKM@NNDJI OC<O M@Q@MON <IJOC@M @SKM@NNDJI DN RMDOO@I >GJN@ OJ DO.
E5AB7
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
205
TDPQF NO<O@H@ION ><I =@ NK@>DAD@? <N =GJ>FN <N R@GG:
TDPQF(FYJU) \
// ... FYQSFTTJPOT ...
^
%@M@ DN <IJOC@M API>ODJI OC<O O@NON <GG OCM@@ JA OC@N@ NO<O@H@ION:
WPJE UFTU()
\
TDPQF(FYJU) XSJUFMO("XIFO FYJUJOH 1");
TDPQF(TVDDFTT) \
XSJUFMO("JG TVDDFTTGVM 1");
XSJUFMO("JG TVDDFTTGVM 2");
^
TDPQF(GBJMVSF) XSJUFMO("JG UISPXO 1");
TDPQF(FYJU) XSJUFMO("XIFO FYJUJOH 2");
TDPQF(GBJMVSF) XSJUFMO("JG UISPXO 2");
UISPXT)BMG5IF5JNF();
^
&A IJ @S>@KODJI DN OCMJRI, OC@ JPOKPO JA OC@ API>ODJI DI>GP?@N JIGT OC@ TDPQF(FYJU) <I?
TDPQF(TVDDFTT) @SKM@NNDJIN:
XIFO FYJUJOH 2
JG TVDDFTTGVM 1
JG TVDDFTTGVM 2
XIFO FYJUJOH 1
&A <I @S>@KODJI DN OCMJRI, OC@ JPOKPO DI>GP?@N OC@ TDPQF(FYJU) <I? TDPQF(GBJMVSF)
@SKM@NNDJIN:
JG UISPXO 2
XIFO FYJUJOH 2
JG UISPXO 1
XIFO FYJUJOH 1
PCKFDU.&YDFQUJPO!...: UIF FSSPS NFTTBHF
AN N@@I DI OC@ JPOKPON, OC@ =GJ>FN JA OC@ TDPQF NO<O@H@ION <M@ @S@>PO@? DI M@Q@MN@ JM?@M. 1CDN
DN =@><PN@ G<O@M >J?@ H<T ?@K@I? JI KM@QDJPN Q<MD<=G@N. "S@>PODIB OC@ TDPQF NO<O@H@ION DI
M@Q@MN@ JM?@M @I<=G@N PI?JDIB ND?@ @AA@>ON JA @<MGD@M @SKM@NNDJIN DI < >JINDNO@IO JM?@M.
E5AB7
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
206
BTTFSU and FOGPSDF 42
&I OC@ KM@QDJPN ORJ >C<KO@MN R@ C<Q@ N@@I CJR @S>@KODJIN <I? TDPQF NO<O@H@ION <M@ PN@?
OJR<M?N KMJBM<H >JMM@>OI@NN. BTTFSU DN <IJOC@M KJR@MAPG OJJG OJ <>CD@Q@ OC@ N<H@ BJ<G =T
@INPMDIB OC<O >@MO<DI <NNPHKODJIN OC<O OC@ KMJBM<H DN =<N@? JI <M@ Q<GD?.
&O H<T NJH@ODH@N =@ ?DAAD>PGO OJ ?@>D?@ RC@OC@M OJ OCMJR <I @S>@KODJI JM OJ ><GG BTTFSU. & RDGG
PN@ BTTFSU DI <GG JA OC@ @S<HKG@N =@GJR RDOCJPO HP>C EPNODAD><ODJI. & RDGG @SKG<DI OC@
?DAA@M@I>@N G<O@M DI OC@ >C<KO@M.
AGOCJPBC IJO <GR<TN J=QDJPN, KMJBM<HN <M@ APGG JA <NNPHKODJIN. #JM @S<HKG@, OC@ AJGGJRDIB
API>ODJI DN RMDOO@I PI?@M OC@ <NNPHKODJI OC<O =JOC <B@ K<M<H@O@MN <M@ BM@<O@M OC<I JM @LP<G
OJ U@MJ:
EPVCMF BWFSBHF"HF(EPVCMF GJSTU, EPVCMF TFDPOE)
\
SFUVSO (GJSTU + TFDPOE) / 2;
^
AGOCJPBC DO H<T =@ DIQ<GD? AJM OC@ KMJBM<H OJ @Q@M C<Q@ <I <B@ Q<GP@ OC<O DN I@B<ODQ@, OC@
API>ODJI RJPG? NODGG KMJ?P>@ <I <Q@M<B@, RCD>C H<T =@ PN@? DI OC@ KMJBM<H PIIJOD>@?,
M@NPGODIB DI OC@ KMJBM<H'N >JIODIPDIB RDOC DI>JMM@>O ?<O<.
AN <IJOC@M @S<HKG@, OC@ AJGGJRDIB API>ODJI <NNPH@N OC<O DO RDGG <GR<TN =@ ><GG@? RDOC ORJ
>JHH<I?N: "NDIB" JM "?<I>@":
WPJE BQQMZ$PNNBOE(TUSJOH DPNNBOE)
\
JG (DPNNBOE == "TJOH") \
SPCPU4JOH();
^ FMTF \
SPCPU%BODF();
^
^
B@><PN@ JA OC<O <NNPHKODJI, OC@ SPCPU%BODF() API>ODJI RJPG? =@ ><GG@? AJM @Q@MT >JHH<I?
JOC@M OC<I "NDIB", Q<GD? JM DIQ<GD?.
4C@I NP>C <NNPHKODJIN <M@ F@KO JIGT DI OC@ KMJBM<HH@M'N HDI?, OC@ KMJBM<H H<T @I? PK
RJMFDIB DI>JMM@>OGT. BTTFSU >C@>FN <NNPHKODJIN <I? O@MHDI<O@N KMJBM<HN DHH@?D<O@GT RC@I
OC@T <M@ IJO Q<GD?.
Syntax
42.1
BTTFSU ><I =@ PN@? DI ORJ R<TN:
BTTFSU(logical_expression);
BTTFSU(logical_expression, message);
1C@ GJBD><G @SKM@NNDJI M@KM@N@ION <I <NNPHKODJI <=JPO OC@ KMJBM<H. BTTFSU @Q<GP<O@N OC<O
@SKM@NNDJI OJ Q<GD?<O@ OC<O <NNPHKODJI. &A OC@ Q<GP@ JA OC@ GJBD><G @SKM@NNDJI DN USVF OC@I OC@
<NNPHKODJI DN >JIND?@M@? OJ =@ Q<GD?. ,OC@MRDN@ OC@ <NNPHKODJI DN DIQ<GD? <I? <I
"TTFSU&SSPS DN OCMJRI.
AN DON I<H@ NPBB@NON, OCDN @S>@KODJI DN DIC@MDO@? AMJH &SSPS, <I? <N TJP H<T M@H@H=@M AMJH
OC@ "S>@KODJIN >C<KO@M, @S>@KODJIN OC<O <M@ DIC@MDO@? AMJH &SSPS HPNO I@Q@M =@ ><PBCO. &O DN
3EE7DF 3@6 7@8AD57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
207
DHKJMO<IO AJM OC@ KMJBM<H OJ =@ O@MHDI<O@? MDBCO <R<T DINO@<? JA >JIODIPDIB PI?@M DIQ<GD?
<NNPHKODJIN.
1C@ ORJ DHKGD>DO <NNPHKODJIN JA BWFSBHF"HF() <=JQ@ H<T =@ NK@GG@? JPO =T ORJ BTTFSU ><GGN
<N DI OC@ AJGGJRDIB API>ODJI:
EPVCMF BWFSBHF"HF(EPVCMF GJSTU, EPVCMF TFDPOE)
\
BTTFSU(GJSTU >= 0);
BTTFSU(TFDPOE >= 0);
SFUVSO (GJSTU + TFDPOE) / 2;
^
WPJE NBJO()
\
BVUP SFTVMU = BWFSBHF"HF(-1, 10);
^
1CJN@ BTTFSU >C@>FN ><MMT OC@ H@<IDIB "<NNPHDIB OC<O =JOC JA OC@ <B@N BM@<O@M OC<I JM
@LP<G OJ U@MJ". &O ><I <GNJ =@ OCJPBCO JA <N H@<IDIB "OCDN API>ODJI ><I RJMF >JMM@>OGT JIGT DA
=JOC JA OC@ <B@N <M@ BM@<O@M OC<I JM @LP<G OJ U@MJ".
BTTFSU >C@>FN OCJN@ <NNPHKODJIN <I? O@MHDI<O@N OC@ KMJBM<H RDOC <I "TTFSU&SSPS RC@I
OC@T <M@ IJO Q<GD?:
DPSF.FYDFQUJPO."TTFSU&SSPS!EFOFNF(3): "TTFSUJPO GBJMVSF
1C@ K<MO <AO@M OC@ ! >C<M<>O@M DI OC@ H@NN<B@ DI?D><O@N OC@ NJPM>@ ADG@ <I? OC@ GDI@ IPH=@M JA
OC@ BTTFSU >C@>F OC<O C<N A<DG@?. A>>JM?DIB OJ OC@ JPOKPO <=JQ@, OC@ BTTFSU OC<O C<N A<DG@? DN
JI GDI@ 3 JA ADG@ EFOFNF.E.
1C@ JOC@M NTIO<S JA BTTFSU <GGJRN KMDIODIB < >PNOJH H@NN<B@ RC@I OC@ BTTFSU >C@>F A<DGN:
BTTFSU(GJSTU >= 0, ""HF DBOOPU CF OFHBUJWF.");
1C@ JPOKPO:
DPSF.FYDFQUJPO."TTFSU&SSPS!EFOFNF.E(3): "HF DBOOPU CF OFHBUJWF.
0JH@ODH@N DO DN OCJPBCO OJ =@ DHKJNND=G@ AJM OC@ KMJBM<H OJ @Q@M @IO@M < >J?@ K<OC. &I NP>C
><N@N DO DN >JHHJI OJ PN@ OC@ GDO@M<G GBMTF <N OC@ GJBD><G @SKM@NNDJI OJ A<DG <I BTTFSU >C@>F.
#JM @S<HKG@, OJ DI?D><O@ OC<O BQQMZ$PNNBOE() API>ODJI DN I@Q@M @SK@>O@? OJ =@ ><GG@? RDOC <
>JHH<I? JOC@M OC<I "NDIB" <I? "?<I>@", <I? OJ BP<M? <B<DINO NP>C < KJNND=DGDOT, <I
BTTFSU(GBMTF) ><I =@ DIN@MO@? DIOJ OC@ ;?BAEE;4>7 =M<I>C:
WPJE BQQMZ$PNNBOE(TUSJOH DPNNBOE)
\
JG (DPNNBOE == "TJOH") \
SPCPU4JOH();
^ FMTF JG (DPNNBOE == "EBODF") \
SPCPU%BODF();
^ FMTF \
BTTFSU(GBMTF);
^
^
3EE7DF 3@6 7@8AD57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
208
1C@ API>ODJI DN BP<M<IO@@? OJ RJMF RDOC OC@ JIGT ORJ >JHH<I?N OC<O DO FIJRN <=JPO. (Note:
A@ 3>F7D@3F;H7 5:A;57 :7D7 IAG>6 47 FA GE7 3 final switch EF3F7?7@F.)
TUBUJD BTTFSU
42.2
0DI>@ BTTFSU >C@>FN <M@ AJM >JMM@>O @S@>PODJI JA KMJBM<HN, OC@T <M@ <KKGD@? RC@I OC@
KMJBM<H DN <>OP<GGT MPIIDIB. ,OC@M >C@>FN <M@ <=JPO OC@ NOMP>OPM@ JA OC@ KMJBM<H <I? ><I =@
<KKGD@? @Q@I <O >JHKDG@ ODH@.
TUBUJD BTTFSU DN OC@ >JPIO@MK<MO JA BTTFSU OC<O DN <KKGD@? <O >JHKDG@ ODH@. 1C@ <?Q<IO<B@ DN
OC<O DO ?J@N IJO <GGJR @Q@I >JHKDGDIB < KMJBM<H OC<O RJPG? C<Q@ JOC@MRDN@ MPI DI>JMM@>OGT. A
I<OPM<G M@LPDM@H@IO DN OC<O DO HPNO =@ KJNND=G@ OJ @Q<GP<O@ OC@ GJBD><G @SKM@NNDJI <O >JHKDG@
ODH@.
#JM @S<HKG@, <NNPHDIB OC<O OC@ ODOG@ JA < H@IP RDGG =@ KMDIO@? JI <I JPOKPO ?@QD>@ OC<O C<N
GDHDO@? RD?OC, OC@ AJGGJRDIB TUBUJD BTTFSU @INPM@N OC<O DO RDGG I@Q@M =@ RD?@M OC<I OC<O
GDHDO:
FOVN ETUSJOH NFOV5JUMF = "$PNNBOE .FOV";
TUBUJD BTTFSU(NFOV5JUMF.MFOHUI <= 16);
+JO@ OC<O OC@ NOMDIB DN ?@ADI@? <N FOVN NJ OC<O DON G@IBOC ><I =@ @Q<GP<O@? <O >JHKDG@ ODH@.
)@O'N <NNPH@ OC<O < KMJBM<HH@M >C<IB@N OC<O ODOG@ OJ H<F@ DO HJM@ ?@N>MDKODQ@:
FOVN ETUSJOH NFOV5JUMF = "%JSFDUJPOBM $PNNBOET .FOV";
TUBUJD BTTFSU(NFOV5JUMF.MFOHUI <= 16);
1C@ TUBUJD BTTFSU >C@>F KM@Q@ION >JHKDGDIB OC@ KMJBM<H:
&SSPS: TUBUJD BTTFSU (25V <= 16V) JT GBMTF
1CDN RJPG? M@HDI? OC@ KMJBM<HH@M JA OC@ GDHDO<ODJI JA OC@ JPOKPO ?@QD>@.
TUBUJD BTTFSU DN @Q@I HJM@ PN@APG RC@I PN@? DI O@HKG<O@N. 4@ RDGG N@@ O@HKG<O@N DI G<O@M
>C<KO@MN.
BTTFSU even if absolutely true
42.3
& @HKC<NDU@ "<=NJGPO@GT OMP@" =@><PN@ <NNPHKODJIN <=JPO OC@ KMJBM<H <M@ I@Q@M @SK@>O@? OJ
=@ A<GN@ <ITR<T. A G<MB@ N@O JA KMJBM<H @MMJMN <M@ =<N@? JI <NNPHKODJIN OC<O <M@ OCJPBCO OJ
=@ <=NJGPO@GT OMP@.
#JM OC<O M@<NJI, O<F@ <?Q<IO<B@ JA BTTFSU >C@>FN @Q@I DA OC@T A@@G PII@>@NN<MT. )@O'N GJJF <O
OC@ AJGGJRDIB API>ODJI OC<O M@OPMIN OC@ ?<TN JA HJIOCN DI < BDQ@I T@<M:
JOU<> NPOUI%BZT(JO JOU ZFBS)
\
JOU<> EBZT = <
31, GFCSVBSZ%BZT(ZFBS),
31, 30, 31, 30, 31, 31, 30, 31, 30, 31
>;
BTTFSU((TVN(EBZT) == 365) ]]
(TVN(EBZT) == 366));
SFUVSO EBZT;
^
3EE7DF 3@6 7@8AD57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
209
1C<O BTTFSU >C@>F H<T =@ N@@I <N PII@>@NN<MT =@><PN@ OC@ API>ODJI RJPG? I<OPM<GGT M@OPMI
@DOC@M 365 JM 366. %JR@Q@M, OCJN@ >C@>FN <M@ BP<M?DIB <B<DINO KJO@IOD<G HDNO<F@N @Q@I DI OC@
GFCSVBSZ%BZT() API>ODJI. #JM @S<HKG@, OC@ KMJBM<H RJPG? =@ O@MHDI<O@? DA GFCSVBSZ%BZT()
M@OPMI@? 30.
AIJOC@M N@@HDIBGT PII@>@NN<MT >C@>F ><I @INPM@ OC<O OC@ G@IBOC JA OC@ NGD>@ RJPG? <GR<TN
=@ 12:
BTTFSU(EBZT.MFOHUI == 12);
1C<O R<T, ?@G@ODIB JM <??DIB @G@H@ION OJ OC@ NGD>@ PIDIO@IOD<GGT RJPG? <GNJ =@ ><PBCO. 0P>C
>C@>FN <M@ DHKJMO<IO OJJGN OJR<M?N KMJBM<H >JMM@>OI@NN.
BTTFSU DN <GNJ OC@ =<ND> OJJG OC<O DN PN@? DI G@;F F7EF;@9 <I? 5A@FD35F BDA9D3??;@9, =JOC JA
RCD>C RDGG =@ >JQ@M@? DI G<O@M >C<KO@MN.
No value nor side effect
42.4
4@ C<Q@ N@@I OC<O @SKM@NNDJIN KMJ?P>@ Q<GP@N JM H<F@ ND?@ @AA@>ON. BTTFSU >C@>FN ?J IJO
C<Q@ Q<GP@N IJM OC@T E:AG>6 C<Q@ <IT ND?@ @AA@>ON.
1C@ ! G<IBP<B@ M@LPDM@N OC<O OC@ @Q<GP<ODJI JA OC@ GJBD><G @SKM@NNDJI HPNO IJO C<Q@ <IT ND?@
@AA@>O. BTTFSU HPNO M@H<DI <N < K<NNDQ@ J=N@MQ@M JA KMJBM<H NO<O@.
Disabling BTTFSU checks
42.5
0DI>@ BTTFSU DN <=JPO KMJBM<H >JMM@>OI@NN, OC@T ><I =@ N@@I <N PII@>@NN<MT JI>@ OC@
KMJBM<H C<N =@@I O@NO@? NPAAD>D@IOGT. #PMOC@M, NDI>@ BTTFSU >C@>FN KMJ?P>@ IJ Q<GP@N IJM
OC@T C<Q@ ND?@ @AA@>ON, M@HJQDIB OC@H AMJH OC@ KMJBM<H NCJPG? IJO H<F@ <IT ?DAA@M@I>@.
1C@ >JHKDG@M NRDO>C -SFMFBTF ><PN@N OC@ BTTFSU >C@>FN OJ =@ DBIJM@? <N DA OC@T C<Q@ I@Q@M
=@@I DI>GP?@? DI OC@ KMJBM<H:
ENE EFOFNF.E -SFMFBTF
1CDN RJPG? <GGJR KMJBM<HN MPI A<NO@M =T IJO @Q<GP<ODIB KJO@IOD<GGT NGJR GJBD><G @SKM@NNDJIN
JA OC@ BTTFSU >C@>FN.
AN <I @S>@KODJI, OC@ BTTFSU >C@>FN OC<O C<Q@ OC@ GDO@M<G GBMTF (JM 0) <N OC@ GJBD><G
@SKM@NNDJI <M@ IJO ?DN<=G@? @Q@I RC@I OC@ KMJBM<H DN >JHKDG@? RDOC jSFMFBTF. 1CDN DN
=@><PN@ BTTFSU(GBMTF) DN AJM @INPMDIB OC<O < =GJ>F JA >J?@ DN I@Q@M M@<>C@?, <I? OC<O NCJPG?
=@ KM@Q@IO@? @Q@I AJM jSFMFBTF =PDG?N.
FOGPSDF for throwing exceptions
42.6
+JO @Q@MT PI@SK@>O@? NDOP<ODJI DN <I DI?D><ODJI JA < KMJBM<H @MMJM. -MJBM<HN H<T <GNJ
@SK@MD@I>@ PI@SK@>O@? DIKPON <I? PI@SK@>O@? @IQDMJIH@IO<G NO<O@. #JM @S<HKG@, OC@ ?<O<
OC<O DN @IO@M@? =T OC@ PN@M NCJPG? IJO =@ Q<GD?<O@? =T <I BTTFSU >C@>F =@><PN@ DIQ<GD? ?<O<
C<N IJOCDIB OJ ?J RDOC OC@ >JMM@>OI@NN JA OC@ KMJBM<H DON@GA. &I NP>C ><N@N DO DN <KKMJKMD<O@ OJ
OCMJR @S>@KODJIN GDF@ R@ C<Q@ =@@I ?JDIB DI KM@QDJPN KMJBM<HN.
TUE.FYDFQUJPO.FOGPSDF DN < >JIQ@ID@IO R<T JA OCMJRDIB @S>@KODJIN. #JM @S<HKG@, G@O'N
<NNPH@ OC<O <I @S>@KODJI HPNO =@ OCMJRI RC@I < NK@>DAD> >JI?DODJI DN IJO H@O:
3EE7DF 3@6 7@8AD57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
210
JG (DPVOU < 3) \
UISPX OFX &YDFQUJPO(".VTU CF BU MFBTU 3.");
^
FOGPSDF() DN < RM<KK@M <MJPI? OC@ >JI?DODJI >C@>F <I? OC@ UISPX NO<O@H@IO. 1C@ AJGGJRDIB
DN OC@ @LPDQ<G@IO JA OC@ KM@QDJPN >J?@:
JNQPSU TUE.FYDFQUJPO;
// ...
FOGPSDF(DPVOU >= 3, ".VTU CF BU MFBTU 3.");
+JO@ CJR OC@ GJBD><G @SKM@NNDJI DN I@B<O@? >JHK<M@? RDOC OC@ JG NO<O@H@IO, <N DO IJR NK@GGN
JPO RC<O DN =@DIB 7@8AD576.
How to use
42.7
BTTFSU DN AJM ><O>CDIB KMJBM<HH@M @MMJMN. 1C@ >JI?DODJIN OC<O BTTFSU BP<M?N <B<DINO DI OC@
NPOUI%BZT() API>ODJI <I? OC@ NFOV5JUMF Q<MD<=G@ <=JQ@ <M@ <GG <=JPO KMJBM<HH@M HDNO<F@N.
0JH@ODH@N DO DN ?DAAD>PGO OJ ?@>D?@ RC@OC@M OJ M@GT JI <I BTTFSU >C@>F JM OJ OCMJR <I
@S>@KODJI. 1C@ ?@>DNDJI NCJPG? =@ =<N@? JI RC@OC@M OC@ PI@SK@>O@? NDOP<ODJI DN ?P@ OJ <
KMJ=G@H RDOC CJR OC@ KMJBM<H C<N =@@I >J?@?.
,OC@MRDN@, OC@ KMJBM<H HPNO OCMJR <I @S>@KODJI RC@I DO DN IJO KJNND=G@ OJ <>>JHKGDNC < O<NF.
FOGPSDF() DN @SKM@NNDQ@ <I? >JIQ@ID@IO RC@I OCMJRDIB @S>@KODJIN.
AIJOC@M KJDIO OJ >JIND?@M DN RC@OC@M OC@ PI@SK@>O@? NDOP<ODJI ><I =@ M@H@?D@? DI <IT R<T. &A
OC@ KMJBM<H ><I ?J <ITOCDIB NK@>D<G, @Q@I =T NDHKGT KMDIODIB <I @MMJM H@NN<B@ <=JPO OC@
KMJ=G@H RDOC NJH@ DIKPO ?<O<, OC@I DO DN <KKMJKMD<O@ OJ OCMJR <I @S>@KODJI. 1C<O R<T, ><GG@MN
JA OC@ >J?@ OC<O OCM@R OC@ @S>@KODJI ><I ><O>C DO OJ ?J NJH@OCDIB NK@>D<G OJ M@>JQ@M AMJH OC@
@MMJM >JI?DODJI.
Exercises
42.8
1. 1C@ AJGGJRDIB KMJBM<H DI>GP?@N < IPH=@M JA BTTFSU >C@>FN. JHKDG@ <I? MPI OC@
KMJBM<H OJ ?DN>JQ@M DON =PBN OC<O <M@ M@Q@<G@? =T OCJN@ BTTFSU >C@>FN.
1C@ KMJBM<H O<F@N < NO<MO ODH@ <I? < ?PM<ODJI AMJH OC@ PN@M <I? ><G>PG<O@N OC@ @I?
ODH@ =T <??DIB OC@ ?PM<ODJI OJ OC@ NO<MO ODH@:
10 IPVST BOE 8 NJOVUFT BGUFS 06:09 JT 16:17.
+JO@ OC<O OCDN KMJ=G@H ><I =@ RMDOO@I DI < HP>C >G@<I@M R<T =T ?@ADIDIB TUSVDU
OTK@N. 4@ RDGG M@A@M OJ OCDN KMJBM<H DI G<O@M >C<KO@MN.
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
JNQPSU TUE.FYDFQUJPO;
/* 3FBET UIF UJNF BT IPVS BOE NJOVUF BGUFS QSJOUJOH B
* NFTTBHF. */
WPJE SFBE5JNF(JO TUSJOH NFTTBHF, PVU JOU IPVS, PVU JOU NJOVUF)
\
XSJUF(NFTTBHF, " ()):..) ");
SFBEG(" %T:%T", &IPVS, &NJOVUF);
FOGPSDF((IPVS >= 0) && (IPVS <= 23) &&
(NJOVUF >= 0) && (NJOVUF <= 59),
3EE7DF 3@6 7@8AD57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
211
"*OWBMJE UJNF!");
^
/* 3FUVSOT UIF UJNF JO TUSJOH GPSNBU. */
TUSJOH UJNF5P4USJOH(JO JOU IPVS, JO JOU NJOVUF)
\
BTTFSU((IPVS >= 0) && (IPVS <= 23));
BTTFSU((NJOVUF >= 0) && (NJOVUF <= 59));
SFUVSO GPSNBU("%02T:%02T", IPVS, NJOVUF);
^
/* "EET EVSBUJPO UP TUBSU UJNF BOE SFUVSOT UIF SFTVMU BT UIF
* UIJSE QBJS PG QBSBNFUFST. */
WPJE BEE%VSBUJPO(JO JOU TUBSU)PVS, JO JOU TUBSU.JOVUF,
JO JOU EVSBUJPO)PVS, JO JOU EVSBUJPO.JOVUF,
PVU JOU SFTVMU)PVS, PVU JOU SFTVMU.JOVUF)
\
SFTVMU)PVS = TUBSU)PVS + EVSBUJPO)PVS;
SFTVMU.JOVUF = TUBSU.JOVUF + EVSBUJPO.JOVUF;
JG (SFTVMU.JOVUF > 59) \
++SFTVMU)PVS;
^
^
WPJE NBJO()
\
JOU TUBSU)PVS;
JOU TUBSU.JOVUF;
SFBE5JNF("4UBSU UJNF", TUBSU.JOVUF, TUBSU)PVS);
JOU EVSBUJPO)PVS;
JOU EVSBUJPO.JOVUF;
SFBE5JNF("%VSBUJPO", EVSBUJPO)PVS, EVSBUJPO.JOVUF);
JOU FOE)PVS;
JOU FOE.JOVUF;
BEE%VSBUJPO(TUBSU)PVS, TUBSU.JOVUF,
EVSBUJPO)PVS, EVSBUJPO.JOVUF,
FOE)PVS, FOE.JOVUF);
XSJUFGMO("%T IPVST BOE %T NJOVUFT BGUFS %T JT %T.",
EVSBUJPO)PVS, EVSBUJPO.JOVUF,
UJNF5P4USJOH(TUBSU)PVS, TUBSU.JOVUF),
UJNF5P4USJOH(FOE)PVS, FOE.JOVUF));
^
/PI OC@ KMJBM<H <I? @IO@M 06:09 <N OC@ NO<MO ODH@ <I? 1:2 <N OC@ ?PM<ODJI. ,=N@MQ@
OC<O OC@ KMJBM<H O@MHDI<O@N IJMH<GGT.
Note: .AG ?3K @AF;57 3 BDA4>7? I;F: F:7 AGFBGF. !9@AD7 F:3F BDA4>7? 8AD @AI 3E KAG
I;>> 6;E5AH7D ;F 4K F:7 :7>B A8 assert 5:75=E EAA@.
2. 1CDN ODH@ @IO@M 06:09 <I? 15:2. ,=N@MQ@ OC<O OC@ KMJBM<H DN O@MHDI<O@? =T <I
"TTFSU&SSPS. $J OJ OC@ GDI@ JA OC@ KMJBM<H OC<O DN DI?D><O@? DI OC@ <NN@MO H@NN<B@ <I?
N@@ RCD>C JI@ JA OC@ BTTFSU >C@>FN C<Q@ A<DG@?. &O H<T O<F@ < RCDG@ OJ ?DN>JQ@M OC@ ><PN@
JA OCDN K<MOD>PG<M A<DGPM@.
3. "IO@M 06:09 <I? 20:0 <I? J=N@MQ@ OC<O OC@ N<H@ BTTFSU >C@>F NODGG A<DGN <I? ADS OC<O =PB
<N R@GG.
4. *J?DAT OC@ KMJBM<H OJ KMDIO OC@ ODH@N DI 12-CJPM AJMH<O RDOC <I "<H" JM "KH" DI?D><OJM.
... F:7 EA>GF;A@E
3EE7DF 3@6 7@8AD57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
212
Unit Testing 43
AN DO NCJPG? =@ FIJRI =T HJNO K@JKG@, <IT ?@QD>@ OC<O MPIN NJH@ KD@>@ JA >JHKPO@M KMJBM<H
>JIO<DIN NJAOR<M@ =PBN. 0JAOR<M@ =PBN KG<BP@ >JHKPO@M ?@QD>@N AMJH OC@ NDHKG@NO OJ OC@
HJNO >JHKG@S. !@=PBBDIB <I? ADSDIB NJAOR<M@ =PBN DN <HJIB OC@ G@NN A<QJM<=G@ ?<DGT
<>ODQDOD@N JA < KMJBM<HH@M.
Causes of bugs
43.1
1C@M@ <M@ H<IT M@<NJI RCT NJAOR<M@ =PBN J>>PM. 1C@ AJGGJRDIB DN <I DI>JHKG@O@ GDNO MJPBCGT
AMJH OC@ ?@NDBI NO<B@ JA < KMJBM<H OCMJPBC OC@ <>OP<G >J?DIB JA DO:
t 1C@ M@LPDM@H@ION <I? OC@ NK@>DAD><ODJIN JA OC@ KMJBM<H H<T IJO =@ >G@<M. 4C<O OC@
KMJBM<H NCJPG? <>OP<GGT ?J H<T IJO =@ FIJRI <O OC@ ?@NDBI NO<B@.
t 1C@ KMJBM<HH@M H<T HDNPI?@MNO<I? NJH@ JA OC@ M@LPDM@H@ION JA OC@ KMJBM<H.
t 1C@ KMJBM<HHDIB G<IBP<B@ H<T IJO =@ @SKM@NNDQ@ @IJPBC. JIND?@MDIB OC<O OC@M@ <M@
>JIAPNDJIN @Q@I =@OR@@I I<ODQ@ NK@<F@MN JA CPH<I G<IBP<B@N, OC@ PII<OPM<G NTIO<S <I?
MPG@N JA < KMJBM<HHDIB G<IBP<B@ H<T =@ ><PN@ JA HDNO<F@N.
t @MO<DI <NNPHKODJIN JA OC@ KMJBM<HH@M H<T =@ DI>JMM@>O. #JM @S<HKG@, OC@ KMJBM<HH@M
H<T =@ <NNPHDIB OC<O 3.14 RJPG? =@ KM@>DN@ @IJPBC OJ M@KM@N@IO h.
t 1C@ KMJBM<HH@M H<T C<Q@ DI>JMM@>O DIAJMH<ODJI JI < OJKD> JM IJI@ <O <GG. #JM @S<HKG@,
OC@ KMJBM<HH@M H<T IJO FIJR OC<O PNDIB < AGJ<ODIB KJDIO Q<MD<=G@ DI < K<MOD>PG<M GJBD><G
@SKM@NNDJI RJPG? IJO =@ M@GD<=G@.
t 1C@ KMJBM<H H<T @I>JPIO@M <I PIAJM@N@@I NDOP<ODJI. #JM @S<HKG@, JI@ JA OC@ ADG@N JA <
?DM@>OJMT H<T =@ ?@G@O@? JM M@I<H@? RCDG@ OC@ KMJBM<H DN PNDIB OC@ ADG@N JA OC<O ?DM@>OJMT
DI < GPSFBDI GJJK.
t 1C@ KMJBM<HH@M H<T H<F@ NDGGT HDNO<F@N. #JM @S<HKG@, OC@ I<H@ JA < Q<MD<=G@ H<T =@
HDNOTK@? <I? <>>D?@IO<GGT H<O>C@? OC@ I<H@ JA <IJOC@M Q<MD<=G@.
t @O>.
2IAJMOPI<O@GT, OC@M@ DN NODGG IJ NJAOR<M@ ?@Q@GJKH@IO H@OCJ?JGJBT OC<O @INPM@N OC<O <
KMJBM<H RDGG <GR<TN RJMF >JMM@>OGT. 1CDN DN NODGG < CJO NJAOR<M@ @IBDI@@MDIB OJKD> RC@M@
KMJHDNDIB NJGPODJIN @H@MB@ @Q@MT ?@><?@ JM NJ.
Discovering the bugs
43.2
0JAOR<M@ =PBN <M@ ?DN>JQ@M@? <O Q<MDJPN NO<B@N JA OC@ GDA@ODH@ JA OC@ KMJBM<H =T Q<MDJPN
OTK@N JA OJJGN <I? K@JKG@. 1C@ AJGGJRDIB DN < K<MOD<G GDNO JA RC@I < =PB H<T =@ ?DN>JQ@M@?,
AMJH OC@ @<MGD@NO OJ OC@ G<O@NO:
t 4C@I RMDODIB OC@ KMJBM<H
w BT OC@ KMJBM<HH@M
w BT <IJOC@M KMJBM<HH@M ?PMDIB B3;D BDA9D3??;@9
w BT OC@ >JHKDG@M OCMJPBC >JHKDG@M H@NN<B@N
w BT PIDO O@NON <N < K<MO JA =PDG?DIB OC@ KMJBM<H
t 4C@I M@QD@RDIB OC@ >J?@
w BT OJJGN OC<O <I<GTU@ OC@ >J?@ <O >JHKDG@ ODH@
w BT JOC@M KMJBM<HH@MN ?PMDIB 5A67 D7H;7IE
t 4C@I MPIIDIB OC@ KMJBM<H
w BT OJJGN OC<O <I<GTU@ OC@ @S@>PODJI JA OC@ KMJBM<H <O MPI ODH@ (@.B. =T Q<GBMDI?)
*@;F )7EF;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
213
w !PMDIB .A O@NODIB, @DOC@M =T OC@ A<DGPM@ JA BTTFSU >C@>FN JM =T OC@ J=N@MQ@?
=@C<QDJM JA OC@ KMJBM<H
w BT OC@ 47F3 PN@MN =@AJM@ OC@ M@G@<N@ JA OC@ KMJBM<H
w BT OC@ @I? PN@MN <AO@M OC@ M@G@<N@ JA OC@ KMJBM<H
!@O@>ODIB OC@ =PB <N @<MGT <N KJNND=G@ M@?P>@N GJNN JA HJI@T, ODH@, <I? DI NJH@ ><N@N CPH<I
GDQ@N. A??DODJI<GGT, D?@IODATDIB OC@ ><PN@N JA =PBN OC<O C<Q@ =@@I ?DN>JQ@M@? =T OC@ @I? PN@MN
<M@ C<M?@M OC<I D?@IODATDIB OC@ ><PN@N JA =PBN OC<O <M@ ?DN>JQ@M@? @<MGT, ?PMDIB ?@Q@GJKH@IO.
Unit testing for catching bugs
43.3
0DI>@ KMJBM<HN <M@ RMDOO@I =T KMJBM<HH@MN <I? ! DN < >JHKDG@? G<IBP<B@, OC@ KMJBM<HH@MN
<I? OC@ >JHKDG@M RDGG <GR<TN =@ OC@M@ OJ ?DN>JQ@M =PBN. 1CJN@ ORJ <ND?@, OC@ @<MGD@NO <I?
K<MOGT AJM OC<O M@<NJI OC@ HJNO @AA@>ODQ@ R<T JA ><O>CDIB =PBN DN PIDO O@NODIB.
2IDO O@NODIB DN <I DI?DNK@IN<=G@ K<MO JA HJ?@MI KMJBM<HHDIB. &O DN OC@ HJNO @AA@>ODQ@ H@OCJ?
JA M@?P>DIB >J?DIB @MMJMN. A>>JM?DIB OJ NJH@ ?@Q@GJKH@IO H@OCJ?JGJBD@N, >J?@ OC<O DN IJO
BP<M?@? =T PIDO O@NON DN =PBBT >J?@.
2IAJMOPI<O@GT, OC@ JKKJNDO@ DN IJO OMP@: 2IDO O@NON ?J IJO BP<M<IO@@ OC<O OC@ >J?@ DN AM@@ JA
=PBN. AGOCJPBC OC@T <M@ Q@MT @AA@>ODQ@, OC@T ><I JIGT M@?P>@ OC@ MDNF JA =PBN.
2IDO O@NODIB <GNJ @I<=G@N M@A<>OJMDIB OC@ >J?@ (D.@. H<FDIB DHKMJQ@H@ION OJ DO) RDOC @<N@ <I?
>JIAD?@I>@. ,OC@MRDN@, DO DN >JHHJI OJ <>>D?@IO<GGT =M@<F NJH@ JA OC@ @SDNODIB API>ODJI<GDOT
JA < KMJBM<H RC@I <??DIB I@R A@<OPM@N OJ DO. BPBN JA OCDN OTK@ <M@ ><GG@? D79D7EE;A@E.
4DOCJPO PIDO O@NODIB, M@BM@NNDJIN <M@ NJH@ODH@N ?DN>JQ@M@? <N G<O@ <N ?PMDIB OC@ .A O@NODIB
JA APOPM@ M@G@<N@N, JM RJMN@, =T OC@ @I? PN@MN.
/DNF JA M@BM@NNDJIN ?DN>JPM<B@ KMJBM<HH@MN AMJH M@A<>OJMDIB OC@ >J?@, NJH@ODH@N
KM@Q@IODIB OC@H AMJH K@MAJMHDIB OC@ NDHKG@NO JA DHKMJQ@H@ION GDF@ >JMM@>ODIB OC@ I<H@ JA <
Q<MD<=G@. 1CDN DI OPMI ><PN@N 5A67 DAF, < >JI?DODJI RC@M@ OC@ >J?@ =@>JH@N HJM@ <I? HJM@
PIH<DIO<DI<=G@. #JM @S<HKG@, <GOCJPBC NJH@ GDI@N JA >J?@ RJPG? =@OO@M =@ HJQ@? OJ < I@RGT
?@ADI@? API>ODJI DI JM?@M OJ =@ ><GG@? AMJH HJM@ OC<I JI@ KG<>@, A@<M JA M@BM@NNDJIN H<F@
KMJBM<HH@MN >JKT <I? K<NO@ OC@ @SDNODIB GDI@N OJ JOC@M KG<>@N DINO@<?, G@<?DIB OJ OC@
KMJ=G@H JA 5A67 6GB>;53F;A@.
-CM<N@N GDF@ "DA DO DNI'O =MJF@I, ?JI'O ADS DO" <M@ M@G<O@? OJ A@<M JA M@BM@NNDJIN. AGOCJPBC OC@T
N@@H OJ =@ >JIQ@TDIB RDN?JH, NP>C BPD?@GDI@N ><PN@ OC@ >J?@ OJ MJO NGJRGT <I? =@>JH@ <I
PIOJP>C<=G@ H@NN.
*J?@MI KMJBM<HHDIB M@E@>ON NP>C "RDN?JH". 1J OC@ >JIOM<MT, OJ KM@Q@IO DO AMJH =@>JHDIB <
NJPM>@ JA =PBN, OC@ >J?@ DN NPKKJN@? OJ =@ "M@A<>OJM@? H@M>DG@NNGT". 1C@ HJNO KJR@MAPG OJJG JA
OCDN HJ?@MI <KKMJ<>C DN PIDO O@NODIB.
2IDO O@NODIB DIQJGQ@N O@NODIB OC@ NH<GG@NO PIDON JA >J?@ DI?@K@I?@IOGT. 4C@I PIDON JA >J?@ <M@
O@NO@? DI?@K@I?@IOGT, DO DN G@NN GDF@GT OC<O OC@M@ <M@ =PBN DI CDBC@M-G@Q@G >J?@ OC<O PN@ OCJN@
PIDON. 4C@I OC@ K<MON RJMF >JMM@>OGT, DO DN HJM@ GDF@GT OC<O OC@ RCJG@ RDGG RJMF >JMM@>OGT <N
R@GG.
2IDON O@NON <M@ KMJQD?@? <N GD=M<MT NJGPODJIN DI JOC@M G<IBP<B@N (@.B. '2IDO, KK2IDO,
2IDOO@NO++, @O>.) &I !, PIDO O@NODIB DN < >JM@ A@<OPM@ JA OC@ G<IBP<B@. &O DN ?@=<O<=G@ RC@OC@M
< GD=M<MT NJGPODJI JM < G<IBP<B@ A@<OPM@ DN =@OO@M AJM PIDO O@NODIB. B@><PN@ ! ?J@N IJO KMJQD?@
NJH@ JA OC@ A@<OPM@N OC<O <M@ >JHHJIGT AJPI? DI PIDO O@NODIB GD=M<MD@N, DO H<T =@ RJMOCRCDG@
OJ >JIND?@M GD=M<MT NJGPODJIN <N R@GG.
1C@ PIDO O@NODIB A@<OPM@N JA ! <M@ <N NDHKG@ <N DIN@MODIB BTTFSU >C@>FN DIOJ VOJUUFTU =GJ>FN.
*@;F )7EF;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
214
Activating the unit tests
43.4
2IDO O@NON <M@ IJO < K<MO JA OC@ <>OP<G @S@>PODJI JA OC@ KMJBM<H. 1C@T NCJPG? =@ <>ODQ<O@?
JIGT ?PMDIB KMJBM<H ?@Q@GJKH@IO RC@I @SKGD>DOGT M@LP@NO@?.
1C@ ENE >JHKDG@M NRDO>C OC<O <>ODQ<O@N PIDO O@NON DN jVOJUUFTU.
ANNPHDIB OC<O OC@ KMJBM<H DN RMDOO@I DI < NDIBG@ NJPM>@ ADG@ I<H@? EFOFNF.E, DON PIDO O@NON
><I =@ <>ODQ<O@? =T OC@ AJGGJRDIB >JHH<I?:
ENE EFOFNF.E -X -VOJUUFTU
4C@I < KMJBM<H OC<O DN =PDGO =T OC@ jVOJUUFTU NRDO>C DN NO<MO@?, DON PIDO O@NO =GJ>FN <M@
@S@>PO@? ADMNO. ,IGT DA <GG JA OC@ PIDO O@NON K<NN OC<O OC@ KMJBM<H @S@>PODJI >JIODIP@N RDOC
NBJO().
VOJUUFTU blocks
43.5
1C@ GDI@N JA >J?@ OC<O DIQJGQ@ PIDO O@NON <M@ RMDOO@I DIND?@ VOJUUFTU =GJ>FN. 1C@N@ =GJ>FN ?J
IJO C<Q@ <IT NDBIDAD><I>@ AJM OC@ KMJBM<H JOC@M OC<I >JIO<DIDIB OC@ PIDO O@NON:
VOJUUFTU
\
/* ... UIF UFTUT BOE UIF DPEF UIBU TVQQPSU UIFN ... */
^
AGOCJPBC VOJUUFTU =GJ>FN ><I <KK@<M <ITRC@M@, DO DN >JIQ@ID@IO OJ ?@ADI@ OC@H MDBCO <AO@M
OC@ >J?@ OC<O OC@T O@NO.
AN <I @S<HKG@, G@O'N O@NO < API>ODJI OC<O M@OPMIN OC@ JM?DI<G AJMH JA OC@ NK@>DAD@? IPH=@M <N
DI "1NO", "2I?", @O>. A VOJUUFTU =GJ>F JA OCDN API>ODJI ><I NDHKGT >JIO<DI BTTFSU NO<O@H@ION
OC<O >JHK<M@ OC@ M@OPMI Q<GP@N JA OC@ API>ODJI OJ OC@ @SK@>O@? Q<GP@N. 1C@ AJGGJRDIB API>ODJI
DN =@DIB O@NO@? RDOC OC@ AJPM ?DNODI>O @SK@>O@? JPO>JH@N JA OC@ API>ODJI:
TUSJOH PSEJOBM(TJ[F@U OVNCFS)
\
// ...
^
VOJUUFTU
\
BTTFSU(PSEJOBM(1) == "1TU");
BTTFSU(PSEJOBM(2) == "2OE");
BTTFSU(PSEJOBM(3) == "3SE");
BTTFSU(PSEJOBM(10) == "10UI");
^
1C@ AJPM O@NON <=JQ@ O@NO OC<O OC@ API>ODJI RJMFN >JMM@>OGT <O G@<NO AJM OC@ Q<GP@N JA 1, 2, 3,
<I? 10 =T H<FDIB AJPM N@K<M<O@ ><GGN OJ OC@ API>ODJI <I? >JHK<MDIB OC@ M@OPMI@? Q<GP@N OJ
OC@ @SK@>O@? JI@N.
AGOCJPBC PIDO O@NON <M@ =<N@? JI BTTFSU >C@>FN, VOJUUFTU =GJ>FN ><I >JIO<DI <IT ! >J?@.
1CDN <GGJRN AJM KM@K<M<ODJIN =@AJM@ <>OP<GGT NO<MODIB OC@ O@NON JM <IT JOC@M NPKKJMODIB >J?@
OC<O OC@ O@NON H<T I@@?. #JM @S<HKG@, OC@ AJGGJRDIB =GJ>F ADMNO ?@ADI@N < Q<MD<=G@ OJ M@?P>@
>J?@ ?PKGD><ODJI:
ETUSJOH UP'SPOU(ETUSJOH TUS, JO EDIBS MFUUFS)
\
// ...
*@;F )7EF;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
215
^
VOJUUFTU
\
JNNVUBCMF TUS = "IFMMP"E;
BTTFSU(UP'SPOU(TUS, 'I') == "IFMMP");
BTTFSU(UP'SPOU(TUS, 'P') == "PIFMM");
BTTFSU(UP'SPOU(TUS, 'M') == "MMIFP");
^
1C@ OCM@@ BTTFSU >C@>FN <=JQ@ O@NO OC<O UP'SPOU() RJMFN <>>JM?DIB OJ DON NK@>DAD><ODJI.
AN OC@N@ @S<HKG@N NCJR, PIDO O@NON <M@ <GNJ PN@APG <N @S<HKG@N JA CJR K<MOD>PG<M API>ODJIN
NCJPG? =@ ><GG@?. 2NP<GGT DO DN @<NT OJ B@O <I D?@< JI RC<O < API>ODJI ?J@N EPNO =T M@<?DIB DON
PIDO O@NON.
Test driven development
43.6
1@NO ?MDQ@I ?@Q@GJKH@IO (1!!) DN < NJAOR<M@ ?@Q@GJKH@IO H@OCJ?JGJBT OC<O KM@N>MD=@N
RMDODIB PIDO O@NON =@AJM@ DHKG@H@IODIB API>ODJI<GDOT. &I 1!!, OC@ AJ>PN DN JI PIDO O@NODIB.
J?DIB DN < N@>JI?<MT <>ODQDOT OC<O H<F@N OC@ O@NON K<NN.
&I <>>JM?<I>@ OJ 1!!, OC@ PSEJOBM() API>ODJI <=JQ@ ><I ADMNO =@ DHKG@H@IO@? DIO@IOD<GGT
DI>JMM@>OGT:
JNQPSU TUE.TUSJOH;
TUSJOH PSEJOBM(TJ[F@U OVNCFS)
\
SFUVSO ""; // j JOUFOUJPOBMMZ XSPOH
^
VOJUUFTU
\
BTTFSU(PSEJOBM(1) == "1TU");
BTTFSU(PSEJOBM(2) == "2OE");
BTTFSU(PSEJOBM(3) == "3SE");
BTTFSU(PSEJOBM(10) == "10UI");
^
WPJE NBJO()
\^
AGOCJPBC OC@ API>ODJI DN J=QDJPNGT RMJIB, OC@ I@SO NO@K RJPG? =@ OJ MPI OC@ PIDO O@NON OJ N@@
OC<O OC@ O@NON ?J DI?@@? ><O>C KMJ=G@HN RDOC OC@ API>ODJI:
$ ENE EFOFNF.E -X -0 -VOJUUFTU
$ ./EFOFNF
DPSF.FYDFQUJPO."TTFSU&SSPS!EFOFNF(10): VOJUUFTU GBJMVSF
1C@ API>ODJI NCJPG? =@ DHKG@H@IO@? JIGT 38F7D N@@DIB OC@ A<DGPM@, <I? JIGT OJ H<F@ OC@ O@NON
K<NN. %@M@ DN EPNO JI@ DHKG@H@IO<ODJI OC<O K<NN@N OC@ O@NON:
JNQPSU TUE.TUSJOH;
TUSJOH PSEJOBM(TJ[F@U OVNCFS)
\
TUSJOH TVGGJY;
TXJUDI (OVNCFS) \
DBTF 1: TVGGJY = "TU"; CSFBL;
*@;F )7EF;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
216
DBTF 2: TVGGJY = "OE"; CSFBL;
DBTF 3: TVGGJY = "SE"; CSFBL;
EFGBVMU: TVGGJY = "UI"; CSFBL;
^
SFUVSO GPSNBU("%T%T", OVNCFS, TVGGJY);
^
VOJUUFTU
\
BTTFSU(PSEJOBM(1) == "1TU");
BTTFSU(PSEJOBM(2) == "2OE");
BTTFSU(PSEJOBM(3) == "3SE");
BTTFSU(PSEJOBM(10) == "10UI");
^
WPJE NBJO()
\^
0DI>@ OC@ DHKG@H@IO<ODJI <=JQ@ ?J@N K<NN OC@ PIDO O@NON, OC@M@ DN M@<NJI OJ OMPNO OC<O OC@
PSEJOBM() API>ODJI DN >JMM@>O. 2I?@M OC@ <NNPM<I>@ OC<O OC@ O@NON =MDIB, OC@ DHKG@H@IO<ODJI
JA OC@ API>ODJI ><I =@ >C<IB@? DI H<IT R<TN RDOC >JIAD?@I>@.
Unit tests before bug fixes 43.6.1
2IDO O@NON <M@ IJO < K<I<>@<; OC@M@ RDGG <GR<TN =@ =PBN. &A < =PB DN ?DN>JQ@M@? RC@I <>OP<GGT
MPIIDIB OC@ KMJBM<H, DO ><I =@ N@@I <N <I DI?D><ODJI OC<O OC@ PIDO O@NON C<Q@ =@@I
DI>JHKG@O@. #JM OC<O M@<NJI, DO DN =@OO@M OJ 8;DEF RMDO@ < PIDO O@NO OC<O M@KMJ?P>@N OC@ =PB <I?
JIGT F:7@ OJ ADS OC@ =PB OJ K<NN OC@ I@R O@NO.
)@O'N C<Q@ < GJJF <O OC@ AJGGJRDIB API>ODJI OC<O M@OPMIN OC@ NK@GGDIB JA OC@ JM?DI<G AJMH JA <
IPH=@M NK@>DAD@? <N < ETUSJOH:
JNQPSU TUE.FYDFQUJPO;
JNQPSU TUE.TUSJOH;
ETUSJOH PSEJOBM4QFMMFE(ETUSJOH OVNCFS)
\
FOGPSDF(OVNCFS.MFOHUI, "OVNCFS DBOOPU CF FNQUZ");
ETUSJOH<ETUSJOH> FYDFQUJPOT = <
"POF": "GJSTU", "UXP" : "TFDPOE", "UISFF" : "UIJSE",
"GJWF" : "GJGUI", "FJHIU": "FJHIUI", "OJOF" : "OJOUI",
"UXFMWF" : "UXFMGUI"
>;
ETUSJOH SFTVMU;
JG (OVNCFS JO FYDFQUJPOT) \
SFTVMU = FYDFQUJPOT<OVNCFS>;
^ FMTF \
SFTVMU = OVNCFS _ "UI";
^
SFUVSO SFTVMU;
^
VOJUUFTU
\
BTTFSU(PSEJOBM4QFMMFE("POF") == "GJSTU");
BTTFSU(PSEJOBM4QFMMFE("UXP") == "TFDPOE");
BTTFSU(PSEJOBM4QFMMFE("UISFF") == "UIJSE");
BTTFSU(PSEJOBM4QFMMFE("UFO") == "UFOUI");
^
*@;F )7EF;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
217
WPJE NBJO()
\^
1C@ API>ODJI O<F@N ><M@ JA @S>@KODJI<G NK@GGDIBN <I? @Q@I DI>GP?@N < PIDO O@NO AJM OC<O. 0ODGG,
OC@ API>ODJI C<N < =PB T@O OJ =@ ?DN>JQ@M@?:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFGMO(")F DBNF UIF %T JO UIF SBDF.",
PSEJOBM4QFMMFE("UXFOUZ"));
^
1C@ NK@GGDIB @MMJM DI OC@ JPOKPO JA OC@ KMJBM<H DN ?P@ OJ < =PB DI PSEJOBM4QFMMFE(), RCD>C
DON PIDO O@NON C<Q@ PIAJMOPI<O@GT A<DG OJ ><O>C:
)F DBNF UIF UXFOUZUI JO UIF SBDF.
AGOCJPBC DO DN @<NT OJ N@@ OC<O OC@ API>ODJI ?J@N IJO KMJ?P>@ OC@ >JMM@>O NK@GGDIB AJM IPH=@MN
OC<O @I? RDOC OC@ G@OO@M T, 1!! KM@N>MD=@N OC<O ADMNO < PIDO O@NO HPNO =@ RMDOO@I OJ M@KMJ?P>@
OC@ =PB =@AJM@ <>OP<GGT ADSDIB DO:
VOJUUFTU
\
// ...
BTTFSU(PSEJOBM4QFMMFE("UXFOUZ") == "UXFOUJFUI");
^
4DOC OC<O DHKMJQ@H@IO OJ OC@ O@NON, IJR OC@ =PB DI OC@ API>ODJI DN =@DIB ><PBCO ?PMDIB
?@Q@GJKH@IO:
DPSF.FYDFQUJPO."TTFSU&SSPS!EFOFNF(3274338): VOJUUFTU GBJMVSF
1C@ API>ODJI NCJPG? =@ ADS@? JIGT OC@I:
ETUSJOH PSEJOBM4QFMMFE(ETUSJOH OVNCFS)
\
// ...
JG (OVNCFS JO FYDFQUJPOT) \
SFTVMU = FYDFQUJPOT<OVNCFS>;
^ FMTF \
JG (OVNCFS<$-1> == 'Z') \
SFTVMU = OVNCFS<0..$-1> _ "JFUI";
^ FMTF \
SFTVMU = OVNCFS _ "UI";
^
^
SFUVSO SFTVMU;
^
Exercise
43.7
t &HKG@H@IO UP'SPOU() <>>JM?DIB OJ 1!!. 0O<MO RDOC OC@ DIO@IODJI<GGT DI>JHKG@O@
DHKG@H@IO<ODJI =@GJR. ,=N@MQ@ OC<O OC@ PIDO O@NON A<DG <I? KMJQD?@ <I DHKG@H@IO<ODJI OC<O
K<NN@N OC@ O@NON.
*@;F )7EF;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
218
ETUSJOH UP'SPOU(ETUSJOH TUS, JO EDIBS MFUUFS)
\
ETUSJOH SFTVMU;
SFUVSO SFTVMU;
^
VOJUUFTU
\
JNNVUBCMF TUS = "IFMMP"E;
BTTFSU(UP'SPOU(TUS, 'I') == "IFMMP");
BTTFSU(UP'SPOU(TUS, 'P') == "PIFMM");
BTTFSU(UP'SPOU(TUS, 'M') == "MMIFP");
^
WPJE NBJO()
\^
... F:7 EA>GF;A@
*@;F )7EF;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
219
Contract Programming 44
JIOM<>O KMJBM<HHDIB DN < NJAOR<M@ ?@NDBI <KKMJ<>C OC<O OM@<ON K<MON JA NJAOR<M@ <N
DI?DQD?P<G @IODOD@N OC<O KMJQD?@ N@MQD>@N OJ @<>C JOC@M. 1CDN <KKMJ<>C M@<GDU@N OC<O NJAOR<M@
><I RJMF <>>JM?DIB OJ DON NK@>DAD><ODJI <N GJIB <N OC@ KMJQD?@M <I? OC@ >JINPH@M JA OC@
N@MQD>@ =JOC J=@T < 5A@FD35F.
!'N >JIOM<>O KMJBM<HHDIB A@<OPM@N DIQJGQ@ API>ODJIN <N OC@ PIDON JA NJAOR<M@ N@MQD>@N. )DF@ DI
PIDO O@NODIB, >JIOM<>O KMJBM<HHDIB DN <GNJ =<N@? JI BTTFSU >C@>FN.
JIOM<>O KMJBM<HHDIB DI ! DN DHKG@H@IO@? =T OCM@@ OTK@N JA >J?@ =GJ>FN:
t #PI>ODJI JO =GJ>FN
t #PI>ODJI PVU =GJ>FN
t 0OMP>O <I? >G<NN JOWBSJBOU =GJ>FN
4@ RDGG N@@ JOWBSJBOU =GJ>FN <I? 5A@FD35F ;@:7D;F3@57 DI < G<O@M >C<KO@M <AO@M >JQ@MDIB
NOMP>ON <I? >G<NN@N.
JO blocks for entry conditions
44.1
JMM@>O @S@>PODJI JA API>ODJIN PNP<GGT ?@K@I? JI RC@OC@M OC@ Q<GP@N JA OC@DM K<M<H@O@MN <M@
Q<GD?. #JM @S<HKG@, < NLP<M@ MJJO API>ODJI H<T M@LPDM@ OC<O DON K<M<H@O@M ><IIJO =@ I@B<ODQ@.
A API>ODJI OC<O ?@<GN RDOC ?<O@N H<T M@LPDM@ OC<O OC@ IPH=@M JA OC@ HJIOC HPNO =@ =@OR@@I
1 <I? 12.
4@ C<Q@ <GM@<?T N@@I NP>C >JI?DODJI >C@>FN DI OC@ BTTFSU <I? FOGPSDF >C<KO@M. JI?DODJIN
JI K<M<H@O@M Q<GP@N ><I =@ @IAJM>@? =T BTTFSU >C@>FN RDOCDI API>ODJI ?@ADIDODJIN:
TUSJOH UJNF5P4USJOH(JO JOU IPVS, JO JOU NJOVUF)
\
BTTFSU((IPVS >= 0) && (IPVS <= 23));
BTTFSU((NJOVUF >= 0) && (NJOVUF <= 59));
SFUVSO GPSNBU("%02T:%02T", IPVS, NJOVUF);
^
&I >JIOM<>O KMJBM<HHDIB, OC@ N<H@ >C@>FN <M@ RMDOO@I DIND?@ OC@ JO =GJ>FN JA API>ODJIN.
4C@I <I JO JM PVU =GJ>F DN PN@?, OC@ <>OP<G =J?T JA OC@ API>ODJI HPNO =@ NK@>DAD@? <N < CPEZ
=GJ>F:
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
TUSJOH UJNF5P4USJOH(JO JOU IPVS, JO JOU NJOVUF)
JO
\
BTTFSU((IPVS >= 0) && (IPVS <= 23));
BTTFSU((NJOVUF >= 0) && (NJOVUF <= 59));
^
CPEZ
\
SFUVSO GPSNBU("%02T:%02T", IPVS, NJOVUF);
^
WPJE NBJO()
\
XSJUFMO(UJNF5P4USJOH(12, 34));
^
CA@FD35F &DA9D3??;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
220
A =@I@ADO JA <I JO =GJ>F DN OC<O <GG JA OC@ @IOMT >JI?DODJIN ><I =@ F@KO OJB@OC@M <I? N@K<M<O@
AMJH OC@ <>OP<G =J?T JA OC@ API>ODJI. 1CDN R<T, OC@ API>ODJI =J?T RJPG? =@ AM@@ JA BTTFSU
>C@>FN <=JPO OC@ @IOMT >JI?DODJIN. AN I@@?@?, DO DN NODGG KJNND=G@ <I? <?QDN<=G@ OJ C<Q@ JOC@M
BTTFSU >C@>FN DIND?@ OC@ API>ODJI =J?T <N PIM@G<O@? >C@>FN OC<O BP<M? <B<DINO KJO@IOD<G
KMJBM<HHDIB @MMJMN DI OC@ API>ODJI =J?T.
1C@ >J?@ OC<O DN DIND?@ OC@ JO =GJ>F DN @S@>PO@? <POJH<OD><GGT @Q@MT ODH@ OC@ API>ODJI DN
><GG@?. 1C@ <>OP<G @S@>PODJI JA OC@ API>ODJI NO<MON JIGT DA <GG JA OC@ BTTFSU >C@>FN DIND?@ OC@
JO =GJ>F K<NN. 1CDN KM@Q@ION @S@>PODIB OC@ API>ODJI RDOC DIQ<GD? @IOMT >JI?DODJIN <I? <N <
>JIN@LP@I>@, <QJD?N KMJ?P>DIB DI>JMM@>O M@NPGON.
AI BTTFSU >C@>F OC<O A<DGN DIND?@ OC@ JO =GJ>F DI?D><O@N OC<O OC@ >JIOM<>O C<N =@@I QDJG<O@?
=T OC@ ><GG@M.
PVU blocks for exit conditions
44.2
1C@ JOC@M ND?@ JA OC@ >JIOM<>O DIQJGQ@N BP<M<IO@@N OC<O OC@ API>ODJI KMJQD?@N. AI @S<HKG@ JA
< API>ODJI RDOC <I @SDO >JI?DODJI RJPG? =@ < API>ODJI OC<O M@OPMIN OC@ IPH=@M JA ?<TN DI
#@=MP<MT; OC@ M@OPMI@? Q<GP@ RJPG? <GR<TN =@ @DOC@M 28 JM 29.
1C@ @SDO >JI?DODJIN <M@ >C@>F@? DIND?@ OC@ PVU =GJ>FN JA API>ODJIN.
B@><PN@ OC@ Q<GP@ OC<O < API>ODJI M@OPMIN =T OC@ SFUVSO NO<O@H@IO I@@? IJO =@ ?@ADI@? <N <
Q<MD<=G@ DIND?@ OC@ API>ODJI, OC@M@ DN PNP<GGT IJ I<H@ OJ M@A@M OJ OC@ M@OPMI Q<GP@. 1CDN ><I =@
N@@I <N < KMJ=G@H =@><PN@ OC@ BTTFSU >C@>FN DIND?@ OC@ PVU =GJ>F ><IIJO M@A@M OJ OC@
M@OPMI@? Q<MD<=G@ =T I<H@.
! NJGQ@N OCDN KMJ=G@H =T KMJQD?DIB < R<T JA I<HDIB OC@ M@OPMI Q<GP@ MDBCO <AO@M OC@ PVU
F@TRJM?. 1C<O I<H@ M@KM@N@ION OC@ Q@MT Q<GP@ OC<O OC@ API>ODJI DN DI OC@ KMJ>@NN JA
M@OPMIDIB:
JOU EBZT*O'FCSVBSZ(JO JOU ZFBS)
PVU (SFTVMU)
\
BTTFSU((SFTVMU == 28) ]] (SFTVMU == 29));
^
CPEZ
\
SFUVSO JT-FBQ:FBS(ZFBS) 29 : 28;
^
AGOCJPBC SFTVMU DN < M@<NJI<=G@ I<H@ AJM OC@ M@OPMI@? Q<GP@, JOC@M Q<GD? I<H@N H<T <GNJ =@
PN@?.
0JH@ API>ODJIN ?J IJO C<Q@ M@OPMI Q<GP@N JM OC@ M@OPMI Q<GP@ I@@? IJO =@ >C@>F@?. &I OC<O
><N@ OC@ PVU =GJ>F ?J@N IJO NK@>DAT < I<H@:
PVU
\
// ...
^
0DHDG<M OJ JO =GJ>FN, OC@ PVU =GJ>FN <M@ @S@>PO@? <POJH<OD><GGT <AO@M OC@ =J?T JA OC@ API>ODJI
DN @S@>PO@?.
AI BTTFSU >C@>F OC<O A<DGN DIND?@ OC@ PVU =GJ>F DI?D><O@N OC<O OC@ >JIOM<>O C<N =@@I QDJG<O@?
=T OC@ API>ODJI.
CA@FD35F &DA9D3??;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
221
AN DO C<N =@@I J=QDJPN, JO <I? PVU =GJ>FN <M@ JKODJI<G. JIND?@MDIB OC@ VOJUUFTU =GJ>FN <N
R@GG, RCD>C <M@ <GNJ JKODJI<G, ! API>ODJIN H<T >JINDNO JA PK OJ AJPM =GJ>FN JA >J?@:
t JO: ,KODJI<G
t PVU: ,KODJI<G
t CPEZ: *<I?<OJMT =PO OC@ CPEZ F@TRJM? H<T =@ NFDKK@? DA IJ JO JM PVU =GJ>F DN ?@ADI@?.
t VOJUUFTU: ,KODJI<G <I? O@>CID><GGT IJO < K<MO JA < API>ODJI'N ?@ADIDODJI =PO >JHHJIGT
?@ADI@? MDBCO <AO@M OC@ API>ODJI.
%@M@ DN <I @S<HKG@ OC<O PN@N <GG JA OC@N@ =GJ>FN:
JNQPSU TUE.TUEJP;
/*
* %JTUSJCVUFT UIF TVN CFUXFFO UXP WBSJBCMFT.
*
* %JTUSJCVUFT UP UIF GJSTU WBSJBCMF GJSTU, CVU OFWFS HJWFT
* NPSF UIBO 7 UP JU. 5IF SFTU PG UIF TVN JT EJTUSJCVUFE UP
* UIF TFDPOE WBSJBCMF.
*/
WPJE EJTUSJCVUF(JO JOU TVN, PVU JOU GJSTU, PVU JOU TFDPOE)
JO
\
BTTFSU(TVN >= 0);
^
PVU
\
BTTFSU(TVN == (GJSTU + TFDPOE));
^
CPEZ
\
GJSTU = (TVN >= 7) 7 : TVN;
TFDPOE = TVN - GJSTU;
^
VOJUUFTU
\
JOU GJSTU;
JOU TFDPOE;
// #PUI NVTU CF 0 JG UIF TVN JT 0
EJTUSJCVUF(0, GJSTU, TFDPOE);
BTTFSU(GJSTU == 0);
BTTFSU(TFDPOE == 0);
// *G UIF TVN JT MFTT UIBO 7, UIFO BMM PG JU NVTU CF HJWFO
// UP GJSTU
EJTUSJCVUF(3, GJSTU, TFDPOE);
BTTFSU(GJSTU == 3);
BTTFSU(TFDPOE == 0);
// 5FTUJOH B CPVOEBSZ DPOEJUJPO
EJTUSJCVUF(7, GJSTU, TFDPOE);
BTTFSU(GJSTU == 7);
BTTFSU(TFDPOE == 0);
// *G UIF TVN JT NPSF UIBO 7, UIFO UIF GJSTU NVTU HFU 7
// BOE UIF SFTU NVTU CF HJWFO UP TFDPOE
EJTUSJCVUF(8, GJSTU, TFDPOE);
BTTFSU(GJSTU == 7);
BTTFSU(TFDPOE == 1);
// " SBOEPN MBSHF WBMVF
EJTUSJCVUF(1@000@007, GJSTU, TFDPOE);
BTTFSU(GJSTU == 7);
BTTFSU(TFDPOE == 1@000@000);
^
CA@FD35F &DA9D3??;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
222
WPJE NBJO()
\
JOU GJSTU;
JOU TFDPOE;
EJTUSJCVUF(123, GJSTU, TFDPOE);
XSJUFMO("GJSTU: ", GJSTU, " TFDPOE: ", TFDPOE);
^
1C@ KMJBM<H ><I =@ >JHKDG@? <I? MPI JI OC@ >JINJG@ =T OC@ AJGGJRDIB >JHH<I?N:
$ ENE EFOFNF.E -X -VOJUUFTU
$ ./EFOFNF
GJSTU: 7 TFDPOE: 116
AGOCJPBC OC@ <>OP<G RJMF JA OC@ API>ODJI >JINDNON JA JIGT ORJ GDI@N, OC@M@ <M@ < OJO<G JA 19
IJIOMDQD<G GDI@N OC<O NPKKJMO DON API>ODJI<GDOT. &O H<T =@ <MBP@? OC<O NJ HP>C @SOM< >J?@ DN OJJ
HP>C AJM NP>C < NCJMO API>ODJI. %JR@Q@M, =PBN <M@ I@Q@M DIO@IODJI<G. 1C@ KMJBM<HH@M <GR<TN
RMDO@N >J?@ OC<O DN 7JB75F76 FA IAD= 5ADD75F>K, RCD>C >JHHJIGT @I?N PK >JIO<DIDIB Q<MDJPN
OTK@N JA =PBN.
4C@I @SK@>O<ODJIN <M@ G<D? JPO @SKGD>DOGT =T PIDO O@NON <I? >JIOM<>ON, API>ODJIN OC<O <M@
DIDOD<GGT >JMM@>O C<Q@ < BM@<O@M >C<I>@ JA NO<TDIB >JMM@>O. & M@>JHH@I? OC<O TJP O<F@ APGG
<?Q<IO<B@ JA <IT A@<OPM@ OC<O DHKMJQ@N KMJBM<H >JMM@>OI@NN. BJOC PIDO O@NON <I? >JIOM<>ON
<M@ @AA@>ODQ@ OJJGN OJR<M?N OC<O BJ<G. 1C@T C@GK M@?P>@ ODH@ NK@IO AJM ?@=PBBDIB, @AA@>ODQ@GT
DI>M@<NDIB ODH@ NK@IO AJM <>OP<GGT RMDODIB >J?@.
Disabling contract programming
44.3
JIOM<MT OJ PIDO O@NODIB, >JIOM<>O KMJBM<HHDIB A@<OPM@N <M@ @I<=G@? =T ?@A<PGO. 1C@
jSFMFBTF >JHKDG@M NRDO>C ?DN<=G@N >JIOM<>O KMJBM<HHDIB:
ENE EFOFNF.E -X -SFMFBTF
4C@I OC@ KMJBM<H DN >JHKDG@? RDOC OC@ jSFMFBTF NRDO>C, OC@ >JIO@ION JA JO, PVU, <I?
JOWBSJBOU =GJ>FN <M@ DBIJM@?.
JO blocks versus FOGPSDF checks
44.4
4@ C<Q@ N@@I DI OC@ BTTFSU <I? FOGPSDF >C<KO@M OC<O NJH@ODH@N DO DN ?DAAD>PGO OJ ?@>D?@
RC@OC@M OJ PN@ BTTFSU JM FOGPSDF >C@>FN. 0DHDG<MGT, NJH@ODH@N DO DN ?DAAD>PGO OJ ?@>D?@
RC@OC@M OJ PN@ BTTFSU >C@>FN RDOCDI JO =GJ>FN Q@MNPN FOGPSDF >C@>FN RDOCDI API>ODJI
=J?D@N.
1C@ A<>O OC<O DO DN KJNND=G@ OJ ?DN<=G@ >JIOM<>O KMJBM<HHDIB DN <I DI?D><ODJI OC<O >JIOM<>O
KMJBM<HHDIB DN AJM KMJO@>ODIB <B<DINO KMJBM<HH@M @MMJMN. #JM OC<O M@<NJI, OC@ ?@>DNDJI C@M@
NCJPG? =@ =<N@? JI OC@ N<H@ BPD?@GDI@N OC<O R@ C<Q@ N@@I DI OC@ BTTFSU <I? FOGPSDF
>C<KO@M:
t &A OC@ >C@>F DN BP<M?DIB <B<DINO < >J?DIB @MMJM, OC@I DO NCJPG? =@ DI OC@ JO =GJ>F. #JM
@S<HKG@, DA OC@ API>ODJI DN ><GG@? JIGT AMJH JOC@M K<MON JA OC@ KMJBM<H, GDF@GT OJ C@GK RDOC
<>CD@QDIB < API>ODJI<GDOT JA DO, OC@I OC@ K<M<H@O@M Q<GP@N <M@ @IODM@GT OC@ M@NKJIND=DGDOT JA
OC@ KMJBM<HH@M. #JM OC<O M@<NJI, OC@ @IOMT >JI?DODJIN JA NP>C < API>ODJI NCJPG? =@
>C@>F@? DI DON JO =GJ>F.
CA@FD35F &DA9D3??;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
223
t &A OC@ API>ODJI ><IIJO <>CD@Q@ NJH@ O<NF AJM <IT JOC@M M@<NJI, DI>GP?DIB DIQ<GD? K<M<H@O@M
Q<GP@N, OC@I DO HPNO OCMJR <I @S>@KODJI, >JIQ@ID@IOGT =T FOGPSDF.
1J N@@ <I @S<HKG@ JA OCDN, G@O'N ?@ADI@ < API>ODJI OC<O M@OPMIN < NGD>@ JA OC@ HD??G@ JA
<IJOC@M NGD>@. )@O'N <NNPH@ OC<O OCDN API>ODJI DN AJM OC@ >JINPHKODJI JA OC@ PN@MN JA
OC@ HJ?PG@, <N JKKJN@? OJ =@DIB <I DIO@MI<G API>ODJI PN@? =T OC@ HJ?PG@ DON@GA. 0DI>@
OC@ PN@MN JA OCDN HJ?PG@ ><I ><GG OCDN API>ODJI =T Q<MDJPN <I? KJO@IOD<GGT DIQ<GD?
K<M<H@O@M Q<GP@N, DO RJPG? =@ <KKMJKMD<O@ OJ >C@>F OC@ K<M<H@O@M Q<GP@N @Q@MT ODH@
OC@ API>ODJI DN ><GG@?. &O RJPG? =@ DINPAAD>D@IO OJ JIGT >C@>F OC@H <O KMJBM<H
?@Q@GJKH@IO ODH@, <AO@M RCD>C >JIOM<>ON ><I =@ ?DN<=G@? =T jSFMFBTF.
#JM OC<O M@<NJI, OC@ AJGGJRDIB API>ODJI Q<GD?<O@N DON K<M<H@O@MN =T ><GGDIB FOGPSDF DI
OC@ API>ODJI =J?T DINO@<? JA <I BTTFSU >C@>F DI OC@ JO =GJ>F:
JNQPSU TUE.FYDFQUJPO;
JOPVU(JOU)<> NJEEMF(JOPVU(JOU)<> PSJHJOBM4MJDF, TJ[F@U XJEUI)
PVU (SFTVMU)
\
BTTFSU(SFTVMU.MFOHUI == XJEUI);
^
CPEZ
\
FOGPSDF(PSJHJOBM4MJDF.MFOHUI >= XJEUI);
JNNVUBCMF TUBSU = (PSJHJOBM4MJDF.MFOHUI - XJEUI) / 2;
JNNVUBCMF FOE = TUBSU + XJEUI;
SFUVSO PSJHJOBM4MJDF<TUBSU .. FOE>;
^
VOJUUFTU
\
BVUP TMJDF = <1, 2, 3, 4, 5>;
BTTFSU(NJEEMF(TMJDF, 3) == <2, 3, 4>);
BTTFSU(NJEEMF(TMJDF, 2) == <2, 3>);
BTTFSU(NJEEMF(TMJDF, 5) == TMJDF);
^
WPJE NBJO()
\^
1C@M@ DNI'O < NDHDG<M KMJ=G@H RDOC OC@ PVU =GJ>FN. 0DI>@ OC@ M@OPMI Q<GP@ JA @Q@MT
API>ODJI DN OC@ M@NKJIND=DGDOT JA OC@ KMJBM<HH@M, @SDO >JI?DODJIN HPNO <GR<TN =@
>C@>F@? DI OC@ PVU =GJ>FN. 1C@ API>ODJI <=JQ@ AJGGJRN OCDN BPD?@GDI@.
t AIJOC@M >MDO@MDJI OJ >JIND?@M RC@I ?@>D?DIB =@OR@@I JO =GJ>FN Q@MNPN FOGPSDF DN OJ
>JIND?@M RC@OC@M OC@ >JI?DODJI DN M@>JQ@M<=G@. &A DO DN M@>JQ@M<=G@ =T OC@ CDBC@M G<T@MN JA
>J?@, OC@I DO H<T =@ HJM@ <KKMJKMD<O@ OJ OCMJR <I @S>@KODJI, >JIQ@ID@IOGT =T FOGPSDF.
Exercise
44.5
t 4MDO@ < KMJBM<H OC<O DI>M@<N@N OC@ OJO<G KJDION JA ORJ AJJO=<GG (NJ>>@M) O@<HN <>>JM?DIB OJ
OC@ M@NPGO JA < B<H@.
1C@ ADMNO ORJ K<M<H@O@MN JA OCDN API>ODJI <M@ OC@ BJ<GN OC<O @<>C O@<H C<N N>JM@?. 1C@
JOC@M ORJ K<M<H@O@MN <M@ OC@ KJDION JA @<>C O@<H =@AJM@ OC@ B<H@. 1CDN API>ODJI
NCJPG? <?EPNO OC@ KJDION JA OC@ O@<HN <>>JM?DIB OJ OC@ BJ<GN OC<O OC@T C<Q@ N>JM@?. AN
< M@HDI?@M, OC@ RDII@M O<F@N 3 KJDION <I? OC@ GJN@M O<F@N IJ KJDIO. &I OC@ @Q@IO JA <
?M<R, =JOC O@<HN B@O 1 KJDIO @<>C.
CA@FD35F &DA9D3??;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
224
A??DODJI<GGT, OC@ API>ODJI NCJPG? DI?D><O@ RCD>C O@<H C<N =@@I OC@ RDII@M: 1 DA OC@
ADMNO O@<H C<N RJI, 2 DA OC@ N@>JI? O@<H C<N RJI, <I? 0 DA OC@ B<H@ C<N @I?@? DI <
?M<R.
0O<MO RDOC OC@ AJGGJRDIB KMJBM<H <I? ADGG DI OC@ AJPM =GJ>FN JA OC@ API>ODJI
<KKMJKMD<O@GT. !J IJO M@HJQ@ OC@ BTTFSU >C@>FN DI NBJO(); OC@T ?@HJINOM<O@ CJR
OCDN API>ODJI DN @SK@>O@? OJ RJMF.
JOU BEE1PJOUT(JO JOU HPBMT1,
JO JOU HPBMT2,
SFG JOU QPJOUT1,
SFG JOU QPJOUT2)
JO
\
// ...
^
PVU (SFTVMU)
\
// ...
^
CPEZ
\
JOU XJOOFS;
// ...
SFUVSO XJOOFS;
^
VOJUUFTU
\
// ...
^
WPJE NBJO()
\
JOU QPJOUT1 = 10;
JOU QPJOUT2 = 7;
JOU XJOOFS;
XJOOFS = BEE1PJOUT(3, 1, QPJOUT1, QPJOUT2);
BTTFSU(QPJOUT1 == 13);
BTTFSU(QPJOUT2 == 7);
BTTFSU(XJOOFS == 1);
XJOOFS = BEE1PJOUT(2, 2, QPJOUT1, QPJOUT2);
BTTFSU(QPJOUT1 == 14);
BTTFSU(QPJOUT2 == 8);
BTTFSU(XJOOFS == 0);
^
Note: !F ?3K 47 47FF7D FA D7FGD@ 3@ enum H3>G7 8DA? F:;E 8G@5F;A@:
FOVN (BNF3FTVMU
\
GJSTU8PO, TFDPOE8PO, ESBX
^
(BNF3FTVMU BEE1PJOUT(JO JOU HPBMT1,
JO JOU HPBMT2,
SFG JOU QPJOUT1,
SFG JOU QPJOUT2)
// ...
& >CJN@ OJ M@OPMI <I JOU AJM OCDN @S@M>DN@, NJ OC<O OC@ M@OPMI Q<GP@ ><I =@ >C@>F@?
<B<DINO OC@ Q<GD? Q<GP@N JA 0, 1, <I? 2.
CA@FD35F &DA9D3??;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
225
... F:7 EA>GF;A@
CA@FD35F &DA9D3??;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
226
Lifetimes and Fundamental Operations 45
4@ RDGG NJJI >JQ@M NOMP>ON, OC@ =<ND> A@<OPM@ OC<O <GGJRN OC@ KMJBM<HH@M OJ ?@ADI@
<KKGD><ODJI-NK@>DAD> OTK@N. 0OMP>ON <M@ AJM >JH=DIDIB API?<H@IO<G OTK@N <I? JOC@M NOMP>ON
OJB@OC@M OJ ?@ADI@ CDBC@M-G@Q@G OTK@N OC<O =@C<Q@ <>>JM?DIB OJ NK@>D<G I@@?N JA KMJBM<HN.
AAO@M NOMP>ON, R@ RDGG G@<MI <=JPO >G<NN@N, RCD>C <M@ OC@ =<NDN JA OC@ J=E@>O JMD@IO@?
KMJBM<HHDIB A@<OPM@N JA !.
B@AJM@ B@OODIB OJ NOMP>ON <I? >G<NN@N, DO RDGG =@ =@OO@M OJ O<GF <=JPO NJH@ DHKJMO<IO >JI>@KON
ADMNO. 1C@N@ >JI>@KON RDGG C@GK PI?@MNO<I? NOMP>ON <I? >G<NN@N <I? NJH@ JA OC@DM ?DAA@M@I>@N.
4@ C<Q@ =@@I ><GGDIB <IT KD@>@ JA ?<O< OC<O M@KM@N@IO@? < >JI>@KO DI < KMJBM<H < H3D;34>7. &I
< A@R KG<>@N R@ C<Q@ ><GG@? NOMP>O <I? >G<NN Q<MD<=G@N NK@>DAD><GGT <N A4<75FE. & RDGG >JIODIP@
><GGDIB =JOC JA OC@N@ >JI>@KON Q<MD<=G@N DI OCDN >C<KO@M.
AGOCJPBC OCDN >C<KO@M DI>GP?@N JIGT API?<H@IO<G OTK@N, NGD>@N, <I? <NNJ>D<ODQ@ <MM<TN; OC@N@
>JI>@KON <KKGT OJ PN@?-?@ADI@? OTK@N <N R@GG.
Lifetime of a variable
45.1
1C@ ODH@ =@OR@@I RC@I < Q<MD<=G@ DN ?@ADI@? <I? RC@I DO DN 8;@3>;L76 DN OC@ GDA@ODH@ JA OC<O
Q<MD<=G@. AGOCJPBC DO DN OC@ ><N@ AJM H<IT OTK@N, 475A?;@9 G@3H3;>34>7 <I? 47;@9 8;@3>;L76
I@@? IJO =@ <O OC@ N<H@ ODH@.
6JP RJPG? M@H@H=@M AMJH OC@ +<H@ 0K<>@ >C<KO@M CJR Q<MD<=G@N =@>JH@ PI<Q<DG<=G@. &I
NDHKG@ ><N@N, @SDODIB OC@ N>JK@ RC@M@ < Q<MD<=G@ C<N =@@I ?@ADI@? RJPG? H<F@ OC<O Q<MD<=G@
PI<Q<DG<=G@.
)@O'N >JIND?@M OC@ AJGGJRDIB @S<HKG@ <N < M@HDI?@M:
WPJE TQFFE5FTU()
\
JOU TQFFE; // 4JOHMF WBSJBCMF ...
GPSFBDI (J; 0 .. 10) \
TQFFE = 100 + J; // ... UBLFT 10 EJGGFSFOU WBMVFT.
// ...
^
^ // j 'TQFFE' JT VOBWBJMBCMF CFZPOE UIJT QPJOU.
1C@ GDA@ODH@ JA OC@ TQFFE Q<MD<=G@ DI OC<O >J?@ @I?N PKJI @SDODIB OC@ TQFFE5FTU() API>ODJI.
1C@M@ DN < NDIBG@ Q<MD<=G@ DI OC@ >J?@ <=JQ@, RCD>C O<F@N O@I ?DAA@M@IO Q<GP@N AMJH 100 OJ 109.
4C@I DO >JH@N OJ Q<MD<=G@ GDA@ODH@N, OC@ AJGGJRDIB >J?@ DN Q@MT ?DAA@M@IO >JHK<M@? OJ OC@
KM@QDJPN JI@:
WPJE TQFFE5FTU()
\
GPSFBDI (J; 0 .. 10) \
JOU TQFFE = 100 + J; // 5FO TFQBSBUF WBSJBCMFT.
// ...
^ // j -JGFUJNF PG FBDI WBSJBCMF FOET IFSF.
^
1C@M@ <M@ O@I N@K<M<O@ Q<MD<=G@N DI OC<O >J?@, @<>C O<FDIB < NDIBG@ Q<GP@. 2KJI @Q@MT
DO@M<ODJI JA OC@ GJJK, < I@R Q<MD<=G@ NO<MON DON GDA@, RCD>C @Q@IOP<GGT @I?N <O OC@ @I? JA @<>C
DO@M<ODJI.
";87F;?7E 3@6 FG@63?7@F3> %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
227
Lifetime of a parameter
45.2
1C@ GDA@ODH@ JA < K<M<H@O@M ?@K@I?N JI DON LP<GDAD@MN:
SFG: 1C@ K<M<H@O@M DN EPNO <I <GD<N JA OC@ <>OP<G Q<MD<=G@ OC<O DN NK@>DAD@? RC@I ><GGDIB OC@
API>ODJI. SFG K<M<H@O@MN ?J IJO <AA@>O OC@ GDA@ODH@N JA <>OP<G Q<MD<=G@N.
JO: #JM H3>G7 FKB7E, OC@ GDA@ODH@ JA OC@ K<M<H@O@M NO<MON PKJI @IO@MDIB OC@ API>ODJI <I? @I?N
PKJI @SDODIB DO. #JM D787D7@57 FKB7E, OC@ GDA@ODH@ JA OC@ K<M<H@O@M DN OC@ N<H@ <N RDOC SFG.
(4@ RDGG N@@ Q<GP@ OTK@N <I? M@A@M@I>@ OTK@N DI OC@ I@SO >C<KO@M.)
PVU: 0<H@ RDOC SFG, OC@ K<M<H@O@M DN EPNO <I <GD<N JA OC@ <>OP<G Q<MD<=G@ OC<O DN NK@>DAD@?
RC@I ><GGDIB OC@ API>ODJI. 1C@ JIGT ?DAA@M@I>@ DN OC<O OC@ Q<MD<=G@ DN N@O OJ DON .JOJU Q<GP@
<POJH<OD><GGT PKJI @IO@MDIB OC@ API>ODJI.
MB[Z: 1C@ GDA@ JA OC@ K<M<H@O@M NO<MON RC@I OC@ K<M<H@O@M DN <>OP<GGT PN@? <I? @I?N MDBCO
OC@I.
1C@ AJGGJRDIB @S<HKG@ PN@N OC@N@ AJPM OTK@N JA K<M<H@O@MN <I? @SKG<DIN OC@DM GDA@ODH@N DI
KMJBM<H >JHH@ION:
WPJE NBJO()
\
JOU NBJO@JO; /* 5IF WBMVF PG NBJO@JO JT DPQJFE UP UIF
* QBSBNFUFS. */
JOU NBJO@SFG; /* NBJO@SFG JT QBTTFE UP UIF GVODUJPO BT
* JUTFMG. */
JOU NBJO@PVU; /* NBJO@PVU JT QBTTFE UP UIF GVODUJPO BT
* JUTFMG. *UT WBMVF JT TFU UP JOU.JOJU
* VQPO FOUFSJOH UIF GVODUJPO. */
GPP(NBJO@JO, NBJO@SFG, NBJO@PVU, B$BMDVMBUJPO());
^
WPJE GPP(
JO JOU Q@JO, /* 5IF MJGFUJNF PG Q@JO TUBSUT VQPO
* FOUFSJOH UIF GVODUJPO BOE FOET VQPO
* FYJUJOH UIF GVODUJPO. */
SFG JOU Q@SFG, /* Q@SFG JT BO BMJBT PG NBJO@SFG. */
PVU JOU Q@PVU, /* Q@PVU JT BO BMJBT PG NBJO@PVU. *UT
* WBMVF JT TFU UP JOU.JOJU VQPO
* FOUFSJOH UIF GVODUJPO. */
MB[Z JOU Q@MB[Z) /* 5IF MJGFUJNF PG Q@MB[Z TUBSUT XIFO JU
* JT VTFE BOE FOET XIFO JUT VTF
* FOET. *UT WBMVF JT DBMDVMBUFE CZ
* DBMMJOH B$BMDVMBUJPO() FWFSZ UJNF
* Q@MB[Z JT VTFE JO UIF GVODUJPO. */
\
// ...
^
JOU B$BMDVMBUJPO()
\
JOU SFTVMU;
// ...
SFUVSO SFTVMU;
^
";87F;?7E 3@6 FG@63?7@F3> %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
228
Fundamental operations
45.3
/@B<M?G@NN JA DON OTK@, OC@M@ <M@ OCM@@ API?<H@IO<G JK@M<ODJIN OCMJPBCJPO OC@ GDA@ODH@ JA <
Q<MD<=G@:
t Initialization: 1C@ NO<MO JA DON GDA@.
t Finalization: 1C@ @I? JA DON GDA@.
t Assignment: C<IBDIB DON Q<GP@ <O JI>@.
1J =@ >JIND?@M@? <I J=E@>O, DO HPNO ADMNO =@ DIDOD<GDU@?. 1C@M@ H<T =@ ADI<G JK@M<ODJIN AJM NJH@
OTK@N. 1C@ Q<GP@ JA < Q<MD<=G@ H<T >C<IB@ ?PMDIB DON GDA@ODH@.
Initialization 45.3.1
"Q@MT Q<MD<=G@ HPNO =@ DIDOD<GDU@? =@AJM@ =@DIB PN@?. &IDOD<GDU<ODJI DIQJGQ@N ORJ NO@KN:
1. Reserving space for the variable: 1CDN NK<>@ DN RC@M@ OC@ Q<GP@ JA OC@ Q<MD<=G@ DN NOJM@?
DI H@HJMT.
2. Construction: 0@OODIB OC@ ADMNO Q<GP@ JA OC@ Q<MD<=G@ JI OC<O NK<>@ (JM OC@ ADMNO Q<GP@N JA
OC@ H@H=@MN JA NOMP>ON <I? >G<NN@N).
"Q@MT Q<MD<=G@ GDQ@N DI < KG<>@ DI H@HJMT OC<O DN M@N@MQ@? AJM DO. 0JH@ JA OC@ >J?@ OC<O OC@
>JHKDG@M B@I@M<O@N DN <=JPO M@N@MQDIB NK<>@ AJM @<>C Q<MD<=G@.
)@O'N >JIND?@M OC@ AJGGJRDIB Q<MD<=G@:
JOU TQFFE = 123;
1C@ IPH=@M JA =TO@N OC<O OC@ Q<MD<=G@ TQFFE J>>PKD@N DN OC@ NDU@ JA <I JOU. &A R@ QDNP<GDU@ OC@
H@HJMT <N < MD==JI BJDIB AMJH G@AO OJ MDBCO, R@ ><I DH<BDI@ OC@ Q<MD<=G@ GDQDIB JI NJH@ K<MO
JA DO:
--+-----+-----+-----+--
] ] 123 ] ]
--+-----+-----+-----+--
1C@ H@HJMT GJ><ODJI OC<O < Q<MD<=G@ DN KG<>@? <O DN ><GG@? DON <??M@NN. &I < N@IN@, OC@ Q<MD<=G@
GDQ@N <O OC<O <??M@NN. 4C@I OC@ Q<GP@ JA < Q<MD<=G@ DN >C<IB@?, OC@ I@R Q<GP@ DN NOJM@? <O OC@
N<H@ KG<>@:
++TQFFE;
1C@ I@R Q<GP@ RJPG? =@ <O OC@ N<H@ KG<>@ RC@M@ OC@ JG? Q<GP@ C<N =@@I:
--+-----+-----+-----+--
] ] 124 ] ]
--+-----+-----+-----+--
JINOMP>ODJI DN I@>@NN<MT OJ KM@K<M@ Q<MD<=G@N AJM PN@. 0DI>@ < Q<MD<=G@ ><IIJO =@ PN@?
M@GD<=GT =@AJM@ =@DIB >JINOMP>O@?, DO DN K@MAJMH@? =T OC@ >JHKDG@M <POJH<OD><GGT.
3<MD<=G@N ><I =@ >JINOMP>O@? DI OCM@@ R<TN:
t By their default value: RC@I OC@ KMJBM<HH@M ?J@N IJO NK@>DAT < Q<GP@ @SKGD>DOGT
t By copying: RC@I OC@ Q<MD<=G@ DN >JINOMP>O@? <N < >JKT JA <IJOC@M Q<MD<=G@ JA OC@ N<H@
OTK@
t By a specific value: RC@I OC@ KMJBM<HH@M NK@>DAD@N < Q<GP@ @SKGD>DOGT
";87F;?7E 3@6 FG@63?7@F3> %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
229
4C@I < Q<GP@ DN IJO NK@>DAD@?, OC@ Q<GP@ JA OC@ Q<MD<=G@ RJPG? =@ OC@ 6783G>F Q<GP@ JA DON OTK@,
D.@. DON .JOJU Q<GP@.
JOU TQFFE;
1C@ Q<GP@ JA TQFFE <=JQ@ DN JOU.JOJU, RCD>C C<KK@IN OJ =@ U@MJ. +<OPM<GGT, < Q<MD<=G@ OC<O DN
>JINOMP>O@? =T DON ?@A<PGO Q<GP@ H<T C<Q@ JOC@M Q<GP@N ?PMDIB DON GDA@ODH@ (PIG@NN DO DN
JNNVUBCMF).
'JMF GJMF;
4DOC OC@ ?@ADIDODJI <=JQ@, OC@ Q<MD<=G@ GJMF DN < 'JMF J=E@>O OC<O DN IJO T@O <NNJ>D<O@? RDOC <I
<>OP<G ADG@ JI OC@ ADG@ NTNO@H. &O DN IJO PN<=G@ PIODG DO DN HJ?DAD@? OJ =@ <NNJ>D<O@? RDOC < ADG@.
3<MD<=G@N <M@ NJH@ODH@N >JINOMP>O@? <N < >JKT JA <IJOC@M Q<MD<=G@:
JOU TQFFE = PUIFS4QFFE;
TQFFE <=JQ@ DN >JINOMP>O@? =T OC@ Q<GP@ JA PUIFS4QFFE.
AN R@ RDGG N@@ DI G<O@M >C<KO@MN, OCDN JK@M<ODJI C<N < ?DAA@M@IO H@<IDIB AJM >G<NN Q<MD<=G@N:
BVUP DMBTT7BSJBCMF = PUIFS$MBTT7BSJBCMF;
AGOCJPBC DMBTT7BSJBCMF NO<MON DON GDA@ <N < >JKT JA PUIFS$MBTT7BSJBCMF, OC@M@ DN <
API?<H@IO<G ?DAA@M@I>@ RDOC >G<NN@N: AGOCJPBC TQFFE <I? PUIFS4QFFE <M@ ?DNODI>O Q<GP@N,
DMBTT7BSJBCMF <I? PUIFS$MBTT7BSJBCMF =JOC KMJQD?@ <>>@NN OJ OC@ N<H@ Q<GP@. 1CDN DN OC@
API?<H@IO<G ?DAA@M@I>@ =@OR@@I Q<GP@ OTK@N <I? M@A@M@I>@ OTK@N. 4@ RDGG >JQ@M OCDN OJKD> DI
OC@ I@SO >C<KO@M.
#DI<GGT, Q<MD<=G@N ><I =@ >JINOMP>O@? =T OC@ Q<GP@ JA <I @SKM@NNDJI JA < >JHK<OD=G@ OTK@:
JOU TQFFE = TPNF$BMDVMBUJPO();
TQFFE <=JQ@ RJPG? =@ >JINOMP>O@? =T OC@ M@OPMI Q<GP@ JA TPNF$BMDVMBUJPO().
Finalization 45.3.2
#DI<GDUDIB DN OC@ ADI<G JK@M<ODJIN OC<O <M@ @S@>PO@? AJM < Q<MD<=G@ <I? M@>G<DHDIB DON H@HJMT:
1. Destruction: 1C@ ADI<G JK@M<ODJIN OC<O HPNO =@ @S@>PO@? AJM OC@ Q<MD<=G@.
2. Reclaiming the variable's memory: /@>G<DHDIB OC@ KD@>@ JA H@HJMT OC<O OC@ Q<MD<=G@
C<N =@@I GDQDIB JI.
#JM NDHKG@ API?<H@IO<G OTK@N, OC@M@ <M@ IJ ADI<G JK@M<ODJIN OJ @S@>PO@. #JM @S<HKG@, OC@
Q<GP@ JA < Q<MD<=G@ JA OTK@ JOU DN IJO N@O =<>F OJ U@MJ. #JM NP>C Q<MD<=G@N OC@M@ DN JIGT
M@>G<DHDIB OC@DM H@HJMT, NJ OC<O DO RDGG =@ PN@? AJM JOC@M Q<MD<=G@N G<O@M.
,I OC@ JOC@M C<I?, NJH@ OTK@N JA Q<MD<=G@N M@LPDM@ NK@>D<G JK@M<ODJIN ?PMDIB ADI<GDU<ODJI. #JM
@S<HKG@, < 'JMF J=E@>O RJPG? I@@? OJ RMDO@ OC@ >C<M<>O@MN OC<O <M@ NODGG DI DON JPOKPO =PAA@M OJ
?DNF <I? IJODAT OC@ ADG@ NTNO@H OC<O DO IJ GJIB@M PN@N OC@ ADG@. 1C@N@ JK@M<ODJIN <M@ OC@
?@NOMP>ODJI JA < 'JMF J=E@>O.
#DI<G JK@M<ODJIN JA <MM<TN <M@ <O < GDOOG@ CDBC@M-G@Q@G: B@AJM@ ADI<GDUDIB OC@ <MM<T, ADMNO DON
@G@H@ION <M@ ?@NOMP>O@?. &A OC@ @G@H@ION <M@ JA < NDHKG@ API?<H@IO<G OTK@ GDF@ JOU, OC@I
OC@M@ <M@ IJ NK@>D<G ADI<G JK@M<ODJIN AJM OC@H. &A OC@ @G@H@ION <M@ JA < NOMP>O JM < >G<NN OTK@
OC<O I@@?N ADI<GDU<ODJI, OC@I OCJN@ JK@M<ODJIN <M@ @S@>PO@? AJM @<>C @G@H@IO.
";87F;?7E 3@6 FG@63?7@F3> %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
230
ANNJ>D<ODQ@ <MM<TN <M@ NDHDG<M OJ <MM<TN. A??DODJI<GGT, OC@ F@TN H<T <GNJ =@ ADI<GDU@? DA OC@T
<M@ JA < OTK@ OC<O I@@?N ?@NOMP>ODJI.
The garbage collector: ! DN < 93D4397-5A>>75F76 G<IBP<B@. &I NP>C G<IBP<B@N ADI<GDUDIB <I
J=E@>O I@@? IJO =@ DIDOD<O@? @SKGD>DOGT =T OC@ KMJBM<HH@M. 4C@I < Q<MD<=G@'N GDA@ODH@ @I?N, DON
ADI<GDU<ODJI DN <POJH<OD><GGT C<I?G@? =T OC@ B<M=<B@ >JGG@>OJM. 4@ RDGG >JQ@M OC@ B<M=<B@
>JGG@>OJM <I? NK@>D<G H@HJMT H<I<B@H@IO DI < G<O@M >C<KO@M.
3<MD<=G@N ><I =@ ADI<GDU@? DI ORJ R<TN:
t When the lifetime ends: 1C@ ADI<GDU<ODJI C<KK@IN PKJI OC@ @I? JA GDA@ JA OC@ Q<MD<=G@.
t Some time in the future: 1C@ ADI<GDU<ODJI C<KK@IN <O <I DI?@O@MHDI<O@ ODH@ DI OC@
APOPM@ =T OC@ B<M=<B@ >JGG@>OJM.
4CD>C JA OC@ ORJ R<TN < Q<MD<=G@ RDGG =@ ADI<GDU@? ?@K@I?N KMDH<MDGT JI DON OTK@. 0JH@ OTK@N
GDF@ <MM<TN, <NNJ>D<ODQ@ <MM<TN <I? >G<NN@N <M@ IJMH<GGT ?@NOMP>O@? =T OC@ B<M=<B@ >JGG@>OJM
NJH@ ODH@ DI OC@ APOPM@.
Assignment 45.3.3
1C@ JOC@M API?<H@IO<G JK@M<ODJI OC<O < Q<MD<=G@ @SK@MD@I>@N ?PMDIB DON GDA@ODH@ DN
<NNDBIH@IO.
#JM NDHKG@ API?<H@IO<G OTK@N <NNDBIH@IO DN H@M@GT >C<IBDIB OC@ Q<GP@ JA OC@ Q<MD<=G@. AN R@
C<Q@ N@@I <=JQ@ JI OC@ H@HJMT M@KM@N@IO<ODJI, <I JOU Q<MD<=G@ RJPG? NO<MO C<QDIB OC@ Q<GP@
124 DINO@<? JA 123. %JR@Q@M, HJM@ B@I@M<GGT, <NNDBIH@IO >JINDNON JA ORJ NO@KN, RCD>C <M@ IJO
I@>@NN<MDGT @S@>PO@? DI OC@ AJGGJRDIB JM?@M:
t Destructing the old value
t Constructing the new value
1C@N@ ORJ NO@KN <M@ IJO DHKJMO<IO AJM NDHKG@ API?<H@IO<G OTK@N OC<O ?JI'O I@@? ?@NOMP>ODJI.
#JM OTK@N OC<O I@@? ?@NOMP>ODJI, DO DN DHKJMO<IO OJ M@H@H=@M OC<O <NNDBIH@IO DN <
>JH=DI<ODJI JA OC@ ORJ NO@KN <=JQ@.
";87F;?7E 3@6 FG@63?7@F3> %B7D3F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
231
Value Types and Reference Types 46
1CDN >C<KO@M DIOMJ?P>@N OC@ >JI>@KON JA Q<GP@ OTK@N <I? M@A@M@I>@ OTK@N. 1C@N@ >JI>@KON <M@
K<MOD>PG<MGT DHKJMO<IO OJ PI?@MNO<I? OC@ ?DAA@M@I>@N =@OR@@I NOMP>ON <I? >G<NN@N.
1CDN >C<KO@M <GNJ B@ON DIOJ HJM@ ?@O<DG RDOC OC@ & JK@M<OJM.
1C@ >C<KO@M @I?N RDOC < O<=G@ OC<O >JIO<DIN OC@ JPO>JH@N JA OC@ AJGGJRDIB ORJ >JI>@KON AJM
?DAA@M@IO OTK@N JA Q<MD<=G@N:
t 3<GP@ >JHK<MDNJI
t A??M@NN >JHK<MDNJI
Value types
46.1
3<GP@ OTK@N <M@ @<NT OJ ?@N>MD=@: 3<MD<=G@N JA Q<GP@ OTK@N ><MMT Q<GP@N. #JM @S<HKG@, <GG JA OC@
DIO@B@M <I? AGJ<ODIB KJDIO OTK@N <M@ Q<GP@N OTK@N. AGOCJPBC IJO DHH@?D<O@GT J=QDJPN, ADS@?-
G@IBOC <MM<TN <M@ Q<GP@ OTK@N <N R@GG.
#JM @S<HKG@, < Q<MD<=G@ JA OTK@ JOU C<N <I DIO@B@M Q<GP@:
JOU TQFFE = 123;
<MMTDIB JI AMJH OC@ KM@QDJPN >C<KO@M <I? M@KM@N@IODIB OC@ H@HJMT <N < MD==JI BJDIB AMJH
G@AO OJ MDBCO, NP>C < Q<MD<=G@ RJPG? =@ GDQDIB JI NJH@ K<MO JA OC@ H@HJMT:
TQFFE
---+-----+---
] 123 ]
---+-----+---
4C@I Q<MD<=G@N JA Q<GP@ OTK@N <M@ >JKD@?, OC@T B@O OC@DM JRI Q<GP@N:
JOU OFX4QFFE = TQFFE;
1C@ I@R Q<MD<=G@ RJPG? C<Q@ < KG<>@ <I? < Q<GP@ JA DON JRI:
TQFFE OFX4QFFE
---+-----+--- ---+-----+---
] 123 ] ] 123 ]
---+-----+--- ---+-----+---
+<OPM<GGT, HJ?DAD><ODJIN OC<O <M@ H<?@ OJ OC@N@ Q<MD<=G@N <M@ DI?@K@I?@IO:
TQFFE = 200;
1C@ Q<GP@ JA OC@ JOC@M Q<MD<=G@ ?J@N IJO >C<IB@:
TQFFE OFX4QFFE
---+-----+--- ---+-----+---
] 200 ] ] 123 ]
---+-----+--- ---+-----+---
+3>G7 )KB7E 3@6 '787D7@57 )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
232
The use of BTTFSU checks below 46.1.1
1C@ AJGGJRDIB @S<HKG@N >JIO<DI BTTFSU >C@>FN OJ DI?D><O@ OC<O OC@DM >JI?DODJIN <M@ OMP@. &I
JOC@M RJM?N, OC@T <M@ IJO >C@>FN DI OC@ IJMH<G N@IN@, M<OC@M HT R<T JA O@GGDIB OJ OC@ M@<?@M
OC<O "OCDN DN OMP@".
#JM @S<HKG@, OC@ >C@>F BTTFSU(TQFFE == OFX4QFFE) =@GJR H@<IN "NK@@? DN @LP<G OJ
I@R0K@@?".
Value identity 46.1.2
AN OC@ H@HJMT M@KM@N@IO<ODJIN <=JQ@ DI?D><O@, OC@M@ <M@ ORJ OTK@N JA @LP<GDOT OC<O >JI>@MI
Q<MD<=G@N:
t Value equality: 1C@ == JK@M<OJM OC<O <KK@<M DI H<IT @S<HKG@N OCMJPBCJPO OC@ =JJF
>JHK<M@N Q<MD<=G@N =T OC@DM Q<GP@N. 4C@I ORJ Q<MD<=G@N <M@ N<D? OJ =@ 7CG3> DI OC<O N@IN@,
OC@DM Q<GP@N <M@ @LP<G.
t Value identity: &I OC@ N@IN@ JA JRIDIB N@K<M<O@ Q<GP@N, TQFFE <I? OFX4QFFE C<Q@
N@K<M<O@ D?@IODOD@N. "Q@I RC@I OC@DM Q<GP@N <M@ @LP<G, OC@T <M@ ?DAA@M@IO Q<MD<=G@N.
JOU TQFFE = 123;
JOU OFX4QFFE = TQFFE;
BTTFSU(TQFFE == OFX4QFFE);
TQFFE = 200;
BTTFSU(TQFFE != OFX4QFFE);
Address-of operator, & 46.1.3
4@ C<Q@ =@@I PNDIB OC@ & JK@M<OJM NJ A<M RDOC SFBEG(). 1C@ & JK@M<OJM O@GGN SFBEG() RC@M@
OJ KPO OC@ DIKPO ?<O<.
1C@ <??M@NN@N JA Q<MD<=G@N ><I =@ PN@? AJM JOC@M KPMKJN@N <N R@GG. 1C@ AJGGJRDIB >J?@ NDHKGT
KMDION OC@ <??M@NN@N JA ORJ Q<MD<=G@N:
JOU TQFFE = 123;
JOU OFX4QFFE = TQFFE;
XSJUFMO("TQFFE : ", TQFFE, " BEESFTT: ", &TQFFE);
XSJUFMO("OFX4QFFE: ", OFX4QFFE, " BEESFTT: ", &OFX4QFFE);
TQFFE <I? OFX4QFFE C<Q@ OC@ N<H@ Q<GP@ =PO OC@DM <??M@NN@N <M@ ?DAA@M@IO:
TQFFE : 123 BEESFTT: 7'''4#39$738
OFX4QFFE: 123 BEESFTT: 7'''4#39$73$
Note: !F ;E @AD?3> 8AD F:7 366D7EE7E FA :3H7 6;887D7@F H3>G7E 7H7DK F;?7 F:7 BDA9D3? ;E DG@.
+3D;34>7E >;H7 3F B3DFE A8 ?7?ADK F:3F :3BB7@ FA 47 3H3;>34>7 6GD;@9 F:3F B3DF;5G>3D 7J75GF;A@
A8 F:7 BDA9D3?.
A??M@NN@N <M@ IJMH<GGT KMDIO@? DI C@S<?@>DH<G AJMH<O.
A??DODJI<GGT, OC@ A<>O OC<O OC@ ORJ <??M@NN@N <M@ 4 <K<MO DI?D><O@N OC<O OCJN@ ORJ DIO@B@MN <M@
KG<>@? I@SO OJ @<>C JOC@M DI H@HJMT. (+JO@ OC<O OC@ Q<GP@ JA C@S<?@>DH<G DN 12, NJ OC@
?DAA@M@I>@ =@OR@@I 8 <I? 12 DN 4.)
Reference variables
46.2
B@AJM@ B@OODIB OJ M@A@M@I>@ OTK@N G@O'N ADMNO ?@ADI@ M@A@M@I>@ Q<MD<=G@N.
+3>G7 )KB7E 3@6 '787D7@57 )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
233
Terminology: 4@ C<Q@ =@@I PNDIB OC@ KCM<N@ FA BDAH;67 3557EE FA NJ A<M DI N@Q@M<G >JIO@SON
OCMJPBCJPO OC@ =JJF. #JM @S<HKG@, NGD>@N <I? <NNJ>D<ODQ@ <MM<TN ?J IJO JRI <IT @G@H@ION =PO
KMJQD?@ <>>@NN OJ @G@H@ION OC<O <M@ JRI@? =T OC@ ! MPIODH@. AIJOC@M KCM<N@ OC<O DN D?@IOD><G
DI H@<IDIB DN 47;@9 3 D787D7@57 A8 <N DI "NGD>@N <M@ M@A@M@I>@N JA U@MJ JM HJM@ @G@H@ION",
RCD>C DN NJH@ODH@N PN@? @Q@I NCJMO@M <N FA D787D7@57 <N DI "OCDN NGD>@ M@A@M@I>@N ORJ
@G@H@ION". #DI<GGT, OC@ <>O JA <>>@NNDIB < Q<GP@ OCMJPBC < M@A@M@I>@ DN 67D787D7@5;@9.
/@A@M@I>@ Q<MD<=G@N <M@ Q<MD<=G@N OC<O <>O GDF@ <GD<N@N JA JOC@M Q<MD<=G@N. AGOCJPBC OC@T GJJF
<I? <M@ PN@? GDF@ Q<MD<=G@N, OC@T ?J IJO C<Q@ Q<GP@N OC@HN@GQ@N. *J?DAD><ODJIN H<?@ JI <
M@A@M@I>@ Q<MD<=G@ >C<IB@ OC@ Q<GP@ JA OC@ <>OP<G Q<MD<=G@.
4@ C<Q@ <GM@<?T PN@? M@A@M@I>@ Q<MD<=G@N NJ A<M DI ORJ >JIO@SON:
t SFG in GPSFBDI loops: 1C@ SFG F@TRJM? H<F@N OC@ GJJK Q<MD<=G@ OC@ 35FG3> @G@H@IO OC<O
>JMM@NKJI?N OJ OC<O DO@M<ODJI. 4C@I OC@ SFG F@TRJM? DN IJO PN@?, OC@ GJJK Q<MD<=G@ DN <
5ABK JA OC@ <>OP<G @G@H@IO.
1CDN ><I =@ ?@HJINOM<O@? =T OC@ & JK@M<OJM <N R@GG. &A OC@DM <??M@NN@N <M@ OC@ N<H@,
ORJ Q<MD<=G@N RJPG? =@ M@A@M@I>DIB OC@ N<H@ Q<GP@ (JM OC@ E3?7 7>7?7@F DI OCDN
><N@):
JOU<> TMJDF = < 0, 1, 2, 3, 4 >;
GPSFBDI (J, SFG FMFNFOU; TMJDF) \
BTTFSU(&FMFNFOU == &TMJDF<J>);
^
AGOCJPBC OC@T <M@ N@K<M<O@ Q<MD<=G@N, OC@ A<>O OC<O OC@ <??M@NN@N JA FMFNFOU <I?
TMJDF<J> <M@ OC@ N<H@ KMJQ@N OC<O OC@T C<Q@ OC@ N<H@ Q<GP@ D?@IODOT.
&I JOC@M RJM?N, FMFNFOU <I? TMJDF<J> <M@ M@A@M@I>@N JA OC@ N<H@ Q<GP@. *J?DATDIB
@DOC@M JA OCJN@ <AA@>ON OC@ <>OP<G Q<GP@. 1C@ AJGGJRDIB H@HJMT G<TJPO DI?D><O@N <
NI<KNCJO JA OC@ DO@M<ODJI RC@I J DN 3:
TMJDF<0> TMJDF<1> TMJDF<2> TMJDF<3> TMJDF<4>
p p p (FMFNFOU)
--+--------+--------+--------+--------+---------+--
] 0 ] 1 ] 2 ] 3 ] 4 ]
--+--------+--------+--------+--------+---------+--
t SFG and PVU function parameters: #PI>ODJI K<M<H@O@MN OC<O <M@ NK@>DAD@? <N SFG JM
PVU <M@ <GD<N@N JA OC@ <>OP<G Q<MD<=G@ OC@ API>ODJI DN ><GG@? RDOC.
1C@ AJGGJRDIB @S<HKG@ ?@HJINOM<O@N OCDN ><N@ =T K<NNDIB OC@ N<H@ Q<MD<=G@ OJ
N@K<M<O@ SFG <I? PVU K<M<H@O@MN JA < API>ODJI. AB<DI, OC@ & JK@M<OJM DI?D><O@N OC<O
=JOC K<M<H@O@MN C<Q@ OC@ N<H@ Q<GP@ D?@IODOT:
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
JOU PSJHJOBM7BSJBCMF;
XSJUFMO("BEESFTT PG PSJHJOBM7BSJBCMF: ", &PSJHJOBM7BSJBCMF);
GPP(PSJHJOBM7BSJBCMF, PSJHJOBM7BSJBCMF);
^
WPJE GPP(SFG JOU SFG1BSBNFUFS, PVU JOU PVU1BSBNFUFS)
\
XSJUFMO("BEESFTT PG SFG1BSBNFUFS : ", &SFG1BSBNFUFS);
XSJUFMO("BEESFTT PG PVU1BSBNFUFS : ", &PVU1BSBNFUFS);
BTTFSU(&SFG1BSBNFUFS == &PVU1BSBNFUFS);
^
+3>G7 )KB7E 3@6 '787D7@57 )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
234
AGOCJPBC OC@T <M@ ?@ADI@? <N N@K<M<O@ K<M<H@O@MN, SFG1BSBNFUFS <I? PVU1BSBNFUFS
<M@ <GD<N@N JA PSJHJOBM7BSJBCMF:
BEESFTT PG PSJHJOBM7BSJBCMF: 7'''24172958
BEESFTT PG SFG1BSBNFUFS : 7'''24172958
BEESFTT PG PVU1BSBNFUFS : 7'''24172958
Reference types
46.3
3<MD<=G@N JA M@A@M@I>@ OTK@N C<Q@ DI?DQD?P<G D?@IODOD@N =PO OC@T ?J IJO C<Q@ DI?DQD?P<G Q<GP@N.
1C@T BDAH;67 3557EE FA @SDNODIB Q<MD<=G@N.
4@ C<Q@ <GM@<?T N@@I OCDN >JI>@KO RDOC NGD>@N. 0GD>@N ?J IJO JRI @G@H@ION, OC@T KMJQD?@
<>>@NN OJ @SDNODIB @G@H@ION:
WPJE NBJO()
\
// "MUIPVHI JU JT OBNFE BT 'BSSBZ' IFSF, UIJT WBSJBCMF JT
// B TMJDF BT XFMM. *U QSPWJEFT BDDFTT UP BMM PG UIF
// JOJUJBM FMFNFOUT:
JOU<> BSSBZ = < 0, 1, 2, 3, 4 >;
// " TMJDF UIBU QSPWJEFT BDDFTT UP FMFNFOUT PUIFS UIBO UIF
// GJSTU BOE UIF MBTU:
JOU<> TMJDF = BSSBZ<1 .. $ - 1>;
// "U UIJT QPJOU TMJDF<0> BOE BSSBZ<1> QSPWJEF BDDFTT UP
// UIF TBNF WBMVF:
BTTFSU(&TMJDF<0> == &BSSBZ<1>);
// $IBOHJOH TMJDF<0> DIBOHFT BSSBZ<1>:
TMJDF<0> = 42;
BTTFSU(BSSBZ<1> == 42);
^
JIOM<MT OJ M@A@M@I>@ Q<MD<=G@N, M@A@M@I>@ OTK@N <M@ IJO NDHKGT <GD<N@N. 1J N@@ OCDN
?DNODI>ODJI, G@O'N ?@ADI@ <IJOC@M NGD>@ <N < >JKT JA JI@ JA OC@ @SDNODIB NGD>@N:
JOU<> TMJDF2 = TMJDF;
1C@ ORJ NGD>@N C<Q@ OC@DM JRI <?M@NN@N. &I JOC@M RJM?N, OC@T C<Q@ N@K<M<O@ D?@IODOD@N:
BTTFSU(&TMJDF != &TMJDF2);
1C@ AJGGJRDIB GDNO DN < NPHH<MT JA OC@ ?DAA@M@I>@N =@OR@@I M@A@M@I>@ Q<MD<=G@N <I? M@A@M@I>@
OTK@N:
t /@A@M@I>@ Q<MD<=G@N ?J IJO C<Q@ D?@IODOD@N, OC@T <M@ <GD<N@N JA @SDNODIB Q<MD<=G@N.
t 3<MD<=G@N JA M@A@M@I>@ OTK@N C<Q@ D?@IODOD@N =PO OC@T ?J IJO JRI Q<GP@N; M<OC@M, OC@T
KMJQD?@ <>>@NN OJ @SDNODIB Q<GP@N.
1C@ R<T TMJDF <I? TMJDF2 GDQ@ DI H@HJMT ><I =@ DGGPNOM<O@? <N DI OC@ AJGGJRDIB ADBPM@:
TMJDF TMJDF2
---+---+---+---+---+---+--- ---+---+--- ---+---+---
] 0 ] 1 ] 2 ] 3 ] 4 ] ] P ] ] P ]
---+---+---+---+---+---+--- ---+-]-+--- ---+-]-+---
q ] ]
] ] ]
+--------------------+------------+
+3>G7 )KB7E 3@6 '787D7@57 )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
235
1C@ OCM@@ @G@H@ION OC<O OC@T =JOC M@A@M@I>@ <M@ CDBCGDBCO@? DI =GP@.
,I@ JA OC@ ?DAA@M@I>@N =@OR@@I ++ <I? ! DN OC<O >G<NN@N <M@ M@A@M@I>@ OTK@N DI !. AGOCJPBC
R@ RDGG >JQ@M >G<NN@N DI G<O@M >C<KO@MN DI ?@O<DG, OC@ AJGGJRDIB DN < NCJMO @S<HKG@ OJ
?@HJINOM<O@ OCDN A<>O:
DMBTT .Z$MBTT
\
JOU NFNCFS;
^
G<NN J=E@>ON <M@ >JINOMP>O@? =T OC@ OFX F@TRJM?:
BVUP WBSJBCMF = OFX .Z$MBTT;
WBSJBCMF DN < M@A@M@I>@ OJ <I <IJITHJPN .Z$MBTT J=E@>O OC<O C<N =@@I >JINOMP>O@? =T OFX:
(BOPOZNPVT .Z$MBTT PCKFDU) WBSJBCMF
---+-------------------+--- ---+---+---
] ... ] ] P ]
---+-------------------+--- ---+-]-+---
q ]
] ]
+--------------------+
'PNO GDF@ RDOC NGD>@N, RC@I WBSJBCMF DN >JKD@?, OC@ >JKT =@>JH@N <IJOC@M M@A@M@I>@ OJ OC@
N<H@ J=E@>O. 1C@ >JKT C<N DON JRI <??M@NN:
BVUP WBSJBCMF = OFX .Z$MBTT;
BVUP WBSJBCMF2 = WBSJBCMF;
BTTFSU(WBSJBCMF == WBSJBCMF2);
BTTFSU(&WBSJBCMF != &WBSJBCMF2);
1C@T <M@ @LP<G AMJH OC@ KJDIO JA QD@R JA M@A@M@I>DIB OC@ N<H@ J=E@>O, =PO OC@T <M@ N@K<M<O@
Q<MD<=G@N:
(BOPOZNPVT .Z$MBTT PCKFDU) WBSJBCMF WBSJBCMF2
---+-------------------+--- ---+---+--- ---+---+---
] ... ] ] P ] ] P ]
---+-------------------+--- ---+-]-+--- ---+-]-+---
q ] ]
] ] ]
+--------------------+------------+
1CDN ><I <GNJ =@ NCJRI =T HJ?DATDIB OC@ H@H=@M JA OC@ J=E@>O:
BVUP WBSJBCMF = OFX .Z$MBTT;
WBSJBCMF.NFNCFS = 1;
BVUP WBSJBCMF2 = WBSJBCMF; // 5IFZ TIBSF UIF TBNF PCKFDU
WBSJBCMF2.NFNCFS = 2;
BTTFSU(WBSJBCMF.NFNCFS == 2); // 5IF PCKFDU UIBU WBSJBCMF
// QSPWJEFT BDDFTT UP IBT
// DIBOHFE.
AIJOC@M M@A@M@I>@ OTK@ DN <NNJ>D<ODQ@ <MM<TN. 4C@I <NNDBI@?, OC@T OJJ KMJQD?@ <>>@NN OJ OC@
N<H@ N@O JA @G@H@ION:
TUSJOH<JOU> CZ/BNF =
<
+3>G7 )KB7E 3@6 '787D7@57 )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
236
1 : "POF",
10 : "UFO",
100 : "IVOESFE",
>;
// 5IF UXP BTTPDJBUJWF BSSBZT XJMM CF TIBSJOH UIF TBNF
// TFU PG FMFNFOUT
TUSJOH<JOU> CZ/BNF2 = CZ/BNF;
// 5IF NBQQJOH BEEFE UISPVHI UIF TFDPOE ...
CZ/BNF2<4> = "GPVS";
// ... JT WJTJCMF UISPVHI UIF GJSTU.
BTTFSU(CZ/BNF<4> == "GPVS");
The difference in the assignment operation 46.3.1
4DOC Q<GP@ OTK@N <I? M@A@M@I>@ Q<MD<=G@N, OC@ <NNDBIH@IO JK@M<ODJI >C<IB@N F:7 35FG3> H3>G7:
WPJE NBJO()
\
JOU OVNCFS = 8;
IBMWF(OVNCFS); // 5IF BDUVBM WBMVF DIBOHFT
BTTFSU(OVNCFS == 4);
^
WPJE IBMWF(SFG JOU EJWJEFOE)
\
EJWJEFOE /= 2;
^
,I OC@ JOC@M C<I?, RDOC M@A@M@I>@ OTK@N, OC@ <NNDBIH@IO JK@M<ODJI >C<IB@N I:;5: H3>G7 ;E
47;@9 3557EE76. #JM @S<HKG@, OC@ <NNDBIH@IO JA OC@ TMJDF3 Q<MD<=G@ =@GJR ?J@N IJO >C<IB@
OC@ Q<GP@ JA <IT @G@H@IO; M<OC@M, DO >C<IB@N RC<O @G@H@ION TMJDF3 DN IJR < M@A@M@I>@ JA:
JOU<> TMJDF1 = < 10, 11, 12, 13, 14 >;
JOU<> TMJDF2 = < 20, 21, 22 >;
JOU<> TMJDF3 = TMJDF1<1 .. 3>; // "DDFTT UP UIF FMFNFOUT
// PG TMJDF1 XJUI JOEFYFT 1
// BOE 2
TMJDF3<0> = 777;
BTTFSU(TMJDF1 == < 10, 777, 12, 13, 14 >);
// 5IJT BTTJHONFOU EPFT OPU NPEJGZ UIF FMFNFOUT UIBU
// TMJDF3 JT QSPWJEJOH BDDFTT UP, JU NBLFT TMJDF3 QSPWJEF
// BDDFTT UP PUIFS FMFNFOUT.
TMJDF3 = TMJDF2<$ - 1 .. $>; // "DDFTT UP UIF MBTU FMFNFOU
TMJDF3<0> = 888;
BTTFSU(TMJDF2 == < 20, 21, 888 >);
)@O'N ?@HJINOM<O@ OC@ N<H@ @AA@>O OCDN ODH@ RDOC ORJ J=E@>ON JA OC@ .Z$MBTT OTK@:
BVUP WBSJBCMF1 = OFX .Z$MBTT;
WBSJBCMF1.NFNCFS = 1;
BVUP WBSJBCMF2 = OFX .Z$MBTT;
WBSJBCMF2.NFNCFS = 2;
BVUP B$PQZ = WBSJBCMF1;
B$PQZ.NFNCFS = 3;
B$PQZ = WBSJBCMF2;
+3>G7 )KB7E 3@6 '787D7@57 )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
237
B$PQZ.NFNCFS = 4;
BTTFSU(WBSJBCMF1.NFNCFS == 3);
BTTFSU(WBSJBCMF2.NFNCFS == 4);
1C@ B$PQZ Q<MD<=G@ <=JQ@ ADMNO M@A@M@I>@N OC@ N<H@ J=E@>O <N WBSJBCMF1, <I? OC@I OC@ N<H@
J=E@>O <N WBSJBCMF2. AN < >JIN@LP@I>@, OC@ .NFNCFS OC<O DN HJ?DAD@? OCMJPBC B$PQZ DN ADMNO
WBSJBCMF1'N <I? OC@I WBSJBCMF2'N.
Variables of reference types may not be referencing any object 46.3.2
4DOC < M@A@M@I>@ Q<MD<=G@, OC@M@ DN <GR<TN <I <>OP<G Q<MD<=G@ OC<O DO DN <I <GD<N JA; DO ><I IJO
NO<MO DON GDA@ RDOCJPO < Q<MD<=G@. ,I OC@ JOC@M C<I?, Q<MD<=G@N JA M@A@M@I>@ OTK@N ><I NO<MO OC@DM
GDQ@N RDOCJPO M@A@M@I>DIB <IT J=E@>O.
#JM @S<HKG@, < .Z$MBTT Q<MD<=G@ ><I =@ ?@ADI@? RDOCJPO <I <>OP<G J=E@>O C<QDIB =@@I >M@<O@?
=T OFX:
.Z$MBTT WBSJBCMF;
0P>C Q<MD<=G@N C<Q@ OC@ NK@>D<G Q<GP@ JA OVMM. 4@ RDGG >JQ@M OVMM <I? OC@ JT F@TRJM? DI OC@
I@SO >C<KO@M.
Fixed-length arrays are value types, slices are reference types
46.4
!'N <MM<TN <I? NGD>@N ?DQ@MB@ RC@I DO >JH@N OJ Q<GP@ OTK@ Q@MNPN M@A@M@I>@ OTK@.
AN R@ C<Q@ <GM@<?T N@@I <=JQ@, NGD>@N <M@ M@A@M@I>@ OTK@N. ,I OC@ JOC@M C<I?, ADS@?-G@IBOC
<MM<TN <M@ Q<GP@ OTK@N. 1C@T JRI OC@DM @G@H@ION <I? =@C<Q@ <N DI?DQD?P<G Q<GP@N:
JOU<3> BSSBZ1 = < 10, 20, 30 >;
BVUP BSSBZ2 = BSSBZ1; // BSSBZ2'T FMFNFOUT BSF EJGGFSFOU
// GSPN BSSBZ1'T
BSSBZ2<0> = 11;
// 'JSTU BSSBZ JT OPU BGGFDUFE:
BTTFSU(BSSBZ1<0> == 10);
BSSBZ1 DN < ADS@?-G@IBOC <MM<T =@><PN@ DON G@IBOC DN NK@>DAD@? RC@I DO C<N =@@I ?@ADI@?. 0DI>@
BVUP H<F@N OC@ >JHKDG@M DIA@M OC@ OTK@ JA BSSBZ2, DO DN < ADS@?-G@IBOC <MM<T <N R@GG. 1C@
Q<GP@N JA BSSBZ2'N @G@H@ION <M@ >JKD@? AMJH OC@ Q<GP@N JA OC@ @G@H@ION JA BSSBZ1. "<>C
<MM<T C<N DON JRI @G@H@ION. *J?DATDIB <I @G@H@IO OCMJPBC JI@ ?J@N IJO <AA@>O OC@ JOC@M.
Experiment
46.5
1C@ AJGGJRDIB KMJBM<H DN <I @SK@MDH@IO JA <KKGTDIB OC@ == JK@M<OJM OJ ?DAA@M@IO OTK@N. &O
<KKGD@N OC@ JK@M<OJM OJ =JOC Q<MD<=G@N JA < >@MO<DI OTK@ <I? OJ OC@ <??M@NN@N JA OCJN@
Q<MD<=G@N. 1C@ KMJBM<H KMJ?P>@N OC@ AJGGJRDIB JPOKPO:
+3>G7 )KB7E 3@6 '787D7@57 )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
238
5ZQF PG WBSJBCMF B == C &B == &C
===========================================================================
WBSJBCMFT XJUI FRVBM WBMVFT (WBMVF UZQF) USVF GBMTF
WBSJBCMFT XJUI EJGGFSFOU WBMVFT (WBMVF UZQF) GBMTF GBMTF
GPSFBDI XJUI 'SFG' WBSJBCMF USVF USVF
GPSFBDI XJUIPVU 'SFG' WBSJBCMF USVF GBMTF
GVODUJPO XJUI 'PVU' QBSBNFUFS USVF USVF
GVODUJPO XJUI 'SFG' QBSBNFUFS USVF USVF
GVODUJPO XJUI 'JO' QBSBNFUFS USVF GBMTF
TMJDFT QSPWJEJOH BDDFTT UP TBNF FMFNFOUT USVF GBMTF
TMJDFT QSPWJEJOH BDDFTT UP EJGGFSFOU FMFNFOUT GBMTF GBMTF
.Z$MBTT WBSJBCMFT UP TBNF PCKFDU (SFGFSFODF UZQF) USVF GBMTF
.Z$MBTT WBSJBCMFT UP EJGGFSFOU PCKFDUT (SFGFSFODF UZQF) GBMTF GBMTF
1C@ O<=G@ <=JQ@ C<N =@@I B@I@M<O@? =T OC@ AJGGJRDIB KMJBM<H:
JNQPSU TUE.TUEJP;
JNQPSU TUE.DPOW;
JNQPSU TUE.BSSBZ;
JOU NPEVMF7BSJBCMF = 9;
DMBTT .Z$MBTT
\
JOU NFNCFS;
^
WPJE QSJOU)FBEFS()
\
JNNVUBCMF EDIBS<> IFBEFS =
" 5ZQF PG WBSJBCMF"
" B == C &B == &C";
XSJUFMO();
XSJUFMO(IFBEFS);
XSJUFMO(SFQMJDBUF("=", IFBEFS.MFOHUI));
^
WPJE QSJOU*OGP(DPOTU EDIBS<> MBCFM,
CPPM WBMVF&RVBMJUZ,
CPPM BEESFTT&RVBMJUZ)
\
XSJUFGMO("%55T%9T%9T",
MBCFM,
UP!TUSJOH(WBMVF&RVBMJUZ),
UP!TUSJOH(BEESFTT&RVBMJUZ));
^
WPJE NBJO()
\
QSJOU)FBEFS();
JOU OVNCFS1 = 12;
JOU OVNCFS2 = 12;
QSJOU*OGP("WBSJBCMFT XJUI FRVBM WBMVFT (WBMVF UZQF)",
OVNCFS1 == OVNCFS2,
&OVNCFS1 == &OVNCFS2);
JOU OVNCFS3 = 3;
QSJOU*OGP("WBSJBCMFT XJUI EJGGFSFOU WBMVFT (WBMVF UZQF)",
OVNCFS1 == OVNCFS3,
&OVNCFS1 == &OVNCFS3);
JOU<> TMJDF = < 4 >;
GPSFBDI (J, SFG FMFNFOU; TMJDF) \
QSJOU*OGP("GPSFBDI XJUI 'SFG' WBSJBCMF",
FMFNFOU == TMJDF<J>,
&FMFNFOU == &TMJDF<J>);
^
+3>G7 )KB7E 3@6 '787D7@57 )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
239
GPSFBDI (J, FMFNFOU; TMJDF) \
QSJOU*OGP("GPSFBDI XJUIPVU 'SFG' WBSJBCMF",
FMFNFOU == TMJDF<J>,
&FMFNFOU == &TMJDF<J>);
^
PVU1BSBNFUFS(NPEVMF7BSJBCMF);
SFG1BSBNFUFS(NPEVMF7BSJBCMF);
JO1BSBNFUFS(NPEVMF7BSJBCMF);
JOU<> MPOH4MJDF = < 5, 6, 7 >;
JOU<> TMJDF1 = MPOH4MJDF;
JOU<> TMJDF2 = TMJDF1;
QSJOU*OGP("TMJDFT QSPWJEJOH BDDFTT UP TBNF FMFNFOUT",
TMJDF1 == TMJDF2,
&TMJDF1 == &TMJDF2);
JOU<> TMJDF3 = TMJDF1<0 .. $ - 1>;
QSJOU*OGP("TMJDFT QSPWJEJOH BDDFTT UP EJGGFSFOU FMFNFOUT",
TMJDF1 == TMJDF3,
&TMJDF1 == &TMJDF3);
BVUP WBSJBCMF1 = OFX .Z$MBTT;
BVUP WBSJBCMF2 = WBSJBCMF1;
QSJOU*OGP(
".Z$MBTT WBSJBCMFT UP TBNF PCKFDU (SFGFSFODF UZQF)",
WBSJBCMF1 == WBSJBCMF1,
&WBSJBCMF1 == &WBSJBCMF2);
BVUP WBSJBCMF3 = OFX .Z$MBTT;
QSJOU*OGP(
".Z$MBTT WBSJBCMFT UP EJGGFSFOU PCKFDUT (SFGFSFODF UZQF)",
WBSJBCMF1 == WBSJBCMF3,
&WBSJBCMF1 == &WBSJBCMF3);
^
WPJE PVU1BSBNFUFS(PVU JOU QBSBNFUFS)
\
QSJOU*OGP("GVODUJPO XJUI 'PVU' QBSBNFUFS",
QBSBNFUFS == NPEVMF7BSJBCMF,
&QBSBNFUFS == &NPEVMF7BSJBCMF);
^
WPJE SFG1BSBNFUFS(SFG JOU QBSBNFUFS)
\
QSJOU*OGP("GVODUJPO XJUI 'SFG' QBSBNFUFS",
QBSBNFUFS == NPEVMF7BSJBCMF,
&QBSBNFUFS == &NPEVMF7BSJBCMF);
^
WPJE JO1BSBNFUFS(JO JOU QBSBNFUFS)
\
QSJOU*OGP("GVODUJPO XJUI 'JO' QBSBNFUFS",
QBSBNFUFS == NPEVMF7BSJBCMF,
&QBSBNFUFS == &NPEVMF7BSJBCMF);
^
+JO@N:
t 1C@ KMJBM<H H<F@N PN@ JA < HJ?PG@ Q<MD<=G@ RC@I >JHK<MDIB ?DAA@M@IO OTK@N JA API>ODJI
K<M<H@O@MN. *J?PG@ Q<MD<=G@N <M@ ?@ADI@? <O HJ?PG@ G@Q@G, JPOND?@ JA <GG JA OC@ API>ODJIN.
1C@T <M@ BGJ=<GGT <>>@NND=G@ OJ <GG JA OC@ >J?@ DI OC@ HJ?PG@.
t 1C@ SFQMJDBUF() API>ODJI JA OC@ TUE.BSSBZ HJ?PG@ O<F@N < M<IB@ (OC@ "=" <=JQ@) <I?
M@K@<ON DO NK@>DAD@? IPH=@M JA ODH@N.
+3>G7 )KB7E 3@6 '787D7@57 )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
240
Summary
46.6
t 3<MD<=G@N JA Q<GP@ OTK@N C<Q@ OC@DM JRI Q<GP@N <I? <?M@NN@N.
t /@A@M@I>@ Q<MD<=G@N ?J IJO C<Q@ OC@DM JRI Q<GP@N IJM <??M@NN@N. 1C@T <M@ <GD<N@N JA
@SDNODIB Q<MD<=G@N.
t 3<MD<=G@N JA M@A@M@I>@ OTK@N C<Q@ OC@DM JRI <??M@NN@N =PO OC@ Q<GP@N OC<O OC@T M@A@M@I>@
?J IJO =@GJIB OJ OC@H.
t 4DOC M@A@M@I>@ OTK@N, <NNDBIH@IO ?J@N IJO >C<IB@ Q<GP@, DO >C<IB@N RCD>C Q<GP@ DN =@DIB
<>>@NN@?.
t 3<MD<=G@N JA M@A@M@I>@ OTK@N H<T =@ OVMM.
+3>G7 )KB7E 3@6 '787D7@57 )KB7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
241
The OVMM Value and the JT Operator 47
&I OC@ KM@QDJPN >C<KO@M R@ C<Q@ N@@I OC<O < Q<MD<=G@ JA < M@A@M@I>@ OTK@ I@@? IJO =@
M@A@M@I>DIB <I J=E@>O:
.Z$MBTT SFGFSFODFT"O0CKFDU = OFX .Z$MBTT;
.Z$MBTT WBSJBCMF; // EPFT OPU SFGFSFODF BO PCKFDU
B@DIB < M@A@M@I>@ OTK@, WBSJBCMF <=JQ@ ?J@N C<Q@ <I D?@IODOT =PO DO ?J@N IJO M@A@M@I>@ <IT
J=E@>O T@O. 0P>C <I J=E@>O ><I =@ KD>OPM@? <N C<QDIB < KG<>@ DI OC@ H@HJMT <N DI OC@
AJGGJRDIB KD>OPM@:
WBSJBCMF
---+------+---
] OVMM ]
---+------+---
A M@A@M@I>@ OC<O ?J@N IJO M@A@M@I>@ <IT Q<GP@ DN OVMM. 4@ RDGG @SK<I? JI OCDN =@GJR.
0P>C < Q<MD<=G@ DN DI <I <GHJNO PN@G@NN NO<O@. 0DI>@ OC@M@ DN IJ .Z$MBTT J=E@>O OC<O DO
M@A@M@I>@N, DO ><IIJO =@ PN@? DI < >JIO@SO RC@M@ <I <>OP<G .Z$MBTT J=E@>O DN I@@?@?:
JNQPSU TUE.TUEJP;
DMBTT .Z$MBTT
\
JOU NFNCFS;
^
WPJE VTF(.Z$MBTT WBSJBCMF)
\
XSJUFMO(WBSJBCMF.NFNCFS); // j #6(
^
WPJE NBJO()
\
.Z$MBTT WBSJBCMF;
VTF(WBSJBCMF);
^
AN OC@M@ DN IJ J=E@>O OC<O DN M@A@M@I>@? =T OC@ K<M<H@O@M OC<O VTF() M@>@DQ@N, <OO@HKODIB OJ
<>>@NN < H@H=@M JA < IJI-@SDNODIB J=E@>O M@NPGON DI < KMJBM<H >M<NC:
$ ./EFOFNF
4FHNFOUBUJPO GBVMU
"0@BH@IO<ODJI A<PGO" DN <I DI?D><ODJI OC<O OC@ KMJBM<H C<N =@@I O@MHDI<O@? =T OC@ JK@M<ODIB
NTNO@H =@><PN@ JA <OO@HKODIB OJ <>>@NN <I DGG@B<G H@HJMT <??M@NN.
The OVMM value
47.1
1C@ NK@>D<G Q<GP@ OVMM ><I =@ KMDIO@? EPNO GDF@ <IT JOC@M Q<GP@.
Note: &D;@F;@9 null 6;D75F>K 6A7E @AF 5A?B;>7 6G7 FA 3 >;?;F3F;A@ A8 F:7 5A?B;>7D F:3F :3E
477@ GE76 I:7@ ID;F;@9 F:;E 5:3BF7D (6?6 2.059). ):3F ;E I:K ;FE H3>G7 ;E 8;DEF 47;@9 53EF76
FA int 47>AI. ():7 cast AB7D3FAD I;>> 47 7JB>3;@76 ;@ F:7 @7JF 5:3BF7D.)
XSJUFMO(DBTU(JOU)OVMM);
):7 @G>> +3>G7 3@6 F:7 ;E %B7D3FAD
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
242
)DF@ DI H<IT JOC@M G<IBP<B@N, OC@ DIO@B@M M@KM@N@IO<ODJI JA OVMM DN 0:
0
A OVMM Q<MD<=G@ ><I =@ PN@? JIGT DI ORJ >JIO@SON:
1. ANNDBIDIB <I J=E@>O OJ DO
WBSJBCMF = OFX .Z$MBTT; // OPX SFGFSFODFT BO PCKFDU
1C@ <NNDBIH@IO <=JQ@ H<F@N WBSJBCMF KMJQD?@ <>>@NN OJ OC@ I@RGT >JINOMP>O@?
J=E@>O. #MJH OC<O KJDIO JI, WBSJBCMF ><I =@ PN@? AJM <IT Q<GD? JK@M<ODJI JA OC@
.Z$MBTT OTK@.
2. !@O@MHDIDIB RC@OC@M DO DN OVMM
%JR@Q@M, =@><PN@ OC@ == JK@M<OJM I@@?N <>OP<G J=E@>ON OJ >JHK<M@, OC@ @SKM@NNDJI
=@GJR ><IIJO =@ >JHKDG@?:
JG (WBSJBCMF == OVMM) // j DPNQJMBUJPO &3303
#JM OC<O M@<NJI, RC@OC@M < Q<MD<=G@ DN OVMM HPNO =@ ?@O@MHDI@? =T OC@ JT JK@M<OJM.
The JT operator
47.2
1CDN JK@M<OJM <INR@MN OC@ LP@NODJI "?J@N C<Q@ OC@ IPGG Q<GP@?":
JG (WBSJBCMF JT OVMM) \
// %PFT OPU SFGFSFODF BOZ PCKFDU
^
JT ><I =@ PN@? RDOC JOC@M OTK@N JA Q<MD<=G@N <N R@GG. &I OC@ AJGGJRDIB PN@, DO >JHK<M@N OC@
WBMVFT JA ORJ DIO@B@MN:
JG (TQFFE JT OFX4QFFE) \
// 5IFJS WBMVFT BSF FRVBM
^ FMTF \
// 5IFJS WBMVFT BSF EJGGFSFOU
^
4C@I PN@? RDOC NGD>@N, DO ?@O@MHDI@N RC@OC@M OC@ ORJ NGD>@N M@A@M@I>@ OC@ N<H@ N@O JA
@G@H@ION:
JG (TMJDF JT TMJDF2) \
// 5IFZ QSPWJEF BDDFTT UP UIF TBNF FMFNFOUT
^
The !JT operator
47.3
!JT DN OC@ JKKJNDO@ JA JT. &O KMJ?P>@N USVF RC@I OC@ Q<GP@N <M@ ?DAA@M@IO:
JG (TQFFE !JT OFX4QFFE) \
// 5IFJS WBMVFT BSF EJGGFSFOU
^
):7 @G>> +3>G7 3@6 F:7 ;E %B7D3FAD
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
243
Assigning the OVMM value
47.4
ANNDBIDIB OC@ OVMM Q<GP@ OJ < Q<MD<=G@ JA < M@A@M@I>@ OTK@ H<F@N OC<O Q<MD<=G@ NOJK
M@A@M@I>DIB DON >PMM@IO J=E@>O.
&A OC<O <NNDBIH@IO C<KK@IN OJ =@ O@MHDI<ODIB OC@ Q@MT G<NO M@A@M@I>@ OJ OC@ <>OP<G J=E@>O,
OC@I OC@ <>OP<G J=E@>O =@>JH@N < ><I?D?<O@ AJM ADI<GDU<ODJI =T OC@ B<M=<B@ >JGG@>OJM. AAO@M <GG,
IJO =@DIB M@A@M@I>@? =T <IT Q<MD<=G@ H@<IN OC<O OC@ J=E@>O DN IJO =@DIB PN@? DI OC@ KMJBM<H
<O <GG.
)@O'N GJJF <O OC@ @S<HKG@ AMJH OC@ KM@QDJPN >C<KO@M RC@M@ ORJ Q<MD<=G@N C<Q@ =@@I
M@A@M@I>DIB OC@ N<H@ J=E@>O:
BVUP WBSJBCMF = OFX .Z$MBTT;
BVUP WBSJBCMF2 = WBSJBCMF;
1C@ AJGGJRDIB DN < M@KM@N@IO<ODJI JA OC@ NO<O@ JA OC@ H@HJMT <AO@M @S@>PODIB OC<O >J?@:
(BOPOZNPVT .Z$MBTT PCKFDU) WBSJBCMF WBSJBCMF2
---+-------------------+--- ---+---+--- ---+---+---
] ... ] ] P ] ] P ]
---+-------------------+--- ---+-]-+--- ---+-]-+---
q ] ]
] ] ]
+--------------------+------------+
ANNDBIDIB OC@ OVMM Q<GP@ OJ JI@ JA OC@N@ Q<MD<=G@N =M@<FN DON M@G<ODJINCDK RDOC OC@ J=E@>O:
WBSJBCMF = OVMM;
AO OCDN KJDIO OC@M@ DN JIGT WBSJBCMF2 OC<O M@A@M@I>@N OC@ .Z$MBTT J=E@>O:
(BOPOZNPVT .Z$MBTT PCKFDU) WBSJBCMF WBSJBCMF2
---+-------------------+--- ---+----+--- ---+---+---
] ... ] ]OVMM] ] P ]
---+-------------------+--- ---+----+--- ---+-]-+---
q ]
] ]
+----------------------------------+
ANNDBIDIB OVMM OJ OC@ G<NO M@A@M@I>@ RJPG? H<F@ OC@ .Z$MBTT J=E@>O PIM@<>C<=G@:
WBSJBCMF2 = OVMM;
0P>C PIM@<>C<=G@ J=E@>ON <M@ ADI<GDU@? =T OC@ B<M=<B@ >JGG@>OJM NJH@ODH@ DI OC@ APOPM@. #MJH
OC@ KJDIO JA QD@R JA OC@ KMJBM<H, OC@ J=E@>O ?J@N IJO @SDNO:
WBSJBCMF WBSJBCMF2
---+-------------------+--- ---+----+--- ---+----+---
] ] ]OVMM] ]OVMM]
---+-------------------+--- ---+----+--- ---+----+--
4@ C<? ?DN>PNN@? R<TN JA @HKOTDIB <I <NNJ>D<ODQ@ <MM<T DI OC@ @S@M>DN@N N@>ODJI JA OC@
ANNJ>D<ODQ@ AMM<TN >C<KO@M. 4@ IJR FIJR < AJPMOC H@OCJ?: ANNDBIDIB OVMM OJ <I <NNJ>D<ODQ@
<MM<T Q<MD<=G@ RDGG =M@<F OC@ M@G<ODJINCDK JA OC<O Q<MD<=G@ RDOC OC@ @G@H@ION:
TUSJOH<JOU> OBNFT;
// ...
OBNFT = OVMM; // /PU QSPWJEJOH BDDFTT UP BOZ FMFNFOU
):7 @G>> +3>G7 3@6 F:7 ;E %B7D3FAD
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
244
0DHDG<M OJ OC@ .Z$MBTT @S<HKG@N, DA OBNFT C<N =@@I OC@ G<NO M@A@M@I>@ OJ OC@ @G@H@ION JA OC@
<NNJ>D<ODQ@ <MM<T, OCJN@ @G@H@ION RJPG? =@ ADI<GDU@? =T OC@ B<M=<B@ >JGG@>OJM.
0GD>@N ><I =@ <NNDBI@? OVMM <N R@GG:
JOU<> TMJDF = PUIFS4MJDF< 10 .. 20 >;
// ...
TMJDF = OVMM; // /PU QSPWJEJOH BDDFTT UP BOZ FMFNFOU
Summary
47.5
t OVMM DN OC@ Q<GP@ DI?D><ODIB OC<O < Q<MD<=G@ ?J@N IJO KMJQD?@ <>>@NN OJ <IT Q<GP@
t /@A@M@I>@N OC<O C<Q@ OC@ OVMM Q<GP@ ><I JIGT =@ PN@? DI ORJ JK@M<ODJIN: <NNDBIDIB < Q<GP@
OJ OC@H <I? ?@O@MHDIDIB RC@OC@M OC@T <M@ OVMM JM IJO
t 0DI>@ OC@ == JK@M<OJM H<T C<Q@ OJ <>>@NN <I <>OP<G J=E@>O, ?@O@MHDIDIB RC@OC@M <
Q<MD<=G@ DN OVMM HPNO =@ K@MAJMH@? =T OC@ JT JK@M<OJM
t !JT DN OC@ JKKJNDO@ JA JT
t ANNDBIDIB OVMM OJ < Q<MD<=G@ H<F@N OC<O Q<MD<=G@ KMJQD?@ <>>@NN OJ IJOCDIB
t ,=E@>ON OC<O <M@ IJO M@A@M@I>@? =T <IT Q<MD<=G@ <M@ ADI<GDU@? =T OC@ B<M=<B@ >JGG@>OJM
):7 @G>> +3>G7 3@6 F:7 ;E %B7D3FAD
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
245
Type Conversions 48
3<MD<=G@N HPNO =@ >JHK<OD=G@ RDOC OC@ @SKM@NNDJIN OC<O OC@T O<F@ K<MON DI. AN DO C<N KMJ=<=GT
=@@I J=QDJPN =T OC@ KMJBM<HN OC<O R@ C<Q@ N@@I NJ A<M, ! DN < EF3F;53>>K FKB76 >3@9G397,
H@<IDIB OC<O OC@ >JHK<OD=DGDOT JA OTK@N DN Q<GD?<O@? <O >JHKDG@ ODH@.
AGG JA OC@ @SKM@NNDJIN OC<O R@ C<Q@ RMDOO@I NJ A<M <GR<TN C<? >JHK<OD=G@ OTK@N =@><PN@
JOC@MRDN@ OC@ >J?@ RJPG? =@ M@E@>O@? =T OC@ >JHKDG@M. 1C@ AJGGJRDIB DN <I @S<HKG@ JA < >J?@
OC<O C<N DI>JHK<OD=G@ OTK@N:
DIBS<> TMJDF;
XSJUFMO(TMJDF + 5); // j DPNQJMBUJPO &3303
1C@ >JHKDG@M M@E@>ON OC@ >J?@ ?P@ OJ OC@ DI>JHK<OD=G@ OTK@N DIBS<> <I? JOU AJM OC@ <??DODJI
JK@M<ODJI:
&SSPS: JODPNQBUJCMF UZQFT GPS ((TMJDF) + (5)): 'DIBS<>' BOE 'JOU'
1TK@ DI>JHK<OD=DGDOT ?J@N IJO H@<I OC<O OC@ OTK@N <M@ ?DAA@M@IO; ?DAA@M@IO OTK@N ><I DI?@@? =@
PN@? DI @SKM@NNDJIN N<A@GT. #JM @S<HKG@, <I JOU Q<MD<=G@ ><I N<A@GT =@ PN@? DI KG<>@ JA <
EPVCMF Q<GP@:
EPVCMF TVN = 1.25;
JOU JODSFNFOU = 3;
TVN += JODSFNFOU;
"Q@I OCJPBC TVN <I? JODSFNFOU <M@ JA ?DAA@M@IO OTK@N, OC@ >J?@ <=JQ@ DN Q<GD? =@><PN@
DI>M@H@IODIB < EPVCMF Q<MD<=G@ =T <I JOU Q<GP@ DN G@B<G.
Automatic type conversions
48.1
APOJH<OD> OTK@ >JIQ@MNDJIN <M@ <GNJ ><GG@? ;?B>;5;F FKB7 5A@H7DE;A@E.
AGOCJPBC EPVCMF <I? JOU <M@ >JHK<OD=G@ OTK@N DI OC@ @SKM@NNDJI <=JQ@, OC@ <??DODJI
JK@M<ODJI HPNO NODGG =@ @Q<GP<O@? <N < NK@>DAD> OTK@ <O OC@ HD>MJKMJ>@NNJM G@Q@G. AN TJP RJPG?
M@H@H=@M AMJH OC@ #GJ<ODIB -JDIO 1TK@N >C<KO@M, OC@ 64-=DO OTK@ EPVCMF DN I;67D (JM >3D97D)
OC<I OC@ 32-=DO OTK@ JOU. A??DODJI<GGT, <IT Q<GP@ OC<O ><I ADO <I JOU ><I ADO < EPVCMF.
4C@I OC@ >JHKDG@M @I>JPIO@MN <I @SKM@NNDJI OC<O DIQJGQ@N HDNH<O>C@? OTK@N, DO ADMNO >JIQ@MON
OC@ K<MON JA OC@ @SKM@NNDJIN OJ < >JHHJI OTK@ <I? OC@I @Q<GP<O@N OC@ JQ@M<GG @SKM@NNDJI. 1C@
<POJH<OD> >JIQ@MNDJIN OC<O <M@ K@MAJMH@? =T OC@ >JHKDG@M <M@ DI OC@ ?DM@>ODJI OC<O <QJD?N
?<O< GJNN. #JM @S<HKG@, EPVCMF ><I CJG? <IT Q<GP@ OC<O JOU ><I CJG? =PO OC@ JKKJNDO@ DN IJO
OMP@. 1C@ += JK@M<ODJI <=JQ@ ><I RJMF =@><PN@ <IT JOU Q<GP@ ><I N<A@GT =@ >JIQ@MO@? OJ
EPVCMF.
1C@ Q<GP@ OC<O C<N =@@I B@I@M<O@? <POJH<OD><GGT <N < M@NPGO JA < >JIQ@MNDJI DN <GR<TN <I
<IJITHJPN (<I? JAO@I O@HKJM<MT) Q<MD<=G@. 1C@ JMDBDI<G Q<GP@ ?J@N IJO >C<IB@. #JM @S<HKG@,
OC@ <POJH<OD> >JIQ@MNDJI ?PMDIB += <=JQ@ ?J@N IJO >C<IB@ OC@ OTK@ JA JODSFNFOU; DO DN
<GR<TN <I JOU. /<OC@M, < O@HKJM<MT Q<GP@ JA OTK@ EPVCMF DN >JINOMP>O@? =T OC@ Q<GP@ JA
JODSFNFOU. 1C@ >JIQ@MNDJI OC<O O<F@N KG<>@ DI OC@ =<>FBMJPI? DN @LPDQ<G@IO OJ OC@ AJGGJRDIB
>J?@:
\
EPVCMF an_anonymous_double_value = JODSFNFOU;
TVN += an_anonymous_double_value;
^
)KB7 CA@H7DE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
246
1C@ >JHKDG@M >JIQ@MON OC@ JOU Q<GP@ OJ < O@HKJM<MT EPVCMF Q<GP@ <I? PN@N OC<O Q<GP@ DI OC@
JK@M<ODJI. &I OCDN @S<HKG@, OC@ O@HKJM<MT Q<MD<=G@ GDQ@N JIGT ?PMDIB OC@ += JK@M<ODJI.
APOJH<OD> >JIQ@MNDJIN <M@ IJO GDHDO@? OJ <MDOCH@OD> JK@M<ODJIN. 1C@M@ <M@ JOC@M ><N@N RC@M@
OTK@N <M@ >JIQ@MO@? OJ JOC@M OTK@N <POJH<OD><GGT. AN GJIB <N OC@ >JIQ@MNDJIN <M@ Q<GD?, OC@
>JHKDG@M O<F@N <?Q<IO<B@ JA OTK@ >JIQ@MNDJIN OJ =@ <=G@ OJ PN@ Q<GP@N DI @SKM@NNDJIN. #JM
@S<HKG@, < CZUF Q<GP@ ><I =@ K<NN@? AJM <I JOU K<M<H@O@M:
WPJE GVOD(JOU OVNCFS)
\
// ...
^
WPJE NBJO()
\
CZUF TNBMM7BMVF = 7;
GVOD(TNBMM7BMVF); // BVUPNBUJD UZQF DPOWFSTJPO
^
&I OC@ >J?@ <=JQ@, ADMNO < O@HKJM<MT JOU Q<GP@ DN >JINOMP>O@? <I? OC@ API>ODJI DN ><GG@? RDOC
OC<O Q<GP@.
Integer promotions 48.1.1
3<GP@N JA OTK@N OC<O <M@ JI OC@ G@AO-C<I? ND?@ JA OC@ AJGGJRDIB O<=G@ I@Q@M O<F@ K<MO DI
<MDOCH@OD> @SKM@NNDJIN <N OC@DM <>OP<G OTK@N. "<>C OTK@ DN ADMNO KMJHJO@? OJ OC@ OTK@ OC<O DN JI
OC@ MDBCO-C<I? ND?@ JA OC@ O<=G@.
'SPN 5P
CPPM JOU
CZUF JOU
VCZUF JOU
TIPSU JOU
VTIPSU JOU
DIBS JOU
XDIBS JOU
EDIBS VJOU
&IO@B@M KMJHJODJIN <M@ <KKGD@? OJ FOVN Q<GP@N <N R@GG.
1C@ M@<NJIN AJM DIO@B@M KMJHJODJIN <M@ =JOC CDNOJMD><G (RC@M@ OC@ MPG@N >JH@ AMJH ) <I? OC@
A<>O OC<O OC@ I<OPM<G <MDOCH@OD> OTK@ AJM OC@ HD>MJKMJ>@NNJM DN JOU. #JM @S<HKG@, <GOCJPBC OC@
AJGGJRDIB ORJ Q<MD<=G@N <M@ =JOC VCZUF, OC@ <??DODJI JK@M<ODJI DN K@MAJMH@? JIGT <AO@M =JOC JA
OC@ Q<GP@N <M@ DI?DQD?P<GGT KMJHJO@? OJ JOU:
VCZUF B = 1;
VCZUF C = 2;
XSJUFMO(UZQFPG(B + C).TUSJOHPG); // UIF BEEJUJPO JT OPU JO VCZUF
1C@ JPOKPO:
JOU
+JO@ OC<O OC@ OTK@N JA OC@ Q<MD<=G@N B <I? C ?J IJO >C<IB@; JIGT OC@DM Q<GP@N <M@ O@HKJM<MDGT
KMJHJO@? OJ JOU AJM OC@ ?PM<ODJI JA OC@ <??DODJI JK@M<ODJI.
)KB7 CA@H7DE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
247
Arithmetic conversions 48.1.2
1C@M@ <M@ JOC@M >JIQ@MNDJI MPG@N OC<O <M@ <KKGD@? AJM <MDOCH@OD> JK@M<ODJIN. &I B@I@M<G,
<POJH<OD> <MDOCH@OD> >JIQ@MNDJIN <M@ <KKGD@? DI OC@ N<A@ ?DM@>ODJI: AMJH OC@ @3DDAI OTK@ OJ
OC@ I;67D OTK@. AGOCJPBC OCDN MPG@ DN @<NT OJ M@H@H=@M <I? DN >JMM@>O DI HJNO ><N@N,
<POJH<OD> >JIQ@MNDJI MPG@N <M@ Q@MT >JHKGD><O@? <I? DI OC@ ><N@ JA NDBI@?-OJ-PINDBI@?
>JIQ@MNDJIN, ><MMT NJH@ MDNF JA =PBN.
1C@ <MDOCH@OD> >JIQ@MNDJI MPG@N <M@ OC@ AJGGJRDIB:
1. &A JI@ JA OC@ Q<GP@N DN SFBM, OC@I OC@ JOC@M Q<GP@ DN >JIQ@MO@? OJ SFBM
2. "GN@, DA JI@ JA OC@ Q<GP@N DN EPVCMF, OC@I OC@ JOC@M Q<GP@ DN >JIQ@MO@? OJ EPVCMF
3. "GN@, DA JI@ JA OC@ Q<GP@N DN GMPBU, OC@I OC@ JOC@M Q<GP@ DN >JIQ@MO@? OJ GMPBU
4. "GN@, ADMNO ;@F797D BDA?AF;A@E <M@ <KKGD@? <>>JM?DIB OJ OC@ O<=G@ <=JQ@, <I? OC@I OC@
AJGGJRDIB MPG@N <M@ AJGGJR@?:
1. &A =JOC OTK@N <M@ OC@ N<H@, OC@I IJ HJM@ NO@KN I@@?@?
2. &A =JOC OTK@N <M@ NDBI@? JM =JOC OTK@N <M@ PINDBI@?, OC@I OC@ I<MMJR@M Q<GP@ DN
>JIQ@MO@? OJ OC@ RD?@M OTK@
3. &A OC@ NDBI@? OTK@ DN RD?@M OC<I OC@ PINDBI@? OTK@, OC@I OC@ PINDBI@? Q<GP@ DN
>JIQ@MO@? OJ OC@ NDBI@? OTK@
4. ,OC@MRDN@ OC@ NDBI@? OTK@ DN >JIQ@MO@? OJ OC@ PINDBI@? OTK@
2IAJMOPI<O@GT, OC@ G<NO MPG@ <=JQ@ ><I ><PN@ NP=OG@ =PBN:
JOU B = 0;
JOU C = 1;
TJ[F@U D = 0;
XSJUFMO(B - C + D); // 4VSQSJTJOH SFTVMU!
0PMKMDNDIBGT, OC@ JPOKPO DN IJO -1, =PO TJ[F@U.NBY:
18446744073709551615
AGOCJPBC JI@ RJPG? @SK@>O (0 - 1 + 0) OJ =@ ><G>PG<O@? <N -1, <>>JM?DIB OJ OC@ MPG@N <=JQ@,
OC@ OTK@ JA OC@ @IODM@ @SKM@NNDJI DN TJ[F@U, IJO JOU; <I? NDI>@ TJ[F@U ><IIJO CJG? I@B<ODQ@
Q<GP@N, OC@ M@NPGO PI?@MAGJRN <I? =@>JH@N TJ[F@U.NBY.
DPOTU conversions 48.1.3
AN R@ C<Q@ N@@I @<MGD@M DI OC@ #PI>ODJI -<M<H@O@MN >C<KO@M, M@A@M@I>@ OTK@N ><I
<POJH<OD><GGT =@ >JIQ@MO@? OJ OC@ DPOTU JA OC@ N<H@ OTK@. JIQ@MNDJI OJ DPOTU DN N<A@
=@><PN@ OC@ RD?OC JA OC@ OTK@ ?J@N IJO >C<IB@ <I? DPOTU DN < KMJHDN@ OJ IJO HJ?DAT OC@
Q<MD<=G@:
EDIBS<> QBSFOUIFTJ[FE(DPOTU EDIBS<> UFYU)
\
SFUVSO "\" _ UFYU _ "^";
^
WPJE NBJO()
\
EDIBS<> HSFFUJOH;
HSFFUJOH _= "IFMMP XPSME";
QBSFOUIFTJ[FE(HSFFUJOH);
^
1C@ HPO<=G@ HSFFUJOH <=JQ@ DN <POJH<OD><GGT >JIQ@MO@? OJ < DPOTU EDIBS<> <N DO DN K<NN@?
OJ QBSFOUIFTJ[FE().
)KB7 CA@H7DE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
248
AN R@ C<Q@ <GNJ N@@I @<MGD@M, OC@ JKKJNDO@ >JIQ@MNDJI DN IJO <POJH<OD>. A DPOTU M@A@M@I>@ DN
IJO <POJH<OD><GGT >JIQ@MO@? OJ < HPO<=G@ M@A@M@I>@:
EDIBS<> QBSFOUIFTJ[FE(DPOTU EDIBS<> UFYU)
\
EDIBS<> BSHVNFOU = UFYU; // j DPNQJMBUJPO &3303
// ...
^
+JO@ OC<O OCDN OJKD> DN JIGT <=JPO M@A@M@I>@N; NDI>@ Q<MD<=G@N JA Q<GP@ OTK@N <M@ >JKD@?, DO DN
IJO KJNND=G@ OJ <AA@>O OC@ JMDBDI<G OCMJPBC OC@ >JKT <ITR<T:
DPOTU JOU UPUBM$PSOFST = 4;
JOU UIF$PQZ = UPUBM$PSOFST; // DPNQJMFT (WBMVF UZQF)
1C@ >JIQ@MNDJI AMJH DPOTU OJ HPO<=G@ <=JQ@ DN G@B<G =@><PN@ OC@ >JKT DN IJO < M@A@M@I>@ OJ
OC@ JMDBDI<G.
JNNVUBCMF conversions 48.1.4
B@><PN@ JNNVUBCMF NK@>DAD@N OC<O < Q<MD<=G@ ><I I@Q@M >C<IB@, I@DOC@M >JIQ@MNDJI AMJH
JNNVUBCMF IJM OJ JNNVUBCMF <M@ <POJH<OD>:
TUSJOH B = "IFMMP"; // JNNVUBCMF DIBSBDUFST
DIBS<> C = B; // j DPNQJMBUJPO &3303
TUSJOH D = C; // j DPNQJMBUJPO &3303
AN RDOC DPOTU >JIQ@MNDJIN <=JQ@, OCDN OJKD> DN <GNJ JIGT <=JPO M@A@M@I>@ OTK@N. 0DI>@
Q<MD<=G@N JA Q<GP@ OTK@N <M@ >JKD@? <ITR<T, >JIQ@MNDJIN OJ <I? AMJH JNNVUBCMF <M@ Q<GD?:
JNNVUBCMF B = 10;
JOU C = B; // DPNQJMFT (WBMVF UZQF)
FOVN conversions 48.1.5
AN R@ C<Q@ N@@I DI OC@ FOVN >C<KO@M, FOVN DN AJM ?@ADIDIB @3?76 5A@EF3@FE:
FOVN 4VJU \ TQBEFT, IFBSUT, EJBNPOET, DMVCT ^
/@H@H=@M OC<O NDI>@ IJ Q<GP@N <M@ NK@>DAD@? @SKGD>DOGT <=JQ@, OC@ Q<GP@N JA OC@ FOVN
H@H=@MN NO<MO RDOC U@MJ <I? <M@ <POJH<OD><GGT DI>M@H@IO@? =T JI@. A>>JM?DIBGT, OC@ Q<GP@ JA
4VJU.DMVCT DN 3.
FOVN Q<GP@N <M@ <OJH<OD><GGT >JIQ@MO@? OJ DIO@BM<G OTK@N. #JM @S<HKG@, OC@ Q<GP@ JA
4VJU.IFBSUT DN O<F@I OJ =@ 1 DI OC@ AJGGJRDIB ><G>PG<ODJI <I? OC@ M@NPGO =@>JH@N 11:
JOU SFTVMU = 10 + 4VJU.IFBSUT;
BTTFSU(SFTVMU == 11);
1C@ JKKJNDO@ >JIQ@MNDJI DN IJO <POJH<OD>: &IO@B@M Q<GP@N <M@ IJO <POJH<OD><GGT >JIQ@MO@? OJ
>JMM@NKJI?DIB FOVN Q<GP@N. #JM @S<HKG@, OC@ TVJU Q<MD<=G@ =@GJR HDBCO =@ @SK@>O@? OJ
=@>JH@ 4VJU.EJBNPOET, =PO OC@ >J?@ ><IIJO =@ >JHKDG@?:
4VJU TVJU = 2; // j DPNQJMBUJPO &3303
AN R@ RDGG N@@ =@GJR, >JIQ@MNDJIN AMJH DIO@B@MN OJ FOVN Q<GP@N <M@ NODGG KJNND=G@ =PO OC@T
HPNO =@ @SKGD>DO.
)KB7 CA@H7DE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
249
CPPM conversions 48.1.6
GBMTF <I? USVF <M@ <POJH<OD><GGT >JIQ@MO@? OJ 0 <I? 1, M@NK@>ODQ@GT:
JOU B = GBMTF;
BTTFSU(B == 0);
JOU C = USVF;
BTTFSU(C == 1);
1C@ JKKJNDO@ >JIQ@MNDJI DN <GNJ <POJH<OD> =PO JIGT AJM ORJ NK@>D<G Q<GP@N. 1C@ GDO@M<G Q<GP@N 0
<I? 1 <M@ >JIQ@MO@? <POJH<OD><GGT OJ GBMTF <I? USVF, M@NK@>ODQ@GT:
CPPM B = 0;
BTTFSU(!B); // GBMTF
CPPM C = 1;
BTTFSU(C); // USVF
,OC@M GDO@M<G Q<GP@N ><IIJO =@ >JIQ@MO@? OJ CPPM <POJH<OD><GGT:
CPPM C = 2; // j DPNQJMBUJPO &3303
Automatic conversions to CPPM in conditional statements 48.1.7
0JH@ NO<O@H@ION H<F@ PN@ JA GJBD><G @SKM@NNDJIN: JG, XIJMF, @O>. #JM OC@ GJBD><G @SKM@NNDJIN
JA NP>C NO<O@H@ION, IJO JIGT CPPM =PO HJNO JOC@M OTK@N ><I =@ PN@? <N R@GG. 1C@ Q<GP@ U@MJ DN
<POJH<OD><GGT >JIQ@MO@? OJ GBMTF <I? OC@ IJI-U@MJ Q<GP@N <M@ <POJH<OD><GGT >JIQ@MO@? OJ
USVF.
JOU J;
// ...
JG (J) \ // j JOU WBMVF JT CFJOH VTFE BT B MPHJDBM FYQSFTTJPO
// ... 'J' JT OPU [FSP
^ FMTF \
// ... 'J' JT [FSP
^
0DHDG<MGT, OVMM M@A@M@I>@N <M@ <POJH<OD><GGT >JIQ@MO@? OJ GBMTF <I? IJI-OVMM M@A@M@I>@N <M@
<POJH<OD><GGT >JIQ@MO@? OJ USVF. 1CDN H<F@N DO @<NT OJ @INPM@ OC<O < M@A@M@I>@ DN IJI-OVMM
=@AJM@ <>OP<GGT PNDIB DO:
JOU<> B;
// ...
JG (B) \ // j BVUPNBUJD CPPM DPOWFSTJPO
// ... OPU OVMM; 'B' DBO CF VTFE ...
^ FMTF \
// ... OVMM; 'B' DBOOPU CF VTFE ...
^
Explicit type conversions
48.2
AN R@ C<Q@ N@@I <=JQ@, OC@M@ <M@ ><N@N RC@M@ <POJH<OD> >JIQ@MNDJIN <M@ IJO <Q<DG<=G@:
t JIQ@MNDJIN AMJH RD?@M OTK@N OJ I<MMJR@M OTK@N
t JIQ@MNDJIN AMJH DPOTU OJ HPO<=G@
)KB7 CA@H7DE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
250
t JNNVUBCMF >JIQ@MNDJIN
t JIQ@MNDJIN AMJH DIO@B@MN OJ FOVN Q<GP@N
t @O>.
&A NP>C < >JIQ@MNDJI DN FIJRI OJ =@ N<A@, OC@ KMJBM<HH@M ><I @SKGD>DOGT <NF AJM < OTK@
>JIQ@MNDJI =T JI@ JA OCM@@ H@OCJ?N:
t BT ><GGDIB OC@ TUE.DPOW.UP API>ODJI
t BT ><GGDIB OC@ TUE.FYDFQUJPO.BTTVNF6OJRVF API>ODJI
t BT OC@ DBTU JK@M<OJM
UP() for most conversions 48.2.1
1C@ UP() API>ODJI, RCD>C R@ C<Q@ <GM@<?T PN@? HJNOGT OJ >JIQ@MO Q<GP@N OJ TUSJOH, ><I
<>OP<GGT =@ PN@? AJM H<IT JOC@M OTK@N. &ON >JHKG@O@ NTIO<S DN OC@ AJGGJRDIB:
UP!(DestinationType)(WBMVF)
B@DIB < O@HKG<O@, UP() ><I O<F@ <?Q<IO<B@ JA OC@ NCJMO>PO O@HKG<O@ K<M<H@O@M IJO<ODJI:
4C@I OC@ ?@NODI<ODJI OTK@ >JINDNON JIGT JA < NDIBG@ OJF@I (B@I@M<GGT, 3 E;@9>7 IAD6), DO ><I =@
><GG@? RDOCJPO OC@ ADMNO K<DM JA K<M@IOC@N@N:
UP!DestinationType(WBMVF)
1C@ AJGGJRDIB KMJBM<H DN OMTDIB OJ >JIQ@MO < EPVCMF Q<GP@ OJ TIPSU <I? < TUSJOH Q<GP@ OJ
JOU:
WPJE NBJO()
\
EPVCMF E = -1.75;
TIPSU T = E; // j DPNQJMBUJPO &3303
JOU J = "42"; // j DPNQJMBUJPO &3303
^
0DI>@ IJO @Q@MT EPVCMF Q<GP@ ><I =@ M@KM@N@IO@? <N < TIPSU <I? IJO @Q@MT TUSJOH ><I =@
M@KM@N@IO@? <N <I JOU, OCJN@ >JIQ@MNDJIN <M@ IJO <POJH<OD>. 4C@I DO DN FIJRI =T OC@
KMJBM<HH@M OC<O OC@ >JIQ@MNDJIN <M@ DI A<>O N<A@ JM OC<O OC@ KJO@IOD<G >JIN@LP@I>@N <M@
<>>@KO<=G@, OC@I OC@ OTK@N ><I =@ >JIQ@MO@? =T UP():
JNQPSU TUE.DPOW;
WPJE NBJO()
\
EPVCMF E = -1.75;
TIPSU T = UP!TIPSU(E);
BTTFSU(T == -1);
JOU J = UP!JOU("42");
BTTFSU(J == 42);
^
+JO@ OC<O =@><PN@ TIPSU ><IIJO ><MMT AM<>ODJI<G Q<GP@N, OC@ >JIQ@MO@? Q<GP@ DN -1.
UP() DN N<A@: &O OCMJRN <I @S>@KODJI RC@I < >JIQ@MNDJI DN IJO KJNND=G@.
BTTVNF6OJRVF() for fast JNNVUBCMF conversions 48.2.2
UP() ><I K@MAJMH JNNVUBCMF >JIQ@MNDJIN <N R@GG:
)KB7 CA@H7DE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
251
JOU<> TMJDF = < 10, 20, 30 >;
BVUP JNNVUBCMF4MJDF = UP!(JNNVUBCMF JOU<>)(TMJDF);
&I JM?@M OJ BP<M<IO@@ OC<O OC@ @G@H@ION JA JNNVUBCMF4MJDF RDGG I@Q@M >C<IB@, DO ><IIJO
NC<M@ OC@ N<H@ @G@H@ION RDOC TMJDF. #JM OC<O M@<NJI, UP() >M@<O@N <I <??DODJI<G NGD>@ RDOC
JNNVUBCMF @G@H@ION <=JQ@. ,OC@MRDN@, HJ?DAD><ODJIN OJ OC@ @G@H@ION JA TMJDF RJPG? ><PN@
OC@ @G@H@ION JA JNNVUBCMF4MJDF >C<IB@ <N R@GG. 1CDN =@C<QDJM DN OC@ N<H@ RDOC OC@ .JEVQ
KMJK@MOT JA <MM<TN.
4@ ><I N@@ OC<O OC@ @G@H@ION JA JNNVUBCMF4MJDF <M@ DI?@@? >JKD@N JA OC@ @G@H@ION JA TMJDF
=T GJJFDIB <O OC@ <??M@NN@N JA OC@DM ADMNO @G@H@ION:
BTTFSU(&(TMJDF<0>) != &(JNNVUBCMF4MJDF<0>));
0JH@ODH@N OCDN >JKT DN PII@>@NN<MT <I? H<T NGJR OC@ NK@@? JA OC@ KMJBM<H IJOD>@<=GT DI
>@MO<DI ><N@N. AN <I @S<HKG@ JA OCDN, G@O'N GJJF <O OC@ AJGGJRDIB API>ODJI OC<O O<F@N <I
JNNVUBCMF NGD>@:
WPJE DBMDVMBUF(JNNVUBCMF JOU<> DPPSEJOBUFT)
\
// ...
^
WPJE NBJO()
\
JOU<> OVNCFST;
OVNCFST _= 10;
// ... WBSJPVT PUIFS NPEJGJDBUJPOT ...
OVNCFST<0> = 42;
DBMDVMBUF(OVNCFST); // j DPNQJMBUJPO &3303
^
1C@ KMJBM<H <=JQ@ ><IIJO =@ >JHKDG@? =@><PN@ OC@ ><GG@M DN IJO K<NNDIB <I JNNVUBCMF
<MBPH@IO OJ DBMDVMBUF(). AN R@ C<Q@ N@@I <=JQ@, <I JNNVUBCMF NGD>@ ><I =@ >M@<O@? =T
UP():
JNQPSU TUE.DPOW;
// ...
BVUP JNNVUBCMF/VNCFST = UP!(JNNVUBCMF JOU<>)(OVNCFST);
DBMDVMBUF(JNNVUBCMF/VNCFST); // j OPX DPNQJMFT
%JR@Q@M, DA OVNCFST DN I@@?@? JIGT OJ KMJ?P>@ OCDN <MBPH@IO <I? RDGG I@Q@M =@ PN@? <AO@M OC@
API>ODJI DN ><GG@?, >JKTDIB DON @G@H@ION OJ JNNVUBCMF/VNCFST RJPG? =@ PII@>@NN<MT.
BTTVNF6OJRVF() H<F@N OC@ @G@H@ION JA < NGD>@ JNNVUBCMF RDOCJPO >JKTDIB:
JNQPSU TUE.FYDFQUJPO;
// ...
BVUP JNNVUBCMF/VNCFST = BTTVNF6OJRVF(OVNCFST);
DBMDVMBUF(JNNVUBCMF/VNCFST);
BTTFSU(OVNCFST JT OVMM); // UIF PSJHJOBM TMJDF CFDPNFT OVMM
BTTVNF6OJRVF() M@OPMIN < I@R NGD>@ OC<O KMJQD?@N JNNVUBCMF <>>@NN OJ OC@ @SDNODIB
@G@H@ION. &O <GNJ H<F@N OC@ JMDBDI<G NGD>@ OVMM OJ KM@Q@IO OC@ @G@H@ION AMJH <>>D?@IO<GGT
=@DIB HJ?DAD@? OCMJPBC DO.
)KB7 CA@H7DE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
252
The DBTU operator 48.2.3
BJOC UP() <I? BTTVNF6OJRVF() H<F@ PN@ JA OC@ >JIQ@MNDJI JK@M<OJM DBTU, RCD>C DN <Q<DG<=G@
OJ OC@ KMJBM<HH@M <N R@GG.
1C@ DBTU JK@M<OJM O<F@N OC@ ?@NODI<ODJI OTK@ DI K<M@IOC@N@N:
DBTU(DestinationType)WBMVF
DBTU DN KJR@MAPG @Q@I AJM >JIQ@MNDJIN OC<O UP() ><IIJO N<A@GT K@MAJMH. UP() ?J@N IJO
K@MAJMH OC@ AJGGJRDIB >JIQ@MNDJIN:
4VJU TVJU = UP!4VJU(2); // j DPNQJMBUJPO &3303
CPPM C = UP!CPPM(2); // j DPNQJMBUJPO &3303
0JH@ODH@N JIGT OC@ KMJBM<HH@M ><I FIJR RC@OC@M <I DIO@B@M Q<GP@ >JMM@NKJI?N OJ < Q<GD?
FOVN Q<GP@ JM OC<O DO H<F@N N@IN@ OJ OM@<O <I DIO@B@M Q<GP@ <N < CPPM. 1C@ DBTU JK@M<OJM ><I
=@ PN@? RC@I OC@ >JIQ@MNDJI DN FIJRI OJ =@ >JMM@>O <>>JM?DIB OC@ KMJBM<H'N GJBD>:
4VJU TVJU = DBTU(4VJU)2;
BTTFSU(TVJU == 4VJU.EJBNPOET);
CPPM C = DBTU(CPPM)2;
BTTFSU(C);
Summary
48.3
t APOJH<OD> OTK@ >JIQ@MNDJIN <M@ HJNOGT DI OC@ N<A@ ?DM@>ODJI: #MJH OC@ I<MMJR@M OTK@ OJ
OC@ RD?@M OTK@ <I? AMJH HPO<=G@ OJ DPOTU.
t %JR@Q@M, >JIQ@MNDJIN OJ PINDBI@? OTK@N H<T C<Q@ NPMKMDNDIB @AA@>ON =@><PN@ PINDBI@?
OTK@N ><IIJO C<Q@ I@B<ODQ@ Q<GP@N.
t FOVN OTK@N ><I <POJH<OD><GGT =@ >JIQ@MO@? OJ DIO@B@M Q<GP@N =PO OC@ JKKJNDO@ >JIQ@MNDJI
DN IJO <POJH<OD>.
t GBMTF <I? USVF <M@ <POJH<OD><GGT >JIQ@MO@? OJ 0 <I? 1 M@NK@>ODQ@GT. 0DHDG<MGT, U@MJ Q<GP@N
<M@ <POJH<OD><GGT >JIQ@MO@? OJ GBMTF <I? IJI-U@MJ Q<GP@N <M@ <POJH<OD><GGT >JIQ@MO@? OJ
USVF.
t OVMM M@A@M@I>@N <M@ <POJH<OD><GGT >JIQ@MO@? OJ GBMTF <I? IJI-OVMM M@A@M@I>@N <M@
<POJH<OD><GGT >JIQ@MO@? OJ USVF.
t UP() >JQ@MN HJNO JA OC@ @SKGD>DO >JIQ@MNDJIN.
t BTTVNF6OJRVF() >JIQ@MON OJ JNNVUBCMF RDOCJPO >JKTDIB.
t 1C@ DBTU JK@M<OJM DN OC@ HJNO KJR@MAPG >JIQ@MNDJI OJJG.
)KB7 CA@H7DE;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
253
Structs 49
AN C<N =@@I H@IODJI@? N@Q@M<G ODH@N @<MGD@M DI OC@ =JJF, API?<H@IO<G OTK@N <M@ IJO NPDO<=G@
OJ M@KM@N@IO CDBC@M-G@Q@G >JI>@KON. #JM @S<HKG@, <GOCJPBC < Q<GP@ JA OTK@ JOU DN NPDO<=G@ OJ
M@KM@N@IO OC@ CJPM JA ?<T, ORJ JOU Q<MD<=G@N RJPG? =@ HJM@ NPDO<=G@ OJB@OC@M OJ M@KM@N@IO <
KJDIO DI ODH@: JI@ AJM OC@ CJPM <I? OC@ JOC@M AJM OC@ HDIPO@.
0OMP>ON <M@ OC@ A@<OPM@ OC<O <GGJR ?@ADIDIB I@R OTK@N =T >JH=DIDIB <GM@<?T @SDNODIB JOC@M
OTK@N. 1C@ I@R OTK@ DN ?@ADI@? =T OC@ TUSVDU F@TRJM?. *JNO JA OC@ >JIO@IO JA OCDN >C<KO@M DN
?DM@>OGT <KKGD><=G@ OJ >G<NN@N <N R@GG. "NK@>D<GGT OC@ >JI>@KO JA 5A?4;@;@9 7J;EF;@9 FKB7E FA
678;@7 3 @7I FKB7 DN @S<>OGT OC@ N<H@ AJM OC@H.
1J PI?@MNO<I? CJR PN@APG NOMP>ON <M@, G@O'N O<F@ < GJJF <O OC@ BEE%VSBUJPO() API>ODJI OC<O R@
C<? ?@ADI@? @<MGD@M DI OC@ BTTFSU <I? FOGPSDF >C<KO@M. 1C@ AJGGJRDIB ?@ADIDODJI DN AMJH OC@
@S@M>DN@ NJGPODJI JA OC<O >C<KO@M:
WPJE BEE%VSBUJPO(JO JOU TUBSU)PVS, JO JOU TUBSU.JOVUF,
JO JOU EVSBUJPO)PVS, JO JOU EVSBUJPO.JOVUF,
PVU JOU SFTVMU)PVS, PVU JOU SFTVMU.JOVUF)
\
SFTVMU)PVS = TUBSU)PVS + EVSBUJPO)PVS;
SFTVMU.JOVUF = TUBSU.JOVUF + EVSBUJPO.JOVUF;
SFTVMU)PVS += SFTVMU.JOVUF / 60;
SFTVMU.JOVUF %= 60;
SFTVMU)PVS %= 24;
^
Note: ! I;>> ;9@AD7 F:7 in, out, 3@6 unittest 4>A5=E ;@ F:;E 5:3BF7D FA =77B F:7 5A67 E3?B>7E
E:ADF.
AGOCJPBC OC@ API>ODJI <=JQ@ >G@<MGT O<F@N NDS K<M<H@O@MN, RC@I OC@ OCM@@ K<DMN JA K<M<H@O@MN
<M@ >JIND?@M@?, DO DN >JI>@KOP<GGT O<FDIB JIGT OCM@@ =DON JA DIAJMH<ODJI AJM OC@ NO<MODIB ODH@,
OC@ ?PM<ODJI, <I? OC@ M@NPGO.
Definition
49.1
1C@ TUSVDU F@TRJM? ?@ADI@N < I@R OTK@ =T >JH=DIDIB Q<MD<=G@N OC<O <M@ M@G<O@? DI NJH@ R<T:
TUSVDU 5JNF0G%BZ
\
JOU IPVS;
JOU NJOVUF;
^
1C@ >J?@ <=JQ@ ?@ADI@N < I@R OTK@ I<H@? 5JNF0G%BZ, RCD>C >JINDNON JA ORJ Q<MD<=G@N I<H@?
IPVS <I? NJOVUF. 1C<O ?@ADIDODJI <GGJRN OC@ I@R 5JNF0G%BZ OTK@ OJ =@ PN@? DI OC@ KMJBM<H
EPNO GDF@ <IT JOC@M OTK@. 1C@ AJGGJRDIB >J?@ ?@HJINOM<O@N CJR NDHDG<M DON PN@ DN OJ <I JOU'N:
JOU OVNCFS; // B WBSJBCMF
OVNCFS = PUIFS/VNCFS; // UBLJOH UIF WBMVF PG PUIFS/VNCFS
5JNF0G%BZ UJNF; // BO PCKFDU
UJNF = PUIFS5JNF; // UBLJOH UIF WBMVF PG PUIFS5JNF
1C@ NTIO<S JA TUSVDU ?@ADIDODJI DN OC@ AJGGJRDIB:
TUSVDU TypeName
\
(FDG5FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
254
// ... NFNCFS WBSJBCMFT BOE GVODUJPOT ...
^
4@ RDGG N@@ H@H=@M API>ODJIN DI G<O@M >C<KO@MN.
1C@ Q<MD<=G@N OC<O < NOMP>O >JH=DI@N <M@ ><GG@? DON ?7?47DE. A>>JM?DIB OJ OCDN ?@ADIDODJI,
5JNF0G%BZ C<N ORJ H@H=@MN: IPVS <I? NJOVUF.
TUSVDU defines a type, not a variable 49.1.1
1C@M@ DN <I DHKJMO<IO ?DNODI>ODJI C@M@: "NK@>D<GGT <AO@M OC@ +<H@ 0K<>@ <I? )DA@ODH@N <I?
#PI?<H@IO<G ,K@M<ODJIN >C<KO@MN, OC@ >PMGT =M<>F@ON JA TUSVDU ?@ADIDODJIN H<T BDQ@ OC@
RMJIB DHKM@NNDJI OC<O OC@ NOMP>O H@H=@MN NO<MO <I? @I? OC@DM GDQ@N DIND?@ OC<O N>JK@. 1CDN DN
IJO OMP@.
*@H=@M ?@ADIDODJIN <M@ IJO Q<MD<=G@ ?@ADIDODJIN:
TUSVDU 5JNF0G%BZ
\
JOU IPVS; // j OPU B WBSJBCMF EFGJOJUJPO!
JOU NJOVUF; // j OPU B WBSJBCMF EFGJOJUJPO!
^
1C@ ?@ADIDODJI JA < TUSVDU ?@O@MHDI@N OC@ OTK@N <I? OC@ I<H@N JA OC@ H@H=@MN OC<O OC@
J=E@>ON JA OC<O TUSVDU RDGG C<Q@:
TUSVDU 5JNF0G%BZ
\
JOU IPVS; // j XJMM CFDPNF B QBSU PG B WBSJBCMF
JOU NJOVUF; // j XJMM CFDPNF B QBSU PG B WBSJBCMF
^
1CJN@ H@H=@M Q<MD<=G@N RDGG =@ >JINOMP>O@? <N K<MON JA 5JNF0G%BZ J=E@>ON OC<O O<F@ K<MO DI
OC@ KMJBM<H:
5JNF0G%BZ CFE5JNF; // 5IJT PCKFDU DPOUBJOT JUT PXO IPVS
// BOE NJOVUF NFNCFS WBSJBCMFT.
5JNF0G%BZ XBLF6Q5JNF; // 5IJT PCKFDU DPOUBJOT JUT PXO IPVS
// BOE NJOVUF NFNCFS WBSJBCMFT BT
// XFMM. 5IF NFNCFS WBSJBCMFT PG
// UIJT PCKFDU BSF OPU SFMBUFE UP
// UIF NFNCFS WBSJBCMFT PG UIF
// QSFWJPVT PCKFDU.
Coding convenience 49.1.2
B@DIB <=G@ OJ >JH=DI@ OC@ >JI>@KON JA CJPM <I? HDIPO@ OJB@OC@M <N < I@R OTK@ DN < BM@<O
>JIQ@ID@I>@. #JM @S<HKG@, OC@ API>ODJI <=JQ@ ><I =@ M@RMDOO@I DI < HJM@ H@<IDIBAPG R<T =T
O<FDIB OCM@@ 5JNF0G%BZ K<M<H@O@MN DINO@<? JA OC@ @SDNODIB NDS JOU K<M<H@O@MN:
WPJE BEE%VSBUJPO(JO 5JNF0G%BZ TUBSU,
JO 5JNF0G%BZ EVSBUJPO,
PVU 5JNF0G%BZ SFTVMU)
\
// ...
^
(FDG5FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
255
Note: !F ;E @AF @AD?3> FA 366 FIA H3D;34>7E F:3F D7BD7E7@F FIA BA;@FE ;@ F;?7. FAD 7J3?B>7, ;F
;E ?73@;@9>7EE FA 366 F:7 >G@5: F;?7 12:00 FA F:7 4D73=83EF F;?7 7:30. !F IAG>6 ?3=7 ?AD7
E7@E7 FA 678;@7 3@AF:7D FKB7, 3BBDABD;3F7>K 53>>76 Duration, 3@6 FA 366 A4<75FE A8 F:3F FKB7
FA TimeOfDay A4<75FE. D7EB;F7 F:;E 67E;9@ 7DDAD, ! I;>> 5A@F;@G7 GE;@9 A@>K TimeOfDay A4<75FE
;@ F:;E 5:3BF7D 3@6 ;@FDA6G57 Duration ;@ 3 >3F7D 5:3BF7D.
AN TJP M@H@H=@M, API>ODJIN M@OPMI PK-OJ < NDIBG@ Q<GP@. 1C<O C<N KM@>DN@GT =@@I OC@ M@<NJI
RCT OC@ @<MGD@M ?@ADIDODJI JA BEE%VSBUJPO() R<N O<FDIB ORJ PVU K<M<H@O@MN: &O >JPG? IJO
M@OPMI OC@ CJPM <I? HDIPO@ DIAJMH<ODJI <N < NDIBG@ Q<GP@.
0OMP>ON M@HJQ@ OCDN GDHDO<ODJI <N R@GG: 0DI>@ HPGODKG@ Q<GP@N ><I =@ >JH=DI@? <N < NDIBG@
TUSVDU OTK@, API>ODJIN ><I M@OPMI <I J=E@>O JA NP>C < TUSVDU, @AA@>ODQ@GT M@OPMIDIB HPGODKG@
Q<GP@N <O JI>@. BEE%VSBUJPO() ><I IJR =@ ?@ADI@? <N M@OPMIDIB DON M@NPGO:
5JNF0G%BZ BEE%VSBUJPO(JO 5JNF0G%BZ TUBSU,
JO 5JNF0G%BZ EVSBUJPO)
\
// ...
^
AN < >JIN@LP@I>@, BEE%VSBUJPO() IJR =@>JH@N < API>ODJI OC<O KMJ?P>@N < Q<GP@, <N JKKJN@?
OJ =@DIB < API>ODJI OC<O C<N ND?@ @AA@>ON. AN TJP RJPG? M@H@H=@M AMJH OC@ #PI>ODJIN >C<KO@M,
KMJ?P>DIB M@NPGON DN KM@A@MM@? JQ@M C<QDIB ND?@ @AA@>ON.
0OMP>ON ><I =@ H@H=@MN JA JOC@M NOMP>ON. #JM @S<HKG@, OC@ AJGGJRDIB TUSVDU C<N ORJ
5JNF0G%BZ H@H=@MN:
TUSVDU .FFUJOH
\
TUSJOH UPQJD;
TJ[F@U BUUFOEBODF$PVOU;
5JNF0G%BZ TUBSU;
5JNF0G%BZ FOE;
^
.FFUJOH ><I DI OPMI =@ < H@H=@M JA <IJOC@M TUSVDU. ANNPHDIB OC<O OC@M@ DN <GNJ OC@ .FBM
NOMP>O:
TUSVDU %BJMZ1MBO
\
.FFUJOH QSPKFDU.FFUJOH;
.FBM MVODI;
.FFUJOH CVEHFU.FFUJOH;
^
Accessing the members
49.2
0OMP>O H@H=@MN <M@ PN@? GDF@ <IT JOC@M Q<MD<=G@. 1C@ JIGT ?DAA@M@I>@ DN OC<O OC@ <>OP<G NOMP>O
Q<MD<=G@ <I? < 6AF HPNO =@ NK@>DAD@? =@AJM@ OC@ I<H@ JA OC@ H@H=@M:
TUBSU.IPVS = 10;
1C@ GDI@ <=JQ@ <NNDBIN OC@ Q<GP@ 10 OJ OC@ IPVS H@H=@M JA OC@ TUBSU J=E@>O.
)@O'N M@RMDO@ OC@ BEE%VSBUJPO() API>ODJI RDOC RC<O R@ C<Q@ N@@I NJ A<M:
5JNF0G%BZ BEE%VSBUJPO(JO 5JNF0G%BZ TUBSU,
JO 5JNF0G%BZ EVSBUJPO)
\
(FDG5FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
256
5JNF0G%BZ SFTVMU;
SFTVMU.NJOVUF = TUBSU.NJOVUF + EVSBUJPO.NJOVUF;
SFTVMU.IPVS = TUBSU.IPVS + EVSBUJPO.IPVS;
SFTVMU.IPVS += SFTVMU.NJOVUF / 60;
SFTVMU.NJOVUF %= 60;
SFTVMU.IPVS %= 24;
SFUVSO SFTVMU;
^
+JOD>@ OC<O OC@ I<H@N JA OC@ Q<MD<=G@N <M@ IJR HP>C NCJMO@M DI OCDN Q@MNDJI JA OC@ API>ODJI:
TUBSU, EVSBUJPO, <I? SFTVMU. A??DODJI<GGT, DINO@<? JA PNDIB >JHKG@S I<H@N GDF@ TUBSU)PVS,
DO DN KJNND=G@ OJ <>>@NN NOMP>O H@H=@MN OCMJPBC OC@DM M@NK@>ODQ@ NOMP>O Q<MD<=G@N <N DI
TUBSU.IPVS.
%@M@ DN < >J?@ OC<O PN@N OC@ I@R BEE%VSBUJPO() API>ODJI. $DQ@I OC@ NO<MO ODH@ <I? OC@
?PM<ODJI, OC@ AJGGJRDIB >J?@ ><G>PG<O@N RC@I < >G<NN K@MDJ? <O < N>CJJG RJPG? @I?:
WPJE NBJO()
\
5JNF0G%BZ QFSJPE4UBSU;
QFSJPE4UBSU.IPVS = 8;
QFSJPE4UBSU.NJOVUF = 30;
5JNF0G%BZ QFSJPE%VSBUJPO;
QFSJPE%VSBUJPO.IPVS = 1;
QFSJPE%VSBUJPO.NJOVUF = 15;
JNNVUBCMF QFSJPE&OE = BEE%VSBUJPO(QFSJPE4UBSU,
QFSJPE%VSBUJPO);
XSJUFGMO("1FSJPE FOE: %T:%T",
QFSJPE&OE.IPVS, QFSJPE&OE.NJOVUF);
^
1C@ JPOKPO:
1FSJPE FOE: 9:45
1C@ NBJO() <=JQ@ C<N =@@I RMDOO@I JIGT =T RC<O R@ C<Q@ N@@I NJ A<M. 4@ RDGG H<F@ OCDN >J?@
@Q@I NCJMO@M <I? >G@<I@M NJJI.
Construction
49.3
1C@ ADMNO OCM@@ GDI@N JA NBJO() <M@ <=JPO >JINOMP>ODIB OC@ QFSJPE4UBSU J=E@>O <I? OC@ I@SO
OCM@@ GDI@N <M@ <=JPO >JINOMP>ODIB OC@ QFSJPE%VSBUJPO J=E@>O. &I @<>C OCM@@ GDI@N JA >J?@
ADMNO <I J=E@>O DN =@DIB ?@ADI@? <I? OC@I DON CJPM <I? HDIPO@ Q<GP@N <M@ =@DIB N@O.
&I JM?@M AJM < Q<MD<=G@ OJ =@ PN@? DI < N<A@ R<T, OC<O Q<MD<=G@ HPNO ADMNO =@ >JINOMP>O@? DI <
>JINDNO@IO NO<O@. B@><PN@ >JINOMP>ODJI DN NJ >JHHJI, OC@M@ DN < NK@>D<G >JINOMP>ODJI NTIO<S
AJM NOMP>O J=E@>ON:
5JNF0G%BZ QFSJPE4UBSU = 5JNF0G%BZ(8, 30);
5JNF0G%BZ QFSJPE%VSBUJPO = 5JNF0G%BZ(1, 15);
1C@ Q<GP@N <M@ <POJH<OD><GGT <NNDBI@? OJ OC@ H@H=@MN DI OC@ JM?@M OC<O OC@T <M@ NK@>DAD@?:
B@><PN@ IPVS DN ?@ADI@? ADMNO DI OC@ TUSVDU, OC@ Q<GP@ 8 DN <NNDBI@? OJ QFSJPE4UBSU.IPVS <I?
30 DN <NNDBI@? OJ QFSJPE4UBSU.NJOVUF.
(FDG5FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
257
Constructing objects as JNNVUBCMF 49.3.1
B@DIB <=G@ OJ >JINOMP>O OC@ J=E@>O =T NK@>DATDIB OC@ Q<GP@N JA DON H@H=@MN <O JI>@ H<F@N DO
KJNND=G@ OJ ?@ADI@ J=E@>ON <N JNNVUBCMF:
JNNVUBCMF QFSJPE4UBSU = 5JNF0G%BZ(8, 30);
JNNVUBCMF QFSJPE%VSBUJPO = 5JNF0G%BZ(1, 15);
,OC@MRDN@ DO RJPG? IJO =@ KJNND=G@ OJ H<MF <I J=E@>O ADMNO <N JNNVUBCMF <I? OC@I HJ?DAT DON
H@H=@MN:
JNNVUBCMF 5JNF0G%BZ QFSJPE4UBSU;
QFSJPE4UBSU.IPVS = 8; // j DPNQJMBUJPO &3303
QFSJPE4UBSU.NJOVUF = 30; // j DPNQJMBUJPO &3303
Trailing members need not be specified 49.3.2
1C@M@ H<T =@ G@NN Q<GP@N NK@>DAD@? OC<I OC@ IPH=@M JA H@H=@MN. &I OC<O ><N@, OC@ M@H<DIDIB
H@H=@MN <M@ DIDOD<GDU@? =T OC@ .JOJU Q<GP@N JA OC@DM M@NK@>ODQ@ OTK@N.
1C@ AJGGJRDIB KMJBM<H >JINOMP>ON 5FTU J=E@>ON @<>C ODH@ RDOC JI@ G@NN >JINOMP>OJM
K<M<H@O@M. 1C@ BTTFSU >C@>FN DI?D><O@ OC<O OC@ PINK@>DAD@? H@H=@MN <M@ DIDOD<GDU@?
<POJH<OD><GGT =T OC@DM .JOJU Q<GP@N. (1C@ M@<NJI AJM I@@?DIB OJ ><GG JT/B/() DN @SKG<DI@?
<AO@M OC@ KMJBM<H):
JNQPSU TUE.NBUI;
TUSVDU 5FTU
\
DIBS D;
JOU J;
EPVCMF E;
^
WPJE NBJO()
\
// 5IF JOJUJBM WBMVFT PG BMM PG UIF NFNCFST BSF TQFDJGJFE
BVUP U1 = 5FTU('B', 1, 2.3);
BTTFSU(U1.D == 'B');
BTTFSU(U1.J == 1);
BTTFSU(U1.E == 2.3);
// -BTU POF JT NJTTJOH
BVUP U2 = 5FTU('B', 1);
BTTFSU(U2.D == 'B');
BTTFSU(U2.J == 1);
BTTFSU(JT/B/(U2.E));
// -BTU UXP BSF NJTTJOH
BVUP U3 = 5FTU('B');
BTTFSU(U3.D == 'B');
BTTFSU(U3.J == JOU.JOJU);
BTTFSU(JT/B/(U3.E));
// /P JOJUJBM WBMVF TQFDJGJFE
BVUP U4 = 5FTU();
BTTFSU(U4.D == DIBS.JOJU);
BTTFSU(U4.J == JOU.JOJU);
BTTFSU(JT/B/(U4.E));
// 5IF TBNF BT BCPWF
5FTU U5;
BTTFSU(U5.D == DIBS.JOJU);
BTTFSU(U5.J == JOU.JOJU);
(FDG5FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
258
BTTFSU(JT/B/(U5.E));
^
AN TJP RJPG? M@H@H=@M AMJH OC@ #GJ<ODIB -JDIO 1TK@N >C<KO@M, OC@ DIDOD<G Q<GP@ JA EPVCMF DN
EPVCMF.OBO. 0DI>@ OC@ .OBO Q<GP@ DN G@AD67D76, DO DN H@<IDIBG@NN OJ PN@ DO DI @LP<GDOT
>JHK<MDNJIN. 1C<O DN RCT ><GGDIB TUE.NBUI.JT/B/ DN OC@ >JMM@>O R<T JA ?@O@MHDIDIB RC@OC@M
< Q<GP@ @LP<GN OJ .OBO.
Specifying default values for members 49.3.3
&O DN DHKJMO<IO OC<O H@H=@M Q<MD<=G@N <M@ <POJH<OD><GGT DIDOD<GDU@? RDOC FIJRI DIDOD<G Q<GP@N.
1CDN KM@Q@ION OC@ KMJBM<H AMJH >JIODIPDIB RDOC DI?@O@MHDI<O@ Q<GP@N. %JR@Q@M, OC@ .JOJU
Q<GP@ JA OC@DM M@NK@>ODQ@ OTK@N H<T IJO =@ NPDO<=G@ AJM @Q@MT OTK@. #JM @S<HKG@, DIBS.JOJU DN
IJO @Q@I < Q<GD? Q<GP@.
1C@ DIDOD<G Q<GP@N JA OC@ H@H=@MN JA < NOMP>O ><I =@ NK@>DAD@? RC@I OC@ NOMP>O DN ?@ADI@?. 1CDN
DN PN@APG AJM @S<HKG@ OJ DIDOD<GDU@ AGJ<ODIB KJDIO H@H=@MN =T 0.0, DINO@<? JA OC@ HJNOGT-
PIPN<=G@ .OBO.
1C@ ?@A<PGO Q<GP@N <M@ NK@>DAD@? =T OC@ <NNDBIH@IO NTIO<S <N OC@ H@H=@MN <M@ ?@ADI@?:
TUSVDU 5FTU
\
DIBS D = '"';
JOU J = 11;
EPVCMF E = 0.25;
^
-G@<N@ IJO@ OC<O OC@ NTIO<S <=JQ@ DN IJO M@<GGT <NNDBIH@IO. 1C@ >J?@ <=JQ@ H@M@GT
?@O@MHDI@N OC@ ?@A<PGO Q<GP@N OC<O RDGG =@ PN@? RC@I J=E@>ON JA OC<O NOMP>O <M@ <>OP<GGT
>JINOMP>O@? G<O@M DI OC@ KMJBM<H.
#JM @S<HKG@, OC@ AJGGJRDIB 5FTU J=E@>O DN =@DIB >JINOMP>O@? RDOCJPO <IT NK@>DAD> Q<GP@N:
5FTU U; // OP WBMVF JT TQFDJGJFE GPS UIF NFNCFST
XSJUFGMO("%T,%T,%T", U.D, U.J, U.E);
AGG JA OC@ H@H=@MN <M@ DIDOD<GDU@? =T OC@DM ?@A<PGO Q<GP@N:
",11,0.25
Constructing by the \ ^ syntax 49.3.4
0OMP>O J=E@>ON ><I <GNJ =@ >JINOMP>O@? =T OC@ AJGGJRDIB NTIO<S:
5JNF0G%BZ QFSJPE4UBSU = \ 8, 30 ^;
0DHDG<M OJ OC@ @<MGD@M NTIO<S, OC@ NK@>DAD@? Q<GP@N <M@ <NNDBI@? OJ OC@ H@H=@MN DI OC@ JM?@M
OC<O OC@T <M@ NK@>DAD@?. 1C@ OM<DGDIB H@H=@MN B@O OC@DM ?@A<PGO Q<GP@N.
1CDN NTIO<S DN DIC@MDO@? AMJH OC@ KMJBM<HHDIB G<IBP<B@:
BVUP QFSJPE4UBSU = 5JNF0G%BZ(8, 30); // j SFDPNNFOEFE
5JNF0G%BZ QFSJPE&OE = \ 9, 30 ^; // j BT JO $
(FDG5FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
259
Copying and assignment
49.4
0OMP>ON <M@ Q<GP@ OTK@N. AN C<N =@@I ?@N>MD=@? DI OC@ 3<GP@ 1TK@N <I? /@A@M@I>@ 1TK@N
>C<KO@M, OCDN H@<IN OC<O @Q@MT TUSVDU J=E@>O C<N DON JRI Q<GP@. ,=E@>ON B@O OC@DM JRI Q<GP@N
RC@I >JINOMP>O@?, <I? OC@DM Q<GP@N >C<IB@ RC@I OC@T <M@ <NNDBI@? I@R Q<GP@N.
BVUP ZPVS-VODI5JNF = 5JNF0G%BZ(12, 0);
BVUP NZ-VODI5JNF = ZPVS-VODI5JNF;
// 0OMZ NZ MVODI UJNF CFDPNFT 12:05:
NZ-VODI5JNF.NJOVUF += 5;
// ... ZPVS MVODI UJNF JT TUJMM UIF TBNF:
BTTFSU(ZPVS-VODI5JNF.NJOVUF == 0);
!PMDIB < >JKT, <GG JA OC@ H@H=@MN JA OC@ NJPM>@ J=E@>O <M@ <POJH<OD><GGT >JKD@? OJ OC@
>JMM@NKJI?DIB H@H=@MN JA OC@ ?@NODI<ODJI J=E@>O. 0DHDG<MGT, <NNDBIH@IO DIQJGQ@N <NNDBIDIB
@<>C H@H=@M JA OC@ NJPM>@ OJ OC@ >JMM@NKJI?DIB H@H=@M JA OC@ ?@NODI<ODJI.
0OMP>O H@H=@MN OC<O <M@ JA M@A@M@I>@ OTK@N I@@? @SOM< <OO@IODJI.
Careful with members that are of reference types! 49.4.1
AN TJP M@H@H=@M, >JKTDIB JM <NNDBIDIB Q<MD<=G@N JA M@A@M@I>@ OTK@N ?J@N IJO >C<IB@ <IT
Q<GP@, DO >C<IB@N RC<O J=E@>O DN =@DIB M@A@M@I>@?. AN < M@NPGO, >JKTDIB JM <NNDBIDIB B@I@M<O@N
JI@ HJM@ M@A@M@I>@ OJ OC@ MDBCO-C<I? ND?@ J=E@>O. 1C@ M@G@Q<I>@ JA OCDN AJM NOMP>O H@H=@MN DN
OC<O, OC@ H@H=@MN JA ORJ N@K<M<O@ NOMP>O J=E@>ON RJPG? NO<MO KMJQD?DIB <>>@NN OJ OC@ N<H@
Q<GP@.
1J N@@ <I @S<HKG@ JA OCDN, G@O'N C<Q@ < GJJF <O < NOMP>O RC@M@ JI@ JA OC@ H@H=@MN DN <
M@A@M@I>@ OTK@. 1CDN NOMP>O DN PN@? AJM F@@KDIB OC@ NOP?@IO IPH=@M <I? OC@ BM<?@N JA <
NOP?@IO:
TUSVDU 4UVEFOU
\
JOU OVNCFS;
JOU<> HSBEFT;
^
1C@ AJGGJRDIB >J?@ >JINOMP>ON < N@>JI? 4UVEFOU J=E@>O =T >JKTDIB <I @SDNODIB JI@:
// $POTUSVDUJOH UIF GJSTU PCKFDU:
BVUP TUVEFOU1 = 4UVEFOU(1, < 70, 90, 85 >);
// $POTUSVDUJOH UIF TFDPOE TUVEFOU BT B DPQZ PG UIF GJSTU
// POF BOE UIFO DIBOHJOH JUT OVNCFS:
BVUP TUVEFOU2 = TUVEFOU1;
TUVEFOU2.OVNCFS = 2;
// 8"3/*/(: 5IF HSBEFT BSF OPX CFJOH TIBSFE CZ UIF UXP PCKFDUT!
// $IBOHJOH UIF HSBEFT PG UIF GJSTU TUVEFOU ...
TUVEFOU1.HSBEFT<0> += 5;
// ... BGGFDUT UIF TFDPOE TUVEFOU BT XFMM:
XSJUFMO(TUVEFOU2.HSBEFT<0>);
4C@I TUVEFOU2 DN >JINOMP>O@?, DON H@H=@MN B@O OC@ Q<GP@N JA OC@ H@H=@MN JA TUVEFOU1.
0DI>@ JOU DN < Q<GP@ OTK@, OC@ N@>JI? J=E@>O B@ON DON JRI OVNCFS Q<GP@.
(FDG5FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
260
1C@ ORJ 4UVEFOU J=E@>ON <GNJ C<Q@ DI?DQD?P<G HSBEFT H@H=@MN <N R@GG. %JR@Q@M, NDI>@ NGD>@N
<M@ M@A@M@I>@ OTK@N, OC@ <>OP<G @G@H@ION OC<O OC@ ORJ NGD>@N NC<M@ <M@ OC@ N<H@.
JIN@LP@IOGT, < >C<IB@ H<?@ OCMJPBC JI@ JA OC@ NGD>@N DN N@@I OCMJPBC OC@ JOC@M NGD>@.
1C@ JPOKPO JA OC@ >J?@ DI?D><O@N OC<O OC@ BM<?@ JA OC@ N@>JI? NOP?@IO C<N =@@I DI>M@<N@? <N
R@GG:
75
#JM OC<O M@<NJI, < =@OO@M <KKMJ<>C HDBCO =@ OJ >JINOMP>O OC@ N@>JI? J=E@>O =T OC@ >JKD@N JA
OC@ BM<?@N JA OC@ ADMNO JI@:
// 5IF TFDPOE 4UVEFOU JT CFJOH DPOTUSVDUFE CZ UIF DPQJFT
// PG UIF HSBEFT PG UIF GJSTU POF:
BVUP TUVEFOU2 = 4UVEFOU(2, TUVEFOU1.HSBEFT.EVQ);
// $IBOHJOH UIF HSBEFT PG UIF GJSTU TUVEFOU ...
TUVEFOU1.HSBEFT<0> += 5;
// ... EPFT OPU BGGFDU UIF HSBEFT PG UIF TFDPOE TUVEFOU:
XSJUFMO(TUVEFOU2.HSBEFT<0>);
0DI>@ OC@ BM<?@N C<Q@ =@@I >JKD@? =T .EVQ, OCDN ODH@ OC@ BM<?@N JA OC@ N@>JI? NOP?@IO DN IJO
<AA@>O@?:
70
$AF7: !F ;E BAEE;4>7 FA :3H7 7H7@ F:7 D787D7@57 ?7?47DE 5AB;76 3GFA?3F;53>>K. ,7 I;>> E77
:AI F:;E ;E 6A@7 >3F7D I:7@ 5AH7D;@9 EFDG5F ?7?47D 8G@5F;A@E.
Struct literals
49.5
0DHDG<M OJ =@DIB <=G@ OJ PN@ DIO@B@M GDO@M<G Q<GP@N GDF@ 10 DI @SKM@NNDJIN RDOCJPO I@@?DIB OJ
?@ADI@ < Q<MD<=G@, NOMP>O J=E@>ON ><I =@ PN@? <N GDO@M<GN <N R@GG.
0OMP>O GDO@M<GN <M@ >JINOMP>O@? =T OC@ J=E@>O >JINOMP>ODJI NTIO<S.
5JNF0G%BZ(8, 30) // j TUSVDU MJUFSBM WBMVF
)@O'N ADMNO M@RMDO@ OC@ NBJO() API>ODJI <=JQ@ RDOC RC<O R@ C<Q@ G@<MI@? NDI>@ DON G<NO Q@MNDJI.
1C@ Q<MD<=G@N <M@ >JINOMP>O@? =T OC@ >JINOMP>ODJI NTIO<S <I? <M@ JNNVUBCMF OCDN ODH@:
WPJE NBJO()
\
JNNVUBCMF QFSJPE4UBSU = 5JNF0G%BZ(8, 30);
JNNVUBCMF QFSJPE%VSBUJPO = 5JNF0G%BZ(1, 15);
JNNVUBCMF QFSJPE&OE = BEE%VSBUJPO(QFSJPE4UBSU,
QFSJPE%VSBUJPO);
XSJUFGMO("1FSJPE FOE: %T:%T",
QFSJPE&OE.IPVS, QFSJPE&OE.NJOVUF);
^
+JO@ OC<O QFSJPE4UBSU <I? QFSJPE%VSBUJPO I@@? IJO =@ ?@ADI@? <N I<H@? Q<MD<=G@N DI OC@
>J?@ <=JQ@. 1CJN@ <M@ DI A<>O O@HKJM<MT Q<MD<=G@N DI OCDN NDHKG@ KMJBM<H, RCD>C <M@ PN@?
JIGT AJM ><G>PG<ODIB OC@ QFSJPE&OE Q<MD<=G@. 1C@T >JPG? =@ K<NN@? OJ BEE%VSBUJPO() <N
GDO@M<G Q<GP@N:
(FDG5FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
261
WPJE NBJO()
\
JNNVUBCMF QFSJPE&OE = BEE%VSBUJPO(5JNF0G%BZ(8, 30),
5JNF0G%BZ(1, 15));
XSJUFGMO("1FSJPE FOE: %T:%T",
QFSJPE&OE.IPVS, QFSJPE&OE.NJOVUF);
^
TUBUJD members
49.6
AGOCJPBC J=E@>ON HJNOGT I@@? OJ C<Q@ DI?DQD?P<G >JKD@N JA OC@ NOMP>O H@H=@MN, NJH@ODH@N DO
H<F@N N@IN@ AJM OC@ J=E@>ON JA < K<MOD>PG<M NOMP>O OTK@ OJ NC<M@ NJH@ Q<MD<=G@N. 1CDN H<T =@
I@@?@? @.B. RC@I < B@I@M<G DIAJMH<ODJI <=JPO OC<O NOMP>O OTK@ I@@? OJ =@ H<DIO<DI@?.
AN <I @S<HKG@, G@O'N DH<BDI@ < OTK@ OC<O <NNDBIN < N@K<M<O@ D?@IODAD@M AJM @Q@MT J=E@>O OC<O DN
>JINOMP>O@? JA OC<O OTK@:
TUSVDU 1PJOU
\
// 5IF JEFOUJGJFS PG FBDI PCKFDU
TJ[F@U JE;
JOU MJOF;
JOU DPMVNO;
^
&I JM?@M OJ =@ <=G@ OJ <NNDBI ?DAA@M@IO D?N OJ @<>C J=E@>O, < N@K<M<O@ Q<MD<=G@ DN I@@?@? OJ
F@@K OC@ I@SO <Q<DG<=G@ D?. &O RJPG? =@ DI>M@H@IO@? @Q@MT ODH@ < I@R J=E@>O DN >M@<O@?.
ANNPH@ OC<O OFYU*E DN OJ =@ ?@ADI@? @GN@RC@M@ <I? OJ =@ <Q<DG<=G@ DI OC@ AJGGJRDIB API>ODJI:
1PJOU NBLF1PJOU(JOU MJOF, JOU DPMVNO)
\
TJ[F@U JE = OFYU*E;
++OFYU*E;
SFUVSO 1PJOU(JE, MJOF, DPMVNO);
^
A ?@>DNDJI HPNO =@ H<?@ M@B<M?DIB RC@M@ OC@ >JHHJI OFYU*E Q<MD<=G@ DN OJ =@ ?@ADI@?.
TUBUJD H@H=@MN <M@ PN@APG DI NP>C ><N@N. 0P>C >JHHJI DIAJMH<ODJI DN ?@ADI@? <N < TUBUJD
H@H=@M JA OC@ NOMP>O. JIOM<MT OJ OC@ M@BPG<M H@H=@MN, OC@M@ DN < NDIBG@ Q<MD<=G@ JA @<>C
TUBUJD H@H=@M OCMJPBCJPO OC@ KMJBM<H. 1C<O NDIBG@ Q<MD<=G@ DN NC<M@? =T <GG JA OC@ J=E@>ON
JA OC<O NOMP>O:
JNQPSU TUE.TUEJP;
TUSVDU 1PJOU
\
// 5IF JEFOUJGJFS PG FBDI PCKFDU
TJ[F@U JE;
JOU MJOF;
JOU DPMVNO;
// 5IF JE PG UIF OFYU PCKFDU UP DPOTUSVDU
TUBUJD TJ[F@U OFYU*E;
^
1PJOU NBLF1PJOU(JOU MJOF, JOU DPMVNO)
\
TJ[F@U JE = 1PJOU.OFYU*E;
(FDG5FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
262
++1PJOU.OFYU*E;
SFUVSO 1PJOU(JE, MJOF, DPMVNO);
^
WPJE NBJO()
\
BVUP UPQ = NBLF1PJOU(7, 0);
BVUP NJEEMF = NBLF1PJOU(8, 0);
BVUP CPUUPN = NBLF1PJOU(9, 0);
XSJUFMO(UPQ.JE);
XSJUFMO(NJEEMF.JE);
XSJUFMO(CPUUPN.JE);
^
AN OFYU*E DN DI>M@H@IO@? <O @<>C J=E@>O >JINOMP>ODJI, @<>C J=E@>O B@ON < PIDLP@ D?:
0
1
2
0DI>@ TUBUJD H@H=@MN <M@ JRI@? =T OC@ @IODM@ OTK@, OC@M@ I@@? IJO =@ <I J=E@>O OJ <>>@NN
OC@H. AN R@ C<Q@ N@@I <=JQ@, NP>C J=E@>ON ><I =@ <>>@NN@? =T OC@ I<H@ JA OC@ OTK@, <N R@GG
<N =T OC@ I<H@ JA <IT J=E@>O JA OC<O NOMP>O:
++1PJOU.OFYU*E;
++CPUUPN.OFYU*E; // XPVME CF UIF TBNF BT BCPWF
Exercises
49.7
1. !@NDBI < NOMP>O I<H@? $BSE OJ M@KM@N@IO < KG<TDIB ><M?.
1CDN NOMP>O ><I C<Q@ ORJ H@H=@MN AJM OC@ NPDO <I? OC@ Q<GP@. &O H<T H<F@ N@IN@ OJ
PN@ <I FOVN OJ M@KM@N@IO OC@ NPDO, JM TJP ><I NDHKGT PN@ OC@ >C<M<>O@MN x, y, z, <I?
{.
AI JOU JM < EDIBS Q<GP@ ><I =@ PN@? AJM OC@ ><M? Q<GP@. &A TJP ?@>D?@ OJ PN@ <I JOU,
OC@ Q<GP@N 1, 11, 12, <I? 13 H<T M@KM@N@IO OC@ ><M?N OC<O ?J IJO C<Q@ IPH=@MN (<>@,
E<>F, LP@@I, <I? FDIB).
1C@M@ <M@ JOC@M ?@NDBI >CJD>@N OJ H<F@. #JM @S<HKG@, OC@ ><M? Q<GP@N ><I =@
M@KM@N@IO@? =T <I FOVN OTK@ <N R@GG.
1C@ R<T J=E@>ON JA OCDN NOMP>O RDGG =@ >JINOMP>O@? RDGG ?@K@I? JI OC@ >CJD>@ JA OC@
OTK@N JA DON H@H=@MN. #JM @S<HKG@, DA =JOC H@H=@MN <M@ EDIBS, OC@I $BSE J=E@>ON ><I
=@ >JINOMP>O@? GDF@ OCDN:
BVUP DBSE = $BSE('y', '2');
2. !@ADI@ < API>ODJI I<H@? QSJOU$BSE(), RCD>C O<F@N < $BSE J=E@>O <N < K<M<H@O@M <I?
NDHKGT KMDION DO:
WPJE QSJOU$BSE(JO $BSE DBSE)
\
// ... QMFBTF EFGJOF UIF GVODUJPO CPEZ ...
^
WPJE NBJO()
\
(FDG5FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
263
BVUP DBSE = $BSE(/* ... */);
QSJOU$BSE(DBSE);
^
#JM @S<HKG@, OC@ API>ODJI ><I KMDIO OC@ 2 JA >GP=N <N:
w2
1C@ DHKG@H@IO<ODJI JA OC<O API>ODJI H<T ?@K@I? JI OC@ >CJD>@ JA OC@ OTK@N JA OC@
H@H=@MN.
3. !@ADI@ < API>ODJI I<H@? OFX%FDL() <I? C<Q@ DO M@OPMI 52 ><M?N JA < ?@>F <N < $BSE NGD>@:
$BSE<> OFX%FDL()
PVU (SFTVMU)
\
BTTFSU(SFTVMU.MFOHUI == 52);
^
CPEZ
\
// ... QMFBTF EFGJOF UIF GVODUJPO CPEZ ...
^
&O NCJPG? =@ KJNND=G@ OJ ><GG OFX%FDL() <N DI OC@ AJGGJRDIB >J?@:
WPJE NBJO()
\
$BSE<> EFDL = OFX%FDL();
GPSFBDI (DBSE; EFDL) \
QSJOU$BSE(DBSE);
XSJUF(' ');
^
XSJUFMO();
^
1C@ JPOKPO NCJPG? =@ NDHDG<M OJ OC@ AJGGJRDIB RDOC 52 ?DNODI>O ><M?N:
t2 t3 t4 t5 t6 t7 t8 t9 t0 t+ t2 t, t" u2 u3 u4
u5 u6 u7 u8 u9 u0 u+ u2 u, u" v2 v3 v4 v5 v6 v7
v8 v9 v0 v+ v2 v, v" w2 w3 w4 w5 w6 w7 w8 w9 w0
w+ w2 w, w"
4. 4MDO@ < API>ODJI OC<O NCPAAG@N OC@ ?@>F. ,I@ R<T DN OJ KD>F ORJ ><M?N <O M<I?JH =T
TUE.SBOEPN.VOJGPSN, OJ NR<K OCJN@ ORJ ><M?N, <I? OJ M@K@<O OCDN KMJ>@NN < NPAAD>D@IO
IPH=@M JA ODH@N. 1C@ API>ODJI NCJPG? O<F@ OC@ IPH=@M JA M@K@ODODJI <N < K<M<H@O@M:
WPJE TIVGGMF($BSE<> EFDL, JO JOU SFQFUJUJPO)
\
// ... QMFBTF EFGJOF UIF GVODUJPO CPEZ ...
^
%@M@ DN CJR DO NCJPG? =@ PN@?:
WPJE NBJO()
\
$BSE<> EFDL = OFX%FDL();
TIVGGMF(EFDL, 1);
GPSFBDI (DBSE; EFDL) \
(FDG5FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
264
QSJOU$BSE(DBSE);
XSJUF(' ');
^
XSJUFMO();
^
1C@ API>ODJI NCJPG? NR<K ><M?N SFQFUJUJPO IPH=@M JA ODH@N. #JM @S<HKG@, RC@I
><GG@? =T 1, OC@ JPOKPO NCJPG? =@ NDHDG<M OJ OC@ AJGGJRDIB:
t2 t3 t4 t5 t6 t7 t8 t9 t0 t+ t2 t, t" u2 u3 u4
u5 u6 u7 u8 w4 u0 u+ u2 u, u" v2 v3 v4 v5 v6 v7
v8 v9 v0 v+ v2 v, v" w2 w3 u9 w5 w6 w7 w8 w9 w0
w+ w2 w, w"
A CDBC@M Q<GP@ AJM SFQFUJUJPO NCJPG? M@NPGO DI < HJM@ NCPAAG@? ?@>F:
TIVGGMFE(EFDL, 100);
1C@ JPOKPO:
t4 w7 v9 v6 u2 t6 w6 v" w5 v8 v3 u2 v+ w, w8 w4
u+ w2 t2 t9 v0 u" t" u9 t7 u3 v, v2 u0 t+ v7 u7
t8 u4 w+ v4 w0 u6 v5 u5 u, t3 v2 t2 t5 w2 u8 w"
t, w9 t0 w3
Note: A ?G5: 47FF7D I3K A8 E:G88>;@9 F:7 675= ;E 7JB>3;@76 ;@ F:7 EA>GF;A@E.
... F:7 EA>GF;A@E
(FDG5FE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
265
Variable Number of Parameters 50
1CDN >C<KO@M >JQ@MN ORJ ! A@<OPM@N OC<O =MDIB AG@SD=DGDOT JI K<M<H@O@MN RC@I ><GGDIB
API>ODJIN:
t !@A<PGO K<M<H@O@M Q<GP@N
t 3<MD<?D> API>ODJIN
Default parameter values
50.1
A >JIQ@ID@I>@ RDOC API>ODJI K<M<H@O@MN DN OC@ <=DGDOT OJ NK@>DAT ?@A<PGO Q<GP@N AJM OC@H. 1CDN
DN NDHDG<M OJ OC@ ?@A<PGO DIDOD<G Q<GP@N JA NOMP>O H@H=@MN.
0JH@ JA OC@ K<M<H@O@MN JA NJH@ API>ODJIN <M@ ><GG@? HJNOGT =T OC@ N<H@ Q<GP@N. 1J N@@ <I
@S<HKG@ JA OCDN, G@O'N >JIND?@M < API>ODJI OC<O KMDION OC@ @G@H@ION JA <I <NNJ>D<ODQ@ <MM<T JA
OTK@ TUSJOH<TUSJOH>. )@O'N <NNPH@ OC<O OC@ API>ODJI O<F@N OC@ N@K<M<OJM >C<M<>O@MN <N
K<M<H@O@MN <N R@GG:
WPJE QSJOU""(JO DIBS<> UJUMF,
JO TUSJOH<TUSJOH> BB,
JO DIBS<> LFZ4FQBSBUPS,
JO DIBS<> FMFNFOU4FQBSBUPS)
\
XSJUFMO("-- ", UJUMF, " --");
BVUP LFZT = BB.LFZT.TPSU;
GPSFBDI (J, LFZ; LFZT) \
// /P TFQBSBUPS CFGPSF UIF GJSTU FMFNFOU
JG (J != 0) \
XSJUF(FMFNFOU4FQBSBUPS);
^
XSJUF(LFZ, LFZ4FQBSBUPS, BB<LFZ>);
^
XSJUFMO();
^
1C<O API>ODJI DN =@DIB ><GG@? =@GJR =T ":" <N OC@ F@T N@K<M<OJM <I? ", " <N OC@ @G@H@IO
N@K<M<OJM:
TUSJOH<TUSJOH> EJDUJPOBSZ = <
"CMVF":"NBWJ", "SFE":"LfSNf[f", "HSBZ":"HSJ" >;
QSJOU""("$PMPS %JDUJPOBSZ", EJDUJPOBSZ, ":", ", ");
1C@ JPOKPO:
-- $PMPS %JDUJPOBSZ --
CMVF:NBWJ, HSBZ:HSJ, SFE:LgSNg[g
&A OC@ N@K<M<OJMN <M@ <GHJNO <GR<TN BJDIB OJ =@ OCJN@ ORJ, OC@T ><I =@ ?@ADI@? RDOC ?@A<PGO
Q<GP@N:
WPJE QSJOU""(JO DIBS<> UJUMF,
JO TUSJOH<TUSJOH> BB,
JO DIBS<> LFZ4FQBSBUPS = ": ",
JO DIBS<> FMFNFOU4FQBSBUPS = ", ")
\
+3D;34>7 $G?47D A8 &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
266
// ...
^
-<M<H@O@MN RDOC ?@A<PGO Q<GP@N I@@? IJO =@ NK@>DAD@? RC@I OC@ API>ODJI DN ><GG@?:
QSJOU""("$PMPS %JDUJPOBSZ",
EJDUJPOBSZ); /* j /P TFQBSBUPS TQFDJGJFE. #PUI
* QBSBNFUFST XJMM HFU UIFJS
* EFGBVMU WBMVFT. */
1C@ K<M<H@O@M Q<GP@N ><I NODGG =@ NK@>DAD@? RC@I I@@?@?, <I? IJO I@>@NN<MDGT <GG JA OC@H:
QSJOU""("$PMPS %JDUJPOBSZ", EJDUJPOBSZ, "=");
1C@ JPOKPO:
-- $PMPS %JDUJPOBSZ --
CMVF=NBWJ, HSBZ=HSJ, SFE=LgSNg[g
1C@ AJGGJRDIB ><GG NK@>DAD@N =JOC JA OC@ K<M<H@O@MN:
QSJOU""("$PMPS %JDUJPOBSZ", EJDUJPOBSZ, "=", "=O");
1C@ JPOKPO:
-- $PMPS %JDUJPOBSZ --
CMVF=NBWJ
HSBZ=HSJ
SFE=LgSNg[g
!@A<PGO Q<GP@N ><I JIGT =@ ?@ADI@? AJM OC@ K<M<H@O@MN OC<O <M@ <O OC@ @I? JA OC@ K<M<H@O@M
GDNO.
Variadic functions
50.2
!@NKDO@ <KK@<M<I>@N, ?@A<PGO K<M<H@O@M Q<GP@N ?J IJO >C<IB@ OC@ IPH=@M JA K<M<H@O@MN OC<O
< API>ODJI M@>@DQ@N. #JM @S<HKG@, @Q@I OCJPBC NJH@ K<M<H@O@MN H<T =@ <NNDBI@? OC@DM
?@A<PGO Q<GP@N, QSJOU""() <GR<TN O<F@N AJPM K<M<H@O@MN <I? PN@N OC@H <>>JM?DIB OJ DON
DHKG@H@IO<ODJI.
1C@ Q<MD<?D> API>ODJIN A@<OPM@ JI OC@ JOC@M C<I? <GGJRN ><GGDIB API>ODJIN RDOC PINK@>DAD@?
IPH=@M JA <MBPH@ION. 4@ C<Q@ <GM@<?T =@@I O<FDIB <?Q<IO<B@ JA OCDN A@<OPM@ RDOC API>ODJIN
GDF@ XSJUFMO(). XSJUFMO() ><I =@ ><GG@? RDOC <IT IPH=@M JA <MBPH@ION:
XSJUFMO(
"IFMMP", 7, "XPSME", 9.8 /*, BOE BOZ OVNCFS PG PUIFS
* BSHVNFOUT BT OFFEFE */);
1C@M@ <M@ AJPM R<TN JA ?@ADIDIB Q<MD<?D> API>ODJIN DI !:
t 1C@ A@<OPM@ OC<O RJMFN JIGT AJM API>ODJIN OC<O <M@ H<MF@? <N FYUFSO "$". 1CDN A@<OPM@
?@ADI@N OC@ CD??@I @BSHQUS Q<MD<=G@ OC<O DN PN@? AJM <>>@NNDIB OC@ K<M<H@O@MN. 1CDN =JJF
?J@N IJO >JQ@M OCDN A@<OPM@ K<MOGT =@><PN@ DO DN PIN<A@.
t 1C@ A@<OPM@ OC<O RJMFN RDOC M@BPG<M ! API>ODJIN, RCD>C <GNJ PN@N OC@ CD??@I @BSHQUS
Q<MD<=G@, <N R@GG <N OC@ @BSHVNFOUT Q<MD<=G@, OC@ G<OO@M =@DIB JA OTK@ 5ZQF*OGP<>. 1CDN
+3D;34>7 $G?47D A8 &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
267
=JJF ?J@N IJO >JQ@M OCDN A@<OPM@ <N R@GG =JOC =@><PN@ DO M@GD@N JI BA;@F7DE, RCD>C C<Q@ IJO
=@@I >JQ@M@? T@O, <I? =@><PN@ OCDN A@<OPM@ ><I =@ PN@? DI PIN<A@ R<TN <N R@GG.
t A N<A@ A@<OPM@ RDOC OC@ GDHDO<ODJI OC<O OC@ PINK@>DAD@? IPH=@M JA K<M<H@O@MN HPNO <GG =@
JA OC@ N<H@ OTK@. 1CDN DN OC@ A@<OPM@ OC<O DN >JQ@M@? DI OCDN N@>ODJI.
t 2INK@>DAD@? IPH=@M JA O@HKG<O@ K<M<H@O@MN. 1CDN A@<OPM@ RDGG =@ @SKG<DI@? G<O@M DI OC@
O@HKG<O@N >C<KO@MN.
1C@ K<M<H@O@MN JA Q<MD<?D> API>ODJIN <M@ K<NN@? OJ OC@ API>ODJI <N < NGD>@. 3<MD<?D> API>ODJIN
<M@ ?@ADI@? RDOC < NDIBG@ K<M<H@O@M JA < NK@>DAD> OTK@ JA NGD>@ AJGGJR@? DHH@?D<O@GT =T OC@
... >C<M<>O@MN:
EPVCMF TVN(JO EPVCMF<> OVNCFST ...)
\
EPVCMF SFTVMU = 0.0;
GPSFBDI (OVNCFS; OVNCFST) \
SFTVMU += OVNCFS;
^
SFUVSO SFTVMU;
^
1C<O ?@ADIDODJI H<F@N TVN() < Q<MD<?D> API>ODJI, H@<IDIB OC<O DO DN <=G@ OJ M@>@DQ@ <IT
IPH=@M JA <MBPH@ION <N GJIB <N OC@T <M@ EPVCMF JM <IT JOC@M OTK@ OC<O ><I DHKGD>DOGT =@
>JIQ@MOD=G@ OJ EPVCMF:
XSJUFMO(TVN(1.1, 2.2, 3.3));
1C@ NDIBG@ NGD>@ K<M<H@O@M <I? OC@ ... >C<M<>O@MN M@KM@N@IO <GG JA OC@ <MBPH@ION. #JM
@S<HKG@, OC@ NGD>@ RJPG? C<Q@ ADQ@ @G@H@ION DA OC@ API>ODJI R@M@ ><GG@? RDOC ADQ@ EPVCMF
Q<GP@N.
&I A<>O, OC@ Q<MD<=G@ IPH=@M JA K<M<H@O@MN ><I <GNJ =@ K<NN@? <N < NDIBG@ NGD>@:
XSJUFMO(TVN(< 1.1, 2.2, 3.3 >)); // TBNF BT BCPWF
3<MD<?D> API>ODJIN ><I <GNJ C<Q@ M@LPDM@? K<M<H@O@MN, RCD>C HPNO =@ ?@ADI@? ADMNO DI OC@
K<M<H@O@M GDNO. #JM @S<HKG@, OC@ AJGGJRDIB API>ODJI KMDION <I PINK@>DAD@? IPH=@M JA
K<M<H@O@MN RDOCDI K<M@IOC@N@N. AGOCJPBC OC@ API>ODJI G@<Q@N OC@ IPH=@M JA OC@ @G@H@ION
AG@SD=G@, DO M@LPDM@N OC<O OC@ K<M@IOC@N@N <M@ <GR<TN NK@>DAD@?:
DIBS<> QBSFOUIFTJ[F(
JO DIBS<> PQFOJOH, // j 5IF GJSTU UXP QBSBNFUFST NVTU CF
JO DIBS<> DMPTJOH, // TQFDJGJFE XIFO UIF GVODUJPO JT DBMMFE.
JO DIBS<><> XPSET ...) // j /FFE OPU CF TQFDJGJFE
\
DIBS<> SFTVMU;
GPSFBDI (XPSE; XPSET) \
SFTVMU _= PQFOJOH;
SFTVMU _= XPSE;
SFTVMU _= DMPTJOH;
^
SFUVSO SFTVMU;
^
1C@ ADMNO ORJ K<M<H@O@MN <M@ H<I?<OJMT:
QBSFOUIFTJ[F("\"); // j DPNQJMBUJPO &3303
+3D;34>7 $G?47D A8 &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
268
AN GJIB <N OC@ H<I?<OJMT K<M<H@O@MN <M@ NK@>DAD@?, OC@ M@NO <M@ JKODJI<G:
XSJUFMO(QBSFOUIFTJ[F("\", "^", "BQQMF", "QFBS", "CBOBOB"));
1C@ JPOKPO:
\BQQMF^\QFBS^\CBOBOB^
Exercise
50.3
t ANNPH@ OC<O OC@ AJGGJRDIB FOVN DN <GM@<?T ?@ADI@?:
FOVN 0QFSBUJPO \ BEE, TVCUSBDU, NVMUJQMZ, EJWJEF ^
AGNJ <NNPH@ OC<O OC@M@ DN < TUSVDU OC<O M@KM@N@ION OC@ ><G>PG<ODJI JA <I JK@M<ODJI
<I? DON ORJ JK@M<I?N:
TUSVDU $BMDVMBUJPO
\
0QFSBUJPO PQ;
EPVCMF GJSTU;
EPVCMF TFDPOE;
^
#JM @S<HKG@, OC@ J=E@>O $BMDVMBUJPO(0QFSBUJPO.EJWJEF, 7.7, 8.8) RJPG?
M@KM@N@IO OC@ ?DQDNDJI JA 7.7 =T 8.8.
!@NDBI < API>ODJI OC<O M@>@DQ@N <I PINK@>DAD@? IPH=@M JA OC@N@ TUSVDU J=E@>ON,
><G>PG<O@N OC@ M@NPGO JA @<>C $BMDVMBUJPO, <I? OC@I M@OPMIN <GG JA OC@ M@NPGON <N <
NGD>@ JA OTK@ EPVCMF<>.
#JM @S<HKG@, DO NCJPG? =@ KJNND=G@ OJ ><GG OC@ API>ODJI <N DI OC@ AJGGJRDIB >J?@:
WPJE NBJO()
\
XSJUFMO(DBMDVMBUF($BMDVMBUJPO(0QFSBUJPO.BEE, 1.1, 2.2),
$BMDVMBUJPO(0QFSBUJPO.TVCUSBDU, 3.3, 4.4),
$BMDVMBUJPO(0QFSBUJPO.NVMUJQMZ, 5.5, 6.6),
$BMDVMBUJPO(0QFSBUJPO.EJWJEF, 7.7, 8.8)));
^
1C@ JPOKPO JA OC@ >J?@ NCJPG? =@ NDHDG<M OJ OC@ AJGGJRDIB:
<3.3, -1.1, 36.3, 0.875>
... F:7 EA>GF;A@
+3D;34>7 $G?47D A8 &3D3?7F7DE
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
269
Function Overloading 51
!@ADIDIB HJM@ OC<I JI@ API>ODJI C<QDIB OC@ N<H@ I<H@ DN 8G@5F;A@ AH7D>A36;@9. &I JM?@M OJ
=@ <=G@ OJ ?DAA@M@IOD<O@ OC@N@ API>ODJIN, OC@DM K<M<H@O@MN HPNO =@ ?DAA@M@IO.
1C@ AJGGJRDIB >J?@ C<N HPGODKG@ JQ@MGJ<?N JA OC@ JOGP() API>ODJI, @<>C O<FDIB < ?DAA@M@IO
OTK@ JA K<M<H@O@M:
JNQPSU TUE.TUEJP;
WPJE JOGP(JO EPVCMF OVNCFS)
\
XSJUFMO("'MPBUJOH QPJOU: ", OVNCFS);
^
WPJE JOGP(JO JOU OVNCFS)
\
XSJUFMO("*OUFHFS : ", OVNCFS);
^
WPJE JOGP(JO DIBS<> TUS)
\
XSJUFMO("4USJOH : ", TUS);
^
WPJE NBJO()
\
JOGP(1.2);
JOGP(3);
JOGP("IFMMP");
^
AGOJPBC <GG JA OC@ API>ODJIN <M@ I<H@? JOGP(), OC@ >JHKDG@M KD>FN OC@ JI@ OC<O H<O>C@N OC@
<MBPH@IO OC<O DN PN@? RC@I H<FDIB OC@ ><GG. #JM @S<HKG@, =@><PN@ OC@ GDO@M<G 1.2 DN JA OTK@
EPVCMF, OC@ JOGP() API>ODJI OC<O O<F@N < EPVCMF B@ON ><GG@? AJM DO.
1C@ >CJD>@ JA RCD>C API>ODJI OJ ><GG DN H<?@ <O >JHKDG@ ODH@, RCD>C H<T IJO <GR<TN =@ @<NT
JM >G@<M. #JM @S<HKG@, =@><PN@ JOU ><I DHKGD>DOGT =@ >JIQ@MO@? OJ =JOC EPVCMF <I? SFBM, OC@
>JHKDG@M ><IIJO ?@>D?@ RCD>C JA OC@ API>ODJIN OJ ><GG DI OC@ AJGGJRDIB KMJBM<H:
SFBM TFWFO5JNFT(JO SFBM WBMVF)
\
SFUVSO 7 * WBMVF;
^
EPVCMF TFWFO5JNFT(JO EPVCMF WBMVF)
\
SFUVSO 7 * WBMVF;
^
WPJE NBJO()
\
JOU WBMVF = 5;
BVUP SFTVMU = TFWFO5JNFT(WBMVF); // j DPNQJMBUJPO &3303
^
Note: !F ;E GEG3>>K G@@757EE3DK FA ID;F7 E7B3D3F7 8G@5F;A@E I:7@ F:7 8G@5F;A@ 4A6;7E 3D7
7J35F>K F:7 E3?7. ,7 I;>> E77 >3F7D ;@ F:7 )7?B>3F7E 5:3BF7D :AI 3 E;@9>7 678;@;F;A@ 53@ 47
GE76 8AD ?G>F;B>7 FKB7E.
%JR@Q@M, DA OC@M@ DN <IJOC@M API>ODJI JQ@MGJ<? OC<O O<F@N < MPOH K<M<H@O@M, OC@I OC@
<H=DBPDOT RJPG? =@ M@NJGQ@? =@><PN@ MPOH DN < 47FF7D ?3F5: AJM JOU OC<I EPVCMF JM SFBM:
FG@5F;A@ %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
270
MPOH TFWFO5JNFT(JO MPOH WBMVF)
\
SFUVSO 7 * WBMVF;
^
// ...
BVUP SFTVMU = TFWFO5JNFT(WBMVF); // OPX DPNQJMFT
Overload resolution
51.1
1C@ >JHKDG@M KD>FN OC@ JQ@MGJ<? OC<O DN OC@ 47EF ?3F5: AJM OC@ <MBPH@ION. 1CDN DN ><GG@?
JQ@MGJ<? M@NJGPODJI.
AGOJPBC JQ@MGJ<? M@NJGPODJI DN NDHKG@ <I? DIOPDODQ@ DI HJNO ><N@N, DO DN NJH@ODH@N
>JHKGD><O@?. 1C@ AJGGJRDIB <M@ OC@ MPG@N JA JQ@MGJ<? M@NJGPODJI. 1C@T <M@ =@DIB KM@N@IO@? DI <
NDHKGDAD@? R<T DI OCDN =JJF.
1C@M@ <M@ AJPM NO<O@N JA H<O>C, GDNO@? AMJH OC@ RJMNO OJ OC@ =@NO:
t HDNH<O>C
t H<O>C OCMJPBC <POJH<OD> OTK@ >JIQ@MNDJI
t H<O>C OCMJPBC DPOTU LP<GDAD><ODJI
t @S<>O H<O>C
1C@ >JHKDG@M >JIND?@MN <GG JA OC@ JQ@MGJ<?N JA < API>ODJI ?PMDIB JQ@MGJ<? M@NJGPODJI. &O ADMNO
?@O@MHDI@N OC@ H<O>C NO<O@ JA @Q@MT K<M<H@O@M AJM @Q@MT JQ@MGJ<?. #JM @<>C JQ@MGJ<?, OC@
G@<NO H<O>C NO<O@ <HJIB OC@ K<M<H@O@MN DN O<F@I OJ =@ OC@ H<O>C NO<O@ JA OC<O JQ@MGJ<?.
AAO@M <GG JA OC@ H<O>C NO<O@N JA OC@ JQ@MGJ<?N <M@ ?@O@MHDI@?, OC@I OC@ JQ@MGJ<? RDOC OC@ =@NO
H<O>C DN N@G@>O@?. &A OC@M@ <M@ HJM@ OC<I JI@ JQ@MGJ<? OC<O C<N OC@ =@NO H<O>C, OC@I HJM@
>JHKGD><O@? M@NJGPODJI MPG@N <M@ <KKGD@?. & RDGG IJO B@O DIOJ HJM@ ?@O<DGN JA OC@N@ MPG@N DI OCDN
=JJF. &A TJPM KMJBM<H DN DI < NDOP<ODJI RC@M@ DO ?@K@I?N JI >JHKGD><O@? JQ@MGJ<? M@NJGPODJI
MPG@N, DO H<T <I DI?D><ODJI OC<O DO DN ODH@ OJ >C<IB@ OC@ ?@NDBI JA OC@ KMJBM<H. AIJOC@M JKODJI
DN OJ O<F@ <?Q<IO<B@ JA JOC@M A@<OPM@N JA ! GDF@ O@HKG<O@N. AI @Q@I NDHKG@M <KKMJ<>C RJPG?
=@ OJ <=<I?JI API>ODJI JQ@MGJ<?DIB <GOJB@OC@M =T I<HDIB API>ODJIN ?DAA@M@IOGT AJM @<>C OTK@
@.B. GDF@ TFWFO5JNFT@SFBM() <I? TFWFO5JNFT@EPVCMF().
Function overloading for structs
51.2
#PI>ODJI JQ@MGJ<?DIB DN PN@APG RDOC NOMP>ON <I? >G<NN@N <N R@GG. A??DODJI<GGT, JQ@MGJ<?
M@NJGPODJI <H=DBPDOD@N <M@ HP>C G@NN AM@LP@IO RDOC PN@M-?@ADI@? OTK@N. )@O'N JQ@MGJ<? OC@
JOGP() API>ODJI <=JQ@ AJM NJH@ JA OC@ OTK@N OC<O R@ C<Q@ ?@ADI@? DI OC@ 0OMP>ON >C<KO@M:
TUSVDU 5JNF0G%BZ
\
JOU IPVS;
JOU NJOVUF;
^
WPJE JOGP(JO 5JNF0G%BZ UJNF)
\
XSJUFG("%02T:%02T", UJNF.IPVS, UJNF.NJOVUF);
^
1C<O JQ@MGJ<? @I<=G@N 5JNF0G%BZ J=E@>ON OJ =@ PN@? RDOC JOGP(). AN < M@NPGO, Q<MD<=G@N JA
PN@M-?@ADI@? OTK@N ><I =@ KMDIO@? DI @S<>OGT OC@ N<H@ R<T <N API?<H@IO<G OTK@N:
FG@5F;A@ %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
271
BVUP CSFBLGBTU5JNF = 5JNF0G%BZ(7, 0);
JOGP(CSFBLGBTU5JNF);
1C@ 5JNF0G%BZ J=E@>ON RJPG? =@ H<O>C@? RDOC OC<O JQ@MGJ<? JA JOGP():
07:00
1C@ AJGGJRDIB DN <I JQ@MGJ<? JA JOGP() AJM OC@ .FFUJOH OTK@:
TUSVDU .FFUJOH
\
TUSJOH UPQJD;
TJ[F@U BUUFOEBODF$PVOU;
5JNF0G%BZ TUBSU;
5JNF0G%BZ FOE;
^
WPJE JOGP(JO .FFUJOH NFFUJOH)
\
JOGP(NFFUJOH.TUBSU);
XSJUF('-');
JOGP(NFFUJOH.FOE);
XSJUFG(" ="%T=" NFFUJOH XJUI %T BUUFOEFFT",
NFFUJOH.UPQJD,
NFFUJOH.BUUFOEBODF$PVOU);
^
+JO@ OC<O OCDN JQ@MGJ<? H<F@N PN@ JA OC@ <GM@<?T-?@ADI@? JQ@MGJ<? AJM 5JNF0G%BZ. .FFUJOH
J=E@>ON ><I IJR =@ KMDIO@? DI @S<>OGT OC@ N<H@ R<T <N API?<H@IO<G OTK@N <N R@GG:
BVUP CJLF3JEF.FFUJOH = .FFUJOH("#JLF 3JEF", 3,
5JNF0G%BZ(9, 0),
5JNF0G%BZ(9, 10));
JOGP(CJLF3JEF.FFUJOH);
1C@ JPOKPO:
09:00-09:10 "#JLF 3JEF" NFFUJOH XJUI 3 BUUFOEFFT
Limitations
51.3
AGOCJPBC OC@ JOGP() API>ODJI JQ@MGJ<?N <=JQ@ <M@ < BM@<O >JIQ@ID@I>@, OCDN H@OCJ? C<N
NJH@ GDHDO<ODJIN:
t JOGP() <GR<TN KMDION OJ TUEPVU. &O RJPG? =@ HJM@ PN@APG DA DO >JPG? KMDIO OJ <IT 'JMF. ,I@
R<T JA <>CD@QDIB OCDN DN OJ K<NN OC@ JPOKPO NOM@<H <N < K<M<H@O@M <N R@GG @.B. AJM OC@
5JNF0G%BZ OTK@:
WPJE JOGP('JMF GJMF, JO 5JNF0G%BZ UJNF)
\
GJMF.XSJUFG("%02T:%02T", UJNF.IPVS, UJNF.NJOVUF);
^
1C<O RJPG? @I<=G@ KMDIODIB 5JNF0G%BZ J=E@>ON OJ <IT ADG@, DI>GP?DIB TUEPVU:
JOGP(TUEPVU, CSFBLGBTU5JNF);
BVUP GJMF = 'JMF("B@GJMF", "X");
JOGP(GJMF, CSFBLGBTU5JNF);
FG@5F;A@ %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
272
Note: ):7 EB75;3> A4<75FE stdin, stdout, 3@6 stderr 3D7 A8 FKB7 File.
t *JM@ DHKJMO<IOGT, JOGP() ?J@N IJO NJGQ@ OC@ HJM@ B@I@M<G KMJ=G@H JA KMJ?P>DIB OC@
NOMDIB M@KM@N@IO<ODJI JA Q<MD<=G@N. #JM @S<HKG@, DO ?J@N IJO C@GK RDOC K<NNDIB J=E@>ON JA
PN@M-?@ADI@? OTK@N OJ XSJUFMO():
XSJUFMO(CSFBLGBTU5JNF); // /PU VTFGVM: QSJOUT JO HFOFSJD GPSNBU
1C@ >J?@ <=JQ@ KMDION OC@ J=E@>O DI < B@I@MD> AJMH<O OC<O DI>GP?@N OC@ I<H@ JA OC@
OTK@ <I? OC@ Q<GP@N JA DON H@H=@MN, IJO DI < R<T OC<O RJPG? =@ PN@APG DI OC@ KMJBM<H:
5JNF0G%BZ(7, 0)
&O RJPG? =@ HP>C HJM@ PN@APG DA OC@M@ R@M@ < API>ODJI OC<O >JIQ@MO@? 5JNF0G%BZ
J=E@>ON OJ TUSJOH DI OC@DM NK@>D<G AJMH<O <N DI "12:34". 4@ RDGG N@@ CJR OJ ?@ADI@
TUSJOH M@KM@N@IO<ODJIN JA NOMP>O J=E@>ON DI OC@ I@SO >C<KO@M.
Exercise
51.4
t ,Q@MGJ<? OC@ JOGP() API>ODJI AJM OC@ AJGGJRDIB NOMP>ON <N R@GG:
TUSVDU .FBM
\
5JNF0G%BZ UJNF;
TUSJOH BEESFTT;
^
TUSVDU %BJMZ1MBO
\
.FFUJOH BN.FFUJOH;
.FBM MVODI;
.FFUJOH QN.FFUJOH;
^
0DI>@ .FBM C<N JIGT OC@ NO<MO ODH@, <?? <I CJPM <I? < C<GA OJ ?@O@MHDI@ DON @I? ODH@.
6JP ><I PN@ OC@ BEE%VSBUJPO() API>ODJI OC<O R@ C<Q@ ?@ADI@? @<MGD@M DI OC@ NOMP>ON
>C<KO@M:
5JNF0G%BZ BEE%VSBUJPO(JO 5JNF0G%BZ TUBSU,
JO 5JNF0G%BZ EVSBUJPO)
\
5JNF0G%BZ SFTVMU;
SFTVMU.NJOVUF = TUBSU.NJOVUF + EVSBUJPO.NJOVUF;
SFTVMU.IPVS = TUBSU.IPVS + EVSBUJPO.IPVS;
SFTVMU.IPVS += SFTVMU.NJOVUF / 60;
SFTVMU.NJOVUF %= 60;
SFTVMU.IPVS %= 24;
SFUVSO SFTVMU;
^
,I>@ OC@ @I? ODH@N JA .FBM J=E@>ON <M@ ><G>PG<O@? =T BEE%VSBUJPO(), %BJMZ1MBO
J=E@>ON NCJPG? =@ KMDIO@? <N DI OC@ AJGGJRDIB JPOKPO:
10:30-11:45 "#JLF 3JEF" NFFUJOH XJUI 4 BUUFOEFFT
12:30-14:00 .FBM, "EESFTT: fTUBOCVM
15:30-17:30 "#VEHFU" NFFUJOH XJUI 8 BUUFOEFFT
FG@5F;A@ %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
273
... F:7 EA>GF;A@
FG@5F;A@ %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
274
Member Functions 52
AGOCJPBC OCDN >C<KO@M AJ>PN@N JIGT JI NOMP>ON, HJNO JA OC@ DIAJMH<ODJI DI OCDN >C<KO@M DN
<KKGD><=G@ OJ >G<NN@N <N R@GG.
&I OCDN >C<KO@M R@ RDGG >JQ@M H@H=@M API>ODJIN JA NOMP>ON <I? ?@ADI@ OC@ NK@>D<G UP4USJOH()
H@H=@M API>ODJI OC<O DN PN@? AJM M@KM@N@IODIB J=E@>ON DI OC@ TUSJOH AJMH<O.
4C@I < NOMP>O JM >G<NN DN ?@ADI@?, PNP<GGT < IPH=@M JA API>ODJIN <M@ <GNJ ?@ADI@? <GJIBND?@
RDOC DO. 4@ C<Q@ N@@I @S<HKG@N JA NP>C API>ODJIN DI OC@ @<MGD@M >C<KO@MN: BEE%VSBUJPO() <I?
<I JQ@MGJ<? JA JOGP() C<Q@ =@@I RMDOO@I NK@>DAD><GGT OJ =@ PN@? RDOC OC@ 5JNF0G%BZ OTK@. &I <
N@IN@, OC@N@ ORJ API>ODJIN ?@ADI@ OC@ ;@F7D8357 JA 5JNF0G%BZ.
1C@ ADMNO K<M<H@O@M JA =JOC BEE%VSBUJPO() <I? JOGP() C<N =@@I OC@ 5JNF0G%BZ J=E@>O OC<O
@<>C API>ODJI RJPG? =@ JK@M<ODIB JI. A??DODJI<GGT, EPNO GDF@ <GG JA OC@ JOC@M API>ODJIN OC<O R@
C<Q@ N@@I NJ A<M, =JOC JA OC@ API>ODJIN C<Q@ =@@I ?@ADI@? <O OC@ ?A6G>7 >7H7>, JPOND?@ JA <IT
JOC@M N>JK@.
1C@ >JI>@KO JA < N@O JA API>ODJIN ?@O@MHDIDIB OC@ DIO@MA<>@ JA < NOMP>O DN Q@MT >JHHJI. #JM
OC<O M@<NJI, API>ODJIN OC<O <M@ >GJN@GT M@G<O@? OJ < OTK@ ><I =@ ?@ADI@? RDOCDI OC@ =J?T JA OC<O
OTK@.
Defining member functions
52.1
#PI>ODJIN OC<O <M@ ?@ADI@? RDOCDI OC@ >PMGT =M<>F@ON JA < TUSVDU <M@ ><GG@? ?7?47D
8G@5F;A@E:
TUSVDU 4PNF4USVDU
\
WPJE member_function(/* UIF QBSBNFUFST PG UIF GVODUJPO */)
\
// ... UIF EFGJOJUJPO PG UIF GVODUJPO ...
^
// ... UIF PUIFS NFNCFST PG UIF TUSVDU ...
^
*@H=@M API>ODJIN <M@ <>>@NN@? OC@ N<H@ R<T <N H@H=@M Q<MD<=G@N, N@K<M<O@? AMJH OC@ I<H@
JA OC@ J=E@>O =T < ?JO:
object.member_function(arguments);
4@ C<Q@ PN@? H@H=@M API>ODJIN =@AJM@ RC@I NK@>DATDIB TUEJO <I? TUEPVU @SKGD>DOGT ?PMDIB
DIKPO <I? JPOKPO JK@M<ODJIN:
TUEJO.SFBEG(" %T", &OVNCFS);
TUEPVU.XSJUFMO(OVNCFS);
1C@ SFBEG() <I? XSJUFMO() <=JQ@ <M@ H@H=@M API>ODJI ><GGN, JK@M<ODIB JI OC@ J=E@>ON
TUEJO <I? TUEPVU, M@NK@>ODQ@GT.
)@O'N ?@ADI@ JOGP() <N < H@H=@M API>ODJI. &ON KM@QDJPN ?@ADIDODJI C<N =@@I OC@ AJGGJRDIB:
WPJE JOGP(JO 5JNF0G%BZ UJNF)
\
XSJUFG("%02T:%02T", UJNF.IPVS, UJNF.NJOVUF);
^
#7?47D FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
275
*<FDIB JOGP() < H@H=@M API>ODJI DN IJO <N NDHKG@ <N HJQDIB DON ?@ADIDODJI DIND?@ OC@ NOMP>O.
1C@ API>ODJI HPNO =@ HJ?DAD@? DI ORJ R<TN:
TUSVDU 5JNF0G%BZ
\
JOU IPVS;
JOU NJOVUF;
WPJE JOGP() // (1)
\
XSJUFG("%02T:%02T", IPVS, NJOVUF); // (2)
^
^
1. 1C@ H@H=@M API>ODJI ?J@N IJO O<F@ OC@ J=E@>O @SKGD>DOGT <N < K<M<H@O@M.
2. #JM OC<O M@<NJI, DO M@A@MN OJ OC@ H@H=@M Q<MD<=G@N NDHKGT <N IPVS <I? NJOVUF.
1C@ M@<NJI DN OC<O H@H=@M API>ODJIN <M@ <GR<TN ><GG@? JI <I @SDNODIB J=E@>O. 1C@ J=E@>O DN
DHKGD>DOGT <Q<DG<=G@ OJ OC@ H@H=@M API>ODJI:
BVUP UJNF = 5JNF0G%BZ(10, 30);
UJNF.JOGP();
1C@ JOGP() H@H=@M API>ODJI DN =@DIB ><GG@? JI OC@ UJNF J=E@>O <=JQ@. 1C@ H@H=@MN IPVS
<I? NJOVUF OC<O <M@ M@A@MM@? OJ RDOCDI OC@ API>ODJI ?@ADIDODJI >JMM@NKJI? OJ OC@ H@H=@MN JA
OC@ UJNF J=E@>O, NK@>DAD><GGT UJNF.IPVS <I? UJNF.NJOVUF.
1C@ H@H=@M API>ODJI ><GG <=JQ@ DN <GHJNO OC@ @LPDQ<G@IO JA OC@ AJGGJRDIB M@BPG<M API>ODJI
><GG:
UJNF.JOGP(); // NFNCFS GVODUJPO
JOGP(UJNF); // SFHVMBS GVODUJPO (UIF QSFWJPVT EFGJOJUJPO)
4C@I@Q@M < H@H=@M API>ODJI DN ><GG@? JI <I J=E@>O, OC@ H@H=@MN JA OC@ J=E@>O <M@ DHKGD>DOGT
<>>@NND=G@ =T OC@ API>ODJI:
BVUP NPSOJOH = 5JNF0G%BZ(10, 0);
BVUP FWFOJOH = 5JNF0G%BZ(22, 0);
NPSOJOH.JOGP();
XSJUF('-');
FWFOJOH.JOGP();
XSJUFMO();
4C@I ><GG@? JI NPSOJOH, OC@ IPVS <I? NJOVUF OC<O <M@ PN@? DIND?@ OC@ H@H=@M API>ODJI
M@A@M OJ NPSOJOH.IPVS <I? NPSOJOH.NJOVUF. 0DHDG<MGT, RC@I ><GG@? JI FWFOJOH, OC@T M@A@M OJ
FWFOJOH.IPVS <I? FWFOJOH.NJOVUF:
10:00-22:00
UP4USJOH() for TUSJOH representations 52.1.1
4@ C<Q@ ?DN>PNN@? OC@ GDHDO<ODJIN JA OC@ JOGP() API>ODJI DI OC@ KM@QDJPN >C<KO@M. 1C@M@ DN <O
G@<NO JI@ HJM@ DI>JIQ@ID@I>@ RDOC DO: AGOCJPBC DO KMDION OC@ ODH@ DI CPH<I-M@<?<=G@ AJMH<O,
KMDIODIB OC@ '-' >C<M<>O@M <I? O@MHDI<ODIB OC@ GDI@ NODGG I@@?N OJ =@ ?JI@ @SKGD>DOGT =T OC@
KMJBM<HH@M.
%JR@Q@M, DO RJPG? =@ HJM@ >JIQ@ID@IO DA 5JNF0G%BZ J=E@>ON >JPG? =@ PN@? <N @<NT <N
API?<H@IO<G OTK@N <N DI OC@ AJGGJRDIB >J?@:
#7?47D FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
276
XSJUFGMO("%T-%T", NPSOJOH, FWFOJOH);
&I <??DODJI OJ M@?P>DIB AJPM GDI@N JA >J?@ OJ JI@, DO RJPG? <GNJ <GGJR KMDIODIB J=E@>ON OJ <IT
NOM@<H:
BVUP GJMF = 'JMF("UJNF@JOGPSNBUJPO", "X");
GJMF.XSJUFGMO("%T-%T", NPSOJOH, FWFOJOH);
1C@ UP4USJOH() H@H=@M API>DJI JA PN@M-?@ADI@? OTK@N <M@ OM@<O@? NK@>D<GGT: &O DN ><GG@?
<POJH<OD><GGT OJ KMJ?P>@ OC@ TUSJOH M@KM@N@IO<ODJIN JA J=E@>ON. UP4USJOH() HPNO M@OPMI OC@
TUSJOH M@KM@N@IO<ODJI JA OC@ J=E@>O.
4DOCJPO B@OODIB DIOJ HJM@ ?@O<DG, G@O'N ADMNO N@@ CJR OC@ UP4USJOH() API>ODJI DN ?@ADI@?:
JNQPSU TUE.TUEJP;
TUSVDU 5JNF0G%BZ
\
JOU IPVS;
JOU NJOVUF;
TUSJOH UP4USJOH()
\
SFUVSO "UPEP";
^
^
WPJE NBJO()
\
BVUP NPSOJOH = 5JNF0G%BZ(10, 0);
BVUP FWFOJOH = 5JNF0G%BZ(22, 0);
XSJUFGMO("%T-%T", NPSOJOH, FWFOJOH);
^
UP4USJOH() ?J@N IJO KMJ?P>@ <ITOCDIB H@<IDIBAPG T@O, =PO OC@ JPOKPO NCJRN OC<O DO C<N =@@I
><GG@? =T XSJUFGMO() ORD>@ AJM OC@ ORJ J=E@>O:
UPEP-UPEP
AGNJ IJO@ OC<O JOGP() DN IJO I@@?@? <ITHJM@. UP4USJOH() DN M@KG<>DIB DON API>ODJI<GDOT.
1C@ NDHKG@NO DHKG@H@IO<ODJI JA UP4USJOH() RJPG? =@ OJ ><GG GPSNBU() JA OC@ TUE.TUSJOH
HJ?PG@. GPSNBU() RJMFN DI OC@ N<H@ R<T <N OC@ AJMH<OO@? JPOKPO API>ODJIN GDF@ XSJUFG().
1C@ JIGT ?DAA@M@I>@ DN OC<O DINO@<? JA KMDIODIB Q<MD<=G@N, DO M@OPMIN OC@ AJMH<OO@? M@NPGO DI
TUSJOH AJMH<O.
UP4USJOH() ><I NDHKGT M@OPMI OC@ M@NPGO JA GPSNBU() ?DM@>OGT:
JNQPSU TUE.TUSJOH;
// ...
TUSVDU 5JNF0G%BZ
\
// ...
TUSJOH UP4USJOH()
\
SFUVSO GPSNBU("%02T:%02T", IPVS, NJOVUF);
^
^
#7?47D FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
277
+JO@ OC<O UP4USJOH() M@OPMIN OC@ M@KM@N@IO<ODJI JA JIGT F:;E J=E@>O. 1C@ M@NO JA OC@ JPOKPO DN
C<I?G@? =T XSJUFGMO(): &O ><GGN OC@ UP4USJOH() H@H=@M API>ODJI AJM OC@ ORJ J=E@>ON
N@K<M<O@GT, KMDION OC@ '-' >C<M<>O@M DI =@OR@@I, <I? ADI<GGT O@MHDI<O@N OC@ GDI@:
10:00-22:00
1C@ ?@ADIDODJI JA UP4USJOH() OC<O DN @SKG<DI@? <=JQ@ ?J@N IJO O<F@ <IT K<M<H@O@MN; DO NDHKGT
KMJ?P>@N < TUSJOH <I? M@OPMIN DO. AI <GO@MI<ODQ@ ?@ADIDODJI JA UP4USJOH() O<F@N < EFMFHBUF
K<M<H@O@M. 4@ RDGG N@@ OC<O ?@ADIDODJI G<O@M DI OC@ #PI>ODJI -JDIO@MN, !@G@B<O@N, <I? )<H=?<N
>C<KO@M.
Example: JODSFNFOU() member function 52.1.2
)@O'N ?@ADI@ < H@H=@M API>ODJI OC<O <??N < ?PM<ODJI OJ 5JNF0G%BZ J=E@>ON.
B@AJM@ ?JDIB OC<O, G@O'N ADMNO >JMM@>O < ?@NDBI AG<R OC<O R@ C<Q@ =@@I GDQDIB RDOC. 4@ C<Q@
N@@I DI OC@ 0OMP>ON >C<KO@M OC<O <??DIB ORJ 5JNF0G%BZ J=E@>ON DI BEE%VSBUJPO() DN IJO <
H@<IDIBAPG JK@M<ODJI:
5JNF0G%BZ BEE%VSBUJPO(JO 5JNF0G%BZ TUBSU,
JO 5JNF0G%BZ EVSBUJPO) // NFBOJOHMFTT
\
// ...
^
4C<O DN I<OPM<G OJ <?? OJ < KJDIO DI ODH@ DN 6GD3F;A@. #JM @S<HKG@, <??DIB OC@ OM<Q@G ?PM<ODJI
OJ OC@ ?@K<MOPM@ ODH@ RJPG? M@NPGO DI OC@ <MMDQ<G ODH@.
,I OC@ JOC@M C<I?, NP=OM<>ODIB ORJ KJDION DI ODH@ DN < I<OPM<G JK@M<ODJI, DI RCD>C ><N@ OC@
M@NPGO RJPG? =@ < 6GD3F;A@.
1C@ AJGGJRDIB KMJBM<H ?@ADI@N < %VSBUJPO NOMP>O RDOC HDIPO@-KM@>DNDJI, <I? <I
BEE%VSBUJPO() API>ODJI OC<O PN@N DO:
TUSVDU %VSBUJPO
\
JOU NJOVUF;
^
5JNF0G%BZ BEE%VSBUJPO(JO 5JNF0G%BZ TUBSU,
JO %VSBUJPO EVSBUJPO)
\
// #FHJO XJUI B DPQZ PG TUBSU
5JNF0G%BZ SFTVMU = TUBSU;
// "EE UIF EVSBUJPO UP JU
SFTVMU.NJOVUF += EVSBUJPO.NJOVUF;
// 5BLF DBSF PG PWFSGMPXT
SFTVMU.IPVS += SFTVMU.NJOVUF / 60;
SFTVMU.NJOVUF %= 60;
SFTVMU.IPVS %= 24;
SFUVSO SFTVMU;
^
VOJUUFTU
\
// " USJWJBM UFTU
BTTFSU(BEE%VSBUJPO(5JNF0G%BZ(10, 30), %VSBUJPO(10))
== 5JNF0G%BZ(10, 40));
// " UJNF BU NJEOJHIU
#7?47D FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
278
BTTFSU(BEE%VSBUJPO(5JNF0G%BZ(23, 9), %VSBUJPO(51))
== 5JNF0G%BZ(0, 0));
// " UJNF JO UIF OFYU EBZ
BTTFSU(BEE%VSBUJPO(5JNF0G%BZ(17, 45), %VSBUJPO(8 * 60))
== 5JNF0G%BZ(1, 45));
^
)@O'N M@?@ADI@ < NDHDG<M API>ODJI OCDN ODH@ <N < H@H=@M API>ODJI. BEE%VSBUJPO() C<N =@@I
KMJ?P>DIB < I@R J=E@>O <N DON M@NPGO. )@O'N ?@ADI@ <I JODSFNFOU() H@H=@M API>ODJI OC<O RDGG
?DM@>OGT HJ?DAT F:;E J=E@>O DINO@<?:
TUSVDU %VSBUJPO
\
JOU NJOVUF;
^
TUSVDU 5JNF0G%BZ
\
JOU IPVS;
JOU NJOVUF;
TUSJOH UP4USJOH()
\
SFUVSO GPSNBU("%02T:%02T", IPVS, NJOVUF);
^
WPJE JODSFNFOU(JO %VSBUJPO EVSBUJPO)
\
NJOVUF += EVSBUJPO.NJOVUF;
IPVS += NJOVUF / 60;
NJOVUF %= 60;
IPVS %= 24;
^
VOJUUFTU
\
BVUP UJNF = 5JNF0G%BZ(10, 30);
// " USJWJBM UFTU
UJNF.JODSFNFOU(%VSBUJPO(10));
BTTFSU(UJNF == 5JNF0G%BZ(10, 40));
// 15 IPVST MBUFS NVTU CF JO UIF OFYU EBZ
UJNF.JODSFNFOU(%VSBUJPO(15 * 60));
BTTFSU(UJNF == 5JNF0G%BZ(1, 40));
// 22 IPVST 20 NJOVUFT MBUFS NVTU CF NJEOJHIU
UJNF.JODSFNFOU(%VSBUJPO(22 * 60 + 20));
BTTFSU(UJNF == 5JNF0G%BZ(0, 0));
^
^
JODSFNFOU() DI>M@H@ION OC@ Q<GP@ JA OC@ J=E@>O =T OC@ NK@>DAD@? <HJPIO JA ?PM<ODJI. &I <
G<O@M >C<KO@M R@ RDGG N@@ CJR OC@ AB7D3FAD AH7D>A36;@9 A@<OPM@ JA ! RDGG H<F@ DO KJNND=G@ OJ
<?? < ?PM<ODJI =T OC@ += JK@M<OJM NTIO<S:
UJNF += %VSBUJPO(10); // UP CF FYQMBJOFE JO B MBUFS DIBQUFS
AGNJ IJO@ OC<O VOJUUFTU =GJ>FN ><I =@ RMDOO@I DIND?@ TUSVDU ?@ADIDODJIN <N R@GG, HJNOGT AJM
O@NODIB H@H=@M API>ODJIN. &O DN NODGG KJNND=G@ OJ HJQ@ NP>C VOJUUFTU =GJ>FN JPOND?@ JA OC@
=J?T JA OC@ NOMP>O:
#7?47D FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
279
TUSVDU 5JNF0G%BZ
\
// ... TUSVDU EFGJOJUJPO ...
^
VOJUUFTU
\
// ... TUSVDU UFTUT ...
^
Exercises
52.2
1. A?? < EFDSFNFOU() H@H=@M API>ODJI OJ 5JNF0G%BZ, RCD>C NCJPG? M@?P>@ OC@ ODH@ =T OC@
NK@>DAD@? <HJPIO JA ?PM<ODJI. 0DHDG<M OJ JODSFNFOU(), DO NCJPG? G@67D8>AI OJ OC@ KM@QDJPN
?<T RC@I OC@M@ DN IJO @IJPBC ODH@ DI OC@ >PMM@IO ?<T. #JM @S<HKG@, NP=OM<>ODIB 10
HDIPO@N AMJH 00:05 NCJPG? M@NPGO DI 23:55.
&I JOC@M RJM?N, DHKG@H@IO EFDSFNFOU() OJ K<NN OC@ AJGGJRDIB PIDO O@NON:
TUSVDU 5JNF0G%BZ
\
// ...
WPJE EFDSFNFOU(JO %VSBUJPO EVSBUJPO)
\
// ... QMFBTF JNQMFNFOU UIJT GVODUJPO ...
^
VOJUUFTU
\
BVUP UJNF = 5JNF0G%BZ(10, 30);
// " USJWJBM UFTU
UJNF.EFDSFNFOU(%VSBUJPO(12));
BTTFSU(UJNF == 5JNF0G%BZ(10, 18));
// 3 EBZT BOE 11 IPVST FBSMJFS
UJNF.EFDSFNFOU(%VSBUJPO(3 * 24 * 60 + 11 * 60));
BTTFSU(UJNF == 5JNF0G%BZ(23, 18));
// 23 IPVST BOE 18 NJOVUFT FBSMJFS NVTU CF NJEOJHIU
UJNF.EFDSFNFOU(%VSBUJPO(23 * 60 + 18));
BTTFSU(UJNF == 5JNF0G%BZ(0, 0));
// 1 NJOVUF FBSMJFS
UJNF.EFDSFNFOU(%VSBUJPO(1));
BTTFSU(UJNF == 5JNF0G%BZ(23, 59));
^
^
2. JIQ@MO .FFUJOH, .FBM, <I? %BJMZ1MBO JQ@MGJ<?N JA JOGP() OJ UP4USJOH() H@H=@M
API>ODJIN <N R@GG. (0@@ OC@ @S@M>DN@ NJGPODJIN JA OC@ #PI>ODJI ,Q@MGJ<?DIB >C<KO@M AJM
OC@DM JOGP() JQ@MGJ<?N.)
6JP RDGG IJOD>@ OC<O DI <??DODJI OJ H<FDIB OC@DM M@NK@>ODQ@ NOMP>ON HJM@ >JIQ@ID@IO,
OC@ DHKG@H@IO<ODJIN JA OC@ UP4USJOH() H@H=@M API>ODJIN RDGG <GG >JINDNO JA NDIBG@
GDI@N.
... F:7 EA>GF;A@E
#7?47D FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
280
DPOTU SFG Parameters and DPOTU Member Functions 53
1CDN >C<KO@M DN <=JPO CJR K<M<H@O@MN <I? H@H=@M API>ODJIN <M@ H<MF@? <N DPOTU NJ OC<O
OC@T ><I =@ PN@? RDOC JNNVUBCMF Q<MD<=G@N <N R@GG. AN R@ C<Q@ <GM@<?T >JQ@M@? DPOTU
K<M<H@O@MN DI @<MGD@M >C<KO@MN, NJH@ DIAJMH<ODJI DI OCDN >C<KO@M RDGG =@ < M@QD@R JA NJH@ JA
OC@ A@<OPM@N OC<O TJP <GM@<?T FIJR.
AGOCJPBC OC@ @S<HKG@N DI OCDN >C<KO@M PN@ JIGT NOMP>ON, DPOTU H@H=@M API>ODJIN <KKGT OJ
>G<NN@N <N R@GG.
JNNVUBCMF objects
53.1
4@ C<Q@ <GM@<?T N@@I OC<O DO DN IJO KJNND=G@ OJ HJ?DAT JNNVUBCMF Q<MD<=G@N:
JNNVUBCMF SFBEJOH5JNF = 5JNF0G%BZ(15, 0);
SFBEJOH5JNF ><IIJO =@ HJ?DAD@?:
SFBEJOH5JNF = 5JNF0G%BZ(16, 0); // j DPNQJMBUJPO &3303
SFBEJOH5JNF.NJOVUF += 10; // j DPNQJMBUJPO &3303
1C@ >JHKDG@M ?J@N IJO <GGJR HJ?DATDIB JNNVUBCMF J=E@>ON DI <IT R<T.
SFG parameters that are not DPOTU
53.2
4@ C<Q@ N@@I OCDN >JI>@KO @<MGD@M DI OC@ #PI>ODJI -<M<H@O@MN >C<KO@M. -<M<H@O@MN OC<O <M@
H<MF@? <N SFG ><I AM@@GT =@ HJ?DAD@? =T OC@ API>ODJI. #JM OC<O M@<NJI, @Q@I DA OC@ API>ODJI
?J@N IJO <>OP<GGT HJ?DAT OC@ K<M<H@O@M, OC@ >JHKDG@M ?J@N IJO <GGJR K<NNDIB JNNVUBCMF
J=E@>ON <N OC<O K<M<H@O@M:
/* "MUIPVHI OPU CFJOH NPEJGJFE CZ UIF GVODUJPO, 'EVSBUJPO'
* JT OPU NBSLFE BT 'DPOTU' */
JOU UPUBM4FDPOET(SFG %VSBUJPO EVSBUJPO)
\
SFUVSO 60 * EVSBUJPO.NJOVUF;
^
// ...
JNNVUBCMF XBSN6Q5JNF = %VSBUJPO(3);
UPUBM4FDPOET(XBSN6Q5JNF); // j DPNQJMBUJPO &3303
1C@ >JHKDG@M ?J@N IJO <GGJR K<NNDIB OC@ JNNVUBCMF XBSN6Q5JNF OJ UPUBM4FDPOET =@><PN@
OC<O API>ODJI ?J@N IJO BP<M<IO@@ OC<O OC@ K<M<H@O@M RDGG IJO =@ HJ?DAD@?.
DPOTU SFG parameters
53.3
DPOTU SFG H@<IN OC<O OC@ K<M<H@O@M DN IJO HJ?DAD@? =T OC@ API>ODJI:
JOU UPUBM4FDPOET(DPOTU SFG %VSBUJPO EVSBUJPO)
\
SFUVSO 60 * EVSBUJPO.NJOVUF;
^
// ...
JNNVUBCMF XBSN6Q5JNF = %VSBUJPO(3);
UPUBM4FDPOET(XBSN6Q5JNF); // j OPX DPNQJMFT
5A@EF D78 &3D3?7F7DE 3@6 5A@EF #7?47D FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
281
0P>C API>ODJIN ><I M@>@DQ@ JNNVUBCMF J=E@>ON <N K<M<H@O@MN =@><PN@ OC@ DHHPO<=DGDOT JA OC@
J=E@>O DN @IAJM>@? =T OC@ >JHKDG@M:
JOU UPUBM4FDPOET(DPOTU SFG %VSBUJPO EVSBUJPO)
\
EVSBUJPO.NJOVUF = 7; // j DPNQJMBUJPO &3303
// ...
^
Non-DPOTU member functions
53.4
AN R@ C<Q@ N@@I RDOC OC@ 5JNF0G%BZ.JODSFNFOU H@H=@M API>ODJI, J=E@>ON ><I =@ HJ?DAD@?
OCMJPBC H@H=@M API>ODJIN <N R@GG. JODSFNFOU() HJ?DAD@N OC@ H@H=@MN JA OC@ J=E@>O OC<O DO DN
><GG@? JI:
TUSVDU 5JNF0G%BZ
\
// ...
WPJE JODSFNFOU(JO %VSBUJPO EVSBUJPO)
\
NJOVUF += EVSBUJPO.NJOVUF;
IPVS += NJOVUF / 60;
NJOVUF %= 60;
IPVS %= 24;
^
// ...
^
// ...
BVUP TUBSU = 5JNF0G%BZ(5, 30);
TUBSU.JODSFNFOU(%VSBUJPO(30)); // 'TUBSU' HFUT NPEJGJFE
DPOTU member functions
53.5
0JH@ H@H=@M API>ODJIN ?J IJO H<F@ <IT HJ?DAD><ODJIN OJ OC@ J=E@>O OC<O OC@T <M@ ><GG@? JI.
AI @S<HKG@ JA NP>C < API>ODJI DN UP4USJOH():
TUSVDU 5JNF0G%BZ
\
// ...
TUSJOH UP4USJOH()
\
SFUVSO GPSNBU("%02T:%02T", IPVS, NJOVUF);
^
// ...
^
0DI>@ OC@ RCJG@ KPMKJN@ JA UP4USJOH() DN OJ M@KM@N@IO OC@ J=E@>O DI NOMDIB AJMH<O <ITR<T, DO
NCJPG? IJO HJ?DAT OC@ J=E@>O.
1C@ A<>O OC<O < H@H=@M API>ODJI ?J@N IJO HJ?DAT OC@ J=E@>O DN ?@>G<M@? =T OC@ DPOTU F@TRJM?
<AO@M OC@ K<M<H@O@M GDNO:
TUSVDU 5JNF0G%BZ
\
// ...
TUSJOH UP4USJOH() DPOTU
\
SFUVSO GPSNBU("%02T:%02T", IPVS, NJOVUF);
^
^
5A@EF D78 &3D3?7F7DE 3@6 5A@EF #7?47D FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
282
1C<O DPOTU BP<M<IO@@N OC<O OC@ J=E@>O DON@GA DN IJO BJDIB OJ =@ HJ?DAD@? =T OC@ H@H=@M
API>ODJI. AN < >JIN@LP@I>@, UP4USJOH() H@H=@M API>ODJI DN <GGJR@? OJ =@ ><GG@? @Q@I JI
JNNVUBCMF J=E@>ON. ,OC@MRDN@, OC@ NOMP>O'N UP4USJOH() RJPG? IJO =@ ><GG@?:
TUSVDU 5JNF0G%BZ
\
// ...
// *OGFSJPS EFTJHO: /PU NBSLFE BT 'DPOTU'
TUSJOH UP4USJOH()
\
SFUVSO GPSNBU("%02T:%02T", IPVS, NJOVUF);
^
^
// ...
JNNVUBCMF TUBSU = 5JNF0G%BZ(5, 30);
XSJUFMO(TUBSU); // 5JNF0G%BZ.UP4USJOH() JT OPU DBMMFE!
1C@ JPOKPO DN IJO OC@ @SK@>O@? 05:30, DI?D><ODIB OC<O < B@I@MD> API>ODJI B@ON ><GG@? DINO@<? JA
5JNF0G%BZ.UP4USJOH:
JNNVUBCMF(5JNF0G%BZ)(5, 30)
#PMOC@M, ><GGDIB UP4USJOH() JI <I JNNVUBCMF J=E@>O @SKGD>DOGT RJPG? ><PN@ < >JHKDG<ODJI
@MMJM:
BVUP T = TUBSU.UP4USJOH(); // j DPNQJMBUJPO &3303
A>>JM?DIBGT, OC@ UP4USJOH() API>ODJIN OC<O R@ C<Q@ ?@ADI@? DI OC@ KM@QDJPN >C<KO@M C<Q@ <GG
=@@I ?@NDBI@? DI>JMM@>OGT; OC@T NCJPG? C<Q@ =@@I H<MF@? <N DPOTU.
Note: ):7 const =7KIAD6 53@ 47 EB75;8;76 478AD7 F:7 678;@;F;A@ A8 F:7 8G@5F;A@ 3E I7>>:
// 5IF TBNF BT BCPWF
DPOTU TUSJOH UP4USJOH()
\
SFUVSO GPSNBU("%02T:%02T", IPVS, NJOVUF);
^
(;@57 F:;E H7DE;A@ ?3K 9;H7 F:7 ;@5ADD75F ;?BD7EE;A@ F:3F F:7 const ;E 3 B3DF A8 F:7 D7FGD@
FKB7, ! D75A??7@6 F:3F KAG EB75;8K ;F 38F7D F:7 B3D3?7F7D >;EF.
How to use
53.6
t 1J BDQ@ OC@ BP<M<IO@@ OC<O < K<M<H@O@M DN IJO HJ?DAD@? =T OC@ API>ODJI, H<MF OC<O
K<M<H@O@M <N JO, DPOTU, JM DPOTU SFG.
t *<MF H@H=@M API>ODJIN OC<O ?J IJO HJ?DAT OC@ J=E@>O <N DPOTU:
TUSVDU 5JNF0G%BZ
\
// ...
TUSJOH UP4USJOH() DPOTU
\
SFUVSO GPSNBU("%02T:%02T", IPVS, NJOVUF);
^
^
1CDN RJPG? H<F@ OC@ NOMP>O (JM >G<NN) HJM@ PN@APG =T M@HJQDIB <I PII@>@NN<MT
GDHDO<ODJI. 1C@ @S<HKG@N DI OC@ M@NO JA OC@ =JJF RDGG J=N@MQ@ OCDN BPD?@GDI@.
5A@EF D78 &3D3?7F7DE 3@6 5A@EF #7?47D FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
283
Constructor and Other Special Functions 54
AGOCJPBC OCDN >C<KO@M AJ>PN@N JIGT JI NOMP>ON, OC@ OJKD>N OC<O <M@ >JQ@M@? C@M@ <KKGT HJNOGT
OJ >G<NN@N <N R@GG. 1C@ ?DAA@M@I>@N RDGG =@ @SKG<DI@? DI G<O@M >C<KO@MN.
#JPM H@H=@M API>ODJIN JA NOMP>ON <M@ NK@>D<G =@><PN@ OC@T ?@ADI@ OC@ API?<H@IO<G JK@M<ODJIN
JA OC<O OTK@:
t JINOMP>OJM: UIJT()
t !@NOMP>OJM: _UIJT()
t -JNO=GDO: UIJT(UIJT)
t ANNDBIH@IO JK@M<OJM: PQ"TTJHO()
AGOCJPBC OC@N@ API?<H@IO<G JK@M<ODJIN <M@ C<I?G@? <POJH<OD><GGT AJM NOMP>ON, C@I>@ I@@? IJO
=@ ?@ADI@? =T OC@ KMJBM<HH@M, OC@T ><I =@ JQ@MMD??@I OJ H<F@ OC@ TUSVDU =@C<Q@ DI NK@>D<G
R<TN.
Constructor
54.1
1C@ M@NKJIND=DGDOT JA OC@ >JINOMP>OJM DN OJ KM@K<M@ <I J=E@>O AJM PN@ =T <NNDBIDIB <KKMJKMD<O@
Q<GP@N OJ DON H@H=@MN.
4@ C<Q@ <GM@<?T PN@? >JINOMP>OJMN DI KM@QDJPN >C<KO@MN. 4C@I OC@ I<H@ JA < OTK@ DN PN@?
GDF@ < API>ODJI, DO DN <>OP<GGT OC@ >JINOMP>OJM OC<O B@ON ><GG@?. 4@ ><I N@@ OCDN JI OC@ MDBCO-
C<I? ND?@ JA OC@ AJGGJRDIB GDI@:
BVUP CVT"SSJWBM = 5JNF0G%BZ(8, 30);
0DHDG<MGT, < 5>3EE J=E@>O DN =@DIB >JINOMP>O@? JI OC@ MDBCO C<I? ND?@ JA OC@ AJGGJRDIB GDI@:
BVUP WBSJBCMF = OFX 4PNF$MBTT();
1C@ <MBPH@ION OC<O <M@ NK@>DAD@? RDOCDI K<M@IOC@N@N >JMM@NKJI? OJ OC@ >JINOMP>OJM
K<M<H@O@MN. #JM @S<HKG@, OC@ Q<GP@N 8 <I? 30 <=JQ@ <M@ K<NN@? OJ OC@ 5JNF0G%BZ >JINOMP>OJM
<N DON K<M<H@O@MN.
Constructor syntax 54.1.1
!DAA@M@IO AMJH JOC@M API>ODJIN, >JINOMP>OJMN ?J IJO C<Q@ M@OPMI Q<GP@N. 1C@ I<H@ JA OC@
>JINOMP>OJM DN <GR<TN UIJT:
TUSVDU 4PNF4USVDU
\
// ...
UIJT(/* DPOTUSVDUPS QBSBNFUFST */)
\
// ... PQFSBUJPOT UIBU QSFQBSF UIF PCKFDU GPS VTF ...
^
^
1C@ >JINOMP>OJM K<M<H@O@MN DI>GP?@ DIAJMH<ODJI OC<O DN I@@?@? OJ H<F@ < PN@APG <I?
>JINDNO@IO J=E@>O.
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
284
Compiler-generated automatic constructor 54.1.2
AGG JA OC@ NOMP>ON OC<O R@ C<Q@ N@@I NJ A<M C<Q@ =@@I O<FDIB <?Q<IO<B@ JA < >JINOMP>OJM OC<O
C<N =@@I B@I@M<O@? <POJH<OD><GGT =T OC@ >JHKDG@M. 1C@ <POJH<OD> >JINOMP>OJM <NNDBIN OC@
K<M<H@O@M Q<GP@N OJ OC@ H@H=@MN DI OC@ JM?@M OC<O OC@T <M@ NK@>DAD@?.
AN TJP RDGG M@H@H=@M AMJH OC@ 0OMP>ON >C<KO@M, OC@ DIDOD<G Q<GP@N AJM OC@ OM<DGDIB H@H=@MN
I@@? IJO =@ NK@>DAD@?. 1C@ H@H=@MN OC<O <M@ IJO NK@>DAD@? B@O DIDOD<GDU@? =T OC@ .JOJU Q<GP@
JA OC@DM M@NK@>ODQ@ OTK@N. 1C@ .JOJU Q<GP@N JA < H@H=@M >JPG? =@ KMJQD?@? ?PMDIB OC@
?@ADIDODJI JA OC<O H@H=@M <AO@M OC@ = JK@M<OJM:
TUSVDU 5FTU
\
JOU NFNCFS = 42;
^
AGNJ >JIND?@MDIB OC@ 6783G>F B3D3?7F7D H3>G7E A@<OPM@ AMJH OC@ 3<MD<=G@ +PH=@M JA
-<M<H@O@MN >C<KO@M, R@ ><I DH<BDI@ OC<O OC@ <POJH<OD> >JINOMP>OJM AJM OC@ AJGGJRDIB TUSVDU
RJPG? =@ OC@ @LPDQ<G@IO JA OC@ AJGGJRDIB UIJT():
TUSVDU 5FTU
\
DIBS D;
JOU J;
EPVCMF E;
/* 5IF FRVJWBMFOU PG UIF DPNQJMFS-HFOFSBUFE BVUPNBUJD
* DPOTUSVDUPS (/PUF: 5IJT JT POMZ GPS EFNPOTUSBUJPO; UIF
* GPMMPXJOH DPOTUSVDUPS XPVME OPU BDUVBMMZ CF DBMMFE
* XIFO EFGBVMU-DPOTUSVDUJOH UIF PCKFDU BT 5FTU().) */
UIJT(JO DIBS D@QBSBNFUFS = DIBS.JOJU,
JO JOU J@QBSBNFUFS = JOU.JOJU,
JO EPVCMF E@QBSBNFUFS = EPVCMF.JOJU)
\
D = D@QBSBNFUFS;
J = J@QBSBNFUFS;
E = E@QBSBNFUFS;
^
^
#JM HJNO NOMP>ON, OC@ >JHKDG@M-B@I@M<O@? >JINOMP>OJM DN NPAAD>D@IO: 2NP<GGT, KMJQD?DIB
<KKMJKMD<O@ Q<GP@N AJM @<>C H@H=@M DN <GG OC<O DN I@@?@? AJM J=E@>ON OJ =@ >JINOMP>O@?.
Accessing the members by UIJT. 54.1.3
1J <QJD? HDSDIB OC@ K<M<H@O@MN RDOC OC@ H@H=@MN, OC@ K<M<H@O@M I<H@N <=JQ@ C<?
@QBSBNFUFS <KK@I?@? OJ OC@DM I<H@N. 1C@M@ RJPG? =@ >JHKDG<ODJI @MMJMN RDOCJPO ?JDIB OC<O:
TUSVDU 5FTU
\
DIBS D;
JOU J;
EPVCMF E;
UIJT(JO DIBS D = DIBS.JOJU,
JO JOU J = JOU.JOJU,
JO EPVCMF E = EPVCMF.JOJU)
\
// "O BUUFNQU UP BTTJHO BO 'JO' QBSBNFUFS UP JUTFMG!
D = D; // j DPNQJMBUJPO &3303
J = J;
E = E;
^
^
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
285
1C@ M@<NJI DN; D <GJI@ RJPG? H@<I OC@ K<M<H@O@M, IJO OC@ H@H=@M, <I? <N OC@ K<M<H@O@MN
<=JQ@ <M@ ?@ADI@? <N JO, OC@T ><IIJO =@ HJ?DAD@?:
&SSPS: WBSJBCMF EFOFNF.5FTU.UIJT.D DBOOPU NPEJGZ DPOTU
A NJGPODJI DN OJ KM@K@I? OC@ H@H=@M I<H@N RDOC UIJT.. &IND?@ H@H=@M API>ODJIN, UIJT
H@<IN "OCDN J=E@>O", H<FDIB UIJT.D H@<I "OC@ > H@H=@M JA OCDN J=E@>O":
UIJT(JO DIBS D = DIBS.JOJU,
JO JOU J = JOU.JOJU,
JO EPVCMF E = EPVCMF.JOJU)
\
UIJT.D = D;
UIJT.J = J;
UIJT.E = E;
^
+JR D <GJI@ H@<IN OC@ K<M<H@O@M <I? UIJT.D H@<IN OC@ H@H=@M, <I? OC@ >J?@ >JHKDG@N
<I? RJMFN <N @SK@>O@?: 1C@ H@H=@M D B@ON DIDOD<GDU@? =T OC@ Q<GP@ JA OC@ K<M<H@O@M D.
User-defined constructors 54.1.4
& C<Q@ ?@N>MD=@? OC@ =@C<QDJM JA OC@ >JHKDG@M-B@I@M<O@? >JINOMP>OJM. 0DI>@ OC<O >JINOMP>OJM
DN NPDO<=G@ AJM HJNO ><N@N, OC@M@ DN IJ I@@? OJ ?@ADI@ < >JINOMP>OJM =T C<I?.
0ODGG, OC@M@ <M@ ><N@N RC@M@ >JINOMP>ODIB <I J=E@>O DIQJGQ@N HJM@ >JHKGD><O@? JK@M<ODJIN
OC<I <NNDBIDIB Q<GP@N OJ @<>C H@H=@M DI JM?@M. AN <I @S<HKG@, G@O'N >JIND?@M %VSBUJPO AMJH
OC@ @<MGD@M >C<KO@MN:
TUSVDU %VSBUJPO
\
JOU NJOVUF;
^
1C@ >JHKDG@M-B@I@M<O@? >JINOMP>OJM DN NPAAD>D@IO AJM OCDN NDIBG@-H@H=@M NOMP>O:
UJNF.EFDSFNFOU(%VSBUJPO(12));
0DI>@ OC<O >JINOMP>OJM O<F@N OC@ ?PM<ODJI DI HDIPO@N, OC@ KMJBM<HH@MN RJPG? NJH@ODH@N
I@@? OJ H<F@ ><G>PG<ODJIN:
// 23 IPVST BOE 18 NJOVUFT FBSMJFS
UJNF.EFDSFNFOU(%VSBUJPO(23 * 60 + 18));
// 22 IPVST BOE 20 NJOVUFT MBUFS
UJNF.JODSFNFOU(%VSBUJPO(22 * 60 + 20));
1J @GDHDI<O@ OC@ I@@? AJM OC@N@ ><G>PG<ODJIN, R@ ><I ?@NDBI < %VSBUJPO >JINOMP>OJM OC<O
O<F@N ORJ K<M<H@O@MN <I? H<F@N OC@ ><G>PG<ODJI <POJH<OD><GGT:
TUSVDU %VSBUJPO
\
JOU NJOVUF;
UIJT(JOU IPVS, JOU NJOVUF)
\
UIJT.NJOVUF = IPVS * 60 + NJOVUF;
^
^
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
286
0DI>@ CJPM <I? HDIPO@ <M@ IJR N@K<M<O@ K<M<H@O@MN, OC@ PN@MN NDHKGT KMJQD?@ OC@DM Q<GP@N
RDOCJPO I@@?DIB OJ H<F@ OC@ ><G>PG<ODJI OC@HN@GQ@N:
// 23 IPVST BOE 18 NJOVUFT FBSMJFS
UJNF.EFDSFNFOU(%VSBUJPO(23, 18));
// 22 IPVST BOE 20 NJOVUFT MBUFS
UJNF.JODSFNFOU(%VSBUJPO(22, 20));
User-defined constructor disables compiler-generated constructor 54.1.5
A >JINOMP>OJM OC<O DN ?@ADI@? =T OC@ KMJBM<HH@M H<F@N NJH@ PN@N JA OC@ >JHKDG@M-B@I@M<O@?
>JINOMP>OJM DIQ<GD?: ,=E@>ON ><IIJO =@ >JINOMP>O@? =T 6783G>F B3D3?7F7D H3>G7E <ITHJM@. #JM
@S<HKG@, OMTDIB OJ >JINOMP>O %VSBUJPO =T < NDIBG@ K<M<H@O@M DN < >JHKDG<ODJI @MMJM:
UJNF.EFDSFNFOU(%VSBUJPO(12)); // j DPNQJMBUJPO &3303
<GGDIB OC@ >JINOMP>OJM RDOC < NDIBG@ K<M<H@O@M ?J@N IJO H<O>C OC@ KMJBM<HH@M'N
>JINOMP>OJM <I? OC@ >JHKDG@M-B@I@M<O@? >JINOMP>OJM DN ?DN<=G@?.
,I@ NJGPODJI DN OJ AH7D>A36 OC@ >JINOMP>OJM =T KMJQD?DIB <IJOC@M >JINOMP>OJM OC<O O<F@N EPNO
JI@ K<M<H@O@M:
TUSVDU %VSBUJPO
\
JOU NJOVUF;
UIJT(JOU IPVS, JOU NJOVUF)
\
UIJT.NJOVUF = IPVS * 60 + NJOVUF;
^
UIJT(JOU NJOVUF)
\
UIJT.NJOVUF = NJOVUF;
^
^
A PN@M-?@ADI@? >JINOMP>OJM ?DN<=G@N >JINOMP>ODIB J=E@>ON =T OC@ \ ^ NTIO<S <N R@GG:
%VSBUJPO EVSBUJPO = \ 5 ^; // j DPNQJMBUJPO &3303
&IDOD<GDUDIB RDOCJPO KMJQD?DIB <IT K<M<H@O@M DN NODGG Q<GD?:
BVUP E = %VSBUJPO(); // DPNQJMFT
1C@ M@<NJI DN, DI !, OC@ .JOJU Q<GP@ JA @Q@MT OTK@ HPNO =@ FIJRI <O >JHKDG@ ODH@. 1C@ Q<GP@
JA E <=JQ@ DN @LP<G OJ OC@ DIDOD<G Q<GP@ JA %VSBUJPO:
BTTFSU(E == %VSBUJPO.JOJU);
TUBUJD PQ$BMM instead of the default constructor 54.1.6
B@><PN@ OC@ DIDOD<G Q<GP@ JA @Q@MT OTK@ HPNO =@ FIJRI <O >JHKDG@ ODH@, DO DN DHKJNND=G@ OJ
?@ADI@ OC@ ?@A<PGO >JINOMP>OJM @SKGD>DOGT.
)@O'N >JIND?@M OC@ AJGGJRDIB >JINOMP>OJM OC<O OMD@N OJ KMDIO NJH@ DIAJMH<ODJI @Q@MT ODH@ <I
J=E@>O JA OC<O OTK@ DN >JINOMP>O@?:
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
287
TUSVDU 5FTU
\
UIJT() // j DPNQJMBUJPO &3303
\
XSJUFMO("" 5FTU PCKFDU JT CFJOH DPOTUSVDUFE.");
^
^
1C@ >JHKDG@M JPOKPO:
&SSPS: DPOTUSVDUPS EFOFNF.%FOFNF.UIJT EFGBVMU DPOTUSVDUPS GPS
TUSVDUT POMZ BMMPXFE XJUI !EJTBCMF BOE OP CPEZ
Note: ,7 I;>> E77 ;@ >3F7D 5:3BF7DE F:3F ;F ;E BAEE;4>7 FA 678;@7 F:7 6783G>F 5A@EFDG5FAD 8AD
5>3EE7E.
AN < RJMF<MJPI?, < IJ-K<M<H@O@M TUBUJD PQ$BMM() ><I =@ PN@? AJM >JINOMP>ODIB J=E@>ON
RDOCJPO KMJQD?DIB <IT K<M<H@O@MN. +JO@ OC<O OCDN C<N IJ @AA@>O JI OC@ .JOJU Q<GP@ JA OC@
OTK@.
#JM OCDN OJ RJMF, TUBUJD PQ$BMM() HPNO >JINOMP>O <I? M@OPMI <I J=E@>O JA OC<O NOMP>O OTK@:
JNQPSU TUE.TUEJP;
TUSVDU 5FTU
\
TUBUJD 5FTU PQ$BMM()
\
XSJUFMO("" 5FTU PCKFDU JT CFJOH DPOTUSVDUFE.");
5FTU UFTU;
SFUVSO UFTU;
^
^
WPJE NBJO()
\
BVUP UFTU = 5FTU();
^
1C@ 5FTU() ><GG DI NBJO() @S@>PO@N TUBUJD PQ$BMM():
" 5FTU PCKFDU JT CFJOH DPOTUSVDUFE.
+JO@ OC<O DO DN IJO KJNND=G@ OJ OTK@ 5FTU() DIND?@ TUBUJD PQ$BMM(). 1C<O NTIO<S RJPG?
@S@>PO@ TUBUJD PQ$BMM() <N R@GG <I? ><PN@ <I DIADIDO@ M@>PMNDJI:
TUBUJD 5FTU PQ$BMM()
\
XSJUFMO("" 5FTU PCKFDU JT CFJOH DPOTUSVDUFE.");
SFUVSO 5FTU(); // j $BMMT 'TUBUJD PQ$BMM()' BHBJO
1C@ JPOKPO:
" 5FTU PCKFDU JT CFJOH DPOTUSVDUFE.
" 5FTU PCKFDU JT CFJOH DPOTUSVDUFE.
" 5FTU PCKFDU JT CFJOH DPOTUSVDUFE.
... l SFQFBUT UIF TBNF NFTTBHF
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
288
Calling other constructors 54.1.7
JINOMP>OJMN ><I ><GG JOC@M >JINOMP>OJMN OJ <QJD? >J?@ ?PKGD><ODJI. AGOCJPBC %VSBUJPO DN OJJ
NDHKG@ OJ ?@HJINOM<O@ CJR PN@APG OCDN A@<OPM@ DN, OC@ AJGGJRDIB NDIBG@-K<M<H@O@M >JINOMP>OJM
O<F@N <?Q<IO<B@ JA OC@ ORJ-K<M<H@O@M >JINOMP>OJM:
UIJT(JOU IPVS, JOU NJOVUF)
\
UIJT.NJOVUF = IPVS * 60 + NJOVUF;
^
UIJT(JOU NJOVUF)
\
UIJT(0, NJOVUF); // DBMMT UIF PUIFS DPOTUSVDUPS
^
1C@ >JINOMP>OJM OC<O JIGT O<F@N OC@ HDIPO@ Q<GP@ ><GGN OC@ JOC@M >JINOMP>OJM =T K<NNDIB 0 <N
OC@ Q<GP@ JA CJPM.
Warning: ):7D7 ;E 3 67E;9@ 8>3I ;@ F:7 Duration 5A@EFDG5FADE 34AH7 4753GE7 F:7 ;@F7@F;A@ ;E
@AF 5>73D I:7@ F:7 A4<75FE 3D7 5A@EFDG5F76 4K 3 E;@9>7 B3D3?7F7D:
// 10 IPVST PS 10 NJOVUFT
BVUP USBWFM%VSBUJPO = %VSBUJPO(10);
AGOCJPBC DO DN KJNND=G@ OJ ?@O@MHDI@ =T M@<?DIB OC@ ?J>PH@IO<ODJI JM OC@ >J?@ JA OC@ NOMP>O
OC<O OC@ K<M<H@O@M <>OP<GGT H@<IN "10 HDIPO@N," DO DN <I DI>JINDNO@I>T <N OC@ ADMNO K<M<H@O@M
JA OC@ ORJ-K<M<H@O@M >JINOMP>OJM DN :AGDE.
0P>C ?@NDBI HDNO<F@N <M@ ><PN@N JA =PBN <I? HPNO =@ <QJD?@?.
Immutability of constructor parameters 54.1.8
&I OC@ &HHPO<=DGDOT >C<KO@M R@ C<Q@ N@@I OC<O DO DN IJO @<NT OJ ?@>D?@ RC@OC@M K<M<H@O@MN JA
M@A@M@I>@ OTK@N NCJPG? =@ ?@ADI@? <N DPOTU JM JNNVUBCMF. AGOCJPBC OC@ N<H@ >JIND?@M<ODJIN
<KKGT AJM >JINOMP>OJM K<M<H@O@MN <N R@GG, JNNVUBCMF DN PNP<GGT < =@OO@M >CJD>@ AJM
>JINOMP>OJM K<M<H@O@MN.
1C@ M@<NJI DN, DO DN >JHHJI OJ <NNDBI OC@ K<M<H@O@MN OJ H@H=@MN OJ =@ PN@? <O < G<O@M ODH@.
4C@I < K<M<H@O@M DN IJO JNNVUBCMF, OC@M@ DN IJ BP<M<IO@@ OC<O OC@ JMDBDI<G Q<MD<=G@ RDGG IJO
>C<IB@ =T OC@ ODH@ OC@ H@H=@M B@ON PN@?.
)@O'N >JIND?@M < >JINOMP>OJM OC<O O<F@N < ADG@ I<H@ <N < K<M<H@O@M. 1C@ ADG@ I<H@ RDGG =@ PN@?
G<O@M JI RC@I RMDODIB NOP?@IO BM<?@N. A>>JM?DIB OJ OC@ BPD?@GDI@N DI OC@ &HHPO<=DGDOT
>C<KO@M, OJ =@ HJM@ PN@APG, G@O'N <NNPH@ OC<O OC@ >JINOMP>OJM K<M<H@O@M DN ?@ADI@? <N
DPOTU DIBS<>:
JNQPSU TUE.TUEJP;
TUSVDU 4UVEFOU
\
DPOTU DIBS<> GJMF/BNF;
JOU<> HSBEFT;
UIJT(DPOTU DIBS<> GJMF/BNF)
\
UIJT.GJMF/BNF = GJMF/BNF;
^
WPJE TBWF()
\
BVUP GJMF = 'JMF(GJMF/BNF.JEVQ, "X");
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
289
GJMF.XSJUFMO("5IF HSBEFT PG UIF TUVEFOU:");
GJMF.XSJUFMO(HSBEFT);
^
// ...
^
WPJE NBJO()
\
DIBS<> GJMF/BNF;
GJMF/BNF _= "TUVEFOU@HSBEFT";
BVUP TUVEFOU = 4UVEFOU(GJMF/BNF);
// ...
/* "TTVNF UIF GJMF/BNF WBSJBCMF JT NPEJGJFE MBUFS PO
* QFSIBQT VOJOUFOUJPOBMMZ (BMM PG JUT DIBSBDUFST BSF
* CFJOH TFU UP '"' IFSF): */
GJMF/BNF<> = '"';
// ...
/* 5IF HSBEFT XPVME CF XSJUUFO UP UIF XSPOH GJMF: */
TUVEFOU.TBWF();
^
1C@ KMJBM<H <=JQ@ N<Q@N OC@ BM<?@N JA OC@ NOP?@IO PI?@M < ADG@ I<H@ OC<O >JINDNON JA A
>C<M<>O@MN, IJO OJ "TUVEFOU@HSBEFT". #JM OC<O M@<NJI, NJH@ODH@N DO DN HJM@ NPDO<=G@ OJ ?@ADI@
>JINOMP>OJM K<M<H@O@MN <I? H@H=@MN JA M@A@M@I>@ OTK@N <N JNNVUBCMF. 4@ FIJR OC<O OCDN DN
@<NT AJM NOMDIBN =T PNDIB <GD<N@N GDF@ TUSJOH. 1C@ AJGGJRDIB >J?@ NCJRN OC@ K<MON JA OC@ NOMP>O
OC<O RJPG? I@@? OJ =@ HJ?DAD@?:
TUSVDU 4UVEFOU
\
TUSJOH GJMF/BNF;
// ...
UIJT(TUSJOH GJMF/BNF)
\
// ...
^
// ...
^
+JR OC@ PN@MN JA OC@ NOMP>O HPNO KMJQD?@ JNNVUBCMF NOMDIBN <I? <N < M@NPGO OC@ >JIAPNDJI
<=JPO OC@ I<H@ JA OC@ ADG@ RJPG? =@ KM@Q@IO@?.
Type conversions through single-parameter constructors 54.1.9
0DIBG@-K<M<H@O@M >JINOMP>OJMN ><I =@ OCJPBCO JA <N KMJQD?DIB < NJMO JA OTK@ >JIQ@MNDJI: 1C@T
KMJ?P>@ <I J=E@>O JA OC@ K<MOD>PG<M NOMP>O OTK@ NO<MODIB AMJH < >JINOMP>OJM K<M<H@O@M. #JM
@S<HKG@, OC@ AJGGJRDIB >JINOMP>OJM KMJ?P>@N < 4UVEFOU J=E@>O AMJH < TUSJOH:
TUSVDU 4UVEFOU
\
TUSJOH OBNF;
UIJT(TUSJOH OBNF)
\
UIJT.OBNF = OBNF;
^
^
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
290
UP() <I? DBTU J=N@MQ@ OCDN =@C<QDJM <N < 5A@H7DE;A@ <N R@GG. 1J N@@ @S<HKG@N JA OCDN, G@O'N
>JIND?@M OC@ AJGGJRDIB TBMVUF() API>ODJI. 0@I?DIB < TUSJOH K<M<H@O@M RC@I DO @SK@>ON <
4UVEFOU RJPG? I<OPM<GGT ><PN@ < >JHKDG<ODJI @MMJM:
WPJE TBMVUF(4UVEFOU TUVEFOU)
\
XSJUFMO(")FMMP ", TUVEFOU.OBNF);
^
// ...
TBMVUF("+BOF"); // j DPNQJMBUJPO &3303
,I OC@ JOC@M C<I?, <GG JA OC@ AJGGJRDIB GDI@N @INPM@ OC<O < 4UVEFOU J=E@>O DN >JINOMP>O@?
=@AJM@ ><GGDIB OC@ API>ODJI:
JNQPSU TUE.DPOW;
// ...
TBMVUF(4UVEFOU("+BOF"));
TBMVUF(UP!4UVEFOU("+FBO"));
TBMVUF(DBTU(4UVEFOU)"+JN");
UP <I? DBTU O<F@ <?Q<IO<B@ JA OC@ NDIBG@-K<M<H@O@M >JINOMP>OJM =T >JINOMP>ODIB < O@HKJM<MT
4UVEFOU J=E@>O <I? ><GGDIB TBMVUF() RDOC OC<O J=E@>O.
Destructor
54.2
1C@ ?@NOMP>OJM DI>GP?@N OC@ JK@M<ODJIN OC<O HPNO =@ @S@>PO@? RC@I OC@ GDA@ODH@ JA <I J=E@>O
@I?N.
1C@ >JHKDG@M-B@I@M<O@? <POJH<OD> ?@NOMP>OJM @S@>PO@N OC@ ?@NOMP>OJMN JA <GG JA OC@ H@H=@MN
DI JM?@M. #JM OC<O M@<NJI, <N DO DN RDOC OC@ >JINOMP>OJM, OC@M@ DN IJ I@@? OJ ?@ADI@ < ?@NOMP>OJM
AJM HJNO NOMP>ON.
%JR@Q@M, NJH@ODH@N NJH@ NK@>D<G JK@M<ODJIN H<T I@@? OJ =@ @S@>PO@? RC@I <I J=E@>O'N
GDA@ODH@ @I?N. #JM @S<HKG@, <I JK@M<ODIB NTNO@H M@NJPM>@ OC<O OC@ J=E@>O JRIN H<T I@@? OJ
=@ M@OPMI@? OJ OC@ NTNO@H; < H@H=@M API>ODJI JA <IJOC@M J=E@>O H<T I@@? OJ =@ ><GG@?; <
N@MQ@M MPIIDIB NJH@RC@M@ JI OC@ I@ORJMF H<T I@@? OJ =@ IJODAD@? OC<O < >JII@>ODJI OJ DO DN
<=JPO OJ =@ O@MHDI<O@?; @O>.
1C@ I<H@ JA OC@ ?@NOMP>OJM DN _UIJT <I? EPNO GDF@ >JINOMP>OJMN, DO C<N IJ M@OPMI OTK@.
Destructor is executed automatically 54.2.1
1C@ ?@NOMP>OJM DN @S@>PO@? <N NJJI <N OC@ GDA@ODH@ JA OC@ NOMP>O @I?N. AN TJP RJPG? M@H@H=@M
AMJH OC@ )DA@ODH@N <I? #PI?<H@IO<G ,K@M<ODJIN >C<KO@M, OC@ GDA@ODH@ JA <I J=E@>O @I?N RC@I
G@<QDIB OC@ N>JK@ OC<O DO DN ?@ADI@? DI.
1C@ AJGGJRDIB <M@ ODH@N RC@I OC@ GDA@ODH@ JA < NOMP>O @I?N:
t 4C@I G@<QDIB OC@ N>JK@ JA OC@ J=E@>O @DOC@M IJMH<GGT JM ?P@ OJ < OCMJRI @S>@KODJI:
JG (B$POEJUJPO) \
BVUP EVSBUJPO = %VSBUJPO(7);
// ...
^ // j 5IF EFTUSVDUPS JT FYFDVUFE GPS 'EVSBUJPO' BU UIJT QPJOU
t AIJITHJPN J=E@>ON <M@ ?@NOMJT@? <O OC@ @I? JA OC@ RCJG@ @SKM@NNDJI OC<O OC@T <M@
>JINOMP>O@? DI:
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
291
UJNF.JODSFNFOU(%VSBUJPO(5)); // j 5IF %VSBUJPO(5) PCKFDU
// HFUT EFTUSPZFE BU UIF FOE
// PG UIF XIPMF FYQSFTTJPO.
t AGG JA OC@ NOMP>O H@H=@MN JA < NOMP>O J=E@>O B@O ?@NOMJT@? RC@I OC@ JPO@M J=E@>O DN
?@NOMJT@?.
Destructor example 54.2.2
)@O'N ?@NDBI < OTK@ AJM B@I@M<ODIB NDHKG@ 5*) ?J>PH@ION. 5*) @G@H@ION <M@ ?@ADI@? =T
<IBG@ =M<>F@ON. 1C@T >JIO<DI ?<O< <I? JOC@M 5*) @G@H@ION. 5*) @G@H@ION ><I C<Q@
<OOMD=PO@N <N R@GG; R@ RDGG DBIJM@ OC@H C@M@.
,PM <DH RDGG =@ OJ @INPM@ OC<O <I @G@H@IO OC<O C<N =@@I AB7@76 =T < <OBNF> O<B RDGG <GR<TN
=@ 5>AE76 =T < H<O>CDIB </OBNF> O<B:
<DMBTT1> j PQFOJOH UIF PVUFS 9.- FMFNFOU
<HSBEF> j PQFOJOH UIF JOOFS 9.- FMFNFOU
57 j UIF EBUB
</HSBEF> j DMPTJOH UIF JOOFS FMFNFOU
</DMBTT1> j DMPTJOH UIF PVUFS FMFNFOU
A NOMP>O OC<O ><I KMJ?P>@ OC@ JPOKPO <=JQ@ ><I =@ ?@NDBI@? =T ORJ H@H=@MN OC<O NOJM@ OC@
O<B AJM OC@ 5*) @G@H@IO <I? OC@ DI?@IO<ODJI OJ PN@ RC@I KMDIODIB DO:
TUSVDU 9NM&MFNFOU
\
TUSJOH OBNF;
TUSJOH JOEFOUBUJPO;
^
&A OC@ M@NKJIND=DGDOD@N JA JK@IDIB <I? >GJNDIB OC@ 5*) @G@H@IO <M@ BDQ@I OJ OC@ >JINOMP>OJM
<I? OC@ ?@NOMP>OJM, M@NK@>ODQ@GT, OC@ ?@NDM@? JPOKPO ><I =@ KMJ?P>@? =T H<I<BDIB OC@
GDA@ODH@N JA 5HG"G@H@IO J=E@>ON. #JM @S<HKG@, OC@ >JINOMP>OJM ><I KMDIO <UBH> <I? OC@
?@NOMP>OJM ><I KMDIO </UBH>.
1C@ AJGGJRDIB ?@ADIDODJI JA OC@ >JINOMP>OJM KMJ?P>@N OC@ JK@IDIB O<B:
UIJT(JO TUSJOH OBNF, JO JOU MFWFM)
\
UIJT.OBNF = OBNF;
UIJT.JOEFOUBUJPO = JOEFOUBUJPO4USJOH(MFWFM);
XSJUFMO(JOEFOUBUJPO, '<', OBNF, '>');
^
JOEFOUBUJPO4USJOH() DN OC@ AJGGJRDIB API>ODJI:
JNQPSU TUE.BSSBZ;
// ...
TUSJOH JOEFOUBUJPO4USJOH(JO JOU MFWFM)
\
SFUVSO SFQMJDBUF(" ", MFWFM * 2);
^
1C@ API>ODJI ><GGN SFQMJDBUF() AMJH OC@ TUE.BSSBZ HJ?PG@, RCD>C H<F@N <I? M@OPMIN < I@R
NOMDIB H<?@ PK JA OC@ NK@>DAD@? Q<GP@ M@K@<O@? OC@ NK@>DAD@? IPH=@M JA ODH@N.
1C@ ?@NOMP>OJM ><I =@ ?@ADI@? NDHDG<M OJ OC@ >JINOMP>OJM OJ KMJ?P>@ OC@ >GJNDIB O<B:
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
292
_UIJT()
\
XSJUFMO(JOEFOUBUJPO, "</", OBNF, '>');
^
%@M@ DN < O@NO >J?@ OJ ?@HJINOM<O@ OC@ @AA@>ON JA OC@ <POJH<OD> >JINOMP>OJM <I? ?@NOMP>OJM
><GGN:
WPJE NBJO()
\
JNNVUBCMF DMBTTFT = 9NM&MFNFOU("DMBTTFT", 0);
GPSFBDI (DMBTT*E; 0 .. 2) \
JNNVUBCMF DMBTT5BH = "DMBTT" _ UP!TUSJOH(DMBTT*E);
JNNVUBCMF DMBTT&MFNFOU = 9NM&MFNFOU(DMBTT5BH, 1);
GPSFBDI (J; 0 .. 3) \
JNNVUBCMF HSBEF&MFNFOU = 9NM&MFNFOU("HSBEF", 2);
JNNVUBCMF SBOEPN(SBEF = VOJGPSN(50, 101);
XSJUFMO(JOEFOUBUJPO4USJOH(3), SBOEPN(SBEF);
^
^
^
+JO@ OC<O OC@ 9NM&MFNFOU J=E@>ON <M@ >M@<O@? DI OCM@@ N@K<M<O@ N>JK@N DI OC@ KMJBM<H <=JQ@.
1C@ JK@IDIB <I? >GJNDIB O<BN JA OC@ 5*) @G@H@ION DI OC@ JPOKPO <M@ KMJ?P>@? NJG@GT =T OC@
>JINOMP>OJM <I? OC@ ?@NOMP>OJM JA 9NM&MFNFOU.
& C<Q@ DI?D><O@? OC@ H<O>CDIB JK@IDIB <I? >GJNDIB O<BN JA OC@ JPO@M, OC@ HD??G@, <I? OC@
DII@M N>JK@N =T ?DAA@M@IO >JGJMN:
<DMBTTFT>
<DMBTT0>
<HSBEF>
72
</HSBEF>
<HSBEF>
97
</HSBEF>
<HSBEF>
90
</HSBEF>
</DMBTT0>
<DMBTT1>
<HSBEF>
77
</HSBEF>
<HSBEF>
87
</HSBEF>
<HSBEF>
56
</HSBEF>
</DMBTT1>
</DMBTTFT>
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
293
1C@ <DMBTTFT> @G@H@IO DN KMJ?P>@? =T OC@ DMBTTFT&MFNFOU Q<MD<=G@. B@><PN@ OC<O Q<MD<=G@
DN >JINOMP>O@? ADMNO DI NBJO(), OC@ JPOKPO >JIO<DIN OC@ JPOKPO JA DON >JINOMP>ODJI ADMNO. 0DI>@ DO
DN <GNJ OC@ Q<MD<=G@ OC<O DN ?@NOMJT@? G<NO, PKJI G@<QDIB NBJO(), OC@ JPOKPO >JIO<DIN OC@
JPOKPO JA OC@ ?@NOMP>OJM ><GG AJM DON ?@NOMP>ODJI G<NO.
Postblit
54.3
CABK;@9 DN >JINOMP>ODIB < I@R J=E@>O AMJH <I @SDNODIB JI@. JKTDIB DIQJGQ@N ORJ NO@KN:
1. JKTDIB OC@ H@H=@MN JA OC@ @SDNODIB J=E@>O OJ OC@ I@R J=E@>O. 1CDN NO@K DN ><GG@? 4>;F,
NCJMO AJM 4>A5= FD3@E87D.
2. *<FDIB APMOC@M <?EPNOH@ION OJ OC@ I@R J=E@>O. 1CDN NO@K DN ><GG@? BAEF4>;F.
1C@ ADMNO NO@K DN C<I?G@? <POJH<OD><GGT =T OC@ >JHKDG@M: &O >JKD@N OC@ H@H=@MN JA OC@ @SDNODIB
J=E@>O OJ OC@ H@H=@MN JA OC@ I@R J=E@>O:
BVUP SFUVSO5SJQ%VSBUJPO = USJQ%VSBUJPO; // DPQZJOH
!J IJO >JIAPN@ >JKTDIB RDOC 3EE;9@?7@F. 1C@ BVUP F@TRJM? <=JQ@ DN <I DI?D><ODJI OC<O < I@R
J=E@>O DN =@DIB ?@ADI@?. 1C@ <>OP<G OTK@ I<H@ >JPG? C<Q@ =@@I NK@GG@? JPO DINO@<? JA BVUP.
#JM <I JK@M<ODJI OJ =@ <NNDBIH@IO, OC@ J=E@>O JI OC@ G@AO-C<I? ND?@ HPNO =@ <I @SDNODIB
J=E@>O. #JM @S<HKG@, <NNPHDIB OC<O SFUVSO5SJQ%VSBUJPO C<N <GM@<?T =@@I ?@ADI@?:
SFUVSO5SJQ%VSBUJPO = USJQ%VSBUJPO; // BTTJHONFOU (TFF CFMPX)
0JH@ODH@N DO DN I@>@NN<MT OJ H<F@ <?EPNOH@ION OJ OC@ H@H=@MN JA OC@ I@R J=E@>O <AO@M OC@
<POJH<OD> =GDO. 1C@N@ JK@M<ODJIN <M@ ?@ADI@? DI OC@ KJNO=GDO API>ODJI JA OC@ NOMP>O.
0DI>@ DO DN <=JPO J=E@>O >JINOMP>ODJI, OC@ I<H@ JA OC@ KJNO=GDO DN UIJT <N R@GG. 1J N@K<M<O@ DO
AMJH OC@ JOC@M >JINOMP>OJMN, DON K<M<H@O@M GDNO >JIO<DIN OC@ F@TRJM? UIJT:
UIJT(UIJT)
\
// ...
^
4@ C<Q@ ?@ADI@? < 4UVEFOU OTK@ DI OC@ 0OMP>ON >C<KO@M, RCD>C C<? < KMJ=G@H <=JPO >JKTDIB
J=E@>ON JA OC<O OTK@:
TUSVDU 4UVEFOU
\
JOU OVNCFS;
JOU<> HSBEFT;
^
B@DIB < NGD>@, OC@ HSBEFT H@H=@M JA OC<O TUSVDU DN < M@A@M@I>@ OTK@. 1C@ >JIN@LP@I>@ JA
>JKTDIB < 4UVEFOU J=E@>O DN OC<O OC@ HSBEFT H@H=@MN JA =JOC OC@ JMDBDI<G <I? OC@ >JKT
KMJQD?@ <>>@NN OJ OC@ N<H@ <>OP<G <MM<T @G@H@ION JA OTK@ JOU. AN < M@NPGO, OC@ @AA@>O JA
HJ?DATDIB < BM<?@ OCMJPBC JI@ JA OC@N@ J=E@>ON DN N@@I OCMJPBC OC@ JOC@M J=E@>O <N R@GG:
BVUP TUVEFOU1 = 4UVEFOU(1, < 70, 90, 85 >);
BVUP TUVEFOU2 = TUVEFOU1; // DPQZJOH
TUVEFOU2.OVNCFS = 2;
TUVEFOU1.HSBEFT<0> += 5; // UIJT DIBOHFT UIF HSBEF PG UIF
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
294
// TFDPOE TUVEFOU BT XFMM:
BTTFSU(TUVEFOU2.HSBEFT<0> == 75);
1J <QJD? NP>C < >JIAPNDJI, OC@ @G@H@ION JA OC@ HSBEFT H@H=@M JA OC@ N@>JI? J=E@>O HPNO =@
N@K<M<O@ <I? =@GJIB JIGT OJ OC<O J=E@>O. 0P>C 36<GEF?7@FE <M@ ?JI@ DI OC@ KJNO=GDO:
TUSVDU 4UVEFOU
\
JOU OVNCFS;
JOU<> HSBEFT;
UIJT(UIJT)
\
HSBEFT = HSBEFT.EVQ;
^
^
/@H@H=@M OC<O <GG JA OC@ H@H=@MN C<Q@ <GM@<?T =@@I >JKD@? <POJH<OD><GGT =@AJM@
UIJT(UIJT) NO<MO@? @S@>PODIB. 1C@ NDIBG@ GDI@ DI OC@ KJNO=GDO <=JQ@ H<F@N < >JKT JA OC@
7>7?7@FE JA OC@ JMDBDI<G <MM<T <I? <NNDBIN < NGD>@ JA DO =<>F OJ HSBEFT. AN < M@NPGO, OC@ I@R
J=E@>O B@ON DON JRI >JKT JA OC@ BM<?@N.
*<FDIB HJ?DAD><ODJIN OCMJPBC OC@ ADMNO J=E@>O ?J@N IJO <AA@>O OC@ N@>JI? J=E@>O <ITHJM@:
TUVEFOU1.HSBEFT<0> += 5;
BTTFSU(TUVEFOU2.HSBEFT<0> == 70);
Assignment operator
54.4
ANNDBH@IO DN BDQDIB < I@R Q<GP@ OJ <I @SDNODIB J=E@>O:
SFUVSO5SJQ%VSBUJPO = USJQ%VSBUJPO; // BTTJHONFOU
ANNDBIH@IO DN OC@ HJNO DIQJGQ@? JK@M<ODJI <HJIB OC@ NK@>D<G API>ODJIN =@><PN@ DO DN <
>JH=DI<ODJI JA ORJ JK@M<ODJIN OC<O R@ C<Q@ N@@I <=JQ@:
t !@NOMJTDIB OC@ G@AO-C<I? ND?@ J=E@>O
t JINOMP>ODIB OC@ G@AO-C<I? ND?@ J=E@>O AMJH OC@ MDBCO-C<I? ND?@ J=E@>O
%JR@Q@M, <KKGTDIB OC@ ORJ NO@KN <=JQ@ DI OC<O JM?@M DN MDNFT =@><PN@ OC@ JMDBDI<G J=E@>O
RJPG? =@ ?@NOMJT@? =@AJM@ @INPMDIB OC<O OC@ >JKTDIB RJPG? NP>>@@?. AI @S>@KODJI OC<O DN
OCMJRI ?PMDIB OC@ >JKTDIB RJPG? G@<Q@ OC@ G@AO-C<I? ND?@ J=E@>O DI <I DI>JINDNO@IO (APGGT-
?@NOMJT@? JM C<GA-?@NOMJT@?) NO<O@.
#JM OC<O M@<NJI, OC@ >JHKDG@M-B@I@M<O@? <NNDBIH@IO JK@M<OJM HPNO <>O N<A@GT =T <KKGTDIB OC@
AJGGJRDIB NO@KN:
1. JKT OC@ G@AO-C<I? ND?@ J=E@>O OJ < O@HKJM<MT J=E@>O
2. JKT OC@ MDBCO-C<I? ND?@ J=E@>O OJ OC@ G@AO-C<I? ND?@ J=E@>O
3. !@NOMJT OC@ O@HKJM<MT J=E@>O
AGOCJPBC OC@ >JHKDG@M-B@I@M<O@? <NNDBIH@IO JK@M<OJM DN NPDO<=G@ DI HJNO ><N@N, DO ><I =@
?@ADI@? =T OC@ KMJBM<HH@M. 4C@I TJP ?J OC<O, >JIND?@M KJO@IOD<G @S>@KODJIN <I? RMDO@ OC@
<NNDBIH@IO JK@M<OJM DI < R<T OC<O RJMFN @Q@I <O OC@ KM@N@I>@ JA OCMJRI @S>@KODJIN.
1C@ NTIO<S JA OC@ <NNDBIH@IO JK@M<OJM DN OC@ AJGGJRDIB:
t 1C@ I<H@ JA OC@ API>ODJI DN PQ"TTJHO.
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
295
t 1C@ OTK@ JA OC@ K<M<H@O@M DN OC@ N<H@ <N OC@ TUSVDU OTK@. 1CDN K<M<H@O@M DN JAO@I I<H@?
<N SIT, NCJMO AJM D;9:F-:3@6 E;67.
t 1C@ M@OPMI OTK@ DN OC@ N<H@ <N OC@ TUSVDU OTK@.
t 1C@ API>ODJI DN @SDO@? =T SFUVSO UIJT.
AN <I @S<HKG@, G@O'N >JIND?@M < NDHKG@ %VSBUJPO NOMP>O RC@M@ OC@ <NNDBIH@IO JK@M<OJM KMDION
< H@NN<B@:
TUSVDU %VSBUJPO
\
JOU NJOVUF;
%VSBUJPO PQ"TTJHO(%VSBUJPO SIT)
\
XSJUFGMO("NJOVUF JT CFJOH DIBOHFE GSPN %T UP %T",
UIJT.NJOVUF, SIT.NJOVUF);
UIJT.NJOVUF = SIT.NJOVUF;
SFUVSO UIJT;
^
^
// ...
BVUP EVSBUJPO = %VSBUJPO(100);
EVSBUJPO = %VSBUJPO(200); // BTTJHONFOU
1C@ JPOKPO:
NJOVUF JT CFJOH DIBOHFE GSPN 100 UP 200
Assigning from other types 54.4.1
0JH@ODH@N DO DN >JIQ@ID@IO OJ <NNDBI Q<GP@N JA OTK@N OC<O <M@ ?DAA@M@IO AMJH OC@ OTK@ JA OC@
TUSVDU. #JM @S<HKG@, DINO@<? JA M@LPDMDIB < %VSBUJPO J=E@>O JI OC@ MDBCO-C<I? ND?@, DO H<T
=@ PN@APG OJ <NNDBI AMJH <I DIO@B@M:
EVSBUJPO = 300;
1CDN DN KJNND=G@ =T ?@ADIDIB <IJOC@M <NNDBIH@IO JK@M<OJM OC<O O<F@N <I JOU K<M<H@O@M:
TUSVDU %VSBUJPO
\
JOU NJOVUF;
%VSBUJPO PQ"TTJHO(%VSBUJPO SIT)
\
XSJUFGMO("NJOVUF JT CFJOH DIBOHFE GSPN %T UP %T",
UIJT.NJOVUF, SIT.NJOVUF);
UIJT.NJOVUF = SIT.NJOVUF;
SFUVSO UIJT;
^
%VSBUJPO PQ"TTJHO(JOU NJOVUF)
\
XSJUFGMO("NJOVUF JT CFJOH SFQMBDFE CZ BO JOU");
UIJT.NJOVUF = NJOVUF;
SFUVSO UIJT;
^
^
// ...
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
296
EVSBUJPO = %VSBUJPO(200);
EVSBUJPO = 300;
1C@ JPOKPO:
NJOVUF JT CFJOH DIBOHFE GSPN 100 UP 200
NJOVUF JT CFJOH SFQMBDFE CZ BO JOU
Note: AGOCJPBC >JIQ@ID@IO, <NNDBIDIB ?DAA@M@IO OTK@N OJ @<>C JOC@M H<T ><PN@ >JIAPNDJIN <I?
@MMJMN.
Summary
54.5
t JINOMP>OJM (UIJT) DN AJM KM@K<MDIB J=E@>ON AJM PN@. 1C@ >JHKDG@M-B@I@M<O@? ?@A<PGO
>JINOMP>OJM DN NPAAD>D@IO DI HJNO ><N@N.
t 1C@ =@C<QDJM JA OC@ ?@A<PGO >JINOMP>OJM H<T IJO =@ >C<IB@? DI NOMP>ON; TUBUJD PQ$BMM
><I =@ PN@? DINO@<?.
t 0DIBG@-K<M<H@O@M >JINOMP>OJMN ><I =@ PN@? ?PMDIB OTK@ >JIQ@MNDJIN =T UP <I? DBTU.
t !@NOMP>OJM (_UIJT) DN AJM OC@ JK@M<ODJIN OC<O HPNO =@ @S@>PO@? RC@I OC@ GDA@ODH@N JA
J=E@>ON @I?.
t -JNO=GDO (UIJT(UIJT)) DN AJM <?EPNOH@ION OJ OC@ J=E@>O <AO@M OC@ <POJH<OD> H@H=@M >JKD@N.
t ANNDBH@IO JK@M<OJM (PQ"TTJHO) DN AJM >C<IBDIB Q<GP@N JA @SDNODIB J=E@>ON.
CA@EFDG5FAD 3@6 %F:7D (B75;3> FG@5F;A@E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
297
Operator Overloading 55
1C@ OJKD>N >JQ@M@? DI OCDN >C<KO@M <KKGT HJNOGT AJM >G<NN@N <N R@GG. 1C@ =DBB@NO ?DAA@M@I>@ DN
OC<O OC@ =@C<QDJM JA <NNDBIH@IO JK@M<ODJI PQ"TTJHO() ><IIJO =@ JQ@MGJ<?@? AJM >G<NN@N.
,K@M<OJM JQ@MGJ<?DIB @I<=G@N ?@ADIDIB CJR PN@M-?@ADI@? OTK@N =@C<Q@ RC@I PN@? RDOC
JK@M<OJMN. &I OCDN >JIO@SO, OC@ O@MH AH7D>A36 H@<IN KMJQD?DIB OC@ ?@ADIDODJI JA <I JK@M<OJM
AJM < NK@>DAD> OTK@.
4@ C<Q@ N@@I CJR OJ ?@ADI@ NOMP>ON <I? OC@DM H@H=@M API>ODJIN DI KM@QDJPN >C<KO@MN. AN <I
@S<HKG@, R@ C<Q@ ?@ADI@? OC@ JODSFNFOU() H@H=@M API>ODJI OJ =@ <=G@ OJ <?? %VSBUJPO
J=E@>ON OJ 5JNF0G%BZ J=E@>ON. %@M@ <M@ OC@ ORJ NOMP>ON AMJH KM@QDJPN >C<KO@MN, RDOC JIGT OC@
K<MON OC<O <M@ M@G@Q<IO OJ OCDN >C<KO@M:
TUSVDU %VSBUJPO
\
JOU NJOVUF;
^
TUSVDU 5JNF0G%BZ
\
JOU IPVS;
JOU NJOVUF;
WPJE JODSFNFOU(JO %VSBUJPO EVSBUJPO)
\
NJOVUF += EVSBUJPO.NJOVUF;
IPVS += NJOVUF / 60;
NJOVUF %= 60;
IPVS %= 24;
^
^
WPJE NBJO()
\
BVUP MVODI5JNF = 5JNF0G%BZ(12, 0);
MVODI5JNF.JODSFNFOU(%VSBUJPO(10));
^
A =@I@ADO JA H@H=@M API>ODJIN DN =@DIB <=G@ OJ ?@ADI@ JK@M<ODJIN JA < OTK@ <GJIBND?@ OC@
H@H=@M Q<MD<=G@N JA OC<O OTK@.
!@NKDO@ OC@DM <?Q<IO<B@N, H@H=@M API>ODJIN ><I =@ N@@I <N =@DIB GDHDO@? >JHK<M@? OJ
JK@M<ODJIN JI API?<H@IO<G OTK@N. AAO@M <GG, API?<H@IO<G OTK@N ><I M@<?DGT =@ PN@? RDOC
JK@M<OJMN:
JOU XFJHIU = 50;
XFJHIU += 10; // CZ BO PQFSBUPS
A>>JM?DIB OJ RC<O R@ C<Q@ N@@I NJ A<M, NDHDG<M JK@M<ODJIN ><I JIGT =@ <>CD@Q@? =T H@H=@M
API>ODJIN AJM PN@M-?@ADI@? OTK@N:
BVUP MVODI5JNF = 5JNF0G%BZ(12, 0);
MVODI5JNF.JODSFNFOU(%VSBUJPO(10)); // CZ B NFNCFS GVODUJPO
,K@M<OJM JQ@MGJ<?DIB @I<=G@N PNDIB NOMP>ON <I? >G<NN@N RDOC JK@M<OJMN <N R@GG. #JM @S<HKG@,
<NNPHDIB OC<O OC@ += JK@M<OJM DN ?@ADI@? AJM 5JNF0G%BZ, OC@ JK@M<ODJI <=JQ@ ><I =@ RMDOO@I
DI @S<>OGT OC@ N<H@ R<T <N RDOC API?<H@IO<G OTK@N:
MVODI5JNF += %VSBUJPO(10); // CZ BO PQFSBUPS FWFO GPS UIF TUSVDU
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
298
B@AJM@ B@OODIB OJ OC@ ?@O<DGN JA JK@M<OJM JQ@MGJ<?DIB, G@O'N ADMNO N@@ CJR OC@ GDI@ <=JQ@ RJPG?
=@ @I<=G@? AJM 5JNF0G%BZ. 4C<O DN I@@?@? DN OJ M@?@ADI@ OC@ JODSFNFOU() H@H=@M API>ODJI
PI?@M OC@ NK@>D<G I<H@ PQ0Q"TTJHO(TUSJOH PQ) <I? <GNJ OJ NK@>DAT OC<O OCDN ?@ADIDODJI DN AJM
OC@ + >C<M<>O@M. AN DO RDGG =@ @SKG<DI@? =@GJR, OCDN ?@ADIDODJI <>OP<GGT >JMM@NKJI?N OJ OC@ +=
JK@M<OJM.
1C@ ?@ADIDODJI JA OCDN H@H=@M API>ODJI ?J@N IJO GJJF GDF@ OC@ JI@N OC<O R@ C<Q@ N@@I NJ A<M.
1C<O DN =@><PN@ PQ0Q"TTJHO DN <>OP<GGT < 8G@5F;A@ F7?B>3F7. 0DI>@ R@ RDGG N@@ O@HKG<O@N DI
HP>C G<O@M >C<KO@MN, & RDGG C<Q@ OJ <NF TJP OJ <>>@KO OC@ JK@M<OJM JQ@MGJ<?DIB NTIO<S <N-DN AJM
IJR:
TUSVDU 5JNF0G%BZ
\
// ...
SFG 5JNF0G%BZ PQ0Q"TTJHO(TUSJOH PQ)(JO %VSBUJPO EVSBUJPO) // (1)
JG (PQ == "+") // (2)
\
NJOVUF += EVSBUJPO.NJOVUF;
IPVS += NJOVUF / 60;
NJOVUF %= 60;
IPVS %= 24;
SFUVSO UIJT;
^
^
1C@ O@HKG<O@ ?@ADIDODJI >JINDNON JA ORJ K<MON:
1. PQ0Q"TTJHO(TUSJOH PQ): 1CDN K<MO HPNO =@ RMDOO@I <N-DN <I? NCJPG? =@ <>>@KO@? <N OC@
@3?7 JA OC@ API>ODJI. 4@ RDGG N@@ =@GJR OC<O OC@M@ <M@ JOC@M H@H=@M API>ODJIN DI <??DODJI
OJ PQ0Q"TTJHO.
2. JG (PQ == "+"): PQ0Q"TTJHO DN PN@? AJM HJM@ OC<I JI@ JK@M<OJM JQ@MGJ<?. "+" NK@>DAD@N
OC<O OCDN DN OC@ JK@M<OJM JQ@MGJ<? OC<O >JMM@NKJI?N OJ OC@ + >C<M<>O@M. 1CDN NTIO<S DN <
F7?B>3F7 5A@EFD3;@F, RCD>C RDGG <GNJ =@ >JQ@M@? DI G<O@M >C<KO@MN.
AGNJ IJO@ OC<O OCDN ODH@ OC@ M@OPMI OTK@ DN ?DAA@M@IO AMJH OC@ M@OPMI OTK@ JA OC@ JODSFNFOU()
H@H=@M API>ODJI: &O DN IJO WPJE <ITHJM@. 4@ RDGG ?DN>PNN OC@ M@OPMI OTK@N JA JK@M<OJMN G<O@M
=@GJR.
B@CDI? OC@ N>@I@N, OC@ >JHKDG@M M@KG<>@N OC@ PN@N JA OC@ += JK@M<OJM RDOC ><GGN OJ OC@
PQ0Q"TTJHO!"+" H@H=@M API>ODJI:
MVODI5JNF += %VSBUJPO(10);
// 5IF GPMMPXJOH MJOF JT UIF FRVJWBMFOU PG UIF QSFWJPVT POF
MVODI5JNF.PQ0Q"TTJHO!"+"(%VSBUJPO(10));
1C@ !"+" K<MO OC<O DN <AO@M PQ0Q"TTJHO NK@>DAD@N OC<O OCDN ><GG DN AJM OC@ ?@ADIDODJI JA OC@
JK@M<OJM AJM OC@ + >C<M<>O@M. 4@ RDGG >JQ@M OCDN O@HKG<O@ NTIO<S DI G<O@M >C<KO@MN <N R@GG.
+JO@ OC<O OC@ JK@M<OJM ?@ADIDODJI OC<O >JMM@NKJI?N OJ += DN ?@ADI@? =T "+", IJO =T "+=". 1C@
"TTJHO DI OC@ I<H@ JA PQ0Q"TTJHO() <GM@<?T DHKGD@N OC<O OCDN I<H@ DN AJM <I <NNDBIH@IO
JK@M<OJM.
Do not change conventional behaviors
55.1
B@DIB <=G@ OJ ?@ADI@ OC@ =@C<QDJMN JA JK@M<OJMN =MDIBN < M@NKJIND=DGDOT: 1C@ KMJBM<HH@M HPNO
J=N@MQ@ @SK@>O<ODJIN. AN <I @SOM@H@ @S<HKG@, OC@ KM@QDJPN JK@M<OJM >JPG? C<Q@ =@@I ?@ADI@?
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
299
OJ ?@>M@H@IO OC@ ODH@ Q<GP@ DINO@<? JA DI>M@H@IODIB DO. %JR@Q@M, K@JKG@ RCJ M@<? OC@ >J?@
RJPG? NODGG @SK@>O OC@ Q<GP@ OJ =@ DI>M@H@IO@? =T OC@ += JK@M<OJM.
1J NJH@ @SO@IO, OC@ M@OPMI OTK@N JA JK@M<OJMN ><I <GNJ =@ >CJN@I AM@@GT. 0ODGG, B@I@M<G
@SK@>O<ODJIN HPNO =@ J=N@MQ@? AJM OC@ M@OPMI OTK@N <N R@GG.
(@@K DI HDI? OC<O JK@M<OJMN OC<O =@C<Q@ PII<OPM<GGT RJPG? ><PN@ >JIAPNDJIN <I? =PBN.
Overloadable unary operators
55.2
AI JK@M<OJM OC<O O<F@N < NDIBG@ JK@M<I? DN ><GG@? < PI<MT JK@M<OJM:
++XFJHIU;
++ DN < PI<MT JK@M<OJM =@><PN@ DO RJMFN JI < NDIBG@ Q<MD<=G@.
2I<MT JK@M<OJMN <M@ ?@ADI@? =T H@H=@M API>ODJIN I<H@? PQ6OBSZ. PQ6OBSZ ?J@N IJO O<F@
<IT K<M<H@O@MN =@><PN@ DO PN@N JIGT OC@ J=E@>O OC<O OC@ JK@M<OJM DN =@DIB @S@>PO@? JI.
1C@ JQ@MGJ<?<=G@ PI<MT JK@M<OJMN <I? OC@ >JMM@NKJI?DIB JK@M<OJM NOMDIBN <M@ OC@ AJGGJRDIB:
0QFSBUPS %FTDSJQUJPO 0QFSBUPS 4USJOH
-PCKFDU OFHBUJWF PG (OVNFSJD DPNQMFNFOU PG) "-"
+PCKFDU UIF TBNF WBMVF BT (PS, B DPQZ PG) "+"
_PCKFDU CJUXJTF OFHBUJPO "_"
*PCKFDU BDDFTT UP XIBU JU QPJOUT UP "*"
++PCKFDU JODSFNFOU "++"
--PCKFDU EFDSFNFOU "--"
#JM @S<HKG@, OC@ ++ JK@M<OJM AJM %VSBUJPO ><I =@ ?@ADI@? GDF@ OCDN:
TUSVDU %VSBUJPO
\
JOU NJOVUF;
SFG %VSBUJPO PQ6OBSZ(TUSJOH PQ)()
JG (PQ == "++")
\
++NJOVUF;
SFUVSO UIJT;
^
^
+JO@ OC<O OC@ M@OPMI OTK@ JA OC@ JK@M<OJM DN H<MF@? <N SFG C@M@ <N R@GG. 1CDN RDGG =@
@SKG<DI@? G<O@M =@GJR.
%VSBUJPO J=E@>ON ><I IJR =@ DI>M@H@IO@? =T ++:
BVUP EVSBUJPO = %VSBUJPO(20);
++EVSBUJPO;
1C@ KJNO-DI>M@H@IO <I? KJNO-?@>M@H@IO JK@M<OJMN ><IIJO =@ JQ@MGJ<?@?. 1C@ PCKFDU++ <I?
PCKFDU-- PN@N <M@ C<I?G@? =T OC@ >JHKDG@M <POJH<OD><GGT =T N<QDIB OC@ KM@QDJPN Q<GP@ JA OC@
J=E@>O. #JM @S<HKG@, OC@ >JHKDG@M <KKGD@N OC@ @LPDQ<G@IO JA OC@ AJGGJRDIB >J?@ AJM KJNO-
DI>M@H@IO:
/* 5IF QSFWJPVT WBMVF JT DPQJFE CZ UIF DPNQJMFS
* BVUPNBUJDBMMZ: */
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
300
%VSBUJPO @@QSFWJPVT7BMVF@@ = EVSBUJPO;
/* 5IF ++ PQFSBUPS JT DBMMFE: */
++EVSBUJPO;
/* 5IFO @@QSFWJPVT7BMVF@@ JT VTFE BT UIF WBMVF PG UIF
* QPTU-JODSFNFOU PQFSBUJPO. */
Overloadable binary operators
55.3
AI JK@M<OJM OC<O O<F@N ORJ JK@M<I?N DN ><GG@? < =DI<MT JK@M<OJM:
UPUBM8FJHIU = CPY8FJHIU + DIPDPMBUF8FJHIU;
1C@ GDI@ <=JQ@ C<N ORJ N@K<M<O@ =DI<MT JK@M<OJMN: OC@ + JK@M<OJM, RCD>C <??N OC@ Q<GP@N JA
OC@ ORJ JK@M<I?N OC<O <M@ JI DON ORJ ND?@N, <I? OC@ = JK@M<OJM OC<O <NNDBIN OC@ Q<GP@ JA DON
MDBCO-C<I? JK@M<I? OJ DON G@AO-C<I? JK@M<I?.
1C@ MDBCOHJNO >JGPHI =@GJR ?@N>MD=@N OC@ ><O@BJMT JA @<>C JK@M<OJM. 1C@ JI@N H<MF@? <N
"=" <NNDBI OJ OC@ G@AO-C<I? ND?@ J=E@>O.
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
301
0QFSBUPS %FTDSJQUJPO 'VODUJPO OBNF
'VODUJPO OBNF
GPS SJHIU-IBOE TJEF $BUFHPSZ
+ BEE PQ#JOBSZ PQ#JOBSZ3JHIU BSJUINFUJD
- TVCUSBDU PQ#JOBSZ PQ#JOBSZ3JHIU BSJUINFUJD
* NVMUJQMZ PQ#JOBSZ PQ#JOBSZ3JHIU BSJUINFUJD
/ EJWJEF PQ#JOBSZ PQ#JOBSZ3JHIU BSJUINFUJD
% SFNBJOEFS PG PQ#JOBSZ PQ#JOBSZ3JHIU BSJUINFUJD
?? UP UIF QPXFS PG PQ#JOBSZ PQ#JOBSZ3JHIU BSJUINFUJD
& CJUXJTF and PQ#JOBSZ PQ#JOBSZ3JHIU CJUXJTF
] CJUXJTF or PQ#JOBSZ PQ#JOBSZ3JHIU CJUXJTF
? CJUXJTF xor PQ#JOBSZ PQ#JOBSZ3JHIU CJUXJTF
<< MFGU-TIJGU PQ#JOBSZ PQ#JOBSZ3JHIU CJUXJTF
>> SJHIU-TIJGU PQ#JOBSZ PQ#JOBSZ3JHIU CJUXJTF
>>> MPHJDBM SJHIU-TIJGU PQ#JOBSZ PQ#JOBSZ3JHIU CJUXJTF
_ DPODBUFOBUF PQ#JOBSZ PQ#JOBSZ3JHIU
JO XIFUIFS DPOUBJOFE JO PQ#JOBSZ PQ#JOBSZ3JHIU
== XIFUIFS FRVBM UP PQ&RVBMT - MPHJDBM
!= XIFUIFS OPU FRVBM UP PQ&RVBMT - MPHJDBM
< XIFUIFS CFGPSF PQ$NQ - TPSUJOH
<= XIFUIFS OPU BGUFS PQ$NQ - TPSUJOH
> XIFUIFS BGUFS PQ$NQ - TPSUJOH
>= XIFUIFS OPU CFGPSF PQ$NQ - TPSUJOH
= BTTJHO PQ"TTJHO - =
+= JODSFNFOU PQ0Q"TTJHO - =
-= EFDSFNFOU PQ0Q"TTJHO - =
*= NVMUJQMZ BOE BTTJHO PQ0Q"TTJHO - =
/= EJWJEF BOE BTTJHO PQ0Q"TTJHO - =
%= BTTJHO UIF SFNBJOEFS PG PQ0Q"TTJHO - =
??= BTTJHO UIF QPXFS PG PQ0Q"TTJHO - =
&= BTTJHO UIF SFTVMU PG & PQ0Q"TTJHO - =
]= BTTJHO UIF SFTVMU PG ] PQ0Q"TTJHO - =
?= BTTJHO UIF SFTVMU PG ? PQ0Q"TTJHO - =
<<= BTTJHO UIF SFTVMU PG << PQ0Q"TTJHO - =
>>= BTTJHO UIF SFTVMU PG >> PQ0Q"TTJHO - =
>>>= BTTJHO UIF SFTVMU PG >>> PQ0Q"TTJHO - =
_= BQQFOE PQ0Q"TTJHO - =
PQ#JOBSZ3JHIU DN AJM RC@I OC@ J=E@>O ><I <KK@<M JI OC@ MDBCO-C<I? ND?@ JA OC@ JK@M<OJM. )@O'N
<NNPH@ < =DI<MT JK@M<OJM OC<O R@ NC<GG ><GG AB <KK@<MN DI OC@ KMJBM<H:
Y op Z
&I JM?@M OJ ?@O@MHDI@ RC<O H@H=@M API>ODJI OJ ><GG, OC@ >JHKDG@M >JIND?@MN OC@ AJGGJRDIB ORJ
JKODJIN:
Y.PQ#JOBSZ!"PQ"(Z); // UIF EFGJOJUJPO GPS Y CFJOH PO UIF MFGU
Z.PQ#JOBSZ3JHIU!"PQ"(Y); // UIF EFGJOJUJPO GPS Z CFJOH PO UIF SJHIU
1C@ >JHKDG@M KD>FN OC@ JKODJI OC<O DN < =@OO@M H<O>C OC<I OC@ JOC@M.
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
302
&I HJNO ><N@N DO DN IJO I@>@NN<MT OJ ?@ADI@ PQ#JOBSZ3JHIU, @S>@KO AJM OC@ JO JK@M<OJM: &O
PNP<GGT H<F@N HJM@ N@IN@ OJ ?@ADI@ PQ#JOBSZ3JHIU AJM JO.
1C@ K<M<H@O@M I<H@ SIT OC<O <KK@<MN DI OC@ AJGGJRDIB ?@ADIDODJIN DN NCJMO AJM D;9:F-:3@6 E;67.
&O ?@IJO@N OC@ JK@M<I? OC<O <KK@<MN JI OC@ MDBCO-C<I? ND?@ JA OC@ JK@M<OJM:
Y op Z
#JM OC@ @SKM@NNDJI <=JQ@, OC@ SIT K<M<H@O@M RJPG? M@KM@N@IO OC@ Q<MD<=G@ Z.
Other operators that can be overloaded
55.4
&I <??DODJI OJ OC@ PI<MT <I? =DI<MT JK@M<OJMN <=JQ@, OC@ AJGGJRDIB JK@M<OJMN ><I <GNJ =@
JQ@MGJ<?@?:
%FTDSJQUJPO 'VODUJPO /BNF 4BNQMF 6TBHF
GVODUJPO DBMM PQ$BMM PCKFDU(42)
FMFNFOU BDDFTT PQ*OEFY DPMMFDUJPO<J>
BTTJHONFOU UP FMFNFOU PQ*OEFY"TTJHO DPMMFDUJPO<J> = 7
VOBSZ PQFSBUJPO PO FMFNFOU PQ*OEFY6OBSZ ++DPMMFDUJPO<J>
PQFSBUJPO XJUI BTTJHONFOU PO FMFNFOU PQ*OEFY0Q"TTJHO DPMMFDUJPO<J> *= 2
TMJDF UP BMM FMFNFOUT PQ4MJDF DPMMFDUJPO<>
TMJDF UP TPNF FMFNFOUT PQ4MJDF DPMMFDUJPO<J, K>
VOBSZ PQFSBUJPO PO BMM FMFNFOUT PQ4MJDF6OBSZ ++DPMMFDUJPO<>
VOBSZ PQFSBUJPO PO TPNF FMFNFOUT PQ4MJDF6OBSZ ++DPMMFDUJPO<J, K>
BTTJHONFOU UP BMM FMFNFOUT PQ4MJDF"TTJHO DPMMFDUJPO<> = 42
BTTJHONFOU UP TPNF FMFNFOUT PQ4MJDF"TTJHO DPMMFDUJPO<J, K> = 7
PQFSBUJPO XJUI BTTJHONFOU PO BMM FMFNFOUT PQ4MJDF0Q"TTJHO DPMMFDUJPO<> *= 2
PQFSBUJPO XJUI BTTJHONFOU PO TPNF FMFNFOUT PQ4MJDF0Q"TTJHO DPMMFDUJPO<J, K> *= 2
EJTQBUDI GPS OPO-FYJTUFOU GVODUJPO PQ%JTQBUDI PCKFDU.OPO&YJTUFOU()
1C@N@ JK@M<OJMN RDGG =@ @SKG<DI@? =@GJR PI?@M OC@DM JRI N@>ODJIN.
Return types of operators
55.5
4C@I JQ@MGJ<?DIB <I JK@M<OJM, DO DN <?QDN<=G@ OJ J=N@MQ@ OC@ M@OPMI OTK@ JA OC@ N<H@ JK@M<OJM
JI API?<H@IO<G OTK@N. 1CDN RJPG? C@GK RDOC H<FDIB N@IN@ JA >J?@ <I? M@?P>DIB >JIAPNDJIN.
+JI@ JA OC@ JK@M<OJMN JI API?<H@IO<G OTK@N M@OPMI WPJE. 1CDN A<>O NCJPG? =@ J=QDJPN AJM
NJH@ JK@M<OJMN. #JM @S<HKG@, OC@ M@NPGO JA <??DIB ORJ JOU Q<GP@N <N B + C DN JOU:
JOU B = 1;
JOU C = 2;
JOU D = B + C; // D HFUT JOJUJBMJ[FE CZ UIF SFUVSO WBMVF
// PG UIF + PQFSBUPS
1C@ M@OPMI Q<GP@N JA NJH@ JOC@M JK@M<OJMN H<T IJO =@ NJ J=QDJPN. #JM @S<HKG@, @Q@I
JK@M<OJMN GDF@ ++J C<Q@ Q<GP@N:
JOU J = 1;
XSJUFMO(++J); // QSJOUT 2
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
303
1C@ ++ JK@M<OJM IJO JIGT DI>M@H@ION J, DO <GNJ KMJ?P>@N OC@ I@R Q<GP@ JA J. #PMOC@M, OC@
Q<GP@ OC<O DN KMJ?P>@? =T ++ DN IJO EPNO OC@ I@R Q<GP@ JA J, M<OC@M F:7 H3D;34>7 i ;FE7>8. 4@
><I N@@ OCDN A<>O =T KMDIODIB OC@ <??M@NN JA OC@ M@NPGO JA OC<O @SKM@NNDJI:
JOU J = 1;
XSJUFMO("5IF BEESFTT PG J : ", &J);
XSJUFMO("5IF BEESFTT PG UIF SFTVMU PG ++J: ", &(++J));
1C@ JPOKPO >JIO<DIN D?@IOD><G <??M@NN@N:
5IF BEESFTT PG J : 7'''39#'&&78
5IF BEESFTT PG UIF SFTVMU PG ++J: 7'''39#'&&78
& M@>JHH@I? OC<O TJP J=N@MQ@ OC@ AJGGJRDIB BPD?@GDI@N RC@I JQ@MGJ<?DIB JK@M<OJMN AJM TJPM
JRI OTK@N:
t Operators that modify the object
4DOC OC@ @S>@KODJI JA PQ"TTJHO, DO DN M@>JHH@I?@? OC<O OC@ JK@M<OJMN OC<O HJ?DAT OC@
J=E@>O M@OPMI OC@ J=E@>O DON@GA. 1CDN BPD?@GDI@ C<N =@@I J=N@MQ@? <=JQ@ RDOC OC@
5JNF0G%BZ.PQ0Q"TTJHO!"+" <I? %VSBUJPO.PQ6OBSZ!"++".
1C@ AJGGJRDIB ORJ NO@KN <>CD@Q@ M@OPMIDIB OC@ J=E@>O DON@GA:
1. 1C@ M@OPMI OTK@ DN OC@ OTK@ JA OC@ NOMP>O, H<MF@? =T OC@ SFG F@TRJM? OJ H@<I
D787D7@57.
2. 1C@ API>ODJI DN @SDO@? =T SFUVSO UIJT OJ H@<I D7FGD@ F:;E A4<75F.
1C@ JK@M<OJMN OC<O HJ?DAT OC@ J=E@>O <M@ PQ6OBSZ!"++", PQ6OBSZ!"--", <I? <GG JA OC@
PQ0Q"TTJHO JQ@MGJ<?N.
t Logical operators
PQ&RVBMT OC<O M@KM@N@ION =JOC == <I? != HPNO M@OPMI CPPM. AGOCJPBC OC@ JO
JK@M<OJM IJMH<GGT M@OPMIN F:7 5A@F3;@76 A4<75F, DO ><I NDHKGT M@OPMI CPPM <N R@GG.
t Sort operators
PQ$NQ OC<O M@KM@N@ION <, <=, >, <I? >= HPNO M@OPMI JOU.
t Operators that make a new object
0JH@ JK@M<OJMN HPNO H<F@ <I? M@OPMI < I@R J=E@>O:
w 2I<MT JK@M<OJMN -, +, <I? _; <I? OC@ =DI<MT JK@M<OJM _.
w AMDOCH@OD> JK@M<OJMN +, -, *, /, %, <I? ??.
w BDORDN@ JK@M<OJMN &, ], ?, <<, >>, <I? >>>.
w AN C<N =@@I N@@I DI OC@ KM@QDJPN >C<KO@M, PQ"TTJHO M@OPMIN < >JKT JA OCDN J=E@>O
=T SFUVSO UIJT.
Note: AE 3@ ABF;?;L3F;A@, EA?7F;?7E ;F ?3=7E ?AD7 E7@E7 8AD opAssign FA
D7FGD@ const ref 8AD >3D97 EFDG5FE. ! I;>> @AF 3BB>K F:;E ABF;?;L3F;A@ ;@ F:;E
4AA=.
AN <I @S<HKG@ JA <I JK@M<OJM OC<O H<F@N < I@R J=E@>O, G@O'N ?@ADI@ OC@ PQ#JOBSZ!"+"
JQ@MGJ<? AJM %VSBUJPO. 1CDN JK@M<OJM NCJPG? <?? ORJ %VSBUJPO J=E@>ON OJ H<F@ <I?
M@OPMI < I@R JI@:
TUSVDU %VSBUJPO
\
JOU NJOVUF;
%VSBUJPO PQ#JOBSZ(TUSJOH PQ)(JO %VSBUJPO SIT) DPOTU
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
304
JG (PQ == "+")
\
SFUVSO %VSBUJPO(NJOVUF + SIT.NJOVUF); // OFX PCKFDU
^
^
1C<O ?@ADIDODJI @I<=G@N <??DIB %VSBUJPO J=E@>ON =T OC@ + JK@M<OJM:
BVUP USBWFM%VSBUJPO = %VSBUJPO(10);
BVUP SFUVSO%VSBUJPO = %VSBUJPO(11);
%VSBUJPO UPUBM%VSBUJPO;
// ...
UPUBM%VSBUJPO = USBWFM%VSBUJPO + SFUVSO%VSBUJPO;
1C@ >JHKDG@M M@KG<>@N OC<O @SKM@NNDJI RDOC OC@ AJGGJRDIB H@H=@M API>ODJI ><GG JI OC@
USBWFM%VSBUJPO J=E@>O:
// UIF FRVJWBMFOU PG UIF FYQSFTTJPO BCPWF
UPUBM%VSBUJPO = USBWFM%VSBUJPO.PQ#JOBSZ!"+"(SFUVSO%VSBUJPO);
t Unconstrained operators
1C@ M@OPMI OTK@N JA NJH@ JA OC@ JK@M<OJMN ?@K@I? @IODM@GT JI OC@ ?@NDBI JA OC@ PN@M-
?@ADI@? OTK@: 1C@ PI<MT JK@M<OJM *, PQ$BMM, PQ$BTU, PQ%JTQBUDI, PQ*OEFY <I? JOC@M
DI?@SDIB JK@M<OJMN, <I? PQ4MJDF <I? JOC@M NGD>DIB JK@M<OJMN.
PQ&RVBMT() for equality comparisons
55.6
1CDN H@H=@M API>ODJI ?@ADI@N OC@ =@C<QDJMN JA OC@ == <I? OC@ != JK@M<OJMN.
1C@ M@OPMI OTK@ JA PQ&RVBMT DN CPPM.
A>>JM?DIB OJ G<IBP<B@ MPG@N, OC@ K<M<H@O@M JA PQ&RVBMT AJM NOMP>ON HPNO =@ ?@ADI@? <N DPOTU
SFG.
AN <I DHKJMO<IO MPG@, PQ&RVBMT <I? PQ$NQ HPNO RJMF >JINDNO@IOGT. #JM ORJ J=E@>ON OC<O
PQ&RVBMT M@OPMIN USVF, PQ$NQ HPNO M@OPMI U@MJ.
,I>@ PQ&RVBMT() DN ?@ADI@? AJM @LP<GDOT, OC@ >JHKDG@M PN@N DON JKKJNDO@ AJM DI@LP<GDOT:
Y == Z;
Y.PQ&RVBMT(Z); // UIF FRVJWBMFOU PG UIF QSFWJPVT FYQSFTTJPO
Y != Z;
!(Y.PQ&RVBMT(Z)); // UIF FRVJWBMFOU PG UIF QSFWJPVT FYQSFTTJPO
+JMH<GGT, DO DN IJO I@>@NN<MT OJ ?@ADI@ PQ&RVBMT() AJM NOMP>ON. 1C@ >JHKDG@M B@I@M<O@N DO AJM
NOMP>ON <POJH<OD><GGT. 1C@ <POJH<OD><GGT-B@I@M<O@? PQ&RVBMT >JHK<M@N <GG JA OC@ H@H=@MN
DI?DQD?P<GGT.
0JH@ODH@N OC@ @LP<GDOT JA ORJ J=E@>ON HPNO =@ ?@ADI@? ?DAA@M@IOGT AMJH OCDN <POJH<OD>
=@C<QDJM. #JM @S<HKG@, NJH@ JA OC@ H@H=@MN H<T IJO =@ NDBIDAD><IO DI OCDN >JHK<MDNJI, JM OC@
@LP<GDOT H<T ?@K@I? JI < HJM@ >JHKG@S GJBD>.
'PNO <N <I @S<HKG@, G@O'N ?@ADI@ PQ&RVBMT() DI < R<T OC<O ?DNM@B<M?N OC@ HDIPO@ DIAJMH<ODJI
<GOJB@OC@M:
TUSVDU 5JNF0G%BZ
\
JOU IPVS;
JOU NJOVUF;
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
305
CPPM PQ&RVBMT(DPOTU SFG 5JNF0G%BZ SIT) DPOTU
\
SFUVSO IPVS == SIT.IPVS;
^
^
// ...
BTTFSU(5JNF0G%BZ(20, 10) == 5JNF0G%BZ(20, 59));
0DI>@ OC@ @LP<GDOT >JHK<MDNJI >JIND?@MN OC@ Q<GP@N JA JIGT OC@ IPVS H@H=@MN, 20:10 <I?
20:59 @I? PK =@DIB @LP<G. (1CDN DN EPNO <I @S<HKG@; DO NCJPG? =@ >G@<M OC<O NP>C <I @LP<GDOT
>JHK<MDNJI RJPG? ><PN@ >JIAPNDJIN.)
PQ$NQ() for sorting
55.7
0JMO JK@M<OJMN ?@O@MHDI@ OC@ NJMO JM?@MN JA J=E@>ON. AGG JA OC@ JM?@MDIB JK@M<OJMN <, <=, >,
<I? >= <M@ >JQ@M@? =T OC@ PQ$NQ() H@H=@M API>ODJI.
A>>JM?DIB OJ G<IBP<B@ MPG@N, OC@ K<M<H@O@M JA PQ$NQ AJM NOMP>ON HPNO =@ ?@ADI@? <N DPOTU
SFG.
AN <I DHKJMO<IO MPG@, PQ&RVBMT <I? PQ$NQ HPNO RJMF >JINDNO@IOGT. #JM ORJ J=E@>ON OC<O
PQ&RVBMT M@OPMIN USVF, PQ$NQ HPNO M@OPMI U@MJ.
)@O'N <NNPH@ OC<O JI@ JA OC@N@ AJPM JK@M<OJMN DN PN@? <N DI OC@ AJGGJRDIB >J?@:
JG (Y op Z) \ // j op JT POF PG <, <=, >, PS >=
1C@ >JHKDG@M >JIQ@MON OC<O @SKM@NNDJI OJ OC@ AJGGJRDIB GJBD><G @SKM@NNDJI <I? PN@N OC@ M@NPGO
JA OC@ I@R GJBD><G @SKM@NNDJI:
JG (Y.PQ$NQ(Z) op 0) \
)@O'N >JIND?@M OC@ <= JK@M<OJM:
JG (Y <= Z) \
1C@ >JHKDG@M B@I@M<O@N OC@ AJGGJRDIB >J?@ =@CDI? OC@ N>@I@N:
JG (Y.PQ$NQ(Z) <= 0) \
#JM OC@ PN@M-?@ADI@? PQ$NQ() OJ RJMF >JMM@>OGT, OCDN H@H=@M API>ODJI HPNO M@OPMI < M@NPGO
<>>JM?DIB OJ OC@ AJGGJRDIB MPG@N:
t A @793F;H7 H3>G7 DA OC@ G@AO-C<I? J=E@>O DN >JIND?@M@? OJ =@ =@AJM@ OC@ MDBCO-C<I? J=E@>O
t A BAE;F;H7 H3>G7 DA OC@ G@AO-C<I? J=E@>O DN >JIND?@M@? OJ =@ <AO@M OC@ MDBCO-C<I? J=E@>O
t /7DA DA OC@ J=E@>ON <M@ >JIND?@M@? OJ C<Q@ OC@ N<H@ NJMO JM?@M
1J =@ <=G@ OJ NPKKJMO OCJN@ Q<GP@N, OC@ M@OPMI OTK@ JA PQ$NQ() HPNO =@ JOU, IJO CPPM.
1C@ AJGGJRDIB DN < R<T JA JM?@MDIB 5JNF0G%BZ J=E@>ON =T ADMNO >JHK<MDIB OC@ Q<GP@N JA OC@
IPVS H@H=@MN, <I? OC@I >JHK<MDIB OC@ Q<GP@N JA OC@ NJOVUF H@H=@MN (JIGT DA OC@ IPVS
H@H=@MN <M@ @LP<G):
JOU PQ$NQ(DPOTU SFG 5JNF0G%BZ SIT) DPOTU
\
SFUVSO (IPVS == SIT.IPVS
NJOVUF - SIT.NJOVUF
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
306
: IPVS - SIT.IPVS);
^
1C<O ?@ADIDODJI M@OPMIN OC@ ?DAA@M@I>@ =@OR@@I OC@ NJOVUF Q<GP@N RC@I OC@ IPVS H@H=@MN
<M@ OC@ N<H@, <I? OC@ ?DAA@M@I>@ =@OR@@I OC@ IPVS H@H=@MN JOC@MRDN@. 1C@ M@OPMI Q<GP@
RJPG? =@ < @793F;H7 H3>G7 RC@I OC@ >78F-:3@6 J=E@>O >JH@N =@AJM@ DI >CMJIJGJBD><G JM?@M, <
BAE;F;H7 H3>G7 DA OC@ D;9:F-:3@6 J=E@>O DN =@AJM@, <I? L7DA RC@I OC@T M@KM@N@IO @S<>OGT OC@
N<H@ ODH@ JA ?<T.
,I>@ PQ$NQ() DN ?@ADI@?, OC@ OTK@ ><I =@ PN@? DI <GBJMDOCHN OC<O >JHK<M@ J=E@>ON JA OC<O
OTK@. 1C@ .TPSU KMJK@MOT JA <MM<TN DN NP>C <I <GBJMDOCH. 1C@ AJGGJRDIB KMJBM<H >JINOMP>ON
10 J=E@>ON =T M<I?JH Q<GP@N <I? NJMON OC@H =T .TPSU. AN .TPSU RJMFN JI OC@ @G@H@ION, DO DN
OC@ PQ$NQ() JK@M<OJM OC<O B@ON ><GG@? =@CDI? OC@ N>@I@N OJ ?@O@MHDI@ OC@ NJMO JM?@MN JA OC@
@G@H@ION:
JNQPSU TUE.SBOEPN;
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
TUSVDU 5JNF0G%BZ
\
JOU IPVS;
JOU NJOVUF;
JOU PQ$NQ(DPOTU SFG 5JNF0G%BZ SIT) DPOTU
\
SFUVSO (IPVS == SIT.IPVS
NJOVUF - SIT.NJOVUF
: IPVS - SIT.IPVS);
^
TUSJOH UP4USJOH() DPOTU
\
SFUVSO GPSNBU("%02T:%02T", IPVS, NJOVUF);
^
^
WPJE NBJO()
\
5JNF0G%BZ<> UJNFT;
GPSFBDI (J; 0 .. 10) \
UJNFT _= 5JNF0G%BZ(VOJGPSN(0, 24), VOJGPSN(0, 60));
^
UJNFT.TPSU;
XSJUFMO(UJNFT);
^
AN @SK@>O@?, OC@ @G@H@ION <M@ NJMO@? AMJH OC@ @<MGD@NO ODH@ OJ OC@ G<O@NO ODH@:
<03:40, 04:10, 09:06, 10:03, 10:09, 11:04, 13:42, 16:40, 18:03, 21:08>
PQ$BMM() to call objects as functions
55.8
1C@ K<M@IOC@N@N <MJPI? OC@ K<M<H@O@M GDNO RC@I ><GGDIB API>ODJIN <M@ JK@M<OJMN <N R@GG. 4@
C<Q@ <GM@<?T N@@I CJR TUBUJD PQ$BMM() H<F@N DO KJNND=G@ OJ PN@ OC@ I<H@ JA OC@ FKB7 <N <
API>ODJI. TUBUJD PQ$BMM() <GGJRN >M@<ODIB J=E@>ON RDOC ?@A<PGO Q<GP@N <O MPI ODH@.
+JI-NO<OD> PQ$BMM() JI OC@ JOC@M C<I? <GGJRN PNDIB OC@ A4<75FE JA PN@M-?@ADI@? OTK@N <N
API>ODJIN:
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
307
'PP GPP;
GPP();
1C@ J=E@>O GPP <=JQ@ DN =@DIB ><GG@? GDF@ < API>ODJI.
AN <I @S<HKG@, G@O'N >JIND?@M < TUSVDU OC<O M@KM@N@ION < GDI@<M @LP<ODJI. 1CDN TUSVDU RDGG =@
PN@? AJM ><G>PG<ODIB OC@ K Q<GP@N JA OC@ AJGGJRDIB GDI@<M @LP<ODJI AJM NK@>DAD> J Q<GP@N:
y = ax + b
1C@ AJGGJRDIB PQ$BMM() NDHKGT ><G>PG<O@N <I? M@OPMIN OC@ Q<GP@ JA K <>>JM?DIB OJ OC<O
@LP<ODJI:
TUSVDU -JOFBS&RVBUJPO
\
EPVCMF B;
EPVCMF C;
EPVCMF PQ$BMM(EPVCMF Y) DPOTU
\
SFUVSO B * Y + C;
^
^
4DOC OC<O ?@ADIDODJI, @<>C J=E@>O JA -JOFBS&RVBUJPO M@KM@N@ION < GDI@<M @LP<ODJI AJM NK@>DAD>
3 <I? 4 Q<GP@N. 0P>C <I J=E@>O ><I =@ PN@? <N < API>ODJI OC<O ><G>PG<O@N OC@ K Q<GP@N:
-JOFBS&RVBUJPO FRVBUJPO = \ 1.2, 3.4 ^;
EPVCMF Z = FRVBUJPO(5.6); // UIF PCKFDU JT CFJOH VTFE MJLF B GVODUJPO
Note: D78;@;@9 opCall() 8AD 3 struct 6;E34>7E F:7 5A?B;>7D-97@7D3F76 3GFA?3F;5
5A@EFDG5FAD. ):3F ;E I:K F:7 { } EK@F3J ;E GE76 34AH7 ;@EF736 A8 F:7 D75A??7@676
LinearEquation(1.2, 3.4). ,:7@ F:7 >3FF7D EK@F3J ;E 67E;D76, 3 static opCall() F:3F
F3=7E FIA double B3D3?7F7DE ?GEF 3>EA 47 678;@76.
FRVBUJPO <=JQ@ M@KM@N@ION OC@ K = 1.2J + 3.4 GDI@<M @LP<ODJI. 2NDIB OC<O J=E@>O <N < API>ODJI
@S@>PO@N OC@ PQ$BMM() H@H=@M API>ODJI.
1CDN A@<OPM@ ><I =@ PN@APG OJ ?@ADI@ <I? NOJM@ OC@ 3 <I? 4 Q<GP@N DI <I J=E@>O JI>@ <I? OJ PN@
OC<O J=E@>O HPGODKG@ ODH@N G<O@M JI. 1C@ AJGGJRDIB >J?@ PN@N NP>C <I J=E@>O DI < GJJK:
-JOFBS&RVBUJPO FRVBUJPO = \ 0.01, 0.4 ^;
GPS (EPVCMF Y = 0.0; Y <= 1.0; Y += 0.125) \
XSJUFGMO("%G: %G", Y, FRVBUJPO(Y));
^
1C<O J=E@>O M@KM@N@ION OC@ K = 0.01J + 0.4 @LP<ODJI. &O DN =@DIB PN@? AJM ><G>PG<ODIB OC@
M@NPGON AJM J Q<GP@N DI OC@ M<IB@ AMJH 0.0 OJ 1.0.
Indexing operators
55.9
PQ*OEFY(), PQ*OEFY"TTJHO(), PQ*OEFY6OBSZ(), <I? PQ*OEFY0Q"TTJHO() H<F@ DO KJNND=G@ OJ
PN@ DI?@SDIB JK@M<OJMN AJM PN@M-?@ADI@? OTK@N NDHDG<M OJ <MM<TN <N DI PCKFDU<JOEFY>.
!DAA@M@IO AMJH <MM<TN, OC@M@ ><I =@ HJM@ OC<I JI@ DI?@S DI NLP<M@ =M<>F@ON. 1C@ @S<>O
=@C<QDJM JA OC@N@ JK@M<OJMN ?@K@I? JI OC@ PN@M-?@ADI@? OTK@. 4@ RDGG N@@ < N<HKG@ KMJBM<H
OC<O DI>GP?@N OC@N@ JK@M<OJMN =@GJR.
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
308
PQ*OEFY DN AJM @G@H@IO <>>@NN. 1C@ DI?@S@N RDOCDI =M<>F@ON =@>JH@ OC@ K<M<H@O@MN JA OC@
JK@M<OJM API>ODJI:
HSBEF = HSBEFT<3, 1>; // BDDFTT UIF FMFNFOU BU 3,1
HSBEF = HSBEFT.PQ*OEFY(3, 1); // UIF FRVJWBMFOU PG UIF BCPWF
PQ*OEFY6OBSZ DN NDHDG<M OJ PQ6OBSZ. &ON ?DAA@M@I>@ DN OC<O OC@ JK@M<ODJI DN <KKGD@? OJ OC@
7>7?7@F OC<O >JMM@NKJI?N OJ OC@ NK@>DAD@? DI?@S(@N):
++HSBEFT<4, 0>; // JODSFNFOU UIF FMFNFOU BU 4,0
HSBEFT.PQ*OEFY6OBSZ!"++"(4, 0); // UIF FRVJWBMFOU PG UIF BCPWF
PQ*OEFY"TTJHO DN AJM <NNDBIH@IO. &ON ADMNO K<M<H@O@M DN OC@ Q<GP@ OC<O DN OJ =@ <NNDBI@?, <I?
OC@ M@NO JA OC@ K<M<H@O@MN <M@ OC@ DI?@S(@N):
HSBEFT<1, 1> = 95; // BTTJHO 95 UP FMFNFOU 1,1
HSBEFT.PQ*OEFY"TTJHO(95, 1, 1); // UIF FRVJWBMFOU PG UIF BCPWF
PQ*OEFY0Q"TTJHO DN NDHDG<M OJ PQ0Q"TTJHO. &ON ?DAA@M@I>@ DN OC<O OC@ M@NPGO JA OC@ JK@M<ODJI DN
<NNDBI@? =<>F OJ OC@ NK@>DAD@? @G@H@IO:
HSBEFT<2, 1> += 42; // BEE UP UIF FMFNFOU BU 2,1
HSBEFT.PQ*OEFY0Q"TTJHO!"+"(42, 2, 1);
// UIF FRVJWBMFOU PG UIF BCPWF
Indexing operators example 55.9.1
1C@ AJGGJRDIB NOMP>O DI>GP?@N @S<HKG@N JA @<>C JA OC@N@ JK@M<OJMN. &I OCDN ?@NDBI, OC@ ADMNO
DI?@S NK@>DAD@N OC@ NOP?@IO <I? OC@ N@>JI? DI?@S NK@>DAD@N OC@ BM<?@ JA OC<O NOP?@IO. #JM
@S<HKG@, <1, 2> NK@>DAD@N OC@ BM<?@ D? 2 JA NOP?@IO D? 1.
JNQPSU TUE.TUEJP;
TUSVDU 4UVEFOU(SBEFT
\
// 5XP HSBEFT GPS GJWF TUVEFOUT
JOU<2><5> HSBEFT;
// 3FUVSOT UIF TQFDJGJFE HSBEF
JOU PQ*OEFY(TJ[F@U TUVEFOU*E, TJ[F@U HSBEF*E) DPOTU
\
SFUVSO HSBEFT<TUVEFOU*E><HSBEF*E>;
^
// *ODSFNFOUT UIF TQFDJGJFE HSBEF
SFG JOU PQ*OEFY6OBSZ(TUSJOH PQ)(TJ[F@U TUVEFOU*E,
TJ[F@U HSBEF*E)
JG (PQ == "++")
\
SFUVSO ++HSBEFT<TUVEFOU*E><HSBEF*E>;
^
// "TTJHOT UP UIF TQFDJGJFE HSBEF PG UIF TQFDJGJFE TUVEFOU
JOU PQ*OEFY"TTJHO(JOU HSBEF,
TJ[F@U TUVEFOU*E,
TJ[F@U HSBEF*E)
\
SFUVSO HSBEFT<TUVEFOU*E><HSBEF*E> = HSBEF;
^
// "EET UP UIF TQFDJGJFE HSBEF
SFG JOU PQ*OEFY0Q"TTJHO(TUSJOH PQ)(JOU BNPVOU,
TJ[F@U TUVEFOU*E,
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
309
TJ[F@U HSBEF*E)
JG (PQ == "+")
\
SFUVSO HSBEFT<TUVEFOU*E><HSBEF*E> += BNPVOU;
^
^
WPJE NBJO()
\
4UVEFOU(SBEFT HSBEFT;
XSJUFMO("(SBEF GPS JOEFY 3,1: ",
HSBEFT<3, 1>); // BDDFTT
++HSBEFT<4, 0>; // JODSFNFOU
HSBEFT<1, 1> = 95; // BTTJHONFOU
HSBEFT<2, 1> += 42; // BEE-BTTJHONFOU
XSJUFMO(HSBEFT);
^
1C@ JPOKPO:
(SBEF GPS JOEFY 3,1: 0
4UVEFOU(SBEFT(<<0, 0>, <0, 95>, <0, 42>, <0, 0>, <1, 0>>)
+JO@ OC<O OC@ M@OPMI OTK@N JA PQ*OEFY6OBSZ <I? PQ*OEFY0Q"TTJHO <M@ H<MF@? <N SFG
<>>JM?DIB OJ OC@ M@OPMI OTK@ BPD?@GDI@N <=JQ@. A??DODJI<GGT, OC@ JK@M<OJMN ?J IJO ?J <ITOCDIB
NK@>D<G AJM OC@ M@OPMI Q<GP@N; OC@T NDHKGT M@OPMI OC@ M@NPGON JA OC@ ++ <I? += JK@M<OJMN OC<O
OC@T PN@ DIO@MI<GGT.
Slicing operators
55.10
PQ4MJDF, PQ4MJDF6OBSZ, PQ4MJDF"TTJHO, <I? PQ4MJDF0Q"TTJHO <M@ NDHDG<M OJ OC@ DI?@SDIB
JK@M<OJMN. 1C@DM ?DAA@M@I>@ DN OC<O OC@T <GGJR E>;5;@9 OC@ J=E@>ON JA PN@M-?@ADI@? OTK@N.
AGG JA OC@N@ JK@M<OJMN C<Q@ ORJ ?DNODI>O PN@N: 1C@ PN@ RC@M@ OC@ NLP<M@ =M<>F@ON <M@ @HKOT
<I? OC@ PN@ RC@M@ OC@ NLP<M@ =M<>F@ON >JIO<DI < IPH=@M M<IB@. 0DHDG<M OJ NGD>@N, OC@N@
NCJPG? =@ ?@ADI@? OJ H@<I 3>> A8 F:7 7>7?7@FE <I? EA?7 A8 F:7 7>7?7@FE, M@NK@>ODQ@GT.
0GD>DIB JK@M<OJMN <M@ OC@ HJNO >JHKG@S JK@M<OJMN =@><PN@ OC@T DIQJGQ@ OC@ ORJ ?DNODI>O
>JI>@KON 5A@F3;@7D <I? D3@97. 4@ RDGG N@@ OC@N@ >JI>@KON DI HJM@ ?@O<DG DI G<O@M >C<KO@MN.
1C@N@ JK@M<OJMN RDGG H<F@ HJM@ N@IN@ JIGT <AO@M PI?@MNO<I?DIB M<IB@N.
#JM IJR, & RDGG KM@N@IO <I @S<HKG@ OC<O >JINDNON JIGT JA NOP= OTK@N <I? NOP= API>ODJIN. 0DHDG<M
OJ @<MGD@M @S<HKG@N, OCDN @S<HKG@ PN@N JIGT OC@ "++" <I? "+" >C<M<>O@MN. +JMH<GGT, H<IT
JOC@M JK@M<OJM >C<M<>O@MN HPNO <GNJ =@ ?@ADI@?.
/* 5IF UZQF PG UIF FMFNFOUT DPOUBJOFE JO UIF DPOUBJOFS. */
TUSVDU &MFNFOU
\
// ...
^
/* 4JNJMBS UP TMJDFT, SFQSFTFOUT UIF DPODFQU PG "FMFNFOU
* SBOHF". */
TUSVDU 3BOHF
\
// ...
^
/* 4JNJMBS UP BSSBZT BOE BTTPDJBUJWF BSSBZT, CSJOHT FMFNFOUT
* UPHFUIFS. */
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
310
TUSVDU $POUBJOFS
\
// 'PS UIF PCKFDU<> TZOUBY
3BOHF PQ4MJDF()
\
3BOHF BMM&MFNFOUT;
// ... NVTU QSPWJEF BDDFTT UP BMM FMFNFOUT ...
SFUVSO BMM&MFNFOUT;
^
// 'PS UIF PCKFDU<J .. K> TZOUBY
3BOHF PQ4MJDF(TJ[F@U J, TJ[F@U K)
\
3BOHF TQFDJGJFE&MFNFOUT;
// ... NVTU QSPWJEF BDDFTT UP UIF TQFDJGJFE FMFNFOUT ...
SFUVSO TQFDJGJFE&MFNFOUT;
^
// 'PS UIF ++PCKFDU<> TZOUBY
3BOHF PQ4MJDF6OBSZ(TUSJOH PQ)()
JG (PQ == "++")
\
3BOHF BMM&MFNFOUT;
// ... NVTU JODSFNFOU UIF WBMVFT PG BMM FMFNFOUT ...
SFUVSO BMM&MFNFOUT;
^
// 'PS UIF ++PCKFDU<J .. K> TZOUBY
3BOHF PQ4MJDF6OBSZ(TUSJOH PQ)(TJ[F@U J, TJ[F@U K)
JG (PQ == "++")
\
3BOHF TQFDJGJFE&MFNFOUT;
// ... NVTU JODSFNFOU UIF WBMVFT PG UIF TQFDJGJFE FMFNFOUT ...
SFUVSO TQFDJGJFE&MFNFOUT;
^
// 'PS UIF (PCKFDU<> = WBMVF) TZOUBY
3BOHF PQ4MJDF"TTJHO(&MFNFOU WBMVF)
\
3BOHF BMM&MFNFOUT;
// ... NVTU BTTJHO UP BMM FMFNFOUT ...
SFUVSO BMM&MFNFOUT;
^
// 'PS UIF (PCKFDU<J .. K> = WBMVF) TZOUBY
3BOHF PQ4MJDF"TTJHO(&MFNFOU WBMVF, TJ[F@U J, TJ[F@U K)
\
3BOHF TQFDJGJFE&MFNFOUT;
// ... NVTU BTTJHO UP UIF TQFDJGJFE FMFNFOUT ...
SFUVSO TQFDJGJFE&MFNFOUT;
^
// 'PS UIF (PCKFDU<> += WBMVF) TZOUBY
3BOHF PQ4MJDF0Q"TTJHO(TUSJOH PQ)(&MFNFOU WBMVF)
JG (PQ == "+")
\
3BOHF BMM&MFNFOUT;
// ... NVTU BEE UP BMM FMFNFOUT ...
SFUVSO BMM&MFNFOUT;
^
// 'PS UIF (PCKFDU<J .. K> += WBMVF) TZOUBY
3BOHF PQ4MJDF0Q"TTJHO(TUSJOH PQ)(&MFNFOU WBMVF,
TJ[F@U J, TJ[F@U K)
JG (PQ == "+")
\
3BOHF TQFDJGJFE&MFNFOUT;
// ... NVTU BEE UP UIF TQFDJGJFE FMFNFOUT ...
SFUVSO TQFDJGJFE&MFNFOUT;
^
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
311
// ... PUIFS PQFSBUPS PWFSMPBET BT OFFEFE ...
^
WPJE NBJO()
\
$POUBJOFS D;
3BOHF S;
&MFNFOU F;
/* 5IF GPMMPXJOH MJOFT BSF FYBNQMFT PG XIBU UIF QSFWJPVT
* PQFSBUPS PWFSMPBET NBLF QPTTJCMF. 5IF FRVJWBMFOUT UIBU
* UIF DPNQJMFS VTFT CFIJOE UIF TDFOFT BSF TIPXO BT DPEF
* DPNNFOUT. */
S = D<>; // D.PQ4MJDF();
S = D<1 .. 5>; // D.PQ4MJDF(1, 5);
++D<>; // D.PQ4MJDF6OBSZ!"++"();
++D<2 .. 9>; // D.PQ4MJDF6OBSZ!"++"(2, 9);
D<> = F; // D.PQ4MJDF"TTJHO(F);
D<4 .. 6> = F; // D.PQ4MJDF"TTJHO(F, 4, 6);
D<> += F; // D.PQ4MJDF0Q"TTJHO!"+"(F);
D<5 .. 8> += F; // D.PQ4MJDF0Q"TTJHO!"+"(F, 5, 8);
^
PQ$BTU for type conversions
55.11
PQ$BTU ?@ADI@N OC@ @SKGD>DO OTK@ >JIQ@MNDJIN. &O ><I =@ JQ@MGJ<?@? N@K<M<O@GT AJM @<>C O<MB@O
OTK@. AN TJP RJPG? M@H@H=@M AMJH OC@ @<MGD@M >C<KO@MN, @SKGD>DO OTK@ >JIQ@MNDJIN <M@
K@MAJMH@? =T OC@ UP API>ODJI <I? OC@ DBTU JK@M<OJM.
PQ$BTU DN < O@HKG<O@ <N R@GG, =PO DO C<N < ?DAA@M@IO AJMH<O: 1C@ O<MB@O OTK@ DN NK@>DAD@? =T OC@
(5 : UBSHFU@UZQF) NTIO<S:
target_type PQ$BTU(5 : target_type)()
\
// ...
^
1CDN NTIO<S RDGG =@>JH@ >G@<M G<O@M <AO@M OC@ O@HKG<O@N >C<KO@M <N R@GG.
)@O'N >C<IB@ OC@ ?@ADIDODJI JA %VSBUJPO NJ OC<O DO IJR C<N ORJ H@H=@MN: CJPMN <I? HDIPO@N.
1C@ JK@M<OJM OC<O >JIQ@MON J=E@>ON JA OCDN OTK@ OJ EPVCMF ><I =@ ?@ADI@? <N DI OC@ AJGGJRDIB
>J?@:
JNQPSU TUE.TUEJP;
JNQPSU TUE.DPOW;
TUSVDU %VSBUJPO
\
JOU IPVS;
JOU NJOVUF;
EPVCMF PQ$BTU(5 : EPVCMF)() DPOTU
\
SFUVSO IPVS + (UP!EPVCMF(NJOVUF) / 60);
^
^
WPJE NBJO()
\
BVUP EVSBUJPO = %VSBUJPO(2, 30);
EPVCMF E = UP!EPVCMF(EVSBUJPO); // DBO CF DBTU(EPVCMF)EVSBUJPO BT XFMM
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
312
XSJUFMO(E);
^
1C@ >JHKDG@M M@KG<>@N OC@ OTK@ >JIQ@MNDJI ><GG <=JQ@ RDOC OC@ AJGGJRDIB JI@:
EPVCMF E = EVSBUJPO.PQ$BTU!EPVCMF();
1C@ EPVCMF >JIQ@MNDJI API>ODJI <=JQ@ KMJ?P>@N 2.5 AJM ORJ CJPMN <I? OCDMOT HDIPO@N:
2.5
Catch-all operator PQ%JTQBUDI
55.12
PQ%JTQBUDI B@ON ><GG@? RC@I@Q@M < ?;EE;@9 H@H=@M JA <I J=E@>O DN <>>@NN@?. AGG <OO@HKON OJ
<>>@NN IJI-@SDNO@IO H@H=@MN <M@ ?DNK<O>C@? OJ OCDN API>ODJI.
1C@ I<H@ JA OC@ HDNNDIB H@H=@M =@>JH@N OC@ O@HKG<O@ K<M<H@O@M Q<GP@ JA PQ%JTQBUDI.
1C@ AJGGJRDIB >J?@ ?@HJINOM<O@N < NDHKG@ ?@ADIDODJI:
JNQPSU TUE.TUEJP;
TUSVDU 'PP
\
WPJE PQ%JTQBUDI(TUSJOH OBNF, 5)(5 QBSBNFUFS)
\
XSJUFGMO("'PP.PQ%JTQBUDI - OBNF: %T, WBMVF: %T",
OBNF, QBSBNFUFS);
^
^
WPJE NBJO()
\
'PP GPP;
GPP.B/PO&YJTUFOU'VODUJPO(42);
GPP.BOPUIFS/PO&YJTUFOU'VODUJPO(100);
^
1C@M@ <M@ IJ >JHKDG@M @MMJMN AJM OC@ ><GGN OJ IJI-@SDNO@IO H@H=@MN. &INO@<?, <GG JA OCJN@ ><GGN
<M@ ?DNK<O>C@? OJ PQ%JTQBUDI. 1C@ ADMNO O@HKG<O@ K<M<H@O@M DN OC@ I<H@ JA OC@ H@H=@M. 1C@
K<M<H@O@M Q<GP@N OC<O <M@ PN@? RC@I ><GGDIB OC@ API>ODJI <KK@<M <N OC@ K<M<H@O@MN JA
PQ%JTQBUDI:
'PP.PQ%JTQBUDI - OBNF: B/PO&YJTUFOU'VODUJPO, WBMVF: 42
'PP.PQ%JTQBUDI - OBNF: BOPUIFS/PO&YJTUFOU'VODUJPO, WBMVF: 100
1C@ OBNF O@HKG<O@ K<M<H@O@M ><I =@ PN@? DIND?@ OC@ API>ODJI OJ H<F@ ?@>DNDJIN JI CJR OC@
><GG OJ OC<O NK@>DAD> IJI-@SDNO@IO API>ODJI NCJPG? =@ C<I?G@?:
TXJUDI (OBNF) \
// ...
^
Inclusion query by PQ#JOBSZ3JHIU!"JO"
55.13
1CDN JK@M<OJM <GGJRN ?@ADIDIB OC@ =@C<QDJM JA OC@ JO JK@M<OJM AJM PN@M-?@ADI@? OTK@N. JO DN
>JHHJIGT PN@? RDOC <NNJ>D<ODQ@ <MM<TN OJ ?@O@MHDI@ RC@OC@M < Q<GP@ AJM < NK@>DAD> F@T @SDNON
DI OC@ <MM<T.
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
313
!DAA@M@IO AMJH JOC@M JK@M<OJMN, OCDN JK@M<OJM DN IJMH<GGT JQ@MGJ<?@? AJM OC@ ><N@ RC@M@ OC@
J=E@>O <KK@<MN JI OC@ MDBCO-C<I? ND?@:
JG (UJNF JO MVODI#SFBL) \
1C@ >JHKDG@M RJPG? PN@ PQ#JOBSZ3JHIU =@CDI? OC@ N>@I@N:
// UIF FRVJWBMFOU PG UIF BCPWF:
JG (MVODI#SFBL.PQ#JOBSZ3JHIU!"JO"(UJNF)) \
Example of the JO operator 55.13.1
1C@ AJGGJRDIB KMJBM<H ?@ADI@N < 5JNF4QBO OTK@ DI <??DODJI OJ %VSBUJPO <I? 5JNF0G%BZ. 1C@
JO JK@M<OJM OC<O DN ?@ADI@? AJM 5JNF4QBO ?@O@MHDI@N RC@OC@M < HJH@IO DI ODH@ DN RDOCDI OC<O
ODH@ NK<I.
1J F@@K OC@ >J?@ NCJMO, OC@ AJGGJRDIB KMJBM<H ?@ADI@N JIGT OC@ I@>@NN<MT H@H=@M API>ODJIN.
+JO@ CJR OC@ 5JNF0G%BZ J=E@>O DN PN@? N@<HG@NNGT DI OC@ GPS GJJK. 1C<O GJJK DN <
?@HJINOM<ODJI JA CJR PN@APG JK@M<OJM JQ@MGJ<?DIB ><I =@.
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
TUSVDU %VSBUJPO
\
JOU NJOVUF;
^
TUSVDU 5JNF0G%BZ
\
JOU IPVS;
JOU NJOVUF;
SFG 5JNF0G%BZ PQ0Q"TTJHO(TUSJOH PQ)(JO %VSBUJPO EVSBUJPO)
JG (PQ == "+")
\
NJOVUF += EVSBUJPO.NJOVUF;
IPVS += NJOVUF / 60;
NJOVUF %= 60;
IPVS %= 24;
SFUVSO UIJT;
^
JOU PQ$NQ(DPOTU SFG 5JNF0G%BZ SIT) DPOTU
\
SFUVSO (IPVS == SIT.IPVS
NJOVUF - SIT.NJOVUF
: IPVS - SIT.IPVS);
^
TUSJOH UP4USJOH() DPOTU
\
SFUVSO GPSNBU("%02T:%02T", IPVS, NJOVUF);
^
^
TUSVDU 5JNF4QBO
\
5JNF0G%BZ CFHJO;
5JNF0G%BZ FOE; // FOE JT DPOTJEFSFE UP CF PVUTJEF PG UIF TQBO
CPPM PQ#JOBSZ3JHIU(TUSJOH PQ)(5JNF0G%BZ UJNF) DPOTU
JG (PQ == "JO")
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
314
\
SFUVSO (UJNF >= CFHJO) && (UJNF < FOE);
^
^
WPJE NBJO()
\
BVUP MVODI#SFBL = 5JNF4QBO(5JNF0G%BZ(12, 00),
5JNF0G%BZ(13, 00));
GPS (BVUP UJNF = 5JNF0G%BZ(11, 30);
UJNF < 5JNF0G%BZ(13, 30);
UJNF += %VSBUJPO(15)) \
JG (UJNF JO MVODI#SFBL) \
XSJUFMO(UJNF, " JT EVSJOH UIF MVODI CSFBL");
^ FMTF \
XSJUFMO(UJNF, " JT PVUTJEF PG UIF MVODI CSFBL");
^
^
^
1C@ JPOKPO:
11:30 JT PVUTJEF PG UIF MVODI CSFBL
11:45 JT PVUTJEF PG UIF MVODI CSFBL
12:00 JT EVSJOH UIF MVODI CSFBL
12:15 JT EVSJOH UIF MVODI CSFBL
12:30 JT EVSJOH UIF MVODI CSFBL
12:45 JT EVSJOH UIF MVODI CSFBL
13:00 JT PVUTJEF PG UIF MVODI CSFBL
13:15 JT PVUTJEF PG UIF MVODI CSFBL
Defining more than one operator at the same time
55.14
1J F@@K OC@ >J?@ N<HKG@N NCJMO, R@ C<Q@ PN@? JIGT OC@ ++, +, <I? += JK@M<OJMN <=JQ@. &O DN
>JI>@DQ<=G@ OC<O RC@I JI@ JK@M<OJM DN JQ@MGJ<?@? AJM < OTK@, H<IT JOC@MN RJPG? <GNJ I@@? OJ
=@ JQ@MGJ<?@?. #JM @S<HKG@, OC@ -- <I? -= JK@M<OJMN <M@ <GNJ ?@ADI@? AJM OC@ AJGGJRDIB
%VSBUJPO:
TUSVDU %VSBUJPO
\
JOU NJOVUF;
SFG %VSBUJPO PQ6OBSZ(TUSJOH PQ)()
JG (PQ == "++")
\
++NJOVUF;
SFUVSO UIJT;
^
SFG %VSBUJPO PQ6OBSZ(TUSJOH PQ)()
JG (PQ == "--")
\
--NJOVUF;
SFUVSO UIJT;
^
SFG %VSBUJPO PQ0Q"TTJHO(TUSJOH PQ)(JO JOU BNPVOU)
JG (PQ == "+")
\
NJOVUF += BNPVOU;
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
315
SFUVSO UIJT;
^
SFG %VSBUJPO PQ0Q"TTJHO(TUSJOH PQ)(JO JOU BNPVOU)
JG (PQ == "-")
\
NJOVUF -= BNPVOU;
SFUVSO UIJT;
^
^
VOJUUFTU
\
BVUP EVSBUJPO = %VSBUJPO(10);
++EVSBUJPO;
BTTFSU(EVSBUJPO.NJOVUF == 11);
--EVSBUJPO;
BTTFSU(EVSBUJPO.NJOVUF == 10);
EVSBUJPO += 5;
BTTFSU(EVSBUJPO.NJOVUF == 15);
EVSBUJPO -= 3;
BTTFSU(EVSBUJPO.NJOVUF == 12);
^
WPJE NBJO()
\^
1C@ JK@M<OJM JQ@MGJ<?N <=JQ@ C<Q@ >J?@ ?PKGD><ODJIN. 1C@ JIGT ?DAA@M@I>@N =@OR@@I OC@
NDHDG<M API>ODJIN <M@ CDBCGDBCO@? =T T@GGJR. 0P>C >J?@ ?PKGD><ODJIN ><I =@ M@?P>@? <I?
NJH@ODH@N <QJD?@? <GOJB@OC@M =T EFD;@9 ?;J;@E. 4@ RDGG N@@ OC@ NJYJO F@TRJM? DI < G<O@M
>C<KO@M <N R@GG. & RJPG? GDF@ OJ NCJR =MD@AGT CJR OCDN F@TRJM? C@GKN RDOC JK@M<OJM
JQ@MGJ<?DIB.
NJYJO DIN@MON OC@ NK@>DAD@? NOMDIB <N NJPM>@ >J?@ MDBCO RC@M@ OC@ NJYJO NO<O@H@IO <KK@<MN DI
>J?@. 1C@ AJGGJRDIB NOMP>O DN OC@ @LPDQ<G@IO JA OC@ JI@ <=JQ@:
TUSVDU %VSBUJPO
\
JOU NJOVUF;
SFG %VSBUJPO PQ6OBSZ(TUSJOH PQ)()
JG ((PQ == "++") ]] (PQ == "--"))
\
NJYJO (PQ _ "NJOVUF;");
SFUVSO UIJT;
^
SFG %VSBUJPO PQ0Q"TTJHO(TUSJOH PQ)(JO JOU BNPVOU)
JG ((PQ == "+") ]] (PQ == "-"))
\
NJYJO ("NJOVUF " _ PQ _ "= BNPVOU;");
SFUVSO UIJT;
^
^
&A OC@ %VSBUJPO J=E@>ON <GNJ I@@? OJ =@ HPGODKGD@? <I? ?DQD?@? =T <I <HJPIO, <GG OC<O DN
I@@?@? DN OJ <?? ORJ HJM@ >JI?DODJIN OJ OC@ O@HKG<O@ >JINOM<DIO:
TUSVDU %VSBUJPO
\
// ...
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
316
SFG %VSBUJPO PQ0Q"TTJHO(TUSJOH PQ)(JO JOU BNPVOU)
JG ((PQ == "+") ]] (PQ == "-") ]]
(PQ == "*") ]] (PQ == "/"))
\
NJYJO ("NJOVUF " _ PQ _ "= BNPVOU;");
SFUVSO UIJT;
^
^
VOJUUFTU
\
BVUP EVSBUJPO = %VSBUJPO(12);
EVSBUJPO *= 4;
BTTFSU(EVSBUJPO.NJOVUF == 48);
EVSBUJPO /= 2;
BTTFSU(EVSBUJPO.NJOVUF == 24);
^
Exercise
55.15
t !@ADI@ < AM<>ODJI OTK@ OC<O CJG?N DON IPH@M<OJM <I? ?@IJHDI<OJM <N H@H=@MN JA OTK@ MPOH.
0P>C < OTK@ H<T =@ PN@APG, =@><PN@ DO ?J@N IJO GJN@ Q<GP@ GDF@ GMPBU, EPVCMF, <I? SFBM ?J
?P@ OJ OC@DM KM@>DNDJIN. #JM @S<HKG@, <GOCJPBC OC@ M@NPGO JA HPGODKGTDIB < EPVCMF Q<GP@ JA
1.0/3 =T 3 DN @AF 1.0, HPGODKGTDIB < 'SBDUJPO J=E@>O OC<O M@KM@N@ION OC@ AM<>ODJI 13 =T 3
RJPG? =@ @S<>OGT 1:
TUSVDU 'SBDUJPO
\
MPOH OVN; // OVNFSBUPS
MPOH EFO; // EFOPNJOBUPS
/* "T B DPOWFOJFODF, UIF DPOTUSVDUPS VTFT UIF EFGBVMU
* WBMVF PG 1 GPS UIF EFOPNJOBUPS. */
UIJT(MPOH OVN, MPOH EFO = 1)
\
FOGPSDF(EFO != 0, "5IF EFOPNJOBUPS DBOOPU CF [FSP");
UIJT.OVN = OVN;
UIJT.EFO = EFO;
/* &OTVSJOH UIBU UIF EFOPNJOBUPS JT BMXBZT QPTJUJWF
* XJMM TJNQMJGZ UIF EFGJOJUJPOT PG TPNF PG UIF
* PQFSBUPS GVODUJPOT. */
JG (UIJT.EFO < 0) \
UIJT.OVN = -UIJT.OVN;
UIJT.EFO = -UIJT.EFO;
^
^
/* ... ZPV EFGJOF UIF PQFSBUPS PWFSMPBET ... */
^
!@ADI@ JK@M<OJMN <N I@@?@? AJM OCDN OTK@ OJ H<F@ DO < >JIQ@ID@IO OTK@ <N >GJN@ OJ
API?<H@IO<G OTK@N <N KJNND=G@. "INPM@ OC<O OC@ ?@ADIDODJI JA OC@ OTK@ K<NN@N <GG JA OC@
AJGGJRDIB PIDO O@NON. 1C@ PIDO O@NON @INPM@ OC@ AJGGJRDIB =@C<QDJMN:
w AI @S>@KODJI HPNO =@ OCMJRI RC@I >JINOMP>ODIB <I J=E@>O RDOC U@MJ ?@IJHDI<OJM.
(1CDN DN <GM@<?T O<F@I ><M@ JA =T OC@ FOGPSDF @SKM@NNDJI <=JQ@.)
w -MJ?P>DIB OC@ I@B<ODQ@ JA OC@ Q<GP@: #JM @S<HKG@, OC@ I@B<ODQ@ JA 13 NCJPG? =@
-13 <I? I@B<ODQ@ JA -25 NCJPG? =@ 25.
w &I>M@H@IODIB <I? ?@>M@H@IODIB OC@ Q<GP@ =T ++ <I? --.
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
317
w 0PKKJMO AJM AJPM <MDOCH@OD> JK@M<ODJIN: BJOC HJ?DATDIB OC@ Q<GP@ JA <I J=E@>O =T
+=, -=, *=, <I? /=; <I? KMJ?P>DIB OC@ M@NPGO JA PNDIB ORJ J=E@>ON RDOC OC@ +, -, *,
<I? / JK@M<OJMN. (0DHDG<M OJ OC@ >JINOMP>OJM, ?DQD?DIB =T U@MJ NCJPG? =@
KM@Q@IO@?.)
AN < M@HDI?@M, C@M@ <M@ OC@ AJMHPG<N JA <MDOCH@OD> JK@M<ODJIN OC<O DIQJGQ@ ORJ
AM<>ODJIN <= <I? >?:
v A??DODJI: <= + >? = (<*? + >*=)(=*?)
v 0P=OM<>ODJI: <= - >? = (<*? - >*=)(=*?)
v *PGODKGD><ODJI: <= * >? = (<*>)(=*?)
v !DQDNDJI: (<=) / (>?) = (<*?)(=*>)
w 1C@ <>OP<G (<I? I@>@NN<MDGT GJNNAPG) Q<GP@ JA OC@ J=E@>O ><I =@ >JIQ@MO@? OJ
EPVCMF.
w 0JMO JM?@M <I? @LP<GDOT >JHK<MDNJIN <M@ K@MAJMH@? =T OC@ <>OP<G Q<GP@N JA OC@
AM<>ODJIN, IJO =T OC@ Q<GP@N JA OC@ IPH@M<OJMN <I? ?@IJHDI<OJMN. #JM @S<HKG@, OC@
AM<>ODJIN 13 <I? 2060 HPNO =@ >JIND?@M@? OJ =@ @LP<G.
VOJUUFTU
\
/* .VTU UISPX XIFO EFOPNJOBUPS JT [FSP. */
BTTFSU5ISPXO('SBDUJPO(42, 0));
/* -FU'T TUBSU XJUI 13. */
BVUP B = 'SBDUJPO(1, 3);
/* -13 */
BTTFSU(-B == 'SBDUJPO(-1, 3));
/* 13 + 1 == 43 */
++B;
BTTFSU(B == 'SBDUJPO(4, 3));
/* 43 - 1 == 13 */
--B;
BTTFSU(B == 'SBDUJPO(1, 3));
/* 13 + 23 == 33 */
B += 'SBDUJPO(2, 3);
BTTFSU(B == 'SBDUJPO(1));
/* 33 - 23 == 13 */
B -= 'SBDUJPO(2, 3);
BTTFSU(B == 'SBDUJPO(1, 3));
/* 13 * 8 == 83 */
B *= 'SBDUJPO(8);
BTTFSU(B == 'SBDUJPO(8, 3));
/* 83 / 169 == 32 */
B /= 'SBDUJPO(16, 9);
BTTFSU(B == 'SBDUJPO(3, 2));
/* .VTU QSPEVDF UIF FRVJWBMFOU WBMVF JO UZQF 'EPVCMF'.
*
* /PUF UIBU BMUIPVHI EPVCMF DBOOPU SFQSFTFOU FWFSZ WBMVF
* QSFDJTFMZ, 1.5 JT BO FYDFQUJPO. 5IBU JT XIZ UIJT UFTU
* JT CFJOH BQQMJFE BU UIJT QPJOU. */
BTTFSU(UP!EPVCMF(B) == 1.5);
/* 1.5 + 2.5 == 4 */
BTTFSU(B + 'SBDUJPO(5, 2) == 'SBDUJPO(4, 1));
/* 1.5 - 0.75 == 0.75 */
BTTFSU(B - 'SBDUJPO(3, 4) == 'SBDUJPO(3, 4));
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
318
/* 1.5 * 10 == 15 */
BTTFSU(B * 'SBDUJPO(10) == 'SBDUJPO(15, 1));
/* 1.5 / 4 == 38 */
BTTFSU(B / 'SBDUJPO(4) == 'SBDUJPO(3, 8));
/* .VTU UISPX XIFO EJWJEJOH CZ [FSP. */
BTTFSU5ISPXO('SBDUJPO(42, 1) / 'SBDUJPO(0));
/* 5IF POF XJUI MPXFS OVNFSBUPS JT CFGPSF. */
BTTFSU('SBDUJPO(3, 5) < 'SBDUJPO(4, 5));
/* 5IF POF XJUI MBSHFS EFOPNJOBUPS JT CFGPSF. */
BTTFSU('SBDUJPO(3, 9) < 'SBDUJPO(3, 8));
BTTFSU('SBDUJPO(1, 1@000) > 'SBDUJPO(1, 10@000));
/* 5IF POF XJUI MPXFS WBMVF JT CFGPSF. */
BTTFSU('SBDUJPO(10, 100) < 'SBDUJPO(1, 2));
/* 5IF POF XJUI OFHBUJWF WBMVF JT CFGPSF. */
BTTFSU('SBDUJPO(-1, 2) < 'SBDUJPO(0));
BTTFSU('SBDUJPO(1, -2) < 'SBDUJPO(0));
/* 5IF POFT XJUI FRVBM WBMVFT NVTU CF CPUI <= BOE >=. */
BTTFSU('SBDUJPO(-1, -2) <= 'SBDUJPO(1, 2));
BTTFSU('SBDUJPO(1, 2) <= 'SBDUJPO(-1, -2));
BTTFSU('SBDUJPO(3, 7) <= 'SBDUJPO(9, 21));
BTTFSU('SBDUJPO(3, 7) >= 'SBDUJPO(9, 21));
/* 5IF POFT XJUI FRVBM WBMVFT NVTU CF FRVBM. */
BTTFSU('SBDUJPO(1, 3) == 'SBDUJPO(20, 60));
/* 5IF POFT XJUI FRVBM WBMVFT XJUI TJHO NVTU CF FRVBM. */
BTTFSU('SBDUJPO(-1, 2) == 'SBDUJPO(1, -2));
BTTFSU('SBDUJPO(1, 2) == 'SBDUJPO(-1, -2));
^
TUE.FYDFQUJPO.BTTFSU5ISPXO OC<O <KK@<MN DI OC@ O@NON <=JQ@ DN AJM @INPMDIB OC<O <I
@SKM@NNDJI ?J@N DI?@@? OCMJR <I @S>@KODJI. &O RJMFN <N OC@ @LPDQ<G@IO JA OC@
AJGGJRDIB >J?@:
BTTFSU5ISPXO('SBDUJPO(42, 1) / 'SBDUJPO(0));
// ...
/* 5IF FRVJWBMFOU PG UIF MJOF BCPWF */
\
BVUP JT5ISPXO = GBMTF;
USZ \
'SBDUJPO(42, 1) / 'SBDUJPO(0);
^ DBUDI (&YDFQUJPO FYD) \
JT5ISPXO = USVF;
^
BTTFSU(JT5ISPXO);
^
... F:7 EA>GF;A@
%B7D3FAD %H7D>A36;@9
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
319
Classes 56
0DHDG<M OJ NOMP>ON, DMBTT DN < A@<OPM@ AJM ?@ADIDIB I@R OTK@N. !DAA@M@IO AMJH NOMP>ON, >G<NN@N
KMJQD?@ OC@ A4<75F AD;7@F76 BDA9D3??;@9 (,,-) K<M<?DBH DI !. 1C@ H<EJM <NK@>ON JA ,,-
<M@ OC@ AJGGJRDIB:
t Encapsulation: JIOMJGGDIB <>>@NN OJ H@H=@MN (E@53BEG>3F;A@ ;E 3H3;>34>7 8AD EFDG5FE 3E
I7>> 4GF ;F :3E @AF 477@ ?7@F;A@76 G@F;> F:;E 5:3BF7D.)
t Inheritance: A>LPDMDIB H@H=@MN JA <IJOC@M OTK@
t Polymorphism: B@DIB <=G@ OJ PN@ < HJM@ NK@>D<G OTK@ DI KG<>@ JA < HJM@ B@I@M<G OTK@
"I><KNPG<ODJI DN <>CD@Q@? =T BDAF75F;A@ 3FFD;4GF7E, RCD>C R@ RDGG N@@ DI < G<O@M >C<KO@M.
&IC@MDO<I>@ DN AJM <>LPDMDIB ;?B>7?7@F3F;A@E JA JOC@M OTK@N. -JGTHJMKCDNH DN AJM <=NOM<>ODIB
K<MON JA KMJBM<HN AMJH @<>C JOC@M <I? DN <>CD@Q@? =T >G<NN ;@F7D8357E.
1CDN >C<KO@M RDGG DIOMJ?P>@ >G<NN@N <O < CDBC G@Q@G, PI?@MGDIDIB OC@ A<>O OC<O OC@T <M@
M@A@M@I>@ OTK@N. G<NN@N RDGG =@ @SKG<DI@? DI HJM@ ?@O<DG DI G<O@M >C<KO@MN.
Comparing with structs
56.1
&I B@I@M<G, >G<NN@N <M@ Q@MT NDHDG<M OJ NOMP>ON. *JNO JA OC@ A@<OPM@N OC<O R@ C<Q@ N@@I AJM
NOMP>ON DI OC@ AJGGJRDIB >C<KO@MN <KKGT OJ >G<NN@N <N R@GG:
t 0OMP>ON
t *@H=@M #PI>ODJIN
t DPOTU SFG -<M<H@O@MN <I? DPOTU *@H=@M #PI>ODJIN
t JINOMP>OJM <I? ,OC@M 0K@>D<G #PI>ODJIN
t ,K@M<OJM ,Q@MGJ<?DIB
%JR@Q@M, OC@M@ <M@ DHKJMO<IO ?DAA@M@I>@N =@OR@@I >G<NN@N <I? NOMP>ON.
Classes are reference types 56.1.1
1C@ =DBB@NO ?DAA@M@I>@ AMJH NOMP>ON DN OC<O NOMP>ON <M@ H3>G7 FKB7E <I? >G<NN@N <M@ D787D7@57
FKB7E. 1C@ JOC@M ?DAA@M@I>@N JPOGDI@? =@GJR <M@ HJNOGT ?P@ OJ OCDN A<>O.
Class objects may be OVMM 56.1.2
AN DO C<N =@@I H@IODJI@? =MD@AGT DI 1C@ OVMM 3<GP@ <I? OC@ JT ,K@M<OJM >C<KO@M, >G<NN
Q<MD<=G@N ><I =@ OVMM. &I JOC@M RJM?N, >G<NN Q<MD<=G@N H<T IJO =@ KMJQD?DIB <>>@NN OJ <IT
J=E@>O. G<NN Q<MD<=G@N ?J IJO C<Q@ Q<GP@N OC@HN@GQ@N; OC@ <>OP<G >G<NN J=E@>ON HPNO =@
>JINOMP>O@? =T OC@ OFX F@TRJM?.
AN TJP RJPG? <GNJ M@H@H=@M, >JHK<MDIB < M@A@M@I>@ OJ OVMM =T OC@ == JM OC@ != JK@M<OJM DN
<I @MMJM. &INO@<?, OC@ >JHK<MDNJI HPNO =@ ?JI@ =T OC@ JT JM OC@ !JT JK@M<OJM, <>>JM?DIBGT:
.Z$MBTT SFGFSFODFT"O0CKFDU = OFX .Z$MBTT;
BTTFSU(SFGFSFODFT"O0CKFDU !JT OVMM);
.Z$MBTT WBSJBCMF; // EPFT OPU SFGFSFODF BO PCKFDU
BTTFSU(WBSJBCMF JT OVMM);
1C@ M@<NJI DN OC<O, OC@ == JK@M<OJM H<T I@@? OJ >JINPGO OC@ Q<GP@N JA OC@ H@H=@MN JA OC@
J=E@>ON <I? OC<O <OO@HKODIB OJ <>>@NN OC@ H@H=@MN OCMJPBC < KJO@IOD<GGT OVMM Q<MD<=G@ RJPG?
><PN@ < H@HJMT <>>@NN @MMJM. #JM OC<O M@<NJI, >G<NN Q<MD<=G@N HPNO <GR<TN =@ >JHK<M@? =T
OC@ JT <I? !JT JK@M<OJMN.
C>3EE7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
320
Class variables versus class objects 56.1.3
G<NN Q<MD<=G@ <I? >G<NN J=E@>O <M@ N@K<M<O@ >JI>@KON.
G<NN J=E@>ON <M@ >JINOMP>O@? =T OC@ OFX F@TRJM?; OC@T ?J IJO C<Q@ I<H@N. 1C@ <>OP<G
>JI>@KO OC<O < >G<NN OTK@ M@KM@N@ION DI < KMJBM<H DN KMJQD?@? =T < >G<NN J=E@>O. #JM @S<HKG@,
<NNPHDIB OC<O < 4UVEFOU >G<NN M@KM@N@ION NOP?@ION =T OC@DM I<H@N <I? BM<?@N, NP>C
DIAJMH<ODJI RJPG? =@ NOJM@? =T OC@ H@H=@MN JA 4UVEFOU A4<75FE. -<MOGT =@><PN@ OC@T <M@
<IJITHJPN, DO DN IJO KJNND=G@ OJ <>>@NN >G<NN J=E@>ON ?DM@>OGT.
A >G<NN Q<MD<=G@ JI OC@ JOC@M C<I? DN < G<IBP<B@ A@<OPM@ AJM <>>@NNDIB >G<NN J=E@>ON. AGOCJPBC
DO H<T N@@H NTIO<>OD><GGT OC<O JK@M<ODJIN <M@ =@DIB K@MAJMH@? JI < >G<NN H3D;34>7, OC@
JK@M<ODJIN <M@ <>OP<GGT ?DNK<O>C@? OJ < >G<NN A4<75F.
)@O'N >JIND?@M OC@ AJGGJRDIB >J?@ OC<O R@ C<Q@ N@@I KM@QDJPNGT DI OC@ 3<GP@ 1TK@N <I?
/@A@M@I>@ 1TK@N >C<KO@M:
BVUP WBSJBCMF1 = OFX .Z$MBTT;
BVUP WBSJBCMF2 = WBSJBCMF1;
1C@ OFX F@TRJM? >JINOMP>ON <I <IJITHJPN >G<NN J=E@>O. WBSJBCMF1 <I? WBSJBCMF2 <=JQ@
H@M@GT KMJQD?@ <>>@NN OJ OC<O <IJITHJPN J=E@>O:
(BOPOZNPVT .Z$MBTT PCKFDU) WBSJBCMF1 WBSJBCMF2
---+-------------------+--- ---+---+--- ---+---+---
] ... ] ] P ] ] P ]
---+-------------------+--- ---+-]-+--- ---+-]-+---
q ] ]
] ] ]
+--------------------+------------+
Copying 56.1.4
JKTDIB <AA@>ON JIGT OC@ Q<MD<=G@N, IJO OC@ J=E@>O.
B@><PN@ >G<NN@N <M@ M@A@M@I>@ OTK@N, ?@ADIDIB < I@R >G<NN Q<MD<=G@ <N < >JKT JA <IJOC@M
H<F@N ORJ Q<MD<=G@N OC<O KMJQD?@ <>>@NN OJ OC@ N<H@ J=E@>O. 1C@ <>OP<G J=E@>O DN IJO >JKD@?.
0DI>@ IJ J=E@>O B@ON >JKD@?, OC@ KJNO=GDO API>ODJI UIJT(UIJT) DN IJO <Q<DG<=G@ AJM >G<NN@N.
BVUP WBSJBCMF2 = WBSJBCMF1;
&I OC@ >J?@ <=JQ@, WBSJBCMF2 DN =@DIB DIDOD<GDU@? =T WBSJBCMF1. 1C@ ORJ Q<MD<=G@N NO<MO
KMJQD?DIB <>>@NN OJ OC@ N<H@ J=E@>O.
4C@I OC@ <>OP<G J=E@>O I@@?N OJ =@ >JKD@?, OC@ >G<NN HPNO C<Q@ < H@H=@M API>ODJI AJM OC<O
KPMKJN@. 1J =@ >JHK<OD=G@ RDOC <MM<TN, OCDN API>ODJI H<T =@ I<H@? EVQ(). 1CDN API>ODJI HPNO
>M@<O@ <I? M@OPMI < I@R >G<NN J=E@>O. )@O'N N@@ OCDN JI < >G<NN OC<O C<N Q<MDJPN OTK@N JA
H@H=@MN:
DMBTT 'PP
\
4 P; // BTTVNF 4 JT B TUSVDU UZQF
DIBS<> T;
JOU J;
// ...
UIJT(4 P, DPOTU DIBS<> T, JOU J)
\
UIJT.P = P;
C>3EE7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
321
UIJT.T = T.EVQ;
UIJT.J = J;
^
'PP EVQ() DPOTU
\
SFUVSO OFX 'PP(P, T, J);
^
^
1C@ EVQ() H@H=@M API>ODJI H<F@N < I@R J=E@>O =T O<FDIB <?Q<IO<B@ JA OC@ >JINOMP>OJM JA
'PP <I? M@OPMIN OC@ I@R J=E@>O. +JO@ OC<O OC@ >JINOMP>OJM >JKD@N OC@ T H@H=@M @SKGD>DOGT =T
OC@ .EVQ KMJK@MOT JA <MM<TN. B@DIB Q<GP@ OTK@N, P <I? J <M@ >JKD@? <POJH<OD><GGT.
1C@ AJGGJRDIB >J?@ H<F@N PN@ JA EVQ() OJ >M@<O@ < I@R J=E@>O:
BVUP WBS1 = OFX 'PP(4(1.5), "IFMMP", 42);
BVUP WBS2 = WBS1.EVQ();
AN < M@NPGO, OC@ J=E@>ON OC<O <M@ <NNJ>D<O@? RDOC WBS1 <I? WBS2 <M@ ?DAA@M@IO.
0DHDG<MGT, <I JNNVUBCMF >JKT JA <I J=E@>O ><I =@ KMJQD?@? =T < H@H=@M API>ODJI
<KKMJKMD<O@GT I<H@? JEVQ():
DMBTT 'PP
\
// ...
JNNVUBCMF('PP) JEVQ() DPOTU
\
SFUVSO OFX JNNVUBCMF('PP)(P, T, J);
^
^
// ...
JNNVUBCMF('PP) JNN = WBS1.JEVQ();
Assignment 56.1.5
'PNO GDF@ >JKTDIB, <NNDBIH@IO <AA@>ON JIGT OC@ Q<MD<=G@N.
ANNDBIDIB OJ < >G<NN Q<MD<=G@ ?DN<NNJ>D<O@N OC<O Q<MD<=G@ AMJH DON >PMM@IO J=E@>O <I? <NNJ>D<O@N
DO RDOC < I@R J=E@>O.
&A OC@M@ DN IJ JOC@M >G<NN Q<MD<=G@ OC<O NODGG KMJQD?@N <>>@NN OJ OC@ J=E@>O OC<O C<N =@@I
?DN<NNJ>D<O@? AMJH, OC@I OC<O J=E@>O DN BJDIB OJ =@ ?@NOMJT@? NJH@ ODH@ DI OC@ APOPM@ =T OC@
B<M=<B@ >JGG@>OJM.
BVUP WBSJBCMF1 = OFX .Z$MBTT();
BVUP WBSJBCMF2 = OFX .Z$MBTT();
WBSJBCMF1 = WBSJBCMF2;
1C@ <NNDBIH@IO <=JQ@ H<F@N WBSJBCMF1 G@<Q@ DON J=E@>O <I? NO<MO KMJQD?DIB <>>@NN OJ
WBSJBCMF2'N J=E@>O. 0DI>@ OC@M@ DN IJ JOC@M Q<MD<=G@ AJM WBSJBCMF1'N JMDBDI<G J=E@>O, OC<O
J=E@>O RDGG =@ ?@NOMJT@? =T OC@ B<M=<B@ >JGG@>OJM.
1C@ =@C<QDJM JA <NNDBIH@IO ><IIJO =@ >C<IB@? AJM >G<NN@N. &I JOC@M RJM?N, PQ"TTJHO ><IIJO
=@ JQ@MGJ<?@? AJM OC@H.
C>3EE7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
322
Definition 56.1.6
G<NN@N <M@ ?@ADI@? =T OC@ DMBTT F@TRJM? DINO@<? JA OC@ TUSVDU F@TRJM?:
DMBTT $IFTT1JFDF
\
// ...
^
Construction 56.1.7
AN RDOC NOMP>ON, OC@ I<H@ JA OC@ >JINOMP>OJM DN UIJT. 2IGDF@ NOMP>ON, >G<NN J=E@>ON ><IIJO =@
>JINOMP>O@? =T OC@ \ ^ NTIO<S.
DMBTT $IFTT1JFDF
\
EDIBS TIBQF;
UIJT(EDIBS TIBQF)
\
UIJT.TIBQF = TIBQF;
^
^
2IGDF@ NOMP>ON, OC@M@ DN IJ <POJH<OD> J=E@>O >JINOMP>ODJI RC@M@ OC@ >JINOMP>OJM K<M<H@O@MN
<M@ <NNDBI@? OJ H@H=@MN N@LP@IOD<GGT:
DMBTT $IFTT1JFDF
\
EDIBS TIBQF;
TJ[F@U WBMVF;
^
WPJE NBJO()
\
BVUP LJOH = OFX $IFTT1JFDF('u', 100); // j DPNQJMBUJPO &3303
^
&SSPS: OP DPOTUSVDUPS GPS $IFTT1JFDF
#JM OC<O NTIO<S OJ RJMF, < >JINOMP>OJM HPNO =@ ?@ADI@? @SKGD>DOGT =T OC@ KMJBM<HH@M.
Destruction 56.1.8
AN RDOC NOMP>ON, OC@ I<H@ JA OC@ ?@NOMP>OJM DN _UIJT:
_UIJT()
\
// ...
^
Member access 56.1.9
0<H@ <N NOMP>ON, OC@ H@H=@MN <M@ <>>@NN@? =T OC@ 6AF JK@M<OJM:
BVUP LJOH = OFX $IFTT1JFDF('u');
XSJUFMO(LJOH.TIBQF);
C>3EE7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
323
AGOCJPBC OC@ NTIO<S H<F@N DO GJJF <N DA < H@H=@M JA OC@ H3D;34>7 DN =@DIB <>>@NN@?, DO DN
<>OP<GGT OC@ H@H=@M JA OC@ A4<75F. G<NN Q<MD<=G@N ?J IJO C<Q@ H@H=@MN, OC@ >G<NN J=E@>ON ?J.
1C@ LJOH Q<MD<=G@ ?J@N IJO C<Q@ < TIBQF H@H=@M, OC@ <IJITHJPN J=E@>O ?J@N.
Note: !F ;E GEG3>>K @AF BDAB7D FA 3557EE ?7?47DE 6;D75F>K 3E ;@ F:7 5A67 34AH7. ,:7@ F:3F
7J35F EK@F3J ;E 67E;D76, BDAB7DF;7E E:AG>6 47 BD787DD76, I:;5: I;>> 47 7JB>3;@76 ;@ 3 >3F7D
5:3BF7D.
Operator overloading 56.1.10
,OC@M OC<I OC@ A<>O OC<O PQ"TTJHO ><IIJO =@ JQ@MGJ<?@? AJM >G<NN@N, JK@M<OJM JQ@MGJ<?DIB DN
OC@ N<H@ <N NOMP>ON. #JM >G<NN@N, OC@ H@<IDIB JA PQ"TTJHO DN <GR<TN 3EEA5;3F;@9 3 5>3EE
H3D;34>7 I;F: 3 5>3EE A4<75F.
Member functions 56.1.11
*@H=@M API>ODJIN <M@ B@I@M<GGT OC@ N<H@ <N NOMP>ON.
A ?DAA@M@I>@ DN OC<O NJH@ H@H=@M API>ODJIN <M@ <POJH<OD><GGT DIC@MDO@? AMJH OC@ 0CKFDU
>G<NN. 4@ RDGG N@@ DI OC@ I@SO >C<KO@M CJR OC@ ?@ADIDODJI JA UP4USJOH DN >C<IB@? =T OC@
PWFSSJEF F@TRJM?.
The JT and !JT operators 56.1.12
1C@N@ JK@M<OJMN JK@M<O@ JI >G<NN Q<MD<=G@N.
JT NK@>DAD@N RC@OC@M ORJ >G<NN Q<MD<=G@N KMJQD?@ <>>@NN OJ OC@ N<H@ >G<NN J=E@>O. &O M@OPMIN
USVF DA OC@ J=E@>O DN OC@ N<H@ <I? GBMTF JOC@MRDN@. !JT DN OC@ JKKJNDO@ JA JT.
BVUP NZ,JOH = OFX $IFTT1JFDF('u');
BVUP ZPVS,JOH = OFX $IFTT1JFDF('u');
BTTFSU(NZ,JOH !JT ZPVS,JOH);
0DI>@ OC@ J=E@>ON JA NZ,JOH <I? ZPVS,JOH Q<MD<=G@N <M@ ?DAA@M@IO, OC@ !JT JK@M<OJM M@OPMIN
USVF. "Q@I OCJPBC OC@ ORJ J=E@>ON <M@ >JINOMP>O@? =T OC@ N<H@ >C<M<>O@M 's', OC@T <M@ NODGG
ORJ N@K<M<O@ J=E@>ON.
4C@I OC@ Q<MD<=G@N KMJQD?@ <>>@NN OJ OC@ N<H@ J=E@>O, JT M@OPMIN USVF:
BVUP NZ,JOH2 = NZ,JOH;
BTTFSU(NZ,JOH2 JT NZ,JOH);
BJOC JA OC@ Q<MD<=G@N <=JQ@ KMJQD?@ <>>@NN OJ OC@ N<H@ J=E@>O.
Summary
56.2
t G<NN@N <I? NOMP>ON NC<M@ >JHHJI A@<OPM@N =PO C<Q@ =DB ?DAA@M@I>@N.
t G<NN@N <M@ M@A@M@I>@ OTK@N. 1C@ OFX F@TRJM? >JINOMP>ON <I <IJITHJPN 5>3EE A4<75F <I?
M@OPMIN < 5>3EE H3D;34>7.
t G<NN Q<MD<=G@N OC<O <M@ IJO <NNJ>D<O@? RDOC <IT J=E@>O <M@ OVMM. C@>FDIB <B<DINO OVMM
HPNO =@ ?JI@ =T JT JM !JT, IJO =T == JM !=.
t 1C@ <>O JA >JKTDIB <NNJ>D<O@N <I <??DODJI<G Q<MD<=G@ RDOC <I J=E@>O. &I JM?@M OJ >JKT >G<NN
J=E@>ON, OC@ OTK@ HPNO C<Q@ < NK@>D<G API>ODJI GDF@GT I<H@? EVQ().
t ANNDBIH@IO <NNJ>D<O@N < Q<MD<=G@ RDOC <I J=E@>O. 1CDN =@C<QDJM ><IIJO =@ >C<IB@?.
C>3EE7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
324
Inheritance 57
&IC@MDO<I>@ DN ?@ADIDIB < HJM@ NK@>D<G OTK@ =<N@? JI <I @SDNODIB HJM@ B@I@M<G OTK@. 1C@
NK@>D<G OTK@ <>LPDM@N OC@ H@H=@MN JA OC@ B@I@M<G OTK@ <I? <N < M@NPGO, ><I O<F@ KG<>@ JA OC@
B@I@M<G OTK@.
&IC@MDO<I>@ DN <Q<DG<=G@ AJM >G<NN@N, IJO NOMP>ON. 1C@ >G<NN OC<O DIC@MDON <IJOC@M >G<NN DN ><GG@?
OC@ EG45>3EE, <I? OC@ >G<NN OC<O B@ON DIC@MDO@? DN ><GG@? OC@ EGB7D5>3EE.
1C@M@ <M@ ORJ OTK@N JA DIC@MDO<I>@ DI !. 4@ RDGG >JQ@M ;?B>7?7@F3F;A@ ;@:7D;F3@57 DI OCDN
>C<KO@M <I? G@<Q@ ;@F7D8357 ;@:7D;F3@57 OJ < G<O@M >C<KO@M.
4C@I ?@ADIDIB < NP=>G<NN, OC@ NPK@M>G<NN DN NK@>DAD@? <AO@M < >JGJI >C<M<>O@M:
DMBTT SubClass : SuperClass
\
// ...
^
1J N@@ <I @S<HKG@ JA OCDN, G@O'N <NNPH@ OC<O OC@M@ DN <GM@<?T OC@ AJGGJRDIB >G<NN OC<O
M@KM@N@ION < >GJ>F:
DMBTT $MPDL
\
JOU IPVS;
JOU NJOVUF;
JOU TFDPOE;
WPJE BEKVTU(JOU IPVS, JOU NJOVUF, JOU TFDPOE = 0)
\
UIJT.IPVS = IPVS;
UIJT.NJOVUF = NJOVUF;
UIJT.TFDPOE = TFDPOE;
^
^
AKK<M@IOGT, OC@ H@H=@MN JA OC<O >G<NN ?J IJO I@@? NK@>D<G Q<GP@N ?PMDIB >JINOMP>ODJI; NJ
OC@M@ DN IJ >JINOMP>OJM. &INO@<?, OC@ H@H=@MN <M@ N@O =T OC@ BEKVTU() H@H=@M API>ODJI:
BVUP EFTL$MPDL = OFX $MPDL;
EFTL$MPDL.BEKVTU(20, 30);
XSJUFGMO(
"%02T:%02T:%02T",
EFTL$MPDL.IPVS, EFTL$MPDL.NJOVUF, EFTL$MPDL.TFDPOE);
Note: !F IAG>6 47 ?AD7 GE78G> FA BDA6G57 F:7 F;?7 EFD;@9 4K 3 toString() 8G@5F;A@. !F I;>> 47
36676 >3F7D I:7@ 7JB>3;@;@9 F:7 override =7KIAD6 47>AI.
1C@ JPOKPO:
20:30:00
4DOC JIGT OC<O HP>C API>ODJI<GDOT, $MPDL >JPG? =@ < NOMP>O <N R@GG, <I? ?@K@I?DIB JI OC@
I@@?N JA OC@ KMJBM<H, OC<O >JPG? =@ NPAAD>D@IO.
%JR@Q@M, =@DIB < >G<NN H<F@N DO KJNND=G@ OJ DIC@MDO AMJH $MPDL.
1J N@@ <I @S<HKG@ JA DIC@MDO<I>@, G@O'N >JIND?@M <I "MBSN$MPDL OC<O IJO JIGT DI>GP?@N <GG JA
OC@ API>ODJI<GDOT JA $MPDL, =PO <GNJ KMJQD?@N < R<T JA N@OODIB OC@ <G<MH. )@O'N ADMNO ?@ADI@ OCDN
OTK@ RDOCJPO M@B<M? OJ $MPDL. &A R@ ?D? OC<O, R@ RJPG? C<Q@ OJ DI>GP?@ OC@ N<H@ OCM@@
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
325
H@H=@MN JA $MPDL <I? OC@ N<H@ BEKVTU() API>ODJI OC<O <?EPNO@? OC@H. "MBSN$MPDL RJPG?
<GNJ C<Q@ JOC@M H@H=@MN AJM DON <??DODJI<G API>ODJI<GDOT:
DMBTT "MBSN$MPDL
\
JOU IPVS;
JOU NJOVUF;
JOU TFDPOE;
JOU BMBSN)PVS;
JOU BMBSN.JOVUF;
WPJE BEKVTU(JOU IPVS, JOU NJOVUF, JOU TFDPOE = 0)
\
UIJT.IPVS = IPVS;
UIJT.NJOVUF = NJOVUF;
UIJT.TFDPOE = TFDPOE;
^
WPJE BEKVTU"MBSN(JOU IPVS, JOU NJOVUF)
\
BMBSN)PVS = IPVS;
BMBSN.JOVUF = NJOVUF;
^
^
1C@ H@H=@MN OC<O <KK@<M @S<>OGT DI $MPDL <M@ CDBCGDBCO@?. AN ><I =@ N@@I, ?@ADIDIB $MPDL
<I? "MBSN$MPDL N@K<M<O@GT M@NPGON DI >J?@ ?PKGD><ODJI.
&IC@MDO<I>@ DN C@GKAPG DI NP>C ><N@N. &IC@MDODIB "MBSN$MPDL AMJH $MPDL NDHKGDAD@N OC@ I@R
>G<NN <I? M@?P>@N >J?@ ?PKGD><ODJI:
DMBTT "MBSN$MPDL : $MPDL
\
JOU BMBSN)PVS;
JOU BMBSN.JOVUF;
WPJE BEKVTU"MBSN(JOU IPVS, JOU NJOVUF)
\
BMBSN)PVS = IPVS;
BMBSN.JOVUF = NJOVUF;
^
^
1C@ I@R ?@ADIDODJI JA "MBSN$MPDL DN OC@ @LPDQ<G@IO JA OC@ KM@QDJPN JI@. 1C@ CDBCGDBCO@? K<MO
JA OC@ I@R ?@ADIDODJI >JMM@NKJI?N OJ OC@ CDBCGDBCO@? K<MON JA OC@ JG? ?@ADIDODJI.
B@><PN@ "MBSN$MPDL DIC@MDON OC@ H@H=@MN JA $MPDL, DO ><I =@ PN@? EPNO GDF@ < $MPDL:
BVUP CFE4JEF$MPDL = OFX "MBSN$MPDL;
CFE4JEF$MPDL.BEKVTU(20, 30);
CFE4JEF$MPDL.BEKVTU"MBSN(7, 0);
1C@ H@H=@MN OC<O <M@ DIC@MDO@? AMJH OC@ NPK@M>G<NN <M@ <>>@NN@? <N DA OC@T <M@ OC@ H@H=@MN
JA OC@ NP=>G<NN:
XSJUFGMO("%02T:%02T:%02T |%02T:%02T",
CFE4JEF$MPDL.IPVS,
CFE4JEF$MPDL.NJOVUF,
CFE4JEF$MPDL.TFDPOE,
CFE4JEF$MPDL.BMBSN)PVS,
CFE4JEF$MPDL.BMBSN.JOVUF);
1C@ JPOKPO:
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
326
20:30:00 z07:00
Note: A@ AlarmClock.toString 8G@5F;A@ IAG>6 47 ?AD7 GE78G> ;@ F:;E 53E7. !F I;>> 47 678;@76
>3F7D 47>AI.
1C@ DIC@MDO<I>@ PN@? DI OCDN @S<HKG@ DN ;?B>7?7@F3F;A@ ;@:7D;F3@57.
&A R@ DH<BDI@ OC@ H@HJMT <N < MD==JI BJDIB AMJH OJK OJ =JOOJH, OC@ KG<>@H@IO JA OC@
H@H=@MN JA "MBSN$MPDL DI H@HJMT ><I =@ KD>OPM@? <N DI OC@ AJGGJRDIB DGGPNOM<ODJI:
] . ]
] . ]
UIF BEESFTT PG UIF PCKFDU l +-------------+
] IPVS ]
] NJOVUF ]
] TFDPOE ]
] BMBSN)PVS ]
] BMBSN.JOVUF ]
+-------------+
] . ]
] . ]
1C@ DGGPNOM<ODJI <=JQ@ DN EPNO OJ BDQ@ <I D?@< JI CJR OC@ H@H=@MN JA OC@ NPK@M>G<NN <I? OC@
NP=>G<NN H<T =@ >JH=DI@? OJB@OC@M. 1C@ <>OP<G G<TJPO JA OC@ H@H=@MN RJPG? ?@K@I? JI
?@>DNDJIN OC<O <M@ H<?@ =T OC@ >JHKDG@M ?PMDIB >JHKDG<ODJI.
Warning: Inherit only if "is a"
57.1
4@ C<Q@ N@@I OC<O DHKG@H@IO<ODJI DIC@MDO<I>@ DN <=JPO <>LPDMDIB H@H=@MN. JIND?@M OCDN
FDI? JA DIC@MDO<I>@ JIGT DA OC@ NP=OTK@ ><I =@ OCJPBCO JA =@DIB < FDI? JA OC@ NPK@MOTK@ <N DI
OC@ KCM<N@ "<G<MH >GJ>F ;E 3 >GJ>F."
"&N <" DN IJO OC@ JIGT M@G<ODJINCDK =@OR@@I OTK@N; < HJM@ >JHHJI M@G<ODJINCDK DN OC@ "C<N <"
M@G<ODJINCDK. #JM @S<HKG@, G@O'N <NNPH@ OC<O R@ R<IO OJ <?? OC@ >JI>@KO JA < #BUUFSZ OJ OC@
$MPDL >G<NN. &O RJPG? IJO =@ <KKMJKMD<O@ OJ <?? #BUUFSZ OJ $MPDL =T DIC@MDO<I>@ =@><PN@ OC@
NO<O@H@IO ">GJ>F DN < =<OO@MT" DN IJO OMP@:
DMBTT $MPDL : #BUUFSZ // j 8SPOH EFTJHO
\
// ...
^
A >GJ>F DN IJO < =<OO@MT; DO :3E 3 =<OO@MT. 4C@I OC@M@ DN NP>C < M@G<ODJINCDK JA >JIO<DIH@IO,
OC@ OTK@ OC<O DN >JIO<DI@? HPNO =@ ?@ADI@? <N < H@H=@M JA OC@ OTK@ OC<O >JIO<DIN DO:
DMBTT $MPDL
\
#BUUFSZ CBUUFSZ; // j $PSSFDU EFTJHO
// ...
^
Interitance from at most one class
57.2
G<NN DIC@MDO<I>@ DN GDHDO@? =T < NDIBG@ >G<NN.
#JM @S<HKG@, <NNPHDIB OC<O OC@M@ DN <GNJ < 4PVOE&NJUUFS >G<NN, <I? @Q@I OCJPBC "<G<MH
>GJ>F DN < NJPI? @HDOODIB J=E@>O" DN <GNJ OMP@, DO DN IJO KJNND=G@ OJ DIC@MDO "MBSN$MPDL =JOC
AMJH $MPDL <I? 4PVOE&NJUUFS:
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
327
DMBTT 4PVOE&NJUUFS
\
// ...
^
DMBTT "MBSN$MPDL : $MPDL, 4PVOE&NJUUFS // j DPNQJMBUJPO &3303
\
// ...
^
,I OC@ JOC@M C<I?, OC@M@ DN IJ GDHDO OJ OC@ IPH=@M JA ;@F7D8357E OC<O < >G<NN ><I DIC@MDO AMJH.
4@ RDGG N@@ OC@ JOUFSGBDF F@TRJM? DI < G<O@M >C<KO@M.
A??DODJI<GGT, OC@M@ DN IJ GDHDO OJ CJR ?@@K OC@ DIC@MDO<I>@ CD@M<M>CT ><I BJ:
DMBTT .VTJDBM*OTUSVNFOU
\
// ...
^
DMBTT 4USJOH*OTUSVNFOU : .VTJDBM*OTUSVNFOU
\
// ...
^
DMBTT 7JPMJO : 4USJOH*OTUSVNFOU
\
// ...
^
1C@ DIC@MDO<I>@ CD@M<M>CT <=JQ@ ?@ADI@N < M@G<ODJINCDK AMJH OC@ HJM@ B@I@M<G OJ OC@ HJM@
NK@>DAD>: HPND><G DINOMPH@IO, NOMDIB DINOMPH@IO, <I? QDJGDI.
Hierarchy charts
57.3
1TK@N OC<O <M@ M@G<O@? =T OC@ "DN <" M@G<ODJINCDK AJMH < 5>3EE :;7D3D5:K.
A>>JM?DIB OJ ,,- >JIQ@IODJIN, >G<NN CD@M<M>CD@N <M@ M@KM@N@IO@? =T NPK@M>G<NN@N =@DIB JI
OC@ OJK <I? OC@ NP=>G<NN@N =@DIB <O OC@ =JOOJH. 1C@ DIC@MDO<I>@ M@G<ODJINCDKN <M@ DI?D><O@?
=T <MMJRN KJDIODIB AMJH OC@ NP=>G<NN@N OJ OC@ NPK@M>G<NN@N.
#JM @S<HKG@, OC@ AJGGJRDIB ><I =@ < CD@M<M>CT JA HPND><G DINOMPH@ION:
.VTJDBM*OTUSVNFOU
o n
4USJOH*OTUSVNFOU 8JOE*OTUSVNFOU
o n o n
7JPMJO (VJUBS 'MVUF 3FDPSEFS
Accessing superclass members
57.4
1C@ TVQFS F@TRJM? <GGJRN M@A@MMDIB OJ H@H=@MN OC<O <M@ DIC@MDO@? AMJH OC@ NPK@M>G<NN.
DMBTT "MBSN$MPDL : $MPDL
\
// ...
WPJE GPP()
\
TVQFS.NJOVUF = 10; // 5IF JOIFSJUFE 'NJOVUF' NFNCFS
NJOVUF = 10; // 4BNF UIJOH JG UIFSF JT OP BNCJHVJUZ
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
328
^
^
1C@ TVQFS F@TRJM? DN IJO <GR<TN I@>@NN<MT; NJOVUF <GJI@ C<N OC@ N<H@ H@<IDIB DI OC@ >J?@
<=JQ@. 1C@ TVQFS F@TRJM? DN I@@?@? RC@I =JOC OC@ NPK@M>G<NN <I? OC@ NP=>G<NN C<Q@
H@H=@MN PI?@M OC@ N<H@ I<H@N. 4@ RDGG N@@ OCDN =@GJR RC@I R@ RDGG I@@? OJ RMDO@
TVQFS.SFTFU() <I? TVQFS.UP4USJOH().
Constructing superclass members
57.5
1C@ JOC@M PN@ JA OC@ TVQFS F@TRJM? DN OJ ><GG OC@ >JINOMP>OJM JA OC@ NPK@M>G<NN. 1CDN DN NDHDG<M
OJ ><GGDIB OC@ >JINOMP>OJMN JA OC@ >PMM@IO >G<NN: UIJT RC@I ><GGDIB >JINOMP>OJMN JA OCDN >G<NN
<I? TVQFS RC@I ><GGDIB >JINOMP>OJMN JA OC@ NPK@M>G<NN.
&O DN IJO M@LPDM@? OJ ><GG OC@ NPK@M>G<NN >JINOMP>OJM @SKGD>DOGT; RC@I OC@ NPK@M>G<NN C<N <
?@A<PGO >JINOMP>OJM, OC@ DIC@MDO@? H@H=@MN JA OC@ NPK@M>G<NN <M@ <POJH<OD><GGT ?@A<PGO-
>JINOMP>O@?.
4@ C<Q@ IJO ?@ADI@? >JINOMP>OJMN AJM OC@ $MPDL <I? "MBSN$MPDL >G<NN@N T@O. #JM OC<O M@<NJI,
OC@ H@H=@MN JA =JOC JA OCJN@ >G<NN@N <M@ DIDOD<GDU@? =T OC@ .JOJU Q<GP@N JA OC@DM M@NK@>ODQ@
OTK@N, RCD>C DN 0 AJM JOU.
)@O'N <NNPH@ OC<O $MPDL C<N OC@ AJGGJRDIB >JINOMP>OJM:
DMBTT $MPDL
\
UIJT(JOU IPVS, JOU NJOVUF, JOU TFDPOE)
\
UIJT.IPVS = IPVS;
UIJT.NJOVUF = NJOVUF;
UIJT.TFDPOE = TFDPOE;
^
// ...
^
1C<O >JINOMP>OJM HPNO =@ PN@? RC@I >JINOMP>ODIB $MPDL J=E@>ON:
BVUP DMPDL = OFX $MPDL(17, 15, 0);
+<OPM<GGT, OC@ KMJBM<HH@MN RCJ PN@ OC@ $MPDL OTK@ ?DM@>OGT RJPG? C<Q@ OJ PN@ OC<O NTIO<S.
%JR@Q@M, RC@I >JINOMP>ODIB <I "MBSN$MPDL J=E@>O, OC@T ><IIJO >JINOMP>O DON $MPDL K<MO
N@K<M<O@GT. B@ND?@N, OC@ PN@MN JA "MBSN$MPDL I@@? IJO @Q@I FIJR OC<O DO DIC@MDON AMJH $MPDL.
A PN@M JA "MBSN$MPDL NCJPG? NDHKGT >JINOMP>O <I "MBSN$MPDL J=E@>O <I? PN@ DO DI OC@
KMJBM<H RDOCJPO I@@?DIB OJ K<T <OO@IODJI OJ DON $MPDL C@MDO<B@:
BVUP CFE4JEF$MPDL = OFX "MBSN$MPDL(/* ... */);
// ... VTF BT BO "MBSN$MPDL ...
#JM OC<O M@<NJI, >JINOMP>ODIB OC@ NPK@M>G<NN K<MO DN OC@ M@NKJIND=DGDOT JA OC@ NP=>G<NN. 1C@
NP=>G<NN ><GGN OC@ >JINOMP>OJM JA OC@ NPK@M>G<NN =T OC@ TVQFS() NTIO<S:
DMBTT "MBSN$MPDL : $MPDL
\
UIJT(JOU IPVS, JOU NJOVUF, JOU TFDPOE, // GPS $MPDL'T NFNCFST
JOU BMBSN)PVS, JOU BMBSN.JOVUF) // GPS "MBSN$MPDL'T NFNCFST
\
TVQFS(IPVS, NJOVUF, TFDPOE);
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
329
UIJT.BMBSN)PVS = BMBSN)PVS;
UIJT.BMBSN.JOVUF = BMBSN.JOVUF;
^
// ...
^
1C@ >JINOMP>OJM JA "MBSN$MPDL O<F@N <MBPH@ION AJM =JOC DON JRI H@H=@MN <I? OC@ H@H=@MN
JA DON NPK@M>G<NN. &O OC@I PN@N NJH@ JA OC<O DIAJMH<ODJI OJ >JINOMP>O DON NPK@M>G<NN K<MO.
&A OC@ NPK@M>G<NN <GM@<?T C<N < ?@A<PGO >JINOMP>OJM, DO DN IJO I@>@NN<MT OJ ><GG TVQFS()
@SKGD>DOGT.
Overriding the definitions of member functions
57.6
,I@ JA OC@ =@I@ADON JA DIC@MDO<I>@ DN =@DIB <=G@ OJ M@?@ADI@ OC@ H@H=@M API>ODJIN JA OC@
NPK@M>G<NN DI OC@ NP=>G<NN. 1CDN DN ><GG@? AH7DD;6;@9: 1C@ @SDNODIB ?@ADIDODJI JA OC@ H@H=@M
API>ODJI JA OC@ NPK@M>G<NN DN JQ@MMD??@I =T OC@ NP=>G<NN =T OC@ PWFSSJEF F@TRJM?.
)@O'N <NNPH@ OC<O $MPDL C<N < H@H=@M API>ODJI OC<O DN PN@? AJM M@N@OODIB DON H@H=@MN <GG OJ
U@MJ:
DMBTT $MPDL
\
WPJE SFTFU()
\
IPVS = 0;
NJOVUF = 0;
TFDPOE = 0;
^
// ...
^
1C<O API>ODJI DN DIC@MDO@? =T "MBSN$MPDL <I? ><I =@ ><GG@? JI <I "MBSN$MPDL J=E@>O:
BVUP CFE4JEF$MPDL = OFX "MBSN$MPDL(20, 30, 0, 7, 0);
// ...
CFE4JEF$MPDL.SFTFU();
%JR@Q@M, ?@>D?@?GT DBIJM<IO JA OC@ H@H=@MN JA "MBSN$MPDL, $MPDL.SFTFU ><I JIGT M@N@O DON
JRI H@H=@MN. #JM OC<O M@<NJI, OJ M@N@O OC@ H@H=@MN JA OC@ NP=>G<NN <N R@GG, SFTFU() HPNO
=@ JQ@MMD??@I:
DMBTT "MBSN$MPDL : $MPDL
\
PWFSSJEF WPJE SFTFU()
\
TVQFS.SFTFU();
BMBSN)PVS = 0;
BMBSN.JOVUF = 0;
^
// ...
^
1C@ NP=>G<NN M@N@ON JIGT DON JRI H@H=@MN <I? ?DNK<O>C@N OC@ M@NO JA OC@ O<NF OJ $MPDL =T OC@
TVQFS.SFTFU() ><GG. +JO@ OC<O RMDODIB EPNO SFTFU() RJPG? IJO RJMF <N DO RJPG? H@<I OC@
SFTFU() API>ODJI JA "MBSN$MPDL DON@GA. <GGDIB SFTFU() AMJH RDOCDI DON@GA RJPG? ><PN@ <I
DIADIDO@ M@>PMNDJI.
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
330
1C@ M@<NJI OC<O & C<Q@ ?@G<T@? OC@ ?@ADIDODJI JA UP4USJOH() PIODG OCDN KJDIO DN OC<O DO HPNO =@
?@ADI@? =T OC@ PWFSSJEF F@TRJM? AJM >G<NN@N. AN R@ RDGG N@@ DI OC@ I@SO >C<KO@M, @Q@MT >G<NN
DN <POJH<OD><GGT DIC@MDO@? AMJH < NPK@M>G<NN ><GG@? 0CKFDU <I? 0CKFDU <GM@<?T ?@ADI@N <
UP4USJOH() H@H=@M API>ODJI.
#JM OC<O M@<NJI, OC@ UP4USJOH() H@H=@M API>ODJI AJM >G<NN@N HPNO =@ ?@ADI@? =T OC@
PWFSSJEF F@TRJM?:
DMBTT $MPDL
\
PWFSSJEF TUSJOH UP4USJOH() DPOTU
\
SFUVSO GPSNBU("%02T:%02T:%02T", IPVS, NJOVUF, TFDPOE);
^
// ...
^
DMBTT "MBSN$MPDL : $MPDL
\
PWFSSJEF TUSJOH UP4USJOH() DPOTU
\
SFUVSO GPSNBU("%T |%02T:%02T", TVQFS.UP4USJOH(),
BMBSN)PVS, BMBSN.JOVUF);
^
// ...
^
+JO@ OC<O "MBSN$MPDL DN <B<DI ?DNK<O>CDIB NJH@ JA OC@ O<NF OJ $MPDL =T OC@
TVQFS.UP4USJOH() ><GG.
1CJN@ ORJ JQ@MMD?@N JA UP4USJOH() <GGJR PNDIB "MBSN$MPDL J=E@>ON <N NOMDIBN:
WPJE NBJO()
\
BVUP EFTL$MPDL = OFX "MBSN$MPDL(10, 15, 0, 6, 45);
XSJUFMO(EFTL$MPDL);
^
1C@ JPOKPO:
10:15:00 z06:45
Using the subclass in place of the superclass
57.7
0DI>@ OC@ NPK@M>G<NN DN HJM@ 97@7D3> <I? OC@ NP=>G<NN DN HJM@ EB75;3>, J=E@>ON JA < NP=>G<NN
><I =@ PN@? DI KG<>@N RC@M@ <I J=E@>O JA NPK@M>G<NN DN M@LPDM@?. 1CDN DN ><GG@? BA>K?ADB:;E?.
1C@ >JI>@KON JA B@I@M<G <I? NK@>D<G OTK@N ><I =@ N@@I DI NO<O@H@ION GDF@ "OCDN OTK@ DN JA OC<O
OTK@": "<G<MH >GJ>F DN < >GJ>F", "NOP?@IO DN < K@MNJI", "><O DN <I <IDH<G", @O>. A>>JM?DIBGT, <I
<G<MH >GJ>F ><I =@ PN@? RC@M@ < >GJ>F DN I@@?@?, < NOP?@IO ><I =@ PN@? RC@M@ < K@MNJI DN
I@@?@?, <I? < ><O ><I =@ PN@? RC@M@ <I <IDH<G DN I@@?@?.
4C@I < NP=>G<NN J=E@>O DN =@DIB PN@? <N < NPK@M>G<NN J=E@>O, DO ?J@N IJO GJN@ DON JRI NK@>D<G
OTK@. 1CDN DN NDHDG<M OJ DON @S<HKG@N DI M@<G GDA@: 2NDIB <I <G<MH >GJ>F NDHKGT <N < >GJ>F ?J@N
IJO >C<IB@ OC@ A<>O OC<O DO DN <I <G<MH >GJ>F; DO RJPG? NODGG =@C<Q@ GDF@ <I <G<MH >GJ>F.
)@O'N <NNPH@ OC<O < API>ODJI O<F@N < $MPDL J=E@>O <N K<M<H@O@M, RCD>C DO M@N@ON <O NJH@ KJDIO
?PMDIB DON @S@>PODJI:
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
331
WPJE VTF($MPDL DMPDL)
\
// ...
DMPDL.SFTFU();
// ...
^
-JGTHJMKCDNH H<F@N DO KJNND=G@ OJ N@I? <I "MBSN$MPDL OJ NP>C < API>ODJI:
BVUP EFTL$MPDL = OFX "MBSN$MPDL(10, 15, 0, 6, 45);
XSJUFMO("#FGPSF: ", EFTL$MPDL);
VTF(EFTL$MPDL);
XSJUFMO(""GUFS : ", EFTL$MPDL);
1CDN DN DI <>>JM?<I>@ RDOC OC@ M@G<ODJINCDK "<G<MH >GJ>F DN < >GJ>F." AN < M@NPGO, OC@ H@H=@MN
JA OC@ EFTL$MPDL J=E@>O B@ON M@N@O:
#FGPSF: 10:15:00 z06:45
"GUFS : 00:00:00 z00:00
1C@ DHKJMO<IO J=N@MQ<ODJI C@M@ DN OC<O IJO JIGT OC@ H@H=@MN JA $MPDL =PO <GNJ OC@ H@H=@MN
JA "MBSN$MPDL C<Q@ =@@I M@N@O.
AGOCJPBC VTF() ><GGN SFTFU() JI < $MPDL J=E@>O, NDI>@ OC@ <>OP<G J=E@>O DN <I "MBSN$MPDL,
OC@ API>ODJI OC<O B@ON ><GG@? DN "MBSN$MPDL.SFTFU. A>>JM?DIB OJ DON ?@ADIDODJI <=JQ@,
"MBSN$MPDL.SFTFU M@N@ON OC@ H@H=@MN JA =JOC $MPDL <I? "MBSN$MPDL.
&I JOC@M RJM?N, <GOCJPBC VTF() PN@N OC@ J=E@>O <N < $MPDL, OC@ <>OP<G J=E@>O H<T =@ <I
DIC@MDO@? OTK@ OC<O =@C<Q@N DI DON JRI NK@>D<G R<T.
)@O'N <?? <IJOC@M >G<NN OJ OC@ $MPDL CD@M<M>CT. 1C@ SFTFU() API>ODJI JA OCDN OTK@ N@ON DON
H@H=@MN OJ M<I?JH Q<GP@N:
JNQPSU TUE.SBOEPN;
DMBTT #SPLFO$MPDL : $MPDL
\
UIJT()
\
TVQFS(0, 0, 0);
^
PWFSSJEF WPJE SFTFU()
\
IPVS = VOJGPSN(0, 24);
NJOVUF = VOJGPSN(0, 60);
TFDPOE = VOJGPSN(0, 60);
^
^
4C@I <I J=E@>O JA #SPLFO$MPDL DN N@IO OJ VTF(), OC@I OC@ NK@>D<G SFTFU() API>ODJI JA
#SPLFO$MPDL RJPG? =@ ><GG@?. AB<DI, <GOCJPBC DO DN K<NN@? <N < $MPDL, OC@ <>OP<G J=E@>O DN
NODGG < #SPLFO$MPDL:
BVUP TIFMG$MPDL = OFX #SPLFO$MPDL;
VTF(TIFMG$MPDL);
XSJUFMO(TIFMG$MPDL);
1C@ JPOKPO NCJRN M<I?JH ODH@ Q<GP@N <N < M@NPGO JA M@N@OODIB < #SPLFO$MPDL:
22:46:37
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
332
Inheritance is transitive
57.8
-JGTHJMKCDNH DN IJO GDHDO@? OJ ORJ >G<NN@N. 0P=>G<NN@N JA NP=>G<NN@N ><I <GNJ =@ PN@? DI
KG<>@ JA <IT NPK@M>G<NN DI OC@ CD@M<M>CT.
)@O'N >JIND?@M OC@ .VTJDBM*OTUSVNFOU CD@M<M>CT:
DMBTT .VTJDBM*OTUSVNFOU
\
// ...
^
DMBTT 4USJOH*OTUSVNFOU : .VTJDBM*OTUSVNFOU
\
// ...
^
DMBTT 7JPMJO : 4USJOH*OTUSVNFOU
\
// ...
^
1C@ DIC@MDO<I>@N <=JQ@ =PDG? OC@ AJGGJRDIB M@G<ODJINCDKN: "NOMDIB DINOMPH@IO DN < HPND><G
DINOMPH@IO" <I? "QDJGDI DN < NOMDIB DINOMPH@IO." 1C@M@AJM@, DO DN <GNJ OMP@ OC<O "QDJGDI DN <
HPND><G DINOMPH@IO." JIN@LP@IOGT, < 7JPMJO J=E@>O ><I =@ PN@? DI KG<>@ JA <
.VTJDBM*OTUSVNFOU.
ANNPHDIB OC<O <GG JA OC@ NPKKJMODIB >J?@ =@GJR C<Q@ <GNJ =@@I ?@ADI@?:
WPJE QMBZ*O5VOF(.VTJDBM*OTUSVNFOU JOTUSVNFOU,
.VTJDBM1JFDF QJFDF)
\
JOTUSVNFOU.UVOF();
JOTUSVNFOU.QMBZ(QJFDF);
^
// ...
BVUP NZ7JPMJO = OFX 7JPMJO;
QMBZ*O5VOF(NZ7JPMJO, JNQSPWJTBUJPO);
AGOCJPBC QMBZ*O5VOF() @SK@>ON < .VTJDBM*OTUSVNFOU, DO DN =@DIB ><GG@? RDOC < 7JPMJO ?P@
OJ OC@ M@G<ODJINCDK "QDJGDI DN < HPND><G DINOMPH@IO."
&IC@MDO<I>@ ><I =@ <N ?@@K <N I@@?@?.
Abstract member functions and abstract classes
57.9
0JH@ODH@N OC@M@ <M@ H@H=@M API>ODJIN OC<O <M@ I<OPM<G OJ <KK@<M JI < >G<NN DIO@MA<>@ @Q@I
OCJPBC OC<O >G<NN ><IIJO KMJQD?@ DON ?@ADIDODJI. 4C@I OC@M@ DN IJ 5A@5D7F7 ?@ADIDODJI JA <
H@H=@M API>ODJI, OC<O API>ODJI DN <I 34EFD35F H@H=@M API>ODJI. A >G<NN OC<O C<N <O G@<NO JI@
<=NOM<>O H@H=@M API>ODJI DN <I <=NOM<>O >G<NN.
#JM @S<HKG@, OC@ $IFTT1JFDF NPK@M>G<NN DI < CD@M<M>CT H<T C<Q@ <I JT7BMJE() H@H=@M
API>ODJI OC<O ?@O@MHDI@N RC@OC@M < BDQ@I HJQ@ DN Q<GD? AJM OC<O >C@NN KD@>@. 0DI>@ Q<GD?DOT JA
< HJQ@ ?@K@I?N JI OC@ <>OP<G OTK@ JA OC@ >C@NN KD@>@, OC@ $IFTT1JFDF B@I@M<G >G<NN ><IIJO
H<F@ OCDN ?@>DNDJI DON@GA. 1C@ Q<GD? HJQ@N ><I JIGT =@ FIJRI =T OC@ NP=>G<NN@N GDF@ 1BXO,
,JOH, @O>.
1C@ BCTUSBDU F@TRJM? NK@>DAD@N OC<O OC@ ?@ADIDODJI JA OC@ H@H=@M API>ODJI DN IJO KMJQD?@? =T
OCDN >G<NN <I? ?GEF 47 KMJQD?@? =T < NP=>G<NN:
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
333
DMBTT $IFTT1JFDF
\
BCTUSBDU CPPM JT7BMJE(JO 4RVBSF GSPN, JO 4RVBSF UP);
^
1C@ JT7BMJE() API>ODJI ?J@N IJO C<Q@ < ?@ADIDODJI; DO DN < ?@>G<M<ODJI.
&O DN IJO KJNND=G@ OJ >JINOMP>O J=E@>ON JA <=NOM<>O >G<NN@N:
BVUP QJFDF = OFX $IFTT1JFDF; // j DPNQJMBUJPO &3303
,=E@>ON JA <I <=NOM<>O >G<NN ><IIJO =@ PN@? =@><PN@ JA OC@ HDNNDIB API>ODJI ?@ADIDODJIN. 0P>C
< >G<NN RJPG? IJO FIJR CJR OJ M@NKJI? OJ < ><GG GDF@
QJFDF.JT7BMJE(UIJT4RVBSF, UIBU4RVBSF).
,I OC@ JOC@M C<I?, DO DN KJNND=G@ OJ >JINOMP>O J=E@>ON JA NP=>G<NN@N <N GJIB <N OC@M@ <M@ IJ
API>ODJI ?@ADIDODJIN OC<O <M@ NODGG G@AO PI?@ADI@? (D.@. OC<O <M@ NODGG <=NOM<>O):
DMBTT 1BXO : $IFTT1JFDF
\
PWFSSJEF CPPM JT7BMJE(JO 4RVBSF GSPN, JO 4RVBSF UP)
\
// ... UIF JNQMFNFOUBUJPO PG JT7BMJE GPS QBXO ...
SFUVSO EFDJTJPO;
^
^
B@><PN@ DO KMJQD?@N OC@ ?@ADIDODJI AJM OC@ <=NOM<>O API>ODJI, DO DN KJNND=G@ OJ >JINOMP>O J=E@>ON
JA 1BXO:
BVUP QJFDF = OFX 1BXO; // DPNQJMFT
Example
57.10
)@O'N >JIND?@M < >G<NN CD@M<M>CT OC<O M@KM@N@ION M<DGR<T Q@CD>G@N:
3BJMXBZ7FIJDMF
/ ] =
-PDPNPUJWF 5SBJO 3BJMXBZ$BS \ MPBE(), VOMPBE() ^
/ =
1BTTFOHFS$BS 'SFJHIU$BS
1C@ API>ODJIN OC<O OC@ 3BJMXBZ$BS RDGG ?@>G<M@ <N BCTUSBDU <M@ DI?D><O@? =T M@? LP@NODJI
H<MFN.
0DI>@ HT BJ<G DN JIGT OJ KM@N@IO < >G<NN CD@M<M>CT <I? KJDIO JPO NJH@ JA DON ?@NDBI ?@>DNDJIN, &
RDGG IJO APGGT DHKG@H@IO OC@N@ >G<NN@N. &INO@<? JA ?JDIB <>OP<G RJMF, OC@T RDGG NDHKGT KMDIO
H@NN<B@N.
1C@ HJNO B@I@M<G >G<NN JA OC@ CD@M<M>CT <=JQ@ DN 3BJMXBZ7FIJDMF. &I OCDN KMJBM<H, DO RDGG
JIGT FIJR CJR OJ HJQ@ DON@GA:
DMBTT 3BJMXBZ7FIJDMF
\
WPJE BEWBODF(JO TJ[F@U LJMPNFUFST)
\
XSJUFGMO("5IF WFIJDMF JT BEWBODJOH %T LJMPNFUFST",
LJMPNFUFST);
^
^
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
334
A >G<NN OC<O DIC@MDON AMJH 3BJMXBZ7FIJDMF DN -PDPNPUJWF, RCD>C ?J@N IJO C<Q@ <IT NK@>D<G
H@H=@MN T@O:
DMBTT -PDPNPUJWF : 3BJMXBZ7FIJDMF
\^
4@ RDGG <?? < NK@>D<G NBLF4PVOE() H@H=@M API>ODJI OJ -PDPNPUJWF G<O@M ?PMDIB JI@ JA OC@
@S@M>DN@N.
3BJMXBZ$BS DN < 3BJMXBZ7FIJDMF <N R@GG. %JR@Q@M, DA OC@ CD@M<M>CT NPKKJMON ?DAA@M@IO OTK@N
JA M<DGR<T ><MN, OC@I >@MO<DI =@C<QDJMN GDF@ GJ<?DIB <I? PIGJ<?DIB HPNO =@ ?JI@ <>>JM?DIB OJ
OC@DM @S<>O OTK@N. #JM OC<O M@<NJI, 3BJMXBZ$BS ><I JIGT ?@>G<M@ OC@N@ ORJ API>ODJIN <N
<=NOM<>O:
DMBTT 3BJMXBZ$BS : 3BJMXBZ7FIJDMF
\
BCTUSBDU WPJE MPBE();
BCTUSBDU WPJE VOMPBE();
^
)J<?DIB <I? PIGJ<?DIB < K<NN@IB@M ><M DN <N NDHKG@ <N JK@IDIB OC@ ?JJMN JA OC@ ><M, RCDG@
GJ<?DIB <I? PIGJ<?DIB < AM@DBCO ><M H<T DIQJGQ@ KJMO@MN <I? RDI>C@N. 1C@ AJGGJRDIB
NP=>G<NN@N KMJQD?@ ?@ADIDODJIN AJM OC@ <=NOM<>O API>ODJIN JA 3BJMXBZ$BS:
DMBTT 1BTTFOHFS$BS : 3BJMXBZ$BS
\
PWFSSJEF WPJE MPBE()
\
XSJUFMO("5IF QBTTFOHFST BSF HFUUJOH PO");
^
PWFSSJEF WPJE VOMPBE()
\
XSJUFMO("5IF QBTTFOHFST BSF HFUUJOH PGG");
^
^
DMBTT 'SFJHIU$BS : 3BJMXBZ$BS
\
PWFSSJEF WPJE MPBE()
\
XSJUFMO("5IF DSBUFT BSF CFJOH MPBEFE");
^
PWFSSJEF WPJE VOMPBE()
\
XSJUFMO("5IF DSBUFT BSF CFJOH VOMPBEFE");
^
^
B@DIB <I <=NOM<>O >G<NN ?J@N IJO KM@>GP?@ OC@ PN@ JA 3BJMXBZ$BS DI OC@ KMJBM<H. ,=E@>ON JA
3BJMXBZ$BS ><I IJO =@ >JINOMP>O@? =PO 3BJMXBZ$BS ><I =@ PN@? <N <I DIO@MA<>@. AN OC@
NP=>G<NN@N ?@ADI@ OC@ ORJ M@G<ODJINCDKN "K<NN@IB@M ><M DN < M<DGR<T ><M" <I? "AM@DBCO ><M DN <
M<DGR<T ><M", OC@ J=E@>ON JA 1BTTFOHFS$BS <I? 'SFJHIU$BS ><I =@ PN@? DI KG<>@N JA
3BJMXBZ$BS. 1CDN RDGG =@ N@@I DI OC@ 5SBJO >G<NN =@GJR.
1C@ >G<NN OC<O M@KM@N@ION < OM<DI ><I >JINDNO JA < GJ>JHJODQ@ <I? <I <MM<T JA M<DGR<T><MN:
DMBTT 5SBJO : 3BJMXBZ7FIJDMF
\
-PDPNPUJWF MPDPNPUJWF;
3BJMXBZ$BS<> DBST;
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
335
// ...
^
& RJPG? GDF@ OJ M@K@<O <I DHKJMO<IO KJDIO: AGOCJPBC =JOC -PDPNPUJWF <I? 3BJMXBZ$BS DIC@MDO
AMJH 3BJMXBZ7FIJDMF, DO RJPG? IJO =@ >JMM@>O OJ DIC@MDO 5SBJO AMJH @DOC@M JA OC@H.
&IC@MDO<I>@ HJ?@GN OC@ "DN <" M@G<ODJINCDK <I? < OM<DI DN I@DOC@M < GJ>JHJODQ@ IJM < K<NN@IB@M
><M. A OM<DI >JINDNON JA OC@H.
&A R@ M@LPDM@ OC<O @Q@MT OM<DI HPNO C<Q@ < GJ>JHJODQ@, OC@ 5SBJO >JINOMP>OJM HPNO @INPM@
OC<O DO O<F@N < Q<GD? -PDPNPUJWF J=E@>O. 0DHDG<MGT, DA OC@ M<DGR<T ><MN <M@ JKODJI<G, OC@T ><I =@
<??@? =T < H@H=@M API>ODJI:
JNQPSU TUE.FYDFQUJPO;
// ...
DMBTT 5SBJO : 3BJMXBZ7FIJDMF
\
// ...
UIJT(-PDPNPUJWF MPDPNPUJWF)
\
FOGPSDF(MPDPNPUJWF !JT OVMM,
"-PDPNPUJWF DBOOPU CF OVMM");
UIJT.MPDPNPUJWF = MPDPNPUJWF;
^
WPJE BEE$BS(3BJMXBZ$BS<> DBST...)
\
UIJT.DBST _= DBST;
^
// ...
^
+JO@ OC<O BEE$BS() ><I Q<GD?<O@ OC@ 3BJMXBZ$BS J=E@>ON <N R@GG. & <H DBIJMDIB OC<O
Q<GD?<ODJI C@M@.
4@ ><I DH<BDI@ OC<O OC@ ?@K<MOPM@N <I? <MMDQ<GN JA OM<DIN NCJPG? <GNJ =@ NPKKJMO@?:
DMBTT 5SBJO : 3BJMXBZ7FIJDMF
\
// ...
WPJE EFQBSU4UBUJPO(TUSJOH TUBUJPO)
\
GPSFBDI (DBS; DBST) \
DBS.MPBE();
^
XSJUFGMO("%FQBSUJOH GSPN UIF %T TUBUJPO", TUBUJPO);
^
WPJE BSSJWF4UBUJPO(TUSJOH TUBUJPO)
\
XSJUFGMO(""SSJWJOH BU UIF %T TUBUJPO", TUBUJPO);
GPSFBDI (DBS; DBST) \
DBS.VOMPBE();
^
^
^
1C@ AJGGJRDIB NBJO() DN H<FDIB PN@ JA OC@ 3BJMXBZ7FIJDMF CD@M<M>CT:
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
336
JNQPSU TUE.TUEJP;
// ...
WPJE NBJO()
\
BVUP MPDPNPUJWF = OFX -PDPNPUJWF;
BVUP USBJO = OFX 5SBJO(MPDPNPUJWF);
USBJO.BEE$BS(OFX 1BTTFOHFS$BS, OFX 'SFJHIU$BS);
USBJO.EFQBSU4UBUJPO(""OLBSB");
USBJO.BEWBODF(500);
USBJO.BSSJWF4UBUJPO(")BZEBSQBhB");
^
1C@ 5SBJO >G<NN DN =@DIB PN@? =T API>ODJIN OC<O <M@ KMJQD?@? =T ORJ N@K<M<O@ DIO@MA<>@N:
1. 4C@I OC@ BEWBODF() API>ODJI DN ><GG@?, OC@ 5SBJO J=E@>O DN =@DIB PN@? <N <
3BJMXBZ7FIJDMF =@><PN@ OC<O API>ODJI DN ?@>G<M@? =T 3BJMXBZ7FIJDMF.
2. 4C@I OC@ EFQBSU4UBUJPO() <I? BSSJWF4UBUJPO() API>ODJIN <M@ ><GG@?, USBJO DN =@DIB
PN@? <N < 5SBJO =@><PN@ OCJN@ API>ODJIN <M@ ?@>G<M@? =T 5SBJO.
1C@ =GP@ <MMJRN DI?D><O@ OC<O OC@ MPBE() <I? VOMPBE() API>ODJIN RJMF <>>JM?DIB OJ OC@
<>OP<G OTK@ JA 3BJMXBZ$BS:
5IF QBTTFOHFST BSF HFUUJOH PO l
5IF DSBUFT BSF CFJOH MPBEFE l
%FQBSUJOH GSPN UIF "OLBSB TUBUJPO
5IF WFIJDMF JT BEWBODJOH 500 LJMPNFUFST
"SSJWJOH BU UIF )BZEBSQBiB TUBUJPO
5IF QBTTFOHFST BSF HFUUJOH PGG l
5IF DSBUFT BSF CFJOH VOMPBEFE l
Summary
57.11
t &IC@MDO<I>@ DN AJM OC@ "DN <" M@G<ODJINCDK.
t "<>C >G<NN ><I DIC@MDO AMJH PK OJ JI@ DMBTT.
t TVQFS C<N ORJ PN@N: <GGDIB OC@ >JINOMP>OJM JA OC@ NPK@M>G<NN <I? <>>@NNDIB OC@ H@H=@MN
JA OC@ NPK@M>G<NN.
t PWFSSJEF DN AJM M@?@ADIDIB H@H=@M API>ODJIN JA OC@ NPK@M>G<NN NK@>D<GGT AJM OCDN >G<NN.
t BCTUSBDU M@LPDM@N OC<O < H@H=@M API>ODJI HPNO =@ JQ@MMD??@I.
Exercises
57.12
1. )@O'N HJ?DAT 3BJMXBZ7FIJDMF. &I <??DODJI OJ M@KJMODIB OC@ ?DNO<I>@ OC<O DO <?Q<I>@N, G@O'N
C<Q@ DO <GNJ H<F@ NJPI?N. 1J F@@K OC@ JPOKPO NCJMO, G@O'N KMDIO OC@ NJPI?N K@M 100
FDGJH@O@MN:
DMBTT 3BJMXBZ7FIJDMF
\
WPJE BEWBODF(JO TJ[F@U LJMPNFUFST)
\
XSJUFGMO("5IF WFIJDMF JT BEWBODJOH %T LJMPNFUFST",
LJMPNFUFST);
GPSFBDI (J; 0 .. LJMPNFUFST / 100) \
XSJUFGMO(" %T", NBLF4PVOE());
^
^
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
337
// ...
^
%JR@Q@M, NBLF4PVOE() ><IIJO =@ ?@ADI@? =T 3BJMXBZ7FIJDMF =@><PN@ Q@CD>G@N H<T
C<Q@ ?DAA@M@IO NJPI?N:
w ">CJJ >CJJ" AJM -PDPNPUJWF
w ">G<>F >G<>F" AJM 3BJMXBZ$BS
Note: "73H7 Train.makeSound FA F:7 @7JF 7J7D5;E7.
B@><PN@ DO HPNO =@ JQ@MMD??@I, NBLF4PVOE() HPNO =@ ?@>G<M@? <N BCTUSBDU =T OC@
NPK@M>G<NN:
DMBTT 3BJMXBZ7FIJDMF
\
// ...
BCTUSBDU TUSJOH NBLF4PVOE();
^
&HKG@H@IO NBLF4PVOE() AJM OC@ NP=>G<NN@N <I? OMT OC@ >J?@ RDOC OC@ AJGGJRDIB
NBJO():
WPJE NBJO()
\
BVUP SBJMXBZ$BS1 = OFX 1BTTFOHFS$BS;
SBJMXBZ$BS1.BEWBODF(100);
BVUP SBJMXBZ$BS2 = OFX 'SFJHIU$BS;
SBJMXBZ$BS2.BEWBODF(200);
BVUP MPDPNPUJWF = OFX -PDPNPUJWF;
MPDPNPUJWF.BEWBODF(300);
^
*<F@ OC@ KMJBM<H KMJ?P>@ OC@ AJGGJRDIB JPOKPO:
5IF WFIJDMF JT BEWBODJOH 100 LJMPNFUFST
DMBDL DMBDL
5IF WFIJDMF JT BEWBODJOH 200 LJMPNFUFST
DMBDL DMBDL
DMBDL DMBDL
5IF WFIJDMF JT BEWBODJOH 300 LJMPNFUFST
DIPP DIPP
DIPP DIPP
DIPP DIPP
+JO@ OC<O OC@M@ DN IJ M@LPDM@H@IO OC<O OC@ NJPI?N JA 1BTTFOHFS$BS <I? 'SFJHIU$BS
=@ ?DAA@M@IO. 1C@T ><I NC<M@ OC@ N<H@ DHKG@H@IODJI AMJH 3BJMXBZ$BS.
3. 1CDIF <=JPO CJR NBLF4PVOE() ><I =@ DHKG@H@IO@? AJM 5SBJO. ,I@ D?@< DN OC<O
5SBJO.NBLF4PVOE H<T M@OPMI < TUSJOH OC<O >JINDNON JA OC@ NJPI?N JA OC@ H@H=@MN JA
5SBJO.
... F:7 EA>GF;A@E
!@:7D;F3@57
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
338
0CKFDU 58
G<NN@N OC<O ?J IJO @SKGD>DOGT DIC@MDO <IT >G<NN, <POJH<OD><GGT DIC@MDO OC@ 0CKFDU >G<NN.
BT OC<O ?@ADIDODJI, OC@ OJKHJNO >G<NN DI <IT >G<NN CD@M<M>CT DIC@MDON 0CKFDU:
// ": 0CKFDU" JT OPU XSJUUFO; JU JT BVUPNBUJD
DMBTT .VTJDBM*OTUSVNFOU : 0CKFDU
\
// ...
^
// *OIFSJUT 0CKFDU JOEJSFDUMZ
DMBTT 4USJOH*OTUSVNFOU : .VTJDBM*OTUSVNFOU
\
// ...
^
0DI>@ OC@ OJKHJNO >G<NN DIC@MDON 0CKFDU, @Q@MT >G<NN DI?DM@>OGT DIC@MDON 0CKFDU <N R@GG. &I OC<O
N@IN@, @Q@MT >G<NN "DN <I" 0CKFDU.
"Q@MT >G<NN DIC@MDON OC@ AJGGJRDIB H@H=@M API>ODJIN JA 0CKFDU:
t UP4USJOH: 1C@ TUSJOH M@KM@N@IO<ODJI JA OC@ J=E@>O.
t PQ&RVBMT: "LP<GDOT >JHK<MDNJI RDOC <IJOC@M J=E@>O.
t PQ$NQ: 0JMO JM?@M >JHK<MDNJI RDOC <IJOC@M J=E@>O.
t UP)BTI: ANNJ>D<ODQ@ <MM<T C<NC Q<GP@.
1C@ G<NO OCM@@ JA OC@N@ API>ODJIN @HKC<NDU@ OC@ Q<GP@N JA J=E@>ON. 1C@T <GNJ H<F@ < >G<NN
@GDBD=G@ AJM =@DIB OC@ F@T OTK@ JA <NNJ>D<ODQ@ <MM<TN.
B@><PN@ OC@N@ API>ODJIN <M@ DIC@MDO@?, OC@DM M@?@ADIDODJIN AJM OC@ NP=>G<NN@N M@LPDM@ OC@
PWFSSJEF F@TRJM?.
Note: Object 678;@7E AF:7D ?7?47DE 3E I7>>. ):;E 5:3BF7D I;>> ;@5>G67 A@>K F:7E7 8AGD
8G@5F;A@E.
UP4USJOH
58.1
0<H@ RDOC NOMP>ON, UP4USJOH @I<=G@N PNDIB J=E@>ON <N NOMDIBN:
BVUP DMPDL = OFX $MPDL(20, 30, 0);
XSJUFMO(DMPDL); // $BMMT DMPDL.UP4USJOH()
1C@ DIC@MDO@? UP4USJOH() DN PNP<GGT IJO PN@APG; DO KMJ?P>@N EPNO OC@ I<H@ JA OC@ OTK@:
EFOFNF.$MPDL
1C@ K<MO =@AJM@ OC@ I<H@ JA OC@ OTK@ DN OC@ I<H@ JA OC@ HJ?PG@. 1C@ JPOKPO <=JQ@ DI?D><O@N
OC<O $MPDL C<N =@@I ?@ADI@? DI OC@ EFOFNF HJ?PG@.
AN R@ C<Q@ N@@I DI OC@ KM@QDJPN >C<KO@M, OCDN API>ODJI DN <GHJNO <GR<TN JQ@MMD??@I OJ KMJ?P>@
< HJM@ H@<IDIBAPG TUSJOH M@KM@N@IO<ODJI:
JNQPSU TUE.TUSJOH;
DMBTT $MPDL
\
PWFSSJEF TUSJOH UP4USJOH() DPOTU
\
%4<75F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
339
SFUVSO GPSNBU("%02T:%02T:%02T", IPVS, NJOVUF, TFDPOE);
^
// ...
^
DMBTT "MBSN$MPDL : $MPDL
\
PWFSSJEF TUSJOH UP4USJOH() DPOTU
\
SFUVSO GPSNBU("%T |%02T:%02T", TVQFS.UP4USJOH(),
BMBSN)PVS, BMBSN.JOVUF);
^
// ...
^
// ...
BVUP CFE4JEF$MPDL = OFX "MBSN$MPDL(20, 30, 0, 7, 0);
XSJUFMO(CFE4JEF$MPDL);
1C@ JPOKPO:
20:30:00 z07:00
PQ&RVBMT
58.2
AN R@ C<Q@ N@@I DI OC@ ,K@M<OJM ,Q@MGJ<?DIB >C<KO@M, OCDN H@H=@M API>ODJI DN <=JPO OC@
=@C<QDJM JA OC@ == JK@M<OJM (<I? OC@ != JK@M<OJM DI?DM@>OGT). 1C@ M@OPMI Q<GP@ JA OC@ JK@M<OJM
HPNO =@ USVF DA OC@ J=E@>ON <M@ >JIND?@M@? OJ =@ @LP<G <I? GBMTF JOC@MRDN@.
Warning: 1C@ ?@ADIDODJI JA OCDN API>ODJI HPNO =@ >JINDNO@IO RDOC PQ$NQ(); AJM ORJ J=E@>ON
OC<O PQ&RVBMT() M@OPMIN USVF, PQ$NQ() HPNO M@OPMI U@MJ.
JIOM<MT OJ NOMP>ON, OC@ >JHKDG@M ?J@N IJO ><GG B.PQ&RVBMT(C) MDBCO <R<T RC@I DO N@@N OC@
@SKM@NNDJI B == C. 4C@I ORJ >G<NN J=E@>ON <M@ >JHK<M@? =T OC@ == JK@M<OJM, < AJPM-NO@K
<GBJMDOCH DN @S@>PO@?:
CPPM PQ&RVBMT(0CKFDU B, 0CKFDU C) \
JG (B JT C) SFUVSO USVF; // (1)
JG (B JT OVMM ]] C JT OVMM) SFUVSO GBMTF; // (2)
JG (UZQFJE(B) == UZQFJE(C)) SFUVSO B.PQ&RVBMT(C); // (3)
SFUVSO B.PQ&RVBMT(C) && C.PQ&RVBMT(B); // (4)
^
1. &A OC@ ORJ Q<MD<=G@N KMJQD?@ <>>@NN OJ OC@ N<H@ J=E@>O (JM OC@T <M@ =JOC OVMM), OC@I OC@T
<M@ @LP<G.
2. #JGGJRDIB AMJH OC@ KM@QDJPN >C@>F, DA JIGT JI@ DN OVMM OC@I OC@T <M@ IJO @LP<G.
3. &A =JOC JA OC@ J=E@>ON <M@ JA OC@ N<H@ OTK@, OC@I B.PQ&RVBMT(C) DN ><GG@? OJ ?@O@MHDI@ OC@
@LP<GDOT.
4. ,OC@MRDN@, AJM OC@ ORJ J=E@>ON OJ =@ >JIND?@M@? @LP<G, PQ&RVBMT HPNO C<Q@ =@@I ?@ADI@?
AJM =JOC JA OC@DM OTK@N <I? B.PQ&RVBMT(C) <I? C.PQ&RVBMT(B) HPNO <BM@@ OC<O OC@
J=E@>ON <M@ @LP<G.
A>>JM?DIBGT, DA PQ&RVBMT() DN IJO KMJQD?@? AJM < >G<NN OTK@, OC@I OC@ Q<GP@N JA OC@ J=E@>ON <M@
IJO >JIND?@M@?; M<OC@M, @LP<GDOT DN ?@O@MHDI@? =T >C@>FDIB RC@OC@M OC@ ORJ >G<NN Q<MD<=G@N
KMJQD?@ <>>@NN OJ OC@ N<H@ J=E@>O:
%4<75F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
340
BVUP WBSJBCMF0 = OFX $MPDL(6, 7, 8);
BVUP WBSJBCMF1 = OFX $MPDL(6, 7, 8);
BTTFSU(WBSJBCMF0 != WBSJBCMF1); // 5IFZ BSF OPU FRVBM
// CFDBVTF UIF PCKFDUT BSF
// EJGGFSFOU
"Q@I OCJPBC OC@ ORJ J=E@>ON <M@ >JINOMP>O@? =T OC@ N<H@ <MBPH@ION <=JQ@, OC@ Q<MD<=G@N <M@
IJO @LP<G =@><PN@ OC@T <M@ IJO <NNJ>D<O@? RDOC OC@ N<H@ J=E@>O.
,I OC@ JOC@M C<I?, =@><PN@ OC@ AJGGJRDIB ORJ Q<MD<=G@N KMJQD?@ <>>@NN OJ OC@ N<H@ J=E@>O,
OC@T <M@ 7CG3>:
BVUP QBSUOFS0 = OFX $MPDL(9, 10, 11);
BVUP QBSUOFS1 = QBSUOFS0;
BTTFSU(QBSUOFS0 == QBSUOFS1); // 5IFZ BSF FRVBM CFDBVTF
// UIF PCKFDU JT UIF TBNF
0JH@ODH@N DO H<F@N HJM@ N@IN@ OJ >JHK<M@ J=E@>ON =T OC@DM Q<GP@N DINO@<? JA OC@DM D?@IODOD@N.
#JM @S<HKG@, DO DN >JI>@DQ<=G@ OC<O WBSJBCMF0 <I? WBSJBCMF1 <=JQ@ >JHK<M@ @LP<G =@><PN@
OC@DM Q<GP@N <M@ OC@ N<H@.
!DAA@M@IO AMJH NOMP>ON, OC@ OTK@ JA OC@ K<M<H@O@M JA PQ&RVBMT AJM >G<NN@N DN 0CKFDU:
DMBTT $MPDL
\
PWFSSJEF CPPM PQ&RVBMT(0CKFDU P) DPOTU
\
// ...
^
// ...
^
AN TJP RDGG N@@ =@GJR, OC@ K<M<H@O@M DN <GHJNO I@Q@M PN@? ?DM@>OGT. #JM OC<O M@<NJI, DO NCJPG?
=@ <>>@KO<=G@ OJ I<H@ DO NDHKGT <N P. *JNO JA OC@ ODH@ OC@ ADMNO OCDIB OJ ?J RDOC OC<O
K<M<H@O@M DN OJ PN@ DO DI < OTK@ >JIQ@MNDJI.
1C@ K<M<H@O@M JA PQ&RVBMT DN OC@ J=E@>O OC<O <KK@<MN JI OC@ MDBCO-C<I? ND?@ JA OC@ ==
JK@M<OJM:
WBSJBCMF0 == WBSJBCMF1; // P SFQSFTFOUT WBSJBCMF1
0DI>@ OC@ KPMKJN@ JA PQ&RVBMT() DN OJ >JHK<M@ ORJ J=E@>ON JA OCDN >G<NN OTK@, OC@ ADMNO OCDIB
OJ ?J DN OJ >JIQ@MO P OJ < Q<MD<=G@ JA OC@ N<H@ OTK@ JA OCDN >G<NN. 0DI>@ DO RJPG? IJO =@
<KKMJKMD<O@ OJ HJ?DAT OC@ MDBCO-C<I? ND?@ J=E@>O DI <I @LP<GDOT >JHK<MDNJI, DO DN <GNJ KMJK@M
OJ >JIQ@MO OC@ OTK@ <N DPOTU:
PWFSSJEF CPPM PQ&RVBMT(0CKFDU P) DPOTU
\
BVUP SIT = DBTU(DPOTU $MPDL)P;
// ...
^
AN TJP RJPG? M@H@H=@M, SIT DN < >JHHJI <==M@QD<ODJI AJM D;9:F-:3@6 E;67. AGNJ,
TUE.DPOW.UP ><I =@ PN@? AJM OC@ >JIQ@MNDJI <N R@GG:
%4<75F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
341
JNQPSU TUE.DPOW;
// ...
BVUP SIT = UP!(DPOTU $MPDL)(P);
&A OC@ JMDBDI<G J=E@>O JI OC@ MDBCO-C<I? ND?@ ><I =@ >JIQ@MO@? OJ $MPDL, OC@I SIT =@>JH@N <
IJI-OVMM >G<NN Q<MD<=G@. ,OC@MRDN@, SIT DN N@O OJ OVMM, DI?D><ODIB OC<O OC@ J=E@>ON <M@ IJO JA
OC@ N<H@ OTK@.
A>>JM?DIB OJ OC@ ?@NDBI JA < KMJBM<H, DO H<T H<F@ N@IN@ OJ >JHK<M@ J=E@>ON JA ORJ
DI>JHK<OD=G@ OTK@N. & RDGG <NNPH@ C@M@ OC<O AJM OC@ >JHK<MDNJI OJ =@ Q<GD?, SIT HPNO IJO =@
OVMM; NJ, OC@ ADMNO GJBD><G @SKM@NNDJI DI OC@ AJGGJRDIB SFUVSO NO<O@H@IO >C@>FN OC<O DO DN IJO
OVMM. ,OC@MRDN@, DO RJPG? =@ <I @MMJM OJ OMT OJ <>>@NN OC@ H@H=@MN JA SIT:
DMBTT $MPDL
\
JOU IPVS;
JOU NJOVUF;
JOU TFDPOE;
PWFSSJEF CPPM PQ&RVBMT(0CKFDU P) DPOTU
\
BVUP SIT = DBTU(DPOTU $MPDL)P;
SFUVSO (SIT &&
(IPVS == SIT.IPVS) &&
(NJOVUF == SIT.NJOVUF) &&
(TFDPOE == SIT.TFDPOE));
^
// ...
^
4DOC OC<O ?@ADIDODJI, $MPDL J=E@>ON ><I IJR =@ >JHK<M@? =T OC@DM Q<GP@N:
BVUP WBSJBCMF0 = OFX $MPDL(6, 7, 8);
BVUP WBSJBCMF1 = OFX $MPDL(6, 7, 8);
BTTFSU(WBSJBCMF0 == WBSJBCMF1); // /PX UIFZ BSF FRVBM
// CFDBVTF UIFJS WBMVFT
// BSF FRVBM
4C@I ?@ADIDIB PQ&RVBMT DO DN DHKJMO<IO OJ M@H@H=@M OC@ H@H=@MN JA OC@ NPK@M>G<NN. #JM
@S<HKG@, RC@I >JHK<MDIB J=E@>ON JA "MBSN$MPDL DO RJPG? H<F@ N@IN@ OJ <GNJ >JIND?@M OC@
DIC@MDO@? H@H=@MN:
DMBTT "MBSN$MPDL : $MPDL
\
JOU BMBSN)PVS;
JOU BMBSN.JOVUF;
PWFSSJEF CPPM PQ&RVBMT(0CKFDU P) DPOTU
\
BVUP SIT = DBTU(DPOTU "MBSN$MPDL)P;
SFUVSO (SIT &&
(BMBSN)PVS == SIT.BMBSN)PVS) &&
(BMBSN.JOVUF == SIT.BMBSN.JOVUF) &&
TVQFS.PQ&RVBMT(P));
^
// ...
^
%4<75F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
342
1C<O @SKM@NNDJI >JPG? =@ RMDOO@I <N TVQFS == P <N R@GG. %JR@Q@M, OC<O RJPG? DIDOD<O@ OC@
AJPM-NO@K <GBJMDOCH <B<DI <I? <N < M@NPGO, OC@ >J?@ HDBCO =@ < GDOOG@ NGJR@M.
PQ$NQ
58.3
1CDN JK@M<OJM DN PN@? RC@I NJMODIB >G<NN J=E@>ON. PQ$NQ DN OC@ API>ODJI OC<O B@ON ><GG@? =@CDI?
OC@ N>@I@N AJM OC@ <, <=, >, <I? >=.
1CDN JK@M<OJM HPNO M@OPMI < I@B<ODQ@ Q<GP@ RC@I OC@ G@AO-C<I? J=E@>O DN =@AJM@, < KJNDODQ@
Q<GP@ RC@I OC@ G@AO-C<I? J=E@>O DN <AO@M, <I? U@MJ RC@I =JOC J=E@>ON C<Q@ OC@ N<H@ NJMODIB
JM?@M.
Warning: 1C@ ?@ADIDODJI JA OCDN API>ODJI HPNO =@ >JINDNO@IO RDOC PQ&RVBMT(); AJM ORJ J=E@>ON
OC<O PQ&RVBMT() M@OPMIN USVF, PQ$NQ() HPNO M@OPMI U@MJ.
2IGDF@ UP4USJOH <I? PQ&RVBMT, OC@M@ DN IJ ?@A<PGO DHKG@H@IO<ODJI JA OCDN API>ODJI DI
0CKFDU. &A OC@ DHKG@H@IO<ODJI DN IJO <Q<DG<=G@, >JHK<MDIB J=E@>ON AJM NJMO JM?@M ><PN@N <I
@S>@KODJI OJ =@ OCMJRI:
BVUP WBSJBCMF0 = OFX $MPDL(6, 7, 8);
BVUP WBSJBCMF1 = OFX $MPDL(6, 7, 8);
BTTFSU(WBSJBCMF0 <= WBSJBCMF1); // j $BVTFT FYDFQUJPO
PCKFDU.&YDFQUJPO: OFFE PQ$NQ GPS DMBTT EFOFNF.$MPDL
&O DN PK OJ OC@ ?@NDBI JA OC@ KMJBM<H RC<O C<KK@IN RC@I OC@ G@AO-C<I? <I? MDBCO-C<I? J=E@>ON
<M@ JA ?DAA@M@IO OTK@N. ,I@ R<T DN OJ O<F@ <?Q<IO<B@ JA OC@ JM?@M JA OTK@N OC<O DN H<DIO<DI@?
=T OC@ >JHKDG@M <POJH<OD><GGT. 1CDN DN <>CD@Q@? =T ><GGDIB OC@ PQ$NQ API>ODJI JI OC@ UZQFJE
Q<GP@N JA OC@ ORJ OTK@N:
DMBTT $MPDL
\
JOU IPVS;
JOU NJOVUF;
JOU TFDPOE;
PWFSSJEF JOU PQ$NQ(0CKFDU P) DPOTU
\
/* 5BLJOH BEWBOUBHF PG UIF BVUPNBUJDBMMZ-NBJOUBJOFE
* PSEFS PG UIF UZQFT. */
JG (UZQFJE(UIJT) != UZQFJE(P)) \
SFUVSO UZQFJE(UIJT).PQ$NQ(UZQFJE(P));
^
BVUP SIT = DBTU(DPOTU $MPDL)P;
/* /P OFFE UP DIFDL XIFUIFS SIT JT OVMM, CFDBVTF JU JT
* LOPXO BU UIJT MJOF UIBU JU IBT UIF TBNF UZQF BT P. */
JG (IPVS != SIT.IPVS) \
SFUVSO IPVS - SIT.IPVS;
^ FMTF JG (NJOVUF != SIT.NJOVUF) \
SFUVSO NJOVUF - SIT.NJOVUF;
^ FMTF \
SFUVSO TFDPOE - SIT.TFDPOE;
^
^
// ...
^
%4<75F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
343
1C@ ?@ADIDODJI <=JQ@ ADMNO >C@>FN RC@OC@M OC@ OTK@N JA OC@ ORJ J=E@>ON <M@ OC@ N<H@. &A IJO, DO
PN@N OC@ JM?@MDIB JA OC@ OTK@N OC@HN@GQ@N. ,OC@MRDN@, DO >JHK<M@N OC@ J=E@>ON =T OC@ Q<GP@N
JA OC@DM IPVS, NJOVUF, <I? TFDPOE H@H=@MN.
A >C<DI JA O@MI<MT JK@M<OJMN H<T <GNJ =@ PN@?:
PWFSSJEF JOU PQ$NQ(0CKFDU P) DPOTU
\
JG (UZQFJE(UIJT) != UZQFJE(P)) \
SFUVSO UZQFJE(UIJT).PQ$NQ(UZQFJE(P));
^
BVUP SIT = DBTU(DPOTU $MPDL)P;
SFUVSO (IPVS != SIT.IPVS
IPVS - SIT.IPVS
: (NJOVUF != SIT.NJOVUF
NJOVUF - SIT.NJOVUF
: TFDPOE - SIT.TFDPOE));
^
&A DHKJMO<IO, OC@ >JHK<MDNJI JA OC@ H@H=@MN JA OC@ NPK@M>G<NN HPNO <GNJ =@ >JIND?@M@?. 1C@
AJGGJRDIB "MBSN$MPDL.PQ$NQ DN ><GGDIB $MPDL.PQ$NQ ADMNO:
DMBTT "MBSN$MPDL : $MPDL
\
PWFSSJEF JOU PQ$NQ(0CKFDU P) DPOTU
\
BVUP SIT = DBTU(DPOTU "MBSN$MPDL)P;
DPOTU JOU TVQFS3FTVMU = TVQFS.PQ$NQ(P);
JG (TVQFS3FTVMU != 0) \
SFUVSO TVQFS3FTVMU;
^ FMTF JG (BMBSN)PVS != SIT.BMBSN)PVS) \
SFUVSO BMBSN)PVS - SIT.BMBSN)PVS;
^ FMTF \
SFUVSO BMBSN.JOVUF - SIT.BMBSN.JOVUF;
^
^
// ...
^
A=JQ@, DA OC@ NPK@M>G<NN >JHK<MDNJI M@OPMIN < IJI-U@MJ Q<GP@ OC@I OC<O M@NPGO DN PN@? =@><PN@
OC@ NJMO JM?@M JA OC@ J=E@>ON DN <GM@<?T ?@O@MHDI@? =T OC<O Q<GP@.
"MBSN$MPDL J=E@>ON ><I IJR =@ >JHK<M@? AJM OC@DM NJMO JM?@MN:
BVUP BD0 = OFX "MBSN$MPDL(8, 0, 0, 6, 30);
BVUP BD1 = OFX "MBSN$MPDL(8, 0, 0, 6, 31);
BTTFSU(BD0 < BD1);
PQ$NQ DN PN@? =T JOC@M G<IBP<B@ A@<OPM@N <I? GD=M<MD@N <N R@GG. #JM @S<HKG@, OC@ .TPSU
KMJK@MOT JA <MM<TN O<F@N <?Q<IO<B@ JA PQ$NQ RC@I NJMODIB OC@ @G@H@ION.
PQ$NQ for string members 58.3.1
4C@I NJH@ JA OC@ H@H=@MN <M@ NOMDIBN, OC@T ><I =@ >JHK<M@? @SKGD>DOGT OJ M@OPMI < I@B<ODQ@,
KJNDODQ@, JM U@MJ Q<GP@:
%4<75F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
344
JNQPSU TUE.FYDFQUJPO;
DMBTT 4UVEFOU
\
TUSJOH OBNF;
PWFSSJEF JOU PQ$NQ(0CKFDU P) DPOTU
\
BVUP SIT = DBTU(4UVEFOU)P;
FOGPSDF(SIT);
JG (OBNF < SIT.OBNF) \
SFUVSO -1;
^ FMTF JG (OBNF > SIT.OBNF) \
SFUVSO 1;
^ FMTF \
SFUVSO 0;
^
^
// ...
^
&INO@<?, OC@ @SDNODIB TUE.BMHPSJUIN.DNQ API>ODJI ><I =@ PN@?, RCD>C C<KK@IN OJ =@ A<NO@M <N
R@GG:
JNQPSU TUE.BMHPSJUIN;
DMBTT 4UVEFOU
\
TUSJOH OBNF;
PWFSSJEF JOU PQ$NQ(0CKFDU P) DPOTU
\
BVUP SIT = DBTU(4UVEFOU)P;
FOGPSDF(SIT);
SFUVSO DNQ(OBNF, SIT.OBNF);
^
// ...
^
AGNJ IJO@ OC<O 4UVEFOU ?J@N IJO NPKKJMO >JHK<MDIB DI>JHK<OD=G@ OTK@N =T @IAJM>DIB OC<O OC@
>JIQ@MNDJI AMJH 0CKFDU OJ 4UVEFOU DN KJNND=G@.
UP)BTI
58.4
1CDN API>ODJI <GGJRN J=E@>ON JA < >G<NN OTK@ OJ =@ PN@? <N <NNJ>D<ODQ@ <MM<T =7KE. &O ?J@N IJO
<AA@>O OC@ ><N@N RC@M@ OC@ OTK@ DN PN@? <N <NNJ>D<ODQ@ <MM<T H3>G7E.
Warning: !@ADIDIB JIGT OCDN API>ODJI DN IJO NPAAD>D@IO. &I JM?@M AJM OC@ >G<NN OTK@ OJ =@ PN@?
<N <NNJ>D<ODQ@ <MM<T F@TN, >JINDNO@IO ?@ADIDODJIN JA PQ&RVBMT <I? PQ$NQ HPNO <GNJ =@ ?@ADI@?.
Hash table indexes 58.4.1
ANNJ>D<ODQ@ <MM<TN <M@ < C<NC O<=G@ DHKG@H@IO<ODJI. %<NC O<=G@ DN < Q@MT A<NO ?<O< NOMP>OPM@
RC@I DO >JH@N OJ N@<M>CDIB @G@H@ION DI OC@ O<=G@. ($AF7: ";=7 ?AEF AF:7D F:;@9E ;@ EA8FI3D7,
F:;E EB776 5A?7E 3F 3 5AEF: 3E: F34>7E ?GEF =77B 7>7?7@FE ;@ 3@ G@AD67D76 I3K, 3@6 F:7K
?3K 47 F3=;@9 GB EB357 F:3F ;E ?AD7 F:3@ 7J35F>K @757EE3DK.)
%4<75F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
345
1C@ CDBC NK@@? JA C<NC O<=G@N >JH@N AMJH OC@ A<>O OC<O OC@T ADMNO KMJ?P>@ DIO@B@M Q<GP@N AJM
F@TN. 1C@N@ DIO@B@MN <M@ ><GG@? :3E: H3>G7E. 1C@ C<NC Q<GP@N <M@ OC@I PN@? AJM DI?@SDIB DIOJ
<I DIO@MI<G <MM<T OC<O DN H<DIO<DI@? =T OC@ O<=G@.
A =@I@ADO JA OCDN H@OCJ? DN OC@ A<>O OC<O <IT OTK@ OC<O ><I =@ >JIQ@MO@? OJ <I DIO@B@M Q<GP@
><I =@ PN@? <N OC@ F@T OTK@ JA <NNJ>D<ODQ@ <MM<TN. UP)BTI DN OC@ API>ODJI OC<O M@OPMIN OC@
C<NC O<=G@ DI?@S Q<GP@ AJM <I J=E@>O.
AGOCJPBC K@MC<KN IJO Q@MT H@<IDIBAPG, @Q@I $MPDL J=E@>ON ><I =@ PN@? <N <NNJ>D<ODQ@ <MM<T
F@T Q<GP@N:
TUSJOH<$MPDL> UJNF5BHT;
UJNF5BHT<OFX $MPDL(12, 0, 0)> = "/PPO";
1C@ ?@A<PGO ?@ADIDODJI JA UP)BTI OC<O DN DIC@MDO@? AMJH $MPDL KMJ?P>@N ?DAA@M@IO C<NC Q<GP@N
AJM ?DAA@M@IO J=E@>ON. 1CDN DN NDHDG<M OJ CJR OC@ ?@A<PGO =@C<QDJM JA PQ&RVBMT >JIND?@MN
?DAA@M@IO J=E@>ON <N =@DIB IJO @LP<G.
1C@ >J?@ <=JQ@ >JHKDG@N <I? MPIN @Q@I RC@I OC@M@ DN IJ NK@>D<G ?@ADIDODJI JA UP)BTI AJM
$MPDL. %JR@Q@M, DON ?@A<PGO =@C<QDJM DN <GHJNO I@Q@M RC<O DN ?@NDM@?. 1J N@@ OC<O ?@A<PGO
=@C<QDJM, G@O'N OMT OJ <>>@NN <I @G@H@IO =T <I J=E@>O OC<O DN ?DAA@M@IO AMJH OC@ JI@ OC<O C<N
=@@I PN@? RC@I DIN@MODIB OC@ @G@H@IO. AGOCJPBC OC@ I@R $MPDL J=E@>O =@GJR C<N OC@ N<H@
Q<GP@ <N OC@ $MPDL J=E@>O OC<O C<N =@@I PN@? RC@I DIN@MODIB DIOJ OC@ <NNJ>D<ODQ@ <MM<T <=JQ@,
OC@ Q<GP@ ><IIJO =@ AJPI?:
JG (OFX $MPDL(12, 0, 0) JO UJNF5BHT) \
XSJUFMO("&YJTUT");
^ FMTF \
XSJUFMO(".JTTJOH");
^
A>>JM?DIB OJ OC@ JO JK@M<OJM, OC@M@ DN IJ @G@H@IO DI OC@ O<=G@ OC<O >JMM@NKJI?N OJ OC@ Q<GP@
$MPDL(12, 0, 0):
.JTTJOH
1C@ M@<NJI AJM OCDN NPMKMDNDIB =@C<QDJM DN OC<O OC@ F@T J=E@>O OC<O C<N =@@I PN@? RC@I
DIN@MODIB OC@ @G@H@IO DN IJO OC@ N<H@ <N OC@ F@T J=E@>O OC<O C<N =@@I PN@? RC@I <>>@NNDIB
OC@ @G@H@IO.
Selecting members for UP)BTI 58.4.2
AGOCJPBC OC@ C<NC Q<GP@ DN ><G>PG<O@? AMJH OC@ H@H=@MN JA <I J=E@>O, IJO @Q@MT H@H=@M DN
NPDO<=G@ AJM OCDN O<NF.
1C@ ><I?D?<O@N <M@ OC@ H@H=@MN OC<O ?DNODIBPDNC J=E@>ON AMJH @<>C JOC@M. #JM @S<HKG@, OC@
H@H=@MN OBNF <I? MBTU/BNF JA < 4UVEFOU >G<NN RJPG? =@ NPDO<=G@ DA OCJN@ H@H=@MN ><I =@
PN@? AJM D?@IODATDIB J=E@>ON JA OC<O OTK@.
,I OC@ JOC@M C<I?, < HSBEFT <MM<T JA < 4UVEFOU >G<NN RJPG? IJO =@ NPDO<=G@ =JOC =@><PN@
H<IT J=E@>ON H<T C<Q@ OC@ N<H@ <MM<T <I? <GNJ DO DN GDF@GT OC<O OC@ HSBEFT <MM<T H<T >C<IB@
JQ@M ODH@.
%4<75F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
346
Calculating hash values 58.4.3
1C@ >CJD>@ JA C<NC Q<GP@N C<N < ?DM@>O @AA@>O JI OC@ K@MAJMH<I>@ JA <NNJ>D<ODQ@ <MM<TN.
#PMOC@MHJM@, < C<NC ><G>PG<ODJI OC<O DN @AA@>ODQ@ JI JI@ OTK@ JA ?<O< H<T IJO =@ <N @AA@>ODQ@
JI <IJOC@M OTK@ JA ?<O<. AN :3E: 3>9AD;F:?E <M@ =@TJI? OC@ N>JK@ JA OCDN =JJF, & RDGG BDQ@ EPNO
JI@ BPD?@GDI@ C@M@: &I B@I@M<G, DO DN =@OO@M OJ KMJ?P>@ ?DAA@M@IO C<NC Q<GP@N AJM J=E@>ON OC<O
<M@ >JIND?@M@? OJ C<Q@ ?DAA@M@IO Q<GP@N. %JR@Q@M, DO DN IJO <I @MMJM DA ORJ J=E@>ON RDOC
?DAA@M@IO Q<GP@N KMJ?P>@ OC@ N<H@ DI?@S Q<GP@; DO DN H@M@GT PI?@NDM<=G@ AJM K@MAJMH<I>@
M@<NJIN.
&O DN >JI>@DQ<=G@ OC<O <GG JA OC@ H@H=@MN JA $MPDL <M@ NDBIDAD><IO OJ ?DNODIBPDNC DON J=E@>ON
AMJH @<>C JOC@M. #JM OC<O M@<NJI, OC@ C<NC Q<GP@N ><I =@ ><G>PG<O@? AMJH OC@ Q<GP@N JA DON
OCM@@ H@H=@MN. ):7 @G?47D A8 E75A@6E E;@57 ?;6@;9:F RJPG? =@ @AA@>ODQ@ C<NC Q<GP@N AJM
J=E@>ON OC<O M@KM@N@IO ?DAA@M@IO KJDION DI ODH@:
DMBTT $MPDL
\
JOU IPVS;
JOU NJOVUF;
JOU TFDPOE;
PWFSSJEF TJ[F@U UP)BTI() DPOTU
\
/* #FDBVTF UIFSF BSF 3600 TFDPOET JO BO IPVS BOE 60
* TFDPOET JO B NJOVUF: */
SFUVSO (3600 * IPVS) + (60 * NJOVUF) + TFDPOE;
^
// ...
^
4C@I@Q@M $MPDL DN PN@? <N OC@ F@T OTK@ JA <NNJ>D<ODQ@ <MM<TN, OC<O NK@>D<G ?@ADIDODJI JA
UP)BTI RJPG? =@ PN@?. AN < M@NPGO, @Q@I OCJPBC OC@ ORJ F@T J=E@>ON JA $MPDL(12, 0, 0)
<=JQ@ <M@ ?DNODI>O, OC@T RJPG? IJR KMJ?P>@ OC@ N<H@ C<NC Q<GP@.
1C@ I@R JPOKPO:
&YJTUT
0DHDG<M OJ OC@ JOC@M H@H=@M API>ODJIN, OC@ NPK@M>G<NN H<T I@@? OJ =@ >JIND?@M@? <N R@GG. #JM
@S<HKG@, "MBSN$MPDL.UP)BTI ><I O<F@ <?Q<IO<B@ JA $MPDL.UP)BTI ?PMDIB DON DI?@S
><G>PG<ODJI:
DMBTT "MBSN$MPDL : $MPDL
\
JOU BMBSN)PVS;
JOU BMBSN.JOVUF;
PWFSSJEF TJ[F@U UP)BTI() DPOTU
\
SFUVSO TVQFS.UP)BTI() + BMBSN)PVS + BMBSN.JOVUF;
^
// ...
^
Note: )3=7 F:7 53>5G>3F;A@ 34AH7 <GEF 3E 3@ 7J3?B>7. !@ 97@7D3>, 366;@9 ;@F797D H3>G7E ;E @AF
3@ 78875F;H7 I3K A8 97@7D3F;@9 :3E: H3>G7E.
1C@M@ <M@ @SDNODIB @AAD>D@IO <GBJMDOCHN AJM ><G>PG<ODIB C<NC Q<GP@N AJM Q<MD<=G@N JA AGJ<ODIB
KJDIO, <MM<T, <I? NOMP>O OTK@N. 1C@N@ <GBJMDOCHN <M@ <Q<DG<=G@ OJ OC@ KMJBM<HH@M <N R@GG.
%4<75F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
347
4C<O I@@?N OJ =@ ?JI@ DN OJ ><GG HFU)BTI() JI OC@ UZQFJE JA @<>C H@H=@M. 1C@ NTIO<S JA OCDN
H@OCJ? DN OC@ N<H@ AJM AGJ<ODIB KJDIO, <MM<T, <I? NOMP>O OTK@N.
#JM @S<HKG@, C<NC Q<GP@N JA < 4UVEFOU OTK@ ><I =@ ><G>PG<O@? AMJH DON OBNF H@H=@M <N DI OC@
AJGGJRDIB >J?@:
DMBTT 4UVEFOU
\
TUSJOH OBNF;
PWFSSJEF TJ[F@U UP)BTI() DPOTU
\
SFUVSO UZQFJE(OBNF).HFU)BTI(&OBNF);
^
// ...
^
Hash values for structs 58.4.4
0DI>@ NOMP>ON <M@ Q<GP@ OTK@N, C<NC Q<GP@N AJM OC@DM J=E@>ON <M@ ><G>PG<O@? <POJH<OD><GGT =T <I
@AAD>D@IO <GBJMDOCH. 1C<O <GBJMDOCH O<F@N <GG JA OC@ H@H=@MN JA OC@ J=E@>O DIOJ >JIND?@M<ODJI.
4C@I OC@M@ DN < NK@>DAD> M@<NJI GDF@ I@@?DIB OJ @S>GP?@ >@MO<DI H@H=@MN AMJH OC@ C<NC
><G>PG<ODJI, UP)BTI() ><I =@ JQ@MMD??@I AJM NOMP>ON <N R@GG.
Exercises
58.5
1. 0O<MO RDOC OC@ AJGGJRDIB >G<NN, RCD>C M@KM@N@ION >JGJM@? KJDION:
FOVN $PMPS \ CMVF, HSFFO, SFE ^
DMBTT 1PJOU
\
JOU Y;
JOU Z;
$PMPS DPMPS;
UIJT(JOU Y, JOU Z, $PMPS DPMPS)
\
UIJT.Y = Y;
UIJT.Z = Z;
UIJT.DPMPS = DPMPS;
^
^
&HKG@H@IO PQ&RVBMT AJM OCDN >G<NN DI < R<T OC<O DBIJM@N >JGJMN. 4C@I DHKG@H@IO@? DI
OC<O R<T, OC@ AJGGJRDIB BTTFSU >C@>F NCJPG? K<NN:
// %JGGFSFOU DPMPST
BVUP CMVF1PJOU = OFX 1PJOU(1, 2, $PMPS.CMVF);
BVUP HSFFO1PJOU = OFX 1PJOU(1, 2, $PMPS.HSFFO);
// 5IFZ BSF TUJMM FRVBM
BTTFSU(CMVF1PJOU == HSFFO1PJOU);
2. &HKG@H@IO PQ$NQ =T >JIND?@MDIB ADMNO Y OC@I Z. 1C@ AJGGJRDIB BTTFSU >C@>FN NCJPG? K<NN:
BVUP SFE1PJOU1 = OFX 1PJOU(-1, 10, $PMPS.SFE);
BVUP SFE1PJOU2 = OFX 1PJOU(-2, 10, $PMPS.SFE);
BVUP SFE1PJOU3 = OFX 1PJOU(-2, 7, $PMPS.SFE);
BTTFSU(SFE1PJOU1 < CMVF1PJOU);
%4<75F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
348
BTTFSU(SFE1PJOU3 < SFE1PJOU2);
/* &WFO UIPVHI CMVF JT CFGPSF HSFFO JO FOVN $PMPS,
* CFDBVTF DPMPS JT CFJOH JHOPSFE, CMVF1PJOU NVTU OPU CF
* CFGPSF HSFFO1PJOU. */
BTTFSU(!(CMVF1PJOU < HSFFO1PJOU));
)DF@ OC@ 4UVEFOU >G<NN <=JQ@, TJP ><I DHKG@H@IO PQ$NQ =T @S>GP?DIB DI>JHK<OD=G@
OTK@N =T OC@ C@GK JA FOGPSDF.
3. JIND?@M OC@ AJGGJRDIB >G<NN OC<O >JH=DI@N OCM@@ 1PJOU J=E@>ON DI <I <MM<T:
DMBTT 5SJBOHVMBS"SFB
\
1PJOU<3> QPJOUT;
UIJT(1PJOU POF, 1PJOU UXP, 1PJOU UISFF)
\
QPJOUT = < POF, UXP, UISFF >;
^
^
&HKG@H@IO UP)BTI AJM OC<O >G<NN. AB<DI, OC@ AJGGJRDIB BTTFSU >C@>FN NCJPG? K<NN:
/* BSFB1 BOE BSFB2 BSF DPOTUSVDUFE CZ EJTUJODU QPJOUT UIBU
* IBQQFO UP IBWF UIF TBNF WBMVFT. (3FNFNCFS UIBU
* CMVF1PJOU BOE HSFFO1PJOU TIPVME CF DPOTJEFSFE FRVBM.) */
BVUP BSFB1 = OFX 5SJBOHVMBS"SFB(CMVF1PJOU, HSFFO1PJOU, SFE1PJOU1);
BVUP BSFB2 = OFX 5SJBOHVMBS"SFB(HSFFO1PJOU, CMVF1PJOU, SFE1PJOU1);
// 5IF BSFBT TIPVME CF FRVBM
BTTFSU(BSFB1 == BSFB2);
// "O BTTPDJBUJWF BSSBZ
EPVCMF<5SJBOHVMBS"SFB> BSFBT;
// " WBMVF JT CFJOH FOUFSFE CZ BSFB1
BSFBT<BSFB1> = 1.25;
// 5IF WBMVF JT CFJOH BDDFTTFE CZ BSFB2
BTTFSU(BSFB2 JO BSFBT);
BTTFSU(BSFBT<BSFB2> == 1.25);
/@H@H=@M OC<O PQ&RVBMT <I? PQ$NQ HPNO <GNJ =@ ?@ADI@? RC@I UP)BTI DN ?@ADI@?.
... F:7 EA>GF;A@E
%4<75F
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
349
Interfaces 59
1C@ JOUFSGBDF F@TRJM? DA AJM ?@ADIDIB DIO@MA<>@N DI >G<NN CD@M<M>CD@N. JOUFSGBDF DN Q@MT
NDHDG<M OJ DMBTT RDOC OC@ AJGGJRDIB M@NOMD>ODJIN:
t 1C@ H@H=@M API>ODJIN OC<O DO ?@>G<M@N (=PO IJO DHKG@H@ION) <M@ <=NOM<>O @Q@I RDOCJPO OC@
BCTUSBDU F@TRJM?.
t 1C@ H@H=@M API>ODJIN OC<O DO DHKG@H@ION HPNO =@ TUBUJD JM GJOBM. (TUBUJD <I? GJOBM
H@H=@M API>ODJIN <M@ @SKG<DI@? =@GJR.)
t &ON H@H=@M Q<MD<=G@N HPNO =@ TUBUJD.
t &IO@MA<>@N ><I DIC@MDO JIGT DIO@MA<>@N.
!@NKDO@ OC@N@ M@NOMD>ODJIN, OC@M@ DN IJ GDHDO JI OC@ IPH=@M JA JOUFSGBDFN OC<O < >G<NN ><I
DIC@MDO AMJH. (&I >JIOM<NO, < >G<NN ><I DIC@MDO AMJH PK OJ JI@ DMBTT.)
Definition
59.1
&IO@MA<>@N <M@ ?@ADI@? =T OC@ JOUFSGBDF F@TRJM?, OC@ N<H@ R<T <N >G<NN@N:
JOUFSGBDF 4PVOE&NJUUFS
\
// ...
^
AI JOUFSGBDF DA AJM ?@>G<MDIB H@H=@M API>ODJIN OC<O <M@ DHKGD>DOGT <=NOM<>O:
JOUFSGBDF 4PVOE&NJUUFS
\
TUSJOH FNJU4PVOE(); // %FDMBSFE (OPU JNQMFNFOUFE)
^
G<NN@N OC<O DIC@MDO AMJH OC<O DIO@MA<>@ RJPG? C<Q@ OJ KMJQD?@ OC@ DHKG@H@IO<ODJIN JA OC@
<=NOM<>O API>ODJIN JA OC@ DIO@MA<>@.
Inheriting from an JOUFSGBDF
59.2
1C@ JOUFSGBDF DIC@MDO<I>@ NTIO<S DN OC@ N<H@ <N DMBTT DIC@MDO<I>@:
DMBTT 7JPMJO : 4PVOE&NJUUFS
\
TUSJOH FNJU4PVOE()
\
SFUVSO "z{{";
^
^
DMBTT #FMM : 4PVOE&NJUUFS
\
TUSJOH FNJU4PVOE()
\
SFUVSO "EJOH";
^
^
&IO@MA<>@N NPKKJMO KJGTHJMKCDNH: #PI>ODJIN OC<O O<F@ DIO@MA<>@ K<M<H@O@MN ><I PN@ OCJN@
K<M<H@O@MN RDOCJPO I@@?DIB OJ FIJR OC@ <>OP<G OTK@N JA J=E@>ON. #JM @S<HKG@, OC@ AJGGJRDIB
API>ODJI OC<O O<F@N < K<M<H@O@M JA 4PVOE&NJUUFS ><GGN FNJU4PVOE() JI OC<O K<M<H@O@M
RDOCJPO I@@?DIB OJ FIJR OC@ <>OP<G OTK@ JA OC@ J=E@>O:
!@F7D8357E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
350
WPJE VTF4PVOE&NJUUJOH0CKFDU(4PVOE&NJUUFS PCKFDU)
\
// ... TPNF PQFSBUJPOT ...
XSJUFMO(PCKFDU.FNJU4PVOE());
// ... NPSF PQFSBUJPOT ...
^
'PNO GDF@ RDOC >G<NN@N, OC<O API>ODJI ><I =@ ><GG@? RDOC <IT OTK@ JA J=E@>O OC<O DIC@MDON AMJH
OC@ 4PVOE&NJUUFS DIO@MA<>@:
VTF4PVOE&NJUUJOH0CKFDU(OFX 7JPMJO);
VTF4PVOE&NJUUJOH0CKFDU(OFX #FMM);
1C@ NK@>D<G FNJU4PVOE() API>ODJI AJM @<>C J=E@>O RJPG? B@O ><GG@? <I? OC@ JPOKPON JA
7JPMJO.FNJU4PVOE <I? #FMM.FNJU4PVOE RJPG? =@ KMDIO@?:
xyy
EJOH
Inheriting from more than one JOUFSGBDF
59.3
A >G<NN ><I =@ DIC@MDO@? AMJH PK OJ JI@ DMBTT. 1C@M@ DN IJ GDHDO JI OC@ IPH=@M JA JOUFSGBDFN
OJ DIC@MDO AMJH.
)@O'N >JIND?@M OC@ AJGGJRDIB DIO@MA<>@ OC<O M@KM@N@ION >JHHPID><ODJI ?@QD>@N:
JOUFSGBDF $PNNVOJDBUJPO%FWJDF
\
WPJE UBML(TUSJOH NFTTBHF);
TUSJOH MJTUFO();
^
&A < 1IPOF >G<NN I@@?N OJ =@ PN@? =JOC <N < NJPI? @HDOO@M <I? < >JHHPID><ODJI ?@QD>@, DO ><I
DIC@MDO =JOC JA OCJN@ DIO@MA<>@N:
DMBTT 1IPOF : 4PVOE&NJUUFS, $PNNVOJDBUJPO%FWJDF
\
// ...
^
1C<O ?@ADIDODJI M@KM@N@ION =JOC JA OC@N@ M@G<ODJINCDKN: "KCJI@ DN < NJPI? @HDOO@M" <I? "KCJI@
DN < >JHHPID><ODJI ?@QD>@."
&I JM?@M OJ >JINOMP>O J=E@>ON JA OCDN >G<NN, 1IPOF HPNO DHKG@H@IO OC@ <=NOM<>O API>ODJIN JA
=JOC JA OC@ DIO@MA<>@N:
DMBTT 1IPOF : 4PVOE&NJUUFS, $PNNVOJDBUJPO%FWJDF
\
TUSJOH FNJU4PVOE() // GPS 4PVOE&NJUUFS
\
SFUVSO "SSSJOH";
^
WPJE UBML(TUSJOH NFTTBHF) // GPS $PNNVOJDBUJPO%FWJDF
\
// ... QVU UIF NFTTBHF PO UIF MJOF ...
^
TUSJOH MJTUFO() // GPS $PNNVOJDBUJPO%FWJDF
\
!@F7D8357E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
351
TUSJOH TPVOE0O5IF-JOF;
// ... HFU UIF NFTTBHF GSPN UIF MJOF ...
SFUVSO TPVOE0O5IF-JOF;
^
^
A >G<NN ><I DIC@MDO AMJH <IT IPH=@M JA DIO@MA<>@N <N DO H<F@N N@IN@ <>>JM?DIB OJ OC@ ?@NDBI
JA OC@ KMJBM<H.
Inheriting from JOUFSGBDF and DMBTT
59.4
G<NN@N ><I NODGG DIC@MDO AMJH PK OJ JI@ DMBTT <N R@GG:
DMBTT $MPDL
\
// ... DMPDL JNQMFNFOUBUJPO ...
^
DMBTT "MBSN$MPDL : $MPDL, 4PVOE&NJUUFS
\
TUSJOH FNJU4PVOE()
\
SFUVSO "CFFQ";
^
^
"MBSN$MPDL DIC@MDON OC@ H@H=@MN JA $MPDL. A??DODJI<GGT, DO <GNJ KMJQD?@N OC@ FNJU4PVOE()
API>ODJI OC<O OC@ 4PVOE&NJUUFS DIO@MA<>@ M@LPDM@N.
Inheriting JOUFSGBDF from JOUFSGBDF
59.5
AI DIO@MA<>@ OC<O DN DIC@MDO@? AMJH <IJOC@M DIO@MA<>@ @AA@>ODQ@GT DI>M@<N@N OC@ IPH=@M JA
API>ODJIN OC<O OC@ NP=>G<NN@N HPNO DHKG@H@IO:
JOUFSGBDF .VTJDBM*OTUSVNFOU : 4PVOE&NJUUFS
\
WPJE BEKVTU5VOJOH();
^
A>>JM?DIB OJ OC@ ?@ADIDODJI <=JQ@, DI JM?@M OJ =@ < .VTJDBM*OTUSVNFOU, =JOC OC@
FNJU4PVOE() API>ODJI OC<O 4PVOE&NJUUFS M@LPDM@N <I? OC@ BEKVTU5VOJOH() API>ODJI OC<O
.VTJDBM*OTUSVNFOU M@LPDM@N HPNO =@ DHKG@H@IO@?.
#JM @S<HKG@, DA 7JPMJO DIC@MDON AMJH .VTJDBM*OTUSVNFOU DINO@<? JA 4PVOE&NJUUFS, DO HPNO
IJR <GNJ DHKG@H@IO BEKVTU5VOJOH():
DMBTT 7JPMJO : .VTJDBM*OTUSVNFOU
\
TUSJOH FNJU4PVOE() // GPS 4PVOE&NJUUFS
\
SFUVSO "z{{";
^
WPJE BEKVTU5VOJOH() // GPS .VTJDBM*OTUSVNFOU
\
// ... TQFDJBM UVOJOH PG UIF WJPMJO ...
^
^
!@F7D8357E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
352
TUBUJD member functions
59.6
& C<Q@ ?@G<T@? @SKG<DIDIB TUBUJD H@H=@M API>ODJIN PIODG OCDN >C<KO@M OJ F@@K OC@ @<MGD@M
>C<KO@MN NCJMO@M. TUBUJD H@H=@M API>ODJIN <M@ <Q<DG<=G@ AJM NOMP>ON, >G<NN@N, <I? DIO@MA<>@N.
/@BPG<M H@H=@M API>ODJIN <M@ <GR<TN ><GG@? JI <I J=E@>O. 1C@ H@H=@M Q<MD<=G@N OC<O <M@
M@A@M@I>@? DIND?@ OC@ H@H=@M API>ODJI <M@ OC@ H@H=@MN JA < K<MOD>PG<M J=E@>O:
TUSVDU 'PP
\
JOU J;
WPJE NPEJGZ(JOU WBMVF)
\
J = WBMVF;
^
^
WPJE NBJO()
\
BVUP PCKFDU0 = 'PP();
BVUP PCKFDU1 = 'PP();
PCKFDU0.NPEJGZ(10); // PCKFDU0.J DIBOHFT
PCKFDU1.NPEJGZ(10); // PCKFDU1.J DIBOHFT
^
1C@ H@H=@MN ><I <GNJ =@ M@A@M@I>@? =T UIJT:
WPJE NPEJGZ(JOU WBMVF)
\
UIJT.J = WBMVF; // FRVJWBMFOU PG UIF QSFWJPVT POF
^
A TUBUJD H@H=@M API>ODJI ?J@N IJO JK@M<O@ JI <I J=E@>O; OC@M@ DN IJ J=E@>O OC<O OC@ UIJT
F@TRJM? RJPG? M@A@M OJ, NJ UIJT DN IJO Q<GD? DIND?@ < TUBUJD API>ODJI. #JM OC<O M@<NJI, IJI@
JA OC@ M@BPG<M H@H=@M Q<MD<=G@N <M@ <Q<DG<=G@ DIND?@ TUBUJD H@H=@M API>ODJIN:
TUSVDU 'PP
\
JOU J;
TUBUJD WPJE DPNNPO'VODUJPO(JOU WBMVF)
\
J = WBMVF; // j DPNQJMBUJPO &3303
UIJT.J = WBMVF; // j DPNQJMBUJPO &3303
^
^
TUBUJD H@H=@M API>ODJIN ><I PN@ JIGT OC@ TUBUJD H@H=@M Q<MD<=G@N.
)@O'N M@?@NDBI OC@ 1PJOU NOMP>O OC<O R@ C<Q@ N@@I @<MGD@M DI OC@ 0OMP>ON >C<KO@M, OCDN ODH@
RDOC < TUBUJD H@H=@M API>ODJI. &I OC@ AJGGJRDIB >J?@, @Q@MT 1PJOU J=E@>O B@ON < PIDLP@ D?,
RCD>C DN ?@O@MHDI@? =T < TUBUJD H@H=@M API>ODJI:
JNQPSU TUE.TUEJP;
TUSVDU 1PJOU
\
TJ[F@U JE; // 0CKFDU JE
JOU MJOF;
JOU DPMVNO;
// 5IF JE UP CF VTFE GPS UIF OFYU PCKFDU
!@F7D8357E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
353
TUBUJD TJ[F@U OFYU*E;
UIJT (JOU MJOF, JOU DPMVNO)
\
UIJT.MJOF = MJOF;
UIJT.DPMVNO = DPMVNO;
UIJT.JE = NBLF/FX*E();
^
TUBUJD TJ[F@U NBLF/FX*E()
\
JNNVUBCMF OFX*E = OFYU*E;
++OFYU*E;
SFUVSO OFX*E;
^
^
WPJE NBJO()
\
BVUP UPQ = 1PJOU(7, 0);
BVUP NJEEMF = 1PJOU(8, 0);
BVUP CPUUPN = 1PJOU(9, 0);
XSJUFMO(UPQ.JE);
XSJUFMO(NJEEMF.JE);
XSJUFMO(CPUUPN.JE);
^
1C@ TUBUJD NBLF/FX*E() API>ODJI ><I PN@ OC@ >JHHJI Q<MD<=G@ OFYU*E. AN < M@NPGO, @Q@MT
J=E@>O B@ON < PIDLP@ D?:
0
1
2
AGOCJPBC OC@ @S<HKG@ <=JQ@ >JIO<DIN < TUSVDU, TUBUJD H@H=@M API>ODJIN <M@ <Q<DG<=G@ AJM
>G<NN@N <I? DIO@MA<>@N <N R@GG.
GJOBM member functions
59.7
& C<Q@ ?@G<T@? @SKG<DIDIB GJOBM H@H=@M API>ODJIN PIODG OCDN >C<KO@M OJ F@@K OC@ @<MGD@M
>C<KO@MN NCJMO@M. GJOBM H@H=@M API>ODJIN <M@ <Q<DG<=G@ JIGT AJM >G<NN@N <I? DIO@MA<>@N; OC@T
<M@ IJO M@G@Q<IO OJ NOMP>ON =@><PN@ NOMP>ON ?J IJO NPKKJMO DIC@MDO<I>@.
GJOBM NK@>DAD@N OC<O < H@H=@M API>ODJI ><IIJO =@ M@?@ADI@? =T < NP=>G<NN. &I < N@IN@, OC@
DHKG@H@IO<ODJI OC<O OCDN DMBTT JM JOUFSGBDF KMJQD?@N DN OC@ 8;@3> DHKG@H@IO<ODJI JA OC<O
API>ODJI. AI @S<HKG@ JA < ><N@ RC@M@ OCDN A@<OPM@ DN PN@APG DN RC@M@ OC@ B@I@M<G NO@KN JA <I
<GBJMDOCH <M@ ?@ADI@? =T <I DIO@MA<>@ <I? OC@ ADI@M ?@O<DGN <M@ G@AO OJ NP=>G<NN@N.
)@O'N N@@ <I @S<HKG@ JA OCDN RDOC < (BNF DIO@MA<>@. 1C@ B@I@M<G NO@KN JA KG<TDIB < B<H@ DN
=@DIB ?@O@MHDI@? =T OC@ QMBZ() API>ODJI JA OC@ AJGGJRDIB JOUFSGBDF:
JOUFSGBDF (BNF
\
GJOBM WPJE QMBZ()
\
TUSJOH OBNF = HBNF/BNF();
XSJUFGMO("4UBSUJOH %T", OBNF);
JOUSPEVDF1MBZFST();
QSFQBSF();
CFHJO();
!@F7D8357E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
354
FOE();
XSJUFGMO("&OEJOH %T", OBNF);
^
TUSJOH HBNF/BNF();
WPJE JOUSPEVDF1MBZFST();
WPJE QSFQBSF();
WPJE CFHJO();
WPJE FOE();
^
&O DN IJO KJNND=G@ AJM NP=>G<NN@N OJ HJ?DAT OC@ ?@ADIDODJI JA OC@ QMBZ() H@H=@M API>ODJI. 1C@
NP=>G<NN@N ><I (<I? HPNO) KMJQD?@ OC@ ?@ADIDODJIN JA OC@ ADQ@ <=NOM<>O H@H=@M API>ODJIN OC<O
<M@ ?@>G<M@? =T OC@ DIO@MA<>@. BT ?JDIB NJ, OC@ NP=>G<NN@N >JHKG@O@ OC@ HDNNDIB NO@KN JA OC@
<GBJMDOCH:
JNQPSU TUE.TUEJP;
JNQPSU TUE.TUSJOH;
JNQPSU TUE.SBOEPN;
JNQPSU TUE.DPOW;
DMBTT %JDF4VNNJOH(BNF : (BNF
\
TUSJOH QMBZFS;
TJ[F@U DPVOU;
TJ[F@U TVN;
TUSJOH HBNF/BNF()
\
SFUVSO "%JDF 4VNNJOH (BNF";
^
WPJE JOUSPEVDF1MBZFST()
\
XSJUF("8IBU JT ZPVS OBNF ");
QMBZFS = DIPNQ(SFBEMO());
^
WPJE QSFQBSF()
\
XSJUF(")PX NBOZ UJNFT UP UISPX UIF EJDF ");
SFBEG(" %T", &DPVOU);
TVN = 0;
^
WPJE CFHJO()
\
GPSFBDI (J; 0 .. DPVOU) \
JNNVUBCMF EJDF = VOJGPSN(1, 7);
XSJUFGMO("%T: %T", J, EJDF);
TVN += EJDF;
^
^
WPJE FOE()
\
XSJUFGMO("1MBZFS: %T, %JDF TVN: %T, "WFSBHF: %T",
QMBZFS, TVN, UP!EPVCMF(TVN) / DPVOU);
^
^
WPJE VTF(BNF((BNF HBNF)
\
HBNF.QMBZ();
^
WPJE NBJO()
!@F7D8357E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
355
\
VTF(BNF(OFX %JDF4VNNJOH(BNF());
^
AGOCJPBC OC@ @S<HKG@ <=JQ@ >JIO<DIN <I JOUFSGBDF, GJOBM H@H=@M API>ODJIN <M@ <Q<DG<=G@
AJM >G<NN@N <N R@GG.
How to use
59.8
JOUFSGBDF DN < >JHHJIGT PN@? A@<OPM@. 1C@M@ DN JI@ JM HJM@ JOUFSGBDF <O OC@ OJK JA <GHJNO
@Q@MT >G<NN CD@M<M>CT. A FDI? JA CD@M<M>CT OC<O DN >JHHJIGT @I>JPIO@M@? DI KMJBM<HN DIQJGQ@N
< NDIBG@ JOUFSGBDF <I? < IPH=@M JA >G<NN@N OC<O DHKG@H@IO OC<O DIO@MA<>@:
.VTJDBM*OTUSVNFOU
(JOUFSGBDF)
/ ] = =
7JPMJO (VJUBS 'MVUF ...
AGOCJPBC OC@M@ <M@ HJM@ >JHKGD><O@? CD@M<M>CD@N DI KM<>OD>@, OC@ NDHKG@ CD@M<M>CT <=JQ@
NJGQ@N H<IT KMJ=G@HN.
&O DN <GNJ >JHHJI OJ HJQ@ >JHHJI DHKG@H@IO<ODJI ?@O<DGN JA >G<NN CD@M<M>CD@N OJ
DIO@MH@?D<O@ >G<NN@N. 1C@ NP=>G<NN@N DIC@MDO AMJH OC@N@ DIO@MH@?D<O@ >G<NN@N. 1C@
4USJOH*OTUSVNFOU <I? 8JOE*OTUSVNFOU >G<NN@N =@GJR ><I >JIO<DI OC@ >JHHJI H@H=@MN JA
OC@DM M@NK@>ODQ@ NP=>G<NN@N:
.VTJDBM*OTUSVNFOU
(JOUFSGBDF)
/ =
4USJOH*OTUSVNFOU 8JOE*OTUSVNFOU
/ ] = / ] =
7JPMJO 7JPMB ... 'MVUF $MBSJOFU ...
1C@ NP=>G<NN@N RJPG? DHKG@H@IO OC@DM M@NK@>ODQ@ NK@>D<G ?@ADIDODJIN JA H@H=@M API>ODJIN.
Abstraction
59.9
&IO@MA<>@N C@GK H<F@ K<MON JA KMJBM<HN DI?@K@I?@IO AMJH @<>C JOC@M. 1CDN DN ><GG@?
34EFD35F;A@. #JM @S<HKG@, < KMJBM<H OC<O ?@<GN RDOC HPND><G DINOMPH@ION ><I =@ RMDOO@I
KMDH<MDGT =T PNDIB OC@ .VTJDBM*OTUSVNFOU DIO@MA<>@, RDOCJPO @Q@M NK@>DATDIB OC@ <>OP<G OTK@N
JA OC@ HPND><G DINOMPH@ION.
A .VTJDJBO >G<NN ><I >JIO<DI < .VTJDBM*OTUSVNFOU RDOCJPO @Q@M FIJRDIB OC@ <>OP<G OTK@ JA
OC@ DINOMPH@IO:
DMBTT .VTJDJBO
\
.VTJDBM*OTUSVNFOU JOTUSVNFOU;
// ...
^
!DAA@M@IO OTK@N JA HPND><G DINOMPH@ION ><I =@ >JH=DI@? DI < >JGG@>ODJI RDOCJPO M@B<M? OJ OC@
<>OP<G OTK@N JA OC@N@ DINOMPH@ION:
.VTJDBM*OTUSVNFOU<> PSDIFTUSB*OTUSVNFOUT;
*JNO JA OC@ API>ODJIN JA OC@ KMJBM<H ><I =@ RMDOO@I JIGT =T PNDIB OCDN DIO@MA<>@:
!@F7D8357E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
356
CPPM OFFET5VOJOH(.VTJDBM*OTUSVNFOU JOTUSVNFOU)
\
CPPM SFTVMU;
// ...
SFUVSO SFTVMU;
^
WPJE QMBZ*O5VOF(.VTJDBM*OTUSVNFOU JOTUSVNFOU)
\
JG (OFFET5VOJOH(JOTUSVNFOU))\
JOTUSVNFOU.BEKVTU5VOJOH();
^
XSJUFMO(JOTUSVNFOU.FNJU4PVOE());
^
A4EFD35F;@9 3I3K K<MON JA < KMJBM<H AMJH @<>C JOC@M <GGJRN H<FDIB >C<IB@N DI JI@ K<MO JA
OC@ KMJBM<H RDOCJPO I@@?DIB OJ HJ?DAT OC@ JOC@M K<MON. 4C@I DHKG@H@IO<ODJIN JA >@MO<DI
K<MON JA OC@ KMJBM<H <M@ 47:;@6 < K<MOD>PG<M DIO@MA<>@, OC@ >J?@ OC<O PN@N JIGT OC<O DIO@MA<>@
?J@N IJO B@O <AA@>O@?.
Example
59.10
1C@ AJGGJRDIB KMJBM<H ?@ADI@N OC@ 4PVOE&NJUUFS, .VTJDBM*OTUSVNFOU, <I?
$PNNVOJDBUJPO%FWJDF DIO@MA<>@N:
JNQPSU TUE.TUEJP;
JOUFSGBDF 4PVOE&NJUUFS
\
TUSJOH FNJU4PVOE();
^
/* 5IJT DMBTT OFFET UP JNQMFNFOU POMZ FNJU4PVOE(). */
DMBTT #FMM : 4PVOE&NJUUFS
\
TUSJOH FNJU4PVOE()
\
SFUVSO "EJOH";
^
^
JOUFSGBDF .VTJDBM*OTUSVNFOU : 4PVOE&NJUUFS
\
WPJE BEKVTU5VOJOH();
^
/* 5IJT DMBTT OFFET UP JNQMFNFOU CPUI FNJU4PVOE() BOE
* BEKVTU5VOJOH(). */
DMBTT 7JPMJO : .VTJDBM*OTUSVNFOU
\
TUSJOH FNJU4PVOE()
\
SFUVSO "z{{";
^
WPJE BEKVTU5VOJOH()
\
// ... UVOJOH PG UIF WJPMJO ...
^
^
JOUFSGBDF $PNNVOJDBUJPO%FWJDF
\
WPJE UBML(TUSJOH NFTTBHF);
TUSJOH MJTUFO();
!@F7D8357E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
357
^
/* 5IJT DMBTT OFFET UP JNQMFNFOU FNJU4PVOE(), UBML(), BOE
* MJTUFO(). */
DMBTT 1IPOF : 4PVOE&NJUUFS, $PNNVOJDBUJPO%FWJDF
\
TUSJOH FNJU4PVOE()
\
SFUVSO "SSSJOH";
^
WPJE UBML(TUSJOH NFTTBHF)
\
// ... QVU UIF NFTTBHF PO UIF MJOF ...
^
TUSJOH MJTUFO()
\
TUSJOH TPVOE0O5IF-JOF;
// ... HFU UIF NFTTBHF GSPN UIF MJOF ...
SFUVSO TPVOE0O5IF-JOF;
^
^
DMBTT $MPDL
\
// ... UIF JNQMFNFOUBUJPO PG $MPDL ...
^
/* 5IJT DMBTT OFFET UP JNQMFNFOU POMZ FNJU4PVOE(). */
DMBTT "MBSN$MPDL : $MPDL, 4PVOE&NJUUFS
\
TUSJOH FNJU4PVOE()
\
SFUVSO "CFFQ";
^
// ... UIF JNQMFNFOUBUJPO PG "MBSN$MPDL ...
^
WPJE NBJO()
\
4PVOE&NJUUFS<> EFWJDFT;
EFWJDFT _= OFX #FMM;
EFWJDFT _= OFX 7JPMJO;
EFWJDFT _= OFX 1IPOF;
EFWJDFT _= OFX "MBSN$MPDL;
GPSFBDI (EFWJDF; EFWJDFT) \
XSJUFMO(EFWJDF.FNJU4PVOE());
^
^
B@><PN@ EFWJDFT DN < 4PVOE&NJUUFS NGD>@, DO ><I >JIO<DI J=E@>ON JA <IT OTK@ OC<O DIC@MDON
AMJH 4PVOE&NJUUFS (D.@. OTK@N OC<O C<Q@ <I "DN <" M@G<ODJINCDK RDOC 4PVOE&NJUUFS). AN <
M@NPGO, OC@ JPOKPO JA OC@ KMJBM<H >JINDNON JA ?DAA@M@IO NJPI?N OC<O <M@ @HDOO@? =T OC@
?DAA@M@IO OTK@N JA J=E@>ON:
EJOH
xyy
SSSJOH
CFFQ
!@F7D8357E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
358
Summary
59.11
t JOUFSGBDF ?@ADI@N <I DIO@MA<>@, RCD>C DN NDHDG<M OJ >G<NN@N OC<O >JINDNO JIGT JA <=NOM<>O
API>ODJIN. JOUFSGBDF ><I >JIO<DI JIGT TUBUJD H@H=@M Q<MD<=G@N <I? TUBUJD JM GJOBM
H@H=@M API>ODJIN.
t &I JM?@M OJ =@ <=G@ OJ >JINOMP>O J=E@>ON JA < >G<NN, OC<O >G<NN HPNO DI>GP?@ DHKG@H@IO<ODJIN
AJM <GG JA OC@ H@H=@M API>ODJIN JA <GG JA OC@ DIO@MA<>@N OC<O DO DIC@MDON AMJH.
t JOUFSGBDF ?J@N IJO C<Q@ < M@NOMD>ODJI JA DMBTT: &O DN KJNND=G@ OJ DIC@MDO AMJH PIGDHDO@?
IPH=@M JA JOUFSGBDFN.
t A >JHHJI CD@M<M>CT >JINDNON JA < NDIBG@ JOUFSGBDF <I? < IPH=@M JA NP=>G<NN@N OC<O
DHKG@H@IO OC<O DIO@MA<>@.
!@F7D8357E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
359
EFTUSPZ and TDPQFE 60
4@ C<Q@ N@@I OC@ GDA@ODH@N JA J=E@>ON DI OC@ )DA@ODH@N <I? #PI?<H@IO<G ,K@M<ODJIN >C<KO@M.
&I G<O@M >C<KO@MN, R@ C<Q@ N@@I OC<O OC@ J=E@>ON <M@ KM@K<M@? AJM PN@ DI OC@ >JINOMP>OJM,
RCD>C DN ><GG@? UIJT(); <I? OC@ ADI<G JK@M<ODJIN JA J=E@>ON <M@ <KKGD@? DI OC@ ?@NOMP>OJM,
RCD>C DN ><GG@? _UIJT().
#JM NOMP>ON <I? JOC@M Q<GP@ OTK@N, OC@ ?@NOMP>OJM DN @S@>PO@? <O OC@ ODH@ RC@I OC@ GDA@ODH@ JA
<I J=E@>O @I?N. #JM >G<NN@N <I? JOC@M M@A@M@I>@ OTK@N, DO DN @S@>PO@? =T OC@ B<M=<B@ >JGG@>OJM
NJH@ ODH@ DI OC@ APOPM@. 1C@ DHKJMO<IO ?DNODI>ODJI DN OC<O OC@ ?@NOMP>OJM JA < >G<NN J=E@>O DN
IJO @S@>PO@? RC@I DON GDA@ODH@ @I?N.
0TNO@H M@NJPM>@N <M@ >JHHJIGT M@OPMI@? =<>F OJ OC@ NTNO@H DI ?@NOMP>OJMN. #JM @S<HKG@,
TUE.TUEJP.'JMF M@OPMIN OC@ ADG@ M@NJPM>@ =<>F OJ OC@ JK@M<ODIB NTNO@H DI DON ?@NOMP>OJM. AN DO
DN IJO >@MO<DI RC@I OC@ ?@NOMP>OJM JA < >G<NN J=E@>O RDGG =@ ><GG@?, OC@ NTNO@H M@NJPM>@N OC<O DO
CJG?N H<T IJO =@ M@OPMI@? PIODG OJJ G<O@ RC@I JOC@M J=E@>ON ><IIJO B@O < CJG? JA OC@ N<H@
M@NJPM>@.
An example of calling destructors late
60.1
)@O'N ?@ADI@ < >G<NN OJ N@@ OC@ @AA@>ON JA @S@>PODIB >G<NN ?@NOMP>OJMN G<O@. 1C@ AJGGJRDIB
>JINOMP>OJM DI>M@H@ION < TUBUJD >JPIO@M, <I? OC@ ?@NOMP>OJM ?@>M@H@ION DO. AN TJP
M@H@H=@M, OC@M@ DN JIGT JI@ JA @<>C TUBUJD H@H=@M, RCD>C DN NC<M@? =T <GG JA OC@ J=E@>ON JA <
OTK@. 0P>C < >JPIO@M RJPG? DI?D><O@ OC@ IPH=@M JA J=E@>ON OC<O <M@ T@O OJ =@ ?@NOMJT@?.
DMBTT -JGFUJNF0CTFSWFE
\
JOU<> BSSBZ; // j #FMPOHT UP FBDI PCKFDU
TUBUJD TJ[F@U DPVOUFS; // j 4IBSFE CZ BMM PCKFDUT
UIJT()
\
/*
* 8F BSF VTJOH B SFMBUJWFMZ MBSHF BSSBZ UP NBLF FBDI
* PCKFDU DPOTVNF B MBSHF BNPVOU PG NFNPSZ. )PQFGVMMZ
* UIJT XJMM NBLF UIF HBSCBHF DPMMFDUPS DBMM PCKFDU
* EFTUSVDUPST NPSF GSFRVFOUMZ UP PQFO VQ TQBDF GPS
* NPSF PCKFDUT.
*/
BSSBZ.MFOHUI = 30@000;
/*
* *ODSFNFOU UIF DPVOUFS GPS UIJT PCKFDU UIBU JT CFJOH
* DPOTUSVDUFE.
*/
++DPVOUFS;
^
_UIJT()
\
/*
* %FDSFNFOU UIF DPVOUFS GPS UIJT PCKFDU UIBU JT CFJOH
* EFTUSPZFE.
*/
--DPVOUFS;
^
^
1C@ AJGGJRDIB KMJBM<H >JINOMP>ON J=E@>ON JA OC<O >G<NN DIND?@ < GJJK:
67EFDAK 3@6 E5AB76
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
360
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
GPSFBDI (J; 0 .. 20) \
BVUP WBSJBCMF = OFX -JGFUJNF0CTFSWFE; // j TUBSU
XSJUF(-JGFUJNF0CTFSWFE.DPVOUFS, ' ');
^ // j FOE
XSJUFMO();
^
1C@ GDA@ODH@ JA @<>C -JGFUJNF0CTFSWFE J=E@>O DN DI A<>O Q@MT NCJMO: &ON GDA@ NO<MON RC@I DO DN
>JINOMP>O@? =T OC@ OFX F@TRJM? <I? @I?N <O OC@ >GJNDIB >PMGT =M<>F@O JA OC@ GPSFBDI GJJK.
"<>C J=E@>O OC@I =@>JH@N OC@ M@NKJIND=DGDOT JA OC@ B<M=<B@ >JGG@>OJM. 1C@ TUBSU <I? FOE
>JHH@ION DI?D><O@ OC@ NO<MO <I? @I? JA OC@ GDA@ODH@N.
"Q@I OCJPBC OC@M@ DN PK OJ JI@ J=E@>O <GDQ@ <O < BDQ@I ODH@, OC@ Q<GP@ JA OC@ >JPIO@M DI?D><O@N
OC<O OC@ ?@NOMP>OJM DN IJO @S@>PO@? RC@I OC@ GDA@ODH@ @I?N:
1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 2 3 4 5 6
A>>JM?DIB OJ OC<O JPOKPO, OC@ H@HJMT NR@@K <GBJMDOCH JA OC@ B<M=<B@ >JGG@>OJM C<N ?@G<T@?
@S@>PODIB OC@ ?@NOMP>OJM AJM PK OJ 8 J=E@>ON. ($AF7: ):7 AGFBGF ?3K 47 6;887D7@F 67B7@6;@9 A@
F:7 93D4397 5A>>75F;A@ 3>9AD;F:?, 3H3;>34>7 ?7?ADK, 3@6 AF:7D 835FADE.)
EFTUSPZ() to execute the destructor
60.2
EFTUSPZ() @S@>PO@N OC@ ?@NOMP>OJM AJM <I J=E@>O:
WPJE NBJO()
\
GPSFBDI (J; 0 .. 20) \
BVUP WBSJBCMF = OFX -JGFUJNF0CTFSWFE;
XSJUF(-JGFUJNF0CTFSWFE.DPVOUFS, ' ');
EFTUSPZ(WBSJBCMF);
^
XSJUFMO();
^
)DF@ =@AJM@, OC@ Q<GP@ JA -JGFUJNF0CTFSWFE.DPVOUFS DN DI>M@H@IO@? =T OC@ >JINOMP>OJM <N <
M@NPGO JA OFX, <I? =@>JH@N 1. 1CDN ODH@, MDBCO <AO@M DO B@ON KMDIO@?, EFTUSPZ() @S@>PO@N OC@
?@NOMP>OJM AJM OC@ J=E@>O <I? OC@ Q<GP@ JA OC@ >JPIO@M DN ?@>M@H@IO@? <B<DI ?JRI OJ U@MJ. #JM
OC<O M@<NJI, OCDN ODH@ DON Q<GP@ DN <GR<TN 1:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
,I>@ ?@NOMJT@?, OC@ J=E@>O NCJPG? =@ >JIND?@M@? OJ =@ DI <I DIQ<GD? NO<O@ <I? HPNO IJO =@
PN@? <ITHJM@:
EFTUSPZ(WBSJBCMF);
// ...
// 8BSOJOH: 6TJOH B QPUFOUJBMMZ JOWBMJE PCKFDU
XSJUFMO(WBSJBCMF.BSSBZ);
AGOCJPBC EFTUSPZ() DN KMDH<MDGT AJM M@A@M@I>@ OTK@N, DO ><I <GNJ =@ ><GG@? JI TUSVDU J=E@>ON
OJ ?@NOMJT OC@H =@AJM@ OC@ @I? JA OC@DM IJMH<G GDA@ODH@N.
67EFDAK 3@6 E5AB76
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
361
When to use
60.3
AN C<N =@@I N@@I DI OC@ KM@QDJPN @S<HKG@, EFTUSPZ() DN PN@? RC@I M@NJPM>@N I@@? OJ =@
M@G@<N@? <O < NK@>DAD> ODH@ RDOCJPO M@GTDIB JI OC@ B<M=<B@ >JGG@>OJM.
Example
60.4
4@ C<? ?@NDBI@? <I 9NM&MFNFOU NOMP>O DI OC@ JINOMP>OJM <I? ,OC@M 0K@>D<G #PI>ODJIN
>C<KO@M. 1C<O NOMP>O R<N =@DIB PN@? AJM KMDIODIB 5*) @G@H@ION DI OC@ AJMH<O
<UBH>WBMVF</UBH>. -MDIODIB OC@ >GJNDIB O<B C<N =@@I OC@ M@NKJIND=DGDOT JA OC@ ?@NOMP>OJM:
TUSVDU 9NM&MFNFOU
\
// ...
_UIJT()
\
XSJUFMO(JOEFOUBUJPO, "</", OBNF, '>');
^
^
1C@ AJGGJRDIB JPOKPO R<N KMJ?P>@? =T < KMJBM<H OC<O PN@? OC<O NOMP>O. 1CDN ODH@, & <H
M@KG<>DIB OC@ RJM? ">G<NN" RDOC ">JPMN@" OJ <QJD? >JIAPNDIB DO RDOC OC@ DMBTT F@TRJM?:
<DPVSTFT>
<DPVSTF0>
<HSBEF>
72
</HSBEF> l 5IF DMPTJOH UBHT BQQFBS PO DPSSFDU MJOFT
<HSBEF>
97
</HSBEF> l
<HSBEF>
90
</HSBEF> l
</DPVSTF0> l
<DPVSTF1>
<HSBEF>
77
</HSBEF> l
<HSBEF>
87
</HSBEF> l
<HSBEF>
56
</HSBEF> l
</DPVSTF1> l
</DPVSTFT> l
1C@ KM@QDJPN JPOKPO C<KK@IN OJ =@ >JMM@>O =@><PN@ 9NM&MFNFOU DN < TUSVDU. 1C@ ?@NDM@?
JPOKPO DN <>CD@Q@? NDHKGT =T KG<>DIB OC@ J=E@>ON DI <KKMJKMD<O@ N>JK@N:
WPJE NBJO()
\
JNNVUBCMF DPVSTFT = 9NM&MFNFOU("DPVSTFT", 0);
67EFDAK 3@6 E5AB76
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
362
GPSFBDI (DPVSTF*E; 0 .. 2) \
JNNVUBCMF DPVSTF5BH = "DPVSTF" _ UP!TUSJOH(DPVSTF*E);
JNNVUBCMF DPVSTF&MFNFOU = 9NM&MFNFOU(DPVSTF5BH, 1);
GPSFBDI (J; 0 .. 3) \
JNNVUBCMF HSBEF&MFNFOU = 9NM&MFNFOU("HSBEF", 2);
JNNVUBCMF SBOEPN(SBEF = VOJGPSN(50, 101);
XSJUFMO(JOEFOUBUJPO4USJOH(3), SBOEPN(SBEF);
^ // j HSBEF&MFNFOU JT EFTUSPZFE
^ // j DPVSTF&MFNFOU JT EFTUSPZFE
^ // j DPVSTFT JT EFTUSPZFE
1C@ ?@NOMP>OJM KMDION OC@ >GJNDIB O<BN <N OC@ J=E@>ON B@ON ?@NOMJT@?.
1J N@@ CJR >G<NN@N =@C<Q@ ?DAA@M@IOGT, G@O'N >JIQ@MO 9NM&MFNFOU OJ < >G<NN:
JNQPSU TUE.TUEJP;
JNQPSU TUE.BSSBZ;
JNQPSU TUE.SBOEPN;
JNQPSU TUE.DPOW;
TUSJOH JOEFOUBUJPO4USJOH(JO JOU MFWFM)
\
SFUVSO SFQMJDBUF(" ", MFWFM * 2);
^
DMBTT 9NM&MFNFOU
\
TUSJOH OBNF;
TUSJOH JOEFOUBUJPO;
UIJT(JO TUSJOH OBNF, JO JOU MFWFM)
\
UIJT.OBNF = OBNF;
UIJT.JOEFOUBUJPO = JOEFOUBUJPO4USJOH(MFWFM);
XSJUFMO(JOEFOUBUJPO, '<', OBNF, '>');
^
_UIJT()
\
XSJUFMO(JOEFOUBUJPO, "</", OBNF, '>');
^
^
WPJE NBJO()
\
JNNVUBCMF DPVSTFT =
OFX JNNVUBCMF(9NM&MFNFOU)("DPVSTFT", 0);
GPSFBDI (DPVSTF*E; 0 .. 2) \
JNNVUBCMF DPVSTF5BH = "DPVSTF" _ UP!TUSJOH(DPVSTF*E);
JNNVUBCMF DPVSTF&MFNFOU =
OFX JNNVUBCMF(9NM&MFNFOU)(DPVSTF5BH, 1);
GPSFBDI (J; 0 .. 3) \
JNNVUBCMF HSBEF&MFNFOU =
OFX JNNVUBCMF(9NM&MFNFOU)("HSBEF", 2);
JNNVUBCMF SBOEPN(SBEF = VOJGPSN(50, 101);
XSJUFMO(JOEFOUBUJPO4USJOH(3), SBOEPN(SBEF);
^
^
^
67EFDAK 3@6 E5AB76
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
363
AN OC@ M@NKJIND=DGDOT JA ><GGDIB OC@ ?@NOMP>OJMN <M@ IJR G@AO OJ OC@ B<M=<B@ >JGG@>OJM, OC@
KMJBM<H ?J@N IJO KMJ?P>@ OC@ ?@NDM@? JPOKPO:
<DPVSTFT>
<DPVSTF0>
<HSBEF>
57
<HSBEF>
98
<HSBEF>
87
<DPVSTF1>
<HSBEF>
84
<HSBEF>
60
<HSBEF>
99
</HSBEF> l 5IF DMPTJOH UBHT BQQFBS BU UIF FOE
</HSBEF> l
</HSBEF> l
</DPVSTF1> l
</HSBEF> l
</HSBEF> l
</HSBEF> l
</DPVSTF0> l
</DPVSTFT> l
1C@ ?@NOMP>OJM DN NODGG @S@>PO@? AJM @Q@MT J=E@>O =PO OCDN ODH@ <O OC@ @I? RC@I OC@ KMJBM<H DN
@SDODIB. ($AF7: ):7 93D4397 5A>>75FAD 6A7E @AF 9G3D3@F77 F:3F F:7 67EFDG5FAD I;>> 47 53>>76 8AD
7H7DK A4<75F. !@ D73>;FK, ;F ;E BAEE;4>7 F:3F F:7D7 3D7 @A 5>AE;@9 F39E BD;@F76 3F 3>>.)
EFTUSPZ() @INPM@N OC<O OC@ ?@NOMP>OJM DN ><GG@? <O ?@NDM@? KJDION DI OC@ KMJBM<H:
WPJE NBJO()
\
JNNVUBCMF DPVSTFT =
OFX JNNVUBCMF(9NM&MFNFOU)("DPVSTFT", 0);
GPSFBDI (DPVSTF*E; 0 .. 2) \
JNNVUBCMF DPVSTF5BH = "DPVSTF" _ UP!TUSJOH(DPVSTF*E);
JNNVUBCMF DPVSTF&MFNFOU =
OFX JNNVUBCMF(9NM&MFNFOU)(DPVSTF5BH, 1);
GPSFBDI (J; 0 .. 3) \
JNNVUBCMF HSBEF&MFNFOU =
OFX JNNVUBCMF(9NM&MFNFOU)("HSBEF", 2);
JNNVUBCMF SBOEPN(SBEF = VOJGPSN(50, 101);
XSJUFMO(JOEFOUBUJPO4USJOH(3), SBOEPN(SBEF);
EFTUSPZ(HSBEF&MFNFOU);
^
EFTUSPZ(DPVSTF&MFNFOU);
^
EFTUSPZ(DPVSTFT);
^
67EFDAK 3@6 E5AB76
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
364
4DOC OCJN@ >C<IB@N, OC@ JPOKPO JA OC@ >J?@ IJR H<O>C@N OC@ JPOKPO JA OC@ >J?@ OC<O PN@
NOMP>ON:
<DPVSTFT>
<DPVSTF0>
<HSBEF>
66
</HSBEF> l 5IF DMPTJOH UBHT BQQFBS PO DPSSFDU MJOFT
<HSBEF>
75
</HSBEF> l
<HSBEF>
68
</HSBEF> l
</DPVSTF0> l
<DPVSTF1>
<HSBEF>
73
</HSBEF> l
<HSBEF>
62
</HSBEF> l
<HSBEF>
100
</HSBEF> l
</DPVSTF1> l
</DPVSTFT> l
TDPQFE() to call the destructor automatically
60.5
1C@ KMJBM<H <=JQ@ C<N < R@<FI@NN: 1C@ N>JK@N H<T =@ @SDO@? =@AJM@ OC@ EFTUSPZ() GDI@N
<M@ @S@>PO@?, >JHHJIGT =T OCMJRI @S>@KODJIN. &A OC@ EFTUSPZ() GDI@N HPNO =@ @S@>PO@? @Q@I
RC@I @S>@KODJIN <M@ OCMJRI, < NJGPODJI DN OJ O<F@ <?Q<IO<B@ JA TDPQF() <I? JOC@M A@<OPM@N
OC<O R@ C<Q@ N@@I DI OC@ "S>@KODJIN >C<KO@M.
AIJOC@M NJGPODJI DN OJ >JINOMP>O >G<NN J=E@>ON =T TUE.UZQFDPOT.TDPQFE DINO@<? JA =T OC@ OFX
F@TRJM?. TDPQFE() RM<KN OC@ >G<NN J=E@>O DIND?@ < TUSVDU <I? OC@ ?@NOMP>OJM JA OC<O TUSVDU
J=E@>O ?@NOMJTN OC@ >G<NN J=E@>O RC@I DON@GA BJ@N JPO JA N>JK@.
1C@ @AA@>O JA TDPQFE() DN OJ H<F@ >G<NN J=E@>ON =@C<Q@ NDHDG<M OJ NOMP>O J=E@>ON M@B<M?DIB
GDA@ODH@N.
4DOC OC@ AJGGJRDIB >C<IB@N, OC@ KMJBM<H KMJ?P>@N OC@ @SK@>O@? JPOKPO <N =@AJM@:
JNQPSU TUE.UZQFDPOT;
// ...
WPJE NBJO()
\
JNNVUBCMF DPVSTFT =
TDPQFE!(JNNVUBCMF(9NM&MFNFOU))("DPVSTFT", 0);
GPSFBDI (DPVSTF*E; 0 .. 2) \
JNNVUBCMF DPVSTF5BH = "DPVSTF" _ UP!TUSJOH(DPVSTF*E);
JNNVUBCMF DPVSTF&MFNFOU =
TDPQFE!(JNNVUBCMF(9NM&MFNFOU))(DPVSTF5BH, 1);
GPSFBDI (J; 0 .. 3) \
67EFDAK 3@6 E5AB76
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
365
JNNVUBCMF HSBEF&MFNFOU =
TDPQFE!(JNNVUBCMF(9NM&MFNFOU))("HSBEF", 2);
JNNVUBCMF SBOEPN(SBEF = VOJGPSN(50, 101);
XSJUFMO(JOEFOUBUJPO4USJOH(3), SBOEPN(SBEF);
^
^
^
+JO@ OC<O OC@M@ <M@ IJ EFTUSPZ() GDI@N <ITHJM@.
AGNJ IJO@ OC<O OC@ JNNVUBCMF F@TRJM?N H<?@ OCDN KMJBM<H GJJF HJM@ >JHKGD><O@? OC<I
I@>@NN<MT. 1C@ >J?@ RJPG? =@ NDHKG@M RDOC HPO<=G@ J=E@>ON:
BVUP DPVSTFT = TDPQFE!9NM&MFNFOU("DPVSTFT", 0);
Summary
60.6
t EFTUSPZ() DN AJM @S@>PODIB OC@ ?@NOMP>OJM JA < >G<NN J=E@>O @SKGD>DOGT.
t ,=E@>ON OC<O <M@ >JINOMP>O@? =T TDPQFE() <M@ ?@NOMJT@? PKJI G@<QDIB OC@DM M@NK@>ODQ@
N>JK@N.
67EFDAK 3@6 E5AB76
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
366
Modules and Libraries 61
1C@ =PDG?DIB =GJ>FN JA ! KMJBM<HN (<I? GD=M<MD@N) <M@ HJ?PG@N.
! HJ?PG@N <M@ =<N@? JI < NDHKG@ >JI>@KO: "Q@MT NJPM>@ ADG@ DN < HJ?PG@. A>>JM?DIBGT, OC@
NDIBG@ ADG@N OC<O R@ C<Q@ =@@I RMDODIB JPM KMJBM<HN DI C<Q@ <GG =@@I DI?DQD?P<G HJ?PG@N.
BT ?@A<PGO, OC@ I<H@ JA < HJ?PG@ DN OC@ N<H@ <N DON ADG@I<H@ RDOCJPO OC@ .E @SO@INDJI. 4C@I
@SKGD>DOGT NK@>DAD@?, OC@ I<H@ JA OC@ HJ?PG@ DN ?@ADI@? =T OC@ NPEVMF F@TRJM?, RCD>C HPNO
<KK@<M <N OC@ ADMNO IJI->JHH@IO GDI@ DI OC@ NJPM>@ ADG@.
#JM @S<HKG@, <NNPHDIB OC<O OC@ I<H@ JA < NJPM>@ ADG@ DN "><O.?", OC@ I<H@ JA OC@ HJ?PG@
RJPG? =@ NK@>DAD@? =T OC@ NPEVMF F@TRJM?:
NPEVMF DBU;
DMBTT $BU
\
// ...
^
1C@ NPEVMF GDI@ DN JKODJI<G. 4C@I IJO NK@>DAD@?, DO DN OC@ N<H@ <N OC@ ADG@ I<H@ RDOCJPO OC@ .E
@SO@INDJI.
File and module names
61.1
! NPKKJMON 2ID>J?@ DI NJPM>@ >J?@ <I? HJ?PG@ I<H@N. %JR@Q@M, OC@ 2ID>J?@ NPKKJMO JA ADG@
NTNO@HN Q<MT. #JM @S<HKG@, <GOCJPBC HJNO )DIPS ADG@ NTNO@HN NPKKJMO 2ID>J?@, OC@ ADG@ I<H@N
DI 4DI?JRN ADG@ NTNO@HN H<T IJO ?DNODIBPDNC =@OR@@I GJR@M <I? PKK@M ><N@ G@OO@MN.
A??DODJI<GGT, HJNO ADG@ NTNO@HN GDHDO OC@ >C<M<>O@MN OC<O ><I =@ PN@? DI ADG@ <I? ?DM@>OJMT
I<H@N.
#JM KJMO<=DGDOT M@<NJIN, & M@>JHH@I? OC<O TJP PN@ JIGT GJR@M ><N@ A0&& G@OO@MN DI ADG@ I<H@N.
#JM @S<HKG@, "M@NPH@.?" RJPG? =@ < NPDO<=G@ ADG@ I<H@ AJM < >G<NN I<H@? 3bTVNb.
A>>JM?DIBGT, OC@ I<H@ JA OC@ HJ?PG@ RJPG? >JINDNO JA A0&& G@OO@MN <N R@GG:
NPEVMF SFTVNF; // .PEVMF OBNF DPOTJTUJOH PG "4$** MFUUFST
DMBTT 3aTVNa // 1SPHSBN DPEF DPOTJTUJOH PG 6OJDPEF DIBSBDUFST
\
// ...
^
Packages
61.2
A >JH=DI<ODJI JA M@G<O@? HJ?PG@N <M@ ><GG@? < B35=397. ! K<>F<B@N <M@ < NDHKG@ >JI>@KO <N
R@GG: 1C@ NJPM>@ ADG@N OC<O <M@ DIND?@ OC@ N<H@ ?DM@>OJMT <M@ >JIND?@M@? OJ =@GJIB OJ OC@ N<H@
K<>F<B@. 1C@ I<H@ JA OC@ ?DM@>OJMT =@>JH@N OC@ I<H@ JA OC@ K<>F<B@, RCD>C HPNO <GNJ =@
NK@>DAD@? <N OC@ ADMNO K<MON JA HJ?PG@ I<H@N.
#JM @S<HKG@, DA "><O.?" <I? "?JB.?" <M@ DIND?@ OC@ ?DM@>OJMT "<IDH<G", OC@I NK@>DATDIB OC@
?DM@>OJMT I<H@ <GJIB RDOC OC@ HJ?PG@ I<H@ H<F@N OC@H =@ < K<MO JA OC@ N<H@ K<>F<B@:
NPEVMF BOJNBM.DBU;
DMBTT $BU
\
#A6G>7E 3@6 ";4D3D;7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
367
// ...
^
0DHDG<MGT, AJM OC@ EPH HJ?PG@:
NPEVMF BOJNBM.EPH;
DMBTT %PH
\
// ...
^
0DI>@ K<>F<B@ I<H@N >JMM@NKJI? OJ ?DM@>OJMT I<H@N, OC@ K<>F<B@ I<H@N JA HJ?PG@N OC<O <M@
?@@K@M OC<I JI@ ?DM@>OJMT G@Q@G HPNO M@AG@>O OC<O CD@M<M>CT. #JM @S<HKG@, DA OC@ "<IDH<G"
?DM@>OJMT DI>GP?@? < "Q@MO@=M<O@" ?DM@>OJMT, OC@ I<H@ JA < HJ?PG@ DIND?@ OC<O ?DM@>OJMT RJPG?
DI>GP?@ WFSUFCSBUF <N R@GG:
NPEVMF BOJNBM.WFSUFCSBUF.DBU;
1C@ ?DM@>OJMT CD@M<M>CD@N ><I =@ <M=DOM<MDGT >JHKG@S ?@K@I?DIB JI OC@ I@@?N JA OC@ KMJBM<H.
/@G<ODQ@GT NCJMO KMJBM<HN PNP<GGT C<Q@ <GG JA OC@DM NJPM>@ ADG@N DI < NDIBG@ ?DM@>OJMT.
Using modules in programs
61.3
1C@ JNQPSU F@TRJM?, RCD>C R@ C<Q@ =@@I PNDIB DI <GHJNO @Q@MT KMJBM<H NJ A<M, DN AJM
DIOMJ?P>DIB < HJ?PG@ OJ OC@ >PMM@IO HJ?PG@:
JNQPSU TUE.TUEJP;
1C@ HJ?PG@ I<H@ H<T >JIO<DI OC@ K<>F<B@ I<H@ <N R@GG. #JM @S<HKG@, OC@ TUE. K<MO <=JQ@
DI?D><O@N OC<O TUEJP DN < HJ?PG@ OC<O DN < K<MO JA OC@ TUE K<>F<B@.
1C@ BOJNBM.DBU <I? BOJNBM.EPH HJ?PG@N RJPG? =@ DHKJMO@? NDHDG<MGT. )@O'N <NNPH@ OC<O OC@
AJGGJRDIB >J?@ DN DIND?@ < ADG@ I<H@? "?@I@H@.?":
NPEVMF EFOFNF; // UIF OBNF PG UIJT NPEVMF
JNQPSU BOJNBM.DBU; // B NPEVMF UIBU JU VTFT
JNQPSU BOJNBM.EPH; // BOPUIFS NPEVMF UIBU JU VTFT
WPJE NBJO()
\
BVUP DBU = OFX $BU();
BVUP EPH = OFX %PH();
^
Note: AE 67E5D;476 47>AI, 8AD F:7 BDA9D3? FA 47 4G;>F 5ADD75F>K, F:AE7 ?A6G>7 8;>7E ?GEF 3>EA
47 BDAH;676 FA F:7 >;@=7D.
Locations of modules
61.4
1C@ >JHKDG@M ADI?N OC@ HJ?PG@ ADG@N =T >JIQ@MODIB OC@ K<>F<B@ <I? HJ?PG@ I<H@N ?DM@>OGT OJ
?DM@>OJMT <I? ADG@ I<H@N.
#JM @S<HKG@, OC@ KM@QDJPN ORJ HJ?PG@N RJPG? =@ GJ><O@? <N "<IDH<G/><O.?" <I? "<IDH<G/
?JB.?", M@NK@>ODQ@GT (JM "<IDH<G9><O.?" <I? "<IDH<G9?JB.?", ?@K@I?DIB JI OC@ ADG@ NTNO@H).
JIND?@MDIB OC@ H<DI NJPM>@ ADG@ <N R@GG, OC@ KMJBM<H <=JQ@ >JINDNON JA OCM@@ ADG@N.
#A6G>7E 3@6 ";4D3D;7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
368
Long and short module names
61.5
1C@ I<H@N OC<O <M@ PN@? DI OC@ KMJBM<H H<T =@ NK@GG@? JPO RDOC OC@ HJ?PG@ <I? K<>F<B@
I<H@N:
BVUP DBU0 = $BU();
BVUP DBU1 = BOJNBM.DBU.$BU(); // TBNF BT BCPWF
1C@ GJIB I<H@N <M@ IJMH<GGT IJO I@@?@? =PO NJH@ODH@N OC@M@ <M@ I<H@ >JIAGD>ON. #JM
@S<HKG@, RC@I M@A@MMDIB OJ < I<H@ OC<O <KK@<MN DI HJM@ OC<I JI@ HJ?PG@, OC@ >JHKDG@M
><IIJO ?@>D?@ RCD>C JI@ DN H@<IO.
1C@ AJGGJRDIB KMJBM<H DN NK@GGDIB JPO OC@ GJIB I<H@N OJ ?DNODIBPDNC =@OR@@I ORJ N@K<M<O@
+BHVBS NOMP>ON OC<O <M@ ?@ADI@? DI ORJ N@K<M<O@ HJ?PG@N: BOJNBM <I? DBS:
JNQPSU BOJNBM.KBHVBS;
JNQPSU DBS.KBHVBS;
// ...
BVUP DPOGMJDUFE = +BHVBS(); // j DPNQJMBUJPO &3303
BVUP NZ"OJNBM = BOJNBM.KBHVBS.+BHVBS(); // j DPNQJMFT
BVUP NZ$BS = DBS.KBHVBS.+BHVBS(); // j DPNQJMFT
Adding module definitions to the program
61.6
1C@ JNQPSU F@TRJM? DN IJO NPAAD>D@IO OJ H<F@ HJ?PG@N =@>JH@ K<MON JA OC@ KMJBM<H. &O
NDHKGT H<F@N <Q<DG<=G@ OC@ A@<OPM@N JA < HJ?PG@ DIND?@ OC@ >PMM@IO HJ?PG@. 1C<O HP>C DN
I@@?@? JIGT OJ 5A?B;>7 OC@ >J?@.
&O DN IJO KJNND=G@ OJ =PDG? OC@ KM@QDJPN KMJBM<H JIGT =T OC@ H<DI NJPM>@ ADG@, "?@I@H@.?":
$ ENE EFOFNF.E -X
EFOFNF.P: *O GVODUJPO A@%NBJO':
EFOFNF.E: VOEFGJOFE SFGFSFODF UP A@%6BOJNBM3DBU3$BU7@@$MBTT;'
EFOFNF.E: VOEFGJOFE SFGFSFODF UP A@%6BOJNBM3EPH3%PH7@@$MBTT;'
DPMMFDU2: ME SFUVSOFE 1 FYJU TUBUVT
--- FSSPSMFWFM 1
1CJN@ @MMJM H@NN<B@N <M@ B@I@M<O@? =T OC@ >;@=7D. AGOCJPBC OC@T <M@ IJO PN@M-AMD@I?GT
H@NN<B@N, OC@T DI?D><O@ OC<O NJH@ ?@ADIDODJIN OC<O <M@ I@@?@? =T OC@ KMJBM<H <M@ HDNNDIB.
1C@ <>OP<G =PDG? JA OC@ KMJBM<H DN OC@ M@NKJIND=DGDOT JA OC@ GDIF@M, RCD>C B@ON ><GG@?
<POJH<OD><GGT =T OC@ >JHKDG@M =@CDI? OC@ N>@I@N. 1C@ >JHKDG@M K<NN@N OC@ HJ?PG@N OC<O DO C<N
EPNO >JHKDG@? OJ OC@ GDIF@M, <I? OC@ GDIF@M >JH=DI@N OCJN@ HJ?PG@N (<I? GD=M<MD@N) OJ KMJ?P>@
OC@ @S@>PO<=G@ KMJBM<H.
#JM OC<O M@<NJI, <GG JA OC@ HJ?PG@N OC<O H<F@ PK OC@ KMJBM<H HPNO =@ KMJQD?@? OJ OC@ GDIF@M.
#JM OC@ KMJBM<H <=JQ@ OJ =@ =PDGO, "<IDH<G/><O.?" <I? "<IDH<G/?JB.?" HPNO <GNJ =@ NK@>DAD@?
JI OC@ >JHKDG<ODJI GDI@:
$ ENE EFOFNF.E BOJNBM/DBU.E BOJNBM/EPH.E -X
&INO@<? JA C<QDIB OJ H@IODJI OC@ HJ?PG@N DI?DQD?P<GGT @Q@MT ODH@ JI OC@ >JHH<I? GDI@, OC@T
><I =@ >JH=DI@? <N GD=M<MD@N.
#A6G>7E 3@6 ";4D3D;7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
369
Libraries
61.7
A >JGG@>ODJI JA >JHKDG@? HJ?PG@N DN ><GG@? < GD=M<MT. )D=M<MD@N <M@ IJO KMJBM<HN OC@HN@GQ@N;
OC@T ?J IJO C<Q@ OC@ NBJO() API>ODJI. )D=M<MD@N >JIO<DI >JHKDG@? ?@ADIDODJIN JA API>ODJIN,
NOMP>ON, >G<NN@N, <I? JOC@M A@<OPM@N JA HJ?PG@N, RCD>C <M@ OJ =@ GDIF@? G<O@M =T OC@ GDIF@M OJ
KMJ?P>@ OC@ KMJBM<H.
?H?'N -MJC >JHH<I? GDI@ JKODJI DN AJM H<FDIB GD=M<MD@N. 1C@ AJGGJRDIB >JHH<I? H<F@N <
GD=M<MT OC<O >JIO<DIN OC@ "><O.?" <I? OC@ "?JB.?" HJ?PG@N. 1C@ I<H@ JA OC@ GD=M<MT DN NK@>DAD@?
=T OC@ -PG NRDO>C:
$ ENE BOJNBM/DBU.E BOJNBM/EPH.E -MJC -PGBOJNBM -X
1C@ <>OP<G I<H@ JA OC@ GD=M<MT ADG@ ?@K@I?N JI OC@ KG<OAJMH. #JM @S<HKG@, OC@ @SO@INDJI JA
GD=M<MT ADG@N DN .B PI?@M )DIPS NTNO@HN: BOJNBM.B.
,I>@ OC<O GD=M<MT DN =PDGO, &O DN IJO I@>@NN<MT OJ NK@>DAT OC@ "<IDH<G/><O.?" <I? "<IDH<G/?JB.?"
HJ?PG@N DI?DQD?P<GGT <ITHJM@. 1C@ GD=M<MT ADG@ DN NPAAD>D@IO:
$ ENE EFOFNF.E BOJNBM.B -X
1C@ >JHH<I? <=JQ@ M@KG<>@N OC@ AJGGJRDIB JI@:
$ ENE EFOFNF.E BOJNBM/DBU.E BOJNBM/EPH.E -X
AN <I @S>@KODJI, OC@ ! NO<I?<M? GD=M<MT -CJ=JN I@@? IJO =@ NK@>DAD@? JI OC@ >JHH<I? GDI@.
1C<O GD=M<MT DN <POJH<OD><GGT DI>GP?@? =@CDI? OC@ N>@I@N. ,OC@MRDN@, DO >JPG? =@ NK@>DAD@?
NDHDG<M OJ OC@ AJGGJRDIB GDI@:
$ ENE EFOFNF.E BOJNBM.B /VTS/MJC64/MJCQIPCPT2.B -X
Note: ):7 @3?7 3@6 >A53F;A@ A8 F:7 &:A4AE >;4D3DK ?3K 47 6;887D7@F A@ 6;887D7@F EKEF7?E.
#A6G>7E 3@6 ";4D3D;7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
370
Encapsulation and Protection Attributes 62
AGG JA OC@ NOMP>ON <I? >G<NN@N OC<O R@ C<Q@ ?@ADI@? NJ A<M C<Q@ <GG =@@I <>>@NND=G@ AMJH OC@
AGFE;67.
)@O'N >JIND?@M OC@ AJGGJRDIB NOMP>O:
FOVN (FOEFS \ GFNBMF, NBMF ^
TUSVDU 4UVEFOU
\
TUSJOH OBNF;
(FOEFS HFOEFS;
^
1C@ H@H=@MN JA OC<O NOMP>O DN AM@@GT <>>@NND=G@ OJ OC@ M@NO JA OC@ KMJBM<H:
BVUP TUVEFOU = 4UVEFOU("5JN", (FOEFS.NBMF);
XSJUFGMO("%T JT B %T TUVEFOU.", TUVEFOU.OBNF, TUVEFOU.HFOEFS);
0P>C AM@@?JH DN < >JIQ@ID@I>@ DI KMJBM<HN. #JM @S<HKG@, OC@ KM@QDJPN GDI@ C<N =@@I PN@APG
OJ KMJ?P>@ OC@ AJGGJRDIB JPOKPO:
5JN JT B NBMF TUVEFOU.
%JR@Q@M, OCDN AM@@?JH DN <GNJ < GD<=DGDOT. AN <I @S<HKG@, G@O'N <NNPH@ OC<O K@MC<KN =T HDNO<F@,
OC@ I<H@ JA < NOP?@IO J=E@>O B@ON HJ?DAD@? DI OC@ KMJBM<H:
TUVEFOU.OBNF = ""OOB";
1C<O <NNDBIH@IO H<T KPO OC@ J=E@>O DI <I DIQ<GD? NO<O@:
"OOB JT B NBMF TUVEFOU.
AN <IJOC@M @S<HKG@, G@O'N >JIND?@M < 4DIPPM >G<NN. )@O'N <NNPH@ OC<O OCDN >G<NN C<N ORJ
H@H=@M Q<MD<=G@N OC<O NOJM@ OC@ IPH=@MN JA OC@ H<G@ <I? A@H<G@ NOP?@ION N@K<M<O@GT:
DMBTT 4DIPPM
\
4UVEFOU<> TUVEFOUT;
TJ[F@U GFNBMF$PVOU;
TJ[F@U NBMF$PVOU;
WPJE BEE(JO 4UVEFOU TUVEFOU)
\
TUVEFOUT _= TUVEFOU;
GJOBM TXJUDI (TUVEFOU.HFOEFS) \
DBTF (FOEFS.GFNBMF:
++GFNBMF$PVOU;
CSFBL;
DBTF (FOEFS.NBMF:
++NBMF$PVOU;
CSFBL;
^
^
PWFSSJEF TUSJOH UP4USJOH() DPOTU
\
E@53BEG>3F;A@ 3@6 &DAF75F;A@ AFFD;4GF7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
371
SFUVSO GPSNBU("%T GFNBMF, %T NBMF; UPUBM %T TUVEFOUT",
GFNBMF$PVOU, NBMF$PVOU, TUVEFOUT.MFOHUI);
^
^
1C@ BEE() H@H=@M API>ODJI <??N NOP?@ION RCDG@ @INPMDIB OC<O OC@ >JPION <M@ <GR<TN >JMM@>O:
BVUP TDIPPM = OFX 4DIPPM;
TDIPPM.BEE(4UVEFOU("-JOETFZ", (FOEFS.GFNBMF));
TDIPPM.BEE(4UVEFOU(".BSL", (FOEFS.NBMF));
XSJUFMO(TDIPPM);
1C@ KMJBM<H KMJ?P>@N OC@ AJGGJRDIB >JINDNO@IO JPOKPO:
1 GFNBMF, 1 NBMF; UPUBM 2 TUVEFOUT
%JR@Q@M, =@DIB <=G@ OJ <>>@NN OC@ H@H=@MN JA 4DIPPM AM@@GT RJPG? IJO BP<M<IO@@ OC<O OCDN
>JINDNO@I>T RJPG? <GR<TN =@ H<DIO<DI@?. )@O'N >JIND?@M <??DIB < I@R @G@H@IO OJ OC@
TUVEFOUT H@H=@M, OCDN ODH@ ?DM@>OGT:
TDIPPM.TUVEFOUT _= 4UVEFOU("/BODZ", (FOEFS.GFNBMF);
B@><PN@ OC@ I@R NOP?@IO C<N =@@I <??@? OJ OC@ <MM<T ?DM@>OGT, RDOCJPO BJDIB OCMJPBC OC@
BEE() H@H=@M API>ODJI, OC@ 4DIPPM J=E@>O DN IJR DI <I DI>JINDNO@IO NO<O@:
1 GFNBMF, 1 NBMF; UPUBM 3 TUVEFOUT
Encapsulation
62.1
"I><KNPG<ODJI DN < KMJBM<HHDIB >JI>@KO JA M@NOMD>ODIB <>>@NN OJ H@H=@MN OJ <QJD? KMJ=G@HN
NDHDG<M OJ OC@ JI@ <=JQ@.
AIJOC@M =@I@ADO JA @I><KNPG<ODJI DN OJ @GDHDI<O@ OC@ I@@? OJ FIJR OC@ DHKG@H@IO<ODJI ?@O<DGN
JA OTK@N. &I < N@IN@, @I><KNPG<ODJI <GGJRN KM@N@IODIB < OTK@ <N < =G<>F =JS OC<O DN PN@? JIGT
OCMJPBC DON DIO@MA<>@.
A??DODJI<GGT, KM@Q@IODIB PN@MN AMJH <>>@NNDIB OC@ H@H=@MN ?DM@>OGT <GGJRN >C<IBDIB OC@
H@H=@MN JA < >G<NN AM@@GT DI OC@ APOPM@. AN GJIB <N OC@ API>ODJIN OC<O ?@ADI@ OC@ DIO@MA<>@ JA <
>G<NN DN F@KO OC@ N<H@, DON DHKG@H@IO<ODJI ><I =@ >C<IB@? AM@@GT.
"I><KNPG<ODJI DN IJO AJM M@NOMD>ODIB <>>@NN OJ N@INDODQ@ ?<O< GDF@ < >M@?DO ><M? IPH=@M JM <
K<NNRJM?, <I? DO ><IIJO =@ PN@? AJM OC<O KPMKJN@. "I><KNPG<ODJI DN < ?@Q@GJKH@IO OJJG: &O
<GGJRN PNDIB <I? >J?DIB OTK@N @<NDGT <I? N<A@GT.
Protection attributes
62.2
-MJO@>ODJI <OOMD=PO@N GDHDO <>>@NN OJ H@H=@MN JA NOMP>ON, >G<NN@N, <I? HJ?PG@N. 1C@M@ <M@ ORJ
R<TN JA NK@>DATDIB KMJO@>ODJI <OOMD=PO@N:
t AO NOMP>O JM >G<NN G@Q@G OJ NK@>DAT OC@ KMJO@>ODJI JA @Q@MT NOMP>O JM >G<NN H@H=@M
DI?DQD?P<GGT.
t AO HJ?PG@ G@Q@G OJ NK@>DAT OC@ KMJO@>ODJI JA @Q@MT A@<OPM@ JA < HJ?PG@ DI?DQD?P<GGT: >G<NN,
NOMP>O, API>ODJI, @IPH, @O>.
-MJO@>ODJI <OOMD=PO@N ><I =@ NK@>DAD@? =T OC@ AJGGJRDIB F@TRJM?N. 1C@ ?@A<PGO <OOMD=PO@ DN
QVCMJD.
E@53BEG>3F;A@ 3@6 &DAF75F;A@ AFFD;4GF7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
372
t QVCMJD: 0K@>DAD@N <>>@NND=DGDOT =T <IT K<MO JA OC@ KMJBM<H RDOCJPO <IT M@NOMD>ODJI.
AI @S<HKG@ JA OCDN DN TUEPVU. *@M@GT DHKJMODIB TUE.TUEJP H<F@N TUEPVU <Q<DG<=G@ OJ
@Q@MT HJ?PG@ OC<O DHKJMO@? DO.
t QSJWBUF: 0K@>DAD@N M@NOMD>O@? <>>@NND=DGDOT.
QSJWBUF >G<NN H@H=@MN <I? HJ?PG@ H@H=@MN ><I JIGT =@ <>>@NN@? =T OC@ HJ?PG@
OC<O ?@ADI@N OC<O H@H=@M.
A??DODJI<GGT, QSJWBUF H@H=@M API>ODJIN ><IIJO =@ JQ@MMD??@I =T NP=>G<NN@N.
t QBDLBHF: 0K@>DAD@N K<>F<B@-G@Q@G <>>@NND=DGDOT.
A A@<OPM@ OC<O DN H<MF@? <N QBDLBHF ><I =@ <>>@NN@? =T <GG JA OC@ >J?@ OC<O DN < K<MO
JA OC@ N<H@ K<>F<B@. 1C@ QBDLBHF <OOMD=PO@ DIQJGQ@N JIGT OC@ DII@M-HJNO K<>F<B@.
#JM @S<HKG@, < QBDLBHF ?@ADIDODJI OC<O DN DIND?@ OC@ BOJNBM.WFSUFCSBUF.DBU HJ?PG@
><I =@ <>>@NN@? =T <IT JOC@M HJ?PG@ JA OC@ WFSUFCSBUF K<>F<B@.
0DHDG<M OJ OC@ QSJWBUF <OOMD=PO@, QBDLBHF H@H=@M API>ODJIN ><IIJO =@ JQ@MMD??@I =T
NP=>G<NN@N.
t QSPUFDUFE: 0K@>DAD@N <>>@NND=DGDOT =T ?@MDQ@? >G<NN@N.
1CDN <OOMD=PO@ @SO@I?N OC@ QSJWBUF <OOMD=PO@: A QSPUFDUFE H@H=@M ><I =@ <>>@NN@?
IJO JIGT =T OC@ HJ?PG@ OC<O ?@ADI@N DO, =PO <GNJ =T OC@ >G<NN@N OC<O DIC@MDO AMJH OC@
>G<NN OC<O ?@ADI@N OC<O QSPUFDUFE H@H=@M.
A??DODJI<GGT, OC@ FYQPSU <OOMD=PO@ NK@>DAD@N <>>@NND=DGDOT AMJH OC@ JPOND?@ JA OC@ KMJBM<H.
Definition
62.3
-MJO@>ODJI <OOMD=PO@N ><I =@ NK@>DAD@? DI OCM@@ R<TN.
4C@I RMDOO@I DI AMJIO JA < NDIBG@ ?@ADIDODJI, DO NK@>DAD@N OC@ KMJO@>ODJI <OOMD=PO@ JA OC<O
?@ADIDODJI JIGT. 1CDN DN NDHDG<M OJ OC@ '<Q< KMJBM<HHDIB G<IBP<B@:
QSJWBUF JOU GPP;
QSJWBUF WPJE CBS()
\
// ...
^
4C@I NK@>DAD@? =T < >JGJI, DO NK@>DAD@N OC@ KMJO@>ODJI <OOMD=PO@N JA <GG JA OC@ AJGGJRDIB
?@ADIDODJIN PIODG OC@ I@SO NK@>DAD><ODJI JA < KMJO@>ODJI <OOMD=PO@. 1CDN DN NDHDG<M OJ OC@ ++
KMJBM<HHDIB G<IBP<B@:
QSJWBUF:
// ...
// ... BMM PG UIF EFGJOJUJPOT IFSF BSF QSJWBUF ...
// ...
QSPUFDUFE:
// ...
// ... BMM PG UIF EFGJOJUJPOT IFSF BSF QSPUFDUFE ...
// ...
4C@I NK@>DAD@? AJM < =GJ>F, OC@ KMJO@>ODJI <OOMD=PO@ DN AJM <GG JA OC@ ?@ADIDODJIN OC<O <M@ DIND?@
OC<O =GJ>F:
QSJWBUF \
// ...
E@53BEG>3F;A@ 3@6 &DAF75F;A@ AFFD;4GF7E
CABKD;9:F M 2009-2012, :FFB://66;>;.AD9
373
// ... BMM PG UIF EFGJOJUJPOT IFSF BSF QSJWBUF ...
// ...
^
Module imports are private by default
62.4
A HJ?PG@ OC<O DN DHKJMO@? =T JNQPSU DN KMDQ<O@ OJ OC@ HJ?PG@ OC<O DHKJMON DO. &O RJPG? IJO =@
QDND=G@ OJ JOC@M HJ?PG@N OC<O DHKJMO DO DI?DM@>OGT. #JM @S<HKG@, DA < TDIPPM HJ?PG@ DHKJMON
TUE.TUEJP, HJ?PG@N OC<O DHKJMO TDIPPM ><IIJO <POJH<OD><GGT PN@ OC@ TUE.TUEJP HJ?PG@.
)@O'N <NNPH@ OC<O OC@ TDIPPM HJ?PG@ NO<MON =T OC@ AJGGJRDIB GDI@N:
NPEVMF TDIPPM.TDIPPM;
JNQPSU TUE.TUEJP; // JNQPSUFE GPS UIJT NPEVMF'T PXO VTF...
// ...
1C@ AJGGJRDIB KMJBM<H ><IIJO =@ >JHKDG@? =@><PN@ XSJUFMO DN IJO QDND=G@ OJ DO:
JNQPSU TDIPPM.TDIPPM;
WPJE NBJO()
\
XSJUFMO("IFMMP"); // j DPNQJMBUJPO &3303
^
TUE.TUEJP HPNO =@ DHKJMO@? =T OC<O HJ?PG@ <N R@GG:
JNQPSU TDIPPM.TDIPPM;
JNQPSU TUE.TUEJP;
WPJE NBJO()
\
XSJUFMO("IFMMP"); // OPX DPNQJMFT
^
0JH@ODH@N DO DN ?@NDM@? OC<O < HJ?PG@ KM@N@ION JOC@M HJ?PG@N DI?DM@>OGT. #JM @S<HKG@, DO
RJPG? H<F@ N@IN@ AJM < TDIPPM HJ?PG@ OJ <POJH<OD><GGT DHKJMO < TUVEFOU HJ?PG@ AJM DON
PN@MN. 1CDN DN <>CD@Q@? =T H<MFDIB OC@ JNQPSU <N QVCMJD:
NPEVMF TDIPPM.TDIPPM;
QVCMJD JNQPSU TDIPPM.TUVEFOU;
// ...
4DOC OC<O ?@ADIDODJI, HJ?PG@N O