8QLYHUVLWj GHJOL 6WXGL GL %RORJQD ‡ )DFROWj GL ,QJHJQHULD

2ELHWWLYL GHO FRUVR
,QWURGXUUH DOO¶XVR GHJOL HODERUDWRUL HOHWWURQLFL FRPH VWUXPHQWL SHU OD ULVROX]LRQH GL SUREOHPL

‡ )RUQLUH OD FRQRVFHQ]D GL EDVH VXOOD VWUXWWXUD H OH FDUDWWHULVWLFKH GL XWLOL]]R GHL FDOFRODWRUL HOHWWURQLFL ‡ 3UHVHQWDUH L PHWRGL SHU OD VROX]LRQH GL FODVVL GL SUREOHPL DOJRULWPL HG L FRQFHWWL EDVH GHL OLQJXDJJL GL SURJUDPPD]LRQH WLSL GL GDWR H VWUXWWXUH GL FRQWUROOR ‡ 0HWWHUH D GLVSRVL]LRQH XQ OLQJXDJJLR GL SURJUDPPD]LRQH LO & SHU OD WUDGX]LRQH GHJOL DOJRULWPL GL ULVROX]LRQH LQ SURJUDPPL SHU XQ HODERUDWRUH ‡ 'HVFULYHUH DOFXQL PHWRGL GL FDOFROR QXPHULFR SHU OD VROX]LRQH GL SUREOHPL WLSLFL GHOO¶LQJHJQHULD H IRUQLUH JOL DOJRULWPL & FKH OL UHDOL]]DQR

&2562 ', )21'$0(17, ', ,1)250$7,&$ ,QJ 0HFFDQLFD $2 $QQR $FFDGHPLFR 

352) $11$ &,$032/,1,

'LSDUWLPHQWR GL (OHWWURQLFD ,QIRUPDWLFD H 6LVWHPLVWLFD

FONDAMENTI DI INFORMATICA (ING. MECCANICA)

INTRODUZIONE 1

FONDAMENTI DI INFORMATICA (ING. MECCANICA)

INTRODUZIONE 2

3URJUDPPD GHO FRUVR
‡ $UFKLWHWWXUD GHL VLVWHPL GL HODERUD]LRQH 6WUXWWXUD JHQHUDOH GL XQ FDOFRODWRUH HOHWWURQLFR /D PDFFKLQD GL 9RQ 1HXPDQQ /D UDSSUHVHQWD]LRQH GHOOH LQIRUPD]LRQL QHL VLVWHPL GL HODERUD]LRQH 5DSSUHVHQWD]LRQH LQWHUQD GHL QXPHUL (UURUL &RGLFL ‡ 6RIWZDUH GL EDVH SHU VLVWHPL GL HODERUD]LRQH ,O VLVWHPD RSHUDWLYR , VLVWHPL RSHUDWLYL 06'26 H :LQGRZV¶ 6WUXPHQWL HG DPELHQWL GL SURJUDPPD]LRQH HGLWRU GHEXJJHU FRPSLODWRUL HG LQWHUSUHWL )DVL GL VYLOXSSR GL XQ SURJUDPPD ‡ (OHPHQWL GL SURJUDPPD]LRQH 0HWRGL SHU O¶DQDOLVL GL XQ SUREOHPD $OJRULWPL 0HWRGRORJLD GL VYLOXSSR WRSGRZQ H ERWWRPXS 0HWRGRORJLH GL SURJUDPPD]LRQH VWUXWWXUDWD H PRGXODUH ,WHUD]LRQH H ULFRUVLRQH &RPSOHVVLWDC GHJOL DOJRULWPL ‡ /LQJXDJJL GL SURJUDPPD]LRQH , OLQJXDJJL GL SURJUDPPD]LRQH H FHQQL DOOD ORUR HYROX]LRQH 6LQWDVVL H VHPDQWLFD

‡ ,O OLQJXDJJLR & ,O OLQJXDJJLR & $OIDEHWR H VLQWDVVL GHO & 7LSL GL GDWR VFDODUL H VWUXWWXUDWL (VSUHVVLRQL 'LFKLDUD]LRQH GL FRVWDQWL YDULDELOL H ORUR WLSR ,VWUX]LRQL GL DVVHJQDPHQWR H GL LQJUHVVRXVFLWD ,VWUX]LRQL FRPSRVWH FRQGL]LRQDOL H FLFOL /¶LVWUX]LRQH GL VDOWR LQFRQGL]LRQDWR )XQ]LRQL H SURFHGXUH 7HFQLFKH GL SDVVDJJLR GHL SDUDPHWUL 5HJROH GL YLVLELOLWDC H WHPSR GL YLWD ,O PRGHOOR ³UXQWLPH´ GHO & /LEUHULH VWDQGDUG *HVWLRQH GHL ILOH 0RGXODULWDC LQ & ‡ 6WUXWWXUH GDWL 7HFQLFKH SHU OD UDSSUHVHQWD]LRQH H OD JHVWLRQH GL VWUXWWXUH GDWL OLVWH DOEHUL WDEHOOH  5DSSUHVHQW]LRQH FROOHJDWD GHO WLSR GL GDWR DVWUDWWR ³OLVWD´ HG ³DOEHUR ELQDULR´

FONDAMENTI DI INFORMATICA (ING. MECCANICA)

INTRODUZIONE 3

FONDAMENTI DI INFORMATICA (ING. MECCANICA)

INTRODUZIONE 4

‡ &DOFROR 1XPHULFR 0HWRGL GLUHWWL HG LWHUDWLYL SUHFLVLRQH &DOFROR GHJOL ]HUL GL XQD IXQ]LRQH PHWRGR GHOOD ELVH]LRQH GHOOH FRUGH GHOOH VHFDQWL H GHOOH WDQJHQWL 0HWRGL GLUHWWL SHU OD ULVROX]LRQH GL VLVWHPL GL HTXD]LRQL OLQHDUL PHWRGR GL *DXVV *DXVV -RUGDQ GHFRPSRVL]LRQH /8 FDOFROR GHOOD PDWULFH LQYHUVD 0HWRGL LWHUDWLYL SHU OD ULVROX]LRQH GL VLVWHPL GL HTXD]LRQL OLQHDUL PHWRGR GL -DFREL H *DXVV6HLGHO ,QWHUSROD]LRQH GL )XQ]LRQL PHGLDQWH SROLQRPL PHWRGL GL /DJUDQJH H GL 1HZWRQ ,QWHJUD]LRQH GL )XQ]LRQL PHWRGR GHL UHWWDQJROL GL %H]RXW H GL 6LPSVRQ 0HWRGL SHU OD 6ROX]LRQH GL (TXD]LRQL 'LIIHUHQ]LDOL PHWRGL GL (XOHUR GL 5XQJH .XWWD GL (XOHUR PRGLILFDWR 6ROX]LRQH GL 6LVWHPL GL (TXD]LRQL GLIIHUHQ]LDOL ‡ ,O OLQJXDJJLR )RUWUDQ $QDOLVL GHOOH FDUDWWHULVWLFKH GHO OLQJXDJJLR )RUWUDQ DWWUDYHUVR FRQIURQWL FRQ LO OLQJXDJJLR & $QDOLVL GL DOFXQL DOJRULWPL )RUWUDQ

3DJLQD :::
$OO¶LQGLUL]]R LQWHUQHW
KWWSZZZOLDGHLVXQLERLW&RXUVHV)RQG0HFF

VRQR GLVSRQLELOL LO SURJUDPPD PDWHULDOH GLGDWWLFR LQWHJUDWLYR LO VRIWZDUH JUDWXLWR SHU OR VYLOXSSR GHL SURJUDPPL L WHVWL GL HVDPH DOFXQL HVHUFL]L VYROWL HG DOWUH LQIRUPD]LRQL XWLOL SHU JOL VWXGHQWL GHO FRUVR œ *OL VWXGHQWL SRVVRQR FRQVXOWDUH OD SDJLQD GDO ODERUDWRULR GHO &HQWUR GL &DOFROR GRYH VL VYROJRQR OH HVHUFLWD]LRQL RSSXUH GLUHWWDPHQWH GD FDVD VH FROOHJDWL DG ,QWHUQHW 

FONDAMENTI DI INFORMATICA (ING. MECCANICA)

INTRODUZIONE 5

FONDAMENTI DI INFORMATICA (ING. MECCANICA)

INTRODUZIONE 6

7HVWL FRQVLJOLDWL
œ 'LVSHQVH GHO &RUVR IRWRFRSLH GHL OXFLGL PRVWUDWL D OH]LRQH œ &HUL ' 0DQGULROL / 6EDWWHOOD ,QIRUPDWLFD LVWLWX]LRQL /LQJXDJJLR GL 5LIHULPHQWR $16, &  0F*UDZ+LOO 
/LQJXDJJLR &

2UDULR GHOOH /H]LRQL 0HUFROHGL  *LRYHGL  9HQHUGL 

DXOD  1% DXOD  DXOD 

2UDULR GHOOH (VHUFLWD]LRQL LQ /DERUDWRULR VDOD WHUPLQDOL GHO FHQWUR GL FDOFROR  /XQHGL 0DUWHGu (VDPH 3URYD VFULWWD H SURYD RUDOH  

œ $.HOOH\ ,3RKO ³& ± 'LGDWWLFD H 3URJUDPPD]LRQH´ $GGLVRQ :HVOH\  œ $%HOOLQL $ *XLGL ³*XLGD DO /LQJXDJJLR &´ 0F *UDZ +LOO  œ .HUQLJKDQ 5LWFKLH ,O /LQJXDJJLR & -DFNVRQ 
&DOFROR 1XPHULFR

œ * 0RQHJDWR )RQGDPHQWL /HYURWWR %HOOD 

GL

&DOFROR

1XPHULFR

$OO¶RUDOH HC REEOLJDWRULR SUHVHQWDUH XQD HVHUFLWD]LRQH VXOOH VWUXWWXUH GDWL VYROWD DXWRQRPDPHQWH
2UDULR ULFHYLPHQWR *LRYHGLC  FR '(,6 7HO 

œ : 3UHVV 6 7HXNROVN\ : 9HWWHUOLQJ % )ODQQHU\ 1XPHULFDO 5HFLSHV LQ & 6HFRQG (GLWLRQ &DPEULGJH 8QLYHUVLW\ 3UHVV 
(VHUFL]L

œ $ &LDPSROLQL ( /DPPD (VHUFL]L GL SURJUDPPD]LRQH /LQJXDJJLR & ± 3URJHWWR /HRQDUGR (VFXODSLR 
FONDAMENTI DI INFORMATICA (ING. MECCANICA)
INTRODUZIONE 7

FONDAMENTI DI INFORMATICA (ING. MECCANICA)

INTRODUZIONE 8

,QIRUPDWLFD
(VLLVWRQR YDULLH GHILLQLL]LLRQLL (V VWRQR YDU H GHI Q ] RQ  ‡ ³6FLHQ]D GHL FDOFRODWRUL HOHWWURQLFL &RPSXWHU 6FLHQFH ´ ‡ ³6FLHQ]D GHOO¶LQIRUPD]LRQH´ 'HILLQLL]LLRQH SURSRVWD 'HI Q ] RQH SURSRVWD ³6FLHQ]D GHOOD UDSSUHVHQWD]LRQH H GHOO¶HODERUD]LRQH GHOO¶LQIRUPD]LRQH´ &DOOFROODWRUH (OOHWWURQLLFR &D FR DWRUH ( HWWURQ FR HC XQR VWUXPHQWR SHU OD UDSSUHVHQWD]LRQH H O¶HODERUD]LRQH GHOOH LQIRUPD]LRQL )DQQR SDUWH GHOOOO¶,QIRUPDWLLFD )DQQR SDUWH GH ¶,QIRUPDW FD ‡ ‡ ‡ ‡ ‡ ‡ PHWRGL SHU OD UDSSUHVHQWD]LRQH GHOOH LQIRUPD]LRQL PHWRGL SHU OD UDSSUHVHQWD]LRQH GL SURFHGLPHQWL ULVROXWLYL OLQJXDJJL GL SURJUDPPD]LRQH DUFKLWHWWXUD GHL FDOFRODWRUL VLVWHPL RSHUDWLYL HWF

5LVROX]LRQH GL 3UREOHPL PHGLDQWH O¶XVR GHO &DOFRODWRUH (OHWWURQLFR
&DOOFROODWRUH HOOHWWURQLLFR &D FR DWRUH H HWWURQ FR ‡ H
XQR VWUXPHQWR LQ JUDGR GL HVHJXLUH LQVLHPL GL D]LRQL HOHPHQWDUL ‡ OH D]LRQL YHQJRQR HVHJXLWH VX RJJHWWL GDWL SHU SURGXUUH DOWUL RJJHWWL ULVXOWDWL  ‡ /
HVHFX]LRQH GL D]LRQL YLHQH ULFKLHVWD DOO
HODERUDWRUH DWWUDYHUVR RSSRUWXQH GLUHWWLYH GHWWH LVWUX]LRQL 3URJUDPPD]LLRQH 3URJUDPPD] RQH (C O
DWWLYLWDC FRQ FXL VL SUHGLVSRQH O
HODERUDWRUH DG HVHJXLUH XQ SDUWLFRODUH LQVLHPH GL D]LRQL VX SDUWLFRODUL GDWL DOOR VFRSR GL ULVROYHUH XQ FHUWR SUREOHPD

DATI

Calcolatore Elettronico

RISULTATI

FONDAMENTI DI INFORMATICA

INTRODUZIONE

8

FONDAMENTI DI INFORMATICA

INTRODUZIONE

9

, 3UREOHPL GD ULVROYHUH
, SUREOOHPLL FKH VLLDPR LLQWWHUHVVDWWLL D ULLVROOYHUH FRQ , SURE HP FKH V DPR Q HUHVVD D U VR YHUH FRQ OO

HOODERUDWWRUH VRQR GL QDWXUD PROWR YDULD H DERUD RUH (VHPSL  'DWL GXH QXPHUL WURYDUH LO PDJJLRUH  'DWR XQ HOHQFR GL QRPL H UHODWLYL QXPHUL GL WHOHIRQR WURYDUH LO QXPHUR GL WHOHIRQR GL XQD GHWHUPLQDWD SHUVRQD  3UREOHPD GHO /XSR GHOOD &DSUD H GHO &DYROR  'DWL D H E ULVROYHUH O
HTXD]LRQH D[E   6WDELOLUH VH XQD SDUROD YLHQH DOIDEHWLFDPHQWH SULPD GL XQ
DOWUD  6RPPD GL GXH QXPHUL LQWHUL  6FULYHUH WXWWL JOL Q SHU FXL O
HTXD]LRQH ;Q  <Q = Q KD VROX]LRQL LQWHUH SUREOHPD GL )HUPDW  2UGLQDUH XQD OLVWD GL HOHPHQWL  &DOFRODUH LO PDVVLPR FRPXQ GLYLVRUH IUD GXH QXPHUL GDWL  &DOFRODUH LO PDVVLPR LQ XQ LQVLHPH + /D GHVFUL]LRQH GHO SUREOHPD QRQ IRUQLVFH LQ JHQHUH XQ PHWRGR SHU FDOFRODUH LO ULVXOWDWR

5LVROX]LRQH GL 3UREOHPL
œ $IILQFKHC XQ SUREOHPD VLD ULVROYLELOH LQ JHQHUDOH HC œ QHFHVVDULR FKH OD VXD GHILQL]LRQH VLD FKLDUD H FRPSOHWD œ 1RQ WXWWL L SUREOHPL VRQR ULVROYLELOL DWWUDYHUVR O
XVR GHO œ FDOFRODWRUH ,Q SDUWLFRODUH HVLVWRQR FODVVL GL SUREOHPL SHU OH TXDOL OD VROX]LRQH DXWRPDWLFD QRQ HC SURSRQLELOH $G HVHPSLR œ VH LO SUREOHPD SUHVHQWD LQILQLWH VROX]LRQL œ SHU DOFXQL GHL SUREOHPL QRQ HC VWDWR WURYDWR XQ PHWRGR ULVROXWLYR œ SHU DOFXQL SUREOHPL H
VWDWR GLPRVWUDWR FKH QRQ HVLVWH XQ PHWRGR ULVROXWLYR DXWRPDWL]]DELOH œ 1RL FL FRQFHQWUHUHPR VXL SUREOHPL FKH œ UDJLRQHYROPHQWH DPPHWWRQR XQ PHWRGR ULVROXWLYR IXQ]LRQL FDOFRODELOL  œ 8QR GHJOL RELHWWLYL GHO &RUVR HC SUHVHQWDUH OH œ WHFQRORJLH H OH PHWRGRORJLH GL SURJUDPPD]LRQH 7HFQRORJLH VWUXPHQWL SHU OD UHDOL]]D]LRQH GL SURJUDPPL 0HWRGRORJLH PHWRGL SHU O
XWLOL]]R LQ PRGR FRUUHWWR HG HIILFDFH GHOOH WHFQRORJLH GL SURJUDPPD]LRQH

FONDAMENTI DI INFORMATICA

INTRODUZIONE

10

FONDAMENTI DI INFORMATICA

INTRODUZIONE

11

5LVROX]LRQH GL XQ SUREOHPD
&RQ TXHVWR WHUPLQH VL LQGLFD LO SURFHVVR FKH ‡ GDWR XQ SUREOHPD H ‡ LQGLYLGXDWR XQ PHWRGR ULVROXWLYR WUDVIRUPD L GDWL LQL]LDOL QHL FRUULVSRQGHQWL ULVXOWDWL ILQDOL $IILQFKHC OD ULVROX]LRQH GL XQ SUREOHPD SRVVD HVVHUH UHDOL]]DWD DWWUDYHUVR O¶XVR GHO FDOFRODWRUH WDOH SURFHVVR GHYH SRWHU HVVHUH GHILQLWR FRPH VHTXHQ]D GL D]LRQL HOHPHQWDUL

$/*25,702
/¶DOJRULWPR HC O
LQVLHPH RUGLQDWR GHOOH D]LRQL FKH ULVROYH XQ GDWR SUREOHPD ‡ O¶DOJRULWPR GHVFULYH LO PHWRGR ULVROXWLYR DWWUDYHUVR XQ LQVLHPH RUGLQDWR GL D]LRQL ‡ O
HVHFX]LRQH GHOOH D]LRQL VHFRQGR O
RUGLQH VSHFLILFDWR GDOO
DOJRULWPR D SDUWLUH GDL GDWL GL LQJUHVVR FRQVHQWH GL RWWHQHUH L ULVXOWDWL UHODWLYL DOOD VROX]LRQH GHO SUREOHPD
Metodo Risolutivo (algoritmo)

DATI

Esecutore

RISULTATI

+ 6L ID ULIHULPHQWR DG XQ ³HVHFXWRUH´ FRQ TXHVWR WHUPLQH VL LQWHQGH XQD PDFFKLQD DVWUDWWD QRQ QHFHVVDULDPHQWH XQ FDOFRODWRUH LQ JUDGR GL HVHJXLUH OH D]LRQL VSHFLILFDWH GDO PHWRGR ULVROXWLYR

FONDAMENTI DI INFORMATICA

INTRODUZIONE

12

FONDAMENTI DI INFORMATICA

INTRODUZIONE

13

3URSULHWDC IRQGDPHQWDOL GHOO¶$OJRULWPR 
‡ (VHJXLELOLWDC RJQL ³LVWUX]LRQH´ GHYH HVVHUH HVHJXLELOH GD SDUWH GHOO¶HVHFXWRUH GHOO¶DOJRULWPR ‡ 1RQ $PELJXLWDC RJQL D]LRQH GHYH HVVHUH XQLYRFDPHQWH LQWHUSUHWDELOH GDOO
HVHFXWRUH ‡ )LQLWH]]D LO QXPHUR WRWDOH GL D]LRQL GD HVHJXLUH SHU RJQL LQVLHPH GL GDWL GL LQJUHVVR H
ILQLWR  /¶DOOJRULLWPR GHYH  /¶D JRU WPR GHYH ‡ HVVHUH DSSOLFDELOH D TXDOVLDVL LQVLHPH GHL GDWL GL LQJUHVVR DSSDUWHQHQWL DO GRPLQLR GL GHILQL]LRQH GHOO¶DOJRULWPR ‡ HVVHUH FRVWLWXLWR GD RSHUD]LRQL DSSDUWHQHQWL DG XQ GHWHUPLQDWR LQVLHPH GL RSHUD]LRQL IRQGDPHQWDOL VLVWHPD IRUPDOH  ‡ OH UHJROH FKH FRVWLWXLVFRQR O¶DOJRULWPR QRQ GHYRQR HVVHUH DPELJXH FLRHC GHYRQR HVVHUH LQWHUSUHWDELOL LQ PRGR XQLYRFR TXDOXQTXH VLD OD SHUVRQD R OD ³PDFFKLQD´ FKH OH OHJJH

$OJRULWPR
‡ $OWUH SURSULHWDC GHVLGHUDELOL ‡ JHQHUDOLWDC ‡ WHUPLQD]LRQH ‡ GHWHUPLQLVPR ‡ HIILFLHQ]D ‡ 8Q DOJRULWPR SXRC HVVHUH YLVWR FRPH LO SURFHGLPHQWR GL FDOFROR GL XQD IXQ]LRQH FKH PDSSD XQR R SLXC YDORUL GL XQ GRPLQLR $ LQSXW GHOO¶DOJRULWPR LQ XQ YDORUH GHO FRGRPLQLR % RXWSXW GHOO¶DOJRULWPR 

Dati (input) x

Algoritmo

Risultati (output) f(x)

FONDAMENTI DI INFORMATICA

INTRODUZIONE

14

FONDAMENTI DI INFORMATICA

INTRODUZIONE

15

$OJRULWPR XQ SR¶ GL VWRULD
‡ ,O WHUPLQH ³DOJRULWPR´ GHULYD GDO QRPH GHO PDWHPDWLFR DUDER $O.KRZDU]LPL GHO ,; VHFROR GF FKH SHU SULPR VXJJHULC XQ PHWRGR SHU VRPPDUH GXH QXPHUL UDSSUHVHQWDWL QHO VLVWHPD QXPHULFR +LQGX H FRQWULEXLC DOOD IRQGD]LRQH GHOO¶DOJHEUD ‡ 1HO PHGLRHYR LO WHUPLQH DOJRULVPXV VHUYLC DG LQGLFDUH LO FRPSOHVVR GL RSHUD]LRQL QHO FDOFROR QXPHULFR FRQ QXPHUL DUDEL ‡ $WWXDOPHQWH FRQ LO WHPLQH DOJRULWPR VL LQGLFD OD VHTXHQ]D ILQLWD GL SDVVL HIIHWWXDELOL GD XQD PDFFKLQD SHU ULVROYHUH XQD FODVVH GL SUREOHPL LQ WHPSR ILQLWR

$OJRULWPL H 3URJUDPPL
‡ 2JQL HODERUDWRUH H
XQD PDFFKLQD LQ JUDGR GL HVHJXLUH D]LRQL HOHPHQWDUL VX RJJHWWL GHWWL GDWL ‡ /
HVHFX]LRQH GHOOH D]LRQL H
ULFKLHVWD DOO
HODERUDWRUH WUDPLWH FRPDQGL HOHPHQWDUL FKLDPDWL LVWUX]LRQL ‡ /H LVWUX]LRQL VRQR HVSUHVVH DWWUDYHUVR XQ RSSRUWXQR IRUPDOLVPR LO OLQJXDJJLR GL SURJUDPPD]LRQH (V SUM A,B read(x) scanf("%d",&Y) ‡ /D IRUPXOD]LRQH WHVWXDOH GL XQ DOJRULWPR LQ XQ OLQJXDJJLR FRPSUHQVLELOH DG XQ HODERUDWRUH HC GHWWD SURJUDPPD main() {int A, B; printf(“Immettere i dati (A,B): “); scanf(“%d %d”, &A, &B); printf(“Risultato: A+B=%d\n”, A+B); }

FONDAMENTI DI INFORMATICA

INTRODUZIONE

16

FONDAMENTI DI INFORMATICA

INTRODUZIONE

17

$OJRULWPL H 3URJUDPPL
'DWR XQ SUREOHPD OD VXD VROX]LRQH SXRC HVVHUH RWWHQXWD PHGLDQWH O¶XVR GHO FDOFRODWRUH FRPSLHQGR L VHJXHQWL SDVVL ‡ LQGLYLGXD]LRQH GL XQ SURFHGLPHQWR ULVROXWLYR ‡ VFRPSRVL]LRQH GHO SURFHGLPHQWR LQ LQVLHPH RUGLQDWR GL D]LRQL ! $/*25,702 ‡ UDSSUHVHQWD]LRQH GHL GDWL H GHOO
DOJRULWPR DWWUDYHUVR XQ IRUPDOLVPR FRPSUHQVLELOH SHU O¶HODERUDWRUH OLQJXDJJLR GL SURJUDPPD]LRQH ! 352*5$00$
problema algoritmo programma

$OJRULWPL $OFXQL (VHPSL
,OO 3UREOOHPD GHOOOOD FDSUD GHOO OOXSR H GHOO FDYROOR , 3URE HPD GH D FDSUD GH XSR H GH FDYR R ‡ ‡ ‡ ‡ ‡ ‡ ‡ 3RUWD OD FDSUD VXOO
DOWUD VSRQGD 7RUQD LQGLHWUR 3RUWD LO FDYROR VXOO
DOWUD VSRQGD 3RUWD LQGLHWUR OD FDSUD 3RUWD LO OXSR VXOO
DOWUD VSRQGD 7RUQD LQGLHWUR 3RUWD OD FDSUD VXOO
DOWUD VSRQGD

6ROOX]LLRQH GHOOOO

HTXD]LLRQH D[ E  6R X] RQH GH HTXD] RQH D[ E  ‡ ‡ ‡ ‡ OHJJL L YDORUL GL D H E FDOFROD E GLYLGL TXHOOR FKH KDL RWWHQXWR SHU D H FKLDPD [ LO ULVXOWDWR VWDPSD [

metodo risolutivo

linguaggio di programmazione

,O SURFHGLPHQWR H
FRPSOHVVR H QRQ H
XQLYRFR

FONDAMENTI DI INFORMATICA

INTRODUZIONE

18

FONDAMENTI DI INFORMATICA

INTRODUZIONE

19

6WDELLOOLLUH VH XQD SDUROOD 3 YLLHQH DOOIDEHWLLFDPHQWH SULLPD GLL 6WDE UH VH XQD SDUR D 3 Y HQH D IDEHW FDPHQWH SU PD G XQD SDUROOD 4 XQD SDUR D 4 ‡ ‡ OHJJL 34 ULSHWL TXDQWR VHJXH VH SULPD OHWWHUD GL 3  SULPD OHWWHUD 4 DOORUD VFULYL YHUR DOWULPHQWL VH SULPD OHWWHUD 3 ! 4 DOORUD VFULYL IDOVR DOWULPHQWL OH OHWWHUH VRQR WRJOL GD 3 H 4 OD SULPD OHWWHUD fino a quando hai trovato le prime lettere diverse.

$OJRULWPL HTXLYDOHQWL
'XH DOJRULWPL VL GLFRQR HTXLYDOHQWL TXDQGR ‡ KDQQR OR VWHVVR GRPLQLR GL LQJUHVVR ‡ KDQQR OR VWHVVR GRPLQLR GL XVFLWD ‡ LQ FRUULVSRQGHQ]D GHJOL VWHVVL YDORUL QHO GRPLQLR GL LQJUHVVR SURGXFRQR JOL VWHVVL YDORUL QHO GRPLQLR GL XVFLWD 'XH DOJRULWPL HTXLYDOHQWL ‡ IRUQLVFRQR OR VWHVVR ULVXOWDWR ‡ SRVVRQR DYHUH GLIIHUHQWH HIILFLHQ]D ‡ SRVVRQR HVVHUH SURIRQGDPHQWH GLYHUVL

&DOOFROOR GHOO PDVVLLPR GLL XQ LLQVLLHPH &D FR R GH PDVV PR G XQ QV HPH ‡ ‡ ‡ 6FHJOL XQ HOHPHQWR FRPH PDVVLPR SURYYLVRULR PD[ 3HU RJQL HOHPHQWR L GHOO
LQVLHPH VH L!PD[ HOHJJL L FRPH QXRYR PDVVLPR SURYYLVRULR PD[ ,O ULVXOWDWR H
PD[

127$ VL XWLOL]]DQR YDULDELOL FLRH
QRPL VLPEROLFL XVDWL QHOO¶DOJRULWPR SHU GHQRWDUH GDWL

FONDAMENTI DI INFORMATICA

INTRODUZIONE

20

FONDAMENTI DI INFORMATICA

INTRODUZIONE

21

$OJRULWPL (TXLYDOHQWL (VHPSLR
&DOOFROOR GHOO PDVVLLPR FRPXQ GLLYLLVRUH IUD P HG Q &D FR R GH PDVV PR FRPXQ G Y VRUH IUD P HG Q œ $OJRULWPR D ‡ &DOFROD O
LQVLHPH , GHL GLYLVRUL GL P ‡ &DOFROD O
LQVLHPH - GHL GLYLVRUL GL Q ‡ &DOFROD O
LQVLHPH . GHL GLYLVRUL FRPXQL . ,¬‡ &DOFROD LO PDVVLPR LQ . TXHVWR H
LO ULVXOWDWR ‡ $OJRULWPR E VL EDVD VXO PHWRGR GL (XFOLGH PFG PQ PFG PQ PFG PQ P RSSXUH Q PFG PQ Q PFG P QP VH P Q VH P!Q VH PQ

5DSSUHVHQWD]LRQH GL $OJRULWPL 'LDJUDPPL GL IOXVVR
(
XQ IRUPDOLVPR FKH JUDILFDPHQWH JOL DOJRULWPL FRQVHQWH GL UDSSUHVHQWDUH

‡ XQ GLDJUDPPD GL IOXVVR GHVFULYH OH D]LRQL GD HVHJXLUH HG LO ORUR RUGLQH GL HVHFX]LRQH ‡ RJQL D]LRQH HOHPHQWDUH FRUULVSRQGH DG XQ VLPEROR JUDILFR EORFFR GLYHUVR VRQR FRQYHQ]LRQL QRQ XQLYHUVDOL  ‡ RJQL EORFFR KD XQ UDPR LQ LQJUHVVR HG XQR R SLXC UDPL LQ XVFLWD FROOHJDQGR WUD ORUR L YDUL EORFFKL DWWUDYHUVR L UDPL VL RWWLHQH XQ GLDJUDPPD GL IOXVVR ‡ XQ GLDJUDPPD GL IOXVVR DSSDUH TXLQGL FRPH XQ LQVLHPH GL EORFFKL GL IRUPH GLYHUVH FKH FRQWHQJRQR OH LVWUX]LRQL GD HVHJXLUH FROOHJDWL IUD ORUR GD OLQHH RULHQWDWH FKH VSHFLILFDQR OD VHTXHQ]D LQ FXL L EORFFKL GHYRQR HVVHUH HVHJXLWL IOXVVR GHO FRQWUROOR GL HVHFX]LRQH 

$OJRULWPR ‡ )LQFKH
P HC GLYHUVR GD Q HVHJXL OH VHJXHQWL D]LRQL VH P!Q VRVWLWXLVFL D P LO YDORUH PQ DOWULPHQWL VRVWLWXLVFL D Q LO YDORUH QP ‡ ,O PDVVLPR FRPXQ GLYLVRUH H
Q *OL DOJRULWPL D H E VRQR HTXLYDOHQWL

FONDAMENTI DI INFORMATICA

INTRODUZIONE

22

FONDAMENTI DI INFORMATICA

INTRODUZIONE

23

'LDJUDPPL GL )OXVVR
start

'LDJUDPPL GL )OXVVR
œ /
LQVLHPH GHL GDWL GL LQJUHVVR H GHL ULVXOWDWL YHQJRQR œ UDSSUHVHQWDWL DWWUDYHUVR GHL QRPL VLPEROLFL GHWWL YDULDELOL DG HVHPSLR $ œ 3XRC HVVHUH LQROWUH QHFHVVDULR LQWURGXUUH GHOOH YDULDELOL œ WHPSRUDQHH DG HVHPSLR L  QHFHVVDULH DOOD ULVROX]LRQH GHO SUREOHPD WDOL YDULDELOL YHQJRQR DQFK
HVVH UDSSUHVHQWDWH GD QRPL VLPEROLFL

A<- 1 i<-1 No A<- A+i i<-i+1 stop i>10 Si A

œ I diagrammi di flusso hanno notevoli limiti per œ la soluzione di problemi di una certa complessita‘.

FONDAMENTI DI INFORMATICA

INTRODUZIONE

24

FONDAMENTI DI INFORMATICA

INTRODUZIONE

25

'LDJUDPPL GL )OXVVR
9DOORULL 9D RU  1XPHULFL LQWHUL H UHDOL /RJLFL 9HUR H )DOVR $OIDQXPHULFL VWULQJKH  $$$$  &&RORPER *UDQGH]]H *UDQGH]]H œ &RVWDQWL 4XDQWLWDC QRWH D SULRUL LO FXL YDORUH QRQ FDPELD œ GXUDQWH O
HVHFX]LRQH œ 9DULDELOL 5DSSUHVHQWDWH GD XQ QRPH VLPEROLFR FXL HC œ DVVHJQDWR XQ YDORUH FKH SXRC FDPELDUH GXUDQWH O
HVHFX]LRQH GHOO
DOJRULWPR

(VSUHVVLLRQLL (VSUHVV RQ  œ 6HTXHQ]H GL YDULDELOL H FRVWDQWL FRPELQDWH IUD ORUR œ PHGLDQWH RSHUDWRUL DG HV RSHUDWRUL DULWPHWLFL      DG HVHPSLR V  U  $  œ 1HOOD YDOXWD]LRQH GL XQD HVSUHVVLRQH VL VRVWLWXLVFH DG RJQL œ YDULDELOH LO VXR YDORUH DWWXDOH H VL HVHJXRQR OH RSHUD]LRQL VHFRQGR XQ RUGLQH SUHVWDELOLWR GD UHJROH GL SUHFHGHQ]D SRVVRQR FRPSDULUH SDUHQWHVL  œ $ WXWWH OH YDULDELOL FKH FRPSDLRQR QHOO
HVSUHVVLRQH GHYH œ HVVHUH VWDWR DVVRFLDWR XQ YDORUH SULPD GHOOD YDOXWD]LRQH GHOO
HVSUHVVLRQH œ (VSUHVVLRQL UHOD]LRQDOL H ORJLFKH GDQQR FRPH ULVXOWDWR YHUR œ R IDOVR !   ˜, ˆ, ž

FONDAMENTI DI INFORMATICA

INTRODUZIONE

26

FONDAMENTI DI INFORMATICA

INTRODUZIONE

27

,VWUX]LRQL EORFFKL IRQGDPHQWDOL
,QLL]LLR H ILLQH HVHFX]LLRQH VWDUW H VWRS ,Q ] R H I QH HVHFX] RQH VWDUW H VWRS œ ,QL]LR HC LO EORFFR GD FXL GHYH LQL]LDUH O
HVHFX]LRQH XQR œ VROR  œ ,O EORFFR ILQH ID WHUPLQDUH O
HVHFX]LRQH GHOO
DOJRULWPR œ DOPHQR XQR 

,QJUHVVR OHWWXUD UHDG LQSXW
(VHFX]LRQH GHOO
LVWUX]LRQH 6L ULFHYRQR GDOO
XQLWDC GL LQJUHVVR SHU HVHPSLR OD WDVWLHUD WDQWL YDORUL TXDQWH VRQR OH YDULDELOL VSHFLILFDWH DOO¶LQWHUQR GHO EORFFR H VL DVVHJQDQR QHOOR VWHVVR RUGLQH DOOH YDULDELOL ‡ $ % & VRQR QRPL GL YDULDELOL

INIZIO

A,B,C

FINE

+ ³/HJJL L WUH YDORUL GDWL LQ LQJUHVVR HG DVVHJQDOL ULVSHWWLYDPHQWH DOOH YDULDELOL $ % H &´

FONDAMENTI DI INFORMATICA

INTRODUZIONE

28

FONDAMENTI DI INFORMATICA

INTRODUZIONE

29

8VFLWD VWDPSD W\SH SULQW RXWSXW
(VHFX]LLRQH (VHFX] RQH 6L FDOFRODQR L YDORUL GHOOH HVSUHVVLRQL H VL WUDVPHWWRQR DOO
XQLWD
GL XVFLWD DG HVHPSLR LO YLGHR  (VHFX]LLRQH (VHFX] RQH

$VVHJQDPHQWR
6L FDOFROD LO YDORUH GHOO¶HVSUHVVLRQH D GHVWUD GHO VLPEROR ³ ³ H OR VL DVVHJQD DOOD YDULDELOH LQGLFDWD D VLQLVWUD GHO VLPEROR ³ ³ FRQ HYHQWXDOH SHUGLWD GHO YDORUH SUHFHGHQWH GL 9

X,Y,Z
V:=E

‡ ; < = VRQR HVSUHVVLRQL + ³&DOFROD L YDORUL GHOOH HVSUHVVLRQL ; < H = H WUDVPHWWLOL LQ XVFLWD´ 1% , YDORUL GL ; < = QRQ YHQJRQR DOWHUDWL GDOO¶HVHFX]LRQH GHO EORFFR

‡ 9 HC LO QRPH GL XQD YDULDELOH ( HC XQD HVSUHVVLRQH + ³&DOFROD LO YDORUH GHOO¶HVSUHVVLRQH ( H DVVHJQDOR DOOD YDULDELOH 9´

FONDAMENTI DI INFORMATICA

INTRODUZIONE

30

FONDAMENTI DI INFORMATICA

INTRODUZIONE

31

(VHPSLR VHTXHQ]D 

&RQGL]LRQL R VDOWL FRQGL]LRQDWL
INIZIO

(VHFX]LLRQH (VHFX] RQH 6L YDOXWD OD FRQGL]LRQH VSHFLILFDWD DOO¶LQWHUQR GHO EORFFR VH HC YHULILFDWD VL SURVHJXH FRQ OD OLQHD GL IOXVVR FRQWUDVVHJQDWD GD ³6,´ DOWULPHQWL VH QRQ HC YHULILFDWD VL SURVHJXH SHU LO UDPR HWLFKHWWDWR FRQ ³12´ œ ( HC XQ
HVSUHVVLRQH UHOD]LRQDOH R ORJLFD  ULWRUQD YDORUH œ YHUR RSSXUH IDOVR

A,B

C:=A+B

SI C

E

NO

+ ³&DOFROD LO YDORUH GHOO¶HVSUHVVLRQH ( VH HC YHUR SURVHJXL SHU LO UDPR 6, DOWULPHQWL SURVHJXL SHU LO UDPR 12 ³
FINE

,O EORFFR FRQGL]LRQH HC O¶HOHPHQWR GL EDVH SHU UHDOL]]DUH DOWHUQDWLYH H ULSHWL]LRQL

FONDAMENTI DI INFORMATICA

INTRODUZIONE

32

FONDAMENTI DI INFORMATICA

INTRODUZIONE

33

$OWHUQDWLYD
(VSULPH OD VFHOWD WUD GXH SRVVLELOL D]LRQL R VHTXHQ]H GL D]LRQL PXWXDPHQWH HVFOXVLYH

,WHUD]LRQH R 5LSHWL]LRQH
Esprime la ripetizione di una sequenza di istruzioni. 1HO FDVR SLXC JHQHUDOH HC FRVWLWXLWD GD

INIZIO

‡ ,QL]LDOL]]D]LRQH DVVHJQD]LRQH GHL YDORUL LQL]LDOL DOOH YDULDELOL FDUDWWHULVWLFKH GHO FLFOR YLHQH HVHJXLWD XQD VROD YROWD  ‡ &RUSR HVHFX]LRQH GHOOH LVWUX]LRQL IRQGDPHQWDOL GHO FLFOR FKH GHYRQR HVVHUH HVHJXLWH LQ PRGR ULSHWLWLYR
NO

LEGGI: A,B

SI

A<B

‡ 0RGLILFD PRGLILFD GHL YDORUL GHOOH YDULDELOL FKH FRQWUROODQR O
HVHFX]LRQH GHO FLFOR HVHJXLWR DG RJQL LWHUD]LRQH  ‡ &RQWUROOR GHWHUPLQD LQ EDVH DO YDORUH GHOOH YDULDELOL FKH FRQWUROODQR O
HVHFX]LRQH GHO FLFOR VH LO FLFOR GHYH HVVHUH ULSHWXWR R PHQR

C:=A

C:=B

STAMPA: C

FINE

FONDAMENTI DI INFORMATICA

INTRODUZIONE

34

FONDAMENTI DI INFORMATICA

INTRODUZIONE

35

,WHUD]LRQH

(VHPSLR $OJRULWPR FKH FDOFROD LO SURGRWWR FRPH VHTXHQ]D GL VRPPH VL VXSSRQH <! ;˜ 

INIZIO

INIZIO

X,Y,Z variabili
LEGGI: N

READ: X,Y

lettura

Z:=0

assegnamento

I:=1

Inizializzazione

SI

X=0 NO X:=X-1

condizione

SI I>N NO STAMPA: I

Controllo

Corpo
Z:=Z+Y

I:=I+1

Modifica

WRITE: Z

stampa

FINE

FINE

FONDAMENTI DI INFORMATICA

INTRODUZIONE

36

FONDAMENTI DI INFORMATICA

INTRODUZIONE

37

$UFKLWHWWXUD GL XQ 6LVWHPD GL (ODERUD]LRQH
+DUGZDUH H 6RIWZDUH +DUGZDUH H 6RIWZDUH 3ULPD VFRPSRVL]LRQH GL XQ VLVWHPD LQIRUPDWLFR  +DUGZDUH FRPSRQHQWL ILVLFL GHO VLVWHPD œ 6RIWZDUH L SURJUDPPL FKH YHQJRQR HVHJXLWL GDO VLVWHPD œ ,O FRQILQH WUD KDUGZDUH H VRIWZDUH LQ UHDOWDC QRQ HC VHPSUH EHQ GHILQLWR LQ JHQHUDOH YL q XQR VWUDWR LQWHUPHGLR LO ILUPZDUH FRVWLWXLWR GD SURJUDPPL GLUHWWDPHQWH PDSSDWL VX FLUFXLWL HOHWWURQLFL
UTENTE

0RGHOOR GL 9RQ 1HXPDQQ $UFKLWHWWXUD GL XQ HODERUDWRUH
2UJDQL]]DWD VHFRQGR LO PRGHO,R GHOOD PDFFKLQD GL YRQ 1HXPDQQ GHILQLWD QHL WDUGL DQQL ¶ DOO¶,QVWLWXWH IRU $GYDQFHG 6WXG\ GL 3ULQFHWRQ (C FRVWLWXLWD GD TXDWWUR HOHPHQWL IXQ]LRQDOL IRQGDPHQWDOL œ œ œ œ œ œ œ œ 8QLWj FHQWUDOH GL HODERUD]LRQH &38  0HPRULD &HQWUDOH 3HULIHULFKH %XV GL VLVWHPD

Bus di sistema

Software
Firmware

Hardware

Unita’ di elaborazione

Memoria centrale

Interfacce verso periferiche

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

$UFKLWHWWXUD GL XQ HODERUDWRUH
œ /D &38 FRQWLHQH L GLVSRVLWLYL HOHWWURQLFL LQ JUDGR GL œ DFTXLVLUH LQWHUSUHWDUH HG HVHJXLUH OH LVWUX]LRQL GL RJQL SURJUDPPD WUDVIRUPDQGR L GDWL /H LVWUX]LRQL YHQJRQR HVHJXLWH LQ VHTXHQ]D 'DWL HG LVWUX]LRQL YHQJRQR WUDVIHULWL GD H YHUVR OD PHPRULD FHQWUDOH œ /D PHPRULD FHQWUDOH FRQWLHQH VLD OH LVWUX]LRQL FKH L GDWL œ LQIRUPD]LRQL QHFHVVDULH SHU HVHJXLUH XQ SURJUDPPD  +D GLPHQVLRQL OLPLWDWH HG HC YRODWLOH FLRHC OH LQIRUPD]LRQL PHPRUL]]DWH YHQJRQR SHUVH DOOR VSHJQLPHQWR GHO FRPSXWHU  œ /H SHULIHULFKH FRQVHQWRQR XQR VFDPELR GL LQIRUPD]LRQL œ IUD O
HODERUDWRUH H O
HVWHUQR LQJUHVVRXVFLWD PHPRULD VHFRQGDULD  ,Q SDUWLFRODUH OD PHPRULD VHFRQGDULD R PHPRULD GL PDVVD YLHQH XWLOL]]DWD SHU PHPRUL]]DUH JUDQGL TXDQWLWD
GL LQIRUPD]LRQL LQ PRGR SHUVLVWHQWH +D GLPHQVLRQL HOHYDWH PD O¶DFFHVVR H
PHQR UDSLGR ULVSHWWR DOOD PHPRULD FHQWUDOH œ ,O EXV GL VLVWHPD FROOHJD TXHVWL HOHPHQWL IXQ]LRQDOL œ )RUQLVFH LO VXSSRUWR ILVLFR SHU OD WUDVPLVVLRQH GHL GDWL WUD L YDUL HOHPHQWL

8QLWj GL (ODERUD]LRQH &HQWUDO 3URFHVVLQJ 8QLW &38
(C OD SDUWH FKH HVHJXH H FRQWUROOD O¶HODERUD]LRQH

PC IR RINT

A B

ALU

PSW

RI

RD

clock

CU

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

8QLWj GL (ODERUD]LRQH &HQWUDO 3URFHVVLQJ 8QLW &38
%OORFFKLL &RPSRQHQWLL % RFFK &RPSRQHQW  ‡ O¶XQLWj DULWPHWLFRORJLFD $ULWKPHWLF /RJLF 8QLW $/8 FKH HVHJXH OH RSHUD]LRQL HOHPHQWDUL QHFHVVDULH SHU O¶HVHFX]LRQH GHOOH LVWUX]LRQL ‡ O¶XQLWDC GL FRQWUROOR &RQWURO 8QLW &8  FRQWUROOD H FRRUGLQD O¶DWWLYLWDC GHOOD &38 ,Q SDUWLFRODUH HC UHVSRQVDELOH GHO WUDVIHULPHQWR H GHOOD GHFRGLILFD GHOOH LVWUX]LRQL GDOOD PHPRULD FHQWUDOH DL UHJLVWUL GHOOD &38 ‡ LO FORFN RURORJLR ³FDGHQ]D´ OH RSHUD]LRQL HOHPHQWDUL SHUPHWWHQGR LO VLQFURQLVPR GHOOH RSHUD]LRQL ‡ YDUL UHJLVWUL DG HVHPSLR $ % 3& 36: HWF  XQ UHJLVWUR HC XQD ORFD]LRQH XWLOL]]DWD SHU PHPRUL]]DUH GDWL LVWUX]LRQL R LQGLUL]]L DOO¶LQWHUQR GHOOD &38 /¶DFFHVVR DL UHJLVWUL HC PROWR YHORFH + 1HL PRGHUQL VLVWHPL GL HODERUD]LRQH OD &38 HC UHDOL]]DWD GD XQ XQLFR FLUFXLWR LQWHJUDWR FKLS  LO PLFURSURFHVVRUH

&38 UHJLVWUL SULQFLSDOL
‡ 5' UHJLVWUR GDWL YLHQH XWLOL]]DWR SHU WUDVIHULUH GDWL GD H YHUVR OD PHPRULD FHQWUDOH ‡ 5, UHJLVWUR LQGLUL]]L YLHQH XWLOL]]DWR SHU PHPRUL]]DUH O¶LQGLUL]]R GHOOD FHOOD FRUUHQWH QHOOD PHPRULD FHQWUDOH OD VRUJHQWHGHVWLQD]LRQH GHO WUDVIHULPHQWR GL GDWL  ‡ 3& 3URJUDP &RXQWHU  UHJLVWUR ³FRQWDWRUH´ GHO SURJUDPPD &RQWLHQH O¶LQGLUL]]R GHOOD SURVVLPD LVWUX]LRQH GD HVHJXLUH ‡ ,5 ,QVWUXFWLRQ 5HJLVWHU  UHJLVWUR LVWUX]LRQH FRUUHQWH &RQWLHQH LVWDQWH SHU LVWDQWH O¶LVWUX]LRQH FKH HC DWWXDOPHQWH LQ HVHFX]LRQH ‡ 5HJLVWUL DFFXPXODWRUL R GL ODYRUR DG HVHPSLR $%  &RQWHQJRQR RSHUDQGL H ULVXOWDWL GHOOH RSHUD]LRQL VYROWH GDOOH $/8 ‡ 5,17 UHJLVWUR LQWHUUX]LRQL VWDWR SHULIHULFKH ‡ 36: 3URJUDP 6WDWXV :RUG  L FXL ELW IRUQLVFRQR LQIRUPD]LRQL VXO ULVXOWDWR GHOO¶XOWLPD RSHUD]LRQH HVHJXLWD GDOOD $/8 RYHUIORZ ]HUR FDUU\ VHJQR

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

&38 8QLWDC GL &RQWUROOR
)HWFKH[HFXWH )HWFK H[HFXWH ‡ /¶XQLWDC GL FRQWUROOR KD LO FRPSLWR GL  UHSHULUH GDOOD PHPRULD FHQWUDOH OH LVWUX]LRQL GL XQ SURJUDPPD IHWFK   LQWHUSUHWDUOH  IDUOH HVHJXLUH H[HFXWH  ‡ /D &38 VL FRPSRUWD TXLQGL FRPH VHJXH ULSHWL )(7&+ GL XQD LVWUX]LRQH (;(&87( GHOO¶ LVWUX]LRQH ILQFKHC LVWUX]LRQH +$/7 RSSXUH (5525( ‡ )(7&+  SURYRFD LO WUDVIHULPHQWR H OD GHFRGLILFD GHOOD SURVVLPD LVWUX]LRQH GD HVHJXLUH LO FXL LQGLUL]]R HC QHO UHJLVWUR 3&  /H LVWUX]LRQL VRQR RUJDQL]]DWH LQ PHPRULD LQ VHTXHQ]D ‡ (;(&87( O¶HODERUDWRUH HVHJXH O¶LVWUX]LRQH WURYDWD FKHC HC VWDWD FDULFDWD QHO UHJLVWUR ,5  ,VWUX]LRQL SDUWLFRODUL SRVVRQR DOWHUDUH LO IOXVVR VHTXHQ]LDOH VDOWR FKLDPDWD GL VRWWRSURJUDPPL 

8QLWD
$ULWPHWLFRORJLFD $/8
5HDOL]]D OH RSHUD]LRQL DULWPHWLFKH H ORJLFKH QHFHVVDULH SHU O¶HVHFX]LRQH GHOOH LVWUX]LRQL $G HVHPSLLR $G HVHPS R $/8 D GXH RSHUDQGL FRQWHQXWL QHL UHJLVWUL $ H % LQ JUDGR GL HVHJXLUH OH RSHUD]LRQL DULWPHWLFKH VRPPD VRWWUD]LRQH SURGRWWR GLYLVLRQH   , GXH RSHUDQGL YHQJRQR FDULFDWL QHL UHJLVWUL $ H %  /D $/8 YLHQH DWWLYDWD LQ IXQ]LRQH GD XQ FRGLFH RSHUDWLYR LQYLDWR GDOOD &8 FKH VSHFLILFD LO WLSR GL RSHUD]LRQH  1HO UHJLVWUR $ YLHQH FDULFDWR LO ULVXOWDWR GHOO
RSHUD]LRQH HVHJXLWD GDOOD $/8  ,O UHJLVWUR 36: ULSRUWD VXL VXRL ELW LQGLFD]LRQL VXO ULVXOWDWR GHOO
RSHUD]LRQH ULSRUWR RYHUIORZ HWF 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

0HPRULD FHQWUDOH
œ &RQWLHQH GDWL HG LVWUX]LRQL UHODWLYL DO SURJUDPPD LQ œ HVHFX]LRQH œ (C GL GLPHQVLRQH OLPLWDWD œ œ (C XQ SDVVDJJLR ³REEOLJDWR´ SHU GDWL H LVWUX]LRQL OD &38 œ SXRC VFDPELDUH GLUHWWDPHQWH LQIRUPD]LRQL VROWDQWR FRQ OD PHPRULD FHQWUDOH œ (CYRODWLOH HG LQ JHQHUDOH GL GLPHQVLRQL ULGRWWH œ 6WUXWWXUD GHOOOOD PHPRULLD FHQWUDOOH 6WUXWWXUD GH D PHPRU D FHQWUD H ‡ (C XQD VHTXHQ]D GL FHOOH GL PHPRULD FLDVFXQD FRQWHQHQWH XQD VHTXHQ]D GL ELW FKLDPDWD SDUROD ZRUG GL GLPHQVLRQH SUHILVVDWD ‡ 2JQL SDUROD HC FDUDWWHUL]]DWD GD XQ LQGLUL]]R FKH OD LQGLYLGXD XQLYRFDPHQWH
0 1 2 3

0HPRULD &HQWUDOH
œ /D GLPHQVLRQH GHOOD SDUROD FDPELD D VHFRQGD GHO WLSR GL œ FDOFRODWRUH     ELW  œ /D PHPRULD FHQWUDOH HC FDUDWWHUL]]DWD GD XQD &DSDFLWj FKH œ HVSULPH OD PDVVLPD TXDQWLWj GL ELW PHPRUL]]DELOL 9LHQH JHQHUDOPHQWH PLVXUDWD LQ E\WH  E\WH  ELW œ /D &38 SXz VHOH]LRQDUH XQD SDUWLFRODUH FHOOD GL PHPRULD œ PHGLDQWH O¶ LQGLUL]]R FRQWHQXWR QHO UHJLVWUR LQGLUL]]L 5, 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

0HPRUL]]D]LRQH 8QLWDC GL PLVXUD
, VLVWHPL GL HODERUD]LRQH VRQR UHDOL]]DWL FRQ WHFQRORJLD GLJLWDOH OH LQIRUPD]LRQL VRQR UDSSUHVHQWDWH PHGLDQWH VHJQDOL HOHWWULFL D  YDORUL GL WHQVLRQH ^9ORZ 9KLJK` RSSXUH ^`  œ 3HU TXHVWR PRWLYR O¶XQLWDC ORJLFD GL PHPRUL]]D]LRQH H LQ œ JHQHUDOH GL UDSSUHVHQWD]LRQH GHOOH LQIRUPD]LRQL HC LO ELW ELQDU\ GLJLW  +XQ ELW HC XQD JUDQGH]]D LO FXL GRPLQLR GL YDULD]LRQH HC FRPSRVWR GDL GXH YDORUL ^` œ LO E\WH HTXLYDOH DG  ELW œ 8QLLWDC VXFFHVVLLYH 8Q WDC VXFFHVV YH œ SRWHQ]H LQ EDVH  GHO E\WH FRQ HVSRQHQWH PXOWLSOR LQWHUR œ GL   .LORE\WH  E\WH  E\WH a .% 0HJDE\WH  E\WH  E\WH a a E\WH a E\WH *% 7% 0%

,QGLUL]]DPHQWR
6H LO UHJLVWUR LQGLUL]]L 5, HC OXQJR 1 ELW VL SRVVRQR LQGLUL]]DUH 1 FHOOH GL PHPRULD FRQ LQGLUL]]R GD  D 1    $G HVHPSLR FRQ 1  VL LQGLUL]]DQR  SDUROH
0 1 RI read RD (N bit)

write

N-1 2

,QGLUL]]DWD XQD FHOOD DWWUDYHUVR 5, VL SRVVRQR HVHJXLUH RSHUD]LRQL GL OHWWXUD H GL VFULWWXUD GD H YHUVR LO UHJLVWUR GDWL 5' 2SHUD]LRQH GL OHWWXUD 7UDVIHULVFH LO FRQWHQXWR GHOOD FHOOD GL PHPRULD LQGLUL]]DWD GDO 5HJLVWUR ,QGLUL]]L QHO 5HJLVWUR 'DWL 2SHUD]LRQH GL VFULWWXUD 7UDVIHULVFH LO FRQWHQXWR GHO 5HJLVWUR 'DWL QHOOD FHOOD GL PHPRULD LQGLUL]]DWD GDO 5HJLVWUR ,QGLUL]]L /H RSHUD]LRQL YHQJRQR FRRUGLQDWH GDOOD &RQWURO 8QLW &8 

*LJDE\WH E\WH 7HUDE\WH E\WH

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

'LVSRVLWLYL ILVLFL XWLOL]]DWL SHU OD PHPRULD FHQWUDOH
œ 5$0 5DQGRP $FFHVV 0HPRU\ DG DFFHVVR FDVXDOH  VX GL œ HVVD VL SRVVRQR VYROJHUH RSHUD]LRQL VLD GL OHWWXUD FKH GL VFULWWXUD œ 520 5HDG 2QO\ 0HPRU\ D VROD OHWWXUD  QRQ YRODWLOL H œ QRQ VFULYLELOL LQ HVVH YHQJRQR FRQWHQXWL L GDWL H SURJUDPPL SHU LQL]LDOL]]DUH LO VLVWHPD œ 3520 3URJUDPPDEOH 520 6L SRVVRQR VFULYHUH œ VROWDQWR XQD YROWD PHGLDQWH SDUWLFRODUL DSSDUHFFKL GHWWL SURJUDPPDWRUL GL 520  œ (3520 (UDVDEOHSURJUDPPDEOH 520 VL FDQFHOODQR œ VRWWRSRQHQGROH D UDJJL XOWUDYLROHWWL  + )LUPZDUH  HC FRVWLWXLWR GDO VRIWZDUH PHPRUL]]DWR QHOOH 520 FRGLFH PLFURSURJUDPPDWR 

%XV GL VLVWHPD
œ ,QWHUFRQQHWWH OD &38 OD PHPRULH H OH LQWHUIDFFH YHUVR œ GLVSRVLWLYL SHULIHULFL ,2 PHPRULD GL PDVVD HWF œ &ROOHJD GXH XQLWDC IXQ]LRQDOL DOOD YROWD XQD WUDVPHWWH H œ O¶DOWUD ULFHYH ,O WUDVIHULPHQWR DYYLHQH VRWWR LO FRQWUROOR GHOOD &38 &RQWURO 8QLW 

CPU Bus

Memoria centrale

Int. periferiche

6X TXHVWR VXSSRUWR VSHVVR FRVWLWXLWR GD SL OLQHH YLDJJLDQR GDWL LQGLUL]]L H FRPDQGL 6L GLVWLQJXRQR VSHVVR WDOL OLQHH LQ œ EXV GDWL GDWD EXV œ œ EXV LQGLUL]]L DGGUHVV EXV œ œ EXV FRPDQGL FRPPDQG EXV œ

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

œ %XV GL 6LVWHPD
œ %XV GDWL ELGLUH]LRQDOH 6HUYH SHU WUDVPHWWHUH GDWL GDOOD œ PHPRULD DO UHJLVWUR GDWL R YLFHYHUVD œ %XV LQGLUL]]L XQLGLUH]LRQDOH 6HUYH SHU WUDVPHWWHUH LO œ FRQWHQXWR GHO UHJLVWUR LQGLUL]]L DOOD PHPRULD 9LHQH VHOH]LRQDWD XQD VSHFLILFD FHOOD SHU VXFFHVVLYH RSHUD]LRQL GL OHWWXUD R VFULWWXUD œ %XV FRPDQGL XQLGLUH]LRQDOH $G HVHPSLR FRPDQGR GL œ OHWWXUD R VFULWWXUD YHUVR OD PHPRULD FRPDQGR GL VWDPSD YHUVR XQD SHULIHULFD LQWHUIDFFLD  6H OD GLPHQVLRQH QXPHUR GL ELW GHO EXV GDWL q XJXDOH DOOD GLPHQVLRQH GHOOD SDUROD VL SXz WUDVIHULUH LQ SDUDOOHOR XQ LQWHUR GDWR $OWULPHQWL RFFRUURQR SL WUDVIHULPHQWL

,QWHUIDFFH GL ,QJUHVVR8VFLWD
&RQVHQWRQR LO FROOHJDPHQWR GHOO¶HODERUDWRUH FRQ OH YDULH SHULIHULFKH GLVFKL WHUPLQDOL VWDPSDQWL   6RQR GLYHUVH D VHFRQGD GHO WLSR GL SHULIHULFD VLD KDUGZDUH FKH VRIWZDUH  3HULLIHULLFKH 3HU IHU FKH œ 7DVWLHUD HC XQ GLVSRVLWLYR GL LQJUHVVR LQSXW FKH FRQVHQWH œ DO FDOFRODWRUH GL DFTXLVLUH GDWL GDOO¶XWHQWH , GDWL YHQJRQR LPPHVVL FRPH VHTXHQ]H GL FDUDWWHUL XQR SHU RJQL WDVWR SUHPXWR  œ 0RXVH HC XQ GLVSRVLWLYR GL LQJUHVVR FKH SXRC HVVHUH œ XWLOL]]DWR FRPH LQWHJUD]LRQH R SHUVLQR LQ VRVWLWX]LRQH GHOOD WDVWLHUD œ 9LGHR HC XQ GLVSRVLWLYR GL XVFLWD FKH FRQVHQWH OD œ YLVXDOL]]D]LRQH GL GDWL H ULVXOWDWL GHOO¶HODERUD]LRQH (C FDUDWWHUL]]DWR GD YDUL SDUDPHWUL WUD FXL  QXPHUR GL FRORUL UDSSUHVHQWDELOL  GLPHQVLRQL GHOOR VFKHUPR LQ SROOLFL  ULVROX]LRQH QXPHUR GL SXQWL SHU SROOLFH TXDGUDWR  FDSDFLWDC JUDILFD

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

œ 6WDPSDQWL GLVSRVLWLYL GL XVFLWD FKH SURGXFRQR OD œ YLVXDOL]]D]LRQH VX FDUWD R DOWUL VXSSRUWL VLPLOL GL GDWL H ULVXOWDWL GHOO¶HODERUD]LRQH 6RQR FDUDWWHUL]]DWH GD  YHORFLWj GL VWDPSD E\WH FDUDWWHUH R SDJLQH DO VHFRQGR   ULVROX]LRQH QXPHUR GL SXQWL SHU SROOLFH TXDGUDWR   FDSDFLWj JUDILFD  UXPRURVLWj  VHW GL FDUDWWHUL R IRQW VWDPSDELOL 9DULH WHFQRORJLH GLVSRQLELOL DG DJKL D PDUJKHULWD D JHWWR G¶LQFKLRVWUR ODVHU D WUDVIHULPHQWR WHUPLFR œ 7HUPLQDOL KDQQR XQD WDVWLHUD HG XQ YLGHR 3RVVRQR HVVHUH œ DOIDQXPHULFL R JUDILFL œ $QFKH OD PHPRULD VHFRQGDULD R GL PDVVD q YLVWD FRPH œ GLVSRVLWLYR SHULIHULFR

0HPRULD VHFRQGDULD R GL PDVVD
/D PHPRULD VHFRQGDULD VL EDVD VX GLVSRVLWLYL SHU OD PHPRUL]]D]LRQH GL JUDQGL PDVVH GL GDWL , GDWL PHPRUL]]DWL LQ TXHVWR WLSR GL PHPRULD VRSUDYYLYRQR DOO¶HVHFX]LRQH GHL SURJUDPPL SHUVLVWHQWL  /D FDSDFLWj GLPHQVLRQH GHOOD PHPRULD YDULD PROWR GD GLVSRVLWLYR D GLVSRVLWLYR GDOOH GHFLQH GL PHJDE\WH  E\WH DL JLJDE\WH  E\WH R WHUDE\WH  E\WH  $QFKH OD YHORFLWj GL DFFHVVRWUDVIHULPHQWR YDULD GD GLVSRVLWLYR D GLVSRVLWLYR FRPXQTXH PROWR VXSHULRUH D TXHOOD GHOOD PHPRULD FHQWUDOH  7DFFHVVR PHPRULD FHQWUDOH 7DFFHVVR GLVFKL PDJQHWLFL 7DFFHVVR GLVFKHWWL  PVHF  VHF  QVHF        QVHF  PVHF  PVHF 

VHF

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

'LVSRVLWLYL GL PHPRULD GL PDVVD
'XH FODVVL IRQGDPHQWDOL LQ EDVH DO PHWRGR GL DFFHVVR FRQVHQWLWR  DG DFFHVVR VHTXHQ]LDOH DG HVHPSLR QDVWUL  SHU FHUFDUH XQ GDWR q QHFHVVDULR DFFHGHUH D WXWWL TXHOOL FKH OR SUHFHGRQR VXO GLVSRVLWLYR  DG DFFHVVR GLUHWWR DL GDWL DG HVHPSLR GLVFKL  (C SRVVLELOH DFFHGHUH GLUHWWDPHQWH D TXDOXQTXH GDWR PHPRUL]]DWR JUD]LH DOO¶LQGLUL]]DPHQWR GL SRU]LRQL EORFFKL GHO GLVSRVLWLYR 1HO FDVR GL GLVSRVLWLYL PDJQHWLFL QDVWUL R GLVFKL O¶LQIRUPD]LRQH q SUHVHQWH LQ PHPRULD FRPH VWDWR GL SRODUL]]D]LRQH PDJQHWLFD FKH SXz HVVHUH SRVLWLYR R QHJDWLYR FRGLILFD ELQDULD 

1DVWUL PDJQHWLFL
œ 6RQR IHWWXFFH GL PDWHULDOH PDJQHWL]]DELOH DUURWRODWH VX œ VXSSRUWL FLUFRODUL R LQ FDVVHWWH œ 6XOOD IHWWXFFLD VRQR WUDFFLDWH GHOOH SLVWH RUL]]RQWDOL œ SDUDOOHOH 'L VROLWR  SLVWH SDUDOOHOH GL FXL  FRUULVSRQGRQR DG XQ E\WH H OD QRQD q LO ELW GL SDULWj
Nastro
1 2 3 4 5 6 7 8 bit parita‘

byte

œ ,O SDUDPHWUR SL LPSRUWDQWH q OD GHQVLWj PLVXUDWD LQ ELW SHU œ SROOLFH ESL ELW SHU LQFK  œ , GDWL VX QDVWUR VRQR RUJDQL]]DWL LQ ]RQH FRQWLJXH GHWWH œ UHJLVWUD]LRQL UHFRUG  œ 7XWWH OH HODERUD]LRQL VRQR VHTXHQ]LDOL OHQWH]]D GHOOH œ RSHUD]LRQL GL OHWWXUDVFULWWXUD VX XQ SUHFLVR UHFRUG  œ 2UPDL VYROJRQR VROR XQD IXQ]LRQH GL FRSLD GL ULVHUYD œ EDFNXS 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

'LVFKL PDJQHWLFL

,QJUHVVR R XVFLLWD GD R YHUVR VXSHUIILLFLLHWWUDFFLLDVHWWWWRUH!  ,QJUHVVR R XVF WD GD R YHUVR VXSHU F H UDFF D VH RUH! ‡ VSRVWDPHQWR GHOOD WHVWLQD VHHN YHUVR OD WUDFFLD ULFKLHVWD ‡ DWWHVD DIILQFKp LO VHWWRUH DUULYL VRWWR OD WHVWLQD ‡ WUDVIHULPHQWR GHL GDWL LQ R GD PHPRULD FHQWUDOH VROLWDPHQWH HVHJXLWR GD XQ SURFHVVRUH GHGLFDWR 'LUHFW 0HPRU\ $FFHVV '0$ 

traccia 0 traccia 100

traccia 200

Settore

8Q GLVFR HC FRVWLWXLWR GD XQ FHUWR QXPHUR GL SLDWWL GL PDWHULDOH PDJQHWL]]DELOH FRQ GXH VXSHUILFL FKH UXRWDQR DWWRUQR DG XQ SHUQR FHQWUDOH &LDVFXQD VXSHUILFLH KD XQD VHULH GL FHUFKL FRQFHQWULFL R WUDFFH H YLHQH VXGGLYLVD LQ VSLFFKL GL XJXDO JUDQGH]]D FKLDPDWL VHWWRUL 7XWWH OH WUDFFH HTXLGLVWDQWL GDO FHQWUR IRUPDQR XQ FLOLQGUR /D WHVWLQD VL VSRVWD ORQJLWXGLQDOPHQWH OXQJR OH WUDFFH , GDWL VRQR VFULWWL RFFXSDQGR SRVL]LRQL VXFFHVVLYH OXQJR OH WUDFFH &RUULVSRQGRQR DG XQR VWDWR GL SRODUL]]D]LRQH SRVLWLYD R QHJDWLYD GHO PDWHULDOH PDJQHWL]]DELOH FKH FRVWLWXLVFH L GLVFKL 2JQL EORFFR GL LQJUHVVRXVFLWD q VHOH]LRQDELOH PHGLDQWH OD WHUQD VXSHUILFLH WUDFFLD VHWWRUH! LQGLUL]]R  


FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI

'LLVFKHWWLL IOORSS\ GLLVN  ' VFKHWW I RSS\ G VN 

&DSDFLWj GHOOH PHPRULH
Tipo di memoria 0HPRULD FHQWUDOH 'LVFKL PDJQHWLFL 'LVFKL IORSS\ 1DVWUL ERELQD 1DVWUL FDVVHWWD Capacita‘  0E\WH  0E\WH  0E\WH  0E\WH  0E\WH  0E\WH

œ 6RQR GLVFKHWWL SRUWDWLOL FKH YHQJRQR XWLOL]]DWL SHU WUDVIHULUH œ LQIRUPD]LRQL ILOH WUD FRPSXWHU GLYHUVL œ &RVWLWXLWL GD XQ XQLFR GLVFR FRQ GXH VXSHUILFL œ œ YDUL WLSL LQ EDVH DO GLDPHWUR   H  SROOLFL œ œ , GLVFKL YHQJRQR IRUPDWWDWL GDO 6LVWHPD 2SHUDWLYR FKH OL œ VXGGLYLGH LQ WUDFFH H VHWWRUL H QH GHWHUPLQD OD GHQVLWj H OD FDSDFLWj  7LSLFDPHQWH  0E\WH

'LVFKL RWWLFL &'

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

3HUVRQDO &RPSXWHU

3HUVRQDO &RPSXWHU
³,%0FRPSDWLELOL´ KDQQR SURFHVVRUL GHOOD IDPLJOLD ,QWHO [  8086 80286 80386 80486 80586 (pentium)

prestazioni

œ 0HPRULD GL PDVVD JHQHUDOPHQWH FRPSRVWD GD GLVFR ULJLGR œ KDUG GLVN ILVVR GLVFKHWWL IORSS\ GLVN H &' HVWUDLELOL œ /
LQIRUPD]LRQH QHOOD PHPRULD GL PDVVD H
RUJDQL]]DWD LQ œ DUFKLYL R ILOH FDUDWWHUL]]DWL GD XQ QRPH œ 9DULH FODVVL GL 3& LQ EDVH DO WLSR GL SURFHVVRUH ,QWHO œ 0DFLQWRVK HWF

œ OH SUHVWD]LRQL VRQR LQIOXHQ]DWH DQFKH GD DOWUL SDUDPHWUL  œ  IUHTXHQ]D GHO FORFN  GLPHQVLRQH 5$0  YHORFLWDC GHO %86   œ XQLWDC GL PLVXUD GHOOH SUHVWD]LRQL œ 0,36 PLJOLDLD GL LVWUX]LRQL SHU VHFRQGR

0IORSV PLJOLDLD GL RSHUD]LRQL IORDWLQJ SRLQW SHU VHFRQGR œ , 3HUVRQDO &RPSXWHU KDQQR VWUXWWXUD VHPSOLFH FRVWR œ ULGRWWR PD SUHVWD]LRQL OLPLWDWH

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

$OWUL VLVWHPL GL FDOFROR
:RUNVWDWLLRQ :RUNVWDW RQ VLVWHPL JHQHUDOPHQWH GHGLFDWL DG XQ XWHQWH PD FRQ FDSDFLWDC GL VXSSRUWDUH SLXC DWWLYLWDC FRQWHPSRUDQHH 3UHVWD]LRQL SLXC HOHYDWH GHL 3& 0LLQLLFDOOFROODWRULL 0 Q FD FR DWRU  0DFFKLQH FDSDFL GL VHUYLUH GHFLQH GL FRQWHPSRUDQHDPHQWH FROOHJDWL WUDPLWH WHUPLQDOL 6XSHUFDOOFROODWRULL 6XSHUFD FR DWRU  +DQQR PROWL SURFHVVRUL H JUDQGL PHPRULH GL PDVVD FHQWLQDLD R PLJOLDLD GL WHUPLQDOL + 3RVVLELOLWDC GL FRQQHWWHUH DVVLHPH YDUL FDOFRODWRUL GL WLSR DQFKH GLYHUVR UHWL GL FDOFRODWRUL  XWHQWL

5HWLL OORFDOOLL 5HW RFD  FROOHJDQR HODERUDWRUL ILVLFDPHQWH YLFLQL QHOOR VWHVVR XIILFLR R VWDELOLPHQWR  /¶RELHWWLYR HC OD FRQGLYLVLRQH GL ULVRUVH

Stampante

Stampante

Disk server Bus di rete

Pc

Workstation

Workstation

PC

Workstation

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

5HWLL JHRJUDILLFKH 5HW JHRJUDI FKH œ &ROOHJDQR HODERUDWRUL GLVWULEXLWL VX œ JHRJUDILFD GL GLPHQVLRQL HVWHVH LQWHUFRQWLQHQWDOL  $G HVHPSLR ,QWHUQHW
Rete geografica

XQ¶DUHD DQFKH

+DUGZDUH6RIWZDUH
&38 PHPRULD FHQWUDOH H GLVSRVLWLYL VRQR UHDOL]]DWL FRQ WHFQRORJLD GLJLWDOH 'DWL HG RSHUD]LRQL YHQJRQR FRGLILFDWL PHGLDQWH VHTXHQ]H GL ELW  

printer

Host

Host

terminali remoti

PC mini Host

Rete locale

Linea telefonica

(YROX]LRQH H FRPSOHVVLWD
VLD GHOO
KDUGZDUH FKH GHO VRIWZDUH SURWRFROOL GL FROOHJDPHQWR

/¶XWLOL]]R GHOOD VROD VWUXWWXUD ILVLFD KDUGZDUH GHOO¶HODERUDWRUH LPSRUUHEEH DOO¶XWHQWH GL HVSULPHUH L SURSUL FRPDQGL DWWUDYHUVR VHTXHQ]H GL ELW OLQJXDJJLR PDFFKLQD  3HU TXHVWR PRWLYR RJQL HODERUDWRUH HC FRUUHGDWR GD XQ LQVLHPH GL SURJUDPPL FKH HOHYDQR LO OLYHOOR GL LQWHUD]LRQH XWHQWHPDFFKLQD DYYLFLQDQGR LO OLQJXDJJLR GL LQWHUD]LRQH DO OLQJXDJJLR QDWXUDOH LO VRIWZDUH

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

6RIWZDUH
(
XQ LQVLHPH GL SURJUDPPL 3HU VHPSOLFLWj VL SXz SHQVDUH DG XQD RUJDQL]]D]LRQH D VWUDWL FLDVFXQR FRQ IXQ]LRQDOLWj GL OLYHOOR SL DOWR ULVSHWWR D TXHOOL VRWWRVWDQWL PDFFKLQD YLUWXDOH

6LVWHPD 2SHUDWLYR
,QVLHPH GL SURJUDPPL FKH UHQGRQR O
HODERUDWRUH RSHUDWLYR HG XVDELOH ,O 6LVWHPD 2SHUDWLYR RSHUD GLUHWWDPHQWH DO GL VRSUD GL KDUGZDUH H ILUPZDUH /H IXQ]LRQL PHVVH D GLVSRVL]LRQH GLSHQGRQR GDOOD FRPSOHVVLWj GHO VLVWHPD GL HODERUD]LRQH œ *HVWLVFH OH ULVRUVH GLVSRQLELOL œ œ ,QWHUSUHWD HG HVHJXH FRPDQGL HOHPHQWDUL œ œ 6WDPSD OHJJH YLVXDOL]]D VX YLGHR œ œ *HVWLVFH OD PHPRULD FHQWUDOH HG œ œ 2UJDQL]]D H JHVWLVFH OD PHPRULD GL PDVVD œ œ *HVWLVFH XQ VLVWHPD PXOWLXWHQWH œ œ HWF œ ,Q SUDWLFD O
XWHQWH YHGH OD PDFFKLQD VROR DWWUDYHUVR LO 6LVWHPD 2SHUDWLYR $WWUDYHUVR LO 62 LO OLYHOOR GL LQWHUD]LRQH XWHQWH HODERUDWRUH YLHQH HOHYDWR œ VHQ]D 62 VHTXHQ]H GL ELW SHU HVSULPHUH LVWUX]LRQL H œ GDWL œ FRQ 62 œ SDUROH ³FKLDYH´ FRPDQGL SURJUDPPL GDWL LQWHUL UHDOL FDUDWWHUL HWF 

Programmi Applicativi Ambiente di programmazione Sistema Operativo Firmware Hardware Software di Comunicazione

} Software di Base

)LUPZDUH FRQILQH IUD KDUGZDUH H VRIWZDUH Ê XQR VWUDWR GL PLFURSURJUDPPL VFULWWL VX PHPRULH SHUPDQHQWL GDL FRVWUXWWRUL FKH DJLVFRQR GLUHWWDPHQWH DO GL VRSUD GHOOR VWUDWR KDUGZDUH

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI

6LVWHPD 2SHUDWLYR
,O 62 WUDGXFH OH ULFKLHVWH GHOO¶XWHQWH LQ RSSRUWXQH VHTXHQ]H GL LPSXOVL GD VRWWRSRUUH DOOD PDFFKLQD ILVLFD
"esegui prog_1" S.O. 0011...101... Hardware

6LVWHPD 2SHUDWLYR
4XDOVLDVL RSHUD]LRQH GL DFFHVVR D ULVRUVH GHOOD PDFFKLQD LPSOLFLWDPHQWH ULFKLHVWD GDO FRPDQGR GL XWHQWH YLHQH HVSOLFLWDWD GDO 62 DG HVHPSLR DFFHVVR D PHPRULD FHQWUDOH VHFRQGDULD RSSXUH ,2 YHUVR YLGHR WDVWLHUD HWF 8WHQWH 8WHQWH H VHJX SURJB ³HVHJXLL SURJB ´ 62 62  LQSXW GD WDVWLHUD  ULFHUFD FRGLFH GL ³SURJB´ LQ PHP VHFRQGDULD  FDULFD LQ PHP FHQWUDOH FRGLFH H GDWL HODERUD]LRQH!  RXWSXW VX YLGHR

e viceversa:

"risultato 10" 0001010... S.O. Hardware

U VX WDWR  ³ULLVXOOWDWR  ´

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

&ODVVLILFD]LRQH GHL 6LVWHPL 2SHUDWLYL
,Q EDVH DOO QXPHUR GLL XWHQWLL ,Q EDVH D QXPHUR G XWHQW  ‡ 0RQRXWHQWH XQ VROR XWHQWH DOOD YROWD SXRC XWLOL]]DUH LO VLVWHPD ‡ 0XOWLXWHQWH SLXC XWHQWL FRQWHPSRUDQHDPHQWH SRVVRQR LQWHUDJLUH FRQ OD PDFFKLQD
+ QHO FDVR GL SL XWHQWL FROOHJDWL LO 62 GHYH IRUQLUH D FLDVFXQ XWHQWH O
DVWUD]LRQH GL XQ VLVWHPD ³GHGLFDWR´

6RIWZDUH GL FRPXQLFD]LRQH
(C O
LQVLHPH GHL SURJUDPPL FKH VL RFFXSDQR GL VXSSRUWDUH OD FRPXQLFD]LRQH WUD PDFFKLQH FROOHJDWH LQ UHWH /D FRPXQLFD]LRQH DYYLHQH XWLOL]]DQGR SURWRFROOL FKH JDUDQWLVFRQR XQ FRUUHWWR VFDPELR GHL GDWL H PHVVDJJL (VHPSLLR (VHPS R ‡ UHWH LQWHUQHW SURWRFROOR 7&3,3   WHOQHW DSHUWXUD GL XQD VHVVLRQH UHPRWD  IWS WUDVIHULPHQWR GL ILOH  PDLO SRVWD HOHWWURQLFD 

,Q EDVH DOO QXPHUR GLL SURJUDPPLL LLQ HVHFX]LLRQH ,Q EDVH D QXPHUR G SURJUDPP Q HVHFX] RQH ‡ 0RQRSURJUDPPDWR LO VLVWHPD SXRC JHVWLUH O
HVHFX]LRQH GL DO SLXC XQ SURJUDPPD DOOD YROWD ‡ 0XOWLSURJUDPPDWR LO VLVWHPD RSHUDWLYR HC LQ JUDGR GL SRUWDUH DYDQWL O
HVHFX]LRQH FRQWHPSRUDQHD GL SLXC SURJUDPPL PDQWHQHQGR XQD VROD &38 
+QHO FDVR GL PXOWLSURJUDPPD]LRQH LO 62 GHYH JHVWLUH O¶XQLWj GL HODERUD]LRQH &38 VXGGLYLGHQGROD WUD L YDUL SURJUDPPL

(VHPSLL (VHPS  ‡ 06'26 PRQRXWHQWH PRQRSURJUDPPDWR ‡ 81,; PXOWLXWHQWH PXOWLSURJUDPPDWR ‡ :LQGRZV 
26 PRQRXWHQWH PXOWLSURJUDPPDWR

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

3URJUDPPL DSSOLFDWLYL
5LVROYRQR SUREOHPL VSHFLILFL GHJOL XWHQWL œ ZRUG SURFHVVRU HODERUD]LRQH GL WHVWL œ œ IRJOL HOHWWURQLFL VSUHDGVKHHW  JHVWLRQH GL œ WDEHOOH H JUDILFL œ GDWD EDVH JHVWLRQH GL DUFKLYL œ œ 6RQR VFULWWL LQ OLQJXDJJL GL SURJUDPPD]LRQH GL DOWR œ OLYHOOR œ (VVHQGR GL DOWR OLYHOOR ULVHQWRQR LQ PLVXUD ULGRWWD R QXOOD œ GHOOH FDUDWWHULVWLFKH GHOO
DUFKLWHWWXUD GHOO
DPELHQWH VRWWRVWDQWH SRUWDELOLWj 

$PELHQWH GL SURJUDPPD]LRQH
(C O
LQVLHPH GHL SURJUDPPL FKH FRQVHQWRQR OD VFULWWXUD OD YHULILFD H O
HVHFX]LRQH GL QXRYL SURJUDPPL IDVL GL VYLOXSSR  6YLLOOXSSR GLL XQ 3URJUDPPD 6Y XSSR G XQ 3URJUDPPD œ $IILQFKHC XQ SURJUDPPD VFULWWR LQ XQ TXDOVLDVL OLQJXDJJLR œ GL SURJUDPPD]LRQH DG HV LO & VLD FRPSUHQVLELOH H TXLQGL HVHJXLELOH GD XQ FDOFRODWRUH H
QHFHVVDULD XQ
D]LRQH GL WUDGX]LRQH GDO OLQJXDJJLR RULJLQDULR DO OLQJXDJJLR PDFFKLQD œ 4XHVWD RSHUD]LRQH YLHQH QRUPDOPHQWH VYROWD GD VSHFLDOL œ SURJUDPPL GHWWL WUDGXWWRUL 3URJUDPPD PDLQ ^ LQW $  $ $ LI 7UDGX]LRQH   

, WUDGXWWRUL SURYYHGRQR D FRQYHUWLUH LO FRGLFH GL SURJUDPPL VFULWWL LQ XQ SDUWLFRODUH OLQJXDJJLR GL SURJUDPPD]LRQH SURJUDPPL VRUJHQWL  QHOOD FRUULVSRQGHQWH UDSSUHVHQWD]LRQH LQ OLQJXDJJLR PDFFKLQD SURJUDPPL HVHJXLELOL 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

6YLOXSSR GL 3URJUDPPL
Programma Sorgente: main(){.. Programma eseguibile: 001010111... Esecuzione

)DVL GL 6YLOXSSR GL XQ 3URJUDPPD
/R VYLOXSSR GL XQ VHPSOLFH SURJUDPPD DYYLHQH DWWUDYHUVR O
DWWXD]LRQH GL XQD VHTXHQ]D GL IDVL 'DWR XQ SUREOOHPD GD ULLVROOYHUH 'DWR XQ SURE HPD GD U VR YHUH  VFHOWD GHO PHWRGR ULVROXWLYR H UDSSUHVHQWD]LRQH PHGLDQWH XQ DOJRULWPR  UDSSUHVHQWD]LRQH GHOO
DOJRULWPR QHO OLQJXDJJLR GL SURJUDPPD]LRQH VFHOWR DG HVHPSLR &  VL RWWLHQH LO SURJUDPPD LQ IRUPD VRUJHQWH /D IDVH GL VFULWWXUD GHO SURJUDPPD YLHQH QRUPDOPHQWH GHWWD GL HGLWLQJ  FRPSLOD]LRQH ,O SURJUDPPD VRUJHQWH SURGRWWR QHOOD OD IDVH GL HGLWLQJ YLHQH WUDGRWWR GDO FRPSLODWRUH LQ OLQJXDJJLR PDFFKLQD SURJUDPPD HVHJXLELOH  ,Q TXHVWD IRUPD LO SURJUDPPD HC SURQWR SHU HVVHUH HVHJXLWR /D IDVH GL FRPSLOD]LRQH SXRC ULOHYDUH HYHQWXDOL HUURUL JHQHUDOPHQWH VLQWDWWLFL FRQWHQXWL QHO SURJUDPPD VRUJHQWH

'XH FDWHJRULLH GLL WUDGXWWRULL 'XH FDWHJRU H G WUDGXWWRU  œ L &RPSLODWRUL DFFHWWDQR LQ LQJUHVVR O
LQWHUR SURJUDPPD H œ SURGXFRQR LQ XVFLWD OD UDSSUHVHQWD]LRQH GHOO
LQWHUR SURJUDPPD LQ OLQJXDJJLR PDFFKLQD œ JOL ,QWHUSUHWL WUDGXFRQR HG HVHJXRQR GLUHWWDPHQWH œ FLDVFXQD LVWUX]LRQH GHO SURJUDPPD VRUJHQWH LVWUX]LRQH SHU LVWUX]LRQH 4XLLQGLL 4X QG  œ &RPSLODWRUH SHU RJQL SURJUDPPD GD WUDGXUUH OR VFKHPD œ YLHQH SHUFRUVR XQD YROWD VROD SULPD GHOO
HVHFX]LRQH œ ,QWHUSUHWH OR VFKHPD YLHQH DWWUDYHUVDWR WDQWH YROWH TXDQWH œ VRQR OH LVWUX]LRQL FKH FRPSRQJRQR LO SURJUDPPD DG RJQL DWWLYD]LRQH GHOO
LQWHUSUHWH VX XQD SDUWLFRODUH LVWUX]LRQH VHJXH O
HVHFX]LRQH GHOO
LVWUX]LRQH VWHVVD + O
HVHFX]LRQH GL XQ SURJUDPPD FRPSLODWR HC SLXC YHORFH GHOO
HVHFX]LRQH GL XQ SURJUDPPD PHGLDQWH LQWHUSUHWH

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI  

GHEXJJLQJ $QDOLVL GHO FRUUHWWR IXQ]LRQDPHQWR GHO SURJUDPPD 3XRC HVVHUH DJHYROPHQWH HVHJXLWD PHGLDQWH VSHFLDOL SURJUDPPL GHWWL GHEXJJHU 0HGLDQWH XQ GHEXJJHU HC SRVVLELOH HIIHWWXDUH O
HVHFX]LRQH FRQWUROODWD GHO SURJUDPPD $G HVHPSLR ‡ HVHFX]LRQH GL XQD LVWUX]LRQH SHU YROWD ‡ YLVXDOL]]D]LRQH GHL YDORUL GL YDULDELOL ‡ SXQWL GL DUUHVWR EUHDNSRLQW + ULOHYD]LRQH GL HUURUL QRQ ULVFRQWUDELOL LQ IDVH GL FRPSLOD]LRQH HUURUL D WHPSR GL HVHFX]LRQH  HVHFX]LRQH 4XDQGR LO SURJUDPPD HC VWDWR RSSRUWXQDPHQWH YHULILFDWR SXRC HVVHUH LQILQH HVHJXLWR SHU OD ULVROX]LRQH GHO SUREOHPD GL SDUWHQ]D

)DVL GL 6YLOXSSR
Problema metodo risolutivo Algoritmo diagramma di flusso Editing programma sorgente Compilazione errori a t. di compilazione

programma eseguibile Debugging errori a t. di Esecuzione

eseguibile verificato Esecuzione

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

FONDAMENTI DI INFORMATICA

STRUTTURA DEI CALCOLATORI 

5DSSUHVHQWD]LRQH GHOO
LQIRUPD]LRQH LQ XQ FDOFRODWRUH
,QIRUPD]LLRQLL ,QIRUPD] RQ œ WHVWL QXPHUL LQWHUL H UHDOL LPPDJLQL  VXRQL HWF œ &RPH YLLHQH UDSSUHVHQWDWD OO

LLQIRUPD]LLRQH LLQ XQ FDOOFROODWRUH &RPH Y HQH UDSSUHVHQWDWD QIRUPD] RQH Q XQ FD FR DWRUH " " œ XVR GL WHFQRORJLD GLJLWDOH DOO¶LQWHUQR GHOOD &38 OH œ LQIRUPD]LRQL YHQJRQR UDSSUHVHQWDWH GD  SRVVLELOL YDORUL GL WHQVLRQH HOHWWULFD ^9KLJK 9ORZ` œ ,Q JHQHUDOH D VHFRQGD GHO WLSR GL GLVSRVLWLYR FRQVLGHUDWR L œ YDORUL ]HUR HG XQR VRQR UDSSUHVHQWDWL  GD XQD WHQVLRQH HOHWWULFD DOWD EDVVD   GD XQ GLIIHUHQWH VWDWR GL SRODUL]]D]LRQH PDJQHWLFD SRVLWLYD QHJDWLYD   GD OXFH H EXLR  HWF œ 8QLWD
GL LQIRUPD]LRQH QHO FDOFRODWRUH ELW œ  2JQL LQIRUPD]LRQH YLHQH WUDVIRUPDWD QHO FDOFRODWRUH LQ XQD VHTXHQ]D GL ELW IRUPD %,1$5,$  FLRH
LQ XQD VHTXHQ]D GL  H  

&RGLILFD GHL QXPHUL
,O VLVWHPD GL QXPHUD]LRQH FKH XWLOL]]LDPR VL GLFH DUDELFR H IX LQWURGRWWR LQ (XURSD GDJOL DUDEL QHO 0HGLR (YR œ (C GHFLPDOH R LQ EDVH   HVVR UDSSUHVHQWD L QXPHUL œ WUDPLWH VHTXHQ]H GL FLIUH FKH YDQQR GD  D  GLHFL FLIUH  œ (C SRVL]LRQDOH LO SHVR DWWULEXLWR DG RJQL FLIUD H
IXQ]LRQH œ GHOOD SRVL]LRQH FKH RFFXSD (VLVWRQR DQFKH VLVWHPL DGGLWLYL LQ FXL RJQL XQLWD
H
UDSSUHVHQWDWD GD XQ XQLFR VLPEROR R QRQSRVL]LRQDOL FRPH TXHOOR URPDQR   , VLVWHPL SRVL]LRQDOL FRQVHQWRQR GL UDSSUHVHQWDUH QXPHUL JUDQGL FRQ XQ QXPHUR OLPLWDWR GL FLIUH H GL VYROJHUH VX GL HVVL FDOFROL SLX
HIILFLHQWL

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

, VLLVWHPLL GLL QXPHUD]LLRQH SRVLL]LLRQDOOH VRQR FDUDWWHULL]]DWLL , V VWHP G QXPHUD] RQH SRV ] RQD H VRQR FDUDWWHU ]]DW GD XQD EDVH E H XQ DOOIDEHWR a GD XQD EDVH E H XQ D IDEHWR a œ $OIDEHWR a) H¶ O¶LQVLHPH GHOOH FLIUH GLVSRQLELOL SHU œ HVSULPHUH L QXPHUL $G RJQL FLIUD FRUULVSRQGH XQ YDORUH FRPSUHVR WUD  H E  $G HVHPSLR QHOOD QXPHUD]LRQH GHFLPDOH O¶DOIDEHWR HC a=^` œ %DVH E  HC LO QXPHUR GHJOL HOHPHQWL FKH FRPSRQJRQR œ O¶DOIDEHWR $G HVHPSLR QHO FDVR GHFLPDOH E 

1XPHUD]LRQH LQ EDVH S
a = ^S` E S XQ QXPHUR JHQHULFR 1 LQ EDVH S H
UDSSUHVHQWDWR GD XQD VHTXHQ]D GL FLIUH DQDQDD GRYH DL ³ a, "L Q DQ H
OD FLIUD SLX
VLJQLILFDWLYD PHQWUH D H
OD PHQR VLJQLILFDWLYD

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

&RGLILFD GHL 1XPHUL 1DWXUDOL
&RQVLGHULDPR O¶LQVLHPH GHL QXPHUL QDWXUDOL œ 'DWR XQ VLVWHPD GL QXPHUD]LRQH SRVL]LRQDOH œ a=^S`  E S a, E! (VHPSLR   (VHPSLR 

(6(03,
&RQYHUVLLRQH GLL XQ QXPHUR GD EDVH E D EDVH GHFLLPDOOH &RQYHUV RQH G XQ QXPHUR GD EDVH E D EDVH GHF PD H &RGLILFD GHFLPDOH E  1                 &RGLILFD ELQDULD E  a=^`

œ 6LD DQDQDD OD FRGLILFD GL XQ QXPHUR QDWXUDOH 1 LQ œ EDVH S DOORUD LO YDORUH GL 1 LQ EDVH GHFLPDOH HC GDWR GDOOD IRUPXOD 1S DQ  SQDQ SQDSD DL  S L

1                                 (VHPSLR 1             (VHPSLR &RGLILFD HVDGHFLPDOH E  a=^$%&'()` 1 %) %)         &RGLILFD RWWDOH E  a=^`

R LQ IRUPD SLXC FRPSDWWD 1S ÊL 

Q

œ &RQ Q FLIUH LQ EDVH S H
SRVVLELOH UDSSUHVHQWDUH SQ QXPHUL QDWXUDOL GLYHUVL GD  D SQ L GXH OLPLWL VL RWWHQJRQR VRVWLWXHQGR D WXWWL JOL Q FRHIILFLHQWL DL  R S ULVSHWWLYDPHQWH  ,QIDWWL LO QXPHUR PDVVLPR VL RWWLHQH XWLOL]]DQGR OD FLIUD PDVVLPD S SHU RJQL SRVL]LRQH S SQ  S  SQ  S S S SQ  SQ  SQ  SQ  SQ    S  S   SQ 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

&RQYHUVLLRQH GLL XQ QXPHUR QDWXUDOOH LLQ EDVH  LLQ EDVH QRQ &RQYHUV RQH G XQ QXPHUR QDWXUD H Q EDVH  Q EDVH QRQ GHFLLPDOOH GHF PD H /D IRUPXOD 1S DQ  SQDQ SQDSD 

,Q SUDWLFD LO SURFHGLPHQWR GD VHJXLUH HC 6LD 1 LO QXPHUR  6L GLYLGH 1 SHU OD QXRYD EDVH S VLD 4 LO TXR]LHQWH HG 5 LO UHVWR  6L FRQYHUWH 5 QHOOD FRUULVSRQGHQWH FLIUD GHOOD QXRYD EDVH S  6L DJJLXQJH OD FLIUD FRVL
RWWHQXWD D VLQLVWUD GHOOH FLIUH RWWHQXWH LQ SUHFHGHQ]D  6H 4  ILQH $OWULPHQWL SRQL 1 4 H WRUQD DO SDVVR 

VL SXR
ULVFULYHUH FRPH 1S D  S  D  S  S  DQ DQ  S 

‡ (VHJXHQGR OD GLYLVLRQH LQWHUD SHU S 1S GLY S D  S   S  DQ DQ  S  4 1S PRG S D
>UHVWR

GHOOD GLYLVLRQH LQWHUD@

‡ $SSOLFKLDPR OD GLYLVLRQH LQWHUD SHU S VXO ULVXOWDWR 4 GHOOD GLYLVLRQH SUHFHGHQWH 4 GLY S D  S  S  DQ DQ  S  4 4 PRG S D >UHVWR GHOOD GLYLVLRQH LQWHUD@

‡ 5LSHWLDPR LO SURFHGLPHQWR VX 4 4 HWF SHU RWWHQHUH OH FLIUH ULPDQHQWL D D  DQ 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

&RQYHUVLRQH ELQDULD
6L YXROH FRQYHUWLUH XQ QXPHUR 1 LQ EDVH   QHOOD FRUULVSRQGHQWH UDSSUHVHQWD]LRQH LQ EDVH  œ $SSOLFDQGR LO SURFHGLPHQWR YLVWR ELVRJQD HIIHWWXDUH œ VXFFHVVLYH GLYLVLRQL SHU  œ ,O ULVXOWDWR H
OD VHTXHQ]D GL  H  RWWHQXWL FRVLGHUDQGR L œ UHVWL GHOOH GLYLVLRQL GDOOD PHQR VLJQLILFDWLYD DOOD SLX
VLJQLILFDWLYD (VHPSLLR &RQYHUWWLLUH LLQ IRUPD ELLQDULLD 1  (VHPS R &RQYHU UH Q IRUPD E QDU D 1  'LYLVLRQH                     TXLQGL   4XR]LHQWH            5HVWR DL         

(VHPSLLR &RQYHUWWLLUH LLQ IRUPD ELLQDULLD 1  (VHPS R &RQYHU UH Q IRUPD E QDU D 1  'LYLVLRQH             TXLQGL   4XR]LHQWH         5HVWR DL      

&RQYHUVLRQH LQ EDVH S ž  ž
(VHPSLLR &RQYHUWWLLUH LLQ IRUPD RWWWWDOOH 1  (VHPS R &RQYHU UH Q IRUPD R D H 1  'LYLVLRQH     TXLQGL   4XR]LHQWH     5HVWR DL  

(VHPSLLR &RQYHUWWLLUH LLQ IRUPD HVDGHFLLPDOOH 1  (VHPS R &RQYHU UH Q IRUPD HVDGHF PD H 1  'LYLVLRQH       TXLQGL   4XR]LHQWH   &  5HVWR DL  & 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

7DEHOOD 5LDVVXQWLYD
6LVWHPD GL QXPHUD]LRQH BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB 'HFLPDOH%LQDULR 2WWDOH (VDGHFLPDOH BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB                                            $    %    &    '    (    ) BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

&RQYHUVLLRQLL GD IRUPD ELLQDULLD D RWWDOOH HG HVDGHFLLPDOOH &RQYHUV RQ GD IRUPD E QDU D D RWWD H HG HVDGHF PD H /H UDSSUHVHQWD]LRQL RWWDOL HG HVDGHFLPDOL VRQR LQWHUHVVDQWL SHU OD IDFLOLWD
GL FRQYHUVLRQH GDOOD EDVH GXH H YLFHYHUVD ‡ 2VVHUYLDPR FKH     œ /D FRQYHUVLRQH GD EDVH  D EDVH  VL RWWLHQH VFRPSRQHQGR LO QXPHUR ELQDULR LQ WULSOH GL FLIUH ELQDULH SDUWHQGR GDOOD PHQR VLJQLILFDWLYD  H SHU RJQL WULSOD ULFDYDQGR OD FRUULVSRQGHQWH FLIUD RWWDOH             TXLQGL     œ /D FRQYHUVLRQH GD EDVH  D EDVH  VL RWWLHQH VFRPSRQHQGR LO QXPHUR ELQDULR LQ TXDGUXSOH GL FLIUH ELQDULH SDUWHQGR GDOOD PHQR VLJQLILFDWLYD  H SHU RJQL TXDGUXSOD ULFDYDQGR OD FRUULVSRQGHQWH FLIUD RWWDOH           &  TXLQGL   & 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

$ULWPHWLFD %LQDULD RSHUD]LRQL VXL QDWXUDOL
'HILLQLL]LLRQH GHOOOOH RSHUD]LLRQLL DULLWPHWLLFKH HOOHPHQWDULL 'HI Q ] RQH GH H RSHUD] RQ DU WPHW FKH H HPHQWDU 
$ % US ULSRUWR $% SUHVWLWR $%

6RPPD GLL GXH QXPHULL ELLQDULL QDWXUDOOLL 6RPPD G GXH QXPHU E QDU QDWXUD  9LHQH HVHJXLWD LQFRORQQDQGR L QXPHUL H VRPPDQGR WUD ORUR L ELW LQFRORQQDWL SDUWHQGR GDL PHQR VLJQLILFDWLYL LQ RUGLQH GL SHVR FUHVFHQWH  3HU OD VRPPD GL GXH QXPHUL SRVLWLYL GL OXQJKH]]D . SRVVRQR HVVHUH QHFHVVDUL . SRVWL 6H VRQR GLVSRQLELOL VROR . FLIUH VL JHQHUD XQ HUURUH GL RYHUIORZ R WUDERFFR  (VHPSLLR     (VHPS R             6RWWUD]LLRQH GLL QXPHULL ELLQDULL QDWXUDOOLL 6RWWUD] RQH G QXPHU E QDU QDWXUD  9LHQH HVHJXLWD LQFRORQQDQGR L QXPHUL H VRWWUDHQGR WUD ORUR L ELW LQFRORQQDWL SDUWHQGR GDL PHQR VLJQLILFDWLYL LQ RUGLQH GL SHVR FUHVFHQWH +S VL VXSSRQH FKH VL JHQHUL VHPSUH XQ QXPHUR SRVLWLYR         

                                                       US HVSULPH LO ULSRUWRSUHVWLWR GHOOD FRORQQD SUHFHGHQWH

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

0ROOWLLSOOLLFD]LLRQH GLL QXPHULL ELLQDULL QDWXUDOOLL 0R W S FD] RQH G QXPHU E QDU QDWXUD  6L XWLOL]]D OD VWHVVD WHFQLFD XVDWD DQFKH SHU L QXPHUL LQ EDVH  VRPPD H VFRUULPHQWR  $     %     $™%  ™          

'LLYLLVLLRQH GLL QXPHULL ELLQDULL QDWXUDOOLL ' Y V RQH G QXPHU E QDU QDWXUD  6L XVD OD WHFQLFD XVDWD DQFKH SHU L QXPHUL LQ EDVH  GLIIHUHQ]D H VFRUULPHQWR  $     %     &DOFRODUH $%                 _ _

,O ULVXOWDWR GHOOD RSHUD]LRQH $% HC TXLQGL    

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

(6(5&,=, (6(5&,=, (IIHWWXDUH OH VHJXHQWL RSHUD]LRQL DULWPHWLFKH WUD QXPHUL ELQDUL QDWXUDOL LSRWL]]DQGR GL ODYRUDUH FRQ XQ HODERUDWRUH FRQ OXQJKH]]D GL SDUROD ZRUG SDUL D XQ E\WH œ 'LIIHUHQ]D $% œ     $   %  

œ 0ROWLSOLFD]LRQH $™% $   %   œ          &      ™         5LVXOWDWR
œ œ  

               

3UHVWLWR  '    

     

5LVXOWDWR$  % 

 

œ 6RPPD $% $   %   œ     $     )             5LVXOWDWR$  %       

'LYLVLRQH $/% $   %                
_ _ 

        

5LVXOWDWR
)21'$0(17, ', ,1)250$7,&$
5$335(6(17$=,21( '(, 180(5, 

4XR]LHQWH      5HVWR     
5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

1XPHUL 5HODWLYL
&RQVLGHULDPR O¶LQVLHPH GHL QXPHUL UHODWLYL LQWHUL = ^Š     Š} 9RJOLDPR UDSSUHVHQWDUH JOL HOHPHQWL GL TXHVWR LQVLHPH LQ IRUPD ELQDULD $YHQGR VHPSUH XQ QXPHUR OLPLWDWR GL ELW SRVVLDPR XWLOL]]DUQH XQR SHU OD UDSSUHVHQWD]LRQH GHO VHJQR  R   5DSSUHVHQWD]LLRQH FRQ PRGXOOR H VHJQR 5DSSUHVHQWD] RQH FRQ PRGX R H VHJQR ,O SULPR ELW GL XQ QXPHUR LQWHUR YLHQH XWLOL]]DWR FRPH ELW GL VHJQR  SRVLWLYR  QHJDWLYR  *OL DOWUL ELW LQGLFDQR LO PRGXOR YDORUH DVVROXWR GHO QXPHUR $G HVHPSLLR $G HVHPS R ‡ SDUROH GL  ELW     ‡ SDUROH D  ELW    

5DSSUHVHQWD]LRQH FRQ PRGXOR H VHJQR
œ 7UDPLWH P FLIUH LQ EDVH  H
SRVVLELOH UDSSUHVHQWDUH P QXPHUL GLYHUVL GD  P   D  P   $G HVHPSLLR $G HVHPS R P P P    GD       D       GD  D  GD  D   

$EELDPR GXH UDSSUHVHQWD]LRQL GLYHUVH SHU OR ]HUR   $G HVHPSLLR $G HVHPS R P  ! SRVVR UDSSUHVHQWDUH   9DORUH 'HFLPDOH      QXPHUL  

9DORUH %LQDULR    

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

5DSSUHVHQWD]LRQH LQ &RPSOHPHQWR
3HU VHPSOLILFDUH OH RSHUD]LRQL VX LQWHUL FRQ VHJQR VL DGRWWD XQD UDSSUHVHQWD]LRQH GHL QXPHUL QHJDWLYL LQ FRPSOHPHQWR &RPSOOHPHQWR DOOOOD EDVH &RPS HPHQWR D D EDVH GDWR XQ QXPHUR ; LQ EDVH E GL Q FLIUH LO FRPSOHPHQWR DOOD EDVH HC GHILQLWR FRPH EQ  ; (VHPSLL (VHPS  ‡ Q  E  ;  ,O FRPSOHPHQWR D  GL  HC    ‡ Q  E  ;  ,O FRPSOHPHQWR D  GL ; HC   ;   

&RPSOOHPHQWR DOOOOD EDVH  &RPS HPHQWR D D EDVH  GDWR XQ QXPHUR ; LQ EDVH E GL Q FLIUH LO FRPSOHPHQWR DOOD EDVH  HC GHILQLWR FRPH EQ ; (VHPSLLR (VHPS R ‡ Q  E  ;  ,O FRPSOHPHQWR D  GL ; HC Q   ;     

2VVHUYLDPR FKH LO ULVXOWDWR HC OD VHTXHQ]D GL FLIUH FKH VL RWWLHQH FRPSOHPHQWDQGR D  RJQL VLQJROR ELW 5LLDVVXPHQGR 5 DVVXPHQGR 6XSSRQLDPR GL YROHU UDSSUHVHQWDUH XQ QXPHUR ; WUDPLWH Q FLIUH ELQDULH ‡ OD UDSSUHVHQWD]LRQH LQ FRPSOHPHQWR D  VL RWWLHQH VRWWUDHQGR ; GD Q ‡ OD UDSSUHVHQWD]LRQH LQ FRPSOHPHQWR D  VL RWWLHQH VRWWUDHQGR ; GD Q 

,Q SUDWLFD LO SULQFLSLR HC FKH OD VRPPD GHO QXPHUR SLXC LO VXR FRPSOHPHQWR GLD XQD VWULQJD GL Q FLIUH D ]HUR

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

,Q 3UDWLLFD ,Q 3UDW FD ‡ ,O FRPSOHPHQWR D  GL XQ QXPHUR ELQDULR ; UDSSUHVHQWDWR FRPH LQWHUR SRVLWLYR VL RWWLHQH FRPSOHPHQWDQGR WXWWL L ELW FLRH
VFDPELDQGR  FRQ  H YLFHYHUVD  ‡ ,O FRPSOHPHQWR D  VL RWWLHQH SULPD FDOFRODQGR LO FRPSOHPHQWR D  H SRL VRPPDQGRYL  ,QIDWWL VL SXRC VFULYHUH O
RSHUD]LRQH FRPH Q  ; FRPH Q    ; 

5DSSUHVHQWD]LRQH LQ &RPSOHPHQWR
(VHPSLLR Q  (VHPS R Q  ,QWHUR         0RGXOR         6HJQR  PRGXOR         &RPSO D          &RPSO D         

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

œ 5DSSUHVHQWD]LRQH LQ FRPSOHPHQWR D  , QXPHUL SRVLWLYL œ VRQR UDSSUHVHQWDWL GDO ORUR PRGXOR HG KDQQR LO ELW SLXC VLJQLILFDWLYR D ]HUR , QXPHUL QHJDWLYL VL RWWHQJRQR FRPSOHPHQWDQGR D  L FRUULVSRQGHQWL PRGXOL VHJQR FRPSUHVR 3HUWDQWR KDQQR LO SULPR ELW VHPSUH D  œ 5DSSUHVHQWD]LRQH LQ FRPSOHPHQWR D  , QXPHUL SRVLWLYL œ VRQR UDSSUHVHQWDWL GDO ORUR PRGXOR HG KDQQR LO ELW SLXC VLJQLILFDWLYR D ]HUR , QXPHUL QHJDWLYL VL RWWHQJRQR FRPSOHPHQWDQGR D  L FRUULVSRQGHQWL PRGXOL VHJQR FRPSUHVR 3HUWDQWR KDQQR LO ELW GHO VHJQR VHPSUH D  ,Q TXHVWR FDVR VL KD XQD VROD UDSSUHVHQWD]LRQH GHOOR ]HUR   œ 6X  ELW VL SRVVRQR UDSSUHVHQWDUH L QXPHUL GD   D    œ ,O QXPHUR SLXC SLFFROR UDSSUHVHQWDELOH  QRQ KD LO FRUULVSHWWLYR SRVLWLYR SHU UDSSUHVHQWDUH   RFFRUUHUHEEHUR  ELW 

(VHUFLL]LL (VHUF ]  œ 6L UDSSUHVHQWLQR L VHJXHQWL QXPHUL LQ FRPSOHPHQWR D  œ DYHQGR  ELW D GLVSRVL]LRQH   à  à    à    à   à 0RGXOR &RPSOD    à    à    à    à   à  à à à à à à à à &RPSOD                

œ ,QWHUSUHWDUH OD VHTXHQ]D GL ELW     FRPH œ D QXPHUR QDWXUDOH E QXPHUR UHODWLYR LQ PRGXOR H VHJQR F QXPHUR UHODWLYR LQ FRPSOHPHQWR D GXH D ,QWHUSUHWDQGR OD VHTXHQ]D FRPH QXPHUR QDWXUDOH     ™     &  ™    ™   

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

E ,QWHUSUHWDQGR OD VHTXHQ]D FRPH QXPHUR UHODWLYR LQ PRGXOR H VHJQR        ™       &   ™    ™ 

2SHUD]LRQL DULWPHWLFKH VXL QXPHUL UHODWLYL
6RPPD DOOJHEULLFD 6RPPD D JHEU FD 6IUXWWD OD UDSSUHVHQWD]LRQH LQ FRPSOHPHQWR D  6L VRPPDQR L QXPHUL SHU FRORQQD LQFOXVR LO ELW GHO VHJQR LJQRUDQGR O¶HYHQWXDOH ULSRUWR VXO ELW GHO VHJQR (VHPSLL (VHPS 

F ,QWHUSUHWDQGR OD VHTXHQ]D FRPH QXPHUR UHODWLYR LQ FRPSOHPHQWR D GXH     ‡ ULFDYLDPR LO FRPSOHPHQWR D            ‡ FRPSOHPHQWLDPR L ELW       (%+   ™    ™    ™      

                    

                   

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

œ &RQVLGHULDPR  ELW LO QXPHUR H
FRPSUHVR IUD  H œ            

(VHUFLL]LLR (VHUF ] R (IIHWWXDUH OH VHJXHQWL RSHUD]LRQL VXSSRQHQGR GL RSHUDUH FRQ XQD UDSSUHVHQWD]LRQH GHL QXPHUL VX XQ E\WH H LQ FRPSOHPHQWR D GXH       6ROOX]LLRQH 6R X] RQH        

29(5)/2: 

  FLRHC     LQ FRPSOHPHQWR D        

29(5)/2: 

6L SXRC YHULILFDUH RYHUIORZ VROR TXDQGR JOL RSHUDQGL KDQQR OR VWHVVR VHJQR O¶RSHUD]LRQH SURGXFH XQ QXPHUR GL VHJQR RSSRVWR  6RWWUD]LLRQH WUD QXPHULL UHOODWLLYLL 6RWWUD] RQH WUD QXPHU UH DW Y  6H L QXPHUL VRQR UDSSUHVHQWDWL LQ FRPSOHPHQWR D  O
RSHUD]LRQH GL VRWWUD]LRQH VL HIIHWWXD PHGLDQWH VRPPD YDQWDJJLR GHOOD UDSSUHVHQWD]LRQH LQ FRPSOHPHQWR D  

, YDORUL LQGLFDWL ULVXOWDQR FRVu UDSSUHVHQWDWL                            'D FXL                     
5$335(6(17$=,21( '(, 180(5,  



)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$ 

 

      

0ROOWLLSOOLLFD]LLRQH H GLLYLLVLLRQH WUD QXPHULL UHOODWLLYLL 0R W S FD] RQH H G Y V RQH WUD QXPHU UH DW Y       œ 8WLOL]]DUH L QXPHUL LQ YDORUH DVVROXWR HG XWLOL]]DUH OH œ RSHUD]LRQL YLVWH VXL QXPHUL QDWXUDOL VRPPDGLIIHUHQ]D H VFRUULPHQWR  œ ,O VHJQR VL GHWHUPLQD LQ EDVH DO VHJQR GHJOL RSHUDQGL œ     /
DSSOLFD]LRQH GHOOH RSHUD]LRQL GLUHWWDPHQWH VXL QXPHUL LQ FRPSOHPHQWR H
LQ JHQHUDOH VFRUUHWWD ,QIDWWL  ™  

 

        Ã 29(5)/2:   

         Ã 29(5)/2:

, YDORUL LQGLFDWL ULVXOWDQR FRVu UDSSUHVHQWDWL FRQ  ELW LQ FRPSOHPHQWR D        ,O ULVXOWDWR GHO FDOFROR  QRQ q UDSSUHVHQWDELOH VX VROL  ELW 6XSSRUUHPR GLVSRQLELOH DOOR VFRSR XQD SDUROD GL XQ E\WH  ™         FKH VX XQ E\WH q LQWHUSUHWDELOH FRPH QXPHUR SRVLWLYR GL YDORUH  

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5,

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

2SHUD]LRQH GL VKLIW
6KLLIW YHUVR VLLQLLVWUD 6K IW YHUVR V Q VWUD ‡ 'DOOD UDSSUHVHQWD]LRQH GHL QXPHUL GLVFHQGH LPPHGLDWDPHQWH FKH OR VFRUULPHQWR YHUVR VLQLVWUD GL WXWWH OH FLIUH GHO QXPHUR GL XQD SRVL]LRQH FRQ O
LQVHULPHQWR GL XQR ]HUR QHOOD SRVL]LRQH GL GHVWUD HTXLYDOH D PROWLSOLFDUH LO QXPHUR SHU OD EDVH  /R VFRUULPHQWR GL N SRVL]LRQL YHUVR VLQLVWUD HTXLYDOH D PROWLSOLFDUH LO QXPHUR SHU EN 6KLLIW YHUVR GHVWUD 6K IW YHUVR GHVWUD ‡ /R VFRUULPHQWR VKLIW YHUVR GHVWUD GL WXWWH OH FLIUH GHO QXPHUR GL XQD SRVL]LRQH FRQ O
LQVHULPHQWR GL XQR ]HUR QHOOD SRVL]LRQH GL VLQLVWUD HTXLYDOH D GLYLGHUH LO QXPHUR SHU OD EDVH FLRHC D PROWLSOLFDUH LO QXPHUR SHU E   /R VFRUULPHQWR GL N SRVL]LRQL YHUVR GHVWUD HTXLYDOH D PROWLSOLFDUH LO QXPHUR SHU EN

1XPHUL )UD]LRQDUL
6RQR QXPHUL UHDOL FRPSUHVL IUD ]HUR H  5DSSUHVHQWD]LLRQH GHLL QXPHULL IUD]LLRQDULL 5DSSUHVHQWD] RQH GH QXPHU IUD] RQDU  6LD a=^S` E S 'DWR XQ QXPHUR IUD]LRQDULR 1 OD VXD UDSSUHVHQWD]LRQH LQ EDVH S HC GDWD GD XQD VHTXHQ]D GL FLIUH  D D D  DQ GRYH DL ³ a , " L Q ,O YDORUH GL 1 HC GDWR GDOOD IRUPXOD 1S D  SD SDQSQ

R LQ IRUPD SLXC FRPSDWWD 1S ÊL 

Q

DL  SL

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

$G HVHPSLLR $G HVHPS R ‡ %DVH GHFLPDOH S  1     ™    ™    ™ 103. ‡ %LQDULR S  1 

&RQYHUVLRQH GL XQ QXPHUR GHFLPDOH IUD]LRQDULR LQ EDVH ž 
1S D  SD SDQSQ  

™™™™   

‡ 0ROWLSOLFR 1S SHU S 1S S D D SDSQ

,O ULVXOWDWR HC XQ QXPHUR UHDOH ! ‡ 3DUWH LQWHUD 1S ™ S D ‡ 3DUWH IUD]LRQDULD 1S ™ S D ™ S DQ ™ SQ

0

‡ $SSOLFKLDPR DQFRUD OD PROWLSOLFD]LRQH SHU S VXOOD SDUWH IUD]LRQDULD 0 RWWHQXWD QHOOD GLYLVLRQH SUHFHGHQWH ‡ 3DUWH LQWHUD 0 ™ S D ‡ 3DUWH IUD]LRQDULD 0 ™ S D ™ S DQ ™ SQ 0 ‡ 5LSHWLDPR LO SURFHGLPHQWR VX 0 0 HWF SHU RWWHQHUH OH FLIUH ULPDQHQWL D D  DQ 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5,  

,Q SUDWLFD LO SURFHGLPHQWR GD VHJXLUH HC 6LD 1 LO QXPHUR IUD]LRQDULR  6L PROWLSOLFD 1 SHU OD QXRYD EDVH VLD , OD SDUWH LQWHUD H 0 OD SDUWH IUD]LRQDULD  6L FRQYHUWH , QHOOD FRUULVSRQGHQWH FLIUD GHOOD QXRYD EDVH  6L DJJLXQJH OD FLIUD FRVL
RWWHQXWD D GHVWUD GHOOH FLIUH RWWHQXWH LQ SUHFHGHQ]D OD SULPD FLIUD LPPHGLDWDPHQWH D GHVWUD GHOOD YLUJROD  6H )  RSSXUH VL VRQR RWWHQXWH OH FLIUH ULFKLHVWH ILQH $OWULPHQWL SRQL 1 ) H WRUQD DO SDVVR 

&RQYHUVLRQH ELQDULD GL QXPHUL IUD]LRQDUL
œ 6L RWWLHQH HIIHWWXDQGR VXFFHVVLYH PROWLSOLFD]LRQL SHU GXH œ œ ,O ULVXOWDWR H
OD VHTXHQ]D GL ]HUL H XQL RWWHQXWL FRVLGHUDQGR œ OH SDUWL LQWHUH GHOOH PROWLSOLFD]LRQL GDOOD SLX
VLJQLILFDWLYD DOOD PHQR VLJQLILFDWLYD

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

$G (VHPSLLR $G (VHPS R ‡ &RQYHUWLUH LQ EDVH 
0ROWLSOLFD]LRQH

1  
 
3DUWH ,QWHUD DL

&RQYHUVLRQH GL QXPHUL IUD]LRQDUL LQ EDVH QRQ GHFLPDOH
œ 1RQ VHPSUH VL RWWLHQH XQD FRQYHUVLRQH HVDWWD DG HVHPSLR FRQYHUWLDPR   LQ ELQDULR
0ROWLSOLFD]LRQH 3DUWH )UD]LRQDULD

3DUWH )UD]LRQDULD 

™   ™   ™  

D  D  D

TXLQGL    

1  

3DUWH ,QWHUD DL

‡ &RQYHUWLUH LQ EDVH 
0ROWLSOLFD]LRQH

3DUWH )UD]LRQDULD 

™   

™   ™   ™   ™   ™   

    

3DUWH ,QWHUD DL 

    

TXLQGL    

1  

3DUWH ,QWHUD DL

TXLQGL    

SHULRGLFR

‡ &RQYHUWLUH LQ EDVH 
0ROWLSOLFD]LRQH

3DUWH )UD]LRQDULD

‡ 4XLQGL XQR VWHVVR QXPHUR SXRC DYHUH XQ QXPHUR ILQLWR GL FLIUH LQ XQD EDVH HG XQ QXPHUR LQILQLWR LQ XQ
DOWUD ‡ 4XLQGL QHOOD UDSSUHVHQWD]LRQH LQWHUQD VL SXRC LQWURGXUUH XQ HUURUH GL WURQFDPHQWR 

™  

(

TXLQGL   

( 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

5DSSUHVHQWD]LRQH GHL 1XPHUL 5HDOL
&RQVLGHULDPR O¶LQVLHPH 5 GHL QXPHUL UHDOL 2JQL HOHPHQWR GL 5 LQ JHQHUDOH SXRC HVVHUH HVHSUHVVR FRPH VRPPD GL XQ LQWHUR FRQ XQ QXPHUR IUD]LRQDULR ,Q SUDWLFD XQ QXPHUR UHDOH HC XQLYRFDPHQWH GD ‡ XQD SDUWH LQWHUD , ‡ XQD SDUWH IUD]LRQDULD ) LQGLYLGXDWR

$G HVHPSLLR $G HVHPS R   ULVHUYDQGR  ELW SHU OD SDUWH LQWHUD H  SHU TXHOOD IUD]LRQDULD   /D SUHFLVLRQH HC YDULDELOH H SXRC HVVHUH VFDUVD SHU QXPHUL GL YDORUH SURVVLPR DOOR ]HUR

5DSSUHVHQWD]LLRQH GHLL UHDOOLL LLQ YLLUJROOD ILLVVD 5DSSUHVHQWD] RQH GH UHD Q Y UJR D I VVD 8Q QXPHUR SUHILVVDWR GL FLIUH YLHQH GHGLFDWR DOOD SDUWH LQWHUD HG D TXHOOD IUD]LRQDULD UDSSUHVHQWD]LRQH LQ YLUJROD ILVVD  4XLQGL ‡ 6L FDOFROD OD UDSSUHVHQWD]LRQH GHOOD SDUWH LQWHUD QHOOD EDVH GDWD FRQ OD IRUPXOD 1S DQ  SQDQ SQDSD

‡ 6L FDOFROD OD UDSSUHVHQWD]LRQH GHOOD SDUWH IUD]LRQDULD QHOOD EDVH GDWD WHQHQGR FRQWR GHO QXPHUR GL FLIUH GLVSRQLELOL FRQ OD IRUPXOD 1S D  SD SDQSQ

‡ 6L JLXVWDSSRQJRQR OH GXH UDSSUHVHQWD]LRQL UDSSUHVHQWD]LRQH ,!  UDSSUHVHQWD]LRQH )!
)21'$0(17, ', ,1)250$7,&$
5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

5DSSUHVHQWD]LRQH GHL QXPHUL UHDOL LQ YLUJROD PRELOH
‡ $G XQ QXPHUR UHDOH U YHQJRQR DVVRFLDWL GXH QXPHUL ‡ P PDQWLVVD ‡ Q HVSRQHQWH R FDUDWWHULVWLFD 0DQWLLVVD 0DQW VVD ‡ HC XQ QXPHUR IUD]LRQDULR FRQ VHJQR ,O VXR YDORUH HC TXLQGL FRPSUHVR QHOO¶LQWHUYDOOR @ > ‡ PDQWLVVD QRUPDOL]]DWD VH OD SULPD FLIUD GRSR OD YLUJROD HC GLYHUVD GD  ,O YDORUH DVVROXWR GHOOD PDQWLVVD LQ TXHVWR FDVR ³ >S > (VSRQHQWH (VSRQHQWH ‡ HC XQ LQWHUR FRQ VHJQR  OD PDQWLVVD H O¶HVSRQHQWH VRQR OHJDWL GDOOD UHOD]LRQH U P ™ EQ ‡

5DSSUHVHQWD]LRQH LQ YLUJROD PRELOH
(VHPSLL (VHPS  S E  U 

U VL UDSSUHVHQWD FRQ P  H Q  ‡ &RQYHUVLRQH LQ ELQDULR YLUJROD PRELOH S E   U   P  Q  

‡ &RQYHUVLRQH LQ ELQDULR YLUJROD PRELOH S E   U    Q 

P  

GRYH E H
XQ QXPHUR LQWHUR FKH LQGLFD XQD EDVH XWLOL]]DWD SHU OD QRWD]LRQH HVSRQHQ]LDOH LQ JHQHUDOH VH S HC OD EDVH GHO VLVWHPD GL QXPHUD]LRQH S ž E 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

5DSSUHVHQWD]LLRQH ELLQDULLD LLQ YLLUJROOD PRELLOOH 5DSSUHVHQWD] RQH E QDU D Q Y UJR D PRE H 3HU RJQL QXPHUR UHDOH YHQJRQR XWLOL]]DWL Q ELW
segno esponente n n-1 n-2 l mantissa (cifre a destra della virgola) 1

5DSSUHVHQWD]LRQH ELQDULD LQ YLUJROD PRELOH
‡ SRLFKH
OD PDQWLVVD FRPLQFLD VHPSUH FRQ  XWLOL]]DUH TXHVWR FRPH ELW GL VHJQR VL SXR

segno numero

valore assoluto esponente

$G HVHPSLLR $G HVHPS R 6XSSRQLDPR GL DYHUH D GLVSRVL]LRQH  ELW GL FXL ‡  ELW SHU LO VHJQR GHOOD PDQWLVVD ‡  ELW SHU LO VHJQR GHOO
HVSRQHQWH ‡  ELW SHU LO YDORUH DVVROXWR GHOO
HVSRQHQWH ‡  ELW SHU OD PDQWLVVD à 5DSSUHVHQWD]LRQH LQ YLUJROD PRELOH GL U  U  ™  HVSRQHQWH PDQWLVVD  _  _  _  à 5DSSUHVHQWD]LRQH LQ YLUJROD PRELOH GL U  U  ™  HVSRQHQWH PDQWLVVD  _  _  _ 

‡ LQYHFH GL ULVHUYDUH XQ ELW SHU LO VHJQR GHOO
HVSRQHQWH LQ DOFXQL FDVL VL DGRWWD OD UDSSUHVHQWD]LRQH LQ FRPSOHPHQWR D  GHOO
HVSRQHQWH (VHPSLLR FRPSOOHPHQWWR D  SHU OO

HVSRQHQWWH (VHPS R FRPS HPHQ R D  SHU HVSRQHQ H  ELW  ELW VHJQR PDQWLVVD  ELW YDORUH GHOO
HVSRQHQWH LQ FRPSO D    ELW PDQWLVVD  ™ 

‡ U 

HVSRQHQWH PDQWLVVD  _  _  ‡ U   ™  

_ _ 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

(6(5&,=,2 (6(5&,=,2 5DSSUHVHQWDUH VX  ELW GL FXL  GL HVSRQHQWH LQ FRPSOHPHQWR D GXH XQR SHU LO VHJQR H  GL PDQWLVVD L VHJXHQWL YDORUL   

E U  3DUWH LQWHUD  3DUWH IUD]LRQDULD VL PROWLSOLFD]LRQL VXFFHVVLYH XVD O
DOJRULWPR GHOOH

6ROOX]LLRQH 6R X] RQH D U  3DUWH LQWHUD  3DUWH IUD]LRQDULD DOJRULWPR GHOOH PROWLSOLFD]LRQL VXFFHVVLYH 6L KD  ™   ™   ™    à )  à )  à )  ,  ,  ,    

™   à )  ,   ™   à )  ,   ™   à )  ,   ™   à )  ,   ™   à )  ,   à UDSSUHVHQWD]LRQH DSSURVVLPDWD SHUFKHC VL RWWLHQH XQ QXPHUR SHULRGLFR  à    ‡ 1RUPDOL]]DQGR PDQWLVVD    HVSRQHQWH      HVSRQHQWH  VHJQR PDQWLVVD _  _ 

,O YDORUH ULFKLHVWR q GXQTXH SDUWH,QWHUD!SDUWH)UD]LRQDULD!  Ã 1RQ q QHFHVVDULD DOFXQD QRUPDOL]]D]LRQH 5DSSUHVHQWD]LRQH ILQDOH GL  HVSRQHQWH VHJQR PDQWLVVD  _  _ 

6L QRWL FKH OD PDQWLVVD YLHQH WURQFDWD DO QXPHUR GLVSRQLELOH GL ELW VROR GRSR DYHU QRUPDOL]]DWR œ &
HC GL XQ HUURUH GL WURQFDPHQWR LO QXPHUR UHDOPHQWH UDSSUHVHQWDWR QRQ q  PD  ™    

 ™ 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5,

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

F U  3DUWH LQWHUD    3DUWH IUD]LRQDULD  FRQ O
DOJRULWPR PROWLSOLFD]LRQL VXFFHVVLYH  ™   à )  ,   ™   à )  ,   ™   à )  ,   ™   à )  ,   ™   à )  ,   ™   à )  ,   TXLQGL U   1RUPDOL]]DQGR P   HVSRQHQWH   HVSRQHQWH  VHJQR PDQWLVVD _  _ 

3UHFLLVLLRQH QHOOOOD UDSSUHVHQWD]LLRQH GHLL QXPHULL UHDOOLL 3UHF V RQH QH D UDSSUHVHQWD] RQH GH QXPHU UHD GHOOH 6L SXR¶ LQ JHQHUDOH RVVHUYDUH FKH œ 4XDQWR PDJJLRUH HC LO QXPHUR GL ELW ULVHUYDWL DOOD PDQWLVVD œ WDQWR PDJJLRUH H¶ LO QXPHUR GL FLIUH VLJQLILFDWLYH FKH SRVVRQR HVVHUH PHPRUL]]DWH SUHFLVLRQH  œ 4XDQWR PDJJLRUH HC LO QXPHUR GL ELW ULVHUYDWL DOO¶ HVSRQHQWH œ WDQWR PDJJLRUH HC O¶RUGLQH GL JUDQGH]]D GHOOD FLIUD FKH SXR¶ HVVHUH UDSSUHVHQWDWD 3UHFLLVLLRQH 3UHF V RQH HC GDWD GDO QXPHUR GL FLIUH LQ EDVH  UDSSUHVHQWDELOL FRQ OD PDQWLVVD $G HVHPSLLR $G HVHPS R 6H OD PDQWLVVD H
UDSSUHVHQWDWD GD  ELW OD SUHFLVLRQH H
GL  FLIUH PD[ YDORUH UDSSUHVHQWDELOH GDOOD PDQWLVVD  FLUFD    œ /H FLIUH PHQR VLJQLILFDWLYH GHOOD PDQWLVVD FKH QRQ SRVVRQR HVVHUH UDSSUHVHQWDWH QHO QXPHUR GL ELW D GLVSRVL]LRQH YHQJRQR HOLPLQDWH PHGLDQWH WURQFDPHQWR RG DUURWRQGDPHQWR

9DOXWLDPR O
HUURUH LO QXPHUR UHDOPHQWH UDSSUHVHQWDWR YDOH  ™   ™   ™  

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

7URQFDPHQWR H DUURWRQGDPHQWR 7URQFDPHQWR H DUURWRQGDPHQWR (VHPSLLR (VHPS R U  FRQ  ™  ‡  ELW SHU OD PDQWLVVD ‡  ELW SHU O
HVSRQHQWH

9DORUL PDVVLPL H PLQLPL UDSSUHVHQWDELOL
'LSHQGRQR GDOO
HVSRQHQWH UDSSUHVHQWD]LRQH H GDO PHWRGR GL

6H LO YDORUH DVVROXWR GHOO
HVSRQHQWH H
UDSSUHVHQWDWR GD  ELW ‡ LO PDVVLPR QXPHUR UHDOH H
LQ YDORUH DVVROXWR   ™  ~  ‡ LO PLQLPR QXPHUR UHDOH H
LQ YDORUH DVVROXWR   ™  ~  ™   $FFXUDWH]]D GHOOOOD PDFFKLLQD $FFXUDWH]]D GH D PDFFK QD (¶ LO SLX¶ SLFFROR QXPHUR FKH DJJLXQWR DG  SURGXFH XQ ULVXOWDWR GLYHUVR GD  WLSLFDPHQWH SHU Q  H¶ FLUFD  ™  

5DSSUHVHQWD]LRQH ‡ FRQ WURQFDPHQWR     ‡ FRQ DUURWRQGDPHQWR     (VHPSLLR (VHPS R U  FRQ  ™  SHULRGLFR ‡  ELW SHU OD PDQWLVVD ‡  ELW SHU O
HVSRQHQWH

5DSSUHVHQWD]LRQH ‡ FRQ WURQFDPHQWR H FRQ DUURWRQGDPHQWR     ~

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

2SHUD]LRQL DULWPHWLFKH FRQ QXPHUL UHDOL
6RPPD 6L RSHUD QHO PRGR VHJXHQWH  VL DXPHQWD O¶HVSRQHQWH GHO SLX¶ SLFFROR FRQWHPSRUDQHDPHQWH VSRVWDQGR OD PDQWLVVD D GHVWUD ILQR D FKH L GXH HVSRQHQWL VRQR XJXDOL  VL VRPPDQR OH GXH PDQWLVVH LQVLHPH  VL QRUPDOL]]D LO ULVXOWDWR ,Q TXHVWR FDVR VL SRVVRQR JHQHUDUH HUURUL (UURUH GLL LLQFROORQQDPHQWR (UURUH G QFR RQQDPHQWR 6L PDQLIHVWD QHO VRPPDUH QXPHUL FRQ HVSRQHQWL GLYHUVL

$G HVHPSLLR $G HVHPS R 6XSSRQLDPR GL ODYRUDUH LQ EDVH  FRQ  ELW SHU OD PDQWLVVD QRUPDOL]]DWD  ™    ™   ™    ™  ULSRUWDQGR WXWWR DOO
HVSRQHQWH PDJJLRUH VL SHUGRQR OH XOWLPH  FLIUH GHO VHFRQGR DGGHQGR FLRHC OD TXDQWLWDC  ™  

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

(UURUH GLL FDQFHOOOOD]LLRQH (UURUH G FDQFH D] RQH 6L PDQLIHVWD QHO VRWWUDUUH QXPHUL PROWR VLPLOL IUD ORUR $G HVHPSLLR $G HVHPS R 6XSSRQLDPR GL ODYRUDUH LQ EDVH  FRQ  ELW SHU OD PDQWLVVD QRUPDOL]]DWD 6L FRQVLGHUL OD VHJXHQWH HVSUHVVLRQH    &KH SURGXFH LO YDORUH  ™  1HO FDOFRODWRUH YLHQH YDOXWDWD ™ ; ™ ; ;  ; ™ 1RUPDOL]]DQGR ;  ; ""™ /
HODERUDWRUH LQWURGXFH  DO SRVWR GL "" SURGXFHQGR ™  /H XOWLPH GXH FLIUH QRQ KDQQR SHURC VLJQLILFDWR LO QXPHUR HC DIIHWWR GD HUURUH  œ 4XHVWL HUURUL IDQQR VLC FKH OD VRPPD QRQ JRGD VHPSUH GHOOD SURSULHWDC DVVRFLDWLYD

(VHPSLLR (VHPS R $  ™  %  ™  &  ™  œ 9DOXWLDPR $  %& œ %&  ™    ™  HUURUH GL LQFRORQQDPHQWR   ™   $  ™    ™  1RUPDOL]]D]LRQH  ™  HUURUH GL FDQFHOOD]LRQH œ 9DOXWLDPR $  % & œ $% %  ™   $  ™    ™  1RUPDOL]]D]LRQH  ™   HUURUH GL FDQFHOOD]LRQH &  ™    ™ 
)21'$0(17, ', ,1)250$7,&$
5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

(6(5&,=,2 (IIHWWXDUH OD VRPPD    FRQ QXPHUL UDSSUHVHQWDWL LQ IORDWLQJ SRLQW VX  ELW GL FXL  GL HVSRQHQWH LQ FRPSOHPHQWR D GXH XQR SHU LO VHJQR H  GL PDQWLVVD  YHULILFDQGR L ULVXOWDWL RWWHQXWL 6ROX]LRQH        ™    ™ 

(6(5&,=,2 (IIHWWXDUH OD VHJXHQWH PROWLSOLFD]LRQH IUD QXPHUL UDSSUHVHQWDWL LQ IORDWLQJ SRLQW VX  ELW GL FXL  GL HVSRQHQWH LQ FRPSOHPHQWR D GXH XQR SHU LO VHJQR H  GL PDQWLVVD  YHULILFDQGR LO ULVXOWDWR RWWHQXWR 6ROX]LRQH 6L KD    ™      ™     ™ 

$OOLQHDQGR L QXPHUL LQ PRGR FKH DEELDQR LGHQWLFR HVSRQHQWH    ™      ™  ££££££££££££££££££   ™       9DORUH DWWHVR SHU LO ULVXOWDWR 

3HU PROWLSOLFDUH L GXH YDORUL q VXIILFLHQWH PROWLSOLFDUH OH PDQWLVVH H VRPPDUH JOL HVSRQHQWL 3RLFKp SHUz LO SURGRWWR GL GXH QXPHUL GL  ELW ULFKLHGH LQ JHQHUDOH  ELW SHU HVVHUH HVDWWDPHQWH UDSSUHVHQWDWR PHQWUH DQFKH OD PDQWLVVD ULVXOWDQWH GRYUj HVVHUH UDSSUHVHQWDWD LQ  ELW HYLGHQWHPHQWH L SL VLJQLILFDWLYL  O
RSHUD]LRQH SRWUj GDUH OXRJR DG DSSURVVLPD]LRQL

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5,  

 ™   £££££            ££££££££££    ,VRODQGR L  ELW SL VLJQLILFDWLYL VL KD 5   ™    ™  5LVXOWDWR WHRULFR  

&RGLILFD GHL FDUDWWHUL
, FDUDWWHUL GL XQ WHVWR YHQJRQR FRGLILFDWL WUDPLWH VHTXHQ]H GL ELW XWLOL]]DQGR XQ FRGLFH GL WUDGX]LRQH 4XHOOR SLX
XVDWR H
LO FRGLFH $6&,, $PHULFDQ 6WDQGDUG &RGH IRU ,QIRUPDWLRQ ,QWHUFKDQJH  8WLOL]]D  ELW 5DSSUHVHQWD  FDUDWWHUL 0DQFDQR DG HVHPSLR L FDUDWWHUL DFFHQWDWL JUHFL HWF (VHPSLR  H¶ FRGLILFDWR FRPH  H¶ FRGLILFDWR FRPH D H¶ FRGLILFDWR FRPH ] H¶ FRGLILFDWR FRPH  H  H    H   H   H $

œ 9HQJRQR LQVHULWL LQ XQ E\WH SHU FXL  ELW YLHQH œ QRUPDOPHQWH LJQRUDWR ,Q WUDVPLVVLRQH IXQ]LRQD FRPH ELW GL SDULWD
SHU LQGLYLGXDUH HUURUL œ ,O YDORUH  R  GHO ELW GL SDULWDC HC VFHOWR LQ PRGR œ FKH OD VHTXHQ]D GL  VLD SDUL $G HVHPSLR  !  !    ,O FRGLFH $6&,, HVWHVR XWLOL]]D LQYHFH  ELW  FDUDWWHUL

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

7DEHOOD GHL &RGLFL $6&,,
_ _ _ _ _ _ _ _ _ _ _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _18/ _62+ _67; _ (7; _ (27 _ (14 _$&. _ %(/ _ %6 _ +7 _ /) _ 97 _ )) _ &5 _ 62 _ 6, _ '/( _ '& _ '& _ '& _ '& _ 1DN _ 6<1 _ (7% _ &DQ _ (0 _ 68% _ (6& _ )6 _ *6 _ 56 _ 86 _ _ _ _ _ _ _ _
_ _ __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _! _" _# _$ _% _& _' _( _) _* _+ _, __. _/ _0 _1 _2 _3 _4 _5 _6 __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  _7 _8 _9 _: _; _< _= _> _? _@ _A _B _C _D _E _F _G _H _I _J _K _L _M _N _O _P _Q _R _S _T _U _V _W _X _Y _Z _[ _\ _] _^ __ _` __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  _a _ _b _c _d _e _f _g _h _i _j _k _l _m _n _o _p _q _r _s _t _u _v _w _x _y _z _{ _| _} _~ _ _€ _ _‚ _ƒ _„ _… _† _‡ _ˆ _‰ __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  _Š _‹ _Œ _ _Ž _ _ _‘ _’ _“ _” _• _– _— _˜ _™ _š _ _œ _ _ž _Ÿ _  _¡ _¢ _£ _ _¥ _¦ _§ _¨ _© _ª _« _ _¬ _­ _® _¯ _° _± _² __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  _³ _´ _µ _¶ _· _¸ _¹ _º _» _¼ _½ _¾ _¿ _À _Á _ _à _Ä _Å _Æ _Ç _È _É _Ê _Ë _Ì _Í _Î _Ï _Ð _ _Ñ _Ò _Ó _Ô _Õ _Ö _× _ _Ù _Ú _Û __  __  __  __  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _Ü _Ý _Þ _ß _ _ _ _

5DSSUHVHQWD]LRQL %LQDULH
5LLDVVXPHQGR 5 DVVXPHQGR ‡  FRPH LQWHUR FRQ Q                        ELW  E\WHV 

‡  

$OWUL FRGLFL DG HVHPSLR VRQR (%&',& ([WHQGHG %LQDU\ &RGHG 'HFLPDO ,QWHUFKDQJH &RGH  ELW SHU HODERUDWRUL ,%0 HWF
)21'$0(17, ', ,1)250$7,&$
5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

‡  FRPH UHDOH LQ YLUJROD PRELOH ‡  E\WH SHU O
HVSRQHQWH ‡  E\WHV SHU OD PDQWLVVD     1RUPDOL]]DWR ™ (VSRQHQWH      ,O SULPR ELW H
LO VHJQR ,O SULPR ELW GHOOD SDUWH IUD]LRQDULD H
RPHVVR SHUFKH
HVVHQGR QRUPDOL]]DWD H
VHPSUH  ‡  FRPH UHDOH LQ YLUJROD PRELOH   ‡ 

FRPH FDUDWWHUH FRQ FRGLILFD DVFLL      ‡ 

FRPH FDUDWWHUH FRQ FRGLILFD DVFLL  

&RGLILFD GHOOH LPPDJLQL
&RGLILFDWH FRPH VHTXHQ]H GL  HG  ELW PDS  'LJLWDOL]]D]LRQH SDVVDJJLR GD XQD LPPDJLQH DG XQD VHTXHQ]D ELQDULD /¶LPPDJLQH q YLVWD FRPH XQD PDWULFH GL SXQWL SL[HO  $G RJQL SXQWR q DVVRFLDWR XQ QXPHUR FKH FRUULVSRQGH DG XQ SDUWLFRODUH FRORUH R SHU LPPDJLQL LQ ELDQFR H QHUR DG XQ OLYHOOR GL JULJLR QXPHUR SRWHQ]D GL   8QD PDJJLRUH TXDOLWj GL LPPDJLQH LPSOLFD XQD PDJJLRUH RFFXSD]LRQH GL PHPRULD œ QXPHUR PDJJLRUH GL SXQWL SHU SROOLFH GSL GRW SHU LQFK œ  SROOLFH  FP  œ QXPHUR PDJJLRUH GL FRORUL SDOHWWH  œ 3HU LQWHUSUHWDUH OH VHTXHQ]H GL ELW RFFRUUH FRQRVFHUH GLPHQVLRQL GHOO¶LPPDJLQH ULVROX]LRQH PLVXUDWD LQ GSL  QXPHUR GL FRORUL R VIXPDWXUH

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

&RGLILFD GHOOH ,PPDJLQL
6WDQGDUG GL FRGLILFD 7,)) 7DJJHG ,PDJH )LOH )RUPDW XWLOL]]D WHFQLFKH GL FRPSUHVVLRQH *,) HWF (VHPSLR 

SL[HO LPPDJLQH D GXH FRORUL ELDQFR H QHUR  6HUYH XQ VROR ELW SHU FRGLILFDUH OD WRQDOLWDC GL FRORUH SHU FLDVFXQ SL[HO ,Q WRWDOH  ELW  E\WH  

      

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

)21'$0(17, ', ,1)250$7,&$

5$335(6(17$=,21( '(, 180(5, 

/LQJXDJJL GL 3URJUDPPD]LRQH
(C XQD QRWD]LRQH FRQ FXL HC SRVVLELOH GHVFULYHUH JOL DOJRULWPL œ 3URJUDPPD 3URJUDPPD HC OD UDSSUHVHQWD]LRQH GL XQ DOJRULWPR LQ XQ SDUWLFRODUH OLQJXDJJLR GL SURJUDPPD]LRQH ,Q JHQHUDOH RJQL OLQJXDJJLR GL SURJUDPPD]LRQH GLVSRQH GL XQ LQVLHPH GL SDUROH FKLDYH NH\ZRUGV  DWWUDYHUVR OH TXDOL HC SRVVLELOH HVSULPHUH LO IOXVVR GL D]LRQL GHVFULWWR GDOO
DOJRULWPR 2JQL OLQJXDJJLR HC FDUDWWHUL]]DWR GD XQD VLQWDVVL H GD XQD VHPDQWLFD œ VLQWDVVL H
O
LQVLHPH GL UHJROH IRUPDOL SHU OD FRPSRVL]LRQH GL SURJUDPPL QHO OLQJXDJJLR VFHOWR /H UHJROH VLQWDWWLFKH GHWWDQR OH PRGDOLWDC GL FRPELQD]LRQH WUD OH SDUROH FKLDYH GHO OLQJXDJJLR SHU FRVWUXLUH FRUUHWWDPHQWH LVWUX]LRQL IUDVL  œ VHPDQWLFD HC O
LQVLHPH GHL VLJQLILFDWL GD DWWULEXLUH DOOH IUDVL VLQWDWWLFDPHQWH FRUUHWWH FRVWUXLWH QHO OLQJXDJJLR VFHOWR

/LQJXDJJL GL SURJUDPPD]LRQH 
              0DFFKLQD GL 7XULQJ 'LDJUDPPL GL IOXVVR 9RQ 1HXPDQQ /LQJXDJJL PDFFKLQD HG $66(0%/(5 )2575$1 %DFNXV ,%0 &2%2/ $3/ /,63 0F&DUWK\ $/*2/¶ EORFFR VWDFN 3/, 6,08/$ WLSR GL GDWR DVWUDWWR FODVVH 3$6&$/ :LUWK 02'8/$ :LUWK 352/2* .RZDOVNL  &ROPHUDXHU 6(7/ 60$//7$/. RJJHWWL & & FRQ RJJHWWL

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 205

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 206

,O OLQJXDJJLR PDFFKLQD
'HVFULYLDPR XQ VHPSOLFH OLQJXDJJLR PDFFKLQD SHU OD SURJUDPPD]LRQH GL XQD PDFFKLQD GL YRQ 1HXPDQQ + ,O OLQJXDJJLR PDFFKLQD HC GLUHWWDPHQWH GDOO
HODERUDWRUH VHQ]D QHVVXQD WUDGX]LRQH ,VWUX]LLRQLL ,VWUX] RQ  6L GLYLGRQR LQ GXH SDUWL XQ FRGLFH RSHUDWLYR HG HYHQWXDOPHQWH XQR R SLX¶ RSHUDQGL œ ,O FRGLFH RSHUDWLYR VSHFLILFD O¶RSHUD]LRQH GD FRPSLHUH œ JOL RSHUDQGL LQGLYLGXDQR OH FHOOH GL PHPRULD D FXL VL ULIHULVFRQR OH RSHUD]LRQL 3HU VHPSOLFLWDC FRQVLGHULDPR LVWUX]LRQL DG XQ VROR RSHUDQGR + LVWUX]LRQL HG RSHUDQGL UHODWLYL DO SURJUDPPD LQ HVHFX]LRQH VRQR FDULFDWL LQ PHPRULD H TXLQGL VRQR PHPRUL]]DWL LQ IRUPD ELQDULD HVHJXLELOH

,VWUX]LRQL IRUPDWR
op_code operando

n s

m IR

V OXQJKH]]D GL XQ¶LVWUX]LRQH LQ ELW V QP

œ Q QXPHUR GL ELW GHGLFDWL DO FRGLFH RSHUDWLYR œ P QXPHUR GL ELW GHGLFDWL DOO¶LQGLUL]]DPHQWR GHJOL RSHUDQGL + ,QVLHPH GL LVWUX]LRQL GHO OLQJXDJJLR DO SLXC Q LVWUX]LRQL GLYHUVH FLDVFXQD KD XQ GLYHUVR RSBFRGH + 0HPRULD LQGLUL]]DELOH DO SLXC P FHOOH GL PHPRULD GLYHUVH 8OOWHULLRUH LLSRWHVLL VHPSOOLLILLFDWLLYD 8 WHU RUH SRWHV VHPS I FDW YD 6XSSRQLDPR FKH RJQL LVWUX]LRQH RFFXSL HVDWWDPHQWH XQD FHOOD GL PHPRULD

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 207

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 208

/LQJXDJJLR 0DFFKLQD ,VWUX]LRQL
5LLFRUGLLDPR LL UHJLLVWULL LLQWHUHVVDWLL 5 FRUG DPR UHJ VWU QWHUHVVDW  5, UHJLVWUR LQGLUL]]L 5' UHJLVWUR GDWL 3& SURJUDP FRXQWHU SURVVLPD LVWUX]LRQH ,5 LQVWUXFWLRQ UHJLVWHU LVWUX]LRQH FRUUHQWH $% UHJLVWUL DFFXPXODWRUL /¶HVHFX]LRQH GL RJQL LVWUX]LRQH ULFKLHGH WUH IDVL  DFTXLVL]LRQH GDOOD PHPRULD FHQWUDOH IHWFK   LQWHUSUHWD]LRQH GHO FRGLFH RSHUDWLYR GHFRGH   HVHFX]LRQH H[HFXWH  2JQL IDVH HC UHDOL]]DWD GD XQD VHTXHQ]D GL PLFURLVWUX]LRQL FLDVFXQD GHOOH TXDOL FRUULVSRQGH DG XQ WUDVIHULPHQWR GL GDWL WUD UHJLVWUL WUD PHPRULD H UHJLVWUL RSSXUH WUD SHULIHULFKH H UHJLVWUL )HWFK )HWFK œ HC UHDOL]]DWD GDOOD VHTXHQ]D GL PLFURLVWUX]LRQL PC -> RI read(MEMORIA,RI) -> RD RD -> IR PC + 1 -> PC œ UHDG WUDVIHULVFH GDOOD PHPRULD FHQWUDOH LQGLUL]]R 5, DO UHJLVWUR 5'
FONDAMENTI DI INFORMATICA LINGUAGGI DI PROGRAMMAZIONE 209

/LQJXDJJLR 0DFFKLQD LVWUX]LRQL
/2$'

3ULQFLSDOL

FDULFDPHQWR GL XQD FHOOD GL PHPRULD LQ XQ RSSRUWXQR UHJLVWUR DXVLOLDULR FRQVLGHULDPR VROR L UHJLVWUL $ H % /2$'$ /2$'% /2$'$ ,1' /2$'$ ,1'

(VHFX]LLRQH GLL (VHFX] RQH G

op(IR) -> RI read(MEMORIA,RI) -> RD RD -> A + RS ,5 VHOH]LRQD O¶RSHUDQGR GHOO¶LVWUX]LRQH FRQWHQXWD LQ ,5 6725( FDULFD LO FRQWHQXWR GL XQ UHJLVWUR LQ XQD FHOOD GL PHPRULD FRQVLGHULDPR VROR L UHJLVWUL $ H % 6725($ 6725(% 6725($ ,1' 6725($ ,1'

(VHFX]LLRQH GLL (VHFX] RQH G

A -> RD op(IR) -> RI write(MEMORIA,RI,RD) + ZULWH WUDVIHULVFH LO FRQWHQXWR GHO UHJLVWUR 5' LQ PHPRULD FHQWUDOH LQGLUL]]R 5, 
FONDAMENTI DI INFORMATICA LINGUAGGI DI PROGRAMMAZIONE 210

5($'

WUDVIHULPHQWR GL GDWL GD XQD SHULIHULFD DOOD PHPRULD FHQWUDOH

,VWUX]LLRQLL GLL VDOOWR ,VWUX] RQ G VD WR 0RGLILFDQR O¶HVHFX]LRQH VHTXHQ]LDOH GHO SURJUDPPD OD SURVVLPD LVWUX]LRQH GD HVHJXLUH QRQ HC TXHOOD LPPHGLDWDPHQWH VXFFHVVLYD QHO SURJUDPPD PD TXHOOD LQGLYLGXDWD GDOO¶LQGLUL]]R VSHFLILFDWR -803 ,1' 6DOWR LQFRQGL]LRQDWR OD SURVVLPD LVWUX]LRQH GD HVHJXLUH HC DOO¶LQGLUL]]R ,1'

(VHFX]LLRQH GLL 5($' ,1' (VHFX] RQH G 5($' ,1' VXSSRQLDPR FKH 5'3 VLD XQ UHJLVWUR GDWL GHOOD SHULIHULFD FRQVLGHUDWD RDP -> RD {Int. periferica -> CPU} op(IR) -> RI write(MEMORIA,RI,RD)

(VHFX]LLRQH GLL -803 ,1' (VHFX] RQH G -803 ,1' OP(IR) -> PC ‡-803= , 6DOWR FRQGL]LRQDWR HIIHWWXD LO VDOWR DG , VROR VH LO FRQWHQXWR GL $ HC ]HUR XWLOL]]D LO UHJLVWUR 36: SHU HVHJXLUH LO WHVW

:5,7( VFULWWXUD VX XQD SHULIHULFD (VHFX]LLRQH GLL :5,7( ,1' (VHFX] RQH G :5,7( ,1' op(IR) -> RI read(MEMORIA,RI)->RD RD -> RDP ,VWUX]LRQL DULWPHWLFKH $'' ',) 08/ ',9 *OL RSHUDQGL VRQR LQ $ H % LO ULVXOWDWR HC WUDVIHULWR QHO UHJLVWUR $ ',9 UHVWR LQ % 

‡123

ID WUDVFRUUHUH XQ FLFOR VHQ]D VYROJHUH DOFXQD RSHUD]LRQH DWWHVD WHUPLQD O¶HVHFX]LRQH GHO SURJUDPPD

‡+$/7

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 211

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 212

6HW GLL ,VWUX]LLRQLL GLL XQ HOODERUDWRUH 6HW G ,VWUX] RQ G XQ H DERUDWRUH œ HC O¶LQVLHPH GHOOH LVWUX]LRQL FKH OD PDFFKLQD HC LQ JUDGR GL HVHJXLUH GLUHWWDPHQWH œ ,Q TXHVWR FDVR ,Q TXHVWR FDVR  LVWUX]LRQL 9$; GHOOD 'LJLWDO  ! VRQR VXIILFLHQWL  ELW !  RSBFRGH LVWUX]LRQH  /2$'$  /2$'%  6725($  6725(%  5($'  :5,7(  $''  ',)  08/  ',9  -803  -803=  123  +$/7 5HFHQWHPHQWH VRQR VWDWH SURSRVWH PDFFKLQH 5,6& 5HGXFHG ,QVWUXFWLRQ 6HW &RPSXWHU  œ GD XQ ULGRWWR VHW GL LVWUX]LRQL œ FRQ IRUPDWL UHJRODUL ! 3UHVWD]LRQL PLJOLRUL ULVSHWWR D PDFFKLQH FRQ PROWH LVWUX]LRQL
FONDAMENTI DI INFORMATICA LINGUAGGI DI PROGRAMMAZIONE 213

/LQJXDJJLR 0DFFKLQD
3URJUDPPD 3URJUDPPD œ &RQVLVWH LQ GXH SDUWL LVWUX]LRQL H GDWL œ /D SDUWH LVWUX]LRQL SUHFHGH OD SDUWH GDWL (VHPSLR œ PROWLSOLFD]LRQH VX GDWL LQWHUL 3HU VHPSOLFLWD¶ IDFFLDPR SDUWLUH LO SURJUDPPD GDOOD SULPD FHOOD GL PHPRULD 0 READ 8 1 READ 9 2 LOADA 8 3 LOADB 9 4 MUL 5 STOREA 8 6 WRITE 8 7 HALT 8 DATO INTERO 9 DATO INTERO

{16 bit}

LQ FRGLFH PDFFKLQD FHOOH GL PHPRULD GD  ELW P  Q  4XLQGL SRVVLDPR UDSSUHVHQWDUH QXPHUL QDWXUDOL FRPSUHVL IUD  H  RSSXUH QXPHUL LQWHUL FRPSUHVL IUD     H  

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 214

5DSSUHVHQWD]LLRQH ELLQDULLD 5DSSUHVHQWD] RQH E QDU D                                                  

,QGLUL]]DPHQWR
œ 1RQ VHPSUH LO FDPSR RSHUDQGR GL XQD LVWUX]LRQH UDSSUHVHQWD O¶LQGLUL]]R GHO GDWR VX FXL RSHUDUH (VLVWRQR YDULH SRVVLELOLWDC œ œ œ œ LQGLUL]]DPHQWR LPPHGLDWR LQGLUL]]DPHQWR GLUHWWR LQGLUL]]DPHQWR LQGLUHWWR LQGLUL]]DPHQWR FRQ UHJLVWUR LQGLFH

,QGLLULL]]DPHQWR LLPPHGLLDWR ,QG U ]]DPHQWR PPHG DWR ,O YDORUH GHOO
RSHUDQGR HC JLDC FRQWHQXWR QHO FDPSR RSHUDQGR 1RQ HC ULFKLHVWR DOFXQ DFFHVVR DOOD PHPRULD GXUDQWH O
HVHFX]LRQH GHOO
LVWUX]LRQH + &RQ LVWUX]LRQL D IRUPDWR ILVVR JOL RSHUDQGL SRVVRQR RFFXSDUH DO PDVVLPR P ELW

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 215

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 216

,QGLUL]]DPHQWR GLUHWWR ,O FDPSR RSHUDQGR FRQWLHQH O¶LQGLUL]]R DVVROXWR GL XQD FHOOD GL PHPRULD LQ FXL HC PDQWHQXWR LO YDORUH GL XQ GDWR HC OD PRGDOLWDC YLVWD ILQR DG RUD  œ 3HU LO UHFXSHUR GHOO¶RSHUDQGR HC ULFKLHVWR XQ DFFHVVR DOOD PHPRULD 3XRC HVVHUH RQHURVR VH OD PHPRULD HC PROWR JUDQGH R VH HC QHFHVVDULR ULORFDUH LO SURJUDPPD HG L GDWL
op_code

œ ,QGLLULL]]DPHQWR LLQGLLUHWWR ,QG U ]]DPHQWR QG UHWWR /¶LQGLUL]]R GHOO¶RSHUDQGR HC FRQWHQXWR QHOOD SDUROD GL PHPRULD LQGLUL]]DWD GDO FDPSR RSHUDQGR + (C ULFKLHVWR XQ GRSSLR DFFHVVR DOOD PHPRULD SHU UHFXSHUDUH O¶RSHUDQGR + 6L SRVVRQR XVDUH SLXC ELW SHU UDSSUHVHQWDUH O¶LQGLUL]]R GHOO¶RSHUDQGR

op_code

n
n m

q (<=m)

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 217

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 218

,QGLLULL]]DPHQWR PHGLLDQWH UHJLLVWUR LLQGLLFH ,QG U ]]DPHQWR PHG DQWH UHJ VWUR QG FH /¶LQGLUL]]R GHOO¶RSHUDQGR VL RWWLHQH VRPPDQGR DOJHEULFDPHQWH LO FRQWHQXWR GHO FDPSR RSHUDQGR GHOO¶LVWUX]LRQH H TXHOOR GL XQ SDUWLFRODUH UHJLVWUR GHOOD &38 GHWWR UHJLVWUR LQGLFH  œ + 5LFKLHGH XQ XQLFR DFFHVVR DOOD PHPRULD SHU UHFXSHUDUH O¶RSHUDQGR HG XQD RSHUD]LRQH GL VRPPD (C SDUWLFRODUPHQWH HIILFLHQWH VH VL GHYH DFFHGHUH DG XQ FHUWR QXPHUR GL RSHUDQGL PHPRUL]]DWL LQ FHOOH GL PHPRULD FRQVHFXWLYH DG HVHPSLR YHWWRUL LQ & 
I

,O OLQJXDJJLR $66(0%/(5
(¶ GLIILFLOH OHJJHUH H FDSLUH XQ SURJUDPPD VFULWWR LQ IRUPD ELQDULD                                                  

op_code

œ /LLQJXDJJLL DVVHPEOODWRULL $VVHPEOOHU  / QJXDJJ DVVHPE DWRU $VVHPE HU  œ ‡ /H LVWUX]LRQL FRUULVSRQGRQR XQLYRFDPHQWH D TXHOOH PDFFKLQD PD YHQJRQR HVSUHVVH WUDPLWH QRPL VLPEROLFL SDUROH FKLDYH  œ ‡ , ULIHULPHQWL DOOH FHOOH GL PHPRULD VRQR IDWWL PHGLDQWH QRPL VLPEROLFL LGHQWLILFDWRUL  œ œ ‡ ,GHQWLILFDWRUL FKH UDSSUHVHQWDQR GDWL FRVWDQWL R YDULDELOL RSSXUH LVWUX]LRQL HWLFKHWWH  œ œ + ,O SURJUDPPD SULPD GL HVVHUH HVHJXLWR GHYH HVVHUH WUDGRWWR LQ OLQJXDJJLR PDFFKLQD DVVHPEODWRUH 
FONDAMENTI DI INFORMATICA LINGUAGGI DI PROGRAMMAZIONE 220

n

m

*HVWLRQH SLXC VHPSOLFH LQ FDVR GL ULORFD]LRQH

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 219

œ (VHPSLLR SURJUDPPD $VVHPEOOHU (VHPS R SURJUDPPD $VVHPE HU READ X READ Y LOADA X LOADB Y MUL STOREA X WRITE X HALT X INT Y INT

œ (VHPSLLR (VHPS R 3URJUDPPD $VVHPEOHU FKH FDOFROD LO SURGRWWR FRPH VHTXHQ]D GL VRPPH <! ; !   5($' ; 5($' < /2$'$ =(52 6725($= /2$'$ ; -803= ),1( /2$'% 812 ',) 6725($; /2$'$ < /2$'% = 680 6725($ = -803 7(67 :5,7( = +$/7   ,17 ,17 ,17

7(67

),1( =(52 812 ; < =

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 221

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 222

/LQJXDJJL GL SURJUDPPD]LRQH GL DOWR OLYHOOR
œ /LLQJXDJJLLR 0DFFKLLQD / QJXDJJ R 0DFFK QD œ FRQRVFHQ]D GHL PHWRGL LQIRUPD]LRQL XWLOL]]DWL GL UDSSUHVHQWD]LRQH GHOOH

6LQWDVVL GL XQ OLQJXDJJLR GL SURJUDPPD]LRQH
'DWR XQ OOLLQJXDJJLLR 'DWR XQ QJXDJJ R œ 9 DOIDEHWR YRFDERODULR R OHVVLFR (C O
LQVLHPH GHL VLPEROL FRQ FXL VL FRVWUXLVFRQR L SURJUDPPL DG HVHPSLR SDUROH FKLDYH R VLQJROL FDUDWWHUL  œ 9  XQLYHUVR OLQJXLVWLFR VX 9 (C O¶LQVLHPH GL WXWWH OH VHTXHQ]H ILQLWH GL OXQJKH]]D DUELWUDULD GL HOHPHQWL GL 9 *OL HOHPHQWL GL 9 VRQR OH IUDVL R VWULQJKH GL 9 œ (VHPSLLR (VHPS R 9 ^LI HOVH  $       ` $   HOVH $ $   $  

œ /LLQJXDJJLLR 0DFFKLLQD HG $VVHPEOOHU / QJXDJJ R 0DFFK QD HG $VVHPE HU œ QHFHVVLWDC GL FRQRVFHUH GHWWDJOLDWDPHQWH OH FDUDWWHULVWLFKH GHOOD PDFFKLQD UHJLVWUL GLPHQVLRQL GDWL VHW GL LVWUX]LRQL œ VHPSOLFL DOJRULWPL LPSOLFDQR OD VSHFLILFD GL PROWH LVWUX]LRQL œ /LLQJXDJJLL GLL $OOWR /LLYHOOOOR / QJXDJJ G $ WR / YH R ,O SURJUDPPDWRUH SXRC DVWUDUUH GDL GHWWDJOL OHJDWL DOO¶DUFKLWHWWXUD HG HVSULPHUH L SURSUL DOJRULWPL LQ PRGR VLPEROLFR + 6RQR LQGLSHQGHQWL GDOOD PDFFKLQD DVWUD]LRQH  œ (VHFX]LLRQH (VHFX] RQH 6RQR WUDGRWWL LQ VHTXHQ]H GL LVWUX]LRQL GL EDVVR OLYHOOR GLUHWWDPHQWH HVHJXLWH GDO SURFHVVRUH DWWUDYHUVR œ LQWHUSUHWD]LRQH DG HV %$6,& œ FRPSLOD]LRQH DG HV & )2575$1 3DVFDO

9 ^LI $  $  LI $  $ $ $  $ LI HOVH $ GR $ `

8Q OLQJXDJJLR / VXOO¶DOIDEHWR GL 9 H¶ XQ VRWWRLQVLHPH GL 9 

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 223

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 224

*UDPPDWLFKH
/D VLQWDVVL GL XQ OLQJXDJJLR SXRC HVVHUH GHVFULWWD LQ PRGR LQIRUPDOH DG HVHPSLR D SDUROH RSSXUH LQ PRGR IRUPDOH *UDPPDWLLFD IRUPDOOH *UDPPDW FD IRUPD H HC XQD QRWD]LRQH PDWHPDWLFD FKH FRQVHQWH GL HVSULPHUH LQ PRGR ULJRURVR OD VLQWDVVL GHL OLQJXDJJL GL SURJUDPPD]LRQH 'HI *UDPPDWLLFD %1) %DFNXV1DXU )RUP 'HI *UDPPDW FD %1) %DFNXV 1DXU )RUP 8QD JUDPPDWLFD %1) HC XQ LQVLHPH GL  HOHPHQWL œ XQ DOIDEHWR WHUPLQDOH 9 œ XQ DOIDEHWR QRQ WHUPLQDOH 1 œ XQ LQVLHPH ILQLWR GL UHJROH SURGX]LRQL 3 GHO WLSR ; $ GRYH ; ³ 1 HG $ HC XQD VHTXHQ]D GL VLPEROL a VWULQJKH  a ³ 1 ­ 9  œ XQ DVVLRPD R VLPEROR LQL]LDOH 6 œ (VHPSLLR (VHPS R QDWXUDOH!  _ FLIUDQRQQXOOD! ^FLIUD!` FLIUDQRQQXOOD!  _______ FLIUD!  _FLIUDQRQQXOOD!

%1)
8QD %1) GHILQLVFH XQ OLQJXDJJLR VXOO¶DOIDEHWR WHUPLQDOH PHGLDQWH XQ PHFFDQLVPR GL GHULYD]LRQH R ULVFULWWXUD œ 'HI 'HI 'DWD XQD JUDPPDWLFD * H GXH VWULQJKH b g HOHPHQWL GL 1­9  VL GLFH FKH g GHULYD GLUHWWDPHQWH GD b b “ g  VH OH VWULQJKH VL SRVVRQR GHFRPSRUUH LQ b h$d g had HG HVLVWH OD SURGX]LRQH $ a 6L GLFH FKH g GHULYD GD b VH b=b0 “ b1 “ b2 “ ... “ bn = g œ 'HI 'HI 'DWD XQD JUDPPDWLFD * VL GLFH OLQJXDJJLR JHQHUDWR GD * /* O¶LQVLHPH GHOOH IUDVL GL 9 GHULYDELOL DSSOLFDQGR OH SURGX]LRQL D SDUWLUH GDO VLPEROR LQL]LDOH 6 /H IUDVL GL XQ OLQJXDJJLR GL SURJUDPPD]LRQH YHQJRQR GHWWH SURJUDPPL GL WDOH OLQJXDJJLR

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 225

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 226

,Q XQD JUDPPDWLFD %1) SRVVRQR HVLVWHUH SLXC UHJROH FRQ OD VWHVVD SDUWH VLQLVWUD ; $ ; $  ; $1 (TXLYDOH D VFULYHUH ; $_$__$1 VRQR WXWWH DOWHUQDWLYH 

%1) HVHPSL
9 ^LO JDWWR WRSR VDVVR PDQJLD EHYH` 1 ^ IUDVH! VRJJHWWR! YHUER! FRPSORJJHWWR! DUWLFROR! QRPH!` 6 IUDVH! 3 SURGX]LRQL  IUDVH! VRJJHWWR!YHUER!FRPSORJJHWWR! VRJJHWWR! DUWLFROR!QRPH! DUWLFROR! LO QRPH! JDWWR_WRSR_VDVVR_ YHUER! PDQJLD _ EHYH FRPSORJJHWWR! DUWLFROR!QRPH! $OEHUR VLQWDWWLFR HVSULPH LO SURFHVVR GL GHULYD]LRQH GL XQD IUDVH PHGLDQWH XQD JUDPPDWLFD ³LO JDWWR PDQJLD LO WRSR´

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 227

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 228

(VHPSLR GL GHULYD]LRQH OHIWPRVW 
<frase> <soggetto> <verbo> <compl-oggetto>

$ SDUWLUH GDOOR VFRSR GHOOD JUDPPDWLFD VL ULVFULYH VHPSUH LO QRQWHUPLQDOH SLXC D VLQLVWUD IUDVH! ! VRJJHWWR!YHUER!FRPSORJJHWWR! ! DUWLFROR!QRPH!YHUER!FRPSORJJHWWR! ! LO QRPH!YHUER!FRPSORJJHWWR! ! LO JDWWR YHUER!FRPSORJJHWWR! ! LO JDWWR PDQJLD FRPSORJJHWWR! ! LO JDWWR PDQJLD DUWLFROR!QRPH! ! LO JDWWR PDQJLD LO QRPH! ! LO JDWWR PDQJLD LO WRSR

<articolo> <nome> il gatto

<articolo> <nome> mangia il topo

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 229

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 230

([WHQGHG %1) DOFXQH HVWHQVLRQL
; >D@% D SXR¶ FRPSDULUH ]HUR RG XQD YROWD  HTXLYDOH D ; %_D% ; ^D`Q% D SXR¶ FRPSDULUH GD  DG XQ PDVVLPR GL Q YROWH Q  HTXLYDOH DOOD SURGX]LRQH ; %_D%_DD%_DDD% 6H Q HC RPHVVR LO PDVVLPR HC XQ YDORUH ILQLWR DUELWUDULR (TXLYDOH D ; %_D; ULFRUVLYD

(%1) HVHPSLR 9 ^` 1 ^QDWXUDOH!FLIUD! FLIUDQRQQXOOD!` 6 QDWXUDOH! 3 HC FRVWLWXLWR GDOOH SURGX]LRQL QDWXUDOH! FLIUD!_FLIUDQRQQXOOD! ^FLIUD!` FLIUD!   _ FLIUDQRQQXOOD! FLIUDQRQQXOOD! ________

3HU UDJJUXSSDUH FDWHJRULH VLQWDWWLFKH ; D _ E ' _ F HTXLYDOH D ; D ' _ E ' _ F

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 231

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 232

(%1)  HVHPSL
1XPHUL LQWHUL GL OXQJKH]]D TXDOVLDVL FRQ R VHQ]D VHJQR QRQ VL SHUPHWWRQR QXPHUL FRQ SLXC GL XQD FLIUD VH TXHOOD SLXC D VLQLVWUD HC  HV 

œ (VHPSLLR (VHPS R $OEHUR VLQWDWWLFR SHU OD JHQHUD]LRQH GHO QXPHUR  XVDQGR OD JUDPPDWLFD (%1)

<intero> <intero-senza-segno> <cifra-non-nulla> <cifra> <cifra> <cifra> <cifra-non-nulla> 5 <cifra-non-nulla> <cifra>

9 ^`8^` 1 ^LQWHUR! LQWHURVHQ]DVHJQR! FLIUD! FLIUDQRQQXOOD!` 6 3 LQWHUR! >_@ LQWHURVHQ]DVHJQR! LQWHURVHQ]DVHJQR! _ FLIUDQRQQXOOD! ^FLIUD!` FLIUD! FLIUDQRQQXOOD!_ FLIUDQRQQXOOD! ________ ,GHQWLILFDWRUH LGHQWLILFDWRUH!  OHWWHUD! ^ OHWWHUD! _ FLIUD!` OHWWHUD!  $ _ % _  _ = FLIUD!  _________ LQWHUR!

3

<cifra> <cifra>

4 <cifra-non-nulla>

7

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 233

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 234

5DSSUHVHQWD]LRQH DOWHUQDWLYD GLDJUDPPL VLQWDWWLFL

6HPDQWLFD GL XQ OLQJXDJJLR GL SURJUDPPD]LRQH
$WWULEXLVFH XQ VLJQLILFDWR DL FRVWUXWWL OLQJXLVWLFL GHO OLQJXDJJLR 0ROWR VSHVVR QRQ HC GHILQLWD IRUPDOPHQWH 0HWRGL IRUPDOL

<intero>::= + <intero-senza-segno>::=

<intero-senza-segno>

0

<cifra-non-nulla> <cifra>

<identificatore>::=

<lettera> <lettera> <cifra> 

VHPDQWLFD RSHUD]LRQDOH D]LRQL  VHPDQWLFD GHQRWD]LRQDOH IXQ]LRQL PDWHPDWLFKH  VHPDQWLFD DVVLRPDWLFD IRUPXOH ORJLFKH %HQHILFL SHU LO SURJUDPPDWRUH FRPSUHQVLRQH GHL FRVWUXWWL SURYH IRUPDOL GL FRUUHWWH]]D  O¶LPSOHPHQWDWRUH FRVWUX]LRQH GHO WUDGXWWRUH FRUUHWWR  SURJHWWLVWD GL OLQJXDJJL VWUXPHQWL IRUPDOL GL SURJHWWR 

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 235

FONDAMENTI DI INFORMATICA

LINGUAGGI DI PROGRAMMAZIONE 236

/LQJXDJJL GL SURJUDPPD]LRQH OLQJXDJJLR &

,O

3RWHUH HVSUHVVLYR GL XQ OLQJXDJJLR
(C OD FDSDFLWDC GL IRUQLUH FRVWUXWWL GL DVWUD]LRQH LO SLXC SRVVLELOH VLPLOL DL FRQFHWWL XWLOL]]DWL QHOOD GHVFUL]LRQH GHO PHWRGR ULVROXWLYR + TXDQWR SLXC LO SRWHUH HVSUHVVLYR GL XQ OLQJXDJJLR HC HOHYDWR WDQWR SLXC LO SURJUDPPDWRUH HC IDFLOLWDWR QHOOD IDVH GL IRUPDOL]]D]LRQH GHO PHWRGR ULVROXWLYR LQ SURJUDPPD ,Q SDUWLFRODUH LO SRWHUH HVSUHVVLYR GL XQ OLQJXDJJLR VL PDQLIHVWD œ QHL WLSL GL GDWR FKH VL SRVVRQR HVSULPHUH H PDQLSRODUH œ QHOOH RSHUD]LRQL HVSULPLELOL FLRHC O
LQVLHPH GL LVWUX]LRQL SUHYLVWH SHU HVSULPHUH LO FRQWUROOR GHO IOXVVR GL HVHFX]LRQH 3URJUDPPD 'DWL  &RQWUROOR

8Q SURJUDPPD HC OD UDSSUHVHQWD]LRQH GL XQ DOJRULWPR LQ XQ SDUWLFRODUH OLQJXDJJLR GL SURJUDPPD]LRQH /D SURJUDPPD]LRQH HC EDVDWD VXO FRQFHWWR GL DVWUD]LRQH /
DVWUD]LRQH HC LO SURFHVVR FRQ FXL VL GHVFULYH XQ IHQRPHQR DWWUDYHUVR XQ VRWWRLQVLHPH GHOOH VXH SURSULHWDC + 2JQL SURJUDPPD HC XQD DVWUD]LRQH GHOOD SUREOHPD GD ULVROYHUH VL FRVWUXLVFH XQ PRGHOOR DVWUDWWR GHO SUREOHPD FRQFHQWUDQGRVL VROWDQWR VXOOH SURSULHWDC LPSRUWDQWL DL ILQL GHOOD ULVROX]LRQH H WUDVFXUDQGR HYHQWXDOL DOWUH FDUDWWHULVWLFKH FRQVLGHUDWH LUULOHYDQWL

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,O OLQJXDJJLR &
3URJHWWDWR QHO  GD ' 0 5LWFKLH SUHVVR L ODERUDWRUL $7 7 %HOO SHU SRWHU ULVFULYHUH LQ XQ OLQJXDJJLR GL DOWR OLYHOOR LO FRGLFH GHO VLVWHPD RSHUDWLYR 81,; 'HILQL]LRQH IRUPDOH QHO  %: .HUQLJKDP H ' 0 5LWFKLH 1HO  HC VWDWR GHILQLWR XQR VWDQGDUG $16, & GD SDUWH GHOO
$PHULFDQ 1DWLRQDO 6WDQGDUGV ,QVWLWXWH &DUDWWHULLVWLLFKH SULLQFLLSDOOLL &DUDWWHU VW FKH SU QF SD  œ (OHYDWR SRWHUH HVSUHVVLYR œ ‡ 7LSL GL GDWR SULPLWLYL H WLSL GL GDWR GHILQLELOL GDOO¶XWHQWH ‡ 6WUXWWXUH GL FRQWUROOR SURJUDPPD]LRQH VWUXWWXUDWD IXQ]LRQL H SURFHGXUH œ &DUDWWHULVWLFKH GL EDVVR OLYHOOR JHVWLRQH GHOOH PHPRULD œ DFFHVVR DOOD UDSSUHVHQWD]LRQH œ 6WLOH GL SURJUDPPD]LRQH FKH LQFRUDJJLD OR VYLOXSSR GL œ SURJUDPPL SHU SDVVL GL UDIILQDPHQWR VXFFHVVLYL VYLOXSSR WRSGRZQ œ 6LQWDVVL GHILQLWD IRUPDOPHQWH œ

(OHPHQWL GHO WHVWR GL XQ SURJUDPPD &
1HO WHVWR GL XQ SURJUDPPD & SRVVRQR FRPSDULUH ‡ SDUROH FKLDYH ‡ FRPPHQWL ‡ FDUDWWHUL H VWULQJKH ‡ QXPHUL LQWHUL R UHDOL YDORUL FRVWDQWL ‡ LGHQWLILFDWRUL 3DUROOH FKLLDYH 3DUR H FK DYH
auto continue else for long signed switch volatile break default enum goto register sizeof typedef while case do extern if return static unsigned const double float int short struct void

/H SDUROH FKLDYH VRQR SDUROH ULVHUYDWH FLRHC QRQ SRVVRQR HVVHUH XWLOL]]DWH FRPH LGHQWLILFDWRUL DG HVHPSLR GL YDULDELOL

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

&RPPHQWLL &RPPHQW  6RQR VHTXHQ]H GL FDUDWWHUL LJQRUDWH GDO FRPSLODWRUH 9DQQR UDFFKLXVH WUD    /* questo e‘ un commento dell’autore */ , FRPPHQWL YHQJRQR JHQHUDOPHQWH XVDWL SHU LQWURGXUUH QRWH HVSOLFDWLYH QHO FRGLFH GL XQ SURJUDPPD

&RVWDQWL
1XPHULL LLQWHULL 1XPHU QWHU 5DSSUHVHQWDQR QXPHUL UHODWLYL TXLQGL FRQ VHJQR   E\WH EDVH GHFLPDOH  EDVH RWWDOH  EDVH HVDGHFLPDOH [)) 1XPHULL UHDOOLL 1XPHU UHD 9DULH QRWD]LRQL  6XIILVVL ( O / X 8 I )  RWWWWDOOH  R D H ( LQWHULORQJ XQVLJQHG UHDOL  IORDWLQJ [ 2; HVDGHFLLPDOOH [ 2; HVDGHF PD H  E\WH  /  [

3UHILLVVLL 3UHI VV

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

&DUDWWHULL &DUDWWHU  ,QVLHPH GHL FDUDWWHUL GLVSRQLELOL HC GLSHQGHQWH GDOOD LPSOHPHQWD]LRQH  ,Q JHQHUH $6&,, HVWHVR  FDUDWWHUL  6L LQGLFDQR WUD VLQJROL DSLFL
D
&DUDWWHULL VSHFLLDOOLL &DUDWWHU VSHF D  QHZOLQH WDE EDFNVSDFH IRUP IHHG FDUULDJH UHWXUQ FRGLILFD RWWDOH ?Q ?W ?E ?I ?U ?222 2 FLIUD RWWDOH  ? q OD FRGLILFD GHO FDUDWWHUH
$

6WULLQJKH 6WU QJKH 6RQR VHTXHQ]H GL FDUDWWHUL WUD GRSSL DSLFL D DDD 

VWULQJD QXOOD

(VHPSLLR SULLQWI HC OO¶LLVWUX]LLRQH SHU OOD VWDPSD (VHPS R SU QWI HC ¶ VWUX] RQH SHU D VWDPSD printf("Prima riga\nSeconda riga\n"); printf("\\\"/"); (IIHWWR RWWHQXWR (IIHWWR RWWHQXWR Prima riga Seconda riga \"/

,O FDUDWWHUH ? LQLELVFH LO VLJQLILFDWR SUHGHILQLWR GL DOFXQL FDUDWWHUL ³VSHFLDOL´ HV 
 ? HFF ?
?? ? ? FDUDWWHUH QXOOR

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,GHQWLILFDWRUL
8Q LGHQWLILFDWRUH HC XQ QRPH FKH GHQRWD XQ RJJHWWR XVDWR QHO SURJUDPPD DG HVHPSLR YDULDELOL FRVWDQWL WLSL SURFHGXUH H IXQ]LRQL  ‡ 'HYH LQL]LDUH FRQ XQD OHWWHUD R FRQ LO FDUDWWHUH µB¶  DOOD TXDOH SRVVRQR VHJXLUH OHWWHUH H FLIUH LQ QXPHUR TXDOXQTXH LGHQWLILFDWRUH! OHWWHUD!^OHWWHUD!_FLIUD!` ‡ GLVWLQ]LRQH WUD PDLXVFROH H PLQXVFROH OLQJXDJJLR FDVH VHQVLWLYH $G (VHPSLLR $G (VHPS R 6RQR LGHQWLILFDWRUL YDOLGL $OID EHWD *DPPD *DPPD 1RQ VRQR LGHQWLILFDWRUL YDOLGL ; LQW 5HJROOD *HQHUDOOH 5HJR D *HQHUD H + SULPD GL HVVHUH XVDWR XQ LGHQWLILFDWRUH GHYH HVVHUH JLDC VWDWR GHILQLWR LQ XQD SDUWH GL WHVWR SUHFHGHQWH

6WUXWWXUD GL XQ SURJUDPPD &
1HO FDVR SLXC VHPSOLFH XQ SURJUDPPD & FRQVLVWH LQ 
SURJUDPPD! >SDUWHGLFKJOREDOH!@ PDLQ! >^DOWUHIXQ]LRQL!`@

6WUXWWXUD GHO PDLQ OD SDUWH PDLQ! GL XQ SURJUDPPD HC VXGGLYLVD LQ 
PDLQ!  PDLQ ^ SDUWHGLFKLDUD]LRQL! SDUWHLVWUX]LRQL! `

+ LO PDLQ! HC FRVWLWXLWR GD GXH SDUWL ‡ 8QD SDUWH GL GLFKLDUD]LRQL YDULDELOL WLSL FRVWDQWL HWLFKHWWH HWF LQ FXL YHQJRQR GHVFULWWL H GHILQLWL JOL RJJHWWL FKH YHQJRQR XWLOL]]DWL GDO PDLQ ‡ 8QD SDUWH LVWUX]LRQL FKH GHVFULYH O¶DOJRULWPR ULVROXWLYR XWLOL]]DWR PHGLDQWH LVWUX]LRQL GHO OLQJXDJJLR )RUPDOOPHQWH )RUPD PHQWH LO PDLQ HC XQD IXQ]LRQH FKH QRQ UHVWLWXLVFH DOFXQ YDORUH

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VHPSLLR (VHPS R /*programma che, letti due numeri terminale, ne stampa la somma*/ #include <stdio.h> main() { int X,Y; /* p. dichiarativa */ a

9DULDELOL
8QD YDULDELOH UDSSUHVHQWD XQ GDWR FKH SXRC FDPELDUH LO SURSULR YDORUH GXUDQWH O¶HVHFX]LRQH GHO SURJUDPPD ,Q JHQHUDOOH ,Q JHQHUD H ,Q RJQL OLQJXDJJLR GL DOWR OLYHOOR XQD YDULDELOH HC FDUDWWHUL]]DWD GD XQ QRPH LGHQWLILFDWRUH H TXDWWUR DWWULEXWL EDVH ‡ FDPSR G¶D]LRQH VFRSH  HC O¶LQVLHPH GL LVWUX]LRQL GHO SURJUDPPD LQ FXL OD YDULDELOH HC QRWD SXRC HVVHUH PDQLSRODWD & 3DVFDO GHWHUPLQDELOH VWDWLFDPHQWH /,63 GLQDPLFDPHQWH ‡ WHPSR GL YLWD R GXUDWD R HVWHQVLRQH  HC O¶LQWHUYDOOR GL WHPSR LQ FXL XQ¶DUHD GL PHPRULD HC OHJDWD DOOD YDULDELOH )RU75$1 DOORFD]LRQH VWDWLFD & 3DVFDO DOORFD]LRQH GLQDPLFD ‡ YDORUH HC UDSSUHVHQWDWR VHFRQGR OD FRGLILFD DGRWWDWD QHOO¶DUHD GL PHPRULD OHJDWD DOOD YDULDELOH ‡ WLSR GHILQLVFH O
LQVLHPH GHL YDORUL FKH OD YDULDELOH SXRC DVVXPHUH H GHJOL RSHUDWRUL DSSOLFDELOL

scanf("%d%d",&X,&Y); *p. istruzioni*/ printf("%d",X+Y); }

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

9DULDELOL LQ &
œ 2JQL YDULDELOH SHU SRWHU HVVHUH XWLOL]]DWD GDOOH LVWUX]LRQL œ GHO SURJUDPPD GHYH HVVHUH SUHYHQWLYDPHQWH GHILQLWD 'HILLQLL]LLRQH GLL 9DULLDELLOOLL 'HI Q ] RQH G 9DU DE /D GHILQL]LRQH GL YDULDELOH DVVRFLD DG XQ LGHQWLILFDWRUH QRPH GHOOD YDULDELOH XQ WLSR 
GHIYDULDELOL!  LGHQWLILFDWRUHWLSR! LGHQWLILFDWRUHYDULDELOH! ^ LGHQWLILFDWRUHYDULDELOH! ` 

(IIHWWR GHOOOOD GHILLQLL]LLRQH GLL YDULLDELLOOH (IIHWWR GH D GHI Q ] RQH G YDU DE H + /D GHILQL]LRQH GL XQD YDULDELOH SURYRFD FRPH HIIHWWR O
DOORFD]LRQH LQ PHPRULD GHOOD YDULDELOH VSHFLILFDWD DOORFD]LRQH DXWRPDWLFD  + 2JQL LVWUX]LRQH VXFFHVVLYD DOOD GHILQL]LRQH GL XQD YDULDELOH $ SRWUDC XWLOL]]DUH $

(VHPSL LQW $ % 680 IORDW FKDU URRW 5RRW & 

9DULDELOL $ % 680 LQWHUH   9DULDE URRW 5RRW UHDOL  LQWHUH   9DULDELOH & FDUDWWHUH 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,VWUX]LRQH GL $VVHJQDPHQWR
,O FRQFHWWR GL YDULDELOH QHO OLQJXDJJLR & UDSSUHVHQWD XQ¶DVWUD]LRQH GHOOD FHOOD GL PHPRULD /
LVWUX]LRQH GL DVVHJQDPHQWR TXLQGL HC O
DVWUD]LRQH GHOO
RSHUD]LRQH GL VFULWWXUD QHOOD FHOOD FKH OD YDULDELOH UDSSUHVHQWD $VVHJQDPHQWR $VVHJQDPHQWR LGHQWLILFDWRUHYDULDELOH! (VHPSLL (VHPS  main() { int a; /* definizione di a */ ... a=100; /*assegnamento ad a del valore 100 */ } #include <stdio.h> main() { float X, Y; scanf("%f",&X); /* lettura */ /* assegnamento del risultato espr. aritmetica: */ Y = 2*3.14*X; printf("%f",Y); /* stampa */ } di una HVSUHVVLRQH!

&RVWDQWL
8QD FRVWDQWH UDSSUHVHQWD XQ GDWR FKH QRQ SXRC FDPELDUH GL YDORUH QHO FRUVR GHOO¶HVHFX]LRQH /D GLFKLDUD]LRQH GL XQD FRVWDQWH DVVRFLD DG XQ LGHQWLILFDWRUH QRPH GHOOD FRVWDQWH XQ YDORUH HVSUHVVR HYHQWXDOPHQWH PHGLDQWH DOWUD FRVWDQWH  
GLFKFRVWDQWH!  FRQVW WLSR! LGHQWLILFDWRUHFRVWDQWH! FRVWDQWH! FRVWDQWH!   _  LGHQWLILFDWRUHFRVWDQWH! _ _ QXPHURVHQ]DVHJQR! _ DOWUHFRVWDQWL!

FRQVW IORDW FRQVW IORDW

SLJUHFR 



SLJUHFR  H PHQRH  H

+ $QFKH LQ TXHVWR FDVR SULPD GL HVVHUH XVDWR XQ LGHQWLILFDWRUH GHYH HVVHUH JLDC VWDWR GHILQLWR DG HV e SHU GHILQLUH menoe  9DQWDJJLL GHULLYDQWLL GDOOOO¶XVR GLL FRVWDQWLL 9DQWDJJ GHU YDQW GD ¶XVR G FRVWDQW  /HJJLELOLWDC H PRGLILFDELOLWDC GHL SURJUDPPL

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VHPSLLR (VHPS R #include <stdio.h> main() { /* programma che, letto un numero a terminale stampa il valore della circonferenza del cerchio con quel raggio */ const float pigreco = 3.1415926; float X, Y; scanf("%f",&X); /*legge X */ Y = 2*pigreco*X; printf("%f",Y); /* stampa Y */ }

7LSR GL GDWR
8Q WLSR GL GDWR 7 HC GHILQLWR FRPH ‡ 8Q LQVLHPH GL YDORUL ' GRPLQLR ‡ 8Q LQVLHPH GL IXQ]LRQL RSHUD]LRQL IIQ GHILQLWH VXO GRPLQLR ' ,Q SUDWLLFD ,Q SUDW FD 8Q WLSR 7 HC GHILQLWR ‡ GDOO¶LQVLHPH GL YDORUL FKH OH YDULDELOL GL WLSR 7 SRVVRQR DVVXPHUH ‡ GDOO¶LQVLHPH GL RSHUD]LRQL FKH SRVVRQR HVVHUH DSSOLFDWH DG RSHUDQGL GHO WLSR 7 (VHPSLLR (VHPS R &RQVLGHULDPR L QXPHUL QDWXUDOL 7LSRBQDWXUDOL œ 1 HC LO GRPLQLR œ œ ^     !  HWF ` HC O¶LQVLHPH GL RSHUD]LRQL œ >1 ^     !  HWF `@

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,O FRQFHWWR GL 7LSR
8Q OLQJXDJJLR GL SURJUDPPPD]LRQH HC WLSDWR VH SUHYHGH FRVWUXWWL VSHFLILFL SHU DWWULEXLUH WLSL DL GDWL XWLOL]]DWL QHL SURJUDPPL 6H XQ OOLLQJXDJJLLR HC WLLSDWR 6H XQ QJXDJJ R HC W SDWR + 2JQL GDWR YDULDELOH R FRVWDQWH GHO SURJUDPPD GHYH DSSDUWHQHUH DG XQR HG XQ VROR WLSR + 2JQL RSHUDWRUH ULFKLHGH RSHUDQGL GL WLSR VSHFLILFR H SURGXFH ULVXOWDWL GL WLSR VSHFLILFR 9DQWDJJLL 9DQWDJJ  + $VWUD]LRQH/¶XWHQWH HVSULPH H PDQLSROD L GDWL DG XQ OLYHOOR GL DVWUD]LRQH SLXC DOWR GHOOD ORUR RUJDQL]]D]LRQH ILVLFD 0DJJLRU SRUWDELOLWDC + 3URWH]LRQH ,O OLQJXDJJLR SURWHJJH O¶XWHQWH GD FRPELQD]LRQL HUUDWH GL GDWL HG RSHUDWRUL FRQWUROOR VWDWLFR VXOO¶XVR GL YDULDELOL HWF LQ IDVH GL FRPSLOD]LRQH  + 3RUWDELOLWDC O¶LQGLSHQGHQ]D GDOO¶DUFKLWHWWXUD UHQGH SRVVLELOH OD FRPSLOD]LRQH GHOOR VWHVVR SURJUDPPD VX PDFFKLQH SURIRQGDPHQWH GLYHUVH

7LSR GL 'DWR LQ &
,O & HC XQ OLQJXDJJLR WLSDWR &OODVVLLILLFD]LLRQH GHLL WLLSLL GLL GDWR LLQ & & DVV I FD] RQH GH W S G GDWR Q & 6LL GLLVWLLQJXH WUD 6 G VW QJXH WUD œ WLSL SULPLWLYL VRQR WLSL GL GDWR SUHYLVWL GDO OLQJXDJJLR œ EXLOWLQ H TXLQGL UDSSUHVHQWDELOL GLUHWWDPHQWH œ WLSL QRQ SULPLWLYL VRQR WLSL GHILQLELOL GDOO¶XWHQWH PHGLDQWH œ DSSRVLWL FRVWUXWWRUL GL WLSR Y W\SHGHI  ,QROOWUH VLL GLLVWLLQJXH WUD ,QR WUH V G VW QJXH WUD œ WLSL VFDODUL LO FXL GRPLQLR HC FRVWLWXLWR GD HOHPHQWL DWRPLFL œ FLRHC ORJLFDPHQWH QRQ VFRPSRQLELOL œ WLSL VWUXWWXUDWL LO FXL GRPLQLR HC FRVWLWXLWR GD HOHPHQWL QRQ œ DWRPLFL H TXLQGL VFRPSRQLELOL LQ DOWUL FRPSRQHQWL 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

&ODVVLILFD]LRQH GHL WLSL GL GDWR LQ &

7LSL SULPLWLYL
,O & SUHYHGH TXDWWUR WLSL SULPLWLYL

WLSL GL GDWR

œ œ œ œ œ œ œ œ

FKDU FDUDWWHUL LQW LQWHUL IORDW UHDOL GRXEOH UHDOL LQ GRSSLD SUHFLVLRQH

VFDODUL

strutturati

+ (C SRVVLELOH DSSOLFDUH DL WLSL SULPLWLYL GHL TXDQWLILFDWRUL H GHL TXDOLILFDWRUL 4XDQWLLILLFDWRULL 4XDQW I FDWRU 
GHILQLWL GDOO
XWHQWH   

SUHGHILQLWL LQW FKDU IORDW GRXEOH

GHILQLWL GDOO
XWHQWH HQXP SXQWDWRUL

FRVWUXWWRUL SUHGHILQLWL > @ YHWWRUL VWUXFW UHFRUG XQLRQ

‡ , TXDQWLILFDWRUL ORQJ H VKRUW LQIOXLVFRQR VXOOR VSD]LR LQ PHPRULD ULFKLHVWR SHU O¶DOORFD]LRQH GHO GDWR ‡ VKRUW DSSOLFDELOH DO WLSR LQW ‡ ORQJ DSSOLFDELOH DL WLSL LQW H GRXEOH (VHPSLR int X; /* se X e‘ su 16 bit..*/ long int Y;/*..Y e‘ su 32 bit */ 4XDOOLLILLFDWRULL 4XD I FDWRU  ‡ , TXDOLILFDWRUL FRQGL]LRQDQR LO GRPLQLR GHL GDWL ‡ VLJQHG DSSOLFDELOH DL WLSR LQW H FKDU ‡ XQVLJQHG DSSOLFDELOH DL WLSR LQW H FKDU int A; /*A in[-2e15,2e15-1] */ unsigned int B; /*B in[0,2e16-1]*/

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,O WLSR int
'RPLLQLLR 'RP Q R ,O GRPLQLR DVVRFLDWR DO WLSR LQW UDSSUHVHQWD O¶LQVLHPH GHL QXPHUL LQWHUL FLRHC = LQVLHPH GHL QXPHUL UHODWLYL  RJQL YDULDELOH GL WLSR LQW HC TXLQGL O
DVWUD]LRQH GL XQ LQWHUR (VHPSLR GHILQL]LRQH GL XQD YDULDELOH LQWHUD int A; /* A e‘ un dato intero */ + 3RLFKHC VL KD VHPSUH D GLVSRVL]LRQH XQ QXPHUR ILQLWR GL ELW SHU OD UDSSUHVHQWD]LRQH GHL QXPHUL LQWHUL LO GRPLQLR UDSSUHVHQWDELOH HC GL HVWHQVLRQH ILQLWD $G HVHPSLLR $G HVHPS R VH LO QXPHUR Q GL ELW D GLVSRVL]LRQH SHU OD UDSSUHVHQWD]LRQH GL XQ LQWHUR HC  DOORUD LO GRPLQLR UDSSUHVHQWDELOH HC FRPSRVWR GL Q   YDORUL

8VR GHLL TXDQWLLILLFDWRULL VKRUWOORQJ 8VR GH TXDQW I FDWRU VKRUW RQJ $XPHQWDQRGLPLQXLVFRQR LO QXPHUR GL ELW D GLVSRVL]LRQH SHU OD UDSSUHVHQWD]LRQH GL XQ LQWHUR
VSD]LR VKRUW LQW  VSD]LR LQW  VSD]LR ORQJ LQW

8VR GHLL TXDOOLLILLFDWRULL 8VR GH TXD I FDWRU  ‡ VLJQHG YLHQH XVDWR XQ ELW SHU UDSSUHVHQWDUH LO VHJQR 4XLQGL O¶LQWHUYDOOR UDSSUHVHQWDELOH HC >Q Q@ ‡ XQVLJQHG YHQJRQR UDSSUHVHQWDWL YDORUL D SULRUL SRVLWLYL ,QWHUYDOOR UDSSUHVHQWDELOH > Q   @ > Q   @

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,O WLSR LQW
2SHUDWRULL 2SHUDWRU  $O WLSR LQW H WLSL RWWHQXWL GD TXHVWR PHGLDQWH TXDOLILFD]LRQHTXDQWLILFD]LRQH VRQR DSSOLFDELOL L VHJXHQWL RSHUDWRUL 2SHUDWRULL DULLWPHWLLFLL 2SHUDWRU DU WPHW F  IRUQLVFRQR ULVXOWDWR LQWHUR      VRPPD VRWWUD]LRQH GLYLVLRQH LQWHUD SURGRWWR

2SHUDWRULL UHOOD]LLRQDOOLL 2SHUDWRU UH D] RQD  VL DSSOLFDQR DG RSHUDQGL LQWHUL H SURGXFRQR ULVXOWDWL ³ERROHDQL´ FLRHC LO FXL YDORUH SXRC DVVXPHUH VROWDQWR XQR GHL GXH YDORUL ^YHUR IDOVR`   XJXDJOLDQ]D GLVXJXDJOLDQ]D   !   !  Å IDOVR   Å YHUR PLQRUH PDJJLRUH PLQRUH R XJXDOH PDJJLRUH R XJXDOH !  Å YHUR

RSHUDWRUH PRGXOR UHVWR GHOOD GLYLVLRQH LQWHUD   Å    LQFUHPHQWR H GHFUHPHQWR ULFKLHGRQR XQ VROR RSHUDQGR XQD YDULDELOH H SRVVRQR HVVHUH SRVWILVVL D R SUHILVVL D Y HVSUHVVLRQL

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

%RROHDQL
6RQR GDWL LO FXL GRPLQLR HC GL GXH VROL YDORUL YDORUL ORJLFL  ^YHUR IDOVR` + LQ & QRQ HVLVWH XQ WLSR SULPLWLYR SHU UDSSUHVHQWDUH GDWL ERROHDQL &RPH YHQJRQR UDSSUHVHQWDWLL LL ULLVXOOWDWLL GLL HVSUHVVLLRQLL &RPH YHQJRQR UDSSUHVHQWDW U VX WDW G HVSUHVV RQ UHOOD]LLRQDOOLL " UH D] RQD " ,O & SUHYHGH FKH L YDORUL ORJLFL UHVWLWXLWL GD HVSUHVVLRQL UHOD]LRQDOL YHQJDQR UDSSUHVHQWDWL DWWUDYHUVR JOL LQWHUL ^` VHFRQGR OD FRQYHQ]LRQH œ  HTXLYDOH D IDOVR œ œ  HTXLYDOH D YHUR œ $G HVHPSLLR $G HVHPS R O
HVSUHVVLRQH $ % UHVWLWXLVFH

2SHUDWRULL OORJLLFLL 2SHUDWRU RJ F  VL DSSOLFDQR DG RSHUDQGL GL WLSR LQW H SURGXFRQR ULVXOWDWL ERROHDQL FLRHC LQWHUL DSSDUWHQHQWL DOO
LQVLHPH ^` LO YDORUH  FRUULVSRQGH D ´IDOVR´ LO YDORUH  FRUULVSRQGH D ³ YHUR´  ,Q SDUWLFRODUH O¶LQVLHPH GHJOL RSHUDWRUL ORJLFL HC RSHUDWRUH $1' ORJLFR __ RSHUDWRUH 25 ORJLFR RSHUDWRUH GL QHJD]LRQH 127 'HILLQLL]LLRQH GHJOOLL RSHUDWRULL OORJLLFLL 'HI Q ] RQH GHJ RSHUDWRU RJ F  D IDOVR IDOVR YHUR YHUR E IDOVR YHUR IDOVR YHUR D E IDOVR IDOVR IDOVR YHUR D__E IDOVR YHUR YHUR YHUR D YHUR YHUR IDOVR IDOVR

Å  VH OD UHOD]LRQH QRQ HC YHUD Å   VH OD UHOD]LRQH HC YHUD

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

2SHUDWRULL /RJLLFLL LLQ & 2SHUDWRU /RJ F Q & ,Q & JOOLL RSHUDQGLL GLL RSHUDWRULL OORJLLFLL VRQR GLL WLLSR LLQW ,Q & J RSHUDQG G RSHUDWRU RJ F VRQR G W SR QW ‡ VH LO YDORUH GL XQ RSHUDQGR HC GLYHUVR GD ]HUR YLHQH LQWHUSUHWDWR FRPH YHUR ‡ VH LO YDORUH GL XQ RSHUDQGR HC XJXDOH D ]HUR YLHQH LQWHUSUHWDWR FRPH IDOVR 'HILLQLL]LLRQH GHJOOLL RSHUDWRULL OORJLLFLL LLQ & 'HI Q ] RQH GHJ RSHUDWRU RJ F Q & D   ž ž E  ž  ž D     E D__E     D    

(VHPSLL VXJOOLL RSHUDWRULL WUD LLQWHULL (VHPS VXJ RSHUDWRU WUD QWHU       !   __   __        Å  Å  Å  Å  Å  Å  Å  Å  Å 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

, WLSL IORDW H GRXEOH UHDOL
'RPLLQLLR 'RP Q R &RQFHWWXDOPHQWH HC O¶LQVLHPH GHL QXPHUL UHDOL 5 ,Q UHDOWDC HC XQ VRWWRLQVLHPH GL 5 D FDXVD GL œ SUHFLVLRQH OLPLWDWD œ œ OLPLWDWH]]D GHO GRPLQLR œ /R VSD]LR DOORFDWR SHU RJQL QXPHUR UHDOH H TXLQGL O
LQVLHPH GHL YDORUL UDSSUHVHQWDELOL GLSHQGH GDO PHWRGR GL UDSSUHVHQWD]LRQH DGRWWDWR 'LLIIHUHQ]D WUD IOORDWGRXEOOH ' IIHUHQ]D WUD I RDW GRXE H IORDW VLQJROD SUHFLVLRQH GRXEOH GRSSLD SUHFLVLRQH PDJJLRUH QXPHUR GL ELW SHU OD PDQWLVVD 8VR GHOO TXDQWLLILLFDWRUH OORQJ 8VR GH TXDQW I FDWRUH RQJ VL SXRC DSSOLFDUH D GRXEOH SHU DXPHQWDUH XOWHULRUPHQWH OD SUHFLVLRQH
VSD]LR IORDW  VSD]LR GRXEOH  VSD]LR ORQJ GRXEOH

7LSL IORDWGRXEOH
2SHUDWRULL 2SHUDWRU 2SHUDWRULL DULLWPHWLLFLL 2SHUDWRU DU WPHW F      VL DSSOLFDQR D RSHUDQGL SURGXFRQR ULVXOWDWL UHDOL UHDOL H

2SHUDWRULL UHOOD]LLRQDOOLL 2SHUDWRU UH D] RQD  KDQQR OR VWHVVR VLJQLILFDWR YLVWR QHO FDVR GHJOL LQWHUL  XJXDOH GLYHUVR  !   ! 2YHUOORDGLLQJ 2YHU RDG QJ ,O & FRPH 3DVFDO )RUWUDQ H PROWL DOWUL OLQJXDJJL RSHUD]LRQL SULPLWLYH DVVRFLDWH D WLSL GLYHUVL SRVVRQR HVVHUH GHQRWDWH FRQ OR VWHVVR VLPEROR DG HVHPSLR OH RSHUD]LRQL DULWPHWLFKH VX UHDOL RG LQWHUL  PLQRUH PDJJLRUH HWF

(VHPSLR GHILQL]LRQH GL YDULDELOL UHDOL float x; double A, B;

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VHPSLL (VHPS             Å Å Å Å    

,O WLSR FKDU
&DUDWWHUH &DUDWWHUH RJQL VLPEROR JUDILFR UDSSUHVHQWDELOH DOO
LQWHUQR GHO VLVWHPD $G HVHPSLR œ OH OHWWHUH GHOO
DOIDEHWR PDLXVFROH PLQXVFROH œ œ OH FLIUH GHFLPDOL   

œ œ L VHJQL GL SXQWHJJLDWXUD  


HWF œ œ DOWUL VLPEROL GL YDULR WLSR  

 



# 
HWF  œ œ L FDUDWWHUL GL FRQWUROOR EHOO OI  II HWF œ 'RPLLQLLR GHOO WLLSR FKDU 'RP Q R GH W SR FKDU ; <HSVLORQ (C O¶LQVLHPH GHL FDUDWWHUL GLVSRQLELOL VXO VLVWHPD GL HODERUD]LRQH VHW GL FDUDWWHUL  7DEHOOOOD GHLL &RGLLFLL 7DEH D GH &RG F 'L VROLWR VL ID ULIHULPHQWR DG XQD WDEHOOD GHL FRGLFL DG HVHPSLR $6&,,  ,Q RJQL WDEHOOD GHL FRGLFL DG RJQL FDUDWWHUH YLHQH DVVRFLDWR XQ LQWHUR FKH OR LGHQWLILFD XQLYRFDPHQWH LO FRGLFH œ ,O GRPLQLR DVVRFLDWR DO WLSR FKDU HC RUGLQDWR O¶RUGLQH œ GLSHQGH GDO FRGLFH DVVRFLDWR DL YDUL FDUDWWHUL

+ $ FDXVD GHOOD UDSSUHVHQWD]LRQH ILQLWD FL SRVVRQR HVVHUH HUURUL GL FRQYHUVLRQH $G HVHPSLR L WHVW GL XJXDJOLDQ]D WUD YDORUL UHDOL LQ WHRULD XJXDOL SRWUHEEHUR QRQ HVVHUH YHULILFDWL [  \ \ [

0HJOLR XWLOL]]DUH XQ PDUJLQH DFFHWWDELOH GL HUURUH  ; < Å ; <HSVLORQ GRYH DG HVHPSLR FRQVW IORDW HSVLORQ 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

7DEHOOOOD $6&,, 7DEH D $6&,, 'L VROLWR YHQJRQR XVDWL  ELW !  YDORUL SRVVLELOL
_ _ _ _ _ _ _ _ _ _ _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _18/ __  _62+ __  _67; __  _ (7;__  _ (27__  _ (14__  _$&. __  _ %(/__  _ %6 __  _ +7 __  _ /) __  _ 97 __  _ )) __  _ &5 __  _ 62 __  _ 6, __  _ '/(__  _ '&__  _ '&__  _ '&__  _ '&__  _ 1$N__  _ 6<1__  _ (7%__  _ &DQ __  _ (0 __  _ 68%__  _ (6&__  _ )6 __  _ *6 __  _ 56 __  _ 86 __  _ __  _ __  _ __  _ __  _ __  _ __  _ __  _
__  _ __  _ __  _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _! _" _# _$ _% _& _' _( _) _* _+ _, __. _/ _0 _1 _2 _3 _4 _5 _6 __  _ 7 __  _ 8 __  _ 9 __  _ : __  _ ; __  _ < __  _ = __  _ > __  _ ? __  _ @ __  _ A __  _ B __  _ C __  _ D __  _ E __  _ F __  _ G __  _ H __  _ I __  _ J __  _ K __  _ L __  _ M __  _ N __  _ O __  _ P __  _ Q __  _ R __  _ S __  _ T __  _ U __  _ V __  _ W __  _ X __  _ Y __  _ Z __  _ [ __  _ \ __  _ ] __  _ ^ __  _ _ __  _ ` __  _ a __  _ __  _ b __  _ c __  _ d __  _ e __  _ f __  _ g __  _ h __  _ i __  _ j __  _ k __  _ l __  _ m __  _ n __  _ o __  _ p __  _ q __  _ r __  _ s __  _ t __  _ u __  _ v __  _ w __  _ x __  _ y __  _ z __  _ { __  _ | __  _ } __  _ ~ __  _  __  _ € __  _  __  _ ‚ __  _ ƒ __  _ „ __  _ … __  _ † __  _ ‡ __  _ ˆ __  _ ‰ __  _ Š __  _ ‹ __  _ Œ __  _  __  _ Ž __  _  __  _  __  _ ‘ __  _ ’ __  _ “ __  _ ” __  _ • __  _ – __  _ — __  _ ˜ __  _ ™ __  _ š __  _ __  _ œ __  _  __  _ ž __  _ Ÿ __  _   __  _ ¡ __  _ ¢ __  _ £ __  _ __  _ ¥ __  _ ¦ __  _ § __  _ ¨ __  _ © __  _ ª __  _ « __  _ __  _ ¬ __  _ ­ __  _ ® __  _ ¯ __  _ ° __  _ ± __  _ ² __  _ ³ __  _ ´ __  _ µ __  _ ¶ __  _ · __  _ ¸ __  _ ¹ __  _ º __  _ » __  _ ¼ __  _ ½ __  _ ¾ __  _ ¿ __  _ À __  _ Á __  _  __  _ à __  _ Ä __  _ Å __  _ Æ __  _ Ç __  _ È __  _ É __  _ Ê __  _ Ë __  _ Ì __  _ Í __  _ Î __  _ Ï __  _ Ð __  _ __  _ Ñ __  _ Ò __  _ Ó __  _ Ô __  _ Õ __  _ Ö __  _ × __  _ __  _ Ù __  _ Ú __  _ Û __  _ Ü __  _ Ý __  _ Þ __  _ ß _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

,O WLSR FKDU
,O GRPLQLR DVVRFLDWR DO WLSR FKDU HC RUGLQDWR O¶RUGLQH GLSHQGH GDO FRGLFH DVVRFLDWR DL YDUL FDUDWWHUL QHOOD WDEHOOD GL ULIHULPHQWR 'HILLQLL]LLRQH GLL YDULLDELLOOLL GLL WLLSR FKDU HVHPSLLR 'HI Q ] RQH G YDU DE G W SR FKDU HVHPS R char C1, C2; &RVWDQWLL GLL WLLSR FKDU &RVWDQW G W SR FKDU 2JQL YDORUH GL WLSR FKDU YLHQH VSHFLILFDWR WUD VLQJROL DSLFL $G HVHPSLLR $G HVHPS R µD¶ µE¶ µ$¶ µ¶ µ¶ 5DSSUHVHQWD]LLRQH GHLL FDUDWWHULL LLQ & 5DSSUHVHQWD] RQH GH FDUDWWHU Q & ,O OLQJXDJJLR & UDSSUHVHQWD L GDWL GL WLSR FKDU FRPH GHJOL LQWHUL RJQL FDUDWWHUH YLHQH UDSSUHVHQWDWR GDO VXR FRGLFH FLRHC O¶LQWHUR FKH OR LQGLFD QHOOD WDEHOOD $6&,,

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,O WLSR FKDU 2SHUDWRUL
, FKDU VRQR UDSSUHVHQWDWLL GD LLQWHULL VX  ELLW  , FKDU VRQR UDSSUHVHQWDW GD QWHU VX  E W  + VXL GDWL FKDU HC SRVVLELOH HVHJXLUH WXWWH OH RSHUD]LRQL SUHYLVWH SHU JOL LQWHUL 2JQL RSHUD]LRQH LQIDWWL HC DSSOLFDWD DL FRGLFL DVVRFLDWL DJOL RSHUDQGL

(VHPSLL (VHPS 
$

&

Å  LQIDWWL    HC YHUR



Å
&
FRGLFH FRGLFH 
$
Å 
D
Å  FRGLFH $ HC GLYHUVR GD ]HUR

$
2SHUDWRULL UHOOD]LLRQDOOLL 2SHUDWRU UH D] RQD  
 ! !

SHU L TXDOL YDOJRQR OH VWHVVH UHJROH YLVWH SHU JOL LQWHUL

8VR GHL TXDOLILFDWRUL HC SRVVLELOH FRPH SHU JOL LQWHUL DSSOLFDUH L TXDOLILFDWRUL VLJQHG XQVLJQHG D YDULDELOL GL WLSR FKDU signed char C; unsigned char K;

$G HVHPSLLR $G HVHPS R FKDU [\ [  \ VH H VROR VH FRGLFH [  FRGLFH \ µD¶!¶E¶ IDOVR SHUFKHC FRGLFH µD¶  FRGLFH µE¶

2SHUDWRULL DULLWPHWLLFLL 2SHUDWRU DU WPHW F  VRQR JOL VWHVVL YLVWL SHU JOL LQWHUL 2SHUDWRULL OORJLLFLL 2SHUDWRU RJ F  VRQR JOL VWHVVL YLVWL SHU JOL LQWHUL

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VSUHVVLRQL 2PRJHQHH HG (WHURJHQHH
,Q & HC SRVVLELOH FRPELQDUH WUD GL ORUR RSHUDQGL GL WLSR GLYHUVR œ HVSUHVVLRQL RPRJHQHH WXWWL JOL RSHUDQGL VRQR GHOOR VWHVVR œ WLSR œ HVSUHVVLRQL HWHURJHQHH JOL RSHUDQGL VRQR GL WLSL GLYHUVL œ 5HJROOD DGRWWDWD LLQ & 5HJR D DGRWWDWD Q & œ VRQR HVHJXLELOL OH HVSUHVVLRQL HWHURJHQHH LQ FXL WXWWL L WLSL œ UHIHUHQ]LDWL ULVXOWDQR FRPSDWLELOL FLRHC GRSR O¶DSSOLFD]LRQH GHOOD UHJROD DXWRPDWLFD GL FRQYHUVLRQH LPSOLFLWD GL WLSR GHO & ULVXOWDQR RPRJHQHL  œ QRQ VRQR HVHJXLELOL OH HVSUHVVLRQL HWHURJHQHH VH WXWWL L WLSL œ UHIHUHQ]LDWL ULVXOWDQR QRQ FRPSDWLELOL FLRHC UHVWDQR HWHURJHQHL DQFKH GRSR O¶DSSOLFD]LRQH GHOOD UHJROD DXWRPDWLFD GL FRQYHUVLRQH LPSOLFLWD GL WLSR GHO & 

&RPSDWLELOLWDC IUD WLSL GL GDWR
'HILLQLL]LLRQH 'HI Q ] RQH 8Q WLSR GL GDWR 7 HC FRPSDWLELOH FRQ XQ WLSR GL GDWR 7 VH LO GRPLQLR ' GL 7 HC FRQWHQXWR LQ ' GRPLQLR GL 7 $G HVHPSLR JOL LQWHUL VRQR FRPSDWLELOL FRQ L UHDOL SHUFKHC = ±5 + OD UHOD]LRQH GL FRPSDWLELOLWDC QRQ HC VLPPHWULFD VH 7 HC FRPSDWLELOH FRQ 7 QRQ HC GHWWR FKH 7 VLD FRPSDWLELOH FRQ 7 3URSULLHWDC 3URSU HWDC ‡ 6H 7 HC FRPSDWLELOH FRQ 7  XQ¶RSHUDWRUH 2S GHILQLWR SHU 7 SXRC HVVHUH DQFKH XWLOL]]DWR FRQ DUJRPHQWL 7 4XLLQGLL 4X QG  VH 2S HC GHILQLWR SHU 7 FRPH 2S ' ™ ' “ '

$OORUD SXRC HVVHUH XWLOL]]DWR DQFKH FRPH 2S ' ™ ' “ ' 2S ' ™ ' “ '

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

&RPSDWLELOLWDC WUD WLSL SULPLWLYL
,Q & HC GHILQLWD OD VHJXHQWH JHUDUFKLD WUD L WLSL SULPLWLYL
FKDU  VKRUW  LQW LQW  ORQJ  XQVLJQHG XQVLJQHG ORQJ  IORDW  GRXEOH  ORQJ GRXEOH

5HJROD GL &RQYHUVLRQH ,PSOLFLWD
)DFHQGR ULIHULPHQWR DOOD JHUDUFKLD WUD WLSL & SULPLWLYL DG RJQL HVSUHVVLRQH [ RS \ YLHQH DSSOLFDWD DXWRPDWLFDPHQWH OD VHJXHQWH UHJROD  2JQL YDULDELOH GL WLSR FKDU R VKRUW HYHQWXDOPHQWH FRQ TXDOLILFD VLJQHG R XQVLJQHG YLHQH FRQYHUWLWD QHO WLSR LQW  6H GRSR LO SDVVR  O
HVSUHVVLRQH HC DQFRUD HWHURJHQHDVL FRQYHUWH WHPSRUDQHDPHQWH O
RSHUDQGR GL WLSR LQIHULRUH DO WLSR VXSHULRUH SURPRWLRQ   $ TXHVWR SXQWR O
HVSUHVVLRQH HC RPRJHQHD H YLHQH HVHJXLWD O
RSHUD]LRQH VSHFLILFDWD ,O ULVXOWDWR HC GL WLSR XJXDOH D TXHOOR SURGRWWR GDOO
RSHUDWRUH HIIHWWLYDPHQWH HVHJXLWR ,Q FDVR GL RYHUORDGLQJ TXHOOR GL UDQJR SLXC DOWR 

'RYH LO VLPEROR  LQGLFD OD UHOD]LRQH GL FRPSDWLELOLWDC
+ /D JHUDUFKLD DVVRFLD XQ UDQJR D FLDVFXQ WLSR

$G HVHPSLR

UDQJR LQW UDQJR GRXEOH

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

&RPSDWLELOLWDC H FRQYHUVLRQH LPSOLFLWD GL WLSR
+ /D FRPSDWLELOLWDC GL VROLWR YLHQH FRQWUROODWD VWDWLFDPHQWH DSSOLFDQGR OH UHJROH GL FRQYHUVLRQH LPSOLFLWD LQ IDVH GL FRPSLOD]LRQH VHQ]D FRQRVFHUH L YDORUL DWWULEXLWL DL VLPEROL WLSL]]D]LRQH IRUWH  (VHPSLLR  HVSUHVVLLRQH VHPSOOLLFH (VHPS R  HVSUHVV RQH VHPS FH [\       Å  Å  Å UHDOH UHDOH LQWHUR

,SRWHVL ‡ SDVVR 

JOL RSHUDQGL YHQJRQR YDOXWDWL GD VLQLVWUD D GHVWUD [\

‡ \ YLHQH FRQYHUWLWR QHOO
LQWHUR FRUULVSRQGHQWH ‡ YLHQH DSSOLFDWD OD VRPPD WUD LQWHUL Å ULVXOWDWR LQWHUR WPS ‡ SDVVR  WPS  U ‡ WPS YLHQH FRQYHUWLWR QHO GRXEOH FRUULVSRQGHQWH ‡ YLHQH DSSOLFDWD OD GLYLVLRQH WUD UHDOL Å ULVXOWDWR UHDOH

&RQYHUVLLRQH HVSOOLLFLLWD &RQYHUV RQH HVS F WD ,Q & VL SXRC IRU]DUH OD FRQYHUVLRQH GL XQ GDWR LQ XQ WLSR VSHFLILFDWR PHGLDQWH O¶RSHUDWRUH GL FDVW LO GDWR! YLHQH FRQYHUWLWR HVSOLFLWDPHQWH QHO QXRYR WLSR! int A, B; float C; C=A/(float)B; Ã YLHQH HVHJXLWD OD GLYLVLRQH WUD UHDOL 
QXRYR WLSR! GDWR!

(VHPSLR  HVSUHVVLRQH FRPSRVWD int x; char y; double r; (x+y) / r (C QHFHVVDULR FRQRVFHUH + 3ULRULWDC GHJOL RSHUDWRUL GHILQLWD GDOOR VWDQGDUG + 2UGLQH GL YDOXWD]LRQH GHJOL RSHUDQGL OR VWDQGDUG QRQ OR LQGLFD

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

7LSL SULPLWLYL QHO OLQJXDJJLR & ,QWHJUDO 7\SHV H )ORDWLQJ 7\SHV
, WLSL SULPLWLYL VFDODUL GHO & SRVVRQR HVVHUH VXGGLYLVL LQ WLSL HQXPHUDELOL ,QWHJUDO 7\SHV H QRQ )ORDWLQJ 7\SHV  7LLSLL HQXPHUDELLOOLL ,QWWHJUDOO 7\SHV  7 S HQXPHUDE ,Q HJUD 7\SHV  JOL HOHPHQWL GHO GRPLQLR DVVRFLDWR DO WLSR VRQR ULJLGDPHQWH RUGLQDWL œ HC SRVVLELOH IDU ULIHULPHQWR DO SULPR YDORUH GHO GRPLQLR HG œ DOO
XOWLPR œ SHU RJQL HOHPHQWR H
VHPSUH SRVVLELOH LQGLYLGXDUH œ O
HOHPHQWR SUHFHGHQWH VH QRQ HC LO SULPR HG LO VXFFHVVLYR VH QRQ HC O
XOWLPR  + D FLDVFXQ HOHPHQWR GHO GRPLQLR SXRC HVVHUH DVVRFLDWR XQ YDORUH LQWHUR SRVLWLYR FKH UDSSUHVHQWD LO QXPHUR G¶RUGLQH GHOO¶HOHPHQWR QHOOD VHTXHQ]D RUGLQDWD GHL YDORUL 7LLSLL HQXPHUDELLOOLL LLQ & 7 S HQXPHUDE Q & FKDU LQW

7LLSLL QRQ HQXPHUDELLOOLL )OORDWWLLQJ 7\SHV  7 S QRQ HQXPHUDE ) RD QJ 7\SHV  &RQFHWWXDOPHQWH LO GRPLQLR 5 HC XQ LQVLHPH GHQVR GDWL GXH HOHPHQWL [ HG [ GHO GRPLQLR GLVWDQ]LDWL WUD ORUR GL XQ e SLFFROR D SLDFHUH HVLVWH VHPSUH XQ¶LQILQLWDC GL YDORUL GL 5 FRQWHQXWL QHOO¶LQWHUYDOOR >[ [@ 7LLSLL QRQ HQXPHUDELLOOLL LLQ & 7 S QRQ HQXPHUDE Q & IORDW GRXEOH ^,QWHJUDOO 7\SHV )OORDWLLQJ 7\SHV` ^,QWHJUD 7\SHV ) RDW QJ 7\SHV` /
LQVLHPH GL TXHVWH GXH FDWHJRULH FRVWLWXLVFH L WLSL DULWPHWLFL

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

'HILQL]LRQH H ,QL]LDOL]]D]LRQH GHOOH YDULDELOL GL WLSR VHPSOLFH
'HILLQLL]LLRQH GLL YDULLDELLOOLL 'HI Q ] RQH G YDU DE 7XWWL JOL LGHQWLILFDWRUL GL WLSR SULPLWLYR GHVFULWWL ILQ TXL SRVVRQR HVVHUH XWLOL]]DWL SHU GHILQLUH YDULDELOL $G HVHPSLR char lettera; int, x, y; unsigned int P; float media; ,QLL]LLDOOLL]]D]LLRQH GLL YDULLDELLOOLL ,Q ] D ]]D] RQH G YDU DE (C SRVVLELOH VSHFLILFDUH XQ YDORUH LQL]LDOH GL XQD YDULDEOLH LQ IDVH GL GHILQL]LRQH $G HVHPSLLR $G HVHPS R int x =10; char y = ‘a’; double r = 3.14*2; + 'LIIHULVFH GDOOD GHILQL]LRQH GL FRVWDQWL SHUFKHC L YDORUL GHOOH YDULDELOL GXUDQWH O
HVHFX]LRQH GHO SURJUDPPD SRWUDQQR HVVHUH PRGLILFDWL

,VWUX]LRQL FODVVLILFD]LRQH
,Q & OH LVWUX]LRQL SRVVRQR HVVHUH FODVVLILFDWH LQ GXH FDWHJRULH ‡ LVWUX]LRQL VHPSOLFL ‡ LVWUX]LRQL VWUXWWXUDWH VL HVSULPRQR PHGLDQWH FRPSRVL]LRQH GL DOWUH LVWUX]LRQL VHPSOLFL HR VWUXWWXUDWH 

LVWUX]LRQL

6(03/,&,  DVVHJQDPHQWR LQJUHVVR  XVFLWD JRWR EUHDN FRQWLQXH UHWXUQ

STRUTTURATE:

LVWUX]LRQH FRPSRVWD LI HOVH ZKLOH GR IRU GR ZKLOH VZLWFK

5HJROD VLQWDWWLFD JHQHUDOH ,Q & RJQL LVWUX]LRQH HC WHUPLQDWD GD XQ SXQWR H YLUJROD >ID HFFH]LRQH O¶LVWUX]LRQH FRPSRVWD@

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

$VVHJQDPHQWR
(C O
LVWUX]LRQH FRQ FXL VL PRGLILFD LO YDORUH GL XQD YDULDELOH 6LQWDVVL 
LVWUX]LRQHDVVHJQDPHQWR! LGHQWLILFDWRUHYDULDELOH! HVSUHVVLRQH!

(VHPSLR main() { /*definizioni variabili: */ int X,Y; unsigned int Z; float SUM; /* segue parte istruzioni */ X=27; Y=343; Z = X + Y -300; X = Z / 10 + 23; Y = (X + Z) / 10 * 10; /* qui X=30, Y=100, Z=70 */ X = X + 70; Y = Y % 10; Z = Z + X -70; SUM = Z * 10; /* X=100, Y=0, Z=100 , SUM=1000.0*/ }

$G HVHPSLLR $G HVHPS R int A, B; A=20; B=A*5; /* B=100 */ &RPSDWLLELLOOLLWDC GLL WLLSR HG DVVHJQDPHQWR &RPSDW E WDC G W SR HG DVVHJQDPHQWR ,Q XQ DVVHJQDPHQWR O
LGHQWLILFDWRUH GL YDULDELOH H O¶HVSUHVVLRQH GHYRQR HVVHUH GHOOR VWHVVR WLSR HYHQWXDOPHQWH FRQYHUVLRQH LPSOLFLWD  (VHPSLLR (VHPS R int x; char y=’a’; /*codice(a)=97*/ double r; x=y; /* char -> int: x=97*/ x=y+x; /*x=194*/ r=y+1.33; /* char -> int -> double*/ x=r; /* troncamento: x=98*/

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

$VVHJQDPHQWR FRPH RSHUDWRUH
)RUPDOPHQWH O¶LVWUX]LRQH GL DVVHJQDPHQWR q XQ¶HVSUHVVLRQH + ,O VLPEROR HC XQ RSHUDWRUH Å O
LVWUX]LRQH GL DVVHJQDPHQWR HC XQD HVSUHVVLRQH Å ULWRUQD XQ YDORUH ‡ LO YDORUH ULWRUQDWR HC TXHOOR DVVHJQDWR DOOD YDULDELOH D VLQLVWUD GHO VLPEROR ‡ LO WLSR GHO YDORUH ULWRUQDWR HC OR VWHVVR WLSR GHOOD YDULDELOH RJJHWWR GHOO
DVVHJQDPHQWR
$G HVHPSLLR $G HVHPS R

$VVHJQDPHQWR
,Q & VRQR GLVSRQLELOL RSHUDWRUL FKH UHDOL]]DQR SDUWLFRODUL IRUPH GL DVVHJQDPHQWR 2SHUDWRULL GLL LLQFUHPHQWR H GHFUHPHQWR 2SHUDWRU G QFUHPHQWR H GHFUHPHQWR 'HWHUPLQDQR O¶LQFUHPHQWRGHFUHPHQWR GHO YDORUH GHOOD YDULDELOH D FXL VRQR DSSOLFDWL 5HVWLWXLVFRQR FRPH ULVXOWDWR LO YDORUH LQFUHPHQWDWRGHFUHPHQWDWR GHOOD YDULDELOH D FXL VRQR DSSOLFDWL int A=10; A++; /*equivale a: A=A+1; */ A--; /*equivale a: A=A-1; */ 'LLIIHUHQ]D WUD QRWD]LLRQH SUHILLVVD H SRVWILLVVD ' IIHUHQ]D WUD QRWD] RQH SUHI VVD H SRVWI VVD ‡ 1RWD]LRQH 3UHILVVD DG HVHPSLR $ VLJQLILFD FKH O
LQFUHPHQWR YLHQH IDWWR SULPD GHOO
LPSLHJR GHO YDORUH GL $ QHOOD HVSUHVVLRQH ‡ 1RWD]LRQH 3RVWILVVD DG HVHPSLR $ VLJQLILFD FKH O
LQFUHPHQWR YLHQH HIIHWWXDWR GRSR O
LPSLHJR GHO YDORUH GL $ QHOOD HVSUHVVLRQH

const int valore=122; int K, M; K=valore+100; /* K=122;l’espressione produce il risultato 222 */ M=(K=K/2)+1; /* K=111, M=112*/

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

$G HVHPSLLR $G HVHPS R int A=10, B; char C=’a’; B=++A; B=A++; C++; int k = i = j = /*A e B valgono 11 */ /* A=12, B=11 */ /* C vale ’b’ */

2SHUDWRUH GLL DVVHJQDPHQWR DEEUHYLLDWWR 2SHUDWRUH G DVVHJQDPHQWR DEEUHY D R (C XQ PRGR VLQWHWLFR SHU PRGLILFDUH LO YDORUH GL XQD YDULDELOH 6LD v XQD YDULDELOH RS XQ¶RSHUDWRUH DG HVHPSLR  HWF  HG e XQD HVSUHVVLRQH Y RS H q TXDVL HTXLYDOHQWH D Y Y RS H

i, j, k; 5; ++k; /* i = 6, k = 6 */ i + k++; /* j=12, i=6,k=7 */

$G HVHPSLLR $G HVHPS R N M N DE +  HTXLYDOH D N  HTXLYDOH D N N  M  N D  E 

j = i + k++; /*equivale a: j=i+k; k=k+1;*/ + ,Q & O¶RUGLQH GL YDOXWD]LRQH GHJOL RSHUDQGL QRQ H¶ LQGLFDWR GDOOR VWDQGDUG VL SRVVRQR VFULYHUH HVSUHVVLRQL LO FXL YDORUH HC GLIILFLOH GD SUHGLUH

/H GXH IRUPH VRQR TXDVL HTXLYDOHQWL SHUFKq LQ Y RS H Y YLHQH YDOXWDWR XQD VROD YROWD PHQWUH LQ Y Y RS H Y YLHQH YDOXWDWR GXH YROWH

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VSUHVVLLRQLL VHTXHQ]LLDOOLL (VSUHVV RQ VHTXHQ] D  8Q
HVSUHVVLRQH VHTXHQ]LDOH VL RWWLHQH FRQFDWHQDQGR WUD ORUR SLXC HVSUHVVLRQL FRQ O
RSHUDWRUH YLUJROD   ‡ ,O ULVXOWDWR SURGRWWR GD XQ
HVSUHVVLRQH VHTXHQ]LDOH HC LO ULVXOWDWR RWWHQXWR GDOO
XOWLPD HVSUHVVLRQH GHOOD VHTXHQ]D ‡ /D YDOXWD]LRQH GHOO
HVSUHVVLRQH DYYLHQH YDOXWDQGR QHOO
RUGLQH WHVWXDOH OH HVSUHVVLRQL FRPSRQHQWL GD VLQLVWUD YHUVR GHVWUD
(VHPSLLR (VHPS R

3UHFHGHQ]D H $VVRFLDWLYLWDC GHJOL 2SHUDWRUL
,Q RJQL HVSUHVVLRQH JOL RSHUDWRUL VRQR YDOXWDWL VHFRQGR XQD SUHFHGHQ]D VWDELOLWD GDOOR VWDQGDUG VHJXHQGR RSSRUWXQH UHJROH GL DVVRFLDWLYLWDC œ /D SUHFHGHQ]D LQGLFD O¶RUGLQH FRQ FXL YHQJRQR YDOXWDWL œ RSHUDWRUL GLYHUVL œ /¶DVVRFLDWLYLWDC LQGLFD O¶RUGLQH LQ FXL RSHUDWRUL GL SDUL œ SULRULWDC FLRHC VWHVVD SUHFHGHQ]D YHQJRQR YDOXWDWL
+ (C SRVVLELOH IRU]DUH OH UHJROH GL SUHFHGHQ]D PHGLDQWH O
XVR

int A=1; char B; A=(B=’k’, ++A, A*2); /* A=4 */

GHOOH SDUHQWHVL

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

5HJROOH GLL 3UHFHGHQ]D H $VVRFLLDWLLYLLWDC GHJOOLL 2SHUDWRULL & LLQ 5HJR H G 3UHFHGHQ]D H $VVRF DW Y WDC GHJ 2SHUDWRU & Q RUGLLQH GLL SULLRULLWWDC GHFUHVFHQWWH RUG QH G SU RU DC GHFUHVFHQ H (VHPSLL (VHPS

3UHFHGHQ]D H $VVRFLDWLYLWDC  
 Å ;$ Å             Å HTXLYDOH D    HTXLYDOH D ;  $ Å   Å   Å      Å    Å  

2SHUDWRUH () [] -> ! ~ ++ -- & sizeof * / % + << < <= == & ^ | && || ?: =+ =- =* =/ , >> > != >=

$VVRFLDWLYLWDC GD VLQLVWUD D GHVWUD GD GHVWUD D VLQLVWUD GD VLQLVWUD D GHVWUD GD VLQLVWUD D GHVWUD GD VLQLVWUD D GHVWUD GD VLQLVWUD D GHVWUD GD GHVWUD D VLQLVWUD GD VLQLVWUD D GHVWUD GD VLQLVWUD D GHVWUD GD VLQLVWUD D GHVWUD GD VLQLVWUD D GHVWUD GD VLQLVWUD D GHVWUD GD GHVWUD D VLQLVWUD GD GHVWUD D VLQLVWUD GD VLQLVWUD D GHVWUD

9DOOXWD]LLRQH D FRUWR FLLUFXLLWR VKRUWWFXWW  9D XWD] RQH D FRUWR F UFX WR VKRU FX  QHOOD YDOXWD]LRQH GL XQD HVSUHVVLRQH & VH XQ ULVXOWDWR LQWHUPHGLR GHWHUPLQD D SULRUL LO ULVXOWDWR ILQDOH GHOOD HVSUHVVLRQH LO UHVWR GHOO
HVSUHVVLRQH QRQ YLHQH YDOXWDWR $G HVHPSLLR HVSUHVVLLRQLL OORJLLFKH $G HVHPS R HVSUHVV RQ RJ FKH +S 9DOOXWWD]LLRQH GHJOOLL RSHUDQGLL GD VLLQ D GHVWWUD +S 9D X D] RQH GHJ RSHUDQG GD V Q D GHV UD  !  IDOVR ;  < YHUR Å VROR SULPR RSHUDQGR

+ %LVRJQHUHEEH HYLWDUH GL VFULYHUH HVSUHVVLRQL FKH GLSHQGRQR GDO PHWRGR GL YDOXWD]LRQH XVDWR Å VFDUVD SRUWDELOLWDC DG HV LQ SUHVHQ]D GL IXQ]LRQL FRQ HIIHWWL FROODWHUDOL 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VHUFL]L 6LD 9  $  %  'HWHUPLQDUH LO YDORUH GHOOH VHJXHQWL HVSUHVVLRQL ORJLFKH $  __ $!  % $  $ % $ 9 $! % $! 9 $! % $ 9 $! % __ $ 9 6ROOX]LLRQLL +S YDOOXWWD]LLRQH GHJOOLL RSHUDQGLL VQ!G[ 6R X] RQ  +S YD X D] RQH GHJ RSHUDQG VQ!G[ $  __ $!  Å YHUR ^$ VKRUW FXW` % $  Å IDOVR ^%   ` $ % $ 9 Å IDOVR ^$!9` $! % $! 9 Å IDOVR ^$%` $ % $ 9 Å YHUR $! % __ $ 9 Å YHUR

,VWUX]LRQL GL LQJUHVVR HG XVFLWD LQSXWRXWSXW
/
LPPLVVLRQH GHL GDWL GL XQ SURJUDPPD H O¶XVFLWD GHL VXRL ULVXOWDWL DYYHQJRQR DWWUDYHUVR RSHUD]LRQL GL OHWWXUD H VFULWWXUD ,O & QRQ KD LVWUX]LRQL SUHGHILQLWH SHU O
LQSXWRXWSXW /LLEUHULLD VWDQGDUG GLL ,2 / EUHU D VWDQGDUG G , 2 ,Q RJQL YHUVLRQH $16, & HVLVWH XQD /LEUHULD 6WDQGDUG GL LQSXWRXWSXW VWGLR FKH PHWWH D GLVSRVL]LRQH DOFXQH IXQ]LRQL GHWWH IXQ]LRQL GL OLEUHULD FKH UHDOL]]DQR O
LQJUHVVR H O¶XVFLWD GDYHUVR L GLVSRVLWLYL VWDQGDUG GL LQSXWRXWSXW 'LLVSRVLLWLLYLL VWDQGDUG GLL LLQSXW H GLL RXWSXW ' VSRV W Y VWDQGDUG G QSXW H G RXWSXW SHU RJQL PDFFKLQD VRQR SHULIHULFKH SUHGHILQLWH GL VROLWR WDVWLHUD H YLGHR 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,QSXW2XWSXW
,O & YHGH OH LQIRUPD]LRQL OHWWHVFULWWH GDYHUVR L GLVSRVLWLYL VWDQGDUG GL ,2 FRPH ILOH VHTXHQ]LDOL FLRHC VHTXHQ]H GL FDUDWWHUL , ILOH VWDQGDUG GL LQSXWRXWSXW SRVVRQR FRQWHQHUH GHL FDUDWWHUL GL FRQWUROOR œ (QG 2I )LOH (2) LQGLFD OD ILQH GHO ILOH œ œ (QG 2I /LQH œ LQGLFD OD ILQH GL XQD OLQHD 

,2 FRQ IRUPDWR
1HOO
,QSXW HG 2XWSXW FRQ IRUPDWR RFFRUUH VSHFLILFDUH LO IRUPDWR GHL GDWL FKH VL YRJOLRQR OHJJHUH RSSXUH VWDPSDUH ,OO IRUPDWR VWDELLOOLLVFH , IRUPDWR VWDE VFH œ FRPH LQWHUSUHWDUH OD VHTXHQ]D GHL FDUDWWHUL LPPHVVL GDO œ GLVSRVLWLYR GL LQJUHVVR QHO FDVR GHOOD OHWWXUD œ FRQ TXDOH VHTXHQ]D GL FDUDWWHUL UDSSUHVHQWDUH LQ XVFLWD L œ YDORUL GD VWDPSDUH QHO FDVR GL VFULWWXUD ,O IRUPDWR YLHQH LQGLFDWR FRQ RSSRUWXQH GLUHWWLYH GHO WLSR GLUHWWLYD! )RUPDWLL SLL FRPXQLL )RUPDW S  FRPXQ  VKRUW ORQJ OG OX OR O[ VLJQHG LQW XQVLJQHG LQW G X GHFLPDOH R RWWDOH [ HVDGHFLPDOH IORDW H I J GRXEOH OH OI OJ FDUDWWHUH VLQJROR F VWULQJD GL FDUDWWHUL V SXQWDWRUL LQGLUL]]L S KG KX KR K[

a 7

1

3.7

b 57 *

c 6

testina lettura

fine linea

fine file (EOF) (ctrl-Z)

)XQ]LLRQLL GLL OOLLEUHULLD SHU )XQ] RQ G EUHU D SHU œ ,QSXW2XWSXW D FDUDWWHUL œ œ ,QSXW2XWSXW D VWULQJKH GL FDUDWWHUL œ œ ,QSXW2XWSXW FRQ IRUPDWR œ

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

/HWWXUD FRQ IRUPDWR scanf
/D VFDQI DVVHJQD L YDORUL OHWWL GDO ILOH VWDQGDUG GL LQSXW DOOH YDULDELOL VSHFLILFDWH FRPH DUJRPHQWL 6LLQWDVVLL 6 QWDVV  VFDQI VWULQJDIRUPDWR! VHTXHQ]DYDULDELOL!  $G HVHPSLR int X; float Y; scanf("%d%f", &X, &Y); /D VFDQI œ OHJJH XQD VHULH GL YDORUL LQ EDVH DOOH VSHFLILFKH FRQWHQXWH LQ œ VWULQJDIRUPDWR! LQ TXHVWR FDVR G I LQGLFD FKH L GXH GDWL OHWWL GDOOR VWDQGDUG LQSXW GHYRQR HVVHUH LQWHUSUHWDWL ULVSHWWLYDHQWH FRPH XQ YDORUH LQWHUR GHFLPDOH G HG XQR UHDOH I œ PHPRUL]]D L YDORUL OHWWL QHOOH YDULDELOL VSHFLILFDWH FRPH œ DUJRPHQWL QHOOD VHTXHQ]DBYDULDELOL! ; < QHOO
HVHPSLR œ HC XQD IXQ]LRQH FKH UHVWLWXLVFH LO QXPHUR GL YDORUL OHWWL H œ PHPRUL]]DWL RSSXUH (2) LQ FDVR GL HQG RI ILOH 6HSDUDWRULL 6HSDUDWRU 

VFDQI
RJQL GLUHWWLYD GL IRUPDWR SUHYHGH GHL VHSDUDWRUL VSHFLILFL
7LSR GL GDWR ,QWHUR &DUDWWHUH 6WULQJD 'LUHWWLYH GL IRUPDWR G [ X HWF F V 6HSDUDWRUL 6SD]LR (2) 1HVVXQR 6SD]LR (2) (2/ (2/

1RWD EHQH 1RWD EHQH + 6H OD VWULQJD GL IRUPDWR FRQWLHQH 1 GLUHWWLYH q QHFHVVDULR FKH OH YDULDELOL VSHFLILFDWH QHOOD VHTXHQ]D VLDQR HVDWWDPHQWH 1 + *OL LGHQWLILFDWRUL GHOOH YDULDELOL D FXL DVVHJQDUH L YDORUL VRQR VHPSUH SUHFHGXWL GDO VLPEROR  >,QIDWWL OH YDULDELOL GHYRQR HVVHUH VSHFLILFDWH DWWUDYHUVR LO ORUR LQGLUL]]RÅ RSHUDWRUH Y SXQWDWRUL H IXQ]LRQL @ + /D VWULQJDBIRUPDWR! SXRC FRQWHQHUH GHL FDUDWWHUL TXDOVLDVL FKH YHQJRQR VFDUWDWL GXUDQWH OD OHWWXUD  FKH UDSSUHVHQWDQR VHSDUDWRUL DJJLXQWLYL ULVSHWWR D TXHOOL VWDQGDUG $G HVHPSLLR $G HVHPS R scanf(“%d:%d:%d”, &A, &B, &C);

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

! ULFKLHGH FKH L WUH GDWL GD OHJJHUH YHQJDQR LPPHVVL VHSDUDWL GDO FDUDWWHUH ³´

6FULWWXUD FRQ IRUPDWR printf
1HO FDVR SL JHQHUDOH OD SULQWI YLHQH XWLOL]]DWD SHU IRUQLUH LQ XVFLWD LO ULVXOWDWR GL XQD HVSUHVVLRQH 6LLQWDVVLL 6 QWDVV  SULQWI VWULQJDIRUPDWR!VHTXHQ]DHOHPHQWL! $G HVHPSLLR $G HVHPS R int X; float Y; printf("%d%f", X*X,Y); ‡ VFULYH VXO GLVSRVLWLYR GL VWDQGDUG RXWSXW XQD VHULH GL YDORUL LQ EDVH DOOH VSHFLILFKH FRQWHQXWH LQ VWULQJDIRUPDWR! ,Q TXHVWR FDVR XQ LQWHUR G  HG XQ UHDOH I  ‡ L YDORUL YLVXDOL]]DWL VRQR L ULVXOWDWL GHOOH HVSUHVVLRQL FKH FRPSDLRQR FRPH DUJRPHQWL QHOOD VHTXHQ]DBHOHPHQWL! ‡ UHVWLWXLVFH LO QXPHUR GL FDUDWWHUL VFULWWL

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

SULQWI
1RWD EHQH 1RWD EHQH œ /D VWULQJD GL IRUPDWR GHOOD SULQWI SXRC FRQWHQHUH VHTXHQ]H œ FRVWDQWL GL FDUDWWHUL GD YLVXDOL]]DUH œ q SRVVLELOH LQVHULUH QHOOD VWULQJD GL IRUPDWR DQFKH FDUDWWHUL œ GL FRQWUROOR QHZOLQH WDE EDFNVSDFH IRUP IHHG FDUULDJH UHWXUQ ?Q ?W ?E ?I ?U

,QSXW2XWSXW
6H DOO¶LQWHUQR GL XQ SURJUDPPD & VL YRJOLRQR XVDUH OH IXQ]LRQL GHOOD OLEUHULD VWGLR q QHFHVVDULR LQVHULUH DOO¶LQL]LR GHO ILOH VRUJHQWH OD OLQHD LQFOXGH VWGLRK! + LQFOXGH HC XQD GLUHWWLYD SHU LO SUHSURFHVVRUH & QHOOD IDVH SUHFHGHQWH DOOD FRPSLOD]LRQH GHO SURJUDPPD RJQL GLUHWWLYD ³ ´ YLHQH HVHJXLWD SURYRFDQGR GHOOH PRGLILFKH WHVWXDOL DO SURJUDPPD VRUJHQWH 1HO FDVR GL #include <stdio.h> OD GLUHWWLYD VWHVVD YLHQH VRVWLWXLWD FRQ LO FRQWHQXWR GHO ILOH VWGLRK! (VHPSLLR (VHPS R #include <stdio.h> main() { int k;

3HU OD VWDPSD GHO FDUDWWHUH

VL XVD $G HVHPSLLR $G HVHPS R char Nome=’A’; char Cognome=’C’; printf("%s\n%c. %c. \n\n%s\n", "Programma scritto da:", Nome,Cognome,"Fine"); 6WDPSD Programma scritto da: A. C. Fine

scanf("%d",&k); printf("Quadrato di %d: %d",k,k*k); }

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

SULQWIVFDQI
(VHPSLLR (VHPS R scanf("%c%c%c%d%f", &c1,&c2,&c3,&i,&x); 6H LQ LQJUHVVR YHQJRQR GDWL ABC 3 7.345 OH YDULDELOL DVVXPRQR L VHJXHQWL YDORUL FKDU F
$
FKDU F
%
FKDU F
&
LQW L  IORDW [ 

(VHPSLLR VWWDPSD GHOOOOD FRGLLILLFD GHFLLPDOOH RWWWWDOOH H (VHPS R V DPSD GH D FRG I FD GHF PD H R D H H HVDGHFLLPDOOH GLL XQ FDUDWWWWHUH GDWWR GD LLQSXWW HVDGHF PD H G XQ FDUD HUH GD R GD QSX

#include <stdio.h> main() { int a; printf("Dai un carattere e ottieni il valore \ decimale, ottale e hex "); scanf("%c",&a); printf("\n%c vale %d in decimale, %o in ottale \ e %x in hex.\n",a, a, a, a); }

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

/ (VHUFLL]LLR / (VHUF ] R &DOFROR GHOO¶RUDULR SUHYLVWR GL DUULYR 6FULYHUH XQ SURJUDPPD FKH OHJJD WUH LQWHUL SRVLWLYL GD WHUPLQDOH UDSSUHVHQWDQWL O¶RUDULR GL SDUWHQ]D RUH PLQXWL VHFRQGL GL XQ YHWWRUH DHUHR OHJJD XQ WHU]R LQWHUR SRVLWLYR UDSSUHVHQWDQWH LO WHPSR GL YROR LQ VHFRQGL H FDOFROL TXLQGL O¶RUDULR GL DUULYR $SSURFFLLR WRSGRZQ $SSURFF R WRSGRZQ 1HO FDVR GL XQ SUREOHPD FRPSOHVVR SXRC HVVHUH XWLOH DGRWWDUH XQD PHWRGRORJLD WRSGRZQ 6L DSSOLFD LO SULQFLSLR GL VFRPSRVL]LRQH GLYLGHQGR XQ SUREOHPD LQ VRWWRSUREOHPL H VIUXWWDQGR O¶LSRWHVL GL FRQRVFHUH OH ORUR VROX]LRQL SHU ULVROYHUH LO SUREOHPD GL SDUWHQ]D D GDWR XQ SUREOHPD 3 ULFDYDUQH XQD VROX]LRQH DWWUDYHUVR OD VXD VFRPSRVL]LRQH LQ VRWWRSUREOHPL SLXC VHPSOLFL E HVHJXLUH D  E SHU RJQL VRWWRSUREOHPD LQGLYLGXDWR FKH QRQ VLD ULVROYLELOH DWWUDYHUVR O
HVHFX]LRQH GL XQ VRWWRSUREOHPD HOHPHQWDUH /D VROX]LRQH ILQDOH VL RWWLHQH GRSR XQD VHTXHQ]D ILQLWD GL SDVVL GL UDIILQDPHQWR UHODWLYL DL VRWWRSUREOHPL YLD YLD RWWHQXWL ILQR DG RWWHQHUH VRWWRSUREOHPL HOHPHQWDUL

3ULPD VSHFLILFD PDLQ ^  GLFKLDUD]LRQH GDWL   OHJJL L GDWL GL LQJUHVVR   FDOFROD O¶RUDULR GL DUULYR   VWDPSD O¶RUDULR GL DUULYR 

`

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

&RGLILFD &RPH GDWL RFFRUURQR WUH YDULDELOL LQWHUH SHU O¶RUDULR GL SDUWHQ]D HG XQD YDULDELOH LQWHUD SHU L VHFRQGL GL YROR /*definizione dati */
long unsigned TempoDiVolo; int Ore, Minuti, Secondi

9HUVLLRQH ILLQDOOH 9HUV RQH I QD H
#include <stdio.h> main() { /*dichiarazione dati */ long unsigned int Ore, Minuti, Secondi, TempoDiVolo; /*leggi i dati di ingresso*/: /*leggi l’orario di partenza*/ printf("%s\n","Orario di partenza (hh,mm,ss)?"); scanf("%ld%ld%ld", &Ore, &Minuti, &Secondi); /*leggi il tempo di volo*/ printf("%s\n","Tempo di volo (in sec.)?"); scanf("%ld", &TempoDiVolo); /*calcola l’orario di arrivo*/ Secondi = Secondi + TempoDiVolo; Minuti = Minuti + Secondi / 60; Secondi = Secondi % 60; Ore = Ore + Minuti / 60; Minuti = Minuti % 60; Ore = Ore % 24;

/*leggi i dati di ingresso*/: /*leggi l’orario di partenza*/ printf("%s\n","Orario di partenza (hh,mm,ss)?"); scanf("%ld%ld%ld\n", &Ore, &Minuti, &Secondi); /*leggi il tempo di volo*/ printf("%s\n","Tempo di volo (in sec.)?"); scanf("%ld\n", &TempoDiVolo); /*calcola l’orario di arrivo*/: Secondi = Secondi + TempoDiVolo; Minuti = Minuti + Secondi / 60; Secondi = Secondi % 60; Ore = Ore + Minuti / 60; Minuti = Minuti % 60; Ore = Ore % 24; /*stampa l’orario di arrivo*/: printf("%s\n","Arrivo previsto alle (hh,mm,ss):"); printf("%ld%c%ld%c%ld\n",Ore,‘:’,Minuti,‘:’, Secondi);

/*stampa l’orario di arrivo*/ printf("%s\n","Arrivo previsto (hh,mm,ss):"); printf("%ld%c%ld%c%ld\n", Ore,‘:’,Minuti,‘:’, Secondi); }

alle

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

'LFKLDUD]LRQL H 'HILQL]LRQL
1HOOD SDUWL GLFKLDUDWLYH GL XQ SURJUDPPD & SRVVLDPR LQFRQWUDUH ‡ GHILQL]LRQL GL YDULDELOH R GL IXQ]LRQH ‡ GLFKLDUD]LRQL GL WLSR R GL IXQ]LRQH 'HILLQLL]LLRQH 'HI Q ] RQH 'HVFULYH OH SURSULHWDC GHOO¶RJJHWWR GHILQLWR H QH GHWHUPLQD O¶HVLVWHQ]D $G HVHPSLR GHILQL]LRQH GL XQD YDULDELOH int V; /* la variabile intera V viene creata (allocata in memoria) */ 'LLFKLLDUD]LLRQH ' FK DUD] RQH 'HVFULYH VROWDQWR GHOOH SURSULHWDC GL RJJHWWL FKH YHUUDQQR HYHQWXDOPHQWH FUHDWL PHGLDQWH GHILQL]LRQH $G HVHPSLR GLFKLDUD]LRQH GL XQ WLSR QRQ SULPLWLYR

'LFKLDUD]LRQH GL WLSR
/D GLFKLDUD]LRQH GL WLSR VHUYH SHU LQWURGXUUH WLSL QRQ SULPLWLYL $VVRFLD DG XQ WLSR GL GDWR QRQ SULPLWLYR XQ LGHQWLILFDWRUH VFHOWR DUELWUDULDPHQWH GDO SURJUDPPDWRUH  $XPHQWD OD OHJJLELOLWDC H PRGLILFDELOLWDC GHO SURJUDPPD ,Q & SHU GLFKLDUDUH XQ QXRYR WLSR VL XWLOL]]D OD SDUROD FKLDYH W\SHGHI

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

7LSL VFDODUL QRQ SULPLWLYL
,Q & VRQR SRVVLELOL GLFKLDUD]LRQL GL WLSL VFDODUL QRQ SULPLWLYL ‡ WLSL ULGHILQLWL ‡ WLSL HQXPHUDWL

7LSR HQXPHUDWR
8Q WLSR HQXPHUDWR YLHQH VSHFLILFDWR DWWUDYHUVR O¶HVSOLFLWD]LRQH GL XQ HOHQFR GL YDORUL FKH UDSSUHVHQWD LO VXR GRPLQLR 6LLQWDVVLL 6 QWDVV  W\SHGHI HQXP ^D D D   DQ` (QXP7\SH 8QD YROWD GLFKLDUDWR XQ WLSR HQXPHUDWR SXRC HVVHUH XWLOL]]DWR SHU GHILQLUH YDULDELOL $G HVHPSLLR $G HVHPS R
typedef enum{nord,sud,est,ovest} direzione; direzione D=nord;

7LSR ULGHILQLWR
8Q QXRYR LGHQWLILFDWRUH GL WLSR YLHQH DVVRFLDWR DG XQ WLSR JLDC HVLVWHQWH SULPLWLYR R QRQ  6LLQWDVVLL 6 QWDVV  W\SHGHI (VHPSLLR (VHPS R typedef int MioIntero; MioIntero X,Y,Z; int W; 7LSR(VLVWHQWH 1XRYR7LSR

3URSULLHWDC 3URSU HWDC 'DWL GL WLSR HQXPHUDWR VRQR HQXPHUDELOL LO GRPLQLR HC VWUHWWDPHQWH RUGLQDWR LQ EDVH DOO¶RUGLQH WHVWXDOH FRQ FXL VL LQGLFDQR JOL HOHPHQWL GHO GRPLQLR QHOOD GLFKLDUD]LRQH $G HVHPSLLR $G HVHPS R
typedef enum{nord,sud,est,ovest} direzione; + QRUG SUHFHGH VXG

HVW VHJXH QRUG H VXG HFF

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

7LSR HQXPHUDWR LQ &
,Q & LLOO WLLSR HQXPHUDWR HTXLLYDOOH D XQD ULLGHILLQLL]LLRQH GHOO WLLSR ,Q & W SR HQXPHUDWR HTX YD H D XQD U GHI Q ] RQH GH W SR LLQW QW $G RJQL HOHPHQWR GHO GRPLQLR YLHQH UDSSUHVHQWDWR FRPH XQ LQWHUR FKH YLHQH XWLOL]]DWR QHOOD YDOXWD]LRQH GL HVSUHVVLRQL UHOD]LRQL HG DVVHJQDPHQWL
+ HQXP LQ & KD VWHVVD RFFXSD]LRQH VWHVVR UDQJH H

(VHPSLLR (VHPS R typedef enum{lu,ma,me,g,v,s,d}Giorno; /*lu=0, ma=1, me=2,..d=6*/ typedef enum{cuori,picche,quadri, fiori} Carta; /*cuori=0, picche=1,…*/ Carta C1, C2, C3, C4, C5; Giorno G; G=lu; Å G=0 G=ma Å G=1 ... C1=cuori Å 0 ... + /
XWLOL]]R GL WLSL RWWHQXWL SHU HQXPHUD]LRQH UHQGH SLXC OHJJLELOH LO FRGLFH + 8Q LGHQWLILFDWRUH GL XQ YDORUH VFDODUH GHILQLWR GDOO¶XWHQWH DG HV O GHYH FRPSDULUH QHOOD GHILQL]LRQH GL XQ VROR WLSR HQXPHUDWR W\SHGHI HQXP^OX PD PH J Y V G` *LRUQR W\SHGHI HQXP^OXPDPH` 3ULPL*LRUQL  VFRUUHWWR 

VWHVVR XWLOL]]R GL LQW

&RQYHQ]LLRQH GHIDXOOW  &RQYHQ] RQH GHIDX W  ,O SULPR HOHPHQWR GHO GRPLQLR YLHQH UDSSUHVHQWDWR FRQ LO YDORUH  LO VHFRQGR FRQ  HWF

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

2SHUDWRUL VXO 7LSR (QXPHUDWR
,O WLSR HQXP HC XQ WLSR WRWDOPHQWH RUGLQDWR HC LQROWUH XQ ,QWHJUDO 7\SH  (VVHQGR L GDWL GL WLSR HQXPHUDWR PDSSDWL VX LQWHUL VX GL HVVL VRQR GLVSRQLELOL JOL VWHVVL RSHUDWRUL YLVWL SHU JOL LQWHUL ,Q SDUWLFRODUH 2SHUDWRULL UHOOD]LLRQDOOLL 2SHUDWRU UH D] RQD  OX  PD Å  Å YHUR OX  Y Å  Å YHUR OX ! V Å ! Å IDOVR FXRUL  TXDGUL Å  Å YHUR  + 9DORUL GL WLSR HQXPHUDWR QRQ VRQR VWDPSDELOL QHC OHJJLELOL QRQ HVLVWH FLRHC XQD GLUHWWLYD GL IRUPDWR VSHFLILFD SHU JOL HQXPHUDWL typedef enum{cuori,picche,quadri,fiori}seme; seme S; ... if(S==cuori) printf("%s","cuori");

7LSL HQXPHUDWL LQ &
(C SRVVLELOH IRU]DUH LO PHFFDQLVPR GL DVVRFLD]LRQH DXWRPDWLFR GL LQWHUL D YDORUL GHO GRPLQLR GL WLSL HQXP typedef enum{Nord=90,Sud,Est, Ovest}direzione; /* Nord=90,Sud=91,Est=92,Ovest=93 */ direzione D; $WWHQ]LLRQH $WWHQ] RQH G   9LHQH DFFHWWDWR HG HVHJXLWR 

+ 1RQ F
HC FRQWUROOR VXJOL HVWUHPL GHOO
LQWHUYDOOR GL LQWHUL XWLOL]]DWR

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

7LSL HQXPHUDWL
(VHPSLLR (VHPS R 9RJOLDPR UHDOL]]DUH LO WLSR ERROHDQ ' ^IDOVR YHUR`

6HFRQGD VROOX]LLRQH 6HFRQGD VR X] RQH 8VR GHO WLSR HQXPHUDWR typedef enum {false, true} Boolean; Boolean flag1,flag2; flag1 = true; if (flag1) .../* flag vale 1 */ flag2 = -37 /* !!! */ if (flag2).../*funziona lo stesso!*/

œ (C XQ WLSR SUHGHILQLWR LQ DOWUL OLQJXDJJL GL SURJUDPPD]LRQH œ DG HVHPSLR LQ 3DVFDO  œ 1RQ HC SUHYLVWR LQ & GRYH SHURC œ ‡ LO YDORUH  ]HUR LQGLFD )$/62 ‡ RJQL YDORUH GLYHUVR GD  LQGLFD 9(52 3ULLPD VROOX]LLRQH 3U PD VR X] RQH 3HU GHILQLUH OH GXH FRVWDQWL ERROHDQH GHILQH )$/6( GHILQH 758(  

+ GHILQH HC XQD GLUHWWLYD GHO SUHSURFHVVRUH & SURYRFD XQD VRVWLWX]LRQH QHO WHVWR ‡ GRYH F¶HC ³)$/6(´ !  ‡ GRYH F¶HC ³758(´ !  QRQ VL DOORFD VSD]LR LQ PHPRULD

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(TXLYDOHQ]D WUD WLSL GL GDWR
4XDQGR GXH RJJHWWL KDQQR OR VWHVVR WLSR" 'LSHQGH GDOOD UHDOL]]D]LRQH GHO OLQJXDJJLR 'XH SRVVLELOLWDC œ HTXLYDOHQ]D VWUXWWXUDOH œ œ HTXLYDOHQ]D QRPLQDOH œ (TXLLYDOOHQ]D VWUXWWXUDOOH (TX YD HQ]D VWUXWWXUD H GXH GDWL VRQR FRQVLGHUDWL GL WLSR HTXLYDOHQWH VH KDQQR OD VWHVVD VWUXWWXUD $G HVHPSLR typedef int typedef int MioIntero NuovoIntero MioIntero; NuovoIntero; A; B;

(TXLLYDOOHQ]D QRPLLQDOOH (TX YD HQ]D QRP QD H GXH GDWL VRQR FRQVLGHUDWL GL WLSR HTXLYDOHQWH VROR O¶LGHQWLILFDWRUH GL WLSR FKH FRPSDUH QHOOD ORUR GHILQL]LRQH HC OR VWHVVR $G HVHPSLR typedef int typedef int MioIntero NuovoIntero MioIntero; NuovoIntero; A; B;

$ H % YHQJRQR FRQVLGHUDWL GL WLSR GLYHUVR

(TXLYDOHQ]D GL WLSR LQ & /R VWDQGDUG QRQ VWDELOLVFH LO WLSR GL HTXLYDOHQ]D GD DGRWWDUH
+ 3HU JDUDQWLUH OD SRUWDELOLWDC HC QHFHVVDULR VYLOXSSDUH

$ H % KDQQR OR VWHVVR WLSR

SURJUDPPL FKH SUHVXSSRQJDQR O¶HTXLYDOHQ]D QRPLQDOH

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

3URJUDPPD]LRQH VWUXWWXUDWD 'LLMMNVWWUD ' NV UD 
 /D SURJUDPPD]LRQH VWUXWWXUDWD QDVFH FRPH SURSRVWD SHU UHJRODPHQWDUH H VWDQGDUGL]]DUH OH PHWRGRORJLH GL SURJUDPPD]LRQH 2ELLHWWLLYR 2E HWW YR UHQGHUH SLX¶ IDFLOH OD OHWWXUD GHL SURJUDPPL H TXLQGL OD ORUR PRGLILFD H PDQXWHQ]LRQH  ,GHD GLL EDVH ,GHD G EDVH /D SDUWH LVWUX]LRQL R SDUWH HVHFXWLYD GL XQ SURJUDPPD YLHQH YLVWD FRPH XQ FRPDQGR FRPSOHVVR R VWUXWWXUDWR RWWHQXWR FRPSRQHQGR DOWUL FRPDQGL HOHPHQWDUL DVVHJQDPHQWR FKLDPDWD GL SURFHGXUD H QRQ  PHGLDQWH DOFXQH UHJROH GL FRPSRVL]LRQH VWUXWWXUH GL FRQWUROOR  6WUXWWXUH GLL FRQWUROOOOR 6WUXWWXUH G FRQWUR R œ FRQFDWHQD]LRQH R FRPSRVL]LRQH  œ œ DOWHUQDWLYD R LVWUX]LRQH FRQGL]LRQDOH œ œ ULSHWL]LRQH R LWHUD]LRQH œ HOLPLQD]LRQH GHL VDOWL LQFRQGL]LRQDWL

3URJUDPPD]LRQH 6WUXWWXUDWD
+ $EROL]LRQH GL VDOWL LQFRQGL]LRQDWL JRWR QHO IOXVVR GL FRQWUROOR 9DQWDJJLL 9DQWDJJ  œ OHJJLELOLWDC œ œ VXSSRUWR D PHWRGRORJLD GL SURJHWWR WRSGRZQ VROX]LRQH GL œ SUREOHPL FRPSOHVVL DWWUDYHUVR VFRPSRVL]LRQH LQ VRWWR SUREOHPL D ORUR YROWD VFRPSRQLELOL LQ VRWWR SUREOHPL HWF /D VROX]LRQH VL RWWLHQH FRPSRQHQGR OH VROX]LRQL GHL VRWWRSUREOHPL DWWUDYHUVR FRQFDWHQD]LRQH DOWHUQDWLYD H ULSHWL]LRQH œ VXSSRUWR D PHWRGRORJLD ERWWRPXS OD VROX]LRQH GL œ SUREOHPL DYYLHQH DJJUHJDQGR FRPSRQHQWL JLDC GLVSRQLELOL PHGLDQWH FRQFDWHQD]LRQH DOWHUQDWLYD H ULSHWL]LRQH SURJUDPPD]LRQH SHU FRPSRQHQWL œ IDFLOLWDC GL YHULILFD H PDQXWHQ]LRQH œ

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

7HRUHPD GL %|KP H -DFRSLQL
/H VWUXWWXUH GL FRQFDWHQD]LRQH LWHUD]LRQH H DOWHUQDWLYD FRVWLWXLVFRQR XQ LQVLHPH FRPSOHWR LQ JUDGR GL HVSULPHUH WXWWH OH IXQ]LRQL FDOFRODELOL + /¶XVR GL TXHVWH VROH VWUXWWXUH GL FRQWUROOR QRQ OLPLWD LO SRWHUH HVSUHVVLYR

,VWUX]LRQL VWUXWWXUDWH LQ &
œ LVWUX]LRQH FRPSRVWD R EORFFR ^ ` œ œ DOWHUQDWLYD if, switch œ œ LVWUX]LRQL GL LWHUD]LRQH while, do, for œ /H LVWUX]LRQL VWUXWWXUDWH R GL FRQWUROOR VRQR DOOD EDVH GHOOD SURJUDPPD]LRQH VWUXWWXUDWD

8Q OOLLQJXDJJLLR FRPSRVWR GDOOOOH LLVWUX]LLRQLL 8Q QJXDJJ R FRPSRVWR GD H VWUX] RQ  OHWWXUD VFULWWXUD VFDQI SULQWI DVVHJQDPHQWR LVWUX]LRQH FRPSRVWD ^` LVWUX]LRQH FRQGL]LRQDOH LIHOVH LVWUX]LRQH GL LWHUD]LRQH ZKLOH HC XQ OLQJXDJJLR FRPSOHWR LQ JUDGR GL HVSULPHUH WXWWH OH IXQ]LRQL FDOFRODELOL 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,VWUX]LRQH FRPSRVWD ^ `
'HWHUPLQD O¶HVHFX]LRQH LVWUX]LRQL FRPSRQHQWL 6LLQWDVVLL 6 QWDVV 
^ ` 'LFKLDUD]LRQL H 'HILQL]LRQL! 6HTXHQ]D GL ,VWUX]LRQL!

,VWUX]LRQH &RPSRVWD
GHOOH 'LLFKLLDUD]LLRQLL H GHILLQLL]LLRQLL ' FK DUD] RQ H GHI Q ] RQ  (C SRVVLELOH GHILQLUH YDULDELOL FKH KDQQR YLVLELOLWj H WHPSR GL YLWD OLPLWDWR DO EORFFR VWHVVR $G HVHPSLR … { int A; A=100; printf(“Valore di A: %d\n”, A); } … )RUPDOPHQWH LO FRUSR GHO PDLQ HC FRVWLWXLWR GD XQ
LVWUX]LRQH FRPSRVWD main() { int A; A=100; printf(“Valore di A: %d\n”, A); }

QHOO¶RUGLQH

WHVWXDOH 

VHTXHQ]DLVWUX]LRQL!  LVWUX]LRQH! ^ LVWUX]LRQH! `

I1

I2

I3

In

6LQWDWWLFDPHQWH HTXLYDOHQWH D XQD VLQJROD LVWUX]LRQH VWUXWWXUDWD 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,VWUX]LRQH &RPSRVWD
(VHPSLLR (VHPS R /*programma che letti due numeri a terminale ne stampa la somma*/ #include <stdio.h> main() { int X,Y; scanf("%d%d",&X,&Y); printf("%d",X+Y); }

,VWUX]LRQL GL DOWHUQDWLYD
,VWUX]LLRQH LLI ,VWUX] RQH I VHOH]LRQD O¶HVHFX]LRQH GL XQD VROD WUD OH LVWUX]LRQL FRPSRQHQWL LQ EDVH DO ULVXOWDWR GL XQ¶HVSUHVVLRQH ORJLFD GHWWD VHOHWWRUH  LI HVSUHVVLRQH! LVWUX]LRQH! >HOVH LVWUX]LRQH!@ VH LO ULVXOWDWR GL HVSUHVVLRQH! q YHUR FLRHC GLYHUVR GD ]HUR YLHQH HVHJXLWD LVWUX]LRQH! DOWULPHQWL YLHQH HVHJXLWD LVWUX]LRQH!
(!=0) (==0)

espressione

Istruzione1

Istruzione2

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,VWUX]LRQH LI
(VHPSLLR (VHPS R #include <stdio.h> main() { int A, B; scanf(“%d%d”, &A, &B); if (B==0) printf (“B vale zero!\n”); else printf(“Quoziente: %d\n”, A/B); }

,VWUX]LRQH LI
LI HVSUHVVLRQH! LVWUX]LRQH! HOVH LVWUX]LRQH!
+ LVWUX]LRQH! HG LVWUX]LRQH! SRVVRQR HVVHUH GL WLSR

TXDOXQTXH VHPSOLFH R VWUXWWXUDWR DG HV LVWUX]LRQH FRPSRVWD R LI 

(VHPSLLR (VHPS R /D SDUWH HOVH GHOO¶LVWUX]LRQH LI q RS]LRQDOH #include <stdio.h> main() { int A, B; scanf(“%d%d”, &A, &B); if (B==0) return; /*termina l’esecuzione del main*/ printf(“Quoziente: %d\n”, A/B); } ,QGHQWWD]LLRQH ,QGHQ D] RQH O¶³LQGHQW´ GHOOH OLQHH GHO WHVWR GHO SURJUDPPD ULVSHWWD O¶DQQLGDPHQWR GHOOH YDULH LVWUX]LRQL Å VL DXPHQWD OD OHJJLELOLWDC GHO SURJUDPPD PRGLILFD SLXC IDFLOH    int Eta; if (Eta >=6) { if (Eta <=14) printf("%s","scolare"); } else { printf("%s","Non scolare"); printf("%d",Eta); };

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

2SSXUH 2SSXUH int Eta; if ((Eta >=6) && (Eta <=14)) printf("%s","scolare"); else { printf("%s","Non scolare"); printf("%d",Eta); }; (VHPSLLR (VHPS R

LI HVHPSL
3URJUDPPD FKH OHJJH GXH QXPHUL H GHWHUPLQD TXDO HC LO PDJJLRUH /* determina il maggiore tra due numeri file c2.c */ #include <stdio.h> main() { int primo,secondo; scanf("%d%d",&primo,&secondo); if (primo>secondo) printf("%d",primo); else printf("%d",secondo); }

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VHPSLLR (VHPS R /* Programma che calcola le radici un’equazione di secondo grado*/ #include <stdio.h> #include <math.h> /*lib. matematica*/ main() { float a,b,c; float d,x1,x2; scanf("%f%f%f",&a,&b,&c); if ((b*b) < (4*a*c)) printf("%s","radici complesse"); else { d=sqrt(b*b-4*a*c); x1=(-b+d)/(2*a); x2=(-b-d)/(2*a); printf("%f%f",x1,x2); }; } di

(VHPSLR LI 5LVROYHUH XQ VLVWHPD OLQHDUH GL GXH HTXD]LRQL LQ GXH LQFRJQLWH D[  E\ D[  E\ [ \ F F

FEFE  DE DE ;1  ' DF DF  DE DE <1  ' #include <stdio.h> main() { float A1,B1,C1,A2,B2,C2,XN,YN,D; float X,Y; scanf("%f%f%f%\n",&A1,&B1,&C1); scanf("%f%f%f%\n",&A2,&B2,&C2); XN = (C1*B2 - C2*B1); D = (A1*B2 - A2*B1); YN = (A1*C2 - A2*C1); if (D == 0) {if (XN == 0) printf("sist. indeterm.\n"); else printf("Nessuna soluz.\n"); } else {X= XN/D; Y= YN/D; printf("%f%f\n",X,Y); } }

6ROOX]LLRQH 6R X] RQH

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,I DQQLGDWL
LI HVSUHVVLRQH! LVWUX]LRQH! HOVH LVWUX]LRQH! œ LVWUX]LRQH! HG LVWUX]LRQH! SRVVRQR HVVHUH DQFRUD œ LVWUX]LRQL LI + 6L SRVVRQR DYHUH SLXC LVWUX]LRQL LI DQQLGDWH LI H! LI H! L! HOVH L!
(==0) (!=0)

/¶HOVH VL ULIHULVFH VHPSUH DOO¶LI SL ³LQWHUQR´ VH QRQ VSHFLILFDWR DOWULPHQWL if (n > 0) if (a > b) n = a; /*n > 0 && a > b */ else n = b; /* n > 0 && a <= b */ + 6H VL YXROH ULIHULUH O
HOVH DOO
LI SLXC HVWHUQR VL XVDQR OH JUDIIH if (n > 0) { if (a > b) n = a; }/*n>0 && a>b */ else n = b; /* n <= 0 */

e1
(==0)

(!=0)

e2 i2 i1

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,I DQQLGDWL
/ (VHUFLL]LLR / (VHUF ] R 'DWL WUH QXPHUL LQWHUL SRVLWLYL FKH UDSSUHVHQWDQR L WUH ODWL GL XQ WULDQJROR VL VWDPSL LO WLSR GHO WULDQJROR HTXLODWHUR LVRVFHOH R VFDOHQR  3ULPD VSHFLILFD PDLQ ^  GLFKLDUD]LRQH GDWL   OHJJL OH OXQJKH]]H GHL ODWL   VH WULDQJROR VWDPSDQH LO WLSR 

&RGLLILLFD &RG I FD &RPH GDWL RFFRUURQR WUH YDULDELOL LQWHUH SHU UDSSUHVHQWDUH OH OXQJKH]]H GHL VHJPHQWL /*dichiarazione dati */ unsigned int primo,secondo,terzo; /* leggi le lunghezze dei segmenti */ scanf("%d%d%d",&primo,&secondo,&terzo); /*se triangolo, stampane il tipo */ if (primo + secondo>terzo) if (primo==secondo)/*det.il tipo */ {if (secondo==terzo) printf("equilatero"); else printf("isoscele");} else {if (secondo==terzo) printf("isoscele"); else {if (primo==terzo) printf("isoscele"); else printf("scaleno");} }

`

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VHPSLLR LLI DQQLLGDWLL (VHPS R I DQQ GDW 6WDPSD GL XQD YDULDELOH GL WLSR HQXPHUDWR typedef enum{cuori,picche,quadri, fiori}seme; main() { seme S; … < assegnamento di un valore a S> … if (S==cuori) printf("%s","cuori"); else if (S==picche) printf("%s","picche"); else if (S==quadri) printf("%s","quadri"); else if (S==fiori) printf("%s","fiori"); }

,VWUX]LRQH 6ZLWFK
&RQVHQWH GL VHOH]LRQDUH O¶HVHFX]LRQH GL XQD R SLXC WUD JOL 1 EORFFKL GL LVWUX]LRQL FRPSRQHQWL LQ EDVH DO YDORUH GL XQD HVSUHVVLRQH 6LLQWDVVLL 6 QWDVV  VZLWFK (VSUHVVLRQH,QWHJUDO7\SH! ^ FDVH FRVWDQWH! EORFFR! >EUHDN@ FDVH FRVWDQWH! EORFFR! >EUHDN@  FDVH FRVWDQWH1! EORFFR1! >EUHDN@ >GHIDXOW EORFFR'L'HIDXOW!@ ` ‡ /¶HVSUHVVLRQH HC GHWWD VHOHWWRUH 'HYH UHVWLWXLUH XQ YDORUH GL WLSR ,QWHJUDO7\SH HQXPHUDELOH  ‡ 2JQL FRVWDQWH DVVRFLDWD D XQD ³HWLFKHWWD´ FDVH GHYH HVVHUH GHOOR VWHVVR WLSR GHO VHOHWWRUH ‡ 8Q YDORUH SXRC FRPSDULUH DO SLXC LQ XQ¶HWLFKHWWD

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,VWUX]LRQH 6ZLWFK
6LLJQLLILLFDWR 6 JQ I FDWR 6H O
HVSUHVVLRQH UHVWLWXLVFH XQ YDORUH XJXDOH DG XQD GHOOH FRVWDQWL LQGLFDWH SHU HVHPSLR FRVWDQWH!  VL HVHJXH LO EORFFR! H WXWWL L EORFFKL GHL UDPL VXFFHVVLYL + , EORFFKL QRQ VRQR PXWXDPHQWH HVFOXVLYL SRVVLELOLWj GL HVHJXLUH LQ VHTXHQ]D SL EORFFKL GL LVWUX]LRQL 3H RWWHQHUH OOD PXWXD HVFOOXVLLRQH WUD LL EOORFFKLL 3H RWWHQHUH D PXWXD HVF XV RQH WUD E RFFK  + 1HFHVVLWj GL IRU]DUH O
XVFLWD PHGLDQWH O
LVWUX]LRQH EUHDN FKH SURYRFD O
XVFLWD IRU]DWD GDOOR VZLWFK  (VHPSLLR (VHPS R int X; switch (X%2) { case 0: printf(“X e` pari”); break; case 1:printf(“X e` dispari”); break; }

5DPR GLL GHIDXOOW 5DPR G GHIDX W (C SRVVLELOH VSHFLILFDUH XQ
HWLFKHWWD GHIDXOW HVVD YLHQH HVHJXLWD SHU TXDOXQTXH YDORUH ULWRUQDWR GDO VHOHWWRUH ,Q SUDWLFD FRQVHQWH GL HVHJXLUH XQ EORFFR QHO FDVR LQ FXL LO YDORUH GHOO
HVSUHVVLRQH QRQ FRUULVSRQGH DG DOFXQD HWLFKHWWD

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VHPSLLR FDOOFROOR GHOOOOD GXUDWWD GLL XQ PHVH (VHPS R FD FR R GH D GXUD D G XQ PHVH D VROOX]LLRQH D VR X] RQH #define GENNAIO 1 #define FEBBRAIO 2 #define MARZO 3 ... #define NOVEMBRE 11 #define DICEMBRE 12 ... int mese, anno, giorni; ... switch (mese) { case GENNAIO: giorni = 31; break; case FEBBRAIO: if (<anno bisestile>) giorni = 29; else giorni = 28; break; case MARZO: giorni = 31; break; case APRILE: giorni = 30; break; case MAGGIO: giorni = 31; break; case GIUGNO: giorni = 30; break; case LUGLIO: giorni = 31; break; case AGOSTO: giorni = 31; break; case SETTEMBRE: giorni = 30; break; case OTTOBRE: giorni = 31; break; case NOVEMBRE: giorni = 31; break; case DICEMBRE: giorni = 31; } ... 

D VROOX]LLRQH D VR X] RQH switch (mese) { case FEBBRAIO: if (<bisestile>) giorni = 29; else giorni = 28; break; case APRILE: giorni = 30; break; case GIUGNO: giorni = 30; break; case SETTEMBRE: giorni = 30; break; case NOVEMBRE: giorni = 30; break; default: giorni = 31; } D VROX]LRQH switch (mese) { case FEBBRAIO: if (<bisestile>) giorni = 29; else giorni = 28; break; case APRILE: case GIUGNO: case SETTEMBRE: case NOVEMBRE: giorni = 30; break; default: giorni = 31; }

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VHPSLR 8QD FDOFRODWULFH
6L YXROH UHDOL]]DUH XQ SURJUDPPD FKH HPXOL LO FRPSRUWDPHQWR GL XQD FDOFRODWULFH LQ JUDGR GL HIIHWWXDUH OH RSHUD]LRQL DULWPHWLFKH VX GXH RSHUDQGL $ H % 6L VXSSRQJD FKH O¶XWHQWH LPPHWWD O¶RSHUD]LRQH QHO IRUPDWR $ RS % GRYH RS SXRC YDOHUH      
#include <stdio.h> main() { int A, B; char op; printf("Digita l’operazione che vuoi effettuare [formato da usare A op B=] :\n"); scanf("%d%c%d =", &A, &op, &B); switch (op) { case ’+’: printf("%d\n", A+B);break; case ’-’: printf("%d\n", A-B);break; case ’*’: printf("%d\n", A*B);break; case ’%’: printf("%d\n", A%B);break; case ’/’: printf("%d\n", A/B);break; case ’:’: printf("%f\n",(float)A/(float)B);break; /* divisione non intera*/ default: printf("\n operazione non prevista\n"); } }

,VWUX]LRQL GL LWHUD]LRQH while
&RQVHQWH OD ULSHWL]LRQH GHOO¶LVWUX]LRQH FRPSRQHQWH PRGR FRQWUROODWR GD XQD HVSUHVVLRQH 6LLQWDVVLL 6 QWDVV 
ZKLOH HVSUHVVLRQH! LVWUX]LRQH!

LQ

(==0)

Espressione

(!=0)

Istruzione

6LLJQLLILLFDWR 6 JQ I FDWR /¶HVSUHVVLRQH FRQGL]LRQH GL ULSHWL]LRQH YLHQH YDOXWDWD DOO¶LQL]LR GL RJQL FLFOR ‡ /¶LVWUX]LRQH! YLHQH HVHJXLWD ILQFKHC LO YDORUH GHOO¶HVSUHVVLRQH! ULPDQH YHUR GLYHUVR GD ]HUR  ‡ 6L HVFH GDO FLFOR TXDQGR O¶HVSUHVVLRQH UHVWLWXLVFH XQ YDORUH  IDOVR ORJLFR  ‡ 6H LQL]LDOPHQWH HVSUHVVLRQH! KD YDORUH ]HUR LO FRUSR GHO FLFOR QRQ YLHQH PDL HVHJXLWR

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VHPSLLR VRPPD GHLL SULLPLL GLLHFLL LLQWWHULL (VHPS R VRPPD GH SU P G HF Q HU #include <stdio.h> main() { int somma, j; somma=0; j = 1; while (j <= 10) {somma = somma + j; j++; } printf(“Risultato: %d\n”, somma); } (VHPSLLR VFDUWWR GHLL EOODQN LLQ OOHWWWWXUD (VHPS R VFDU R GH E DQN Q H XUD char car=’ ‘; while (Car==‘ ‘) { scanf("%c",&Car); }

0 (VHUFLL]LLR 0 (VHUF ] R 6FULYHUH XQ SURJUDPPD FKH FDOFROL OD PHGLD GHJOL 1 YRWL ULSRUWDWL GD XQR VWXGHQWH /* Media di n voti */ #include <stdio.h> main() { int voto,N,i; float media, sum; printf(“Quanti sono i voti ?”); scanf("%d",&N); sum=0; /* ripeti ...*/ printf(“Dammi un voto:”); scanf("%d",&voto); sum=sum+voto; /* ... per N volte */ media=sum/N; printf("Risultato: %f",media); }

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VHUFLL]LLR FRQWLLQXD  (VHUF ] R FRQW QXD  /* Media di n voti*/ #include <stdio.h> main() { int voto,N,i; float sum, media; printf(“Quanti sono i voti ?”); scanf("%d",&N); sum=0; i=1; while (i <= N) { /* corpo ciclo while */ printf(“Dammi il voto n.%d:”,sum); scanf("%d",&voto); sum=sum+voto; i=i+1; } media=sum/N; printf("Risultato: %f",media); }

/ (VHUFLL]LLR / (VHUF ] R 3URJUDPPD FKH FDOFROD LO SURGRWWR ; < FRPH VHTXHQ]D GL VRPPH VL VXSSRQH <! ; !  
INIZIO

READ: X,Y

Z:=0

SI

X=0 NO X:=X-1

ciclo

Z:=Z+Y

WRITE: Z

FINE

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

&RGLLILLFD &RG I FD /* moltiplicazione come sequenza di somme */ #include <stdio.h> main() { int X,Y,Z; printf(“Dammi i fattori:”); scanf("%d%d",&X,&Y); Z=0; while (X!=0) { /* corpo ciclo while */ Z=Z+Y; X=X-1; } printf("%d",Z); }

&LFOL ,QQHVWDWL
ZKLOH HVSUHVVLRQH! LVWUX]LRQH!

‡ LVWUX]LRQH! SXRC HVVHUH XQD TXDOXQTXH VLQJROD LVWUX]LRQH VHPSOLFH R VWUXWWXUDWD  HYHQWXDOPHQWH DQFKH XQD LVWUX]LRQH ZKLOH Å FLFOL LQQHVWDWL

/ (VHUFLL]LLR / (VHUF ] R 6L OHJJD XQ QXPHUR QDWXUDOH 1 6WDPSDUH XQD YROWD LO QXPHUR  GXH YROWH LO QXPHUR  1 YROWH LO QXPHUR 1 œ 'RPLQLR GL LQJUHVVR 1 œ œ 'RPLQLR GL XVFLWD         œ

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

&RGLLILLFD &RG I FD
INIZIO

#include <stdio.h> main() { int N,I,J; printf(“dammi N:”); scanf("%d",&N); I=1; while (I<=N) { /* corpo ciclo esterno */ printf(“Prossimo valore:”); printf("%d",I); J=1; while (J<I) { /* corpo ciclo interno */ printf("%d",I); J=J+1; } I=I+1; } }

LEGGI: N

I:=1

SI

I>N NO J:=1

STAMPA: I

J:=J+1

NO J>I SI I := I + 1

FINE

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

/ (VHUFL]LR 6WDELOLUH VH XQ QXPHUR QDWXUDOH 1 HC SULPR 8Q QXPHUR QDWXUDOH 1 HC SULPR VH QRQ HC GLYLVLELOH SHU DOFXQ QXPHUR LQWHUR PLQRUH GL HVVR 1RWD QRQ HC QHFHVVDULR HVHJXLUH WXWWH OH GLYLVLRQL GL 1 SHU    Q  PD EDVWD HVHJXLUH OH GLYLVLRQL SHU L QDWXUDOL PLQRUL R XJXDOL DOOD UDGLFH TXDGUDWD GL 1 /* Numero primo */ #include <stdio.h> #include <math.h> main() { typedef enum {false,true} boolean; int N, I; float N1; boolean primo; scanf("%d",&N); N1=sqrt(N); I=2;primo=true; while ((I<=N1) && (primo==true)) {if (((N / I) * I) == N) {primo=false;} else I=I+1; } if (primo==true) printf("numero primo"); else printf("numero non primo"); }

/ (VHUFLL]LLR / (VHUF ] R &DOFROR GHO IDWWRULDOH GL XQ QXPHUR LQWHUR QRQ QHJDWLYR 1 ‡ IDWWRULDOH   ‡ IDWWRULDOH 1 1 1   IDWWRULDOH 1 1 /* Calcolo del fattoriale */ #include <stdio.h> #include <math.h> main() { int N, F, I; printf(“Dammi N:”); scanf("%d",&N); F=1; I=2; while (I <= N) { F=F*I; I=I+1; } printf("%s%d","Fattoriale: ",F); }

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VHPSLLR (VHPS R 'LYLVLRQH WUD QXPHUL LQWHUL SRVLWLYL DWWUDYHUVR VRPPD H VRWWUD]LRQH /* divisione tra interi positivi */ #include <stdio.h> main() { unsigned int X,Y,Quoz,Resto; scanf("%d%d",&X,&Y); Resto=X; Quoz=0; while (Resto >= Y) { Quoz=Quoz+1; Resto=Resto-Y; } printf("%d%s%d%s%d%s%d", X," diviso ", Y, " = ", Quoz, " resto ", Resto); } ‡

,VWUX]LRQL GL LWHUD]LRQH GRZKLOH
1HOO
LVWUX]LRQH ZKLOH OD FRQGL]LRQH GL ULSHWL]LRQH YLHQH YHULILFDWD DOO¶LQL]LR GL RJQL FLFOR

,VWUX]LLRQH GR ,VWUX] RQH GR FRQVHQWH GL ULSHWHUH O¶LVWUX]LRQH HVHJXHQGR LO FRQWUROOR D ILQH LWHUD]LRQH 6LLQWDVVLL 6 QWDVV  GR LVWUX]LRQH! ZKLOH HVSUHVVLRQH!  ‡ /D FRQGL]LRQH GL ULSHWL]LRQH YLHQH YHULILFDWD DOOD ILQH GL RJQL FLFOR

+ ‡ OD SULPD ULSHWL]LRQH YLHQH VHPSUH HVHJXLWD

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,VWUX]LLRQH GR ,VWUX] RQH GR

(VHPSLLR (VHPS R do scanf("%c",&Car) while (Car ==’ ’) /* salta spazi bianchi */
S1

3ULPD GHOO¶HVHFX]LRQH GHO FLFOR LO YDORUH GL &DU HC LQGHWHUPLQDWR &RQ LLOO ZKLLOOH &RQ ZK H &DU

ZKLOH &DU

...

Sn

VFDQI

F 

&DU 

true Espressione false

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,VWUX]LRQL 5LSHWLWLYH IRU
(C XQD LVWUX]LRQH GL ULSHWL]LRQH SDUWLFRODUPHQWH DGDWWD SHU UHDOL]]DUH FLFOL D FRQWDWRUH 6LLQWDVVLL 6 QWDVV 
IRU HVSUHVVLRQH! HVSUHVVLRQH! HVSUHVVLRQH! LVWUX]LRQH!

(VHPSLLR ZKLLOOHIRU (VHPS R ZK H IRU somma = 0; /* while */ j = 1; while (j <= n) { somma=somma+j; j++; } FRQ LLOO IRU FRQ IRU somma = 0; /* for */ for(j=1;j<=n;j++) somma = somma + j;

6LLJQLLILLFDWR 6 JQ I FDWR ‡ HVSUHVVLRQH! HC O
HVSUHVVLRQH GL LQL]LDOL]]D]LRQH YLHQH HVHJXLWD XQD YROWD VROD SULPD GL HQWUDUH QHO FLFOR ‡ HVSUHVVLRQH! UDSSUHVHQWD OD FRQGL]LRQH GL SHUPDQHQ]D QHO FLFOR YDOXWDWD DOO
LQL]LR GL RJQL LWHUD]LRQH ‡ HVSUHVVLRQH! HC O
HVSUHVVLRQH GL SDVVDJJLR DO FLFOR VXFFHVVLYR YDOXWDWD DOOD ILQH GL RJQL LWHUD]LRQH  IRU q HTXLLYDOOHQWH D IRU q HTX YD HQWH D HVSUHVVLRQH!  ,QL]LDOL]]D]LRQH  ZKLOH HVSUHVVLRQH!  &RQGL]LRQH GL 5LSHWL]LRQH  ^ LVWUX]LRQH! HVSUHVVLRQH!  ,QL]LR1XRYR&LFOR  `

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)RU )RU
IRU HVSUHVVLRQH! HVSUHVVLRQH! HVSUHVVLRQH! LVWUX]LRQH!

(VHPSLLR IDWWRULLDOOH FRQ LLVWWUX]LLRQH IRU (VHPS R IDWWRU D H FRQ V UX] RQH IRU

‡ ‡

2JQXQD GHOOH HVSUHVVLRQL SXz HVVHUH RPHVVD LO SXQWR H YLUJROD GHYH ULPDQHUH 6H PDQFD HVSUHVVLRQH VL KD XQ FLFOR LQILQLWR

/* Calcolo del fattoriale */ #include <stdio.h> #include <math.h> main() { int N, F, I; printf(“Dammi N:”); scanf("%d",&N); F=1; I=2; for (I=2,F=1;I<=N;I++) F=F*I; printf("%s%d","Fattoriale: ",F); }

&RVD HVHJXRQR L VHJXHQWL IRU " IRU L  L  Q L SULQWI IRU  ^  ` G L 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,VWUX]LLRQLL SHU LLOO WUDVIHULLPHQWR GHOO FRQWUROOOOR ,VWUX] RQ SHU WUDVIHU PHQWR GH FRQWUR R ,VWUX]LRQH EUHDN /
LVWUX]LRQH EUHDN SURYRFD O
XVFLWD LPPHGLDWD GDO FLFOR R GD XQ
LVWUX]LRQH VZLWFK LQ FXL q UDFFKLXVD

(VHPSLLR FRQWLLQXH (VHPS R FRQW QXH for (i = N; i > 0; i – – ) { /* Salta alla prossima ripetizione se N è multiplo di i */ if (N % i) continue; /* Esci dal ciclo se i vale 55 */ if (i == 55) break; ... }

,VWUX]LLRQH FRQWLLQXH ,VWUX] RQH FRQW QXH /
LVWUX]LRQH FRQWLQXH SURYRFD O
LQL]LR GHOOD VXFFHVVLYD LWHUD]LRQH GHO FLFOR LQ FXL q UDFFKLXVD QRQ VL DSSOLFD D VZLWFK  for (i = val1; i <= val2; i + + ) { ... ; if ... continue; else ... }

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

,VWUX]LLRQH JRWR ,VWUX] RQH JRWR JRWR ODEHO /
LVWUX]LRQH JRWR SURYRFD LO VDOWR DOO
LVWUX]LRQH HWLFKHWWDWD GDOO
HWLFKHWWD ODEHO 8QD ODEHO q XQ LGHQWLILFDWRUH VHJXLWR GD XQ GXH SXQWL H SXz HVVHUH DSSOLFDWD DG XQD TXDOXQTXH LVWUX]LRQH GHOOD PHGHVLPD IXQ]LRQH LQ FXL VL WURYD LO JRWR  JRWR HUURUH  HUURUH   )RUPDOPHQWH O
LVWUX]LRQH JRWR QRQ q QHFHVVDULD 8VDUH LO JRWR VROR QHL UDUL FDVL LQ FXL LO VXR XVR UHQGH SL OHJJLELOH LO FRGLFH  DG HVHPSLR ‡ SHU XVFLUH GDOO
LQWHUQR GL VWUXWWXUH PROWR QLGLILFDWH ‡ SHU FRQYHUJHUH LQ FDVR GL HUURUH LQ XQ XQLFR SXQWR GD SXQWL GLYHUVL GHO SURJUDPPD 3UREOHPL VH VL HQWUD LQ IOXVVR LQQHVWDWR SURFHGXUH R EORFFKL

0 (VHUFLL]LL 0 (VHUF ]  /HJJHUH XQD VHTXHQ]D GL QXPHUL LQWHUL D WHUPLQDOH H FDOFRODUQH OD VRPPD QHL VHJXHQWL FDVL    /D OXQJKH]]D GHOOD VHTXHQ]D QRQ HC QRWD PD O¶XOWLPR YDORUH HC VHJXLWR GD XQ LQWHUR QHJDWLYR ZKLOH  &RPH LO FDVR  PD VLFXUDPHQWH OD VHTXHQ]D KD OXQJKH]]D PDJJLRUH R XJXDOH DG XQR GR  /D VHTXHQ]D GL QXPHUL GD VRPPDUH HC SUHFHGXWD GD XQ QXPHUR LQWHUR FKH UDSSUHVHQWD OD OXQJKH]]D GHOOD VHTXHQ]D IRU 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

(VSUHVVLRQL FRPSOHPHQWL
2SHUDWRULL VXLL ELLW 2SHUDWRU VX E W  !! _ A a VKLIW D VLQLVWUD VKLIW D GHVWUD DQG ELW D ELW RU LQFOXVLYR ELW D ELW RU HVFOXVLYR ELW D ELW FRPSOHPHQWR D  N VKLIW D VLQLVWUD GL  ELW HTXLYDOH D N  N!! VKLIW D GHVWUD GL  ELW HTXLYDOH D N

3UHFHGHQ]D HG $VVRFLLDWLLYLLWDC GHJOOLL 2SHUDWRULL 3UHFHGHQ]D HG $VVRF DW Y WDC GHJ 2SHUDWRU /D SUHFHGHQ]D GHJOL RSHUDWRUL q VWDELOLWD GDOOD VLQWDVVL GHOOH HVSUHVVLRQL DE F HTXLYDOH VHPSUH D D  E F /
DVVRFLDWLYLWj q DQFRUD VWDELOLWD GDOOD VLQWDVVL GHOOH HVSUHVVLRQL DEF HTXLYDOH VHPSUH D D  E  F

2SHUDWRUH FRQGLL]LLRQDOOH 2SHUDWRUH FRQG ] RQD H " ‡ ‡ FRQGL]LRQH " SDUWH9HUD  SDUWH)DOVH OD SDUWH9HUD YLHQH YDOXWDWD VROR VH OD FRQGL]LRQH q YHULILFDWD YDORUH GLYHUVR GD  OD SDUWH)DOVH YLHQH YDOXWDWD VROR VH OD FRQGL]LRQH QRQ q YHULILFDWD YDORUH XJXDOH D ]HUR [ N \  " \  ,1),1,7<  D  E " D  E  DVVHJQD LO PLQRUH 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

)21'$0(17, ', ,1)250$7,&$

,/ /,1*8$**,2 & 

7LSL GL 'DWR 6WUXWWXUDWL
7LSL GL 'DWR

9HWWRUL
8Q YHWWRUH HC XQ LQVLHPH RUGLQDWR GL HOHPHQWL WXWWL GHOOR VWHVVR WLSR &DUDWWHULLVWLLFKH GHOO YHWWRUH &DUDWWHU VW FKH GH YHWWRUH

6HPSOLFL

6WUXWWXUDWL

‡ RPRJHQHLWDC ‡ RUGLQDPHQWR RWWHQXWR PHGLDQWH GHL YDORUL LQWHUL LQGLFL FKH FRQVHQWRQR GL DFFHGHUH DG RJQL HOHPHQWR GHOOD VWUXWWXUD
1RQ3ULPLWLYL 

3ULPLWLYL int, char, float, double

1RQ3ULPLWLYL ULGHILQLWL enum puntatori

&RVWUXWWRUL 3ULPLWLYL vettori struct union 

   Q

; < = 

7LLSLL GLL GDWR VWUXWWXUDWLL 7 S G GDWR VWUXWWXUDW  , GDWL VWUXWWXUDWL R VWUXWWXUH GL GDWL VRQR RWWHQXWL PHGLDQWH FRPSRVL]LRQH GL DOWUL GDWL GL WLSR VHPSOLFH RSSXUH VWUXWWXUDWR  7LLSLL VWUXWWXUDWLL LLQ & 7 S VWUXWWXUDW Q & ‡ YHWWRUL R DUUD\ ‡ UHFRUG VWUXFW ‡ UHFRUG YDULDQWL XQLRQ

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

374

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

375

'HILQL]LRQH GL 9HWWRUL LQ &
1HO OLQJXDJJLR & SHU GHILQLUH YHWWRUL VL XVD LO FRVWUXWWRUH GL WLSR > @ 6LLQWDVVLL 6 QWDVV  
LGWLSR! LGYDULDELOH! >GLPHQVLRQH!@

9HWWRUL
,QGLLFLL ,QG F  œ HC SRVVLELOH ULIHULUH XQD VLQJROD FRPSRQHQWH VSHFLILFDQGR O
LQGLFH L FRUULVSRQGHQWH 9>L@ œ /
LQGLFH GHYH HVVHUH GL WLSR LQWHJUDO W\SH FLRHC LQW FKDU R HQXP  œ VH 1 HC OD GLPHQVLRQH LO GRPLQLR GHJOL LQGLFL HC >1@ $G HVHPSLLR $G HVHPS R

GRYH ‡ LGWLSR! HC O¶LGHQWLILFDWRUH GL WLSR GHJOL HOHPHQWL FRPSRQHQWL ‡ GLPHQVLRQH! UDSSUHVHQWD LO QXPHUR GHJOL HOHPHQWL FRPSRQHQWL HC XQD FRVWDQWH LQWHUD ‡ LGYDULDELOH! HC O¶LGHQWLILFDWRUH GHOOD YDULDELOH VWUXWWXUDWD FRVLC GHILQLWD

(VHPSLLR (VHPS R LQW 9>@  YHWWRUH GL GLHFL HOHPHQWL LQWHUL  + /D GLPHQVLRQH QXPHUR GL HOHPHQWL GHO YHWWRUH GHYH HVVHUH XQD FRVWDQWH LQWHUD QRWD DO PRPHQWR GHOOD GLFKLDUD]LRQH LQW 1 FKDU 9 >1@  ! HC VEDJOLDWR

int A[3];

0 1 2

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

376

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

377

9HWWRUL
2SHUDWRULL 2SHUDWRU  ,Q & QRQ HVLVWRQR RSHUDWRUL VSHFLILFL SHU L YHWWRUL SHU RSHUDUH VXL YHWWRUL q QHFHVVDULR RSHUDUH VLQJRODUPHQWH VXJOL HOHPHQWL FRPSRQHQWL FRHUHQWHPHQWH FRQ LO WLSR DG HVVL DVVRFLDWR  + QRQ HC SRVVLELOH O
DVVHJQDPHQWR GLUHWWR WUD YHWWRUL int V[10], W[10]; … V=W; /* e` scorretto! */ + 1RQ HC SRVVLELOH OHJJHUH R VFULYHUH XQ LQWHUR YHWWRUH D SDUWH FRPH YHGUHPR OH VWULQJKH  RFFRUUH OHJJHUHVFULYHUH OH VXH FRPSRQHQWL DG HVHPSLR SHU OHJJHUH XQ YHWWRUH int V[100]; int i; for(i=0; i<100; i++) { printf(“valore %d-simo? “, i); scanf(“%d”, &V[i]); }

*HVWLLRQH GHJOOLL HOOHPHQWLL GLL XQ YHWWRUH *HVW RQH GHJ H HPHQW G XQ YHWWRUH /H VLQJROH FRPSRQHQWL GL XQ YHWWRUH SRVVRQR HVVHUH PDQLSRODWH FRHUHQWHPHQWH FRQ LO WLSR DG HVVH DVVRFLDWR $G HVHPSLLR LLQWW $>@ $G HVHPS R Q $>@ + DJOL HOHPHQWL GL $ HC SRVVLELOH DSSOLFDUH WXWWL JOL RSHUDWRUL GHILQLWL SHU JOL LQWHUL 4XLQGL A[i] =n%i; A[10]=A[0]+7; scanf("%d", &A[i]); …

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

378

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

379

9HWWRUL
+ /
LQGLFH GHYH HVVHUH GL WLSR LQWHJUDO W\SH LQW FKDU R HQXP  $G HVHPSLR #include <stdio.h> typedef enum{blu, giallo, rosso} indice; main() {indice k=blu; int A[3]; for(k=blu; k<=rosso; k++) { printf("Dammi elemento %d: ", k); scanf("%d", &A[k]); } printf("Valore %d:%d\n",blu,A[blu]); printf("Valore%d:%d\n",giallo,A[giallo]); printf("Valore %d:%d\n",rosso,A[rosso]); }

9HWWRUH FRPH FRVWUXWWRUH GL WLSR
,Q & HC SRVVLELOH XWLOL]]DUH LO FRVWUXWWRUH > @ SHU LQWURGXUUH WLSL GL GDWR QRQ SULPLWLYL FKH UDSSUHVHQWDQR SDUWLFRODUL YHWWRUL 6LLQWDVVLL GHOOOOD GLLFKLLDUD]LLRQH 6 QWDVV GH D G FK DUD] RQH W\SHGHI WLSRFRPSRQHQWH! WLSRYHWWRUH! >GLP!@ GRYH ‡ WLSRFRPSRQHQWH! HC O
LGHQWLILFDWRUH GL WLSR GL RJQL VLQJROD FRPSRQHQWH ‡ WLSRYHWWRUH! HC O
LGHQWLILFDWRUH FKH VL DWWULEXLVFH DO QXRYR WLSR ‡ GLP! HC LO QXPHUR GL HOHPHQWL FKH FRVWLWXLVFRQR LO YHWWRUH GHYH HVVHUH XQD FRVWDQWH  $G HVHPSLLR $G HVHPS R typedef int Vettori[30]; Vettori V1,V2;

+ 9 H 9 VRQR YDULDELOL GL WLSR 9HWWRUL RJQXQR + 9 H 9 SRVVRQR HVVHUH XWLOL]]DWL FRPH YHWWRUL GL LQWHUL

UDSSUHVHQWD XQ YHWWRUH GL  HOHPHQWL LQWHUL

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

380

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

381

9HWWRUL
5LLDVVXPHQGR 5 DVVXPHQGR 9DULLDELLOOLL GLL WLLSR YHWWRUH 9DU DE G W SR YHWWRUH WLSRFRPSRQHQWH! QRPH!>GLP!@ 9HWWRUH FRPH FRVWUXWWRUH GLL WLLSR 9HWWRUH FRPH FRVWUXWWRUH G W SR W\SHGHI WLSRFRPSRQHQWH! WLSRYHWWRUH! >GLP!@ 9LLQFROOLL 9 QFR  ‡ GLP! HC XQD FRVWDQWH LQWHUD ‡ WLSRFRPSRQHQWH! H
XQ TXDOVLDVL WLSR VHPSOLFH R VWUXWWXUDWR 8VR 8VR ‡ LO YHWWRUH H
XQD VHTXHQ]D GL GLPHQVLRQH ILVVDWD GLP! GL FRPSRQHQWL GHOOR VWHVVR WLSR WLSRBFRPSRQHQWH! ‡ OD VLQJROD FRPSRQHQWH LHVLPD GL XQ YHWWRUH 9 H
LQGLYLGXDWD GDOO
LQGLFH LHVLPR VHFRQGR OD QRWD]LRQH 9>L@ + /¶LQWHUYDOOR GL YDULD]LRQH GHJOL LQGLFL HC >GLP!@ ‡ VXL VLQJROL HOHPHQWL HC SRVVLELOH RSHUDUH VHFRQGR OH PRGDOLWDC SUHYLVWH GDO WLSR WLSRBFRPSRQHQWH!

,QL]LDOL]]D]LRQH GL XQ YHWWRUH
0HGLLDQWH XQ FLLFOOR 0HG DQWH XQ F F R 3HU DWWULEXLUH XQ YDORUH LQL]LDOH DJOL HOHPHQWL GL XQ YHWWRUH VL SXRC DWWXDUH FRQ XQD VHTXHQ]D GL DVVHJQDPHQWL DOOH 1 FRPSRQHQWL GHO YHWWRUH $G (VHPSLLR $G (VHPS R #define N 30 typedef int vettore [N]; vettore v; int i; ... for(i=0; i<N;i++) v[i]=0; + /D GHILQH UHQGH PRGLILFDELOH LO SURJUDPPD SLXC IDFLOPHQWH

,QLL]LLDOOLL]]D]LLRQH LLQ IDVH GLL GHILLQLL]LLRQH ,Q ] D ]]D] RQH Q IDVH G GHI Q ] RQH ,Q DOWHUQDWLYD HC SRVVLELOH LQL]LDOL]]DUH XQ YHWWRUH LQ IDVH GL GHILQL]LRQH (VHPSLLR (VHPS R int v[10] = {1,2,3,4,5,6,7,8,9,10}; /* v[0] = 1; v[1]=2;...v[9]=10; */

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

382

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

383

$GGLULWWXUD HC SRVVLELOH IDUH int v[]={1,2,3,4,5,6,7,8,9,10}; + /D GLPHQVLRQH HC GHOO
LQL]LDOL]]D]LRQH GHWHUPLQDWD VXOOD EDVH

(VHPSLLR (VHPS R 6RPPD GL GXH YHWWRUL VL UHDOL]]L XQ SURJUDPPD FKH GDWL GD VWDQGDUG LQSXW JOL HOHPHQWL GL GXH YHWWRUL $ H % HQWUDPEL GL  LQWHUL FDOFROL H VWDPSL JOL HOHPHQWL GHO YHWWRUH & DQFRUD GL  LQWHUL  RWWHQXWR FRPH OD VRPPD GL $ H % #include <stdio.h> typedef int vettint[10]; main() { vettint A,B,C; int i; /* lettura dei dati */ for(i=0; i<10; i++) { printf(“valore di A[%d] ?, i); scanf(“%d”, &A[i]); } for(i=0; i<10; i++) { printf(“valore di B[%d] ?, i); scanf(“%d”, &B[i]); } /* calcolo del risultato */ for(i=0; i<10; i++) C[i]=A[i]+B[i]; /* stampa del risultato */ for(i=0; i<10; i++) printf(“C[%d]=%d\n”,i, C[i]); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

384

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

385

(VHPSLLR (VHPS R /HJJHUH GD LQSXW DOFXQL FDUDWWHUL DOIDEHWLFL PDLXVFROL VL VXSSRQJDQR DO PDVVLPR  H ULVFULYHUOL LQ XVFLWD HYLWDQGR GL ULSHWHUH FDUDWWHUL JLDC VWDPSDWL 6ROOX]LLRQH 6R X] RQH ZKLOH FL VRQR FDUDWWHUL GD OHJJHUH! ^ OHJJL FDUDWWHUH! LI QRQ JLDC PHPRUL]]DWR! PHPRUL]]DOR LQ XQD VWUXWWXUD GDWL! ` ZKLOH FL VRQR HOHPHQWL GHOOD VWUXWWXUD GDWL! VWDPSD HOHPHQWR! 2FFRUUH XQD VWUXWWXUD GDWL LQ FXL PHPRUL]]DUH VHQ]D ULSHWL]LRQL JOL HOHPHQWL OHWWL LQ LQJUHVVR FKDU $>@

&RGLLILLFD &RG I FD #include <stdio.h> main() { char A[10], c; int i, j, inseriti, trovato; inseriti=0; printf("\n Dammi 10 caratteri: "); for (i=0; (i<10); i++) { scanf("%c", &c); /* verifica unicita‘: */ trovato=0; for(j=0;(j<inseriti)&&!trovato;j++) if (c==A[j]) trovato=1; if (!trovato) { A[inseriti]=c; inseriti++; } } printf("Inseriti %d caratteri \n", inseriti); for (i=0; i<inseriti; i++) printf(“%c\n”, A[i]); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

386

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

387

9HWWRUL PXOWLGLPHQVLRQDOL
1RQ YL VRQR YLQFROL VXO WLSR GHJOL HOHPHQWL GL XQ YHWWRUH + *OL HOHPHQWL GL XQ YHWWRUH SRVVRQR HVVHUH D ORUR YROWD GL WLSR YHWWRUH LQ TXHVWR FDVR VL SDUOD GL YHWWRUL PXOWLGLPHQVLRQDOL R PDWULFL 'HILLQLL]LLRQH GLL YHWW PXOOWLLGLLPHQVLLRQDOOLL PDWULLFLL  'HI Q ] RQH G YHWW PX W G PHQV RQD PDWU F  
LGWLSR! LGYDULDEOH! >GLP@ >GLP@  >GLPQ@

$G HVHPSLLR $G HVHPS R float M[20][30]; HC XQ YHWWRUH GL  HOHPHQWL RJQXQR GHL TXDOL HC XQ YHWWRUH GL  HOHPHQWL RJQXQR GHL TXDOL HC XQ IORDW
0 1 ..... 0 1 . . . . 19 29

6LLJQLLILLFDWR 6 JQ I FDWR LGYDULDELOH! HC LO QRPH GL XQD YDULDELOH GL WLSR YHWWRUH GL GLP FRPSRQHQWL RJQXQD GHOOH TXDOL HC XQ YHWWRUH GL GLP FRPSRQHQWL RJQXQD GHOOH TXDOL HC XQ YHWWRUH GL « RJQXQD GHOOH TXDOL HC XQ YHWWRUH GL GLPQ FRPSRQHQWL GL WLSR LGWLSR!

0 HC XQD PDWULFH [ GL UHDOL
+ 0>@ HC XQ YHWWRUH GL  UHDOL OD SULPD FRPSRQHQWH GL

0 

œ 0>@>@ HC XQ UHDOH O¶XOWLPR HOHPHQWR GHO VHFRQGR YHWWRUH FRPSRQHQWH GL 0 

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

388

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

389

'LLFKLLDUD]LLRQH GLL WLLSLL YHWWRULL PXOOWLLGLLPHQVLLRQDOOLL ' FK DUD] RQH G W S YHWWRU PX W G PHQV RQD 
W\SHGHI LGWLSR! LGWLSRYHWWRUH! >GLP@ >GLP@  >GLPQ@

,QL]LDOL]]D]LRQH GL PDWULFL
$QFKH QHO FDVR GL YHWWRUL PXOWLGLPHQVLRQDOL O¶LQL]LDOL]]D]LRQH VL SXRC HIIHWWXDUH LQ IDVH GL GHILQL]LRQH WHQHQGR FRQWR FKH LQ TXHVWR FDVR JOL HOHPHQWL VRQR D ORUR YROWD YHWWRUL $G HVHPSLR int matrix[4][4] ={{1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1}}; /D PHPRUL]]D]LRQH DYYLHQH ³SHU ULJKH´ PDWUL[                        

(VHPSLL (VHPS  typedef float MatReali [20] [30]; MatReali Mat; /*Mat e‘ un vettore di venti elementi, ognuno dei quali e‘ un vettore di trenta reali; quindi, matrice di 20™30 di reali*/ $OOWUR HVHPSLLR $ WUR HVHPS R typedef float VetReali[30]; typedef VetReali MatReali[20]; MatReali Mat;

6L SXRC DQFKH RPHWWHUH OD GLPHQVLRQH int matrix[][4]={{1,0,0,0}, {0,1,0,0}, {0,0,1,0}, {0,0,0,1}};

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

390

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

391

(VHPSLLR OOHWWWWXUD H VWWDPSD GLL PDWWULLFLL (VHPS R H XUD H V DPSD G PD U F #include <stdio.h> #define R 10 #define C 25 typedef float matrice[R][C]; main() { matrice M; int i, j; /* lettura: */ for(i=0; i<R; i++) for(j=0; j<C; j++) { printf(“M[%d][%d]? “, i, j); scanf(“%f”, &M[i][j]); } /*stampa: */ for(i=0; i<R; i++) { for(j=0; j<C; j++) printf(“%f\t“, M[i][j); printf(“\n”); } }

0 (VHUFLL]LLR 0 (VHUF ] R 3URJUDPPD FKH HVHJXH LO SURGRWWR ULJKH ™ FRORQQH GL PDWULFL TXDGUDWH 1™1 D YDORUL LQWHUL &>LM@ N 1 $>L@>N@ %>N@>M@ #include <stdio.h> #define N 2 main() { typedef int Matrici[N][N]; int Somma; int i,j,k; Matrici A,B,C; /* inizializzazione di A e B */ for (i=0; i<N; i++) for (j=0; j<N; j++) scanf("%d",&A[i][j]); for (i=0; i<N; i++) for (j=0; j<N; j++) scanf("%d",&B[i][j]); /* prodotto matriciale */ for (i=0; i<N; i++) for (j=0; j<N; j++) { Somma=0; for (k=0; k<N; k++) Somma=Somma+A[i][k]*B[k][j]; C[i][j]=Somma; } /* stampa */ for (i=0; i<N; i++) for (j=0; j<N; j++) printf("%d",C[i][j]); }
FONDAMENTI DI INFORMATICA

S

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

392

IL LINGUAGGIO C

393

0 (VHUFLL]LLR 0 (VHUF ] R 'DWL Q YDORUL LQWHUL IRUQLWL LQ RUGLQH TXDOXQTXH VWDPSDUH LQ XVFLWD O
HOHQFR GHL YDORUL GDWL LQ RUGLQH FUHVFHQWH + (C QHFHVVDULR PDQWHQHUH LQ PHPRULD WXWWL L YDORUL GDWL SHU SRWHU HIIHWWXDUH L FRQIURQWL QHFHVVDUL º 8WLOL]]LDPR L YHWWRUL 2UGLLQDPHQWR GLL XQ YHWWRUH 2UG QDPHQWR G XQ YHWWRUH (VLVWRQR YDUL SURFHGLPHQWL ULVROXWLYL Y DOJRULWPL GL RUGLQDPHQWR  XQR GL TXHVWL HC LO 0HWRGR GHL 0DVVLPL VXFFHVVLYL 'DWR XQ YHWWRUH LQW 9>GLP@  HOHJJL XQ HOHPHQWR FRPH PDVVLPR WHPSRUDQHR 9>PD[@  FRQIURQWD LO YDORUH GL 9>PD[@ FRQ WXWWL JOL DOWUL HOHPHQWL GHO YHWWRUH 9>L@  VH 9>L@!9>PD[@  PD[ L  TXDQGR KDL ILQLWR L FRQIURQWL VH PD[ GLP VFDPELD 9>PD[@ FRQ 9>GLP@ ø LO PDVVLPR RWWHQXWR GDOOD VFDQVLRQH YD LQ XOWLPD SRVL]LRQH  ULGXFL LO YHWWRUH GL XQ HOHPHQWR GLP GLP H VH GLP! WRUQD D 

&RGLLILLFD &RG I FD ‡ 3ULPR OLYHOOR GL VSHFLILFD #include <stdio.h> #define dim 10 main() { int V[dim], i,j, max, tmp, quanti; /* lettura dei dati */ /*ordinamento */ for(i=0; i<dim; i++) {quanti=dim-i; /*ciclo di scansione del vettore i-simo (di dimensione=quanti) */ } } /*stampa del vettore V ordinato */ }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

394

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

395

&RGLLILLFD &RG I FD #include <stdio.h> #define dim 10 main() { int V[dim], i,j, max, tmp, quanti; /* lettura dei dati */ for (i=0; i<dim; i++) { printf("valore n. %d: ",i); scanf("%d", &V[i]); } /*ordinamento */ for(i=0; i<dim; i++) {quanti=dim-i; max=quanti-1; for( j=0; j<quanti; j++) { if (V[j]>V[max]) max=j; } if (max<quanti-1) { /*scambio */ tmp=V[quanti-1]; V[quanti-1]=V[max]; V[max]=tmp; } } /*stampa */ for(i=0; i<dim; i++) printf("Valore di V[%d]=%d\n", i, V[i]); }
FONDAMENTI DI INFORMATICA
IL LINGUAGGIO C 396

9HWWRUL GL &DUDWWHUL OH 6WULQJKH
8QD VWULQJD HC XQ YHWWRUH GL FDUDWWHUL PDQLSRODWR H JHVWLWR OD FRQYHQ]LRQH RJQL VWULQJD HC WHUPLQDWD GDO FDUDWWHUH QXOOR
?
YDORUH GHFLPDOH ]HUR  + (C UHVSRQVDELOLWDC GHO SURJUDPPDWRUH JHVWLUH WDOH VWUXWWXUD LQ PRGR FRQVLVWHQWH FRQ LO FRQFHWWR GL VWULQJD DG HVHPSLR JDUDQWHQGR OD SUHVHQ]D GHO WHUPLQDWRUH
? 
$G HVHPSLLR $G HVHPS R
char A[10]={’b’,’o’,’l’,’o’,’g’,’n’,’a’,’\0’};

µE¶

µR¶

µO¶

µR¶

µJ¶

µQ¶

µD¶

µ?¶

"

"

RSSXUH
char A[10]="bologna"; /* il terminatore ’\0’ e‘ aggiunto automaticamente */

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

397

(VHPSLLR (VHPS R 3URJUDPPD FKH FDOFROD OD OXQJKH]]D FLRHC LO QXPHUR GL FDUDWWHUL VLJQLILFDWLYL GL XQD VWULQJD #include <stdio.h>

(VHPSLLR (VHPS R 3URJUDPPD FKH FRQFDWHQD GXH VWULQJKH GDWH #include <stdio.h> /* concatenamento di due stringhe */

/* lunghezza di una stringa */ main() { char str[81]; /* str ha al max. 80 caratteri*/ int i; printf("\nImmettere una stringa:"); scanf("%s",&str); /* %s formato stringa */ for (i=0; str[i]!=’\0’; i++); printf("\nLunghezza: \t %d\n",i); } 9HQJRQR DFTXLVLWL L FDUDWWHUL LQ LQJUHVVR ILQR DO SULPR FDUDWWHUH GL VSD]LDWXUD ELDQFR QHZOLQH WDEXOD]LRQH VDOWL SDJLQD  + OD OXQJKH]]D GL XQD VWULQJD VL SXRC DQFKH FDOFRODUH XWLOL]]DQGR OD IXQ]LRQH VWDQGDUG GL OLEUHULD VWUOHQ SUHYLD LQFOXVLRQH GL VWULQJK

main() { char int

s1[81], s2[81]; l,i;

printf("\nPrima stringa: \t"); scanf("%s",&s1); printf("\nSeconda stringa: \t"); scanf("%s",&s2); for (l=0; s1[l]!=’\0’; l++); for (i=0; s2[i]!=’\0’ && i+l<79; i++) s1[i+l]=s2[i]; s1[i+l]=’\0’; /* fine stringa */

printf("\nStringa:\t%s\n",s1); } + LO FRQFDWHQDPHQWR GL GXH VWULQJKH VL SXRC DQFKH RWWHQHUH XWLOL]]DQGR OD IXQ]LRQH VWDQGDUG GL OLEUHULD VWUFDW SUHYLD LQFOXVLRQH GL VWULQJK

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

398

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

399

/LEUHULD VWDQGDUG VXOOH VWULQJKH
,O & IRUQLVFH XQD OLEUHULD VWDQGDUG GL IXQ]LRQL SHU OD JHVWLRQH GL VWULQJKH 3HU SRWHUOD XWLOL]]DUH HC QHFHVVDULR LQFOXGHUH LO ILOH KHDGHU VWULQJK! #include <string.h> 7UD WXWWH OH IXQ]LRQL SLXC FRPXQHPHQWH XWLOL]]DWH VRQR œ VWUOHQ œ VWUFPS œ VWUFDW œ VWUFS\

/LEUHULD VWDQGDUG VXOOH VWULQJKH
œ /XQJKH]]D GLL XQD VWULLQJD œ /XQJKH]]D G XQD VWU QJD LQW VWUOHQ FKDU VWU> @  UHVWLWXLVFH OD OXQJKH]]D FLRHC LO QXPHUR GL FDUDWWHUL VLJQLILFDWLYL GHOOD VWULQJD VWU VSHFLILFDWD FRPH DUJRPHQWR $G HVHPSLR char S[10]="bologna"; int k; … k=strlen(S); /* k vale 7*/

œ &RQIURQWR WUD VWULLQJKH œ &RQIURQWR WUD VWU QJKH LQW VWUFPS FKDU VWU> @ FKDU VWU> @ HVHJXH LO FRQIURQWR WUD OH GXH VWULQJKH GDWH VWU H VWU 5HVWLWXLVFH œ VH OH GXH VWULQJKH VRQR LGHQWLFKH œ XQ YDORUH QHJDWLYR DG HVHPSLR   VH VWU SUHFHGH VWU LQ RUGLQH OHVVLFRJUDILFR  œ XQ YDORUH SRVLWLYR DG HVHPSLR   VH VWU SUHFHGH VWU LQ RUGLQH OHVVLFRJUDILFR 

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

400

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

401

$G HVHPSLR char S1[10]="bologna"; char S2[10]="napoli"; int k; … k=strcmp(S1,S2); /* k < 0 */ k=strcmp(S1,S1); /* k=0*/ k=strcmp(S2,S1); /* k>0 */ œ &RQFDWHQDPHQWR GLL VWULLQJKH œ &RQFDWHQDPHQWR G VWU QJKH VWUFDW FKDU VWU> @ FKDU VWU> @ FRQFDWHQD OH  VWULQJKH GDWH VWU H VWU ,O ULVXOWDWR GHO FRQFDWHQDPHQWR HC LQ VWU $G HVHPSLR char S1[10]="reggio"; char S2[10]="emilia"; strcat(S1, S2); /*S1="reggioemilia"*/ œ &RSLLD GLL VWULLQJKH œ &RS D G VWU QJKH VWUFS\ FKDU VWU> @ FKDU VWU> @  FRSLD OD VWULQJD VWU LQ VWU $G HVHPSLR char S1[10]="Giuseppe"; char S2[10]; … strcpy(S1,S2); /* S1="Giuseppe"*/

,QSXW2XWSXW D FDUDWWHUL
, GLVSRVLWLYL GL LQJUHVVR HG XVFLWD VRQR YLVWL FRPH ILOH GL FDUDWWHUL ILOH WHVWR WHUPLQDWL GD XQD PDUFD VSHFLDOH GL HQGRIILOH (2) HG RUJDQL]]DWL HYHQWXDOPHQWH VX SLXC OLQHH QHZOLQH
?Q 

a 7 1 3.7 b 57 * c 6

testina lettura

fine linea

fine file (EOF) (ctrl-Z)

(VLVWRQR IXQ]LRQL OLEUHULD VWDQGDUG SHU OHJJHUH H VFULYHUH VLQJROL FDUDWWHUL ‡ JHWFKDU UHVWLWXLVFH LO SURVVLPR FDUDWWHUH GLVSRQLELOH LQ LQJUHVVR ‡ SXWFKDU VWDPSD XQ FDUDWWHUH VX RXWSXW YDULDELOH! JHWFKDU

SXWFKDU YDULDELOH! GRYH YDULDELOH! HC GL WLSR LQWHUR R FDUDWWHUH 

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

402

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

403

SXWFKDUJHWFKDU
JHWFKDU JHWFKDU LQW JHWFKDU YRLG  ‡ QRQ ULFKLHGH DUJRPHQWL H UHVWLWXLVFH FRPH ULVXOWDWR LO FDUDWWHUH OHWWR FRQYHUWLWR LQ LQW R (2) LQ FDVR GL HQGRIILOH R HUURUH 

(VHPSLLR (VHPS R 3URJUDPPD FKH FRSLD GD LQSXW OD WDVWLHUD VX RXWSXW LO YLGHR  #include <stdio.h> main() { char c; c=getchar(); while (c != EOF) { putchar(c); c=getchar(); } } + /D IXQ]LRQH JHWFKDU FRPLQFLD D UHVWLWXLUH FDUDWWHUL VROR TXDQGR q VWDWR EDWWXWR LO WDVWR GL LQYLR LQSXW EXIIHUL]]DWR 9HUVLLRQH SLLXC VLLQWHWLLFD 9HUV RQH S XC V QWHW FD #include <stdio.h> main() { char c; while ((c = getchar()) != EOF) putchar(c); }

SXWFKDU SXWFKDU LQW SXWFKDU FKDU F  ‡ ULFKLHGH XQ DUJRPHQWR LO FDUDWWHUH GD VFULYHUH  H UHVWLWXLVFH FRPH ULVXOWDWR LO FDUDWWHUH VFULWWR R (2) LQ FDVR GL HUURUH 

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

404

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

405

(VHPSLLR (VHPS R 5LFRSLDWXUD GHOO
LQSXW VXOO
RXWSXW FRQYHUWHQGR PLQXVFROH LQ PDLXVFROH #include <stdio.h> #define scostamento ’a’-’A’ main() { char c; while ((c = getchar()) != EOF) if (c>=’a’ && c<=’z’) putchar(c-(scostamento)); else putchar(c); } + SXWFKDU F VFRVWDPHQWR YLHQH HVSDQVR LQ + SXWFKDU F
D 

$

,QSXW2XWSXW D OLQHH GL FDUDWWHUL
/D VFDQI FRQ IRUPDWR V SUHYHGH FRPH VHSDUDWRUH DQFKH LO EODQN VSD]LR ELDQFR  + HC SRVVLELOH VROWDQWR OHJJHUH VWULQJKH FKH QRQ FRQWHQJRQR ELDQFKL $G HVHPSLLR VH OO

LLQSXW HC $G HVHPS R VH QSXW HC Nel mezzo del cammin di nostra vita, mi ritrovai in una selva oscura… … /HJJHQGR FRQ /HJJHQGR FRQ char s1[80], s2[80]; scanf("%s", &s1); /* s1 vale "Nel" */ scanf("%s", &s2); /*s2 vale "mezzo"*/ + OD VFDQI QRQ HC DGDWWD D OHJJHUH LQWHUH OLQHH FKH SRVVRQR FRQWHQHUH VSD]L ELDQFKL FDUDWWHUL GL WDEXOD]LRQH HWF 

3HU TXHVWR PRWLLYR LLQ & HVLLVWRQR IXQ]LLRQLL VSHFLLILLFKH SHU 3HU TXHVWR PRW YR Q & HV VWRQR IXQ] RQ VSHF I FKH SHU IDUH ,2 GLL OOLLQHH IDUH , 2 G QHH ‡ JHWV ‡ SXWV

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

406

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

407

,QSXW2XWSXW D OLQHH GL FDUDWWHUL
JHWV JHWV HC XQD IXQ]LRQH VWDQGDUG FKH OHJJH XQD LQWHUD ULJD GD LQSXW ILQR DO SULPR FDUDWWHUH GL ILQH OLQHD
?Q 
QHZOLQH H O
DVVHJQD DG XQD VWULQJD gets(char str[]); ‡ DVVHJQD DOOD VWULQJD VWU L FDUDWWHUL OHWWL ‡ ,O FDUDWWHUH
?Q
YLHQH VRVWLWXLWR QHOOD VWULQJD GL GHVWLQD]LRQH VWU GD
? 
$G HVHPSLLR GDWR OO

LLQSXW $G HVHPS R GDWR QSXW Nel mezzo del cammin di nostra vita, mi ritrovai in una selva oscura… … char S[80]; gets(S); Õ 6 YDOH
"Nel mezzo del cammin di nostra vita,"

,QSXW2XWSXW D OLQHH GL FDUDWWHUL
SXWV SXWV (C XQD IXQ]LRQH VWDQGDUG FKH VFULYH XQD VWULQJD VXOO
RXWSXW DJJLXQJHQGR XQ FDUDWWHUH GL ILQH OLQHD
?Q 
QHZOLQH  puts(char str[]); ‡LQ FDVR GL HUURUH UHVWLWXLVFH (2) $G HVHPSLLR $G HVHPS R .. char S1[80]="Dante Alighieri"; char S2[80]="La Divina Commedia"; puts(S1); puts(S2); .. KDQQR FRPH HIIHWWR VXOOR VWDQGDUG RXWSXW Dante Alighieri La Divina Commedia ! SXWV 6 HC HTXLYDOHQWH D SULQWI V?Q  6 

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

408

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

409

(VHPSLL (VHPS  ‡ 5LFRSLD O
LQSXW QHOO
RXWSXW D OLQHH  #include <stdio.h> main() { char s[81]; while (gets(s)) puts(s); } ‡ 8VR GL SXWV H SXWFKDU putchar(’A’); putchar(’B’); puts("C"); putchar(’D’); (IIHWWR ABC D (VHPSLLR (VHPS R

,O 5HFRUG
6L YXROH UHDOL]]DUH O
DVWUD]LRQH FDUDWWHUL]]DWD GDL VHJXHQWL DWWULEXWL œ 1RPH œ &RJQRPH œ 5HGGLWR œ $OLTXRWD FRQWULEXHQWH 

&RQ JOL VWUXPHQWL YLVWL ILQRUD SHU RJQL FRQWULEXHQWH HC QHFHVVDULR LQWURGXUUH  YDULDELOL FKDU1RPH>@ &RJQRPH>@ LQW 5HGGLWR $OLTXRWD VROX]LRQH VFRPRGD H QRQ DVWUDWWD  OH TXDWWUR YDULDELOL VRQR LQGLSHQGHQWL WUD GL ORUR œ (C QHFHVVDULR XQ FRVWUXWWR FKH FRQVHQWD O
DJJUHJD]LRQH GHL  DWWULEXWL QHOO
DVWUD]LRQH FRQWULEXHQWH  LO UHFRUG

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

410

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

411

7LSL VWUXWWXUDWL LO 5HFRUG
8Q UHFRUG HC XQ LQVLHPH ILQLWR GL HOHPHQWL QRQ RPRJHQHR ‡ LO QXPHUR GHJOL HOHPHQWL HC ULJLGDPHQWH ILVVDWR D SULRUL ‡ JOL HOHPHQWL SRVVRQR HVVHUH GL WLSR GLYHUVR ‡ LO WLSR GL FLDVFXQ HOHPHQWR FRPSRQHQWH FDPSR HC SUHILVVDWR $G HVHPSLR

,O 5HFRUG LQ & VWUXFW
&ROOH]LRQL FRQ XQ QXPHUR ILQLWR GL FDPSL DQFKH GLVRPRJHQHL VRQR UHDOL]]DELOL LQ & PHGLDQWH LO FRVWUXWWRUH GL WLSR VWUXWWXUDWR VWUXFW 'HILLQLL]LLRQH GLL YDULLDELLOOH GLL WLLSR UHFRUG 'HI Q ] RQH G YDU DE H G W SR UHFRUG VWUXFW ^ OLVWD GHILQL]LRQL FDPSL! ` LGYDULDELOH! GRYH ‡ OLVWD GHILQL]LRQL FDPSL! HC O¶LQVLHPH GHOOH GHILQL]LRQL GHL FDPSL FRPSRQHQWL FRVWUXLWD XVDQGR VWHVVH UHJROH VLQWDWWLFKH GHOOD GHILQL]LRQH GL YDULDELOL WLSR! FDPSR! WLSR! FDPSR!  WLSR1! FDPSR1! ‡ LGYDULDELOH! HC O¶LGHQWLILFDWRUH GHOOD YDULDELOH GL WLSR UHFRUG FRVLC GHILQLWD (VHPSLLR (VHPS R { char Nome[20]; char Cognome[20]; int Reddito; int Aliquota; }contribuente; 1RPH &RJQRPH 5HGGLWR HG $OLTXRWD VRQR L FDPSL GHOOD YDULDELOH FRQWULEXHQWH GL WLSR UHFRUG R VWUXFW  struct

20( 1

&2*120(

5('',72

$/,4827$

)RUPDOOPHQWH )RUPD PHQWH ,O UHFRUG HC XQ WLSR VWUXWWXUDWR LO FXL GRPLQLR VL RWWLHQH PHGLDQWH SURGRWWR FDUWHVLDQR GDWL Q LQVLHPL $F $F  $FQ LO SURGRWWR FDUWHVLDQR WUD HVVL $F ™ $F ™  ™ $FQ FRQVHQWH GL GHILQLUH XQ WLSR GL GDWR VWUXWWXUDWR LO UHFRUG L FXL HOHPHQWL VRQR QSOH RUGLQDWH GRYH DFL³$FL DF DF  DFQ

$G HVHPSLR ,O QXPHUR FRPSOHVVR HC GHILQLWR DWWUDYHUVR LO SURGRWWR FDUWHVLDQR 5 ™ 5

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

412

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

413

,O WLSR VWUXFW
2SHUDWRULL 2SHUDWRU  *OL XQLFL RSHUDWRUL SUHYLVWL SHU GDWL GL WLSR VWUXFW VRQR ‡ O
RSHUDWRUH GL DVVHJQDPHQWR  HC SRVVLELOH O¶DVVHJQDPHQWR GLUHWWR WUD UHFRUG GL WLSR HTXLYDOHQWH ‡ RSHUDWRUL GL XJXDJOLDQ]D H GLVXJXDJOLDQ]D UHOD]LRQDOH 

$G HVHPSLLR $G HVHPS R struct { char Nome[20]; char Cognome[20]; int Reddito; int Aliquota; }contribuente;

contribuente.Reddito=2000+1500; strcpy(contribuente.Nome,"Mario"); strcpy(contribuente.Cognome,"Rossi"); contribuente.Aliquota=40;

$FFHVVR DLL FDPSLL $FFHVVR D FDPS  (C SRVVLELOH DFFHGHUH H PDQLSRODUH L VLQJROL FDPSL GL XQ UHFRUG  3HU DFFHGHUH DL FDPSL GL XQ UHFRUG LQ & VL XVD OD QRWD]LRQH SRVWILVVD  LGYDULDELOH!FRPSRQHQWH! LQGLFD LO YDORUH GHO FDPSR FRPSRQHQWH! GHOOD YDULDELOH LGYDULDELOH!  ‡ , VLQJROL FDPSL SRVVRQR HVVHUH PDQLSRODWL FRQ JOL RSHUDWRUL SUHYLVWL SHU LO WLSR DG HVVL DVVRFLDWR

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

414

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

415

,QL]LDOL]]D]LRQH GL UHFRUG
(C SRVVLELOH LQL]LDOL]]DUH L UHFRUG LQ IDVH GL GHILQL]LRQH $G HVHPSLLR $G HVHPS R struct { char Nome[20]; char Cognome[20]; int Reddito; int Aliquota; }p=("Mario","Rossi",17000,10);

,O FRVWUXWWRUH GL WLSR VWUXFW
,O FRVWUXWWRUH VWUXFW SXRC HVVHUH XWLOL]]DWR SHU GLFKLDUDUH WLSL QRQ SULPLWLYL EDVDWL VXO UHFRUG 'LLFKLLDUD]LLRQH GLL WLLSR VWUXWWXUDWR UHFRUG ' FK DUD] RQH G W SR VWUXWWXUDWR UHFRUG W\SHGHI VWUXFW^OLVWD GLFKLDUD]LRQL FDPSL!` LGWLSR! GRYH ‡ OLVWD GHILQL]LRQL FDPSL! HC O¶LQVLHPH GHOOH GHILQL]LRQL GHL FDPSL FRPSRQHQWL œ LGBWLSR! HC O
LGHQWLILFDWRUH GHO QXRYR WLSR $G HVHPSLR typedef struct { int anno; int mese; int giorno; }tipodata;

tipodata data; unsigned int anno=1999; data.anno=anno; data.mese=1; data.giorno=6; + *OL LGHQWLILFDWRUL GL FDPSR GL XQ UHFRUG GHYRQR HVVHUH GLVWLQWL WUD ORUR PD QRQ QHFHVVDULDPHQWH GLYHUVL GD DOWUL LGHQWLILFDWRUL DG HV DQQR 

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

416

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

417

, 5HFRUG LQ &
5LLDVVXPHQGR 5 DVVXPHQGR 6LQWDVVL
>W\SHGHI@ VWUXFW ^

9HWWRUL H UHFRUG
1RQ FL VRQR YLQFROL ULJXDUGR DO WLSR GHJOL HOHPHQWL GL XQ YHWWRUH VL SRVVRQR UHDOL]]DUH DQFKH YHWWRUL GL UHFRUG WDEHOOH  $G HVHPSLLR $G HVHPS R typedef struct { char Nome[20]; char Cognome[20]; int Reddito; int Aliquota; } Contribuente; contribuente archivio[1000]; + archivio HC XQ YHWWRUH GL  HOHPHQWL FLDVFXQR GHL TXDOL HC GL WLSR contribuente ø YHWWRUH GL UHFRUG VWUXWWXUD WDEHOODUH  1RPH    «  &RJQRPH 5HGGLWR $OLTXRWD 

WLSRB! QRPHBFDPSRB! WLSRB! QRPHBFDPSRB!  WLSRB1! QRPHBFDPSRB1! ` QRPH!

9LLQFROOLL 9 QFR  ‡ ‡ ‡ QRPHBFDPSRBL! H
XQ LGHQWLILFDWRUH VWDELOLWR FKH LQGLYLGXD LO FDPSR LHVLPR WLSRBL! H
XQ TXDOVLDVL WLSR VHPSOLFH R VWUXWWXUDWR QRPH! HC O¶LGHQWLILFDWRUH GHOOD VWUXWWXUD R GHO WLSR VH VL XVD W\SHGHI OD VWUXWWXUD H
XQD FROOH]LRQH GL XQ QXPHUR ILVVDWR GL HOHPHQWL GL YDULR WLSR WLSRBFDPSRBL!  LO VLQJROR FDPSR QRPHBFDPSRBL! GL XQ UHFRUG 5 H
LQGLYLGXDWR PHGLDQWH OD QRWD]LRQH 5QRPHBFDPSRBL! VH GXH VWUXWWXUH GL GDWL GL WLSR VWUXFW KDQQR OR VWHVVR WLSR DOORUD HC SRVVLELOH O
DVVHJQDPHQWR GLUHWWR

8VR 8VR ‡ ‡ ‡

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

418

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

419

9HWWRUL H 5HFRUG
$OOR VWHVVR PRGR VL SRVVRQR IDUH UHFRUG GL UHFRUG H UHFRUG GL YHWWRUL DG HVHPSLR typedef struct{ int giorno; int mese; int anno; }data typedef struct{ char nome[20]; char cognome[40]; data data_nasc; } persona; persona P; … P.data_nasc.giorno=25; P.data_nasc.mese=3; P.data_nasc.anno=1992; … 6WUXWWXUDOH

(TXLYDOHQ]D GL WLSR
9DULDELOL FRQ OD VWHVVD VWUXWWXUD LQWHUQD VRQR FRQVLGHUDWH HTXLYDOHQWL DQFKH VH QRQ KDQQR OR VWHVVR LGHQWLILFDWRUH GL WLSR  6RQR HTXLYDOHQWL VROR YDULDELOL FKH IDQQR ULIHULPHQWR DOOR VWHVVR LGHQWLILFDWRUH GL WLSR

1RPLQDOH

+ ,Q & QRQ VL VSHFLILFD TXDOH WLSR GL HTXLYDOHQ]D YHQJD DGRWWDWR HVLVWRQR UHDOL]]D]LRQL GHO OLQJXDJJLR FKH DGRWWDQR HTXLYDOHQ]D VWUXWWXUDOH DOWUH HTXLYDOHQ]D QRPLQDOH º 3HU JDUDQWLUH OD SRUWDELOLWDC XVDUH VHPSUH HTXLYDOHQ]D QRPLQDOH

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

420

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

421

(TXLLYDOOHQ]D VWUXWWXUDOOH (TX YD HQ]D VWUXWWXUD H typedef struct {float x; float y; }coordinate; coordinate A, C; struct {float x; float y; }B; $ & H % KDQQR OOR VWHVVR WLLSR $ & H % KDQQR R VWHVVR W SR $ % $ & OHFLWD OHFLWD

(TXLLYDOOHQ]D QRPLLQDOOH (TX YD HQ]D QRP QD H typedef struct {float x; float y; }coordinate; coordinate A, B; struct {float x; float y; }C; $ % KDQQR OR VWHVVR WLSR & YLHQH FRQVLGHUDWR GL WLSR GLYHUVR $ % OHFLWD $ & QRQ OHFLWD

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

422

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

423

(VHUFLL]LLR (VHUF ] R 5HDOL]]DUH XQ SURJUDPPD FKH OHWWH GD LQSXW OH FRRUGLQDWH GL XQ SXQWR 3 GHO SLDQR VLD LQ JUDGR GL DSSOLFDUH D 3 DOFXQH WUDVIRUPD]LRQL JHRPHWULFKH WUDVOD]LRQH H SURLH]LRQL VXL GXH DVVL  #include <stdio.h> main() { typedef struct{float x,y;}punto; punto P; unsigned int op; float Dx, Dy; /* si leggono le coordinate da input i dati e le si memorizza in P */ printf("ascissa? "); scanf("%f",&P.x); printf("ordinata? "); scanf("%f",&P.y); /* lettura dell’operazione richiesta: 0: termina 1: proietta sull’asse x 2: proietta sull’asse y 3: trasla di Dx, Dy */ printf("%s\n","operazione(0,1,2,3)?"); scanf("%d",&op);

switch (op) {case 1: P.y= 0;break; case 2: P.x= 0; break; case 3:printf("%s","Traslazione?”); scanf("%f%f",&Dx,&Dy); P.x=P.x+Dx; P.y=P.y+Dy; break; default: printf("errore!"); } printf("%s\n","nuove coordinate "); printf("%f\t%f\n",P.x,P.y); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

424

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

425

0 (VHUFLL]LLR 0 (VHUF ] R 6FULYHUH XQ SURJUDPPD FKH DFTXLVLVFD L GDWL UHODWLYL DJOL VWXGHQWL GL XQD FODVVH ‡ QRPH ‡ HWD ‡ YRWL UDSSUHVHQWD L YRWL GHOOR VWXGHQWH LQ  PDWHULH LWDOLDQR PDWHPDWLFD LQJOHVH  LO SURJUDPPD GHYH VXFFHVVLYDPHQWH FDOFRODUH H VWDPSDUH SHU RJQL VWXGHQWH OD PHGLD GHL YRWL RWWHQXWL QHOOH  PDWHULH #include <stdio.h> typedef enum {ita, mat, ing}materie; typedef struct { char nome[30]; int eta; int voto[3]; } studente; main() { studente classe[20]; float m; int i; materie j; /* lettura dati */ for(i=0;i<20; i++) { scanf("%s%d", &classe[i].nome, classe[i].eta); for(j=ita; j<=ing; j++) scanf("%d", &classe[i].voto[j]); }

/* stampa delle medie */ for(i=0;i<20; i++) { for(m=0, j=ita; j<=ing; j++) m+=classe[i].voto[j]); printf("media di %s: %d\n", classe[i].nome, m); } }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

426

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

427

8QLRQH GLVFULPLQDWD
/¶XQLRQH GLVFULPLQDWD R UHFRUG YDULDQWH HC XQ WLSR GL GDWR SHU LO TXDOH VRQR SRVVLELOL GHILQL]LRQL GLIIHUHQWL HG LQ DOWHUQDWLYD WUD ORUR RJQL DOWHUQDWLYD YLHQH GHWWD YDULDQWH $G HVHPSLLR $G HVHPS R XQD ILJXUD JHRPHWULFD SLDQD SXz HVVHUH UDSSUHVHQWDWD LQ PRGR GLYHUVR D VHFRQGD GHO WLSR 3HU HVHPSLR Œ XQ WULDQJROR q UDSSUHVHQWDWR GDOOD WHUQD GHL VXRL ODWL Œ XQ TXDGUDWR q UDSSUHVHQWDWR GDO ODWR Œ XQ UHWWDQJROR q UDSSUHVHQWDWR GDOOD FRSSLD GHL VXRL ODWL Œ XQD FLUFRQIHUHQ]D q UDSSUHVHQWDWD GDO VXR UDJJLR 0HGLDQWH O¶XQLRQH GLVFULPLQDWD VL SXz UHDOL]]DUH XQ WLSR GL GDWR GRWDWR GL TXDWWUR UDSSUHVHQWD]LRQL DOWHUQDWLYH XQD SHU LO WULDQJROR XQD SHU LO TXDGUDWR XQD SHU LO UHWWDQJROR HG XQD SHU OD FLUFRQIHUHQ]D FKH LQ JHQHUDOH UDSSUHVHQWD XQD ILJXUD JHRPHWULFD + ,Q & O¶XQLRQH GLVFULPLQDWD GL WLSL VWUXWWXUDWL YLHQH UHDOL]]DWD DWWUDYHUVR LO FRVWUXWWRUH GL WLSR XQLRQ

8QLRQH GLVFULPLQDWD LQ & XQLRQ
,Q & q GLVSRQLELOH LO FRVWUXWWRUH XQLRQ SHU LQWURGXUUH GDWL H WLSL EDVDWL VXOO¶XQLRQH GLVFULPLQDWD 'HILLQLL]LLRQH GLL YDULLDELLOOLL XQLLRQ 'HI Q ] RQH G YDU DE XQ RQ XQLRQ ^ GHILQL]LRQH ! GHILQL]LRQH !  GHILQL]LRQH 1! `QRPH! GRYH ‡ QRPH! q O¶LGHQWLILFDWRUH GHOOD QXRYD YDULDELOH ‡ GHILQL]LRQH ! GHILQL]LRQH !  GHILQL]LRQH 1! VRQR OH GHILQL]LRQL DOWHUQDWLYH YDULDQWL SHU OD YDULDELOH QRPH!

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

428

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

429

$G HVHPSLLR $G HVHPS R union{ float raggio_circ; float lati_rettangolo[2]; float lati_triangolo[3]; float lato_quadrato; }oggetto;

8QLRQ
2SHUDWRULL 2SHUDWRU *OL XQLFL RSHUDWRUL SUHYLVWL SHU OH XQLRQ O¶DVVHJQDPHQWR H JOL RSHUDWRUL XJXDJOLDQ]DGLVXJXDJOLDQ]D UHOD]LRQDOH $FFHVVR D UHFRUG YDULLDQWLL $FFHVVR D UHFRUG YDU DQW  SHU VFHJOLHUH XQD WUD OH UDSSUHVHQWD]LRQL DOWHUQDWLYH VL XVD OD QRWD]LRQH SRVWILVVD QRPH!GHILQL]LRQHBL! ‡ LQ EDVH DOOD YDULDQWH VFHOWD WUD OH 1 SRVVLELOL DOWHUQDWLYH  VL SXRC RSHUDUH VXO GDWR FRQ JOL VWUXPHQWL SUHYLVWL GDO WLSR DVVRFLDWR DG HVVR $G HVHPSLLR $G HVHPS R union{ float raggio_circ; float lati_rettangolo[2]; float lati_triangolo[3]; float lato_quadrato; }oggetto; float AREA; /*se oggetto rappresenta un rettangolo:*/ scanf("%f", &oggetto.lati_rettangolo[0]); scanf("%f", &oggetto.lati_rettangolo[1]); AREA= oggetto.lati_rettangolo[0]* oggetto.lati_rettangolo[1]; ... VRQR GL

Œ /D YDULDELOH RJJHWWR KD TXDWWUR UDSSUHVHQWD]LRQL DOWHUQDWLYH D VHFRQGD GL TXDOH ILJXUD JHRPHWULFD VL YXROH PHPRUL]]DUH LQ RJJHWWR YHUUj VFHOWD XQD SDUWLFRODUH YDULDQWH Œ OH YDULDQWL SUHYLVWH VL HVFOXGRQR PXWXDPHQWH PD WXWWL L FRPSRQHQWL FRQGLYLGRQR OD VWHVVD PHPRULD O¶HVWHQVLRQH GHOO¶DUHD DOORFDWD SHU XQ GDWR GL WLSR XQLRQ HC SDUL DOO¶DUHD ULFKLHVWD GDOOD VWUXWWXUD FKH RFFXSD SLXC PHPRULD QHOO¶HVHPSLR OR VSD]LR QHFHVVDULR SHU  IORDW 

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

430

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

431

,O FRVWUXWWRUH GL WLSR XQLRQ
0HGLDQWH LO FRVWUXWWRUH XQLRQ VL SRVVRQR GLFKLDUDUH WLSL QRQ SULPLWLYL 'LLFKLLDUD]LLRQH GLL WLLSLL QRQ SULLPLLWLLYLL ' FK DUD] RQH G W S QRQ SU P W Y  W\SHGHI XQLRQ ^ GHILQL]LRQH ! GHILQL]LRQH !  GHILQL]LRQH 1! `QRPHWLSR! GRYH ‡ QRPHWLSR! q O¶LGHQWLILFDWRUH GHO QXRYR WLSR ‡ GHILQL]LRQH ! GHILQL]LRQH !  GHILQL]LRQH 1! VRQR OH GHILQL]LRQL DOWHUQDWLYH YDULDQWL SHU OD YDULDELOH QRPH! $G HVHPSLLR $G HVHPS R typedef union{ float raggio_circ; float lati_rettangolo[2]; float lati_triangolo[3]; float lato_quadrato; }figura; figura O1, O2; ... O1.raggio_circ=12.5; O2.lato_quadrato=5.25; ...

5HFRUG H 8QLRQ
1RQ FL VRQR YLQFROL ULJXDUGR DO WLSR GHL FDPSL GL XQ UHFRUG q SRVVLELOH UHDOL]]DUH XQR R SL FDPSL GL XQ UHFRUG PHGLDQWH XQLRQ + ,Q TXHVWR PRGR VL SRVVRQR UHDOL]]DUH VWUXWWXUH FKH KDQQR œ XQD SDUWH ILVVD œ XQD SDUWH YDULDQWH (VHPSLLR (VHPS R 6L YXROH UHDOL]]DUH XQ WLSR GL GDWR SHU UDSSUHVHQWDUH L OLEUL JHVWLWL GD XQD ELEOLRWHFD ,Q SDUWLFRODUH SHU RJQL OLEUR VL YRJOLRQR PHPRUL]]DUH OH VHJXHQWL LQIRUPD]LRQL œ &RGLFH œ 7LWROR œ $XWRUH œ (GLWRUH œ $QQR ,QROWUH SHU RJQL OLEUR œ SXz HVVHUH QHFHVVDULR UHJLVWUDUH OD VXD FROORFD]LRQH DOO
LQWHUQR GHOOD ELEOLRWHFD VH q GLVSRQLELOH  œ RSSXUH SXz HVVHUH QHFHVVDULR PHPRUL]]DUH L GDWL GHO FOLHQWH DO TXDOH q LPSUHVWDWR VH q LQ SUHVWLWR 

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

432

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

433

+ 1RWLDPR FKH LO OLEUR q FDUDWWHUL]]DWR GD XQD SDUWH ILVVD FRGLFH DXWRUH WLWROR HWF H GD XQD SDUWH YDULDQWH FOLHQWH R FROORFD]LRQH  typedef struct { char Cognome[30]; char Indirizzo[40]; char N_tel[15]; int giorno; int mese; int anno; }cliente; typedef struct { int stanza; int scaffale; }collocazione;

8QLRQ
(VHPSLLR (VHPS R ... libro L; ... /* L è disponibile: */ L.V.Col.stanza=3; L.V.Col.scaffale=7; printf("%s", L.V.Cli.Cognome); /* ?? */ ... + 1RQ F
q DOFXQ FRQWUROOR VXOOD UDSSUHVHQWD]LRQH DGRWWDWD DQFKH VH / UDSSUHVHQWD XQ OLEUR GLVSRQLELOH VL SXRC DFFHGHUH DG /9&OL&RJQRPH 3UREOOHPD 3URE HPD &RPH ULFRQRVFHUH LO WLSR GL UDSSUHVHQWD]LRQH YDOLGD DG XQ FHUWR LVWDQWH SHU XQ GDWR GL WLSR XQLRQ" + 3HU IDYRULLUH XQD JHVWWLLRQH FRUUHWWWWD GHOOOOH XQLLRQ SXRC HVVHUH + 3HU IDYRU UH XQD JHV RQH FRUUH D GH H XQ RQ SXRC HVVHUH XWWLLOOH LLQWWURGXUUH XQ FDPSR DJJLLXQWWLLYR WDJ LLQ EDVH DOO FXLL X H Q URGXUUH XQ FDPSR DJJ XQ YR WDJ Q EDVH D FX YDOORUH VLL SXRC GHGXUUH LLOO WWLLSR GLL UDSSUHVHQWWD]LLRQH DGRWWWWDWWD SR G UDSSUHVHQ D] RQH DGR D D YD RUH V SXRC GHGXUUH

typedef struct { char Codice[6]; char Titolo[30]; char Autore[30]; char Editore[20]; int Anno; union { cliente Cli; collocazione Col; }V; /* p. variante */ } libro; libro L1, L2; /* L1 è disponibile: */ L1.V.Col.stanza=3; L1.V.Col.scaffale=7; /* L2 è in prestito: */ scanf("%s", &L2.V.Cli.Cognome); gets(L2.V.Cli.Indirizzo); ...

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

434

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

435

(VHPSLLR (VHPS R typedef struct { char Codice[6]; char Titolo[30]; char Autore[30]; char Editore[20]; int Anno; int disponibile; /* tag */ union { cliente Cli; collocazione Col; }V; /* p. variante */ } libro; libro L; ... if (L.disponibile) /* L è disponibile: */ { L.V.Col.stanza=3; L.V.Col.scaffale=7; } else /* L è in prestito: */ { printf("%s", L.V.Cli.Cognome); printf("%s", L.V.Cli.Indirizzo); } ... + ,Q TXHVWR PRGR VL PDQWLHQHHVSOLFLWDPHQWH OD FRQVLVWHQ]D GHOOD VWUXWWXUD GDWL

,O SXQWDWRUH
(C XQ WLSR VFDODUH FKH FRQVHQWH GL UDSSUHVHQWDUH JOL LQGLUL]]L GHOOH YDULDELOL DOORFDWH LQ PHPRULD ,O GRPLQLR GL XQD YDULDELOH GL WLSR SXQWDWRUH q XQ LQVLHPH GL LQGLUL]]L + ,O YDORUH GL XQD YDULDELOH GL WLSR SXQWDWRUH SXRC HVVHUH O¶LQGLUL]]R GL XQ¶DOWUD YDULDELOH YDULDELOH SXQWDWD  ,Q & L SXQWDWRUL VL GHILQLVFRQR PHGLDQWH LO FRVWUXWWRUH  'HILLQLL]LLRQH GLL XQD YDULLDELLOOH SXQWDWRUH 'HI Q ] RQH G XQD YDU DE H SXQWDWRUH 7LSR(OHPHQWR3XQWDWR! 1RPH3XQWDWRUH! GRYH œ 7LSR(OHPHQWR3XQWDWR! HC LO WLSR GHOOD YDULDELOH SXQWDWD œ 1RPH3XQWDWRUH! HC LO QRPH GHOOD YDULDELOH GL WLSR SXQWDWRUH œ LO VLPEROR HC LO FRVWUXWWRUH GHO WLSR SXQWDWRUH $G HVHPSLLR $G HVHPS R int P; /*P è un puntatore a intero */

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

436

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

437

,O SXQWDWRUH
2SHUDWRULL 2SHUDWRU  œ $VVHJQDPHQWR HC SRVVLELOH O
DVVHJQDPHQWR WUD SXQWDWRUL GHOOR VWHVVR WLSR  (
GLVSRQLELOH OD FRVWDQWH 18// SHU LQGLFDUH O
LQGLUL]]R QXOOR œ RSHUDWRUH GL GHUHIHUHQFLQJ  q XQ RSHUDWRUH XQDULR 6L DSSOLFD D XQ SXQWDWRUH H UHVWLWXLVFH LO YDORUH FRQWHQXWR QHOOD FHOOD SXQWDWD ! VHUYH SHU DFFHGHUH DOOD YDULDELOH SXQWDWD œ 2SHUDWRUH LQGLUL]]R VL DSSOLFD DG XQD YDULDELOH H UHVWLWXLVFH O
LQGLUL]]R GHOOD FHOOD GL PHPRULD QHOOD TXDOH HC DOORFDWD OD YDULDELOH œ RSHUDWRUL DULWPHWLFL YHGL YHWWRUL SXQWDWRUL  œ 2SHUDWRUL UHOD]LRQDOL!  $G HVHPSLLR $G HVHPS R int *punt1, *punt2; int A; punt1=&A; *punt1=127; punt2=punt1; punt1=NULL;
SXQW SXQW

A 

SXQW SXQW

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

438

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

439

2SHUDWRUH ,QGLLULL]]R  2SHUDWRUH ,QG U ]]R  + + + VL DSSOLFD VROR DG RJJHWWL FKH HVLVWRQR LQ PHPRULD TXLQGL JLDC GHILQLWL  QRQ HC DSSOOLLFDELLOOH DG HVSUHVVLLRQLL QRQ HC DSS FDE H DG HVSUHVV RQ

3XQWDWRUH FRPH FRVWUXWWRUH GL WLSR
'LLFKLLDUD]LLRQH GLL XQ WLLSR SXQWDWRUH ' FK DUD] RQH G XQ W SR SXQWDWRUH W\SHGHI 7LSR(OHPHQWR3XQWDWR! 1RPH7LSR! ‡ 7LSR(OHPHQWR3XQWDWR! HC LO WLSR GHOOD YDULDELOH SXQWDWD ‡ 1RPH3XQWDWRUH! HC LO QRPH GHO WLSR GLFKLDUDWR $G HVHPSLLR $G HVHPS R typedef float *tpf; tpf p; float f; p=&f; …

2SHUDWRUH 'HUHIHUHQFLLQJ  2SHUDWRUH 'HUHIHUHQF QJ  + FRQVHQWWH GLL DFFHGHUH DG XQD YDULLDELLOOH VSHFLLILLFDQGR LLOO VXR + FRQVHQ H G DFFHGHUH DG XQD YDU DE H VSHF I FDQGR VXR LLQGLLULL]]R QG U ]]R + OO

LLQGLLULL]]R UDSSUHVHQWWD XQ PRGR DOOWWHUQDWWLLYR + QG U ]]R UDSSUHVHQ D XQ PRGR D HUQD YR QRPH SHU DFFHGHUH H PDQLLSROODUH OOD YDULLDELLOOH QRPH SHU DFFHGHUH H PDQ SR DUH D YDU DE H float *p; float R, A; p=&A; /* *p è un alias di A*/ R=2; *p=3.14*R; /* A è modificato */ $  DOOLLDV DOO D DV D

S

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

440

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

441

3XQWDWRUL
1HOOD GHILQL]LRQH GL XQ SXQWDWRUH HC QHFHVVDULR LQGLFDUH LO WLSR GHOOD YDULDELOH SXQWDWD Í LO FRPSLODWRUH SXRC HIIHWWXDUH FRQWUROOL VWDWLFL VXOO
XVR GHL SXQWDWRUL (VHPSLLR (VHPS R W\SHGHI VWUXFW^«`UHFRUG W\SHGHI VWUXFW^«`UHFRUG int *p, A; record *q, X; p=&A; q=p; /*warning!*/ q=&X; *p=*q; /* errore! */ + 9LHQH VHJQDODWR GDO FRPSLODWRUH ZDUQLQJ LO WHQWDWLYR GL XWLOL]]R FRQJLXQWR GL SXQWDWRUL D WLSL GLIIHUHQWL

9DULDELOL 'LQDPLFKH
,Q & VL SRVVRQR GHILQLUH HC SRVVLELOH FODVVLILFDUH OH YDULDELOL LQ EDVH DO ORUR WHPSR GL YLWD HC SRVVLELOH LQGLYLGXDUH GXH FDWHJRULH ‡ YDULDELOL DXWRPDWLFKH ‡ YDULDELOL GLQDPLFKH 9DULLDELLOOLL DXWRPDWLLFKH 9DU DE DXWRPDW FKH œ /
DOORFD]LRQH H OD GHDOORFD]LRQH GL YDULDELOL DXWRPDWLFKH HC HIIHWWXDWD DXWRPDWLFDPHQWH GDO VLVWHPD VHQ]D O
LQWHUYHQWR GHO SURJUDPPDWRUH  œ 2JQL YDULDELOH DXWRPDWLFD KD XQ QRPH DWWUDYHUVR LO TXDOH OD VL SXRC ULIHULUH œ ,O SURJUDPPDWRUH QRQ KD OD SRVVLELOLWDC GL LQIOXLUH VXO WHPSR GL YLWD GL YDULDELOL DXWRPDWLFKH

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

442

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

443

9DULDELOL 'LQDPLFKH
9DULLDELLOOLL GLLQDPLLFKH 9DU DE G QDP FKH œ /H YDULDELOL GLQDPLFKH GHYRQR HVVHUH DOORFDWH H GHDOORFDWH HVSOLFLWDPHQWH GDO SURJUDPPDWRUH œ /
DUHD GL PHPRULD LQ FXL YHQJRQR DOORFDWH OH YDULDELOL GLQDPLFKH VL FKLDPD KHDS œ /H YDULDELOL GLQDPLFKH QRQ KDQQR XQ LGHQWLILFDWRUH DVVRFLDWR DG HVVH PD SRVVRQR HVVHUH ULIHULWH VROWDQWR DWWUDYHUVR LO ORUR LQGLUL]]R PHGLDQWH L SXQWDWRUL  œ ,O WHPSR GL YLWD GHOOH YDULDELOL GLQDPLFKH HC O
LQWHUYDOOR GL WHPSR FKH LQWHUFRUUH O
DOORFD]LRQH H OD GHDOORFD]LRQH FKH VRQR VWDELOLWH GDO SURJUDPPDWRUH  + WXWWH OH YDULDELOL YLVWH ILQRUD ULHQWUDQR QHOOD FDWHJRULD GHOOH YDULDELOL DXWRPDWLFKH

9DULDELOL 'LQDPLFKH
+ ,O & SUHYHGH IXQ]LRQL VWDQGDUG GL DOORFD]LRQH GHDOORFD]LRQH SHU YDULDELOL GLQDPLFKH ‡ PDOORF ‡ IUHH 1RQ VRQR GHILQLWH D OLYHOOR GL OLQJXDJJLR GL SURJUDPPD]LRQH PD D OLYHOOR GL VLVWHPD RSHUDWLYR PHGLDQWH OD OLEUHULD VWDQGDUG VWGOLEK!

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

444

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

445

9DULDELOL 'LQDPLFKH
$OOOORFD]LLRQH GLL YDULLDELLOOLL GLLQDPLLFKH $ RFD] RQH G YDU DE G QDP FKH /D PHPRULD GLQDPLFD YLHQH DOORFDWD FRQ OD IXQ]LRQH VWDQGDUG PDOORF SXQW WLSRGDWR PDOORF VL]HRI WLSRGDWR 

$G HVHPSLLR $G HVHPS R #include <stdlib.h> typedef int *tp; tp punt; ...
punt

punt=(tp )malloc(sizeof(int));
punt
*punt

‡ WLSRGDWR HC LO WLSR GHOOD YDULDELOH SXQWDWD ‡ SXQW HC XQD YDULDELOH GL WLSR WLSRGDWR ‡ VL]HRI HC XQD IXQ]LRQH VWDQGDUG FKH FDOFROD LO QXPHUR GL E\WHV FKH RFFXSD LO GDWR VSHFLILFDWR FRPH DUJRPHQWR ‡ HC QHFHVVDULR FRQYHUWLUH HVSOLFLWDPHQWH LO WLSR GHO YDORUH ULWRUQDWR FDVWLQJ  WLSRGDWR PDOORF 

*punt=12
punt *punt 12

6LLJQLLILLFDWR 6 JQ I FDWR + /D PDOORF SURYRFD OD FUHD]LRQH GL XQD YDULDELOH GLQDPLFD QHOO¶KHDS H UHVWLWXLVFH FRPH YDORUH O
LQGLUL]]R GHOOD YDULDELOH FUHDWD

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

446

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

447

9DULDELOL GLQDPLFKH
'HDOOOORFD]LLRQH 'HD RFD] RQH 6L ULODVFLD OD PHPRULD DOORFDWD GLQDPLFDPHQWH FRQ IUHH SXQW  GRYH SXQW HC O
LQGLUL]]R GHOOD YDULDELOH GD GHDOORFDUH
punt

(VHPSLLR (VHPS R #include <stdlib.h> main() { int *p; /*definizione del puntatore p ad intero;il contenuto di p non è ancora definito */ p = (int *) malloc(sizeof (int)); /*definizione del contenuto di p: indirizzo di una cella di memoria allocata dinamicamente*/ *p = 55; /* assegnamento di un valore alla cella *p referenziata da p */ free(p); /* deallocazione della cella referenziata da p; il contenuto di p non è più definito */ }

?

'RSR TXHVWD RSHUD]LRQH OD FHOOD GL PHPRULD RFFXSDWD GD SXQW YLHQH GHDOORFDWD SXQW QRQ HVLVWH SLXC (VHPSLLR (VHPS R main() { char A, *p; A=’Z’; p=(char *)malloc(sizeof(char)); *p=A; ... <uso di *p> ... free(p); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

448

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

449

(VHPSLLR (VHPS R main() { int *P, *Q, x, y; x=5; y=14; P=(int *)malloc(sizeof(int)); Q=(int *)malloc(sizeof(int));
Q *Q

*P = 25; *Q = 30;
4 4 30 3 3 25 \  [

P

*P 

*P = x; y = *Q;
y 14 x 5
\  [  3 4 30 3 5 4

P = &y;

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

450

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

451

4 30 3 5 \  [ 

3UREOHPL OHJDWL DOO
XVR GHL 3XQWDWRUL
5LLIHULLPHQWLL SHQGHQWLL GDQJOOLLQJ UHIHUHQFHV  5 IHU PHQW SHQGHQW GDQJ QJ UHIHUHQFHV  3RVVLELOLWj GL IDUH ULIHULPHQWR DG DUHH GL PHPRULD QRQ SL DOORFDWH $G HVHPSLLR $G HVHPS R int *P; P = (int *) malloc(sizeof(int)); ... free(P); *P = 100; /* Da non fare! */

... } + O
XOWLPR DVVHJQDPHQWR KD FRPH HIIHWWR FROODWHUDOH OD SHUGLWD GHOO
LQGLUL]]R GL XQD YDULDELOH GLQDPLFD TXHOOD SUHFHGHQWHPHQWH UHIHUHQ]LDWD GD 3 FKH ULPDQH DOORFDWD PD QRQ p SL XWLOL]]DELOH

P

* P 3 0

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

452

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

453

3UREOHPL OHJDWL DOO
XVR GHL 3XQWDWRUL
$UHH LLQXWLLOOLL]]DELLOOLL $UHH QXW ]]DE  3RVVLELOLWj GL SHUGHUH LO ULIHULPHQWR DG DUHH GL PHPRULD DOORFDWH DO SURJUDPPD QRQ SL ULXVDELOL  $G HVHPSLLR $G HVHPS R int *P,*Q; P = (int *) malloc ( sizeof (int)); Q = (int *) malloc ( sizeof (int)); *P = 30; *Q = 20; 3 4
Q *Q 20 P *P 20

3

4
Q *Q 20 P *P 20

/¶DUHD FKH HUD SXQWDWD GD 3 QRQ q SL UDJJLXQJLELOH PD ULPDQH DOORFDWD DO SURJUDPPD

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

454

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

455

3UREOHPL OHJDWL DOO
XVR GHL 3XQWDWRUL
$OOLLDVLLQJ $ DV QJ 3RVVLELOLWDC GL ULIHULUH OD VWHVVD YDULDELOH FRQ SXQWDWRUL GLYHUVL $G HVHPSLLR $G HVHPS R int *p, *q; p=(int *)malloc(sizeof(int)); *p=3; q=p; /*p e q puntano alla stessa variabile */ S T *q = 10;  /* anche *p e‘ cambiato! */

3XQWDWRUL D SXQWDWRUL KDQGOH
8Q SXQWDWRUH SXC SXQWDUH D YDULDELOL GL WLSR TXDOXQTXH VHPSOLFL R VWUXWWXUDWH  SXRC SXQWDUH DQFKH D XQ SXQWDWRUH >W\SHGHI@ 7LSR'DWR 7LSR3XQW $G HVHPSLLR $G HVHPS R int x, *P, *Q, **DoppioPunt; P = &x; DoppioPunt = &P; 'RSSLRSXQW 3 4 4

[ Q = *DoppioPunt; 4 4 3 'RSSLRSXQW

[

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

456

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

457

9HWWRUL
9HWWRULL 9HWWRU 

3XQWDWRUL

9HWWRUL

3XQWDWRUL

‡ LQ & L YHWWRUL YHQJRQR DOORFDWL LQ PHPRULD LQ SDUROH FRQVHFXWLYH FLRHC SDUROH ILVLFDPHQWH DGLDFHQWL  OD FXL GLPHQVLRQH GLSHQGH GDO WLSR GHJOL HOHPHQWL GHO YHWWRUH ‡ ,O QRPH GL XQD YDULDELOH GL WLSR YHWWRUH YLHQH FRQVLGHUDWR GDO & FRPH O
LQGLUL]]R GHO SULPR HOHPHQWR GHO YHWWRUH $G HVHPSLLR $G HVHPS R int V[10]; + 9 q XQD FRVWDQWH ‡ 9 HTXLYDOH D 9>@ ‡ FRPH WLSR q XQ SXQWDWRUH DG LQWHUR int *p, V[10]; p=V; /* p punta a V[0] */ V = p; /*NO! V è un puntatore costante*/

,O & FRQVHQWH GL HVHJXLUH RSHUD]LRQL GL VRPPD H VRWWUD]LRQH VXL SXQWDWRUL D YHWWRUL  2SHUDWRULL DULLWPHWLLFLL VX SXQWDWRULL D YHWWRULL 2SHUDWRU DU WPHW F VX SXQWDWRU D YHWWRU  6H 9 H : VRQR SXQWDWRUL DG HOHPHQWL GL YHWWRUL HG L q XQ LQWHUR œ 9L UHVWLWXLVFH O
LQGLUL]]R GHOO
HOHPHQWR VSRVWDWR GL L SRVL]LRQL LQ DYDQWL ULVSHWWR D TXHOOR LQGLFDWR GD L œ 9: UHVWLWXLVFH O
LQWHUR FKH UDSSUHVHQWD LO QXPHUR GL HOHPHQWL FRPSUHVL WUD 9 H : $G HVHPSLLR $G HVHPS R float V[100], *p, *q; int k; p=V+7; /* p punta a V[7] */ q=V+2; /* p punta a V[5] */ k=p-q; /* k vale 5 */ ...

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

458

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

459

9HWWRUL H 3XQWDWRUL
œ ,Q & RJQL ULIHULPHQWR DG XQ HOHPHQWR GL XQ YHWWRUH q HVSDQVR FRPH XQ SXQWDWRUH GHUHIHUHQ]LDWR 9>@ 9>@ 9>L@ 9>H[SU@ $G HVHPSLLR $G HVHPS R main () { char a[] = "0123456789";/*a e‘ un vettore di caratteri */ int i = 5; printf("%c%c%c%c\n",a[i],a[5],i[a],5[a]); } 6WDPSD 6WDPSD  + 3HU LO FRPSLODWRUH 9>L@ H L>9@ VRQR OR VWHVVR HOHPHQWR SHUFKp YLHQH VHPSUH HVHJXLWD OD FRQYHUVLRQH 9>L@ ! 9L VHQ]D HVHJXLUH DOFXQ FRQWUROOR QHC VX 9 QHC VX L HTXLYDOH D HTXLYDOH D HTXLYDOH D HTXLYDOH D 9 9   9L 9  H[SU

9HWWRUL

3XQWDWRUL

+ > @ KD SUHFHGHQ]D ULVSHWWR D 4XLLQGLL 4X QG  FKDU D>@ ! HTXLYDOH D FKDU D>@ 

D q XQ YHWWRUH GL SXQWDWRUL D FDUDWWHUH
a

’s’

’A’

’%’

+ 3HU XQ SXQWDWRUH DG XQ YHWWRUH GL FDUDWWHUL q QHFHVVDULR IRU]DUH OD SUHFHGHQ]D FRQ OH SDUHQWHVL FKDU D >@

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

460

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

461

3XQWDWRUL D VWUXWWXUH
(
SRVVLELOH XWLOL]]DUH L SXQWDWRUL SHU DFFHGHUH D YDULDELOL GL WLSR VWUXFW $G HVHPSLLR $G HVHPS R typedef struct{ int Campo_1,Campo_2; } TipoDato; TipoDato P = &S; ,O SXQWR GHOOD QRWD]LRQH SRVWILVVD KD SUHFHGHQ]D VXOO
RSHUDWRUH GL GHUHIHUHQFLQJ  SHU DFFHGHUH DOOH FRPSRQHQWL GHOOD VWUXWWXUD UHIHUHQ]LDWD GD 3 q QHFHVVDULR XWLOL]]DUH OH SDUHQWHVL WRQGH (*P).Campo_1=75; 2SHUDWRUH ! 2SHUDWRUH ! /
RSHUDWRUH ! FRQVHQWH GL DFFHGHUH DG XQ FDPSR GL XQD VWUXWWXUD UHIHUHQ]LDWD GD XQ SXQWDWRUH LQ PRGR SL VLQWHWLFR P->Campo_1=75; S, *P;

(VHPSLLR (VHPS R 6L YXROH UHDOL]]DUH XQ SURJUDPPD FKH GDWD GD LQSXW XQD VHTXHQ]D GL 1 SDUROH GL DO PDVVLPR  FDUDWWHUL FLDVFXQD  XQD SHU ULJD VWDPSL LQ RUHGLQH LQYHUVR OH SDUROH GDWH RJQXQD ULEDOWDWD FLRq VWDPSDQGR L FDUDWWHUL LQ RUGLQH LQYHUVR GDOO
XOWLPR DO SULPR  8WLOL]]DUH XQD VWUXWWXUD GLQDPLFD #include <stdio.h> #include <stdlib.h> typedef char parola[20]; parola w, *p; main() { parola w, *p; int i, j, N; printf("Quante parole? "); scanf("%d", &N); /* allocazione del vettore */ p=(parola *)malloc(N*sizeof(parola)); /* lettura della sequenza */ for(i=0; i<N; i++) gets(&p[i]);

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

462

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

463

6RWWRSURJUDPPL )XQ]LRQL H 3URFHGXUH
istruzioni

)XQ]LRQL H 3URFHGXUH
Ad esempiio:: Ordiinamentto dii un iinsiieme Ad esemp o Ord namen o d un ns eme
#include <stdio.h> #define dim 10 main() {int V[dim], i,j, max, tmp, quanti; /* lettura dei dati */ for (i=0; i<dim; i++) { printf("valore n. %d: ",i); scanf("%d", &V[i]); } /*ordinamento */ for(i=0; i<dim; i++) { quanti=dim-i; max=quanti-1; for( j=0; j<quanti; j++) if (V[j]>V[max]) max=j; if (max<quanti-1) { tmp=V[quanti-1]; V[quanti-1]=V[max]; V[max]=tmp; } } /*stampa */ for(i=0; i<dim; i++) printf("Valore di V[%d]=%d\n", i, V[i]); }

VHPSOLFL

VWUXWWXUDWH

predefinite
assegnamento input output break continue goto

predefinite definite dall’utente {..} VRWWRSURJUDPPL if..else.. switch..  IXQ]LRQL while..  SURFHGXUH for.. do

definite dall’utente

I linguaggi di alto livello permettono di definire istruzioni non primitive per risolvere parti specifiche di un problema: i VRWWRSURJUDPPL (funzioni e procedure).

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

466

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

467

• Potrebbe essere conveniente scrivere algoritmo in modo piu‘ DVWUDWWR: #include <stdio.h> #define dim 10 main() { int V[dim]; /* lettura dei dati */ leggi(V, dim); /*ordinamento */ ordina(V, dim); /*stampa */ stampa(V,dim); }

lo

stesso

6RWWRSURJUDPPL IXQ]LRQL H SURFHGXUH
• Rappresentano nuove istruzioni che agiscono sui dati utilizzati dal programma, "nascondendo" la sequenza delle operazioni effettivamente eseguite dalla macchina. • Vengono realizzate mediante la definizione di unita‘ di programma (VRWWRSURJUDPPL) distinte dal programma principale (PDLQ). º '
RUD LQ SRL il programma e‘ una FROOH]LRQH GL XQLWDC GL SURJUDPPD (tra le quali compare l’unita‘ PDLQ) Tutti i linguaggi di alto livello offrono la possibilita‘ di utilizzare funzioni e/o procedure. Ciio‘ e‘ reso possiibiille da:: C o‘ e‘ reso poss b e da • costrutti per la GHILQL]LRQH di sottoprogrammi • meccanismi per l’XWLOL]]R di sottoprogrammi (meccanismi di FKLDPDWD)

• + leggi(), ordina(), stampa() sono VRWWRSURJUDPPL: il main "chiama" leggi, ordina e stampa. Vanttaggii:: Van agg sintesi leggibilita‘ possibilita‘ di riutilizzo del codice

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

468

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

469

)XQ]LRQL H 3URFHGXUH
Defiiniiziione:: Def n z one Nella fase di GHILQL]LRQH di un sottoprogramma (funzione o procedura) si stabilisce: • un LGHQWLILFDWRUH del sottoprogramma • si esplicita il FRUSR del sottoprogramma (cioe‘, l’insieme di istruzioni che verra‘ eseguito ogni volta che il sotto-programma verra‘ FKLDPDWR); • si stabiliscono le PRGDOLWDC GL FRPXQLFD]LRQH tra l’unita‘ di programma che usa il sottoprogramma ed il sottoprogramma stesso (definizione dei SDUDPHWUL IRUPDOL). Uttiilliizzo dii funziionii//procedure (FKLLDPDWWD):: U zzo d funz on procedure (FK DPD D) • Per chiamare un sottoprogramma (cioe‘, per richiedere l’esecuzione del suo corpo), si utilizza l’identificatore assegnato al sottoprogramma in fase di definizione (FKLDPDWD o invocazione del sottoprogramma).

0HFFDQLVPR GL &KLDPDWD
Quando si verifica una chiamata a sottoprogramma, si possono individuare due entita‘: • l’unita‘ di programma FKLDPDQWH; • l’unita‘ di programma FKLDPDWD (il sotto-programma). Quando avviene la chiamata, l’esecuzione dell'unita` di programma "chiamante" (quella, cioe`, che contiene l'invocazione) viene VRVSHVD, ed il controllo passa al sottoprogramma chiamato (che eseguira` le istruzioni contenute nel corpo). L'unita` chiamante funge da FOLHQWH dell'unita` chiamata (che svolge il ruolo di VHUYLWRUH). Modellllo Clliientte-Serviittore Mode o C en e-Serv ore
Cliente
char C, C1; proc(C);

Servitore
proc(C1); void proc(char Car); { ... return; }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

470

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

471

3DUDPHWUL
I SDUDPHWUL costituiscono il mezzo di comunicazione tra unita‘ chiamante ed unita‘ chiamata. Supportano lo scambio di informazioni tra chiamante e sottoprogramma. SDUDPHWUL IRUPDOL: sono quelli specificati nella definizione del sottoprogramma. Sono in numero prefissato e ad ognuno di essi viene associato un tipo. Le istruzioni del corpo del sottoprogramma utilizzano i parametri formali. sono i valori effettivamente forniti dall’unita‘ chiamante al sottoprogramma all’atto della chiamata.

3DUDPHWUL
• Parametri DWWXDOL (specificati nella chiamata) e IRUPDOL (specificati nella definizione) devono corrispondersi in QXPHUR, SRVL]LRQH e WLSR. • All’atto della chiamata avviene il OHJDPH GHL SDUDPHWUL, cioe‘ ai parametri formali vengono associati i parametri attuali. &RPH DYYLHQH O
DVVRFLD]LRQH WUD SDUDPHWUL DWWXDOL H SDUDPHWUL IRUPDOL " Esistono, in generale, varie forme di legame. Ad esempio: • legame per YDORUH; • legame per LQGLUL]]R; Il significato delle due tecniche di legame dei parametri verra‘ spiegato piu‘ avanti.

SDUDPHWUL DWWXDOL:

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

472

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

473

)XQ]LRQL H 3URFHGXUH
Vanttaggii:: Van agg • ULXWLOL]]R GL FRGLFH: sintetizzando in un sottoprogramma un sotto-algoritmo, si ha la possibilita‘ di invocarlo piu‘ volte, sia nell’ambito dello stesso programma, che nell’ambito di programmi diversi (evitando di dover replicare ogni volta lo stesso codice). • migliore OHJJLELOLWDC: si ha in fatti una maggiore capacita‘ di astrazione • sviluppo WRSGRZQ: si delega a funzioni/procedure da sviluppare in una fase successiva la soluzione di sottoproblemi. • testo del programma piu‘ EUHYH: minore probabilita‘ di errori, dimensione del codice eseguibile piu‘ piccola.

3URFHGXUH H )XQ]LRQL
In generale, i sottoprogrammi SURFHGXUH e IXQ]LRQL: Procedura:: Procedura E` un’astrazione della nozione di LVWUX]LRQH. E` un’istruzione non primitiva attivabile in un qualunque punto del programma in cui puo` comparire un’istruzione. Funziione:: Funz one E’ un’astrazione del concetto di RSHUDWRUH. Si puo` attivare durante la valutazione di una qualunque espressione e UHVWLWXLVFH XQ YDORUH Ad esempiio:: Ad esemp o main() { int Ris, N=7; stampa(N);/*procedura*/ Ris=fattoriale(N)-10;/*funzione*/ }; º Formalmente, in C i sottoprogrammi sono soltanto IXQ]LRQL; le procedure possono essere realizzate come funzioni che non restituiscono alcun valore (YRLG). si suddividono in

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

474

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

475

)XQ]LRQL LQ &
Procedure e funzioni si definiscono seguendo regole sintattiche simili. Defiiniiziione dii funziione:: Def n z one d funz one 
GHIIXQ]LRQH!  LQWHVWD]LRQH! { SDUWHGLFKLDUD]LRQL! SDUWHLVWUX]LRQL! }

'HILQL]LRQH GL )XQ]LRQL LQ &
Bllocco :: B occo • Il blocco contiene il FRUSR della funzione e, come al solito, e‘ strutturato in una <parte dichiarazioni> e una <parte istruzioni>: • la <parte dichiarazioni> contiene le dichiarazioni e definizioni ORFDOL alla funzione; • la <parte istruzioni> contiene la sequenza di istruzioni associata al corpo (rappresenta l’algoritmo eseguito dalla funzione) • I dati riferiti nel blocco possono essere FRVWDQWL, YDULDELOL, oppure SDUDPHWUL IRUPDOL: all’interno del blocco, i parametri formali vengono trattati come variabili. Isttruziione retturn:: Is ruz one re urn Per restituire il risultato, la funzione utilizza (all’interno della parte istruzioni) l’istruzione UHWXUQ: UHWXUQ >HVSUHVVLRQH!@ (IIHWWR: restituisce il controllo al chiamante e assegna all’identificatore della funzione il valore dell’<espressione>.

Quindi, per definire una funzione, e‘ necessario specificare una LQWHVWD]LRQH e un EORFFR {..}: Sttruttttura dellll’iinttesttaziione:: S ru ura de ’ n es az one 
LQWHVWD]LRQH! WLSRULV! QRPH! >OLVWDSDUIRUPDOL!@

dove: • WLSRULV!: e‘ un indentificatore che indica il tipo di risultato restituito(FRGRPLQLR). Il tipo restituito puo‘ essere predefinito o definito dall’utente. Una funzione non puo‘ restituire valori di tipo: • YHWWRUH • IXQ]LRQH • QRPH! e‘ l’identificatore della funzione • OLVWDSDUIRUPDOL! e` la lista dei parametri formali (GRPLQLR). Per ciascun parametro formale viene specificato il tipo ed un identificatore che e` un nome simbolico per rappresentare il parametro all'interno della funzione (nel EORFFR). I parametri sono separati mediante virgola.

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

476

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

477

Esempiio:: Esemp o int maggioredi100 (int a) /*intest. */ { /*parte dichiarazioni: */ const int C=100; /* parte istruzioni: */ if (a>C) return 1; else return 0; } Esempiio:: Esemp o #define N 100 typedef char vettore[N];

Esempiio:: Esemp o int read_int () /* intest. */ { int a scanf("%d", &a); return a; } Possono esserci SLXC LVWUX]LRQL UHWXUQ: int max (int a, int b) /*intest.*/ { if (a>b) return a; else return b; } o QHVVXQD: int print_int (int a) /* intestazione */ { printf("%d", a); } º In questo caso, il sottoprogramma termina in corrispondenza del simbolo ` ed il valore restituito e‘ LQGHILQLWR

int minimo (vettore vet) { int i, v, min;/* def. locali a minimo */ for (min=vet[0], i=1; i<N; i++) { v=vet[i]; if (v<min) min=v; } return min; } º i, v, min sono YDULDELOL ORFDOL: • WHPSR GL YLWD: esistono solo durante l’esecuzione della funzione minimo • YLVLELOLWDC: sono visibili (cioe‘ utilizzabili) soltanto all’interno della funzione minimo.

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

478

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

479

Esempiio:: Esemp o /* funzione elevamento a potenza */ long power (int base, int n) { int i; long p=1; for (i=1;i<=n;++i) p *= base; /* p = p*base */ return p; /* ritorna il risultato */ }

)XQ]LRQL LQ &
Chiiamatta dii funziionii:: Ch ama a d funz on In generale, la chiamata di una funzione compare all’interno di una espressione secondo la sintassi: «QRPHIXQ]LRQH OLVWD SDUDPHWUL DWWXDOL! « Ad esempiio:: Ad esemp o main() { int z, x=2; ... z=power(x,2)+power(x,3); x=max(power(z,2), 30); printf("%d\n", x); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

480

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

481

5HDOL]]D]LRQH GHOOH 3URFHGXUH LQ &
Una funzione puo’ anche avere nessun valore (void) come risultato: YRLG YRLG fun(...) insieme vuoto (dominio vuoto) di valori

Esempiio:: Esemp o void print_int(int a) { printf("%d", a); } º Poiche‘ una procedura non restituisce alcun valore, non e‘ necessario prevedere l’istruzione di UHWXUQ all’interno del corpo; se si utilizza, QRQ VL GHYH VSHFLILFDUH DOFXQ DUJRPHQWR:

funzione che non restituisce alcun valore

return;
Uso:: Uso La procedura e‘ l’astrazione del concetto di istruzione: main() { int X; scanf("%d", &X); print_int(X): }

,Q TXHVWR PRGR V UHD ]]D Q & FRQFHWWR G procedura º ,Q TXHVWR PRGR VLL UHDOOLL]]D LLQ & LLOO FRQFHWWR GLL procedura

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

482

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

483

(VHPSLR
#include <stdio.h> int max (int a, int b) /*def. max*/ { if (a>b) return a; else return b; } void print_int (int a) /* def. */ { printf("%d\", a); return; } void dummy() /*def. dummy */ { printf("Ciao!\n"); } main() { int A, B; printf("Dammi A e B: "); scanf("%d %d", &A, &B); print_int(max(A,B)); dummy(); }

6WUXWWXUD GHL 3URJUDPPL &
Quale struttura devono avere i programmi che fanno uso di funzioni ? E‘ necessario aggregare la definizione del main alle definizioni delle funzioni utilizzate, ad esempio secondo lo schema seguente: OLVWD GHOOH GHILQL]LRQL GL IXQ]LRQL! PDLQ! • all’interno del file sorgente vengono prima elencate le definizioni delle funzioni necessarie, ed infine viene esplicitato il main. Ad esempiio:: Ad esemp o #include <stdio.h> int max(int a, int b); main() { int A, B; printf("Dammi A e B: "); scanf("%d %d", &A, &B); printf("%d\n", max(A,B)); } int max (int a, int b) { if (a>b) return a; else return b; }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

484

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

485

º Se all’interno di un blocco viene utilizzata una funzione f, la definizione di f deve comparire prima del blocco che la utilizza. Esempiio:: Esemp o #include <stdio.h> int max (int a, int b) { if (a>b) return a; else return b; } int sommamax(int a1, a2, a3, a4) { return max(a1,a2)+max(a3,a4);} main() { int A, B, C,D; scanf("%d%d%d%d",&A,&B,&C,&D); printf("%d\n", sommamax(A,B,C,D)); }

'LFKLDUD]LRQH GL IXQ]LRQH
Regolla Generalle:: Rego a Genera e Prima di utilizzare una funzione e‘ necessario che sia gia‘ stata GHILQLWD RSSXUH GLFKLDUDWD. Funziionii C:: Funz on C • GHILQL]LRQH: descrive le proprieta‘ della funzione (tipo, nome, lista parametri formali) e la sua realizzazione (lista delle istruzioni contenute nel blocco). • GLFKLDUD]LRQH SURWRWLSR : descrive le proprieta‘ della funzione senza definirne la realizzazione (EORFFR) ◊ serve per "anticipare" le caratteristiche di una funzione definita successivamente. Diichiiaraziione dii una funziione:: D ch araz one d una funz one La GLFKLDUD]LRQH di una funzione si esprime mediante l’intestazione della funzione, seguita da "": 
WLSRULV! QRPH! >OLVWDSDUIRUPDOL!@ 

$G HVHPSLR Dichiarazione della funzione max: int max(int a, int b);

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

486

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

487

Esempiio:: Esemp o #include <stdio.h> main() { int A, B; printf("Dammi A e B: "); scanf("%d %d", &A, &B); printf("%d\n", max(A,B)); } int max (int a, int b) { if (a>b) return a; else return b; } • In questo caso il compilatore segnala un HUURUH in corrispondenza della chiamata PD[ $% , perche‘ viene usato un identificatore che viene definito successivamente (dopo il main())

Solluziione:: So uz one #include <stdio.h> int max(int a, int b); main() { int A, B; printf("Dammi A e B: "); scanf("%d %d", &A, &B); printf("%d\n", max(A,B)); } int max (int a, int b) /*intestaz. */ { if (a>b) return a; else return b; }

E lle diichiiaraziionii dii priinttf,, scanf ettc.. ? E e d ch araz on d pr n f scanf e c ? • sono contentue nel file stdio.h: LQFOXGH VWGLRK! provoca l’inserimento del contenuto del file specificato.

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

488

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

489

'LFKLDUD]LRQH GL )XQ]LRQL
Una funzione puo‘ essere GLFKLDUDWD in punti diversi, ma e‘ GHILQLWD una sola volta. E‘ possibile inserire i prototipi delle funzioni utilizzate: • nella parte dichiarazioni globali di un programma, • nella parte dichiarazioni del PDLQ, • nella parte dichiarazioni delle funzioni. Ad esempiio:: Ad esemp o main() { long power (int base, int n); int X, exp; scanf("%d%d", &X, &exp); printf("%ld", power(X,exp)); } ...

6WUXWWXUD GHL 3URJUDPPL &
Spesso si strutturano i programmi in modo tale che la definizione del main compaia prima delle definizioni delle altre funzioni (per favorire la OHJJLELOLWD‘).

Prottocollllo da uttiilliizzare:: Pro oco o da u zzare OLVWD GLFKLDUD]LRQL GL IXQ]LRQL! PDLQ! GHILQL]LRQL GHOOH IXQ]LRQL GLFKLDUDWH!

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

490

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

491

Ad esempiio:: Ad esemp o Calcolo della radice intera di un numero intero letto a terminale. #include <stdio.h> /* dichiarazioni delle funzioni: */ int RadiceInt (int par); int Quadrato (int par); main(void) { int X; scanf("%d", &X); printf("Radice: %d\n", RadiceInt(X)); printf("Quadrato: %d\n", Quadrato(X)); } /* definizione funzioni: */ int RadiceInt (int par) { int cont = 0; while (cont*cont <= par) cont = cont + 1; return (cont-1); } int Quadrato (int par) { return (par*par); }

7HFQLFKH GL OHJDPH GHL SDUDPHWUL
Come viene realizzata l’associazione tra parametri attuali e parametri formali? In generale, esistono vari meccanismi di legame dei parametri. Meccaniismii piiu‘ comunii:: Meccan sm p u‘ comun • • Legame per YDORUH (C, Pascal); Legame per LQGLUL]]R, o per riferimento (Pascal, Fortran).

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

492

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

493

7HFQLFKH GL /HJDPH GHL SDUDPHWUL
Per spiegare le varie tecniche di legame faremo riferimento alla seguente situazione: Consideriamo una procedura 3 con un parametro formale SI. Supponiamo che P venga chiamata da una unita‘ di programma &, mediante la chiamata: 3 SD dove SD e‘ una variabile visibile in C. Quindi, utilizzando la sintassi C: Unita‘ C int pa; … P(pa); … Unita‘ P: void P(int pf) { … }

/HJDPH SHU YDORUH
6H LO OHJDPH GHL SDUDPHWUL DYYLHQH SHU YDORUH  3ULPD GHOOD FKLDPDWD
Area dati di C

pa

α 

$O PRPHQWR GHOOD FKLDPDWD • viene allocata una cella di memoria associata a pf nell’area dati accessibile a P • viene valutato pa, ed il suo valore viene FRSLDWR in pf
Area dati di C Area dati di P

pa

α

pf

α

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

494

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

495

Esecuziione dii P:: Esecuz one d P Il parametro formale SI viene trattato come una YDULDELOH ORFDOH al sottoprogramma P: puo‘ essere modificato mediante assegnamento, etc.. In generale, al termine della chiamata, pf potra‘ assumere un valore diverso da quello iniziale. $OOD ILQH GHOO¶HVHFX]LRQH GL 3
Area dati di C Area dati di P

/HJDPH SHU YDORUH
Quiindii:: Qu nd Se il legame dei parametri avviene per valore, immediatamente dopo l’esecuzione della chiamata, il parametro attuale (pa) mantiene il valore che aveva immediatamente prima della chiamata º Parametri passati per valore servono soltanto a comunicare YDORUL LQ LQJUHVVR al sotto-programma. º Se il passaggio avviene per valore, pa non e‘ necessariamente una variabile, ma puo‘ essere, in generale, una HVSUHVVLRQH. ,O OHJDPH SHU YDORUH HC O
XQLFD WHFQLFD GL OHJDPH GLVSRQLELOH LQ &

pa

α

pf

α’

• Al termine della chiamata, il valore di pa rimane LQDOWHUDWR

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

496

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

497

Ad esempiio:: Ad esemp o #include <stdio.h> void P(int pf); main() { int pa=10; P(pa); printf("valore finale di pa: %d\n", pa); /* pa vale 10 */ } void P(int pf) { pf=100; printf("valore finale di pf: %d\n", pf); return; }

/HJDPH SHU LQGLUL]]R
Se il legame dei parametri avviene per indirizzo:  3ULPD GHOOD FKLDPDWD
Area dati di C

pa

α 

$O PRPHQWR GHOOD FKLDPDWD • viene associato all’identificatore pf la stessa cella di memoria riferita da pa:
Area dati di C

pa

α

pf

º pf e‘ un DOLDV di pa.

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

498

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

499

Esecuziione dii P:: Esecuz one d P Il parametro formale SI viene trattato come una YDULDELOH ORFDOH al sottoprogramma P: puo‘ essere modificato mediante assegnamento, etc.. In generale, al termine della chiamata, pf (e quindi pa) potra‘ assumere un valore α’ diverso da quello iniziale. $OOD ILQH GHOO¶HVHFX]LRQH GL 3
Area dati di C

/HJDPH SHU LQGLUL]]R
Quiindii:: Qu nd Se il legame dei parametri avviene per indirizzo, immediatamente dopo l’esecuzione della chiamata, il parametro attuale (pa) puo‘ avere un valore diverso da quello che aveva immediatamente prima della chiamata º Parametri passati per indirizzo servono per comunicare valori VLD LQ LQJUHVVR FKH LQ XVFLWD dal sottoprogramma. º Se il passaggio avviene per indirizzo, pa deve necessariamente essere una variabile (cioe‘, un oggetto dotato di un indirizzo). ,Q & LO OHJDPH SHU LQGLUL]]R QRQ HC GLVSRQLELOH

pa

α’

pf

• Al termine della chiamata, il valore di pa risulta PRGLILFDWR

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

500

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

501

Esempiio:: Esemp o Utilizziamo la sintassi C per esemplificare il passaggio per indirizzo Il programma che segue e‘ solo a scopo esemplificativo (in C, non c’e‘ il passaggio per indirizzo!). Funzione che scambia due variabili X, Y (di tipo integer) se X>Y e restituisce il valore minore tra i due. #include <stdio.h> void scambia (int A, int B); main() { int X=5,Y=0 ; scanf("%d %d", &X, &Y); scambia(X,Y); printf("\n%d \t %d %", X,Y); } void scambia (int A, int B) /* se fosse per indirizzo ! */ { int T; if (A>B) { T=A; A=B; B=T; return; } else return; }

3DVVDJJLR GHL SDUDPHWUL SHU LQGLUL]]R LQ &
In C questa tecnica di legame QRQ HC SUHYLVWD. Si puo‘ ottenere lo stesso effetto delo passaggio per indirizzo utilizzando SDUDPHWUL GL WLSR SXQWDWRUH. Ad esempiio:: Ad esemp o #include <stdio.h> int scambia2 (int *A, int *B); main() { int X,Y ; scanf("%d %d", &X, &Y); printf("\n Scambia: %d", scambia2(&X,&Y)); printf("\n%d \t %d %", X,Y); } int scambia2 (int *A, int *B) {int T; if (*A>*B) { T=*A; *A=*B; *B=T; return *A; } else return *B; }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

502

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

503

Esempiio:: Esemp o #include <stdio.h> void Fun ( ? int int N; main() { N=3; Fun (N); printf("%d", N); } void Fun ( ? int { X=X+1; printf("%d", N); printf("%d", X); } X) {1} {2}

X);

{3}

Se il legame e‘ SHU YDORUH viene stampato: {1} 3 {2} 4 {3} 3 Se il legame e‘ SHU LQGLUL]]R : {1} 4 {2} 4 {3} 4

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

504

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C

505

)XQ]LRQL H 3URFHGXUH
$OOFXQLL (VHPSLL $ FXQ (VHPS  #include <stdio.h> void h (int X, int *Y); main() {int A,B; A=0; B=0; h(A, &B); /*B e‘ un parametro di uscita*/ printf("\n %d \t %d", A, B); } void h (int X, int *Y) { X=X+1; *Y=*Y+1; printf("\n %d \t %d", X, *Y); } 1 0 1 1 (stampa di “h”) (stampa di “main”)

(VHUFLL]LLR (VHUF ] R &DOFROR GHOOH UDGLFL GL XQD HTXD]LRQH GL VHFRQGR JUDGR $[  %[ &  #include <stdio.h> #include <math.h> typedef enum {false,true} boolean; boolean radici(int A, int B, int C, float *X1, float *X2); main() { int A,B,C; float X,Y; scanf("%d%d%d%\n",&A,&B,&C); if ( radici(A,B,C,&X,&Y) ) printf("%f%f\n",X,Y); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

502

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

503

boolean

radici(int A, int B, int C, float *X1, float *X2) { float D; D= B*B-4*A*C; if (D<0) return 0; else { D=sqrt(D); *X1 = (-B+D)/(2*A); *X2= (-B-D)/(2*A); return 1; } }

(VHUFLL]LLR (VHUF ] R 3URJUDPPD FKH VWDPSD L QXPHUL SULPL FRPSUHVL WUD  HG Q Q GDWR  #include <stdio.h> #include <math.h> #define NO 0 #define YES 1

int isPrime(int n); /*dichiarazioni*/ int primes(int n); void main() { int n; do { printf("\nNumeri primi non superiori a:\t"); scanf("%d",&n); } while (n<1); printf("\nTrovati %d numeri primi.\n", primes(n)); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

504

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

505

int isPrime(int n) { int max,i; if (n>0 && n<4) return YES; /* 1, 2 e 3 sono primi else if (!(n%2)) return NO; /* escludi i pari > 2 */ max = sqrt( (double)n ); /* CAST:sqrt ha arg double */ for(i=3; i<=max; i+=2) if (!(n%i)) return NO; return YES; } int primes(int n) { int i,count; if (n<=0) return -1; else count=0; if (n>=1) { printf("%5d\t",1); count++; } if (n>=2) { printf("%5d\t",2); count++; } for(i=3;i<=n;i+=2) if (isPrime(i)) { printf("%5d\t",i); count++; } printf("\n"); return count; }
FONDAMENTI DI INFORMATICA
IL LINGUAGGIO C - FUNZIONI 506

(VHUFLL]LLR (VHUF ] R 6FULYHUH XQD SURFHGXUD FKH ULVROYH XQ VLVWHPD OLQHDUH GL GXH HTXD]LRQL LQ GXH LQFRJQLWH */ D[  E\ D[  E\ [ \ F F

FEFE  DE DE ;1  ' DF DF  DE DE <1  '

6ROOX]LLRQH 6R X] RQH #include <stdio.h> void sistema(int A1, int B1, int C1, int A2, int B2, int C2, float *X, float *Y); main() { int float

A1,B1,C1,A2,B2,C2; X,Y;

scanf("%d%d%d%\n",&A1,&B1,&C1); scanf("%d%d%d%\n",&A2,&B2,&C2); sistema(A1,B1,C1,A2,B2,C2,&X,&Y); printf("%f%f\n",X,Y); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

507

void sistema (int A1, int B1, int C1, int A2, int B2, int C2, float *X, float *Y) { int XN,YN,D; XN = (C1*B2 - C2*B1); D = (A1*B2 - A2*B1); YN = (A1*C2 - A2*C1); if (D == 0) {if (XN == 0) printf("sistema indeterminato\n"); else printf("sistema impossibile\n"); } else { printf("Determinante%d",D); *X=(float) (XN) /D; *Y=(float) (YN) /D; } }

9HWWRUL FRPH SDUDPHWUL GL IXQ]LRQL
,Q & L YHWWRUL VRQR VHPSUH SDVVDWL DWWUDYHUVR LO ORUR LQGLUL]]R $G HVHPSLLR $G HVHPS R #include <stdio.h> #define MAXDIM 30 int getvet(int v[], int maxdim); main() { int k, vet[MAXDIM]; int dim; dim=getvet(vet,MAXDIM); ... 'HILLQLL]LLRQH GHOOOOD IXQ]LLRQH 'HI Q ] RQH GH D IXQ] RQH int getvet(int *v, int maxdim); { int i; for(i=0;i<maxdim;i++) {printf("%d elemento:\t", i+1); scanf("%d", &v[i]); } return n; } Ý,O YHWWRUH HC PRGLILFDWR DOO
LQWHUQR GHOOD IXQ]LRQH H OH PRGLILFKH VRSUDYYLYRQR DOO
HVHFX]LRQH GHOOD IXQ]LRQH SRLFKHC LQ & L YHWWRUL VRQR WUDVIHULWL SHU LQGLUL]]R

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

508

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

509

6WUXWWXUD GL XQ 3URJUDPPD &
6H XQ SURJUDPPD ID XVR GL IXQ]LRQLSURFHGXUH OD VXD VWUXWWXUD YLHQH HVWHVD FRPH VHJXH LQFOXGH VWGLRK!  YDULDELOL H WLSL JOREDOL DO SURJUDPPD YLVLELOLWj QHOO
LQWHUR SURJUDPPD  WLSRYDU QRPHYDU   GLFKLDUD]LRQL IXQ]LRQL  LQW ) SDUDPHWUL   LQW )1 SDUDPHWUL   PDLQ  PDLQ YRLG ^  YDULDELOL ORFDOL DO PDLQ YLVLELOLWj QHO VROR PDLQ   FRGLFH GHO PDLQ VL LQYRFDQR OH )L  `  ILQH PDLQ   GHILQL]LRQL IXQ]LRQL  LQW )  ^  SDUWH GLFKLDUDWLYD   FRGLFH GL )  ` œ /H GHILQL]LRQL GL IXQ]LRQL QRQ SRVVRQR HVVHUH LQQHVWDWH LQ DOWUH IXQ]LRQL R EORFFKL
FONDAMENTI DI INFORMATICA
IL LINGUAGGIO C - FUNZIONI 510

9DULLDELLOOLL OORFDOOLL 9DU DE RFD  1HOOD SDUWH GLFKLDUDWLYD GL XQ VRWWRSURJUDPPD SURFHGXUD R IXQ]LRQH SRVVRQR HVVHUH GLFKLDUDWL FRVWDQWL WLSL YDULDELOL GHWWL ORFDOL R DXWRPDWLFKH  #include <stdio.h> char saltabianchi (void); main(void) {char C; C = saltabianchi(); printf("\n%c",C); /* stampa } char saltabianchi (void) {char Car; /* Car e‘ locale*/ do { scanf("%c", &Car);} while (Car==’ ’); return Car; } œ $OOD YDULDELOH &DU VL SXRC IDU ULIHULPHQWR VROR QHO FRUSR GHOOD IXQ]LRQH VDOWDELDQFKL FDPSR GL D]LRQH  œ ,O WHPSR GL YLWD GL &DU HC LO WHPSR GL HVHFX]LRQH GHOOD IXQ]LRQH VDOWDELDQFKL œ , SDUDPHWUL IRUPDOL YHQJRQR WUDWWDWL FRPH YDULDELOL ORFDOL

*/

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

511

9DULDELOL /RFDOL
œ 4XDQGR XQD IXQ]LRQH YLHQH FKLDPDWD YLHQH FUHDWD XQD DVVRFLD]LRQH WUD O
LGHQWLILFDWRUH GL RJQL YDULDELOH ORFDOH H SDUDPHWUR IRUPDOH HG XQD FHOOD GL PHPRULD DOORFDWD DXWRPDWLFDPHQWH (VHPSLLR (VHPS R int f(char Car) { int P; main() { char C; f(C); } œ $OOD ILQH GHOO¶DWWLYD]LRQH RJQL FHOOD GL PHPRULD DVVRFLDWD D YDULDELOL ORFDOL YLHQH GHDOORFDWD 6H OD SURFHGXUD YLHQH DWWLYDWD GL QXRYR YLHQH FUHDWD XQD QXRYD DVVRFLD]LRQH œ 1RQ F¶HC FRUUHOD]LRQH WUD L YDORUL FKH Car DVVXPH GXUDQWH OH YDULH DWWLYD]LRQL GHOOD IXQ]LRQH I

9DULDELOL HVWHUQH R JOREDOL
œ 1HOO¶DPELWR GHO EORFFR GL XQ VRWWRSURJUDPPD GHO PDLQ VL SXRC IDU ULIHULPHQWR DQFKH DG LGHQWLILFDWRUL JOREDOL QHOOD SDUWH GLFKLDUD]LRQL JOREDOL GHO SURJUDPPD œ ,O WHPSR GL YLWD GHOOH YDULDELOL HVWHUQH HC SDUL DO WHPSR GL HVHFX]LRQH GHO SURJUDPPD YDULDELOL VWDWLFKH  (VHPSLLR (VHPS R #include <stdio.h> void saltabianchi (void); char C; /* def. variabile esterna */ main() { saltabianchi(); printf("\n%c",C); }

/* stampa C */

void saltabianchi (void) { do {scanf("%c", &C);} while (C==’ ’); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

512

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

513

9DULDELOL (VWHUQH
1HOOOO

HVHPSLLR 1H HVHPS R œ & HC XQD YDULDELOH HVWHUQD VLD DO PDLQ FKH D VDOWDELDQFKL œ OD GHILQL]LRQH GL & HC YLVLELOH VLD GDOOD IXQ]LRQH PDLQ FKH GDOOD SURFHGXUD VDOWDELDQFKL (QWUDPEH TXHVWH XQLWDC SRVVRQR IDU ULIHULPHQWR DOOD YDULDELOH & œ (C OD VWHVVD YDULDELOH 2JQL PRGLILFD D & SURGRWWD GDOOD IXQ]LRQH YLHQH YLVWD DQFKH GDO PDLQ Ý SRVVLELOLWDC GL HIIHWWL FROODWHUDOL

(IIHWWL FROODWHUDOL
6L FKLDPD HIIHWWR FROODWHUDOH VLGH HIIHFW SURYRFDWR GDOO¶DWWLYD]LRQH GL XQD IXQ]LRQH OD PRGLILFD GL XQD TXDOXQTXH WUD OH YDULDELOL HVWHUQH 6L SRVVRQR DYHUH QHL VHJXHQWL FDVL œ SDUDPHWUL GL WLSR SXQWDWRUH œ DVVHJQDPHQWR D YDULDELOL HVWHUQH 6H SUHVHQWL OH IXQ]LRQL QRQ VRQR SLXC IXQ]LRQL LQ VHQVR PDWHPDWLFR (VHPSLLR (VHPS R #include <stdio.h> int B; int f (int * A); main() { B=1; printf("%d\n",2*f(&B)); /* (1) */ B=1; printf("%d\n",f(&B)+f(&B)); /* (2) */ } int f (int * A) { *A=2*(*A); return *A; } Ý)RUQLVFH YDORUL GLYHUVL SXU HVVHQGR DWWLYDWD FRQ OR VWHVVR SDUDPHWUR DWWXDOH /¶LVWUX]LRQH  VWDPSD  PHQWUH O¶LVWUX]LRQH  VWDPSD 

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

514

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

515

(IIHWWL &ROODWHUDOL
(VHPSLLR (VHPS R int V=2; float f (int X) { V=V*X; /* origine side effect */ return (X+1) } main() { int B; B=2; printf("%f",V+f(B)); B=2;V=2; printf("%f",f(B)+V); } ,Q TXHVWR FDVR 9I ; ž I ; 9

9DULDELOL HVWHUQH SDVVDJJLR GHL SDUDPHWUL
/H YDULDELOL HVWHUQH UDSSUHVHQWDQR XQ PRGR DOWHUQDWLYR DL SDUDPHWUL SHU IDU LQWHUDJLUH WUD ORUR OH YDULH XQLWDC GL SURJUDPPD 9DQWDJJLL 9DQWDJJ  œ (YLWDQR OXQJKH OLVWH GL SDUDPHWUL GD FRSLDUH VH SDVVDWL SHU YDORUH  œ 3HUPHWWRQR GL UHVWLWXLUH LQ PRGR GLUHWWR L ULVXOWDWL DO FKLDPDQWH 6YDQWDJJLL 6YDQWDJJ  œ , SURJUDPPL ULVXOWDQR PHQR OHJJLELOL ULVFKLR GL HUURUL  œ ,QWHUD]LRQH PHQR HVSOLFLWD WUD OH GLYHUVH XQLWDC GL SURJUDPPD HIIHWWL FROODWHUDOL  œ *HQHUDOLWDC ULXVDELOLWDC H SRUWDELOLWDC GLPLQXLVFRQR

(OOLLPLLQD]LLRQH GHJOOLL (IIHWWLL &ROOOODWHUDOOLL ( P QD] RQH GHJ (IIHWW &R DWHUD  3HU HYLWDUH HIIHWWL FROODWHUDOL LQ IXQ]LRQL RFFRUUH œ QRQ DYHUH SDUDPHWUL SDVVDWL SHU LQGLUL]]R QHOOH LQWHVWD]LRQL GL IXQ]LRQL œ QRQ LQWURGXUUH DVVHJQDPHQWL D YDULDELOL HVWHUQH QHO FRUSR GL IXQ]LRQL

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

516

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

517

9LVLELOLWDC GHJOL ,GHQWLILFDWRUL H 7HPSR GL 9LWD
'DWR XQ SURJUDPPD 3 FRVWLWXLWR GD GLYHUVH XQLWDC GL SURJUDPPD HYHQWXDOPHQWH VFRPSRVWH LQ EORFFKL VL GLVWLQJXH WUD ‡ $PELHQWH JOREDOH HC FRVWLWXLWR GDOOH GLFKLDUD]LRQL H GHILQL]LRQL FKH FRPSDLRQR QHOOD SDUWH GL GLFKLDUD]LRQL JOREDOL GL 3 ‡ $PELHQWH ORFDOH D XQD IXQ]LRQH HC O
LQVLHPH GHOOH GLFKLDUD]LRQL H GHILQL]LRQL FKH FRPSDLRQR QHOOD SDUWH GLFKLDUD]LRQL GHOOD IXQ]LRQH SLXC L VXRL SDUDPHWUL IRUPDOL ‡ $PELHQWH GL XQ EORFFR HC O
LQVLHPH GHOOH GLFKLDUD]LRQL H GHILQL]LRQL FKH FRPSDLRQR DOO
LQWHUQR GHO EORFFR

9LVLELOLWDC GHJOL ,GHQWLILFDWRUL
'DWR XQ LGHQWLILFDWRUH LO VXR FDPSR GL D]LRQH R VFRSH GL YLVLELOLWDC HC FRVWLWXLWR GDOO
LQVLHPH GL WXWWH OH LVWUX]LRQL FKH SRVVRQR XWLOL]]DUOR 5HJROOH GLL YLLVLLELLOOLLWDC GHJOOLL LLGHQWLLILLFDWRULL LLQ & 5HJR H G Y V E WDC GHJ GHQW I FDWRU Q & œ LO FDPSR GL D]LRQH GHOOD GLFKLDUD]LRQH R GHILQL]LRQH GL XQ LGHQWLILFDWRUH HVWHUQR YD GDO SXQWR LQ FXL VL WURYD OD GLFKLDUD]LRQHGHILQL]LRQH ILQR DOOD ILQH GHO ILOH VRUJHQWH D PHQR GHOOD UHJROD   œ LO FDPSR GL D]LRQH GHOOD GLFKLDUD]LRQH R GHILQL]LRQH GL XQ LGHQWLILFDWRUH ORFDOH HC LO EORFFR R OD IXQ]LRQH LQ FXL HVVD FRPSDUH H WXWWL L EORFFKL LQ HVVR FRQWHQXWL D PHQR GL ULGHILQL]LRQL Y UHJROD   œ TXDQGR XQ LGHQWLILFDWRUH GLFKLDUDWR LQ XQ EORFFR 3 HC ULGLFKLDUDWR R ULGHILQLWR LQ XQ EORFFR 4 UDFFKLXVR GD 3 DOORUD LO EORFFR 4 H WXWWL L EORFFKL LQQHVWDWL LQ 4 VRQR HVFOXVL GDO FDPSR GL D]LRQH GHOOD GLFKLDUD]LRQH GHOO¶LGHQWLILFDWRUH LQ 3 RYHUULGLQJ  œ ,O FDPSR GL D]LRQH GL RJQL LGHQWLILFDWRUH HC GHWHUPLQDWR VWDWLFDPHQWH GDOOD VWUXWWXUD GHO WHVWR GHO SURJUDPPD UHJROH GL YLVLELOLWDC OHVVLFDOL 

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

518

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

519

9LVLELOLWDC GHJOL ,GHQWLILFDWRUL
'DOOH UHJROH SUHFHGHQWL SRVVLDPR FRQFOXGHUH FKH ‡ 3HU XQD YDULDELOH ORFDOH H SHU L SDUDPHWUL IRUPDOL  GLFKLDUDWD LQ XQD IXQ]LRQH LO FDPSR GL D]LRQH HC OD IXQ]LRQH VWHVVD ‡ 3HU XQD YDULDELOH HVWHUQD FRVLC FRPH SHU OH IXQ]LRQL FKH VRQR WXWWH GLFKLDUDWH HVWHUQDPHQWH LO FDPSR GL D]LRQH YD GDO SXQWR LQ FXL VL WURYD OD GLFKLDUD]LRQH ILQR DOOD ILQH GHO ILOH VRUJHQWH

(VHPSLLR (VHPS R #include <stdio.h> main(void) {int i=0; while (i<=3) { /* BLOCCO 1 */ int j=4; /* def. locale al blocco 1*/ j=j+i; i++; { /* BLOCCO 2: interno al blocco 1*/ float i=j; /*locale al blocco 2*/ printf("%f\t%d\t",i,j); } printf("%d\t\n",i); } }

main blocco1 blocco2

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

520

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

521

(VHPSLLR (VHPS R #include <stdio.h> int X=0; void P1 (); /* superflua void P2 (); main() { X++; P2; } void P1 () { printf("\n%d",X); } void P2 () {float X; X=2.14; P1; } 6WDPSD LO YDORUH  Ý &RQ UHJROH GL YLVLELOLWDC GLQDPLFKH QRQ DGRWWDWH GDO & PD DG HVHPSLR LQ /,63  VWDPSHUHEEH LO YDORUH 

7HPSR GL YLWD GHOOH YDULDELOL
*/ œ (C O
LQWHUYDOOR GL WHPSR FKH LQWHUFRUUH WUD O
LVWDQWH GHOOD FUHD]LRQH DOORFD]LRQH GHOOD YDULDELOH H O
LVWDQWH GHOOD VXD GLVWUX]LRQH GHDOORFD]LRQH  œ (C O
LQWHUYDOOR GL WHPSR LQ FXL OD YDULDELOH HVLVWH HG LQ FXL FRPSDWLELOPHQWH FRQ OH UHJROH GL YLVLELOLWDC SXRC HVVHUH XWLOL]]DWD ,Q & ,Q & œ 9DULDELOL HVWHUQH VRQR DOORFDWH DOO
LQL]LR GHO SURJUDPPD H YHQJRQR GLVWUXWWH TXDQGR LO SURJUDPPD WHUPLQD Å LO WHPSR GL YLWD HC SDUL DO WHPSR GL HVHFX]LRQH GHO SURJUDPPD œ 9DULDELOL ORFDOL H SDUDPHWUL IRUPDOL GHOOH IXQ]LRQL VRQR DOORFDWL RJQL YROWD FKH VL LQYRFD OD IXQ]LRQH H GLVWUXWWL DO WHUPLQH GHOOD IXQ]LRQH œ ,O WHPSR GL YLWD HC TXLQGL SDUL DOOD GXUDWD GHOO¶DWWLYD]LRQH GHOOD IXQ]LRQH LQ FXL FRPSDUH OD GHILQL]LRQH GHOOD YDULDELOH œ 9DULDELOL GLQDPLFKH KDQQR XQ WHPSR GL YLWD SDUL DOOD GXUDWD GHOO
LQWHUYDOOR GL WHPSR FKH LQWHUFRUUH WUD OD PDOORF FKH OH DOORFD H OD IUHH FKH OH GHDOORFD

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

522

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

523

/D PDFFKLQD DVWUDWWD & PRGHOOR D WHPSR GL HVHFX]LRQH
$UHH GLL PHPRULLD $UHH G PHPRU D œ œ œ œ &RGLFH &RGH VHJPHQW $UHD GDWL JOREDOH VWDWLFD  'DWD VHJPHQW +HDS GLQDPLFD 6WDFN GLQDPLFD  &2'( 6(*0(17 '$7$ 6(*0(17 +($3 Ä Â 67$&. 

6WDFN
,Q & OH DWWLYD]LRQL GHOOH IXQ]LRQL VRQR UHDOL]]DWH XWLOL]]DQGR O
DUHD GL PHPRULD VWDFN LQ FXL ULVLHGH XQD VWUXWWXUD GDWL JHVWLWD VHJXHQGR XQD GLVFLSOLQD D SLOD OR VWDFN (C XQD VWUXWWXUD GDWL VX FXL HC SRVVLELOH HVHJXLUH GXH RSHUD]LRQL œ LQVHULPHQWR GL XQ HOHPHQWR SXVK œ HVWUD]LRQH GXL XQ HOHPHQWR SRS 3ROOLLWLLFD GLL JHVWLLRQH GHOOOOR VWDFN 3R W FD G JHVW RQH GH R VWDFN O
XOWLPR HOHPHQWR LQVHULWR HC LO SULPR DG HVVHUH HVWUDWWR SROLWLFD /,)2 /DVW ,Q )LUVW 2XW 
inserimento (push) estrazione (pop) top

bottom

œ $G RJQL FKLDPDWD GL VRWWRSURJUDPPD YLHQH FUHDWR XQ HOHPHQWR UHFRUG G
DWWLYD]LRQH HG LQVHULWR SXVK LQ FLPD DOOR VWDFN

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

524

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

525

5HFRUG G¶DWWLYD]LRQH
8Q UHFRUG G¶DWWLYD]LRQH FRQWLHQH OH LQIRUPD]LRQL UHODWLYH DG XQD VSHFLILFD FKLDPDWD GL IXQ]LRQHSURFHGXUD ,Q SDUWLLFROODUH ,Q SDUW FR DUH  QRPH GHOOD IXQ]LRQH DWWLYDWD H ULIHULPHQWR DO FRGLFH  SXQWR GL ULWRUQR DO FKLDPDQWH UHWXUQ DGGUHVV  HC O¶LQGLUL]]R GHOO¶LVWUX]LRQH GD HVHJXLUH DO WHUPLQH GHOOD DWWLYD]LRQH  ULIHULPHQWR GL FDWHQD VWDWLFD VWDWLF OLQN  HC XQ ULIHULPHQWR DOO¶DPELHQWH YLVWR ³VWDWLFDPHQWH´ GDO VRWWRSURJUDPPD YDULDELOL HVWHUQH   SDUDPHWUL IRUPDOL H ORUR OHJDPH FRQ TXHOOL DWWXDOL VH SHU LQGLUL]]R   YDULDELOL ORFDOL  ULIHULPHQWR DO UHFRUG GL DWWLYD]LRQH SUHFHGHQWH VXOOD SLOD FDWHQD GLQDPLFD R G\QDPLF OLQN  HC XQ ULIHULPHQWR DOO¶DPELHQWH GHO FKLDPDQWH $O WHUPLQH GHOO
HVHFX]LRQH UHWXUQ  LO UHFRUG GL DWWLYD]LRQH YLHQH GHDOORFDWR GDOOR VWDFN RSHUD]LRQH GL SRS

5HFRUG GL $WWLYD]LRQH
4XDQGR O
DWWLYD]LRQH GHOOD IXQ]LRQH WHUPLQD LVWUX]LRQH UHWXUQ R XOWLPD LVWUX]LRQH O¶HVHFX]LRQH SURVHJXH GDOO
LVWUX]LRQH PHPRUL]]DWD QHO UHWXUQ DGGUHVV (VHPSLLR (VHPS R #include <stdio.h> void R(int A) { printf("Valore: %d\n", A); } void Q(int A) { R(A); } void P() { int a=10; Q(a); return; } main() { P(); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

526

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

527

5HFRUG GL $WWLYD]LRQH
&DWHQD 'LLQDPLLFD &DWHQD ' QDP FD
Wh…vhivyvÃy‚phyv …v‡‚…‚ÃhyÃTP …vsÃph‡rhƇh‡vph …vsÃh…rhÃqh‡vÃty‚iÃ9G …vsÃhyÃp‚qvpr Q p‚qvprÀhv

/D FDWHQD GLQDPLFD UDSSUHVHQWD OD VWRULD GHOOH DWWLYD]LRQL GHOOH XQLWDC GL SURJUDPPD $WWLYD]LRQL 62 ! PDLQ ! 3 ! 4 ! 5

p‚qvprÃqvÃQ Wh…vhivyvÃy‚phyv ƒh…h€r‡…v …v‡‚…‚ÃhyÀhv …vsÃph‡rhƇh‡vph …vs h €hv 9G …vsÃhyÃp‚qvpr R …rp‚…qÃqvÃQ

&DWHQD VWDWLLFD &DWHQD VWDW FD /D FDWHQD VWDWLFD LQGLFD GRYH FHUFDUH LQ TXDOH DUHD L ULIHULPHQWL SHU OH YDULDELOL QRQ ORFDOL YDULDELOL HVWHUQH  œ ,Q & OH IXQ]LRQL QRQ SRVVRQR FRQWHQHUH GHILQL]LRQL GL DOWUH IXQ]LRQL à OD FDWHQD VWDWLFD ID VHPSUH ULIHULPHQWR DOO
DUHD GDWL JOREDOH FRQWHQHQWH DG HVHPSLR OH YDULDELOL HVWHUQH

Wh…vhivyvÃy‚phyv ƒh…h€r‡…v …v‡‚…‚ÃhÃÃQ …vsÃph‡rhƇh‡vph …vsÃhÃQÃ9G …vsÃhyÃp‚qvpr

…rp‚…qÃqvÃR

p‚qvprÃqvÃR

S

p‚qvprÃqvÃS Wh…vhivyvÃy‚phyv ƒh…h€r‡…v …v‡‚…‚ÃhÃR …vsÃph‡rhƇh‡vph …vsÃhÃRÃ9G …vsÃhyÃp‚qvpr …rp‚…qÃqvÃS

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

528

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

529

(VHPSLLR (VHPS R #include <stdio.h> void prova(int *a, int b, int n); main() { int c[3], d; c[0] = 100; c[1] = 15; c[2] = 20; d = 0; printf("Prima: %d,%d,%d,%d\n", c[0],c[1],c[2],d); prova(c,d,3); printf("Dopo: %d,%d,%d,%d\n", c[0],c[1],c[2],d); } void { int for b = } prova(int *a, int b, int n) i; (i = 1; i < n; i++) a[i] = b; a[0];

9LVLELOLWDCWHPSR GL YLWD GHOOH YDULDELOL
,Q & HVLVWH OD SRVVLELOLWDC GL DOWHUDUH OD YLVLELOLWDC HG LO WHPSR GL YLWD GHOOH YDULDELOL XWLOL]]DQGR L TXDOLILFDWRUL H[WHUQ VWDWLF œ H[WHUQ R HVWHUQH JOREDOL œ GLFKLDUD]LRQL ULIHULWH D YDULDELOL JOREDOL œ YLVLELOL D WXWWR LO SURJUDPPD DQFKH LQ ILOH GLYHUVL œ WHPSR GL YLWD SDUL DOOD GXUDWD GHO SURJUDPPD œ VWDWLF R VWDWLFKH œ YDULDELOL VWDWLFKH LQWHUQH DOOH IXQ]LRQL œ QRQ VRQR YLVLELOL DOO
HVWHUQR GL TXHVWH œ WHPSR GL YLWD SDUL DOOD GXUDWD GHO SURJUDPPD

,O ULVXOWDWR GHOO¶HVHFX]LRQH GL TXHVWR SURJUDPPD q 3ULPD 'RSR   G"

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

530

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

531

(VHPSLLR (VHPS R #include <stdio.h> int A; int f() { static int cont=0; cont++; return cont; } main() { printf("%d\n", f()); printf("%d\n", f()); } ÝO D YDULDELOH static int cont SHUVLVWH WUD XQD DWWLYD]LRQH GL I H OD VXFFHVVLYD OD SULPD SULQWI VWDPSD  OD VHFRQGD SULQWI VWDPSD 

(VHPSLLR (VHPS R ,Q & HC SRVVLELOH GLVWULEXLUH LO FRGLFH VRUJHQWH GL XQ SURJUDPPD VX SLXC ILOH )LOH $$$F H[WHUQ YRLG IXQ    LQW QFDOO   IXQ  ^ QFDOO  ` )LOH %%%F H[WHUQ IXQ   YRLG IXQ    H[WHUQ LQW QFDOO  YRLG IXQ  ^ IXQ  QFDOO  `

œ OD YDULDELOH QFDOO H OH IXQ]LRQL IXQ H IXQ VRQR YLVLELOL HG XWLOL]]DELOL LQ HQWUDPEL L ILOH

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

532

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

533

6WUXWWXUD GHOOD PHPRULD D WHPSR GL HVHFX]LRQH
ORZ PHPRU\  &2'( 6(*0(17 '$7$ 6(*0(17 +($3 Ä Â 67$&.  KLJK PHPRU\

$OORFD]LRQH
&OODVVH GLL PHPRULL]]D]LLRQH H[WHUQ & DVVH G PHPRU ]]D] RQH H[WHUQ œ DSSOLFDELOH D YDULDELOL H IXQ]LRQL œ GHIDXOW SHU YDULDELOL JOREDOL H IXQ]LRQL œ YLVLELOLWj JOREDOH YLVLELOH RYXQTXH GDO SXQWR GL GHILQL]LRQH R GLFKLDUD]LRQH LQ SRL œ YLVLELOH DQFKH DO GL IXRUL GHO ILOH FKH QH FRQWLHQH OD GHILQL]LRQH œ SHUPDQHQWH HVLVWH GDOO
LQL]LR GHOO
HVHFX]LRQH GHO SURJUDPPD VLQR DOOD VXD ILQH œ DOORFD]LRQH œ VX '$7$ 6(*0(17 SHU OH YDULDELOL œ VX &2'( 6(*0(17 IXQ]LRQL

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

534

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

535

&OODVVH GLL 0HPRULL]]D]LLRQH VWDWLLF & DVVH G 0HPRU ]]D] RQH VWDW F œ DSSOLFDELOH D YDULDELOL œ VWDWLFD  GHILQL]LRQH JOREDOH R ORFDOH œ QRQ DOWHUD OD YLVLELOLWj ÝJOREDOH QHO FDVR GL GHILQL]LRQH JOREDOH YLVLELOH RYXQTXH GDO SXQWR GL GHILQL]LRQH LQ SRL PD VROR DOO
LQWHUQR GHO ILOH FKH OD FRQWLHQH ÝORFDOH QHO FDVR GL GHILQL]LRQH ORFDOH YLVLELOH VROR DOO
LQWHUQR GHO EORFFR R GHOOD IXQ]LRQH LQ FXL q VWDWD GHILQLWD GDO SXQWR GL GHILQL]LRQH LQ SRL œ SHUPDQHQWH WHPSR GL YLWD SDUL DO WHPSR GL HVHFX]LRQH GHO SURJUDPPD œ DOORFD]LRQH VX '$7$ 6(*0(17 &RGH 6HJPHQW ‡

5LLDVVXPHQGR 5 DVVXPHQGR

OH IXQ]LRQL QHO VHJPHQWR FRGLFH

'DWD 6HJPHQW ‡ ‡ YDULDELOL H[WHUQ JOREDOL PXOWLILOH YDULDELOL VWDWLF JOREDOL VLQJOHILOH H ORFDOL

6WDFN ‡ YDULDELOL ORFDOL H SDUDPHWUL IXQ]LRQL

+HDS ‡ VWUXWWXUH GDWL DOORFDWH PDOORF H GHDOORFDWH IUHH HVSOLFLWDPHQWH GDOO
XWHQWH H UHIHUHQ]LDWH WUDPLWH SXQWDWRUL

(VLVWH XQ¶XOWHULRUH FODVVH GL PHPRUL]]D]LRQH UHJLVWHU FKH VH SRVVLELOH DOORFD YDULDELOL VX UHJLVWUL PDFFKLQD

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

536

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

537

/D ULFRUVLRQH
8QD IXQ]LRQH PDWHPDWLFD HC GHILQLWD ULFRUVLYDPHQWH TXDQGR QHOOD VXD GHILQL]LRQH FRPSDUH XQ ULIHULPHQWR D VH VWHVVD (VHPSLLR (VHPS R )XQ]LRQH IDWWRULDOH VX LQWHUL QRQ QHJDWLYL I Q Q

,QGX]LLRQH PDWHPDWLLFD ,QGX] RQH PDWHPDW FD LPPDJLQDQGR GL DYHUH [N FRVWUXLVFL [N œ ,QGXWWLYDPHQWH LO FDOFROR GHO IDWWRULDOH GL XQ QXPHUR Q YLHQH ULFRQGRWWR DO FDOFROR GHO IDWWRULDOH GL Q LQ FDOFROR GHO IDWWRULDOH GL Q D TXHOOR GL Q HWF ILQR D UDJJLXQJHUH XQ FDVR EDVH IDWWRULDOH GL   D ULVXOWDWR QRWR 0HWRGR SDUWLFRODUPHQWH XWLOH SHU DOFXQL SUREOHPL LQWULQVHFDPHQWH ULFRUVLYL R FKH ODYRUDQR VX VWUXWWXUH GDWL ULFRUVLYH OLVWH DOEHUL 

HC GHILQLWD ULFRUVLYDPHQWH FRPH VHJXH I Q  I Q Q I Q VH Q  VH Q! FDVR EDVH FDVR JHQHULFR

Ý8VDQGR LO PHWRGR LQGXWWLYR VL VSHFLILFD FRPH WDOH IXQ]LRQH VL FRPSRUWD QHO FDVR EDVH H QHO FDVR JHQHULFR

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

538

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

539

(VHPSLL GLL SUREOOHPLL ULLFRUVLLYLL (VHPS G SURE HP U FRUV Y  œ 6RPPD GHL SULPL Q QXPHUL QDWXUDOL VRPPD Q  QVRPPD Q VH Q  DOWULPHQWL

3URJUDPPD]LRQH ULFRUVLYD
0ROWL OLQJXDJJL GL SURJUDPPD]LRQH RIIURQR OD SRVVLELOLWDC GL GHILQLUH IXQ]LRQLSURFHGXUH ULFRUVLYH (VHPSLLR (VHPS R &DOFROR GHO IDWWRULDOH GL XQ QXPHUR LQ &  #include <stdio.h> int fattoriale(unsigned int n); main(void) {int n; printf("\nIntrodurre N:\t"); scanf("%d",&n); printf("\nFattoriale di %d:\t%d\n", n, fattoriale(n)); } int fattoriale(unsigned int n) { if (n==0) return 1; else return n*fattoriale(n-1); } œ 1RQ WXWWL L OLQJXDJJL GL DOWR OLYHOOR VXSSRUWDQR SURFHGXUH ULFRUVLYH DG HVHPSLR LO )2575$1 QRQ FRQVHQWH GL VFULYHUH VRWWRSURJUDPPL ULFRUVLYL 

œ *HQHUDUH O¶QHVLPR QXPHUR GL )LERQDFFL ILE Q   ILE Q ILE Q VH Q  VH Q  DOWULPHQWL

œ &DOFROR GHO PLQLPR GL XQD VHTXHQ]D GL HOHPHQWL >D D D @ >D_> D D @@ PLQ >D@ ! D PLQ >DD@ ! D VH DD DOWULPHQWL D PLQ >D_=@ ! PLQ >DPLQ = @ VYLOXSSDQGR PLQ >D_=@ VL RWWLHQH PLQ >DPLQ >D PLQ >D@ @ @

œ &DOFROR GHOOD OXQJKH]]D GL XQD VHTXHQ]D OXQJ >@ OXQJ >D_=@ !  !  OXQJ =

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

540

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

541

&DOFROR GHOOD VRPPD GHL SULPL 1 QDWXUDOL
#include <stdio.h> int sum(unsigned int n); main() { int n; printf("\nIntrodurre N:\t"); scanf("%d",&n); printf("\nSomma fino a %d:\t%d\n", n, sum(n)); } int sum(unsigned int n) { if (n==0) return 0; else return n+sum(n-1); /* ricorsione */ } 6RQR HVHPSL GL ULFRUVLRQH OLQHDUH XQD VROD FKLDPDWD ULFRUVLYD QHO FRUSR GHOOD IXQ]LRQH  ILE Q

1HVLPR QXPHUR GL )LERQDFFL 
 ILE Q ILE Q VH Q  VH Q  DOWULPHQWL

int fib(unsigned int n) { if (n==0) return 0; else if (n==1) return 1; else return fib(n-1)+fib(n-2); /* ricorsione non lineare*/ } (C XQ HVHPSLR GL ULFRUVLRQH QRQ OLQHDUH SLXC FKLDPDWH ULFRUVLYH QHO FRUSR GHOOD IXQ]LRQH SHU GHWHUPLQDUH LO YDORUH UHVWLWXLWR GDOOD IXQ]LRQH

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

542

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

543

(VHUFLL]LLR (VHUF ] R 3LOD GL DWWLYD]LRQL SHU OD FKLDPDWD main() { ... printf("Fattoriale di 2:%d\n", fattoriale(2)); } $OO¶LQ]LR GHOO
HVHFX]LRQH

œ 'RSR OD SULPD DWWLYD]LRQH GHOOD IXQ]LRQH IDWWRULDOH IDWWRULDOH  

AREA DATI GLOBALE

RECORD ATTIVAZIONE main

AREA DATI GLOBALE

RECORD ATTIVAZIONE fattoriale(2)

RECORD ATTIVAZIONE main

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

544

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

545

œ 'RSR OD VHFRQGD DWWLYD]LRQH IDWWRULDOH  

œ 'RSR OD WHU]D DWWLYD]LRQH IDWWRULDOH  

AREA DATI GLOBALE

AREA DATI GLOBALE

RECORD ATTIVAZIONE main

RECORD ATTIVAZIONE main

RECORD ATTIVAZIONE fattoriale(2)

RECORD ATTIVAZIONE fattoriale(2)

RECORD ATTIVAZIONE fattoriale(1)

RECORD ATTIVAZIONE fattoriale(1)

RECORD ATTIVAZIONE fattoriale(0)

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

546

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

547

œ 7HUPLQH GHOOD WHU]D DWWLYD]LRQH UHWXUQ 

œ 7HUPLQH GHOOD VHFRQGD DWWLYD]LRQH UHWXUQ 

AREA DATI GLOBALE

AREA DATI GLOBALE

RECORD ATTIVAZIONE main

RECORD ATTIVAZIONE main

RECORD ATTIVAZIONE fattoriale(2)

1

RECORD ATTIVAZIONE fattoriale(2)

1

RECORD ATTIVAZIONE fattoriale(1)

RECORD ATTIVAZIONE fattoriale(1)

RECORD ATTIVAZIONE fattoriale(0)

œ $O WHUPLQH GHOOD SULPD DWWLYD]LRQH GL IDWWRULDOH YLHQH UHVWLWXLWR DO PDLQ LO YDORUH  H TXHVWR VWDPSD LO ULVXOWDWR

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

548

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

549

5LFRUVLRQH HG LWHUD]LRQH
/D ULFRUVLRQH HC VHPSUH UHDOL]]DELOH PHGLDQWH LWHUD]LRQH $G HVHPSLLR $G HVHPS R 5HDOL]]D]LRQH LWHUDWLYD GHO IDWWRULDOH #include <stdio.h> int fatt_it(unsigned int n); main(void) { int n; printf("\nIntrodurre N:\t"); scanf("%d",&n); printf("\nFattoriale di %d:\t%d\n", n, fatt_it(n)); } int fatt_it(unsigned int n) { int naux, f; f = 1; for (naux=1; naux<=n; naux++) f *= naux; return f; }

5LFRUVLRQH H ,WHUD]LRQH
4XDQGR FRQYLLHQH XWLLOOLL]]DUH OOD ULLFRUVLLRQH" 4XDQGR FRQY HQH XW ]]DUH D U FRUV RQH" œ 6ROX]LRQL ULFRUVLYH VRQR VSHVVR SLXC YLFLQH DOOD GHILQL]LRQH PDWHPDWLFD GL FHUWH IXQ]LRQL œ 9HUVLRQL LWHUDWLYH VRQR JHQHUDOPHQWH SLXC HIILFLHQWL GL XQD VROX]LRQH ULFRUVLYD VLD LQ WHUPLQL GL PHPRULD FKH GL WHPSR GL HVHFX]LRQH 

(VHUFLL]LL (VHUF ]   6FULYHUH OD YHUVLRQH LWHUDWLYD GHOOD SURFHGXUD SHU LO FDOFROR GHOO¶QHVLPR QXPHUR GL )LERQDFFL  6FULYHUH XQD SURFHGXUD 3ULQW5HY FKH OHJJH LQ LQJUHVVR XQD VHTXHQ]D GL FDUDWWHUL WHUPLQDWD GD 

H VWDPSD OD VHTXHQ]D DO FRQWUDULR 520$ $025 'HILQLUQH XQD YHUVLRQH ULFRUVLYD VHQ]D XWLOL]]DUH LO WLSR VWULQJD

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

550

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

551

6ROOX]LLRQLL 6R X] RQ   QHVLPR QXPHUR GL )LERQDFFL YHUVLRQH LWHUDWLYD int fib_it(unsigned int n) { unsigned int i,x=1,y=0,z; for(i=1;i<=n;i++) { z = x; x += y; y=z;} return x; }  3ULQW5HY YHUVLRQH ULFRUVLYD #include <stdio.h> #include <string.h> void print_rev(char car); main(void) { printf("\nIntrodurre una sequenza terminata da .:\t"); print_rev(getchar()); } void print_rev(char car); { if (car != ’.’) { print_rev(getchar()); putchar(car);} else return; } Ý1HOOD YHUVLRQH ULFRUVLYD RJQL UHFRUG GL DWWLYD]LRQH QHOOR VWDFN PHPRUL]]D XQ VLQJROR FDUDWWHUH OHWWR SXVK  LQ IDVH GL SRS L FDUDWWHUL YHQJRQR VWDPSDWL QHOOD VHTXHQ]D LQYHUVD Ý3HU VFULYHUQH XQD YHUVLRQH LWHUDWLYD HC QHFHVVDULR PHPRUL]]DUH LQ XQD VWUXWWXUD GDWL YHWWRUH OD VWULQJD
FONDAMENTI DI INFORMATICA
IL LINGUAGGIO C - FUNZIONI 552 

3ULQW5HY YHUVLRQH LWHUDWLYD FRQ VWULQJD DO PD[  FDUDWWHUL  #include <stdio.h> #include <string.h> #define MAXLEN 30 void print_rev_it(char word[]) { int i; for (i=strlen(word)-1; i>=0; i--) putchar(word[i]); return; } main() { char parola[MAXLEN]; printf("\nIntrodurre una parola:\t"); scanf("%s",&parola); print_rev_it(parola); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

553

œ  3ULQW5HY YHUVLRQH ULFRUVLYD FRQ VWULQJD #include <stdio.h> #include <string.h> #define MAXLEN 30 void print_rev(char word[], int i) { if (strlen(word)-i>1) print_rev(word,i+1); putchar(word[i]); return; } main() { int n; char parola[MAXLEN]; printf("\nIntrodurre una parola:\t"); scanf("%s",&parola); print_rev(parola,0); }

5LFRUVLRQH 7DLO
4XDQGR OD FKLDPDWD ULFRUVLYD GL XQD IXQ]LRQHSURFHGXUD ) HC O¶XOWLPD LVWUX]LRQH GHO FRGLFH GL ) VL GLFH FKH ) HC WDLO ULFRUVLYD $G HVHPSLLR $G HVHPS R #include <stdio.h> int f (int x, int y); main() {int n,m; printf("\nIntrodurre due numeri:\t"); scanf("%d%d",&n,&m); printf("Somma di %d e %d:\t %d", n,m,f(n,m)); } int f (int x, int y) { if (x==0) return y; else if (x>0) return f(x-1,y+1); else return f(x+1,y-1); } LQ SUDWLFD I VRPPD [ DG \

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

554

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

555

5LFRUVLRQH 7DLO
/D FRPSXWD]LRQH FKH VL RULJLQD WUDPLWH O
LQYRFD]LRQH GL XQD IXQ]LRQH WDLOULFRUVLYD FRUULVSRQGH DG XQ SURFHVVR FRPSXWD]LRQDOH LWHUDWLYR œ 8Q SURFHVVR FRPSXWD]LRQDOH HC ULFRUVLYR TXDQGR HC FDUDWWHUL]]DWR GD XQD FDWHQD GL RSHUD]LRQL SRVWLFLSDWH  LO FXL ULVXOWDWR HC GLVSRQLELOH VROR GRSR FKH O
XOWLPR DQHOOR GHOOD FDWHQD VL HC FRQFOXVR œ ,Q XQ SURFHVVR FRPSXWD]LRQDOH LWHUDWLYR DG RJQL SDVVR HC GLVSRQLELOH XQD IUD]LRQH GHO ULVXOWDWR 1HOOOO

HVHPSLLR 1H HVHPS R +S n=2; m=3 ...printf(...,f(n,m))
5

AREA DATI GLOBALE

5

RECORD ATTIVAZIONE main

5

RECORD ATTIVAZIONE f(2,3)

RECORD ATTIVAZIONE f(1,4)

I  ! I  ! I  ! UHWXUQ  ,O ULVXOWDWR QRQ YLHQH ULHODERUDWR GDOOH DWWLYD]LRQL LQWHUPHGLH PD SDVVDWR VHPSOLFHPHQWH GD FLDVFXQD DO FKLDPDQWH ,O FRPSLODWRUH SHU RWWLPL]]DUH O
RFFXSD]LRQH GHOOR VWDFN SRWUHEEH XWLOL]]DUH LO PHGHVLPR UHFRUG GL DWWLYD]LRQH SHU WXWWH OH DWWLYD]LRQL VXFFHVVLYH GHOOD IXQ]LRQH WDLO ULFRUVLYD Ý&RQVHQWH GL RWWLPL]]DUH OR VSD]LR GL PHPRULD DOORFDWR VXOOR VWDFN

RECORD ATTIVAZIONE f(0,5)

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

556

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

557

(VHPSLLR (VHPS R 9HUVLRQH WDLOULFRUVLYD GHO IDWWRULDOH 9HUVLRQH ULFRUVLYD int fattoriale(unsigned int n) { if (n==0) return 1; else if (n==1) return 1 else return n*fattoriale(n-1); /* ricorsione */ } 9HUVLRQH WDLOULFRUVLYD int fattoriale(unsigned int n) { return fatt_tail(1,n,1);} int fatt_tail(unsigned int i, unsigned int n, long int f) { if (i<=n) return fatt_tail(++i,n,f*i); else return f; }

(VHUFLL]LLR (VHUF ] R 6FULYHUH XQD YHUVLRQH ULFRUVLYD GHOO
DOJRULWPR FKH FDOFROD LO SURGRWWR FRPH VHTXHQ]D GL VRPPH 6ROOX]LLRQH 6R X] RQH #include <stdio.h> #include <stdlib.h> int prodotto(int X, int Y); main() { int X,Y; printf("Dammi X ed Y: "); scanf("%d%d", &X, &Y); printf("Prodotto(%d, %d)=%d\n",X, Y, prodotto(X,Y)); } int prodotto(int X, int Y) { if(Y==0) return 0; else return X+prodotto(X,Y-1); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

558

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

559

(VHUFLL]LLR (VHUF ] R 6FULYHUH XQD YHUVLRQH ULFRUVLYD GHOO
DOJRULWPR RUGLQDPHQWR GL XQ YHWWRUH SHU PDVVLPL VXFFHVVLYL 6ROOX]LLRQH 6R X] RQH #include <stdio.h> #include <stdlib.h> void ordina(int *V, int N); main() { int n, *V, i, dim; printf("Quanti valori?"); scanf("%d", &dim); V=(int *)malloc(dim*sizeof(int)); /* lettura dei dati */ for (i=0; i<dim; i++) {printf("valore n. %d: ",i); scanf("%d", &V[i]); } ordina(V,dim); /*stampa dei risultati */ for(i=0; i<dim; i++) printf("Valore di V[%d]=%d\n",i,V[i]); free(V); } GL

void ordina(int *V, int N) {int j, max, tmp; if (N==1) return; else { for( max=N-1,j=0; j<N; j++) if (V[j]>V[max]) max=j; if (max<N-1) { /*scambio */ tmp=V[N-1]; V[N-1]=V[max]; V[max]=tmp; } } ordina(V, N-1); /*scansione sottovettore */ }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

560

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

561

)XQ]LRQL FRPH SDUDPHWUL
)XQ]LLRQLL )XQ] RQ  9LLQFROOR QHOOOO

XVR GHLL SDUDPHWULL 9 QFR R QH XVR GH SDUDPHWU  QRQ HC SRVVLELOH SDVVDUH D IXQ]LRQL H SURFHGXUH SDUDPHWUL GL WLSR IXQ]LRQH 9LLQFROOR VXOO ULLVXOOWDWR 9 QFR R VX U VX WDWR OH IXQ]LRQL QRQ SRVVRQR ULWRUQDUH IXQ]LRQL FRPH ULVXOWDWR (VHPSLLR (VHPS R

3XQWDWRUH D )XQ]LRQH

double (* f) (double par); 3ULLRULLWDC ULLVSHWWR D  3U RU WDC U VSHWWR D  ÝI SXQWD DOOD IXQ]LRQH FRQ XQ SDUDPHWUR GL LQJUHVVR GRXEOH H UHVWLWXLVFH XQ GRXEOH ,O & FRQVLGHUD O
LGHQWLILFDWRUH GL XQD IXQ]LRQH FRPH XQ SXQWDWRUH DO VXR FRGLFH

PD
œ HC SRVVLELOH SDVVDUH D IXQ]LRQL SDUDPHWUL GL WLSR SXQWDWRUH D IXQ]LRQH œ HC SRVVLELOH RWWHQHUH FRPH ULVXOWDWR GL XQD IXQ]LRQH XQ SXQWDWRUH D IXQ]LRQH 3XQWDWRUH D IXQ]LLRQH 3XQWDWRUH D IXQ] RQH WLSRBI! QRPH)! OLVWDBSDUBIRUPDOL!  GRYH œ QRPH)! HC O
LGHQWLILFDWRUH GHO SXQWDWRUH D IXQ]LRQH œ WLSRBI! HC LO WLSR GL GDWR UHVWLWXLWR GDOOD IXQ]LRQH SXQWDWD œ OLVWDBSDUBIRUPDOL! HC OD OLVWD GHL SDUDPHWUL IRUPDOL GHOOD IXQ]LRQH SXQWDWD

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

562

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

563

)XQ]LRQL FRPH SDUDPHWUL
3HU DYHUH XQ SDUDPHWUR ) GL WLSR SXQWDWRUH D IXQ]LRQH RFFRUUH VSHFLILFDUH XQ SDUDPHWUR IRUPDOH GL WLSR IXQ]LRQH FRPH VHJXH
WLSRÃ
A Ã

$OFXQL (VHPSL
'HILLQLL]LLRQHGLLFKLLDUD]LLRQH GLL IXQ]LLRQLL 'HI Q ] RQH G FK DUD] RQH G IXQ] RQ  double fun (double x);

WLSRÃÃÃA!yv†‡hƒh…s‚…€hyvA!
ÃÃÃÃÃÃhy‡…vƒh…s‚…€hyvA 

œ OD IXQ]LRQH ) KD LO SXQWDWRUH D IXQ]LRQH ) FRPH SDUDPHWUR IRUPDOH œ 1HO FRUSR GL ) HC SRVVLELOH FKLDPDUH OD IXQ]LRQH SXQWDWD GD ) PHGLDQWH GHUHIHUHQFLQJ
tipo1 F1(tipo2 (*F2)(lista-par-formali-F2), altri-par-formali-F1) { tipo2 k; ... k=*F2(...); /*chiamata mediante dereferencing*/ }

double sommaquadratif (double (* f)(double par), int m, int n) {...} 8VR GHOO SDUDPHWUR 8VR GH SDUDPHWUR somma = somma + (* f)(k) * (* f)(k); ,QYRFD]LLRQH ,QYRFD] RQH sommaquadratif (fun, 1, 10000);

œ 8Q SDUDPHWUR IRUPDOH IXQ]LRQH HC VSHFLILFDWR LQGLFDQGR XQ QRPH SXQWDWRUH  OD OLVWD GHL SDUDPHWUL IRUPDOL HG LO WLSR GL ULVXOWDWR œ ,O SDUDPHWUR DWWXDOH FRQ FXL VL LQYRFD ) GHYH HVVHUH XQ LGHQWLILFDWRUH GL IXQ]LRQH FRQ OD VWHVVD GHVFUL]LRQH GHL SDUDPHWUL H OR VWHVVR WLSR GL ULVXOWDWR GL )

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

564

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

565

(VHPSLLR (VHPS R #include <math.h> double fun(double x) /*reciproco */ { return 1.0 / x;} double sommaquadratif (double (* f)(double par), int m, int n) {int k; double somma; somma = 0; for (k=m; k <= n; k++) somma = somma + (*f)(k)*(*f)(k); return somma; } double sommacubif (double (*f) (double par), int m, int n) { int k; double cubo; cubo= 0; for (k=m; k <= n; k++) cubo=cubo+(*f)(k)*(*f)(k)*(*f)(k); return cubo; } main () {int a, b, c, i; printf (" Inversi %.7f\n", sommaquadratif (fun,1, 10000)); printf (" Seni %.7f\n", sommaquadratif (sin, 2, 13)); printf (" Inversi %.7f\n", sommacubif (fun, 1, 10000)); printf (" Seni %.7f\n", sommacubif (sin, 2, 13)); }
FONDAMENTI DI INFORMATICA
IL LINGUAGGIO C - FUNZIONI 566

(VHUFLL]LLR (VHUF ] R 6FULYHUH XQ SURJUDPPD & FKH UDSSUHVHQWD XQD IXQ]LRQH 6LJPD 6LJPD GDWL LQ LQJUHVVR XQ SDUDPHWUR IXQ]LRQH ) D YDORUH LQWHUR H GXH HVWUHPL LQWHUL ,QIHULRUH 6XSHULRUH FDOFROD OD VRPPDWRULD

S S S S S 

v2Dsr…v‚…rÃTˆƒr…v‚…rAv

6FULYHUH XQ SURJUDPPD FKH XWLOL]]DQGR TXHVWD IXQ]LRQH FDOFROL L L L

L 

L 

L 

5HQGHUH ³SDUDPHWULFR´ LO SURJUDPPD QHL YDORUL LQI VXS Q SHU FDOFRODUH LQ

L LQIVXS

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

567

6ROOX]LLRQH 6R X] RQH #include <stdio.h> int sommatoria (int (* f)(int par, int N), int inf, int sup, int n) {int k; double somma; somma = 0; for (k=inf; k <= sup; k++) somma = somma + (*f)(k, n); return somma; } int power(int base, int exp) { int i, pow=1; for(i=1; i<=exp; i++) pow*=base; return pow; } main () { int INF,SUP,N, RIS; printf("Inserire INF e SUP:"); scanf("%d%d", &INF, &SUP); printf("Esponente?"); scanf("%d", &N); RIS=sommatoria(power,INF,SUP,N); printf("\nRisultato: %d", RIS); }

'LFKLDUD]LRQH GL XQ WLSR IXQ]LRQH
(C SRVVLELOH GLFKLDUDUH XQ WLSR IXQ]LRQH GD XWLOL]]DUH SRL SHU OD GLFKLUD]LRQH GL YDULDELOL R FRPH LGHQWLILFDWRUH GL WLSR GL XQ SDUDPHWUR IRUPDOH R ULVXOWDWR GL XQ DOWUD IXQ]LRQH 6L GHILQLVFRQR LQ SUDWLFD FRPH YDULDELOL GL WLSR SXQWDWRUH D IXQ]LRQH W\SHGHI LQW ISURW LQW D LQW E   WLSR IXQ]LRQH  W\SHGHI ISURW IXQSWU  WLSR SXQW D IXQ]LRQH  $G HVHPSLR typedef double (* ptrfunzione) (double); ptrfunzione v1ptr; œ 9DULDELOL GL TXHVWR DVVHJQDPHQWL v1ptr = &sin; TXHVWR DVVHJQDPHQWR QRQ LPSOLFD DOFXQD YDOXWD]LRQH GHOOD IXQ]LRQH WLSR SRVVRQR HVVHUH XVDWH LQ

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

568

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

569

)XQ]LRQL FRPH ULVXOWDWL GL IXQ]LRQL
8QD IXQ]LRQH SXRC UHVWLWXLUH XQ SXQWDWRUH D IXQ]LRQH (VHPSLLR (VHPS R typedef int fprot1 (int a, int b); typedef fprot1 *funptr; fprot1 *select1 (int a) ... /D IXQ]LRQH VHOHFW ULWRUQD XQ SXQWDWRUH D IXQ]LRQH GHO WLSR D GXH SDUDPHWUL LQWHUL H XQ LQWHUR GL ULWRUQR ,Q DOOWHUQDWLLYD ,Q D WHUQDW YD IXQSWU VHOHFW LQW D  RSSXUH LQW VHOHFW LQW D LQW D LQW E 

(VHPSLR #include <stdio.h> typedef int fprot1 (int a, int b); typedef fprot1 *funptr; int selecta (int a, int b) { return a + b; } int selectb (int a, int b) { return a - b; } int selectc (int a, int b) { return a * b; } funptr select2 (int a) { switch (a) { case 1: return selecta; break; case 2: return selectb; break; case 3: return selectc; break; default: return selecta; } } main() { int x, y, scelta; funptr F; scanf("%d%dScelta:%d",&x,&y,&scelta); F=select2(scelta); printf("Risultato:%d\n",(*F)(x,y)); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

570

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

571

$UJRPHQWL GHOOH OLQHH GL FRPDQGR DUJF DUJY
$QFKH OD IXQ]LRQH PDLQ SXRC DYHUH SDUDPHWUL , SDUDPHWUL UDSSUHVHQWDQR JOL HYHQWXDOL DUJRPHQWL SDVVDWL DO SURJUDPPD TXDQGR YLHQH PHVVR LQ HVHFX]LRQH
ƒ…‚t h…t h…t!  h…tI

$G HVHPSLLR VH OO

HVHFX]LLRQH HC GHWHUPLLQDWD GD XQ FRPDQGR $G HVHPS R VH HVHFX] RQH HC GHWHUP QDWD GD XQ FRPDQGR GHOO WLLSR GH W SR
ƒ…‚t h…t h…t!  h…tI

, SDUDPHWUL IRUPDOL GL PDLQ GLIIHUHQWHPHQWH GDOOH DOWUH IXQ]LRQL VRQR VHPSUH GXH œ DUJF œ DUJY LLQW DUJF QW DUJF HC XQ SDUDPHWUR GL WLSR LQWHUR 5DSSUHVHQWD LO QXPHUR GHJOL DUJRPHQWL HIIHWWLYDPHQWH SDVVDWL DO SURJUDPPD QHOOD OLQHH GL FRPDQGR FRQ FXL VL LQYRFD OD VXD HVHFX]LRQH $QFKH LO QRPH VWHVVR GHO SURJUDPPD QHOO
HVHPSLR SURJ HC FRQVLGHUDWR XQ DUJRPHQWR TXLQGL DUJF YDOH VHPSUH DOPHQR  FKDU DUJY FKDU DUJY YHWWRUH GL VWULQJKH FLDVFXQD GHOOH TXDOL FRQWLHQH XQ GLYHUVR DUJRPHQWR *OL DUJRPHQWL VRQR PHPRUL]]DWL QHO YHWWRUH QHOO
RUGLQH FRQ FXL VRQR GDWL GDOO
XWHQWH Ý3HU FRQYHQ]LRQH DUJY>@ FRQWLHQH LO QRPH GHO SURJUDPPD VWHVVR FLRHC LO QRPH GHO ILOH HVHJXLELOH 

$OORUD œ DUJF YDOH 1 œ DUJY ULVXOWD DUJY>@ DUJY>@ DUJY>@  DUJY>1@

SURJ DUJ DUJ DUJ1

Ý 3HU FRQYHQ]LRQH DUJY>DUJF@ YDOH 18//

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

572

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

573

(VHPSLLR (VHPS R 3URJUDPPD FKH VWDPSD L VXRL DUJRPHQWL #include <stdio.h> /* programma esempio.exe */ main(int argc, char *argv[]) { int i; for(i=0; i<argc; i++) printf("%s%s",argv[i], (i<argc-1)?"\t":"\n"); return 0; } ,QYRFD]LLRQH ,QYRFD] RQH HVHPSLR D E F ]HWD 6WDPSD 6WDPSD HVHPSLR D B B E F ]HWD

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

574

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - FUNZIONI

575

, )LOH
,O ILOH HC O
XQLWDC ORJLFD GL PHPRUL]]D]LRQH GHL GDWL VX PHPRULD GL PDVVD œ &RQVHQWH XQD PHPRUL]]D]LRQH SHUVLVWHQWH GHL GDWL QRQ OLPLWDWD GDOOH GLPHQVLRQL GHOOD PHPRULD FHQWUDOH œ *HQHUDOPHQWH XQ ILOH HC XQD VHTXHQ]D GL FRPSRQHQWL RPRJHQHH UHFRUG ORJLFL  œ , ILOH VRQR JHVWLWL GDO 6LVWHPD 2SHUDWLYR 3HU RJQL YHUVLRQH & HVLVWRQR IXQ]LRQL SHU LO WUDWWDPHQWR GHL ILOH 6WDQGDUG /LEUDU\ FKH WHQJRQR FRQWR GHOOH IXQ]LRQDOLWDC GHO 62 RVSLWH ,Q & LL ILLOOH YHQJRQR GLLVWLLQWLL LLQ GXH FDWHJRULLH ,Q & I H YHQJRQR G VW QW Q GXH FDWHJRU H œ ILOH GL WHVWR WUDWWDWL FRPH VHTXHQ]H GL FDUDWWHUL RUJDQL]]DWL LQ OLQHH FLDVFXQD WHUPLQDWD GD
?Q

)LOH GL WHVWR
6RQR ILOH GL FDUDWWHUL RUJDQL]]DWL LQ OLQHH 2JQL OLQHD HC WHUPLQDWD GD XQD PDUFD GL ILQH OLQHD QHZOLQH FDUDWWHUH
?Q 
Egregio Sig. Rossi, con la presente le rendo noto di aver provveduto ...

Ý ,O UHFRUG ORJLFR SXRC HVVHUH LO VLQJROR FDUDWWHUH RSSXUH OD VLQJROD OLQHD

œ ILOH ELQDUL YLVWL FRPH VHTXHQ]H GL ELW

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 575

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 576

*HVWLRQH GL ILOH LQ &
, ILOH KDQQR XQD VWUXWWXUD VHTXHQ]LDOH œ L UHFRUG ORJLFL VRQR RUJDQL]]DWL LQ XQD VHTXHQ]D œ SHU DFFHGHUH DG XQ SDUWLFRODUH UHFRUG ORJLFR HC QHFHVVDULR VFRUUHUH WXWWL TXHOOL FKH OR SUHFHGRQR ; «

*HVWLRQH GL ILOH LQ &
$SHUWXUD GLL XQ ILLOOH $SHUWXUD G XQ I H 3ULPD GL DFFHGHUH DG XQ ILOH HC QHFHVVDULR DSULUOR O¶RSHUD]LRQH GL DSHUWXUD FRPSLH OH D]LRQL SUHOLPLQDUL QHFHVVDULH DIILQFKHC LO ILOH SRVVD HVVHUH DFFHGXWR LQ OHWWXUD R LQ VFULWWXUD  /
RSHUD]LRQH GL DSHUWXUD LQL]LDOL]]D LO SXQWDWRUH DO ILOH $FFHVVR DG XQ ILLOOH $FFHVVR DG XQ I H 8QD YROWD DSHUWR LO ILOH HC SRVVLELOH OHJJHUHVFULYHUH LO ILOH ULIHUHQGROR PHGLDQWH LO SXQWDWRUH D ILOH &KLLXVXUD GLL XQ ILLOOH &K XVXUD G XQ I H $OOD ILQH GL XQD VHVVLRQH GL DFFHVVR OHWWXUD R VFULWWXUD DG XQ ILOH HC QHFHVVDULR FKLXGHUH LO ILOH SHU PHPRUL]]DUH SHUPDQHQWHPHQWH LO VXR FRQWHQXWR LQ PHPRULD GL PDVVD

3HU DFFHGHUH DG XQ ILOH GD XQ SURJUDPPD & HC QHFHVVDULR SUHGLVSRUUH XQD YDULDELOH FKH OR UDSSUHVHQWL SXQWDWRUH D ILOH 3XQWDWRUH D ILLOOH 3XQWDWRUH D I H HC XQD YDULDELOH FKH YLHQH XWLOL]]DWD SHU ULIHULUH XQ ILOH QHOOH RSHUD]LRQL GL DFFHVVR OHWWXUD H VFULWWXUD  ,PSOLFLWDPHQWH HVVD LQGLFD ΠLO ILOH ΠO
HOHPHQWR FRUUHQWH DOO
LQWHUQR GHOOD VHTXHQ]D $G HVHPSLLR $G HVHPS R FILE *fp;
Ý LO WLSR ),/( HC XQ WLSR QRQ SULPLWLYR GLFKLDUDWR QHO ILOH VWGLRK

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 577

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 578

$SHUWXUD GL XQ )LOH
),/( IRSHQ FKDU QDPH FKDU PRGH 

$SHUWXUD LQ OHWWXUD
IS IRSHQ ILOHQDPH  U

dove: œ QDPH HC XQ DUUD\ GL FDUDWWHUL FKH UDSSUHVHQWD LO QRPH DVVROXWR R UHODWLYR GHO ILOH QHO ILOH V\VWHP œ PRGH HVSULPH OD PRGDOLWDC GL DFFHVVR VFHOWD U  LQ OHWWXUD UHDG Z  LQ VFULWWXUD ZULWH D  VFULWWXUD DJJLXQWD LQ IRQGR DSSHQG E  D ILDQFR DG XQD GHOOH SUHFHGHQWL LQGLFD FKH LO ILOH HC ELQDULR W  D ILDQFR DG XQD GHOOH SUHFHGHQWL LQGLFD FKH LO ILOH HCGL WHVWR œ 6H HVHJXLWD FRQ VXFFHVVR O
RSHUD]LRQH GL DSHUWXUD ULWRUQD FRPH ULVXOWDWR XQ SXQWDWRUH DO ILOH DSHUWR œ 6H LQYHFH O
DSHUWXUD IDOOLVFH DG HVHPSLR SHUFKHC LO ILOH QRQ HVLVWH  IRSHQ UHVWLWXLVFH LO YDORUH 18//

6H LLOO ILLOOH QRQ HC YXRWR 6H I H QRQ HC YXRWR

fp

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 579

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 580

$SHUWXUD LQ VFULWWXUD
IS IRSHQ ILOHQDPH  Z

$SHUWXUD LQ DJJLXQWD DSSHQG
IS IRSHQ ILOHQDPH  D

$QFKH VH LLOO ILLOOH QRQ HC YXRWR $QFKH VH I H QRQ HC YXRWR

fp

fp ,O SXQWDWRUH DO ILOH VL SRVL]LRQD VXOO¶HOHPHQWR VXFFHVVLYR DOO¶XOWLPR VLJQLILFDWLYR GHO ILOH Å VH LO ILOH HVLVWHYD JLDC LO VXR FRQWHQXWR QRQ YLHQH SHUVR

œ

6H LO ILOH HVLVWHYD JLDC LO VXR FRQWHQXWR YLHQH SHUVR

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 581

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 582

$G HVHPSLLR $G HVHPS R File *fp; fp=fopen("c:\anna\dati", "r"); <uso del file> œ IS UDSSUHVHQWD GDOO
DSHUWXUD LQ SRL LO ULIHULPHQWR GD XWLOL]]DUH QHOOH RSHUD]LRQL GL DFFHVVR D F?DQQD?GDWL (VVR LQGLYLGXD LQ SDUWLFRODUH ‡ ‡ LO ILOH O¶HOHPHQWR FRUUHQWH DOO¶LQWHUQR GHO ILOH

&KLXVXUD GL XQ )LOH
$O WHUPLQH GL XQD VHVVLRQH GL DFFHVVR DO ILOH HVVR GHYH HVVHUH FKLXVR /
RSHUD]LRQH GL FKLXVXUD VL UHDOL]]D FRQ OD IXQ]LRQH IFORVH LQW IFORVH ),/( IS  GRYH ‡ IS UDSSUHVHQWD LO SXQWDWRUH DO ILOH GD FKLXGHUH IFOORVH ULLWRUQD FRPH ULLVXOOWDWR XQ LLQWHUR IF RVH U WRUQD FRPH U VX WDWR XQ QWHUR œ 6H O
RSHUD]LRQH GL FKLXVXUD HC HVHJXLWD FRUUHWWDPHQWH UHVWLWXLVFH LO YDORUH  œ VH OD FKLXVXUD QRQ HC DQGDWD D EXRQ ILQH ULWRUQD OD FRVWDQWH (2)

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 583

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 584

(VHPSLLR (VHPS R #include <stdio.h> main() { FILE *fp; fp = fopen("prova.dat", "w") <scrittura di prova.dat> fclose(fp); return 0; }

)LOH VWDQGDUG GL ,2
(VLVWRQR WUH ILOH WHVWR FKH VRQR DSHUWL DXWRPDWLFDPHQWH DOO
LQL]LR GL RJQL HVHFX]LRQH ‡ VWGLQ VWDQGDUG LQSXW WDVWLHUD  DSHUWR LQ OHWWXUD ‡ VWGRXW VWDQGDUG RXWSXW YLGHR  DSHUWR LQ VFULWWXUD ‡ VWGHUU VWDQGDUG HUURU YLGHR  DSHUWR LQ VFULWWXUD Ý VWGLQ VWGRXW VWGHUU VRQR YDULDELOL GL WLSR SXQWDWRUH D ILOH DXWRPDWLFDPHQWH HG LPSOLFLWDPHQWH GHILQLWH Å QRQ YDQQR GHILQLWH

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 585

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 586

)XQ]LRQH IHRI
'XUDQWH OD IDVH GL DFFHVVR DG XQ ILOH HC SRVVLELOH YHULILFDUH OD SUHVHQ]D GHOOD PDUFD GL ILQH ILOH FRQ OD IXQ]LRQH GL OLEUHULD LQW IHRI ),/( IS  œ IHRI IS FRQWUROOD VH HC VWDWD UDJJLXQWD OD ILQH GHO ILOH IS QHOOD RSHUD]LRQH GL OHWWXUD R VFULWWXUD SUHFHGHQWH 5HVWLWXLVFH LO YDORUH  IDOVR ORJLFR VH QRQ HC VWDWD UDJJLXQWD OD ILQH GHO ILOH DOWULPHQWL XQ YDORUH GLYHUVR GD ]HUR YHUR ORJLFR 

/HWWXUD H 6FULWWXUD GL ILOH
8QD YROWD DSHUWR XQ ILOH VX GL HVVR VL SXRC DFFHGHUH LQ OHWWXUD HR VFULWWXUD FRPSDWLELOPHQWH FRQ TXDQWR VSHFLILFDWR LQ IDVH GL DSHUWXUD 3HU ILLOOH GLL WHVWR VRQR GLLVSRQLLELLOOLL IXQ]LLRQLL GLL 3HU I H G WHVWR VRQR G VSRQ E IXQ] RQ G  œ /HWWXUDVFULWWXUD FRQ IRUPDWR œ /HWWXUDVFULWWXUD GL FDUDWWHUL œ /HWWXUDVFULWWXUD GL VWULQJKH GL FDUDWWHUL

3HU ILLOOH ELLQDULL VLL XWLLOOLL]]DQR IXQ]LLRQLL GLL 3HU I H E QDU V XW ]]DQR IXQ] RQ G  œ /HWWXUDVFULWWXUD GL EORFFKL

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 587

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 588

$FFHVVR D ILOH GL WHVWR /HWWXUDVFULWWXUD FRQ IRUPDWR
)XQ]LRQL VLPLOL D VFDQI H SULQWI PD FRQ XQ SDUDPHWUR DJJLXQWLYR UDSSUHVHQWDQWH LO SXQWDWRUH DO ILOH GL WHVWR VXO TXDOH VL YXROH OHJJHUH R VFULYHUH /HWWXUD FRQ IRUPDWR /HWWXUD FRQ IRUPDWR 6L XVD OD IXQ]LRQH IVFDQI LQW IVFDQI ),/( IS VWULQJDFRQWUROOR LQGHOHP  GRYH œ IS HC LO SXQWDWRUH DO ILOH œ VWULQJDFRQWUROOR LQGLFD LO IRUPDWR GHL GDWL GD OHJJHUH œ LQGHOHP HC OD OLVWD GHJOL LQGLUL]]L GHOOH YDULDELOL D FXL DVVHJQDUH L YDORUL OHWWL (VHPSLLR (VHPS R FILE *fp; int A; char B; float C; fp=fopen("dati.txt", "r"); fscanf(fp, "%d%c%f", &A, &B, &C); ... fclose(fp); 5HVWLWXLVFH LO QXPHUR GL HOHPHQWL OHWWL RSSXUH XQ YDORUH QHJDWLYR LQ FDVR GL HUURUH

6FULLWWXUD FRQ IRUPDWR 6FU WWXUD FRQ IRUPDWR 6L XVD OD IXQ]LRQH ISULQWI LQW ISULQWI ),/( IS VWULQJDFRQWUROOR HOHPHQWL  GRYH œ IS HC LO SXQWDWRUH DO ILOH œ VWULQJDFRQWUROOR LQGLFD LO IRUPDWR GHL GDWL GD VFULYHUH œ HOHPHQWL HC OD OLVWD GHL YDORUL HVSUHVVLRQL GD VFULYHUH (VHPSLLR (VHPS R FILE *fp; float C=0.27; fp=fopen("risultati.txt", "w"); fscanf(fp,"Risultato: %f", c*3.14); ... fclose(fp); 5HVWLWXLVFH LO QXPHUR GL HOHPHQWL VFULWWL RSSXUH XQ YDORUH QHJDWLYR LQ FDVR GL HUURUH SULLQWIVFDQI H ISULLQWIIVFDQI SU QWI VFDQI H ISU QWI IVFDQI œ 1RWLDPR FKH
SULQWI VWULQJDFRQWUROOR HOHPHQWL VFDQI VWULQJDFRQWUROOR LQGHOHPHQWL 

HTXLYDOJRQR D
ISULQWI VWRXW VWULQJDFRQWUROOR HOHPHQWL  IVFDQI VWGLQ VWULQJDFRQWUROOR LQGHOHPHQWL 

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 589

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 590

(VHPSLLR (VHPS R 9LVXDOL]]D]LRQH GHO FRQWHQXWR GL XQ ILOH GL WHVWR #include <stdio.h> main() { char buf[80] FILE *fp; fp=fopen("testo.txt", "r"); fscanf(fp,"%s",buf); while(!feof(fp)) { printf("%s", buf); fscanf(fp,"%s",buf); } fclose(fp); } RSSXUH #include <stdio.h> main() { char buf[80] FILE *fp; fp=fopen("testo.txt", "r"); while (fscanf(fp,"%s",buf)>0) printf("%s", buf); fclose(fp); }

/HWWXUDVFULWWXUD GL FDUDWWHUL
)XQ]LRQL VLPLOL D JHWFKDU H SXWFKDU PD FRQ XQ SDUDPHWUR DJJLXQWLYR UDSSUHVHQWDQWH LO SXQWDWRUH DO ILOH GL WHVWR VXO TXDOH VL YXROH OHJJHUH R VFULYHUH LQW LQW LQW LQW JHWF ),/( IS  SXWF LQW F ),/( IS  IJHWF ),/( IS  ISXWF LQW F ),/( IS 

Ý ,Q FDVR GL HVHFX]LRQH FRUUHWWD UHVWLWXLVFRQR LO FDUDWWHUH OHWWR R VFULWWR FRPH LQWHUR DOWULPHQWL (2)

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 591

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 592

(VHPSLLR (VHPS R 3URJUDPPD FKH FRQFDWHQD L ILOH GDWL FRPH DUJRPHQWR LQ XQ XQLFR ILOH VWGRXW  #include <stdio.h> main(int argc, char **argv) { FILE *fp; void filecopy(FILE *, FILE *); if (argc==1) filecopy(stdin, stdout); else while (--argc>0) if ((fp=fopen(*++argv, "r"))==NULL) { printf("\nImpossibile aprire il file %s\n", *argv); exit(1); } else {filecopy(fp, stdout); fclose(fp); } return 0; } void filecopy(FILE *inputFile, FILE *outputFile) {int c; while((c=getc(inputFile))!=EOF) putc(c, outputFile); }

1RWH VXOOOO

HVHPSLLR 1RWH VX HVHPS R œ VH QRQ FL VRQR DUJRPHQWL DUJF   LO SURJUDPPD FRSLD OR VWDQGDUG LQSXW QHOOR VWDQGDUG RXWSXW œ OD IXQ]LRQH filecopy HIIHWWXD OD FRSLD GHO ILOH FDUDWWHUH SHU FDUDWWHUH œ VH XQR GHL ILOH LQGLFDWL FRPH DUJRPHQWR QRQ HVLVWH OD IXQ]LRQH fopen IDOOLVFH UHVWLWXHQGR LO YDORUH 18// ,Q TXHVWR FDVR LO SURJUDPPD WHUPLQD exit UHVWLWXHQGR LO YDORUH  H VWDPSDQGR XQ PHVVDJJLR GL HUURUH œ VDUHEEH PHJOLR VFULYHUH L PHVVDJJL GL HUURUH VXOOR VWDQGDUG HUURU ULGLUH]LRQH  printf(stderr,"\nImpossibile aprire il file %s\n", *argv); 3HU QRQ SHUGHUH GDWL OD IXQ]LRQH DXWRPDWLFDPHQWH RJQL ILOH DSHUWR exit FKLXGH

œ LO FLFOR while((c=getc(inputFile))!=EOF) putc(c, outputFile); SRWHYD HVVHUH VFULWWR DQFKH FRPH c=getc(inputFile); while(!feof(inputFile)) { putc(c,outputFile); c=getc(inputFile); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 593

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 594

/HWWXUDVFULWWXUD GL VWULQJKH
)XQ]LRQL VLPLOL D JHWV H SXWV FKDU IJHWV FKDU V LQW Q ),/( IS  7UDVIHULVFH QHOOD VWULQJD V L FDUDWWHUL OHWWL GDO ILOH SXQWDWR GD IS ILQR D TXDQGR KD OHWWR Q FDUDWWHUL RSSXUH KD LQFRQWUDWR XQ QHZOLQH RSSXUH OD ILQH GHO ILOH /D IJHWV PDQWLHQH LO QHZOLQH QHOOD VWULQJD V 5HVWLWXLVFH OD VWULQJD OHWWD LQ FDVR GL FRUUHWWD WHUPLQD]LRQH
?
LQ FDVR GL HUURUH R ILQH GHO ILOH LQW ISXWV FKDU V ),/( IS 

$FFHVVR D ILOH ELQDUL /HWWXUDVFULWWXUD GL EORFFKL
6L SXRC OHJJHUH R VFULYHUH GD XQ ILOH ELQDULR XQ LQWHUR EORFFR GL GDWL ELQDUL  8Q ILOH ELQDULR PHPRUL]]D GDWL GL TXDOXQTXH WLSR LQ SDUWLFRODUH GDWL FKH QRQ VRQR FDUDWWHUL LQWHUL UHDOL YHWWRUL R VWUXWWXUH  3HU OD OHWWXUDVFULWWXUD D EORFFKL HC QHFHVVDULR FKH LO ILOH VLD VWDWR DSHUWR LQ PRGR ELQDULR PRGR ³E´  /HWWXUD /HWWXUD LQW IUHDG YRLG YHW LQW VL]H LQW Q ),/( IS  /HJJH DO SLXC Q RJJHWWL GDO ILOH SXQWDWR GD IS FROORFDQGROL QHO YHWWRUH YHW FLDVFXQR GL GLPHQVLRQH VL]H 5HVWLWXLVFH XQ LQWHUR FKH UDSSUHVHQWD LO QXPHUR GL RJJHWWL HIIHWWLYDPHQWH OHWWL 6FULLWWXUD 6FU WWXUD LQW IZULWH YRLG YHW LQW VL]H LQW Q ),/( IS  6FULYH VXO ILOH SXQWDWR GD IS SUHOHYDQGROL GDO YHWWRUH YHW Q RJJHWWL FLDVFXQR GL GLPHQVLRQH VL]H 5HVWLWXLVFH XQ LQWHUR FKH UDSSUHVHQWD LO QXPHUR GL RJJHWWL HIIHWWLYDPHQWH VFULWWL LQIHULRUH DG Q VROR LQ FDVR GL HUURUH R ILQH GHO ILOH 

7UDVIHULVFH OD VWULQJD V WHUPLQDWD GD
?
QHO ILOH SXQWDWR GD IS 1RQ FRSLD LO FDUDWWHUH WHUPLQDWRUH
?
QHC DJJLXQJH XQ QHZOLQH ILQDOH 5HVWLWXLVFH O
XOWLPR FDUDWWHUH VFULWWR LQ FDVR GL WHUPLQD]LRQH FRUUHWWD (2) DOWULPHQWL

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 595

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 596

(VHPSLLR (VHPS R 3URJUDPPD FKH VFULYH XQD VHTXHQ]D GL UHFRUG GDWL GD LQSXW LQ XQ ILOH ELQDULR #include<stdio.h> typedef struct{ char nome[20]; char cognome[20]; int reddito; }persona; main() { FILE *fp; persona p; int fine=0; fp=fopen("archivio.dat","wb"); do { printf("Dati persona?"); scanf("%s%s%d%d",&p.nome, &p.cognome,&p.reddito); fwrite(&p,sizeof(persona),1,fp); printf("Fine (si=1,no=0)?"); scanf("%d", &fine); }while(!fine); fclose(fp); }

(VHPSLLR (VHPS R 3URJUDPPD FKH OHJJH H VWDPSD LO FRQWHQXWR GL XQ ILOH ELQDULR #include<stdio.h> typedef struct{ char nome[20]; char cognome[20]; int reddito; }persona; main() { FILE *fp; persona p; fp=fopen("archivio.dat","rb"); fread(&p, sizeof(persona),1, fp); while (!feof(fp)) { printf("%s%s%d",p.nome,p.cognome, p.reddito); fread(&p,sizeof(persona),1,fp); } fclose(fp); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 597

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 598

(VHPSLLR (VHPS R 3URJUDPPD FKH ULFHYH FRPH DUJRPHQWR LO QRPH GL XQ ILOH H VFULYH LQ TXHVWR ILOH XQ YHWWRUH GL LQWHUL #include <stdio.h> main(int argc, char **argv) {FILE *file; void stop(char *); int i,n,tab[]={3, 6, -12, 5, -76, 3, 32, 12, 65, 1, 0, -9}; if (argc==2) if ((file=fopen(argv[1],"wb"))==NULL) stop("Impossibile aprire file d’uscita\n"); else stop("Manca il nome del file di uscita\n"); n = sizeof(tab)/sizeof(tab[0]); fwrite(tab, sizeof(tab[0]), n, file); fclose(file); exit(0); } void stop(char *msg) { fprintf(stderr,msg); exit(1); }

(VHPSLLR (VHPS R 3URJUDPPD FKH ULFHYH FRPH DUJRPHQWR LO QRPH GL XQ ILOH GL LQWHUL H PHPRUL]]D LO FRQWHQXWR GL TXHVWR ILOH LQ XQ YHWWRUH GL LQWHUL GL DO SLXC  HOHPHQWL  #include <stdio.h> #define MAX 40 main(int argc, char **argv) {FILE *file; void stop(char *); int i, n, tab[MAX]; if (argc==2) {if ((file=fopen(argv[1], "rb"))==NULL) stop("Impossibile aprire file d’ingresso\n"); } else stop("Manca il nome del file d’ingresso\n"); n=fread(tab, sizeof(tab[0]), MAX, file); fclose(file); for(i=0;i<n;i++) printf("%d%c", tab[i], (i==n-1) ? ’\n’ : ’\t’); exit(0); } void stop(char *msg) { fprintf(stderr,msg); exit(1); }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 599

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 600

(VHPSLLR (VHPS R )LOH GL UHFRUG #include <stdio.h> #include <ctype.h> #define DIM 5 typedef struct { char nome[15], cognome[15], via[10]; int eta;} Persona; Persona P[DIM]; main(int argc, char **argv) { int crea_vettore(Persona V[], int dim); int i, n; FILE *file; if (argc==2) {n=crea_vettore(P,DIM); if ((file=fopen(argv[1], "wb"))==NULL) printf("Impossibile aprire file\n"); else { fwrite(P,sizeof(Persona),n,file); fclose(file); } } else printf("Manca qualche parametro\n"); }

int crea_vettore(Persona P[], int dim) {int i=0, n=0; char s[80]; while (!feof(stdin) && i<dim) { scanf("%s\n",P[i].nome); scanf("%s\n",P[i].cognome); scanf("%s\n",P[i].via); scanf("%d",&(P[i].eta));gets(s); i++; n++; printf("%s\n%s\n%s\n%d\n",P[i].nome, P[i].cognome,P[i].via,P[i].eta); } }

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 601

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 602

)LOH DG DFFHVVR GLUHWWR
,O & FRQVHQWH GL JHVWLUH L ILOH QRQ VROR FRPH VHTXHQ]LDOL PD DQFKH FRPH ILOH DG DFFHVVR GLUHWWR 3RVLL]LLRQDPHQWR LLQ XQ ILLOOH 3RV ] RQDPHQWR Q XQ I H /D IXQ]LRQH IVHHN GHOOD 6WDQGDUG /LEUDU\ FRQVHQWH LO SRVL]LRQDPHQWR GHO SXQWDWRUH DO ILOH VX XQ TXDOXQTXH E\WH LQW IVHHN ),/( I ORQJ RIIVHW LQW RULJLQ VL VSRVWD GL RIIVHW E\WH D SDUWLUH GDOOD SRVL]LRQH RULJLQ FKH YDOH   R   5HVWLWXLVFH œ  VH KD VSRVWDWR OD SRVL]LRQH VXO ILOH œ XQ YDORUH GLYHUVR GD  DOWULPHQWL

3HU SRVL]LRQDUVL DOO
LQL]LR GL XQ ILOH JLDC DSHUWR HC SRVVLELOH XWLOL]]DUH DQFKH OD IXQ]LRQH UHZLQG YRLG UHZLQG ),/( I file=fopen(argv[1], "r+"); ...; rewind(file); HTXLYDOH D fseek(f,0,SEEK_SET);

3RVLL]LLRQH FRUUHQWH QHOO ILLOOH 3RV ] RQH FRUUHQWH QH I H /D IXQ]LRQH IWHOO UHVWLWXLVFH OD SRVL]LRQH GHO E\WH VXO TXDOH VL HC SRVL]LRQDWL QHO ILOH DO PRPHQWR GHOOD FKLDPDWD GHOOD IXQ]LRQH UHVWLWXLVFH  LQ FDVR GL HUURUH  ORQJ IWHOO ),/( I ,O YDORUH UHVWLWXLWR GD IWHOO SXRC HVVHUH XWLOL]]DWR LQ XQD FKLDPDWD DG IVHHN

2ULLJLLQH GHOOOOR VSRVWDPHQWR 2U J QH GH R VSRVWDPHQWR 6((.B6(7 6((.B&85  6((.B(1'   LQL]LR ILOH SRVL]LRQH DWWXDOH QHO ILOH ILQH ILOH

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 603

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 604

(VHPSLLR (VHPS R 3URJUDPPD FKH VRVWLWXLVFH WXWWH OH PLQXVFROH LQ PDLXVFROH LQ XQ ILOH WHVWR GDWR FRPH XQLFR DUJRPHQWR #include <stdio.h> #include <ctype.h> main(int argc, char **argv) { FILE *file; void stop(char *); int ch; if (argc==2) {if ((file=fopen(argv[1], "r+"))==NULL) stop("Impossibile aprire file d’ingresso\n"); } else stop("Manca qualche parametro\n"); while((ch=getc(file))!=EOF) if(islower(ch)) {fseek(file, ftell(file)-1, SEEK_SET); putc(toupper(ch), file); fseek(file, 0, SEEK_CUR); } fclose(file); exit(0); } void stop(char *msg) {fprintf(stderr,msg); exit(1); }

1RWH VXOOOO

HVHPSLLR 1RWH VX HVHPS R œ ,O ILOH HC DSHUWR FRQ PRGDOLWDC ³U´ DJJLRUQDPHQWR PD SRVL]LRQH DOO
LQ]LR GHO ILOH  œ ,O SURJUDPPD OHJJH DG XQR DG XQR L FDUDWWHUL GHO ILOH H TXDQGR WURYD XQD OHWWHUD PLQXVFROD IXQ]LRQH LVORZHU GHOOD OLEUHULD FW\SH  UHWURFHGH FRQ IVHHN GL XQD SRVL]LRQH H OD VRVWLWXLVFH FRQ OD FRUUVLSRQGHQWH PDLXVFROD IXQ]LRQH WRXSSHU œ /
XWLOL]]R GHOOD IXQ]LRQH IVHHN HC XWLOL]]DWD SHU ULSRVL]LRQDUVL VXO FDUDWWHUH DSSHQD OHWWR VH TXHVWR HC XQD OHWWHUD PLQXVFROD IVHHN ILOH IWHOO ILOH  6((.B6(7  œ (C LQROWUH REEOLJDWRULD SHU SRWHU DOWHUQDUH VFULWWXUH H OHWWXUH VX ILOH IVHHN ILOH  6((.B&85  œ /
DSHUWXUD GL XQ ILOH LQ PRGR GL DJJLRUQDPHQWR  DEELQDWR DG XQR TXDOXQTXH WUD U  Z  D ULFKLHGH HVSOLFLWDPHQWH FKH GRSR XQD VHTXHQ]D GL OHWWXUH SULPD GL LQL]LDUH TXDOXQTXH VFULWWXUD YHQJD XVDWD XQD GHOOH IXQ]LRQL GL SRVL]LRQDPHQWR VX ILOH H DQDORJDPHQWH SHU VFULWWXUH VHJXLWH GD OHWWXUH 

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 605

FONDAMENTI DI INFORMATICA

IL LINGUAGGIO C - I FILE 606

Sign up to vote on this title
UsefulNot useful