>> Pob hawl, yn croesawu yn ôl i CS-50.
Mae hyn yn dechrau o wythnos 1, felly mae'n yr wythnos hon ein bod yn deifio
i mewn i raglenni gwirioneddol â'r a rhaglenni yn yr iaith fodern
ac yn dechrau mynd i'r afael ac eithrio rhai o flociau adeiladu sylfaenol y
y bydd yn gwneud i ni a dweud y gwir drwy'r cyfan semester.
Mwy am hynny yn y man fel hyn y mae hynny'n ei olygu,
ond yn fuan un, yr ydych yn ei wneud gael allbrint heddiw
yr holl cod ffynhonnell
y byddwn yn eu defnyddio heddiw a hefyd ar ddydd Gwener.
Fe welwch y maent yn eu llinellau wedi'u rhifo,
felly os ydych chi wedi clywed erioed
o iaith a elwir sylfaenol bod mewn gwirionedd yn iaith
o gorffennol lle rydych yn mewn gwirionedd fel y ddynol sut
i linell Math o 10, llinell 20, llinell 30,
ac yna ysgrifennu eich cod i'r dde o hyn.
Felly yr oedd fath o dyniaethau dull yn graff er mwyn gadael ystafell
yn digwydd yn y dyfodol, a fyddech eisiau i ychwanegu rhai llinellau mwy o chyfundrefn.
Gallech roi rhwng y llinellau 11 a 19,
na wnaethoch raddfa yn arbennig o dda.
Mae'r rhain yn unig argraffu, rhifau llinellau,
felly ni fyddwch yn ysgrifennu niferoedd hynny mae
ond byddaf yn aml yn cyfeirio atynt.
Oriau Swyddfa bod yn mynd rhagddo.
Os hoffech chi i ddod hongian allan naill ai yn y Quad
neu bwyta Quincy Hall neuadd y noson
neu nos nesaf, yn gostwng.
Mae'r cymrodyr addysgu yn fod yno gyda'r CAS
i ddim ond mân sgwrsio am y cwrs,
helpu chi gyda eich crafu prosiectau neu ddim ond yn gyffredinol hongian
allan a gwariant y noson yn mwy o amgylchedd cymunedol.
Ac yna adrannau, byddwn yn y broses sectioning
swyddogol yn dechrau tua hanner nos heno,
felly edrychwch CS50.net 's tudalen hafan
a byddwn wedi cyhoeddiad yno i sut yr ydych yn adran.
Ond fel bod gennym rhai anadlu ystafell
ac nid ydym yn dechrau oddi ar y semester ar rhuthro rhy droed,
nid yr hyn rydym yn mynd i'w wneud yn yn dechrau yn adrannau agos
lle 12-15 myfyrwyr a un CF yr wythnos nesaf,
rydym yn mynd i ddechrau hynny mewn dwy wythnos amser
ac mae hyn yn wythnos gyntaf, felly mae hyn yn dod Llun Mawrth Mercher
rydym yn mynd i gael super adrannau a arweinir gan barau o TFs.
Mae dim ond yn mynd i fod yn fwy adrannau ar gyfer yr wythnos gyntaf
bod croeso i chi mynychu unrhyw un o'r rheini.
Bydd y lleoliadau yn cael eu cyhoeddi, felly yn mynd i CS50.net/sections
i weld y presennol Google calendr am hynny.
Ac yna yn olaf rydym yn ei wneud gennym bagad o fyrddau sensor
os ydych yn awyddus i gymryd ar rhifyn haciwr o PSET-0,
ond yn sylweddoli y bydd bob amser yn yr achos, rydym yn disgwyl,
rydym yn annog bron pawb yn y cwrs
i fynd ar drywydd y golygiad safonol, felly nid yw dylech deimlo
fel eich bod yn syrthio y tu ôl i dorri corneli neu
gan jyst yn gwneud y safon argraffiad sy'n golygu
ar gyfer y rhan fwyaf o bobl.
Fel ag a ddywedodd, rydym yn ei wario a nifer
o ydych eisoes wedi treulio amser chwarae gyda safon
ac yr ydym mewn gwirionedd wedi yn cadw llygad,
rhannol yn unig allan o chwilfrydedd, ar fathau hyn
cyflwyniadau wedi wedi bod yn dod yn barod.
Nid yn awr, i phoeni os ydych yn meddwl 'i' braidd yn gynnar ar gyfer -
bach bug weithiau, y llygoden atalnodau yn ymateb fel weld
os bydd y sgrin yn dechrau ymateb eto.
Mae yna rydym yn mynd.
Pob hawl.
Felly, yr wyf yn mynd yn ei flaen ac yn cloddio yma felly os mai dim ond er mwyn chwilfrydedd's,
dim ond tua 10 y cant o'r dosbarth eisoes wedi cyflwyno.
Felly dyma ni hefyd yn poeni
eich bod chi eisoes yn rhoi y tu ôl i pa mor gyflym rhyw bobl wedi
dechrau cyflwyno.
Amdanom 30 o bobl a gyflwynwyd.
Rydym yn gweld hyn ychydig yn ystadegol diddorol.
Efallai Felly, fel y gellid disgwyl, y rhan fwyaf ohonoch,
llawer o ach yn hidlo Ffenestri.
Beth oedd fath o ddiddorol oedd bod neon ohonoch wedi mynd allan
a got a gorseddedig hon brand yn gweithredu chyfundrefn 'n loyw newydd
at Afal, Llewpard Snow, a byddwn yn gweld os yw rhai
o'r rhai a newid y niferoedd.
Neu efallai rhywun yn ôl yno, felly byddwn yn gweld
pan fydd y niferoedd yn dod i mewn
Mae rhai o'r cwestiynau hyn byddwch yn dod o hyd i
yn PSET-0 arolwg yn wirioneddol dim ond at ddibenion ystadegol.
Byddwn yn ceisio gwneud pethau ychydig o hwyl
â'r data ar draws y semester.
Nid ydym yn sylfaenol angen i gwybod beth ddinas cawsoch eich geni yn,
ond mae'n math o diddorol os gallwn ddiweddarach visualize
hynny gyda rhywbeth fel Google map.
Felly peidiwch â bod yn taflu gormod oddi wrth y rheini.
Os ydych chi'n poeni bod pawb yn yn yr ystafell hon yn cael i-Ffôn,
mo iawn wir.
Y rhan fwyaf ohonoch wedi, neu y rhan fwyaf ohonoch
sydd eisoes wedi cyflwyno gennych ffôn symudol arferol,
sydd yn berffaith, oherwydd ei fod mewn gwirionedd yn golygu y tro Willy
Chow CA a minnau wedi wario ar lais bachgen gwennol
o yn hwyr mewn gwirionedd yn cael ei ddefnyddio.
Mae'n fath o ddull retro
i wneud cais defnyddio touchtone brydlon,
ond yn wir os ydych yn ffoniwch 617-BUG-CS50
gwasanaeth hwnnw bellach yn fyw.
Gallwch gael y tu allan i yma yn gyflym i Quad neu i fater
ar ôl dosbarth lle mae'n yn gweithredu 24 / 7, yn ogystal.
Ac yn dod yn fuan yn mynd i fod yn criw cyfan o nodweddion eraill.
Felly dde awron 'i' yn unig opsiwn 1 am fachgen gwennol,
ond yn dod yn fuan yn opsiwn ar gyfer galwadau wakeup,
yr ydym yn amau fod yn fath o ddefnyddiol
tua amser canol-tymor, amser arholiad.
Rydym yn ychydig yn nerfus oherwydd Yr wyf yn meddwl y gallai hyn backfire
yn gyfan gwbl os mai dim ond darganfod bugs y bore oes gennych
midterms ond bydd ceisio osgoi hynny.
Yn dod yn fuan hefyd bydd cael ei fwyta bwydlenni neuadd.
Os ydych chi am ddarganfod hyn ar gyfer cinio heddiw,
hyn ar gyfer cinio heddiw, diolch i gyd-ddysgu blaenorol,
cymryd yn ganiataol Kato Yuchiama [ ] Sillafu,
byddwn yn cael dewis ar-lein yno hefyd,
ac yna yn fuan i chi bydd yn gallu dod o hyd i
allan pan fydd y nesaf CS-50 oriau swyddfa mewn gwirionedd.
Wel, mae pobl yn tueddu i fel lle maent yn byw.
Mae hyn yn llithro ychydig bod yn rhoi syniad i chi
o ble PSETs wedi wedi'u cyflwyno gan.
Grwpiau myfyrwyr Grwpiau Cymuned yn fath o slacking mae'n ymddangos hyd yn hyn,
er Holeworthy a Matthews yn ymddangos yn
i gael rhywfaint o safon gefn.
Ac yna yma ymysg y tai hyn sy'n drawiadol yw
bod y tŷ yn unig sy'n mae'n debyg yn gwneud pethau eraill
yr wythnos hon wedi bod yn Leverett, sy'n eironig yw un
o'r tai mwyaf a'r olaf blwyddyn y data oedd yn hollol
i'r gwrthwyneb.
Cawsom mwy o fyfyrwyr o Leverett mewnol nag unrhyw
felly efallai ein bod bellach yn talu pris hwnnw.
hwyl Felly, yn union ar hap ffeithiau yma o CS-50.
Felly gadewch i ni yn awr blymio i mewn
Felly, yr wythnos hon a y tu hwnt i raddau helaeth
am yr iaith hon o'r enw C a phan fyddwch yn dechrau chwarae
gyda C ydych yn chwarae gyda go iawn,
traddodiadol a rhaglennu iaith.
felly PSET 1, a fydd yn cael eu gwireddu y dydd Gwener hwn
ar wefan y cwrs, bydd wedi i chi fynd i'r afael â rhywbeth
a all fod yn anghyfarwydd i lawer o chi,
hon yn gweithredu chyfundrefn a elwir cymryd yn ganiataol Linux [sillafu],
a hefyd yn yr iaith hon o'r enw C a byddwn yn treulio llawer
y semester ar C. Beth 's hwyl yr wyf yn meddwl yn arbennig o
am yr iaith hon yw bod yn wahanol Java
ac, yn wahanol ieithoedd sgriptio y gallai rhai ohonoch yn gwybod,
PHP, Python, geiriau allweddol fel hyn, mae'n lefel isel iawn.
'I' jyst yn swil o rywbeth iaith gwasanaeth galw,
y byddwn yn gipolwg ar yn ddiweddarach yn y semester,
a thrwy hynny yr wyf yn golygu ei fod mewn gwirionedd rhoi rheolaeth i chi ddod o hyd i-graen
dros lle stwff yn mynd i mewn chof,
fel lle i mewn FAHAREN ydych chi weithredol yn arfer llinynnau
a rhifau a chronfeydd data a phethau fel 'na.
Ac mae'n caniatáu i chi ar yr un pryd amser i wneud pethau yn wael iawn.
Felly, mae llawer o'r rhain yn campau eich bod yn darllen
am os ydych yn darllen y mathau hyn o gyhoeddiadau neu wefannau
sy'n cynnwys gwefannau yn cael ei darniedig dros gweinyddwyr yn cael eu cymryd
drosodd gan hacwyr ac yn y blaen allan, hyd y dydd hwn yn llawer
o'r rhai cyfaddawdu naill ai y canlyniad
o gamgymeriadau yn unig hurtyn gan bobl
neu fwy o weithiau camgymeriadau a wneir cynnil
oherwydd bod y wefan neu rhaglen, beth bynnag ydyw,
oedd yn ysgrifennu yn C neu C + + neu rhai ieithoedd perthnasol yn ogystal
oherwydd gyda'r pŵer hwn yn dod fel maent yn ei ddweud gyfrifoldeb mawr.
Gallwch wneud llawer o ddifrod i'ch data eu hunain,
at eich diogelwch eich hun drwy codio amhriodol ac nid yn cael ei gadw mewn cof
o faterion diogelwch.
A beth '' n weithredol hwyl, Yr wyf yn meddwl, o ganlyniad,
am siarad am yr iaith hon
ar gyfer y nifer o wythnosau nesaf yn ch 'n sylweddol yn deall dim ond rhai
materion cymdeithasol fel bod, fel pam wnaeth y wefan hon yn cael darniedig?
Sut y gallai rhywun bosibl gwneud hynny
ac yna beth yn fwy atebion deallus i rai
y problemau sy'n ein Bydd ein hunain weld.
Felly crafu yn cwrdd C. felly, gobeithio,
hyd yn oed os nad ydych wedi eistedd i lawr gwneud PSET 0 eto, sydd yn iawn,
digon o amser, byddwch yn gobeithio cael ymdeimlad yr wythnos diwethaf
o'r hyn y mae rhai o'r blociau adeiladu sylfaenol
o raglenni.
Mae fel datganiad, gwneud hyn,
p'un a yw'n dweud rhywbeth, yn credu bod rhywbeth, symud i rywle.
Mae amodau.
Gwnewch hyn os bydd y canlynol yn wir.
Mae newidynnau os oes angen i gadw o amgylch darn o ddata,
ac yna, gwelsom rai ychydig yn bynciau yn fwy soffistigedig
fel edau, cadwyn sgriptiau a digwyddiadau, y gallu hwn
i anfon negeseuon o un sgript
i un arall tra bod yr holl blociau un adeilad yn bodoli yng C
fel y maent yn ei wneud yn y rhan fwyaf o ieithoedd.
A hyd yn oed er hyn yr ydych yn yn mynd i ddechrau ysgrifennu
ar gyfer PSET 1 yn mynd ymlaen i edrych yn fwy fel y isod
ac yn llai fel y brig, 'i' 'n sylweddol' r un syniadau.
Ac os ydych yn cymryd un pwysig pwynt i ffwrdd o heddiw ymlaen,
yn sylweddoli eich bod chi, pan fydd i chi yn gyntaf yn eistedd i lawr i raglen,
efallai am y cyntaf amser ac yn fwyaf tebygol o C
am y tro cyntaf, byddwch yn wneud camgymeriad hurtyn
a byddwch yn straen ar rhai manylion diystyr
fel pam nad yw 'm chyfundrefn gweithio, pam na yn paratoi,
a odds yn mae'n mynd i berwi i lawr i rhywbeth gwirion
yn hoffi gweld y tag ar-lein 4,,
fel chi fydd yn ddamweiniol wedi hepgor y
neu bydd yn rhaid i'r chwith oddi ar un pris yn rhywle
neu ddyfynnu dwbl.
A dweud y gwir mae hwn yn un
o'r rhesymau rydym yn dechrau y cwrs wythnos yn unig diwethaf
gyda safon, dde?
Nid oes rhaid i chi boeni am trafod y manylion hyn yn anniddorol
ond yn awr yn sylweddoli ein bod yn mynd i fod yn rhaglenni cyfrifiaduron go iawn
a rhaid i ni fod yn oed yn fwy manwl gywir a 'i' lefel hon
yn fanwl, mae hyn yn lefel o nitpickiness
sydd mewn gwirionedd yn gwneud gwahaniaeth.
Oherwydd byddwch yn dod o hyd i
y bydd y cyfrifiadur ei wneud llythrennol yr hyn yr ydych yn dweud wrthynt
i'w wneud, ac os nad ydych yn dweud tag mae
yn yr iaith benodol, nad ydych yn dweud y cyfrifiadur
bod hyn yn y diwedd o'r datganiad,
a ydych chi'n gwneud pethau amwys.
Felly amwysedd yn tueddu i fod yn wael mewn rhaglenni fel y gwelwn.
Felly beth yw'r pethau hyn yn mynd i ddechrau edrych fel heddiw?
Wel, yn ymlid cyflym, a datganiad yr wythnos diwethaf yn edrych
fel hyn, o, byd hi, â bloc bach piws
a C yn mynd ymlaen i edrych ychydig yn fwy cryptic
ond ar yr amser y dydd y syniad union yr un peth, Print F,
sy'n golygu argraffu formatted mecanwaith Ohi fath, o fyd
ac yna backslash N, yr wyf yn meddwl ein bod ni ddweud yr wythnos diwethaf,
beth mae hynny'n ei olygu?
Newydd lein. Felly, rhaid i chi hyd yn oed yn eglur bob amser fel
am ble rydych eisiau 'r chofnoda allweddol i gael eu taro
a / N cynrychioli y weithred o taro mynd ar y bysellfwrdd.
Felly dyna beth y datganiad yn yn mynd i ddechrau chwilio
fel yn fuan, a bouillon mynegiant,
felly roedd hyn yn rhywbeth y gallai gwerthuso yn wir neu ffug 1
neu 0 ar neu i ffwrdd, fodd bynnag, chi eisiau gweld y byd.
Wel, beth a ddefnyddir i edrych fel oren a gwyrdd
atodol mae yn hytrach na mynd yn
i edrych ychydig yn fwy math ysgol uwchradd,
ond yn llai grwydro X na Y, ar gau grwydro.
Ac yna os ydych am ymuno y pethau hyn gyda'i gilydd rydym yn
mynd i weld pethau fel ampersand ampersand,
ond digwydd bod yn y gyfundrefn enwi yn C
ar gyfer y syniad bouillon o ac.
Ac y syniad o neu, os ydych yn am ddweud hyn neu hyn,
nid yw'n mynd i bod, neu y mae'n mynd
i fod yn fertigol bar bar fertigol.
Ac mae'n hurt pethau fel hyn hefyd,
lle rydym bydd yn anochel cael FAQ y semestr hwn
fel lle yn y fertigol bar ar fy allweddell?
Nid yw hyn yn gymeriad llawer o ddefnydd i chi fel arfer,
'i' fel arfer yn newid ac uwchben y chofnoda agoriad
ar y bysellfwrdd nodweddiadol byddai hynny'n amrywio.
Felly, unwaith eto yn cael eu hongian yn gynnar yn y semester
ar fanylion dwp fel hyn oherwydd eu bod yn dwp
ac maent yn ddeallusol anniddorol
felly dim ond yn derbyn y ffaith bod hyn yn beth
ddechrau o leiaf rai ieithoedd yn cael eu hoffi.
Wel, beth am fwy o diddorol dyfeisiadau,
amodau.
Os yw X yn llai nag wythnos diwethaf Y rydym yn awyddus i ddweud rhywbeth.
Alt rydym yn awyddus i ddweud rhywbeth arall.
Wel, yr hyn sy'n edrych fel hyn pos nythu adeiladu darn
yn gadael yr wythnos hon ymlaen yn mynd i edrych yn
fel y pethau yn iawn.
Ac eto, mae'n cael ychydig o Messier
ond mae'n dal sylfaenol blociau adeiladu,
os grwydro tir agored, X llai na cromfachau Y agos,
bresys cyrliog, yn y bôn yn cynrychioli dechrau yn
gryf gan gofleidio darnau pos yn yr un modd
i gynnwys darnau neu linellau o cod gyda hyn cyrliog cymryd pob cam.
Print i fyny eto yn print formatted.
Rydym wedi cael rhai quotes cyfarwydd yn awr, backslash a tag.
Felly eisoes yn hyn yn hen het.
Arall os yn awr mae gennym ail gangen yma, amgen,
trydydd gangen, a rhybudd unwaith eto drwy gydol y cymesuredd
y cod hwn.
Os ydym yn agor cyrliog brês rydym yn ei gau a phethau ymddangos
i gael ei wneud gymesur.
Felly bydd yn dod yn bwysig, yn ogystal.
Felly dolenni, gwelsom pethau fel am byth
ac ailadrodd rhai nifer o weithiau.
Wel, y ddau yn edrych yn 'n annaearol ychydig ar yr olwg gyntaf
ond ar y dde dyna ni wedi tra 1 ac mae hwn yn fath
o dolen hurtyn mewn synnwyr oherwydd er bod 1,
yn dda yr hyn y mae hynny'n ei olygu?
Wel, 1 yn gyfystyr cyn belled ag yr ydym wedi gweld
â'r syniad o wir.
Felly, er bod gwir beth mae hyn yn awgrymu yn mynd i ddigwydd
â hynny ddolen ar y dde uchaf?
Mae'n mynd i fynd am byth, iawn oherwydd wir yn wir,
nid yw'n mynd i newid.
Nid yw un yn amrywiol, 'i' mo X,
'i' mo Y. Dydy hi ddim yn foo 'mo' n bell.
Nid 'rhyw dalfan, mae'n yn llythrennol wir werth
felly mae hwn yn fwriadol dolen ddiddiwedd.
Nid yn awr mae'n debyg y defnydd gorau
o dolen ddiddiwedd yma oni bai eich bod yn llythrennol am
i ysgrifennu rhaglen sy'n pan dwbl-cleciedig neu redeg
a a 'n barod i jyst says nauseum ad ohi am byth,
llythrennol hyd nes y llinyn yn tynnu oddi wrth y cyfrifiadur.
Felly weithiau byddwn yn gweld dolenni berfenw yn dda
ond dolenni diderfyn os ydych yn Gall rhywsut ca allan ohonynt
a'r unig ffordd i gael allan o mae hyn yn gwneud yr hyn sy'n cyfateb
Bydd yn Linux gweld o rym i roi'r gorau iddi
y rhaglen arbennig.
Ac i fod yn glir, mae'r rhain yn nad ydynt yn rhaglenni gwirioneddol,
yn union fel y chwith-law Nid oes blociau
pan faner werdd bloc cleciedig
a ddechreuodd sgript yr wythnos diwethaf,
nid yw'r rhain yn cael eu cwblhau rhaglenni,
yn unig yw'r rhain snippets hyd yn hyn.
Byddwn yn gweld yr hyn yr ydym Mae angen i lapio
i mewn i wneud eu rhaglenni gwirioneddol.
Nawr dde ar y gwaelod i ' ychydig yn fwy cryptic yn dal i
ond mae'n batrwm yr ydych chi'n mynd i gael yn gyfarwydd iawn
â oherwydd ei fod yn ofnadwy o ddefnyddiol.
Pedwar int wyf yn cael 0.
Felly, mae hyn yn dweud yn datgan newidyn galw, initialize 'i at 0.
Gwnewch y canlynol fel hir fel yr wyf yn llai na 10,
fel y darn canol oes
rhwng y ddau hanner colon Dywed ei fod yn gyflwr,
felly mae'n debyg i mewn i ysbryd beth sydd yn cromfachau fyny uchod,
anniddorol er bod parenthetical oedd,
felly er fy mod yn llai na 10, wneud y canlynol
ac yna yr ydych yn cofio o'r diwethaf wythnos yr hyn ynghyd a throsodd yn tueddu
i ddynodi?
Cynnyddu, felly mae'r effaith yn Saesneg o'r ddolen hon gwaelod yn
i wneud hyn mewn termau lleygwyr?
Print ohi 10 gwaith.
Iawn. Mae'n nid rhaglen ddiddorol,
ond unwaith eto beth sydd fel arall cryptic edrych chystrawen yn mynd yn
gwaith syml iawn wneud.
Ac fel y bydd yn dod yn bellach yn floc adeiladu newydd.
Wel beth am newidynnau?
Wel, mae hyn yn snippet cod crafu
ar y top yn datgan amrywiol o'r enw cownter.
Mae'n initializes i sero
ac yna mae'n debyg y crafu cod am byth dywedodd y gwerth
y cownter ac mae hyn ychydig yn swigen cartŵn ar y sgrîn
ac yna ei newid cownter gan un
ac yr ydym ddweud yr wythnos diwethaf fod yn gyfwerth â ynghyd a mwy.
Fel y ddolen i fyny yno, bod crafu adeiladu yn cael effaith
o gyfrif o 0 ar i fyny.
Wel, sut yr ydym yn gweithredu syniad bod un yn C?
Wel, mae'n mynd i edrych yn rhywbeth bach fel hyn.
I ddatgan yn amrywiol yn ein C Rhaid gweld fydd yn gymharol
syml yn dweud pa fath o amrywiol ydych eisiau?
Yr wyf eisiau cyfanrif, int aka.
beth ydych chi eisiau roi ei enw fel?
Counter. Beth ydych chi eisiau penodi ei fod yn werth ball o?
Gets 0 felly = 0 tag.
Datgan yn newidyn o'r enw cownter, dywed yr wyf i'n mynd
i roi int tu mewn y peth hyn, beth Int?
Dw i'n mynd i roi 0 tu mewn i ddechrau.
Rwan mae hyn yn ddolen welsom cyn tra mae 1 yn golygu gwneud hyn ad
ddydd y farn, F. print Nawr hwn yn ddiddorol ychydig
a byddwn yn edrych ar fwy o hyn heddiw ac ar ddydd Gwener y cant, D,
hyn yn yr hyn y mae'r F yn Argraffu F.
Pan yr ydym yn dweud Print F ei fod yn golygu print llinyn formatted,
yn dda yr hyn mae'n ei olygu i fformat llinyn?
Mae'n golygu i ddweud wrth y cyfrifiadur arddangos y gwerth hwn ychydig yn
wahanol yn seiliedig ar pa fath o werth y mae.
Nawr mae hyn yn fath o achos anniddorol
oherwydd yr hyn yr ydym yn eisiau argraffu'r?
Dim ond y gwerth cownter, oherwydd yr wyf am
i weithredu yn y bôn y peth scratch fyny uchod,
felly y cant o hyn ymlaen D, yn sefyll ar gyfer cyfanrif degol.
Rhowch yn y cyfanrif yma.
Beth cyfanrif?
Wel, beth bynnag rhoi cyfanrif tu mewn y newidyn
sy'n dod ar ôl y atalnod.
Felly, eto, blociau adeiladu sylfaenol.
Rydych yn sicr, croeso i gymryd ychydig nodiadau ar bethau fel hyn
ond bydd yn digwydd eto mor aml
y bydd hyn yn iawn yn gyflym yn dod yn hen het.
Ac yn olaf cownter yn ogystal a throsodd, ac yn syml, yn golygu cynyddiad
y cownter ac yn ei wneud eto.
Felly, rydym wedi dechrau, rydym wedi C,
swyddogaethol eu bod yn yn union yr un fath.
Yr wyf yn credu y byddwch yn dod o hyd yn sero wythnos ac ar gyfer PSET 0,
hwyl yn unig yn llawer mwy i gael rhywbeth i fyny
a rhedeg gan ddefnyddio'r dull atodol, ond rydych yn mynd i redeg,
rhai o chi, i mewn i fath o nenfydau gyda safon,
pethau na allwch ei wneud mewn gwirionedd yn unig
gan nad yw'n mynegiannol digon o iaith.
Byddwn yn gallu gwneud llawer mwy o bethau grymus gyda C,
pethau'n llawer mwy pwerus gyda PHP a gyda sgript java,
felly yr ydym yn eu cymryd, fel y bydd gweld, yn llwybr i fyny.
Ac yn olaf, rydym wedi hwn beth a elwir yn rhestr.
Pa raglen oedd yr wythnos diwethaf yr wyf yn defnyddio rhestr?
Yeah, yr oedd y RPG, y gêm chwarae rôl
lle i mi symud i'r bach guy i fyny, i lawr, i'r chwith,
ffrwythau yn iawn ac yn casglu ac yna cyflwynodd
at 'r guy wrth y cownter.
Wel, yr oeddwn yn rhoi ffrwythau i mewn i an array, aka a vector,
aka rhestr, er yn dechnegol byddwn yn gweld y semester hynny
mewn gwirionedd wedi gwahanol ystyron ddefnyddiol.
Yn dechrau inni gael rhestr o'r enw amrywiol.
Gallwn ychwanegu llinyn, fel oren, ac wrth ddweud llinyn,
gyda llaw, mae hyn yn cyfrifiadur gwyddoniaeth yn siarad am ddilyniant
o gymeriadau sy'n cynrychioli gair, dedfryd, beth bynnag.
Felly, sy'n mynd i cael eu cynrychioli yn C,
efallai y rhan fwyaf o gryptig yn dal i fod rhestr seren cols,
maint braced agored braced tag ar gau.
Ac ni fyddwn yn treulio llawer amser ar drefnau yr wythnos hon,
ond yn C oherwydd eich bod mor agos i 'r hardware, fel petai,
oherwydd eich bod wedi hynny rheoli llawer dros RAM
a lle y cyfrifiadur rhoi pethau,
rhaid i chi fod yn fwy eglur yn C nag yn crafu.
Mae'n rhaid i chi ddweud fy mod eisiau amrywiaeth o beth yw maint?
Wel, yr ydych yn dweud wrth y cyfrifiadurol trwy gyfrwng
nodiant hwn braced sut fawr i chi am yr amrywiaeth i fod,
felly faint mae mewn gwirionedd yn amrywio ar gyfer rhai gwerth cyson
y mae'n rhaid eu diffinio rhywle arall yn fy rhaglen.
Nid yn unig ar y sgrin.
Ond wedyn pan fyddwch am roi rhywbeth mewn lleoliad penodol
ym meysydd, y [IFE seiniau fel lleoliad], byddwn yn dweud,
'n annhymerus' rhoi'r gorau i ddefnyddio bys dyfyniadau heddiw
gan fod yr holl dermau hyn yn newydd, dw i'n siwr.
Felly y lleoliad IFE yn rhestr yn cael ei fynegi
gyda braced rhestr yr braced cau yn cael neu yn dychwelyd
ac yna beth bynnag gwerth Yr wyf eisiau rhoi i mewn 'na.
ac yn awr hysbysiad Im 'yn ceisio hyd yn oed heddiw, mae hyn yn sero diwrnod wythnos 1
i fod yn ofalus gyda termau fel yn dychwelyd oherwydd byddwn yn dod o hyd i
bod hyn yn arwydd hafal eich bod yn gyfarwydd
gyda o algebra yn y rhan fwyaf Nid yw ieithoedd rhaglennu
cyfartal, nid yw cydraddoldeb, 'i' aseiniad.
Mae'n golygu cymryd yr hyn sydd ar y chwith ac yn ei roi y tu mewn
o'r peth ar y chwith.
Wait, take what's on the right and put it inside 424 00:17:32,796 ---> 00:17:34,016 of the thing on the left. 425 00:17:34,176 ---> 00:17:35,916 And that's why we've seen this pattern array. 426 00:17:36,226 ---> 00:17:37,766 So this begs an interesting question 427 00:17:37,766 ---> 00:17:39,526 and invites an interesting bug. 428 00:17:39,826 ---> 00:17:41,526 How do you compare two values? 429 00:17:41,646 ---> 00:17:43,376 Is X equal to Y? 430 00:17:43,666 ---> 00:17:46,356 Well, you can't say X equals Y because that would seem 431 00:17:46,356 ---> 00:17:47,666 to put Y's value in X, 432 00:17:47,806 ---> 00:17:50,606 so hopefully there will be a solution to that. 433 00:17:51,416 ---> 00:17:56,646 So with that said, here comes C. when you write a C program, 434 00:17:56,696 ---> 00:17:59,946 as I did on the fly on my Mac last week, recall that I opened 435 00:17:59,946 ---> 00:18:02,496 that like terminal window, it was this little blinking cursor. 436 00:18:02,646 ---> 00:18:05,696 I quickly whipped up the code, the source code, 437 00:18:05,696 ---> 00:18:07,706 at top left there, and then I ran a command. 438 00:18:08,116 ---> 00:18:11,786 Well, trivia what was that command? 439 00:18:12,306 ---> 00:18:14,936 GCC. So this is a term you will not forget after term's end 440 00:18:14,936 ---> 00:18:16,396 because you're going to be running it every day 441 00:18:16,756 ---> 00:18:17,856 that you're tackling problems. 442 00:18:17,856 ---> 00:18:20,176 That's GCC, is a compiler. 443 00:18:20,586 ---> 00:18:21,946 A compiler is a program 444 00:18:22,016 ---> 00:18:23,636 that takes something called source code 445 00:18:23,866 ---> 00:18:25,896 and turns it into object code. 446 00:18:26,016 ---> 00:18:26,816 Now what does that mean? 447 00:18:27,076 ---> 00:18:29,646 Well, nicely enough this course is not 448 00:18:29,706 ---> 00:18:31,886 about writing stuff like this. 449 00:18:32,246 ---> 00:18:34,016 There was a time and there were -- 450 00:18:34,016 ---> 00:18:36,556 there are people still roaming this campus who had 451 00:18:36,556 ---> 00:18:40,746 to essentially write programs at the level of zeros and ones, 452 00:18:40,746 ---> 00:18:42,576 or if you've heard of this thing called a punch card, 453 00:18:42,576 ---> 00:18:44,816 very similar in spirit, and there was a time 454 00:18:44,816 ---> 00:18:48,026 when the science center was a center of science. 455 00:18:48,026 ---> 00:18:49,826 You would go into the basement, you would hand 456 00:18:49,826 ---> 00:18:54,096 in your punch cards and then the person running the mainframe 457 00:18:54,096 ---> 00:18:55,006 computer in the basement 458 00:18:55,006 ---> 00:18:57,496 of the science center would essentially run your homework 459 00:18:57,496 ---> 00:19:00,236 for you overnight, because it would usually take a while, 460 00:19:00,236 ---> 00:19:03,426 by literally putting a stack of cards somewhere into a machine. 461 00:19:03,736 ---> 00:19:05,816 So not the most efficient process, and I've talked 462 00:19:05,816 ---> 00:19:10,356 to current faculty who grew up this way on a campus, 463 00:19:10,586 ---> 00:19:14,416 not the best time of day to find out that your program is buggy, 464 00:19:14,556 ---> 00:19:15,976 it has some mistake in it, right? 465 00:19:15,976 ---> 00:19:17,146 Because that's probably happening 466 00:19:17,146 ---> 00:19:19,646 at 9 a.m. the next morning when you go back to the center 467 00:19:19,646 ---> 00:19:21,996 of science and pick up the results of your program 468 00:19:22,176 ---> 00:19:23,676 and it's just one big error message. 469 00:19:23,676 ---> 00:19:26,676 So fortunately we've come a long way, and all of your fighting 470 00:19:26,676 ---> 00:19:29,426 with bugs and implementing cool things will happen much more 471 00:19:29,426 ---> 00:19:32,316 interactively within seconds in front of your own computer 472 00:19:32,316 ---> 00:19:35,696 or the lab computer, but the process is essentially still 473 00:19:35,696 ---> 00:19:36,126 the same. 474 00:19:36,216 ---> 00:19:39,236 At the end of the day a computer, whether it's a PC 475 00:19:39,236 ---> 00:19:41,016 or a Mac or whatever you guys own at home, 476 00:19:41,326 ---> 00:19:43,596 it only understands 0s and 1s. 477 00:19:43,596 ---> 00:19:46,126 Now the world has done some really clever things with 0s 478 00:19:46,126 ---> 00:19:49,626 and 1s but you still have to turn your programs into 0s 479 00:19:49,626 ---> 00:19:51,966 and 1s and a compiler does exactly that. 480 00:19:52,176 ---> 00:19:54,476 So you'll be writing stuff that looks like this at top left. 481 00:19:54,786 ---> 00:19:57,416 You'll run that file, which is really just a text file, 482 00:19:57,506 ---> 00:19:58,916 through a program called a compiler, 483 00:19:59,116 ---> 00:20:01,556 and that compiler will take care of the task of turning it 484 00:20:01,556 ---> 00:20:03,976 into 0s and 1s like this. 485 00:20:04,886 ---> 00:20:06,016 So where do you do this? 486 00:20:06,016 ---> 00:20:10,116 Well, last week I went ahead and ran this program, 487 00:20:10,116 ---> 00:20:12,136 so just a quick show of hands then we'll have the data 488 00:20:12,136 ---> 00:20:13,856 for sure, how many of you have Macs in the room? 489 00:20:14,036 ---> 00:20:17,106 All right, so that's actually statistically kind of a lot, 490 00:20:17,556 ---> 00:20:19,446 so it looks like more than half of you 491 00:20:19,446 ---> 00:20:21,266 in the room this semester, and that's pretty consistent 492 00:20:21,266 ---> 00:20:24,626 with terms past, what's neat is that all the stuff we're going 493 00:20:24,626 ---> 00:20:25,926 to start doing this week and beyond, 494 00:20:26,176 ---> 00:20:28,706 you actually could have been doing on your own computers, 495 00:20:28,706 ---> 00:20:30,656 your own Macs, this whole time. 496 00:20:30,656 ---> 00:20:33,156 Now terminal is not a program that's usually 497 00:20:33,386 ---> 00:20:35,676 in the little dock here as it is in mine 498 00:20:35,676 ---> 00:20:38,006 because most users don't need to know or care 499 00:20:38,006 ---> 00:20:39,016 about this little terminal. 500 00:20:39,276 ---> 00:20:40,796 But you may at least know anecdotally 501 00:20:40,796 ---> 00:20:42,986 that Mac OS these days is based 502 00:20:42,986 ---> 00:20:44,576 on an operating system called UNIX. 503 00:20:44,796 ---> 00:20:48,626 And back in the day UNIX was essentially a command line 504 00:20:48,686 ---> 00:20:51,096 interface, CLI is the buzzword there, 505 00:20:51,096 ---> 00:20:54,356 and this means you don't double click on icons to run programs, 506 00:20:54,586 ---> 00:20:57,106 you don't go to pretty menus that have sub menus 507 00:20:57,106 ---> 00:20:59,806 and graphical icons and all of this fanciness that developed 508 00:20:59,806 ---> 00:21:02,186 over the years, you if you want to run a program, 509 00:21:02,396 ---> 00:21:04,796 you type the name of the program and hit enter. 510 00:21:05,026 ---> 00:21:07,726 And if you want to influence the behavior of that program, 511 00:21:07,726 ---> 00:21:11,216 you provide it with commands line argument, words or numbers 512 00:21:11,486 ---> 00:21:13,736 after the program's name, and then hit enter. 513 00:21:13,736 ---> 00:21:16,246 If you've ever seen DOS on a computer back home, 514 00:21:16,416 ---> 00:21:17,746 it's very similar in spirit. 515 00:21:17,826 ---> 00:21:20,636 Usually a black screen, white text, and you can't do much 516 00:21:20,706 ---> 00:21:23,926 of interest with it unless you know what's there underneath. 517 00:21:23,926 ---> 00:21:26,226 So you're not going to write for the most part programs 518 00:21:26,226 ---> 00:21:29,746 in this course on your own Macs or on your own PCs per se, 519 00:21:29,966 ---> 00:21:34,346 because it's quite hard for us to standardize the configuration 520 00:21:34,346 ---> 00:21:36,176 of 300 plus people's laptops. 521 00:21:36,406 ---> 00:21:38,276 So instead what we do is provide everyone 522 00:21:38,276 ---> 00:21:40,676 with a uniform environment throughout the semester. 523 00:21:40,926 ---> 00:21:42,936 Initially this is a cluster 524 00:21:43,146 ---> 00:21:48,036 of servers called nice.fas.harvard.edu. 525 00:21:48,106 ---> 00:21:50,246 And this nice cluster does so happen to live 526 00:21:50,246 ---> 00:21:52,306 in this very idyllic building here. 527 00:21:52,646 ---> 00:21:54,246 So it's in the basement, unfortunately, 528 00:21:54,246 ---> 00:21:55,206 the cluster of servers. 529 00:21:55,206 ---> 00:21:57,216 It's also a bit of a misnomer 530 00:21:57,216 ---> 00:22:01,366 because NICE new instructional computing environments has been 531 00:22:01,446 ---> 00:22:05,486 NICE for about 10 years now, and there isn't even an old version 532 00:22:05,486 ---> 00:22:07,846 so they've kind of painted themselves into a corner here. 533 00:22:07,906 ---> 00:22:10,006 We're using the same NICE cluster as always. 534 00:22:10,006 ---> 00:22:13,416 So nice.fas.harvard.edu, it's not a website, 535 00:22:13,416 ---> 00:22:15,906 it's not something you type into a browser, but it's going 536 00:22:15,906 ---> 00:22:18,966 to be the name of a server that you access by way 537 00:22:18,966 ---> 00:22:21,766 of your own Mac or by way of your own PC. 538 00:22:21,766 ---> 00:22:22,686 Well, what do I mean by that? 539 00:22:23,086 ---> 00:22:26,216 well, a lot of you these days have college.harvard.edu 540 00:22:26,216 ---> 00:22:28,776 addresses, but you also have the so-called FAS accounts, 541 00:22:28,776 ---> 00:22:31,756 and these are the user names and passwords you use to login 542 00:22:31,756 ---> 00:22:34,276 to the basement of the science center, kiosks and all of that, 543 00:22:34,396 ---> 00:22:36,186 frankly it will be a few more years, I think, 544 00:22:36,186 ---> 00:22:38,336 before the university converges on just one account. 545 00:22:38,586 ---> 00:22:40,326 But for now you have these accounts, though they're 546 00:22:40,326 ---> 00:22:41,536 of decreasing utility. 547 00:22:41,926 ---> 00:22:44,256 But for this course at least at the start 548 00:22:44,256 ---> 00:22:48,446 of the semester we do acquaint you with nice.fas.harvard.edu 549 00:22:48,446 ---> 00:22:49,316 because what it means is 550 00:22:49,316 ---> 00:22:52,846 that after the semester you'll actually still have a Linux 551 00:22:52,846 ---> 00:22:56,276 server environment on which you can run and write code, 552 00:22:56,276 ---> 00:22:58,896 whether it's for an economic class in the further 553 00:22:58,896 ---> 00:23:00,646 or a chemistry project or anything 554 00:23:00,646 ---> 00:23:03,326 where you need the ability to write programs and you're not 555 00:23:03,376 ---> 00:23:05,636 in CS-50 anymore, you'll at least have this resource 556 00:23:05,636 ---> 00:23:08,046 for some number for a good amount of time. 557 00:23:08,416 ---> 00:23:10,656 What we will start using mid-semester, though, 558 00:23:10,826 ---> 00:23:12,896 and this is the extent of my Photoshop ability, 559 00:23:13,316 ---> 00:23:15,596 we will use the so-called Cloud. 560 00:23:15,686 ---> 00:23:17,786 So this is another one of these buzzwords these days, 561 00:23:17,836 ---> 00:23:20,316 Cloud computing refers to the ability 562 00:23:20,316 ---> 00:23:23,636 to rent server space somewhere out there on the Internet, 563 00:23:23,636 ---> 00:23:27,236 you don't know or care who or where the servers are 564 00:23:27,236 ---> 00:23:28,676 or who is running these servers. 565 00:23:28,956 ---> 00:23:31,176 So we're essentially going to implement this same idea. 566 00:23:31,176 ---> 00:23:33,356 CS-50 has its own cluster of servers now 567 00:23:33,356 ---> 00:23:35,876 in the computer science building called Maxwell Dworkin 568 00:23:35,876 ---> 00:23:36,096 [sounds like]. 569 00:23:36,096 ---> 00:23:38,426 We've got a couple of really big Dell servers 570 00:23:38,426 ---> 00:23:41,596 that have a whole lot of CPUs, 32 gigabytes of RAM inside 571 00:23:41,596 ---> 00:23:43,446 of them, 6 terabytes of storage space. 572 00:23:43,866 ---> 00:23:46,346 So we can hold our own against HCS here. 573 00:23:46,516 ---> 00:23:47,466 We have a couple of those. 574 00:23:47,466 ---> 00:23:49,866 We have an Apple X Serve and long story short all 575 00:23:49,866 ---> 00:23:52,336 of this stuff is wired together in such a way 576 00:23:52,336 ---> 00:23:54,666 that soon this semester you will have your own accounts 577 00:23:54,976 ---> 00:23:59,976 on cloud.cs50.net, and what this will mean is you'll start using 578 00:23:59,976 ---> 00:24:02,596 at some point this cluster instead of nice.fas 579 00:24:03,166 ---> 00:24:04,376 but it's really the same thing. 580 00:24:04,376 ---> 00:24:05,586 Now what do I mean by cluster? 581 00:24:05,936 ---> 00:24:08,356 In the basement of the science center, and also in the basement 582 00:24:08,356 ---> 00:24:10,396 of Maxwell Dworkin, there's a bunch of computers. 583 00:24:10,466 ---> 00:24:12,766 They're all networked together using Ethernet cables 584 00:24:12,766 ---> 00:24:13,816 into a thing called a switch. 585 00:24:14,106 ---> 00:24:18,866 They are exposed publicly on the Internet by way of IP address 586 00:24:18,866 ---> 00:24:21,596 or these names, nice.fas.harvard.edu, 587 00:24:22,056 ---> 00:24:25,316 and there is this illusion that when you connect to one 588 00:24:25,316 ---> 00:24:27,466 of these servers you're connecting to just one, 589 00:24:27,656 ---> 00:24:29,886 but in fact there's like six computers 590 00:24:29,916 ---> 00:24:32,896 in this nice.fas cluster, but they've set them up in 591 00:24:32,896 ---> 00:24:34,856 such a way that you the user don't know or care 592 00:24:34,856 ---> 00:24:35,946 which particular one you're 593 00:24:35,946 ---> 00:24:39,646 on because your files are all visible on any one 594 00:24:39,646 ---> 00:24:41,006 of those machines and all 595 00:24:41,006 ---> 00:24:43,046 of the commands are identical across the same. 596 00:24:43,326 ---> 00:24:46,746 So when it comes time to tackle PSET 1, what you're going to do 597 00:24:46,746 ---> 00:24:48,736 if you're a Mac person is something like this, 598 00:24:48,946 ---> 00:24:50,516 I just ran the terminal program, 599 00:24:50,936 ---> 00:24:52,576 I'm going to type a command called SSH. 600 00:24:52,666 ---> 00:24:54,786 This stands for Secure Shell, 601 00:24:54,786 ---> 00:24:57,226 and this just means make an encrypted connection 602 00:24:57,226 ---> 00:25:00,646 between my laptop, wherever I am in the world, and this server. 603 00:25:00,836 ---> 00:25:04,546 And so what I'm going to do is type SSH.nice let's do Malan 604 00:25:04,646 ---> 00:25:07,426 at nice.fas.harvard.edu. 605 00:25:07,426 ---> 00:25:10,966 I'm going to get a prompt then, looks like I am indeed connected 606 00:25:10,966 ---> 00:25:12,266 to Harvard University server. 607 00:25:12,306 ---> 00:25:13,646 It's asking me for a password. 608 00:25:13,816 ---> 00:25:16,436 Fortunately it won't show you on the screen what the password is 609 00:25:16,496 ---> 00:25:18,576 but now I'm at the so-called blinking prompt 610 00:25:18,846 ---> 00:25:20,056 that these days no longer blinks 611 00:25:20,276 ---> 00:25:21,906 but a blinking prompt it once was. 612 00:25:22,246 ---> 00:25:24,096 So I apparently have three unread messages, 613 00:25:24,196 ---> 00:25:26,496 which has been some time since I never check this email 614 00:25:26,536 ---> 00:25:27,296 account anymore. 615 00:25:27,556 ---> 00:25:29,316 So Malan at nice and then there's this thing 616 00:25:29,316 ---> 00:25:30,066 in parentheses. 617 00:25:30,206 ---> 00:25:32,426 Well this is the command line, so to speak, 618 00:25:32,426 ---> 00:25:34,216 where you can type anything you want. 619 00:25:34,536 ---> 00:25:36,956 Well, that command was found. 620 00:25:37,566 ---> 00:25:40,046 And this is where initially we're going to be writing things 621 00:25:40,086 ---> 00:25:42,406 like C source code and compiling it. 622 00:25:42,806 ---> 00:25:44,496 So what does that mean exactly? 623 00:25:44,496 ---> 00:25:46,536 Well I'm going to do the following just 624 00:25:46,536 ---> 00:25:48,396 to give you a sense of some basics and all 625 00:25:48,396 ---> 00:25:52,666 of this realize will be repeated in problem set 1 own PDF. 626 00:25:52,826 ---> 00:25:55,266 So your hand will be held certainly early in the semester 627 00:25:55,376 ---> 00:25:56,956 so you needn't try to memorize or scribble 628 00:25:56,956 ---> 00:25:58,836 down all these commands but just to give you a sense 629 00:25:58,836 ---> 00:26:01,326 of what we're doing here, if I type the command LS, 630 00:26:01,576 ---> 00:26:04,816 this is list, this means list the files in this directory, 631 00:26:04,816 ---> 00:26:06,816 this folder, well there's nothing there. 632 00:26:07,096 ---> 00:26:09,756 So when I'm at this prompt it's equivalent conceptually 633 00:26:09,756 ---> 00:26:12,816 to being inside of a folder on your own Mac or your own PC, 634 00:26:12,936 ---> 00:26:15,266 it's just graphically very underwhelming. 635 00:26:15,266 ---> 00:26:16,866 And it's more mechanical at the keyboard 636 00:26:16,866 ---> 00:26:18,046 than it is mouse oriented. 637 00:26:18,286 ---> 00:26:21,246 If I want to make a directory called CS-50 for the duration 638 00:26:21,246 ---> 00:26:24,556 of the semester, MKDIR is make directory. 639 00:26:24,836 ---> 00:26:27,006 Back in the day people didn't like to write long commands, 640 00:26:27,106 ---> 00:26:30,306 pretty reasonable, so it's just MKDIR CS-50 hit enter, 641 00:26:30,536 ---> 00:26:34,686 nothing seems to happen but if I type LS now for list notice aha, 642 00:26:34,686 ---> 00:26:36,336 there's a directory as implied 643 00:26:36,336 ---> 00:26:38,476 by the slash there, that's called CS-50. 644 00:26:38,756 ---> 00:26:41,176 Well, I can't, unfortunately, it's a little blue 645 00:26:41,176 ---> 00:26:44,576 on this screen here but that indeed says CS-50 if I type LS. 646 00:26:45,186 ---> 00:26:48,596 Notice that I can't, and this is kind of a neophyte mistake, 647 00:26:48,596 ---> 00:26:51,236 can't really double click and nothing really happens there 648 00:26:51,396 ---> 00:26:56,136 so you have to change directory CD into CS-50 and then hit enter 649 00:26:56,376 ---> 00:26:58,926 and now my parentheses, this is just a special feature 650 00:26:58,926 ---> 00:27:01,266 of the way we've configured your accounts, you'll see. 651 00:27:01,536 ---> 00:27:03,786 This just reminds you where you are, which is nice 652 00:27:03,786 ---> 00:27:06,116 when you start to have lots of folders open. 653 00:27:06,456 ---> 00:27:09,996 Now if I type in LS here there's nothing here, so if I want 654 00:27:09,996 ---> 00:27:13,106 to write my first program I need to open a text editor 655 00:27:13,106 ---> 00:27:16,266 and the text editor that we'll encourage most students 656 00:27:16,266 ---> 00:27:18,336 to use is this little program called NANO. 657 00:27:18,706 ---> 00:27:22,366 If you've ever used notepad on PCs or text edit on Macs, 658 00:27:22,766 ---> 00:27:25,586 this is like the most user friendly text editor you can 659 00:27:25,586 ---> 00:27:27,996 find but as such it's also lacking a number 660 00:27:27,996 ---> 00:27:29,856 of useful features as some of you 661 00:27:29,856 ---> 00:27:31,116 with more experience will see. 662 00:27:31,376 ---> 00:27:34,456 So let me go ahead and write hello.c. Notice 663 00:27:34,526 ---> 00:27:36,866 that I have just a simple window. 664 00:27:37,056 ---> 00:27:38,466 It's a little cryptic at the bottom, 665 00:27:38,466 ---> 00:27:40,166 but again this is just stupid stuff 666 00:27:40,166 ---> 00:27:41,946 that once you know it you kind of never forget. 667 00:27:42,146 ---> 00:27:44,356 The carrot symbol means hold the control key, 668 00:27:44,626 ---> 00:27:45,856 so we'll get to that in a second. 669 00:27:45,856 ---> 00:27:48,246 But let me go each and recreate really quickly what I did 670 00:27:48,246 ---> 00:27:48,816 last week. 671 00:27:49,096 ---> 00:27:51,266 So I type this cryptic sequence of characters, 672 00:27:51,266 ---> 00:27:55,706 include STDIO.h in angle brackets. 673 00:27:56,056 ---> 00:28:00,936 I then set into main ent arg C char star, 674 00:28:02,156 ---> 00:28:07,196 delete key is not working, we've got to fix that. 675 00:28:07,716 ---> 00:28:09,626 Well, that's interesting. 676 00:28:11,786 ---> 00:28:14,326 Wow. All right, I'm going to do this again 677 00:28:14,326 ---> 00:28:15,736 without making any mistakes. 678 00:28:16,496 ---> 00:28:23,556 Standardio.h ent main ent arg C char star, here it comes, 679 00:28:23,606 ---> 00:28:28,916 arg v bracket parentheses open curly brace 1234 Print F 680 00:28:28,916 ---> 00:28:31,486 and I'm just going to say hello, world. 681 00:28:31,486 ---> 00:28:34,386 I'm going to make it look pretty at the end with the backslash n, 682 00:28:34,536 ---> 00:28:37,126 closed parentheses, semicolon, close. 683 00:28:37,126 ---> 00:28:39,996 Okay. we're not going to indent properly here we go, 684 00:28:41,086 ---> 00:28:41,526 closed brace. 685 00:28:41,656 ---> 00:28:43,076 So that's my first C program, 686 00:28:43,076 ---> 00:28:45,246 and this is the thing we completely underwhelmed you 687 00:28:45,246 ---> 00:28:45,956 with last week. 688 00:28:45,956 ---> 00:28:47,396 But what does this mean I'm going to do now? 689 00:28:47,396 ---> 00:28:49,106 I'm going to hit control X 690 00:28:49,176 ---> 00:28:51,686 because down here it said carrot X for exit 691 00:28:52,016 ---> 00:28:53,966 so now it's saying save modified buffer. 692 00:28:54,146 ---> 00:28:55,626 This is the un-user friendly way 693 00:28:55,626 ---> 00:28:57,356 of saying do you want to save your changes? 694 00:28:57,576 ---> 00:28:58,636 I hit Y for yes. 695 00:28:58,746 ---> 00:29:01,326 It then checks what name do you want to give this file? 696 00:29:01,326 ---> 00:29:03,086 Again it's just reminding me what it's already called 697 00:29:03,086 ---> 00:29:03,986 in case I want to change it. 698 00:29:03,986 ---> 00:29:06,596 I hit enter and voila, I'm back at the prompt. 699 00:29:06,806 ---> 00:29:09,446 But if I now type LS what should I see? 700 00:29:10,196 ---> 00:29:12,656 Hopefully hello.c and this time it's all white 701 00:29:12,726 ---> 00:29:15,346 because it's not a directory so now I need to run this thing, 702 00:29:15,566 ---> 00:29:19,056 so I could try doing hello.c or again the newbie mistake 703 00:29:19,056 ---> 00:29:21,296 of really that's not going to do anything, 704 00:29:21,616 ---> 00:29:23,726 hit enter, permission denied. 705 00:29:23,936 ---> 00:29:26,316 But that makes sense because what's inside this file? 706 00:29:27,086 ---> 00:29:30,396 This is not 0s and 1s clearly, this is just source code. 707 00:29:30,396 ---> 00:29:33,136 So source code is the human readable form of this stuff 708 00:29:33,286 ---> 00:29:36,816 but I remember from last week GCC hello.c. Let me go ahead 709 00:29:36,816 ---> 00:29:40,666 and hit enter now, nothing seems to happen, but wait, LS. 710 00:29:40,956 ---> 00:29:41,486 Interesting. 711 00:29:41,916 ---> 00:29:45,446 Again foolishly named default name for a program anything 712 00:29:45,446 ---> 00:29:48,026 like hello.exe would seem a little more reasonable, 713 00:29:48,256 ---> 00:29:50,586 but a.out is sort of the standard but we'll be able 714 00:29:50,586 ---> 00:29:51,756 to override that soon. 715 00:29:52,056 ---> 00:29:55,836 So a.out has an asterisks which just means I am executable. 716 00:29:55,836 ---> 00:29:57,436 I have 0s and 1s inside of me. 717 00:29:57,436 ---> 00:30:01,076 In fact if I get a little adventurous and now open a.out 718 00:30:01,076 ---> 00:30:03,596 with nano, nano being a text editor, 719 00:30:03,596 ---> 00:30:07,106 a.out being a binary file, binary meaning just 0s 720 00:30:07,106 ---> 00:30:10,526 and 1s inside, so that's the program I wrote. 721 00:30:10,656 ---> 00:30:13,676 Now here's too where you can do very bad things very easily. 722 00:30:13,676 ---> 00:30:16,556 If you so much as hit the space bar accidentally 723 00:30:16,556 ---> 00:30:18,496 as you're flailing about in the basement 724 00:30:18,496 ---> 00:30:19,236 of the science center trying 725 00:30:19,236 ---> 00:30:20,946 to fix whatever problem you've just created, 726 00:30:21,186 ---> 00:30:23,936 you just inserted aski characters, 727 00:30:23,936 ---> 00:30:26,556 random text into what's otherwise a binary file. 728 00:30:26,806 ---> 00:30:28,466 Why does this look so cryptic? 729 00:30:28,746 ---> 00:30:30,406 Well, nano is again a text editor. 730 00:30:30,406 ---> 00:30:34,466 It doesn't know -- it's not supposed to show you 0s and 1s. 731 00:30:34,696 ---> 00:30:37,646 It's supposed to show you the letters and the numbers 732 00:30:37,646 ---> 00:30:40,296 and the punctuation that those 0s and 1s represent. 733 00:30:40,566 ---> 00:30:42,916 So when you see all this crazy stuff on the screen, 734 00:30:43,136 ---> 00:30:45,276 that's just nano's misinterpretation 735 00:30:45,476 ---> 00:30:48,796 of what's really 0s and 1s, not necessarily aski letters. 736 00:30:49,006 ---> 00:30:51,086 Fortunately control X will save me here, 737 00:30:51,086 ---> 00:30:53,096 save modified buffer, no this time. 738 00:30:53,356 ---> 00:30:55,236 So I hit N, I go back to the prompt 739 00:30:55,416 ---> 00:30:58,776 and now I can run a.out just by typing this command here 740 00:30:58,776 ---> 00:31:02,446 and in fact that's my first repeated C program. 741 00:31:02,806 ---> 00:31:03,356 So that's it. 742 00:31:03,356 ---> 00:31:05,606 that's the basic building block of writing a C program. 743 00:31:05,666 ---> 00:31:08,706 Hopefully we can do much more interesting things than that 744 00:31:09,146 ---> 00:31:10,826 but that covers the basic workflow. 745 00:31:10,826 ---> 00:31:12,766 Now just to give you a sense that what we're doing 746 00:31:12,766 ---> 00:31:16,336 in this course is ultimately pretty much operating system 747 00:31:16,336 ---> 00:31:20,246 agnostic, here we have Mac, here we have PC, running Mac OS, 748 00:31:20,246 ---> 00:31:23,246 running Windows, on the Windows world it's not quite as easy 749 00:31:23,246 ---> 00:31:25,886 as going to a terminal window because Windows does not ship 750 00:31:25,966 ---> 00:31:28,616 with that little program called SSH 751 00:31:28,616 ---> 00:31:31,366 with which I created the secure connection, but there's plenty 752 00:31:31,366 ---> 00:31:32,776 of free tools that do that. 753 00:31:33,166 ---> 00:31:33,866 One thing you'll see 754 00:31:33,866 ---> 00:31:35,876 on the course's website is program called putty. 755 00:31:35,966 ---> 00:31:38,666 So if you're a PC user you run a program 756 00:31:38,666 ---> 00:31:41,306 like this it's a little more user friendly if overwhelming 757 00:31:41,306 ---> 00:31:42,296 with all of its options. 758 00:31:42,546 ---> 00:31:44,846 What I'm simply going to do here is say, you know what, 759 00:31:44,846 ---> 00:31:48,196 I want to connect to nice.Fas.harvard.edu. 760 00:31:48,466 ---> 00:31:50,666 so if I were doing this for the first time I would simply do 761 00:31:50,666 ---> 00:31:54,066 something like that, typing in its host name or its IP address. 762 00:31:54,066 ---> 00:31:55,696 Now I'm going to go ahead and click -- 763 00:31:55,866 ---> 00:31:57,666 actually I'm going to load something there 764 00:31:57,716 ---> 00:31:58,806 so I get the bigger font size. 765 00:31:58,806 ---> 00:32:02,296 I'm going to click open and now I get this black window again. 766 00:32:02,296 ---> 00:32:03,566 I'm going to log in as Malan. 767 00:32:03,696 ---> 00:32:07,526 I get a similar prompt, enter, notice I'm back in here. 768 00:32:07,526 ---> 00:32:10,606 if I type LS there's CS-50, CD CS-50, aha, 769 00:32:10,606 ---> 00:32:11,996 there's my a.out program. 770 00:32:12,536 ---> 00:32:13,386 Same exact thing. 771 00:32:13,586 ---> 00:32:14,906 So this is actually powerful. 772 00:32:14,906 ---> 00:32:17,096 Even though I'm on a PC here, a Mac over there, 773 00:32:17,216 ---> 00:32:20,466 I've accessed the same account so that's one of the upsides 774 00:32:20,466 ---> 00:32:21,746 of having this central server. 775 00:32:21,746 ---> 00:32:23,856 You'll be able to access your code anywhere. 776 00:32:23,856 ---> 00:32:26,116 If you go home on break, if you get sick, if you're just hanging 777 00:32:26,116 ---> 00:32:28,566 out a friend's you can access your code in this course 778 00:32:28,896 ---> 00:32:31,106 from any computer on the Internet and you'll find 779 00:32:31,266 ---> 00:32:33,676 that for better or for worse to be at least useful. 780 00:32:33,856 ---> 00:32:35,876 So just to give you a sense of what else you can do 781 00:32:36,126 ---> 00:32:40,136 on a typical Linux server, so the machine I connected to, 782 00:32:40,166 ---> 00:32:42,016 nice.fas, it's running at Linux, 783 00:32:42,336 ---> 00:32:45,306 which is again an operating system that's larger text-based 784 00:32:45,716 ---> 00:32:49,246 but there are goes, graphical user interfaces, 785 00:32:49,426 ---> 00:32:51,006 and in fact if you headed to the basement 786 00:32:51,006 ---> 00:32:53,206 of the science center someday as you might for office hours, 787 00:32:53,466 ---> 00:32:56,196 they've got like 33 percent Linux computers 788 00:32:56,196 ---> 00:32:56,976 in the basement there. 789 00:32:57,256 ---> 00:32:59,326 they do have Windows, they do have menus, 790 00:32:59,376 ---> 00:33:01,306 things you can click, because the world has come 791 00:33:01,306 ---> 00:33:03,596 up with more user-friendly interfaces to Linux. 792 00:33:03,826 ---> 00:33:05,786 But you'll get your hands directly this semester with some 793 00:33:05,786 ---> 00:33:08,636 of the building blocks because frankly when you get comfortable 794 00:33:08,636 ---> 00:33:12,026 with this stuff you can do so much more, so much more easily 795 00:33:12,026 ---> 00:33:13,756 and so much more efficiently just 796 00:33:13,756 ---> 00:33:16,216 by telling the computer precisely what you want. 797 00:33:16,216 ---> 00:33:19,226 So CD we saw, CP, any guess? 798 00:33:19,836 ---> 00:33:23,416 CP, copy, kind of the shorthand version of copy, 799 00:33:23,416 ---> 00:33:26,276 and in fact it is, CP we'll use to copy code this semester. 800 00:33:26,276 ---> 00:33:32,186 LS is list, MK-Dir is make directory, MV, move, 801 00:33:32,186 ---> 00:33:35,826 so that's synonymous, we'll see renaming a file, 802 00:33:35,826 ---> 00:33:39,846 so to rename a file you'll say MV space the original name space 803 00:33:39,936 ---> 00:33:43,206 the new name, not terribly hard. 804 00:33:43,896 ---> 00:33:45,856 PWD. Yeah, so print working directory. 805 00:33:45,856 ---> 00:33:48,726 What this means here is I'm back on the nice.fas server. 806 00:33:48,906 ---> 00:33:50,946 If I kind of get a little confused sometime 807 00:33:50,946 ---> 00:33:55,596 and I type PWD it will tell me the full path to where I am 808 00:33:55,596 ---> 00:33:58,446 on the server, so right now I'm logged in as Malan 809 00:33:58,486 ---> 00:34:01,096 and Malan does not have access to everything on the server 810 00:34:01,096 ---> 00:34:04,156 because there's like 20,000 other people with fas accounts. 811 00:34:04,486 ---> 00:34:07,096 So in fact, fas took the approach years ago, 812 00:34:07,096 ---> 00:34:08,066 very intelligently, 813 00:34:08,256 ---> 00:34:10,196 of organizing things hierarchically. 814 00:34:10,456 ---> 00:34:11,936 So notice there's a directory, 815 00:34:11,976 ---> 00:34:14,716 the root of the hard drive apparently has a directory 816 00:34:14,716 ---> 00:34:17,616 in it called NFS, network file system, home, 817 00:34:17,616 ---> 00:34:19,826 which is where everyone's home directories live. 818 00:34:19,826 ---> 00:34:22,066 And a home directory is just your personal storage space. 819 00:34:22,586 ---> 00:34:25,756 M/A just looks like there's a whole hierarchy of names there 820 00:34:25,756 ---> 00:34:28,536 to keep things clean, slash CS-50, this is the full path 821 00:34:28,866 ---> 00:34:32,516 to where I am, and realize that I can definitely go poking 822 00:34:32,516 ---> 00:34:33,616 around if I get curious. 823 00:34:33,616 ---> 00:34:34,716 This is not a bad thing. 824 00:34:34,716 ---> 00:34:39,216 If I type CD DOT DOT it doesn't go this way it goes backwards 825 00:34:39,216 ---> 00:34:41,846 so to speak or higher up in this tree, so now notice I'm back 826 00:34:41,956 ---> 00:34:43,326 in my so-called home directory. 827 00:34:43,676 ---> 00:34:46,126 Where am I going to be if I do it once more? 828 00:34:47,496 ---> 00:34:49,316 Yeah, the directory called A. So with all 829 00:34:49,316 ---> 00:34:52,976 of the people whose usernames start with MA, 830 00:34:52,976 ---> 00:34:56,616 which if I do CD it's getting a little curious here. 831 00:34:56,866 ---> 00:34:59,186 Maybe will this backfire? 832 00:34:59,456 ---> 00:35:01,546 Maybe if we could put the camera down here for just a moment 833 00:35:01,546 ---> 00:35:03,306 so that we're not podcasting this to the Internet, 834 00:35:03,556 ---> 00:35:07,056 these are all of the people on fas.harvard.edu whose names, 835 00:35:07,146 ---> 00:35:10,206 sorry folks at home can't see this, that begin with MA. 836 00:35:10,406 ---> 00:35:14,066 If we go back one more directory keep the camera underwhelming 837 00:35:14,216 ---> 00:35:15,796 down here, there's the whole alphabet. 838 00:35:16,036 ---> 00:35:17,376 We can see people that start 839 00:35:17,376 ---> 00:35:22,326 with probably no MZs I'm guessing, CDZ, LS, oh there are. 840 00:35:22,326 ---> 00:35:25,026 So it's not a full name, it's first letter 841 00:35:25,136 ---> 00:35:26,826 of the first name, last name. 842 00:35:26,826 ---> 00:35:29,646 So again you can poke around all you want and see a whole bunch 843 00:35:29,646 ---> 00:35:31,366 of stuff on this computer, but Linux, 844 00:35:31,366 ---> 00:35:33,626 like most operating systems, has various permissions 845 00:35:33,626 ---> 00:35:37,086 and fancy control features so you really can't do bad things. 846 00:35:37,086 ---> 00:35:39,726 you can't access ,for instance, if I get nosy, 847 00:35:39,726 ---> 00:35:44,496 let's say home/a/y idida 848 00:35:44,496 ---> 00:35:46,006 for Jansue's [sounds like] home directory. 849 00:35:46,816 ---> 00:35:55,346 Let me, I'm forgetting the day, so I'm in it. 850 00:35:55,796 ---> 00:35:58,086 Thankfully, permission denied. 851 00:35:58,086 ---> 00:36:00,076 So things are, in fact, configured correctly. 852 00:36:00,306 ---> 00:36:01,826 So again just random little trivia 853 00:36:01,826 ---> 00:36:03,246 that ultimately gets useful 854 00:36:03,246 ---> 00:36:05,276 because just understanding the world 855 00:36:05,276 ---> 00:36:07,366 in which you're playing I think lets you get yourself 856 00:36:07,366 ---> 00:36:09,786 out of problems and actually solve more interesting ones. 857 00:36:10,026 ---> 00:36:11,746 So there are some other commands that we'll see, 858 00:36:11,746 ---> 00:36:14,176 and again every piece that's pdf will walk you 859 00:36:14,236 ---> 00:36:15,366 through any of this minutiae. 860 00:36:15,576 ---> 00:36:17,066 You're not going to have to figure it out or figure 861 00:36:17,066 ---> 00:36:20,446 out from week 1 what the heck was the command for this. 862 00:36:20,446 ---> 00:36:23,846 So we're going to see things like GCC, GDB, 863 00:36:24,046 ---> 00:36:26,996 commands like more and then someone got clever. 864 00:36:26,996 ---> 00:36:28,076 More is just a program 865 00:36:28,076 ---> 00:36:29,686 that shows you the contents of a file. 866 00:36:29,906 ---> 00:36:31,606 Well, someone wanted to be clever and kind of one 867 00:36:31,606 ---> 00:36:33,306 up that person so they'd called their program less 868 00:36:33,626 ---> 00:36:35,366 but it does really the same thing. 869 00:36:35,686 ---> 00:36:39,056 Man for manual, which is something you'll get familiar 870 00:36:39,056 ---> 00:36:39,936 with as well. 871 00:36:40,176 ---> 00:36:42,576 So what does it take to write a program, so it's three steps 872 00:36:42,576 ---> 00:36:43,346 and we've done it twice. 873 00:36:43,346 ---> 00:36:46,546 Last week and this, one, open up a text editor like nano 874 00:36:46,676 ---> 00:36:49,046 in a Linux environment and call the file whatever, 875 00:36:49,146 ---> 00:36:53,866 hi1.c or hello1.c, using the dot C convention for namesake. 876 00:36:54,236 ---> 00:36:56,806 GCC the name of the file and then you run the thing 877 00:36:56,886 ---> 00:36:59,516 by simply running the default name. 878 00:36:59,906 ---> 00:37:02,046 Well, what if we want to do something more interesting, 879 00:37:02,046 ---> 00:37:04,866 give it a non-stupid name like a.out? 880 00:37:05,226 ---> 00:37:07,636 So there are again these things called command line arguments 881 00:37:07,676 ---> 00:37:10,736 or switches with which you can control the behavior 882 00:37:10,736 ---> 00:37:12,586 of code more interestingly. 883 00:37:12,586 ---> 00:37:15,736 So if I go back over here into my CS-50 account, 884 00:37:16,166 ---> 00:37:19,526 my CS-50 directory, here's hello.c. my goal now is 885 00:37:19,526 ---> 00:37:22,416 to create a more interesting name than a.out, 886 00:37:22,776 ---> 00:37:25,856 so per that previous slide I say dash 0 for output. 887 00:37:26,156 ---> 00:37:27,816 Let's call the program hello 888 00:37:28,026 ---> 00:37:31,286 and then let's say compile this particular program, LS. 889 00:37:31,286 ---> 00:37:35,346 Now I have a binary, 0s and 1s called hello up. 890 00:37:35,726 ---> 00:37:39,796 So you'll notice little keystrokes that save you time. 891 00:37:39,796 ---> 00:37:42,526 If you hit tab it will do auto complete which saves you time. 892 00:37:42,776 ---> 00:37:44,766 Unfortunately it also reveals all the other commands 893 00:37:44,766 ---> 00:37:47,926 on the server which start with he, in this case head, hello, 894 00:37:47,926 ---> 00:37:50,316 help desk, help desk server, help z tags and [inaudible], 895 00:37:50,546 ---> 00:37:54,636 none of which I care about so hello, enter, 896 00:37:54,766 ---> 00:37:56,996 same exact program even though it's called something 897 00:37:56,996 ---> 00:37:57,606 different still. 898 00:37:57,606 ---> 00:38:00,366 So more interesting but again just a fundamentally 899 00:38:00,366 ---> 00:38:01,586 uninteresting building block. 900 00:38:01,736 ---> 00:38:04,056 But your programs are going to get more complicated over time. 901 00:38:04,296 ---> 00:38:06,846 Most of your programs by semester's end are not going 902 00:38:06,846 ---> 00:38:09,456 to be this big in a simple text file, 903 00:38:09,736 ---> 00:38:11,196 they're going to be multiple files. 904 00:38:11,196 ---> 00:38:12,896 And they're not going to be terribly long each, 905 00:38:12,896 ---> 00:38:14,936 but we'll see one of the principals we'll preach 906 00:38:14,936 ---> 00:38:17,316 in the course is design, and good design, 907 00:38:17,546 ---> 00:38:20,116 and as some of you may have experienced in scratch already, 908 00:38:20,746 ---> 00:38:23,266 if you're implementing a script that's kind of this big 909 00:38:23,266 ---> 00:38:25,106 and then you add some more features and then it's this big 910 00:38:25,106 ---> 00:38:27,566 and then this big and this big and God forbid this wide 911 00:38:27,566 ---> 00:38:30,646 and wide and wide because you keep adding ifs and its and ifs 912 00:38:30,646 ---> 00:38:33,706 and its you're probably crossing the boundary at some point 913 00:38:33,706 ---> 00:38:35,596 of what we'd call good design. 914 00:38:35,596 ---> 00:38:37,486 If you're scrolling up, down, left right just 915 00:38:37,486 ---> 00:38:40,416 to see your own program there's probably a better approach. 916 00:38:40,416 ---> 00:38:43,256 Now for PSET 0, they don't need to worry so much about this 917 00:38:43,256 ---> 00:38:45,336 because we haven't really told you what good design is, 918 00:38:45,636 ---> 00:38:48,596 but among the features will be chopping up your code 919 00:38:48,596 ---> 00:38:50,936 into much more manageable bite-sized pieces, 920 00:38:50,986 ---> 00:38:53,146 putting it in separate files, putting it in scratch 921 00:38:53,146 ---> 00:38:55,436 in separate scripts, and you'll see that a command 922 00:38:55,436 ---> 00:38:57,626 that will facilitate this is called make. 923 00:38:57,886 ---> 00:39:00,316 So you'll see both of these approaches in PSET 1, 924 00:39:00,566 ---> 00:39:03,766 simply typing Make and then the name of the program you want 925 00:39:03,826 ---> 00:39:05,876 to make, in this case, hello. 926 00:39:05,876 ---> 00:39:08,966 Notice I'm not typing DOT C, I'm saying make hello. 927 00:39:09,236 ---> 00:39:11,636 Make is a program that's going to infer, well, 928 00:39:11,636 ---> 00:39:13,806 if this guy wants to make a program called make it's 929 00:39:13,856 ---> 00:39:16,216 probably based on a file called make.c 930 00:39:16,476 ---> 00:39:20,566 so if I hit enter it tried to recompile it but it realized -- 931 00:39:20,566 ---> 00:39:22,646 and this is what's cool about make -- it realized, oh, 932 00:39:22,686 ---> 00:39:23,776 you haven't changed the program, 933 00:39:23,776 ---> 00:39:27,316 why should I waste my time regenerated identical 0s and 1s. 934 00:39:27,626 ---> 00:39:29,046 So in fact it didn't bother. 935 00:39:29,226 ---> 00:39:33,016 But if I do LS to see my files how do I remove hello and a.out? 936 00:39:34,146 ---> 00:39:37,916 Yeah, rm, hello, enter, it's going to ask for a sanity check. 937 00:39:37,916 ---> 00:39:39,786 Remove regular file hello question mark? 938 00:39:39,786 ---> 00:39:42,826 Yes. RM a.out, yes for that too. 939 00:39:42,826 ---> 00:39:46,426 So now I just have my source code, make hello enter, 940 00:39:46,646 ---> 00:39:49,496 so the command's going to look a little more cryptic because we, 941 00:39:49,496 ---> 00:39:51,206 just to get you started at term start, 942 00:39:51,296 ---> 00:39:53,086 are configuring these accounts in a way 943 00:39:53,086 ---> 00:39:54,666 that just makes some things easier 944 00:39:54,666 ---> 00:39:56,666 and we'll take those training wheels off over time, 945 00:39:56,936 ---> 00:39:58,806 but it achieves the same results because I now 946 00:39:58,806 ---> 00:40:02,296 in fact have a program called hello in that directory as well. 947 00:40:02,616 ---> 00:40:04,606 So what are these programs you can use? 948 00:40:04,606 ---> 00:40:07,686 So nano is by far the simplest editor you can use 949 00:40:07,686 ---> 00:40:08,786 at least at term start. 950 00:40:09,026 ---> 00:40:11,596 On my computer it doesn't support the delete feature 951 00:40:11,596 ---> 00:40:14,016 but we will fix that so that you don't need 952 00:40:14,016 ---> 00:40:17,486 to fail the very first time you make a program like I did, 953 00:40:17,746 ---> 00:40:19,116 but there are other tools out there. 954 00:40:19,116 ---> 00:40:21,646 Those of you might know programs like VIM and in fact most 955 00:40:21,646 ---> 00:40:24,446 of the time I'll use a program called VIM just 956 00:40:24,446 ---> 00:40:26,516 because that's what I was taught in CS-50 years ago. 957 00:40:26,516 ---> 00:40:27,556 I kind of grew up on it. 958 00:40:27,766 ---> 00:40:30,816 EMAX is another popular one, but more on this in the future. 959 00:40:30,816 ---> 00:40:33,446 I mention it now only so that you don't get confused 960 00:40:33,446 ---> 00:40:35,776 or puzzled when you see this TF used this program, 961 00:40:35,936 ---> 00:40:36,736 this TF used another. 962 00:40:37,016 ---> 00:40:40,126 It's just like using pages versus Microsoft Word 963 00:40:40,126 ---> 00:40:42,936 versus Word Pad, it's all the same kind of stuff 964 00:40:42,936 ---> 00:40:43,946 in this world as well. 965 00:40:44,256 ---> 00:40:45,366 So in a moment we're going 966 00:40:45,436 ---> 00:40:47,236 to actually tease apart what it means 967 00:40:47,236 ---> 00:40:49,626 to write a more interesting program than hello world. 968 00:40:49,626 ---> 00:40:52,756 The basic building block for almost any C program is going 969 00:40:52,756 ---> 00:40:55,766 to start with this construct here, but why don't we go ahead 970 00:40:55,766 ---> 00:40:56,896 since this is a lot to digest, 971 00:40:57,096 ---> 00:40:58,326 and take a five minute break first. 972 00:41:05,486 ---> 00:41:06,456 All right. 973 00:41:06,876 ---> 00:41:08,826 We are back, so Yuki [assumed spelling] 974 00:41:08,826 ---> 00:41:09,966 and Jantsu [assumed spelling] informed me 975 00:41:09,966 ---> 00:41:12,186 that sectioning will indeed debut this evening, 976 00:41:12,186 ---> 00:41:13,846 go to CS-50.net, take a look 977 00:41:13,846 ---> 00:41:15,966 at any instructions you'll find on the home page. 978 00:41:15,966 ---> 00:41:18,076 Check around midnight or certainly by morning, 979 00:41:18,416 ---> 00:41:21,026 and note this, so per last week's announcements 980 00:41:21,026 ---> 00:41:23,826 in the syllabus we do offer three tracks of sections, 981 00:41:23,826 ---> 00:41:27,146 just like First Nights does for our students who are more 982 00:41:27,146 ---> 00:41:29,566 and less comfortable with music, so we do the same. 983 00:41:29,566 ---> 00:41:32,136 It's just the idea of being in CS-50, 984 00:41:32,386 ---> 00:41:34,106 so there's no steadfast rule 985 00:41:34,106 ---> 00:41:36,056 as to what makes you less comfortable, 986 00:41:36,056 ---> 00:41:37,316 what makes you more comfortable 987 00:41:37,316 ---> 00:41:38,746 and what makes you somewhere in between. 988 00:41:39,036 ---> 00:41:41,036 To be honest, it's a term you can probably just slap 989 00:41:41,036 ---> 00:41:43,606 on yourself based on just how comfortable you actually are 990 00:41:43,826 ---> 00:41:46,396 having returned this week to CS-50. 991 00:41:46,396 ---> 00:41:48,616 You've seen what the percentage break-downs are so we get plenty 992 00:41:48,616 ---> 00:41:49,776 of folks from each of the buckets, 993 00:41:50,036 ---> 00:41:51,946 so when you use fas's sectioning tool, 994 00:41:52,226 ---> 00:41:54,346 realize that because we have these three different types 995 00:41:54,346 ---> 00:41:56,206 of sections you're going to see a long list 996 00:41:56,206 ---> 00:41:57,616 of about 30 sections, since we have 997 00:41:57,616 ---> 00:41:59,756 about 30 teaching fellows and thus 30 sections. 998 00:41:59,976 ---> 00:42:01,336 and in the comments fields 999 00:42:01,336 ---> 00:42:03,116 of the sectioning program you'll see 1000 00:42:03,116 ---> 00:42:05,086 if that section slot has been designated 1001 00:42:05,086 ---> 00:42:06,946 for those more comfortable, those less 1002 00:42:07,246 ---> 00:42:08,586 or those somewhere in between. 1003 00:42:08,846 ---> 00:42:09,776 If you're on the fence as to 1004 00:42:09,776 ---> 00:42:12,476 where you belong you cannot go wrong being 1005 00:42:12,476 ---> 00:42:14,016 in the somewhere in between section. 1006 00:42:14,016 ---> 00:42:16,146 That's really for folks that just don't feel themselves 1007 00:42:16,146 ---> 00:42:19,256 at either extreme, but certainly feel free to chat with me or any 1008 00:42:19,256 ---> 00:42:20,576 of the TFs if you're curious 1009 00:42:20,576 ---> 00:42:23,336 as to what this all means or where you belong. 1010 00:42:23,336 ---> 00:42:25,206 Now what does this mean in real terms for sections? 1011 00:42:25,896 ---> 00:42:27,486 Frankly it just means that especially 1012 00:42:27,486 ---> 00:42:29,906 for those less comfortable, it just means you're going to be 1013 00:42:29,906 ---> 00:42:33,406 in a room full of people who feel as apprehensive 1014 00:42:33,406 ---> 00:42:36,656 or as inexperienced as you and it's sort of a safer space 1015 00:42:36,656 ---> 00:42:37,826 in which to put up your hand 1016 00:42:37,826 ---> 00:42:40,416 and ask a question you think is the proverbial dumb question. 1017 00:42:40,716 ---> 00:42:43,106 The sections for those more comfortable are meant 1018 00:42:43,106 ---> 00:42:45,956 to digress intentionally from say the course's syllabus, 1019 00:42:46,266 ---> 00:42:48,196 from the week's problem set and really go off 1020 00:42:48,196 ---> 00:42:50,336 in any interesting technical direction that some 1021 00:42:50,336 ---> 00:42:51,716 of the savvier students might 1022 00:42:51,716 ---> 00:42:54,186 like to explore even though it's not going to help them per se 1023 00:42:54,356 ---> 00:42:55,896 on that particular week's problem set. 1024 00:42:56,036 ---> 00:42:58,296 But again if you don't feel yourself needing that sort 1025 00:42:58,296 ---> 00:43:00,866 of comfort zone here or you don't really think 1026 00:43:00,916 ---> 00:43:02,786 that you'd want to try holding your own with some 1027 00:43:02,786 ---> 00:43:04,836 of those more comfortable then the sweet spot, 1028 00:43:04,836 ---> 00:43:06,776 just like the standard editions for most students is 1029 00:43:06,776 ---> 00:43:08,886 that for those somewhere in between. 1030 00:43:09,276 ---> 00:43:12,246 All right, so let's do something a little more interesting now. 1031 00:43:12,246 ---> 00:43:15,206 I've gone ahead and per the handout you have I have a whole 1032 00:43:15,206 ---> 00:43:17,456 bunch of code already in this account. 1033 00:43:17,456 ---> 00:43:19,246 So I'm going to go back to my screen here 1034 00:43:19,476 ---> 00:43:21,486 and you'll see again commands lli this on PSETs. 1035 00:43:21,486 ---> 00:43:26,066 What I'm going to do for a moment is copy, tilda, CS-50, 1036 00:43:26,276 ---> 00:43:27,956 so henceforth the Tilda character 1037 00:43:27,956 ---> 00:43:29,306 which on most U.S. keyboards is 1038 00:43:29,306 ---> 00:43:33,336 on the top left near the escape key, Tilda CS-50 means go 1039 00:43:33,336 ---> 00:43:35,946 into CS-50's home directory. 1040 00:43:36,116 ---> 00:43:37,996 Now I'm in Malan's home directory at the moment, 1041 00:43:37,996 ---> 00:43:41,916 so go into CS-50 is just like I tried to idida's home directory, 1042 00:43:41,916 ---> 00:43:43,976 so go into CS-50's and then I know 1043 00:43:43,976 ---> 00:43:46,226 because I'm reading this off of a pdf or whatever 1044 00:43:46,426 ---> 00:43:49,526 that there's a pub directory, that there's a source, src, 1045 00:43:49,526 ---> 00:43:51,996 again, nicknames in Linux, source directory, 1046 00:43:51,996 ---> 00:43:54,536 and then I know there's a lectures directory in there 1047 00:43:54,536 ---> 00:43:55,706 and then we're in week 1. 1048 00:43:55,706 ---> 00:43:56,966 So it turns out there is, in fact, 1049 00:43:56,966 ---> 00:44:00,626 a directory called 1 inside of lectures, inside of source, 1050 00:44:00,626 ---> 00:44:04,086 inside of pub, inside of CS-50's home directory and my goal is 1051 00:44:04,086 ---> 00:44:08,346 to copy it from there to here because I, now a student 1052 00:44:08,346 ---> 00:44:11,146 in the class, decide that I want to push ahead, I want to do this 1053 00:44:11,146 ---> 00:44:12,406 after class and play around. 1054 00:44:12,406 ---> 00:44:14,106 I want to copy David's source code 1055 00:44:14,106 ---> 00:44:16,176 into my own account just so I can play. 1056 00:44:16,176 ---> 00:44:20,516 Perfectly fine, notice that DOT represents this directory just 1057 00:44:20,516 ---> 00:44:22,946 as DOT DOT represents the parent directory. 1058 00:44:23,156 ---> 00:44:24,966 So if hit enter this is actually flawed, 1059 00:44:25,246 ---> 00:44:27,536 notice that something bad happened and then 1060 00:44:27,536 ---> 00:44:29,726 if I type LS I didn't get any files. 1061 00:44:29,966 ---> 00:44:33,286 So realize, too, sort of FAQ early on is if you want 1062 00:44:33,286 ---> 00:44:37,396 to copy a directory with CP you need to do it recursively, 1063 00:44:37,436 ---> 00:44:39,656 which is a keyword that we'll revisit in the term, 1064 00:44:39,656 ---> 00:44:41,596 it means to do it not only to that directory 1065 00:44:41,846 ---> 00:44:45,166 but all the files inside and all the subdirectories inside again 1066 00:44:45,166 ---> 00:44:46,856 and again and again until you bottom out. 1067 00:44:46,856 ---> 00:44:49,846 So CP-R means copy the file recursively 1068 00:44:50,146 ---> 00:44:52,836 into my current directory, enter, took a second 1069 00:44:52,836 ---> 00:44:54,256 because there are, in fact, a bunch of files. 1070 00:44:54,546 ---> 00:44:57,706 In fact if I go into my one directory now here are some 1071 00:44:57,706 ---> 00:45:00,056 examples that I've pre-prepared so that we can go off 1072 00:45:00,056 ---> 00:45:01,686 in a known direction here. 1073 00:45:01,926 ---> 00:45:06,976 So one direction we already went in was hi-1.c. but your version, 1074 00:45:06,976 ---> 00:45:08,826 on paper looks a little different from the quick 1075 00:45:08,826 ---> 00:45:10,566 and dirty thing I've been whipping up. 1076 00:45:10,676 ---> 00:45:14,086 there's a lot more purple on the screen here, so all the stuff 1077 00:45:14,086 ---> 00:45:17,796 at top is fundamentally irrelevant to the computer, 1078 00:45:17,916 ---> 00:45:20,606 but just like scratch does if you've discovered it yet, 1079 00:45:20,846 ---> 00:45:22,466 lets you put little comments here and there, 1080 00:45:22,466 ---> 00:45:24,376 sort of sticky notes, so to speak, post-it notes, 1081 00:45:24,646 ---> 00:45:26,936 so does C in most any high-level language. 1082 00:45:27,156 ---> 00:45:29,086 and by high-level language henceforth I mean something 1083 00:45:29,086 ---> 00:45:30,626 that's not 0s and 1s, 1084 00:45:30,626 ---> 00:45:32,066 not something called assembly language. 1085 00:45:32,386 ---> 00:45:33,846 It means ignore all 1086 00:45:33,846 ---> 00:45:36,246 of the following this is useful only to me the human. 1087 00:45:36,606 ---> 00:45:39,466 So in C the means by which you write a comment is 1088 00:45:39,496 ---> 00:45:43,926 to do a forward slash star and that means anything after that 1089 00:45:43,926 ---> 00:45:47,556 on this line or below will be ignored by the compiler, GCC, 1090 00:45:47,726 ---> 00:45:50,976 and the comment stops when you hit the opposite 1091 00:45:50,976 ---> 00:45:52,286 of that, star slash. 1092 00:45:52,916 ---> 00:45:56,196 So the fact that I actually have all of these stars and I've kind 1093 00:45:56,196 ---> 00:45:59,206 of boxed things in is really just some OCD on my part. 1094 00:45:59,206 ---> 00:46:02,036 It's not technical I just wanted to make a nice pretty purple box 1095 00:46:02,246 ---> 00:46:03,466 to box in all of my code. 1096 00:46:03,676 ---> 00:46:07,416 But one key takeaway is that I made sure that this line 1097 00:46:07,566 ---> 00:46:10,666 of stars and the slashes does not exceed the width 1098 00:46:10,666 ---> 00:46:11,346 of my window. 1099 00:46:11,566 ---> 00:46:13,866 So another lesson that's important early on is 1100 00:46:13,866 ---> 00:46:15,876 to make sure your -- the code you start writing is 1101 00:46:16,186 ---> 00:46:17,296 pretty printed. 1102 00:46:17,516 ---> 00:46:19,746 And this just means it actually displays nicely 1103 00:46:19,746 ---> 00:46:20,906 on a typical window. 1104 00:46:21,126 ---> 00:46:24,136 In real terms this means you should never at least 1105 00:46:24,186 ---> 00:46:27,546 for the C part of this course, have a line of text 1106 00:46:27,606 ---> 00:46:29,556 that is longer than 80 characters long. 1107 00:46:29,776 ---> 00:46:32,676 And even that's pushing it, 76 is kind of the geek's rule 1108 00:46:32,676 ---> 00:46:34,346 of thumb as to how long a line should be. 1109 00:46:34,646 ---> 00:46:37,316 How do you know what's 76 or what's 80 characters? 1110 00:46:37,486 ---> 00:46:41,446 Well, right now my window has essentially been expanded to be 1111 00:46:41,636 ---> 00:46:45,506 as long as I wanted it to be, but you get into the habit 1112 00:46:45,666 ---> 00:46:48,276 of knowing when to hit enter to move things onto the next line 1113 00:46:48,476 ---> 00:46:52,356 and nano and programs like it will actually force your lines 1114 00:46:52,356 ---> 00:46:54,276 to wrap if they start to get long. 1115 00:46:54,596 ---> 00:46:56,036 And this is just a matter of good style, 1116 00:46:56,066 ---> 00:46:58,956 because as you could imagine, if I wrote my whole program 1117 00:46:58,956 ---> 00:47:00,946 on just one line it would still work, 1118 00:47:00,946 ---> 00:47:04,546 the computer would not care, but you the humans probably would. 1119 00:47:04,546 ---> 00:47:07,426 In fact, this is hi1.c, let me go ahead 1120 00:47:07,426 ---> 00:47:09,036 and delete all that stuff. 1121 00:47:09,086 ---> 00:47:11,016 I'm going to go ahead and do this, you know, this, 1122 00:47:11,136 ---> 00:47:13,096 I really don't need that there. 1123 00:47:13,096 ---> 00:47:15,016 Let me go ahead and put that there. 1124 00:47:15,016 ---> 00:47:16,946 Right, if I'd given you a printout of this, I mean, 1125 00:47:16,946 ---> 00:47:20,666 rightly so would things start to look pretty damn complicated. 1126 00:47:20,666 ---> 00:47:22,216 Now some of these spaces are important 1127 00:47:22,216 ---> 00:47:23,646 so that you can distinguish what's ints 1128 00:47:23,646 ---> 00:47:26,206 and what's [inaudible], but if I go ahead and save this 1129 00:47:26,206 ---> 00:47:31,796 with control X, hit Y and now go ahead and run GCC of hi1.c, 1130 00:47:32,016 ---> 00:47:33,796 the program thankfully still compiles. 1131 00:47:33,796 ---> 00:47:36,636 I still have eight odd out, so it still works, this version, 1132 00:47:36,906 ---> 00:47:39,256 but it's certainly not very pretty printed 1133 00:47:39,256 ---> 00:47:40,396 and not very legible. 1134 00:47:40,396 ---> 00:47:42,016 So what else is inside of this file? 1135 00:47:42,256 ---> 00:47:44,676 Well, hmm, I didn't really want to do that. 1136 00:47:44,676 ---> 00:47:48,886 Let me go ahead and go back into CS-50, Pub source lectures 1, 1137 00:47:48,886 ---> 00:47:50,196 let me just get a new copy of this. 1138 00:47:50,546 ---> 00:47:53,856 So now it's going to prompt me, wait a minute, overwrite hi1.c, 1139 00:47:54,016 ---> 00:47:55,366 little sanity check, say yes. 1140 00:47:55,466 ---> 00:47:56,856 So now I have the prettier version back. 1141 00:47:56,856 ---> 00:47:59,776 I'm going to start using a program called VI or VIM just 1142 00:47:59,776 ---> 00:48:01,936 because frankly I'm more comfortable with it than nano, 1143 00:48:01,936 ---> 00:48:05,266 even though nano is simpler, do as I say not as I do. 1144 00:48:05,666 ---> 00:48:09,526 Hi1.c, still kind of pretty prints everything 1145 00:48:09,526 ---> 00:48:10,356 and color codes it. 1146 00:48:10,576 ---> 00:48:13,136 And the color is actually a feature that you don't get 1147 00:48:13,136 ---> 00:48:16,596 if you trying writing your first programs with notebad.exe 1148 00:48:16,846 ---> 00:48:19,236 or other similar client side programs. 1149 00:48:19,476 ---> 00:48:22,266 It's just making some inferences about the different roles 1150 00:48:22,266 ---> 00:48:23,646 that some of these keywords play, 1151 00:48:23,646 ---> 00:48:25,936 so I didn't write the word int in green. 1152 00:48:26,196 ---> 00:48:29,096 I didn't write the word char in green, it just knows 1153 00:48:29,136 ---> 00:48:31,076 that that represents the type of a variable. 1154 00:48:31,076 ---> 00:48:33,016 Let me draw David's attention to it 1155 00:48:33,056 ---> 00:48:34,656 with this default color scheme. 1156 00:48:34,986 ---> 00:48:36,316 So what's interesting here? 1157 00:48:36,396 ---> 00:48:38,916 Well, this stuff at the top is all comments, 1158 00:48:38,916 ---> 00:48:40,946 and actually now we get to use a fun little toy. 1159 00:48:40,946 ---> 00:48:43,106 this stuff up here is all comments. 1160 00:48:43,236 ---> 00:48:43,696 All right. 1161 00:48:43,696 ---> 00:48:45,846 So it's fundamentally irrelevant but useful 1162 00:48:45,846 ---> 00:48:47,196 for documenting your code. 1163 00:48:47,496 ---> 00:48:48,896 This things here I'm going 1164 00:48:48,896 ---> 00:48:52,016 to briefly call today a pre-processor directive, 1165 00:48:52,366 ---> 00:48:55,416 which is just a fancy way of saying tell the compiler 1166 00:48:55,616 ---> 00:48:57,586 to go get the contents of some other file 1167 00:48:57,706 ---> 00:48:59,246 that often someone else wrote. 1168 00:48:59,576 ---> 00:49:02,966 So what we don't have here is any kind 1169 00:49:02,966 ---> 00:49:05,186 of specification of what Print F does. 1170 00:49:05,246 ---> 00:49:06,646 Like who wrote Print F? 1171 00:49:06,746 ---> 00:49:10,096 I wrote this program called main, but who wrote Print F? 1172 00:49:10,096 ---> 00:49:11,716 Right, I'm kind of cutting corners here. 1173 00:49:11,986 ---> 00:49:15,566 Well, it turns out that when Linux shipped with the compiler, 1174 00:49:15,806 ---> 00:49:18,086 someone else, some really smart people spent a lot 1175 00:49:18,086 ---> 00:49:21,706 of time getting this very basic important function implemented 1176 00:49:21,706 ---> 00:49:23,206 in their own source code. 1177 00:49:23,446 ---> 00:49:29,156 So by saying sharp include open angle bracket standard io.h 1178 00:49:29,246 ---> 00:49:32,056 closed bracket, that's just telling GCC, you know what, 1179 00:49:32,186 ---> 00:49:34,876 the following code has some functions, some programs 1180 00:49:35,106 ---> 00:49:36,066 that other people wrote. 1181 00:49:36,296 ---> 00:49:37,976 Those programs happen to be inside 1182 00:49:37,976 ---> 00:49:41,666 of a file called standardio.h. please go grab the contents, 1183 00:49:41,926 ---> 00:49:44,676 paste them in here so that I don't have to do it myself. 1184 00:49:44,976 ---> 00:49:47,136 So I'm kind of oversimplifying there 1185 00:49:47,136 ---> 00:49:50,356 because in real terms what we're using here is a library. 1186 00:49:50,426 ---> 00:49:53,886 So a library of code henceforth is just a bunch of code 1187 00:49:53,886 ---> 00:49:56,976 that someone else wrote or maybe you wrote but that you factored 1188 00:49:56,976 ---> 00:49:59,336 out into separate files so that you don't have to copy 1189 00:49:59,336 ---> 00:50:01,426 and paste it into every future program you write. 1190 00:50:01,836 ---> 00:50:04,566 To technically my white lie I just told is 1191 00:50:04,566 ---> 00:50:06,926 that these program Print F these functions, 1192 00:50:06,926 ---> 00:50:09,706 actually as they should really be called, are not actually 1193 00:50:09,736 ---> 00:50:11,746 in that file, standardio.h, 1194 00:50:12,406 ---> 00:50:15,646 inside of that file will eventually see is just a minimal 1195 00:50:15,646 ---> 00:50:18,026 amount of information, essentially a summary of all 1196 00:50:18,026 ---> 00:50:20,636 of the functions in that file because as you might imagine, 1197 00:50:20,636 ---> 00:50:23,806 where is the actual implementation, the actual lines 1198 00:50:23,806 ---> 00:50:25,346 of code that implement Print F? 1199 00:50:25,386 ---> 00:50:28,726 What would the file name probably be called? 1200 00:50:29,496 ---> 00:50:33,336 Standardio.c, so in fact, yeah, there is a file out somewhere 1201 00:50:33,336 ---> 00:50:35,486 on the system called standardio.c 1202 00:50:35,486 ---> 00:50:38,206 that someone else implemented but there's a lot of lines 1203 00:50:38,206 ---> 00:50:40,256 of code in there, a normal human doesn't want to read through it 1204 00:50:40,256 ---> 00:50:43,016 so we have these header files which are called .h files 1205 00:50:43,266 ---> 00:50:45,446 and then c files which have actual code 1206 00:50:45,636 ---> 00:50:48,196 and initially I'm not making any header files myself, 1207 00:50:48,196 ---> 00:50:50,126 we'll get to that eventually but just realize, 1208 00:50:50,126 ---> 00:50:52,466 some of these minor tripping points early on are just 1209 00:50:52,466 ---> 00:50:54,826 about keeping programs relatively clean, so this, 1210 00:50:54,876 ---> 00:50:58,416 to be short, simply says, go get the contents of this file 1211 00:50:58,466 ---> 00:51:00,646 so that I can use functions like Print F 1212 00:51:01,216 ---> 00:51:02,476 and henceforth let me clarify. 1213 00:51:02,816 ---> 00:51:06,256 So I am writing a program, this program is arbitrarily going 1214 00:51:06,256 ---> 00:51:09,896 to be called hello, but I can override that as we've seen 1215 00:51:09,896 ---> 00:51:11,866 with the -o flag from GCC 1216 00:51:12,266 ---> 00:51:15,256 so programs are made up of functions. 1217 00:51:15,256 ---> 00:51:17,676 one or more functions and these are kind 1218 00:51:17,676 ---> 00:51:19,626 of like miniature programs just 1219 00:51:19,626 ---> 00:51:21,856 like your scratch projects are probably made 1220 00:51:21,856 ---> 00:51:23,316 up of multiple scripts. 1221 00:51:23,616 ---> 00:51:27,036 A script is a function so a function is just a piece of code 1222 00:51:27,036 ---> 00:51:29,456 that does something and as you can have multiple scripts 1223 00:51:29,456 ---> 00:51:32,506 in scratch you can have multiple functions in c and just 1224 00:51:32,506 ---> 00:51:35,866 as the default block or script and scratch is 1225 00:51:35,866 ---> 00:51:37,636 that thing called wingreenflag clicked, 1226 00:51:38,066 ---> 00:51:40,516 so in c is the default script, 1227 00:51:40,516 ---> 00:51:42,456 the default function called main. 1228 00:51:42,886 ---> 00:51:46,006 Now why, if its just called main do I need to write something 1229 00:51:46,006 ---> 00:51:49,906 like int and then ints and then RGC, char or this asterisks, 1230 00:51:49,906 ---> 00:51:52,836 this RGV this bracket notation, there's just a lot of crap 1231 00:51:52,836 ---> 00:51:55,156 at the beginning of a program when you're writing it in c, 1232 00:51:55,316 ---> 00:51:57,156 those of you who took app computer science know 1233 00:51:57,156 ---> 00:52:00,546 that there's classfoo public static, voidmain, its even worse 1234 00:52:00,596 ---> 00:52:02,406 in Java and frankly this is just 1235 00:52:02,406 ---> 00:52:06,456 because these languages were not designed with week 0 1236 00:52:06,456 ---> 00:52:07,916 of a programming class in mind. 1237 00:52:08,056 ---> 00:52:10,366 So again, its stupid stuff, you get past it quickly. 1238 00:52:10,546 ---> 00:52:13,006 For today we're going to wave our hands at details like that 1239 00:52:13,006 ---> 00:52:15,136 but to say for now it's a copy paste thing. 1240 00:52:15,136 ---> 00:52:18,166 To start writing a function you literally write int main 1241 00:52:18,166 ---> 00:52:20,926 and all the rest of that stuff but then the fun part 1242 00:52:20,926 ---> 00:52:23,996 or at least the intellectually interesting part happens 1243 00:52:23,996 ---> 00:52:25,536 in between the curly braces for now. 1244 00:52:25,996 ---> 00:52:28,406 So that's how I would go about printing this simple program. 1245 00:52:28,406 ---> 00:52:31,036 Let me actually take a look at a more interesting version, 1246 00:52:31,036 ---> 00:52:33,656 thought only slightly called high2.c 1247 00:52:33,656 ---> 00:52:35,586 and you have a printout of this. 1248 00:52:36,166 ---> 00:52:39,006 So what's the fundamental difference between version one 1249 00:52:39,116 ---> 00:52:41,356 and this one version two? 1250 00:52:41,956 ---> 00:52:44,926 Hopefully it jumps out at you. 1251 00:52:44,986 ---> 00:52:47,686 yeah, so if bit egotistically this jumps out at you 1252 00:52:47,686 ---> 00:52:50,656 that this program has my name immortalized in it, 1253 00:52:50,916 ---> 00:52:51,946 but how did I do this? 1254 00:52:51,946 ---> 00:52:53,816 Well it looks like I've got this additional line 1255 00:52:53,816 ---> 00:52:56,396 and so David is mentioned but what's going on? 1256 00:52:56,396 ---> 00:53:00,026 well even just using the jargon I started using earlier, string, 1257 00:53:00,246 ---> 00:53:01,586 because it comes before the name 1258 00:53:01,586 ---> 00:53:03,606 of a variable, means what precisely? 1259 00:53:03,606 ---> 00:53:06,706 Yeah, it's the type of variable. 1260 00:53:06,706 ---> 00:53:09,566 So the variable is called main, what's going to be inside 1261 00:53:09,566 ---> 00:53:11,626 of this variable, well its not going to be an int, 1262 00:53:11,626 ---> 00:53:14,346 because David is not an int, it's a string, it's a word, 1263 00:53:14,346 ---> 00:53:16,546 it's a sentence so I'm going to specify that the type 1264 00:53:16,546 ---> 00:53:18,046 of this variable is string, 1265 00:53:18,316 ---> 00:53:21,176 now the equal sign again is the assignment operator, 1266 00:53:21,176 ---> 00:53:23,446 it means put the value of the thing on the right 1267 00:53:23,596 ---> 00:53:26,096 on the inside the thing on the left so the value 1268 00:53:26,096 ---> 00:53:30,136 on the right is itself a string and in c as in many languages 1269 00:53:30,136 ---> 00:53:32,776 if you want to have a literal sequence 1270 00:53:32,776 ---> 00:53:34,586 of characters be treated as one unit, 1271 00:53:34,836 ---> 00:53:37,266 you put them in all double quotes. 1272 00:53:37,546 ---> 00:53:40,906 You can use single quotes but only around single characters, 1273 00:53:40,906 ---> 00:53:42,646 so those of you who know a little 1274 00:53:42,646 ---> 00:53:44,476 of html probably know it doesn't matter if you use single 1275 00:53:44,476 ---> 00:53:45,976 or double, it does matter. 1276 00:53:45,976 ---> 00:53:48,186 In many programming languages including c, 1277 00:53:48,396 ---> 00:53:50,176 so strings are double quotes. 1278 00:53:50,436 ---> 00:53:52,406 All right, then what do I do on the second line? 1279 00:53:52,646 ---> 00:53:54,766 Well, we've not quite seen this syntax 1280 00:53:55,466 ---> 00:53:58,366 but in laymen's term can someone explain what the percent s 1281 00:53:58,486 ---> 00:53:59,996 there represents? 1282 00:54:00,776 ---> 00:54:02,956 String. Right, really not that hard. 1283 00:54:02,956 ---> 00:54:06,716 Percent d happened to represent int, percent s represents string 1284 00:54:06,946 ---> 00:54:09,536 and so the thing after the comma means what do you want 1285 00:54:09,536 ---> 00:54:12,686 to substitute in for that formatting code, the percent s, 1286 00:54:13,176 ---> 00:54:14,506 put the name of the variable. 1287 00:54:14,556 ---> 00:54:20,206 All right, so more interesting, so GCC hi2.c enter, 1288 00:54:20,446 ---> 00:54:23,706 now I'm going to run a.out, ohi David 1289 00:54:23,786 ---> 00:54:26,906 but unfortunately its really not that interesting 1290 00:54:26,946 ---> 00:54:30,276 if your program is completely deterministic and always spits 1291 00:54:30,276 ---> 00:54:32,706 out the same result because really, I mean, 1292 00:54:32,706 ---> 00:54:35,066 there are so many easier ways to just say something like this 1293 00:54:35,066 ---> 00:54:38,366 than this set up so let's take a look at the third version 1294 00:54:38,366 ---> 00:54:40,246 of this which is a little more interesting 1295 00:54:40,246 ---> 00:54:42,746 and finally we get a compelling use for a keyboard, 1296 00:54:43,046 ---> 00:54:46,056 so this program differs for the comments up top 1297 00:54:46,206 ---> 00:54:48,296 in that there's a third line of code here 1298 00:54:48,586 ---> 00:54:51,396 that uses a new function, so we've been using Print F 1299 00:54:51,546 ---> 00:54:53,856 to send output, it turns out that you can also, 1300 00:54:54,176 ---> 00:54:56,056 nicely enough, get input from the user. 1301 00:54:56,296 ---> 00:54:58,896 So not by way of the mouse, at least in this context but by way 1302 00:54:58,896 ---> 00:55:00,906 of the keyboard and what's the new function, 1303 00:55:01,246 ---> 00:55:04,216 just to take an obvious guess here, get string. 1304 00:55:04,576 ---> 00:55:07,696 As the name implies, the function get strings purpose 1305 00:55:07,696 ---> 00:55:10,586 in life is to get a string from the user and do what 1306 00:55:10,586 ---> 00:55:12,856 with the result apparently? 1307 00:55:13,026 ---> 00:55:16,256 Store it. so functions we now see right away, 1308 00:55:16,256 ---> 00:55:19,536 have this ability not only to do something aesthetically, 1309 00:55:19,586 ---> 00:55:23,246 Print F has this so called side effect of displaying something 1310 00:55:23,246 ---> 00:55:26,516 on the screen but functions can also return a value 1311 00:55:26,516 ---> 00:55:29,236 so you can think of a function kind of as a black box 1312 00:55:29,456 ---> 00:55:32,316 that does some work and then can maybe hand you some answers 1313 00:55:32,496 ---> 00:55:33,926 and its up to you, the programmer, 1314 00:55:33,926 ---> 00:55:36,006 to decide if you're going to do anything with that answer. 1315 00:55:36,226 ---> 00:55:37,446 How do you do something with it? 1316 00:55:37,446 ---> 00:55:39,976 well one of the things you could do is keep the answer around. 1317 00:55:40,556 ---> 00:55:42,886 So by having string name on the left 1318 00:55:42,886 ---> 00:55:45,596 that says give me a variable called name, it's a type sting, 1319 00:55:45,596 ---> 00:55:47,586 what are you going to put inside of it? 1320 00:55:47,746 ---> 00:55:49,396 well, whatever the result is. 1321 00:55:49,476 ---> 00:55:52,066 The return value so to speak of the thing on the right 1322 00:55:52,166 ---> 00:55:54,246 so get string returns now a value. 1323 00:55:54,546 ---> 00:55:56,336 How does this all behave in reality? 1324 00:55:56,336 ---> 00:56:00,086 So GCC hi3.c, I'm getting a little tired of a.out, 1325 00:56:00,086 ---> 00:56:01,286 how do I give this a better name? 1326 00:56:02,656 ---> 00:56:07,276 yes, so -o for output, and then let's call it hi3 though I could 1327 00:56:07,276 ---> 00:56:13,726 call it anything I want, oh, oh, did I create a problem there? 1328 00:56:13,846 ---> 00:56:20,286 its kind of a coincidence, so still the same error, yeah, 1329 00:56:20,286 ---> 00:56:24,006 so correct, fancy correct answer so there's a problem here 1330 00:56:24,136 ---> 00:56:25,976 in that get string is a function, 1331 00:56:25,976 ---> 00:56:28,526 yes but it wasn't written by the original authors 1332 00:56:28,526 ---> 00:56:31,976 of the c programming language, in fact just getting input 1333 00:56:31,976 ---> 00:56:34,146 from the user in c is really a pain. 1334 00:56:34,146 ---> 00:56:36,596 You have to use a function among others called scanf 1335 00:56:36,596 ---> 00:56:39,006 and we'll use this eventually, there's other tricks too, 1336 00:56:39,126 ---> 00:56:41,196 but long story short, in week one of a course, 1337 00:56:41,196 ---> 00:56:44,566 especially this course, to complete a bunch of hurdles, 1338 00:56:44,566 ---> 00:56:47,076 you have to jump through as simple as get a character 1339 00:56:47,076 ---> 00:56:49,006 from the keyboard, even in Java, those of you familiar 1340 00:56:49,006 ---> 00:56:51,046 with the scanner class, even that is a pain 1341 00:56:51,046 ---> 00:56:53,766 to actually use I'd say in the first week of a class 1342 00:56:54,256 ---> 00:56:56,096 so notice what did I do in this file? 1343 00:56:56,316 ---> 00:56:58,946 I did one other thing besides mention get string. 1344 00:56:59,226 ---> 00:57:01,696 There's one other key difference outside of the comments. 1345 00:57:02,096 ---> 00:57:03,116 What else is new here? 1346 00:57:05,386 ---> 00:57:08,246 yeah, so I'm not just using the standard io library, 1347 00:57:08,276 ---> 00:57:12,086 it turns out that the cs50 staff a few years ago wrote our 1348 00:57:12,086 ---> 00:57:13,296 own library. 1349 00:57:13,466 ---> 00:57:15,486 And we wrote what that means, two files. 1350 00:57:15,486 ---> 00:57:18,526 One called cs50.h and one called cs50.c 1351 00:57:18,726 ---> 00:57:20,506 and in the latter we wrote a bunch of functions 1352 00:57:20,506 ---> 00:57:22,156 that we thought would be useful to students 1353 00:57:22,156 ---> 00:57:26,746 in the semester namely get int and get string and silly name, 1354 00:57:26,746 ---> 00:57:29,296 but get long long but other functions still 1355 00:57:29,476 ---> 00:57:32,236 that really simplify the process at least at the start 1356 00:57:32,236 ---> 00:57:34,556 of the semester of getting keyboard input from users 1357 00:57:34,826 ---> 00:57:37,826 but unfortunately its not sufficient just to tell GCC, 1358 00:57:37,886 ---> 00:57:41,466 hey make sure you go get a copy of the header file 1359 00:57:41,466 ---> 00:57:43,936 for cs50's library so I know that these functions exist, 1360 00:57:43,936 ---> 00:57:44,906 because I've done that. 1361 00:57:44,906 ---> 00:57:48,736 So one mistake might have been to omit this line together, 1362 00:57:48,736 ---> 00:57:49,636 that would in fact be bad. 1363 00:57:50,256 ---> 00:57:51,836 But that's not the only mistake that I made, 1364 00:57:51,836 ---> 00:57:54,116 I now need to tell cs50's library 1365 00:57:54,416 ---> 00:57:57,516 when you compile this you don't just get away 1366 00:57:57,516 ---> 00:58:01,676 with turning one file, hi3.c into 0's and 1's 1367 00:58:01,896 ---> 00:58:05,106 because you need access to one other file, called what? 1368 00:58:05,566 ---> 00:58:08,626 Yes, so cs50.c so there's a couple solutions here, 1369 00:58:08,626 ---> 00:58:10,436 I could email the core staff, and I could say, 1370 00:58:10,436 ---> 00:58:14,126 hey could I have a copy of cs50.c or they'll be happy 1371 00:58:14,126 ---> 00:58:16,066 to give it because there's no intellectual property there, 1372 00:58:16,066 ---> 00:58:19,276 its just standard c stuff, but that seems kind of wasteful 1373 00:58:19,276 ---> 00:58:21,326 if every time in the semester if you write a program you need 1374 00:58:21,326 ---> 00:58:23,556 to copy the R file, put it in your own directory, 1375 00:58:23,556 ---> 00:58:24,706 kind of bloat your own code, 1376 00:58:24,956 ---> 00:58:26,446 well in fact there's an easier way. 1377 00:58:26,446 ---> 00:58:31,756 You can tell GCC to link against our library 1378 00:58:31,756 ---> 00:58:33,406 and long story short is what this mean, 1379 00:58:33,406 ---> 00:58:37,106 is when GCC is converting hi3.c from source code 1380 00:58:37,106 ---> 00:58:39,646 into object code, from human readable to 0's 1381 00:58:39,676 ---> 00:58:43,486 and 1's it means, oh by the way, go get the library, -l, 1382 00:58:43,746 ---> 00:58:47,546 called cs50 and merge the two at the very last minute 1383 00:58:47,586 ---> 00:58:51,886 so that the file you get out hi3 or a.out contains 0's and 1's 1384 00:58:51,886 ---> 00:58:53,256 from both of those files. 1385 00:58:53,506 ---> 00:58:56,556 So again, another silly detail that's very easy to forget 1386 00:58:56,796 ---> 00:58:58,626 but just to kind of drill it into your minds 1387 00:58:58,626 ---> 00:59:00,276 if you ever see this semester, 1388 00:59:00,496 ---> 00:59:02,566 a message about undefined references, 1389 00:59:03,046 ---> 00:59:05,276 probably means you forgot something simple 1390 00:59:05,276 ---> 00:59:08,126 like that library that you wanted to use, -lcs50. 1391 00:59:08,186 ---> 00:59:11,216 there's others, -lm gives you a math library. 1392 00:59:11,216 ---> 00:59:14,536 There's -lcrypt which gives you a cryptography library and stuff 1393 00:59:14,536 ---> 00:59:16,236 like that, but it did work this time. 1394 00:59:16,236 ---> 00:59:19,776 when I said go ahead and link in this file, I did in fact get hi3 1395 00:59:19,886 ---> 00:59:25,616 so if I run hi3 state your name, we'll say Jensue, 1396 00:59:25,886 ---> 00:59:28,096 well hi Jensue, well what about Yuki? 1397 00:59:28,936 ---> 00:59:31,926 Okay so now I have, still kind of overwhelming right? 1398 00:59:31,926 ---> 00:59:33,556 not anything to write home about 1399 00:59:33,746 ---> 00:59:36,026 but at least it's a dynamic program that's starting 1400 00:59:36,026 ---> 00:59:39,096 to do a little bit more so just to give you a sense now 1401 00:59:39,096 ---> 00:59:42,626 of some formalities, one I've used here, Print F, 1402 00:59:42,706 ---> 00:59:45,646 a couple of times, I've used it pretty simply to use things 1403 00:59:45,646 ---> 00:59:48,936 like strings and ints but we can do more powerful things with it 1404 00:59:48,936 ---> 00:59:51,946 and in fact you'll have to get used to sort of a new sort 1405 00:59:51,946 ---> 00:59:54,266 of a world where people write things somewhat cryptically 1406 00:59:54,266 ---> 00:59:57,046 sometimes, this is an excerpt from the man page, 1407 00:59:57,046 ---> 00:59:59,556 the manual page for Print F and the way you, 1408 00:59:59,556 ---> 01:00:00,646 the programmer know, how 1409 01:00:00,646 ---> 01:00:03,466 to use a function is often frankly, by rtfm. 1410 01:00:03,746 ---> 01:00:04,946 Just read the manual. 1411 01:00:05,266 ---> 01:00:07,346 And actually look up what the instructions are, 1412 01:00:07,436 ---> 01:00:08,486 Google it later if you'd like. 1413 01:00:08,836 ---> 01:00:11,766 For those less comfortable and less familiar with the speak. 1414 01:00:12,576 ---> 01:00:13,736 So what you'll find 1415 01:00:13,736 ---> 01:00:15,806 on the courses website throughout the semester 1416 01:00:15,806 ---> 01:00:17,356 under resources we have a bunch 1417 01:00:17,356 ---> 01:00:19,676 of links we think you'll find useful and one 1418 01:00:19,676 ---> 01:00:22,716 of them is this wonderful site called cppreference.com. 1419 01:00:22,716 ---> 01:00:25,506 the only downside of it means cpp means c plus plus 1420 01:00:25,886 ---> 01:00:28,676 so the author of the site actually made it fairly c plus 1421 01:00:28,756 ---> 01:00:31,036 plus specific so under resources 1422 01:00:31,036 ---> 01:00:34,276 on the courses web sit we have our own copy of this website 1423 01:00:34,276 ---> 01:00:37,036 because he open sourced it to everyone so if I actually go 1424 01:00:37,036 ---> 01:00:42,456 to cs50.net/resources what you'll see up here 1425 01:00:42,456 ---> 01:00:44,686 under the link to c is a bunch of things, 1426 01:00:44,686 ---> 01:00:50,226 most of which we won't need just yet, but if I go to reference, 1427 01:00:50,606 ---> 01:00:53,686 this is a wonderful useful sight and this is one of the reasons 1428 01:00:53,686 ---> 01:00:55,856 that you don't really need a book for a course like this 1429 01:00:56,076 ---> 01:00:58,566 because so much good stuff is available on line. 1430 01:00:58,876 ---> 01:01:01,006 If I know that I'm doing something with io, 1431 01:01:01,006 ---> 01:01:04,336 that sounds cryptic, but it just means input output print get 1432 01:01:04,496 ---> 01:01:06,696 stuff like that, notice if I scroll down here, 1433 01:01:06,926 ---> 01:01:08,966 clearly we're only scratching the surface today 1434 01:01:08,966 ---> 01:01:10,836 of what C can do and I was interested 1435 01:01:10,836 ---> 01:01:12,236 in what Print F can do 1436 01:01:12,486 ---> 01:01:14,906 and here's just fairly user friendly documentation 1437 01:01:14,906 ---> 01:01:18,746 of the other codes you can use so we saw already, let's see, 1438 01:01:18,746 ---> 01:01:21,586 percent d and there it is at the top for signed integers, 1439 01:01:21,686 ---> 01:01:23,826 things that may or may not have negative sign in front, 1440 01:01:24,026 ---> 01:01:25,796 percents for a string of characters 1441 01:01:26,016 ---> 01:01:27,886 but there's some other stuff here too that we may 1442 01:01:27,886 ---> 01:01:30,086 or may not use in this semester, but what's nice 1443 01:01:30,086 ---> 01:01:32,126 about this site is it give you sample code, 1444 01:01:32,306 ---> 01:01:35,416 it tells you what library, what header file you must include 1445 01:01:35,416 ---> 01:01:36,606 at the top of your program 1446 01:01:36,866 ---> 01:01:39,356 and then it tells you what the functions signature is, 1447 01:01:39,586 ---> 01:01:41,626 what is sort of the canonical form of this thing 1448 01:01:41,626 ---> 01:01:44,036 so you can begin to understand how you use it 1449 01:01:44,036 ---> 01:01:46,296 and again you'll get much more acclimated to this 1450 01:01:46,626 ---> 01:01:47,866 over the course of the semester 1451 01:01:47,866 ---> 01:01:50,266 but we can do more interesting things with this. 1452 01:01:50,556 ---> 01:01:53,496 So just as there's n for new line, 1453 01:01:53,796 ---> 01:01:57,216 there's r for carriage return, so if you think back 1454 01:01:57,216 ---> 01:01:59,676 to retro typewriters when you hit enter 1455 01:01:59,676 ---> 01:02:01,056 on an old fashioned typewriter, 1456 01:02:01,286 ---> 01:02:03,236 not only did the paper move this way, 1457 01:02:03,236 ---> 01:02:06,616 the little typing head moved this way 1458 01:02:06,856 ---> 01:02:08,906 so a carriage return moves the curser all the curser all the 1459 01:02:08,906 ---> 01:02:11,656 way back to the left, a new line moves it down one line 1460 01:02:12,086 ---> 01:02:14,526 so this is another sort of stupid thing 1461 01:02:14,526 ---> 01:02:17,836 from religious battles past so in the Windows world, 1462 01:02:17,836 ---> 01:02:21,196 most text files end with what's called a carriage return 1463 01:02:21,356 ---> 01:02:22,836 and a line feed. 1464 01:02:22,836 ---> 01:02:25,346 Crlf, which means rn. 1465 01:02:26,326 ---> 01:02:29,036 in the Linux world people were much more conservative, 1466 01:02:29,036 ---> 01:02:33,756 they just used new lines and n in many systems, Macs and, 1467 01:02:34,486 ---> 01:02:36,266 actually not so much Mac sometimes, 1468 01:02:36,266 ---> 01:02:39,546 so in Linux computers n means yes go to the next line 1469 01:02:39,756 ---> 01:02:42,556 and go all the way to the left so I'm just kind of grimacing 1470 01:02:42,556 ---> 01:02:44,426 at some of these details because these things too, 1471 01:02:44,646 ---> 01:02:46,206 are little things you trip over early on. 1472 01:02:46,206 ---> 01:02:48,136 they're fundamentally not that interesting but you get used 1473 01:02:48,136 ---> 01:02:50,006 to little trivia or minutia like this. 1474 01:02:50,276 ---> 01:02:53,936 But this does beg the question, if n means new line, 1475 01:02:54,326 ---> 01:02:56,866 r means carriage return, 1476 01:02:57,166 ---> 01:03:01,306 how do you just write a literal ? Well the answer is there 1477 01:03:01,306 ---> 01:03:03,806 but this is a common pattern, what is it? 1478 01:03:04,776 ---> 01:03:07,396 so unfortunately a little weird, you kind of have 1479 01:03:07,446 ---> 01:03:09,476 to have a corner case like that if you want a literal 1480 01:03:09,686 ---> 01:03:12,536 and a string you escape the but this notion 1481 01:03:12,536 ---> 01:03:14,686 of escaping is something we'll see quite a bit. 1482 01:03:15,036 ---> 01:03:16,356 Well, it turns out we can do stuff 1483 01:03:16,416 ---> 01:03:19,776 with not just ints not just strings, there are chars 1484 01:03:19,776 ---> 01:03:21,026 for individual characters, 1485 01:03:21,026 ---> 01:03:23,616 doubles for floating point values and float 1486 01:03:23,616 ---> 01:03:25,026 for floating point values and all 1487 01:03:25,026 ---> 01:03:27,556 of these have some different meaning, in fact let me go ahead 1488 01:03:27,846 ---> 01:03:29,986 to an example that's not just text space now, 1489 01:03:30,226 ---> 01:03:32,406 but just to demonstrate, again, to underwhelm, 1490 01:03:32,406 ---> 01:03:33,636 at first because we can definitely one 1491 01:03:33,636 ---> 01:03:37,036 up ourselves easily that math is not hard and c. 1492 01:03:37,346 ---> 01:03:40,066 so here's a program that assigns a variable called x, 1493 01:03:40,426 ---> 01:03:44,486 I'll type integer the value of 1; 1494 01:03:44,556 ---> 01:03:48,696 variable called y type is int, assign it a value of 2 right, 1495 01:03:48,696 ---> 01:03:50,256 new syntax, not interesting. 1496 01:03:50,346 ---> 01:03:53,476 Finally I declare a third variable called z its also going 1497 01:03:53,476 ---> 01:03:56,516 to store an int, its going to store the sum of x and y, 1498 01:03:57,186 ---> 01:04:01,406 looks pretty good but let me go ahead and let me run GCC 1499 01:04:01,576 ---> 01:04:03,896 of math1.c okay, interesting. 1500 01:04:04,186 ---> 01:04:07,036 So it turns out GCC, especially the way we have it configured 1501 01:04:07,036 ---> 01:04:11,826 on nice.faf can also be a bit anal and a little cautious 1502 01:04:11,826 ---> 01:04:13,466 on your behalf saying you know what, 1503 01:04:13,896 ---> 01:04:15,186 odds are you screwed up here. 1504 01:04:15,296 ---> 01:04:16,526 right? its saying it very nicely, 1505 01:04:16,576 ---> 01:04:19,106 warning unused variable z but that's telling you 1506 01:04:19,106 ---> 01:04:21,236 that you did something, you're not using it, 1507 01:04:21,486 ---> 01:04:23,756 probably wasn't a very productive program you just 1508 01:04:23,756 ---> 01:04:24,896 wrote, but let's try it. 1509 01:04:24,936 ---> 01:04:28,866 let's be stubborn and let's run a.out enter, hmm. 1510 01:04:29,316 ---> 01:04:32,876 Is it not working? 1511 01:04:34,226 ---> 01:04:35,886 Its actually working perfectly. 1512 01:04:37,456 ---> 01:04:40,256 Right, the math is correct, what is in z 1513 01:04:40,256 ---> 01:04:41,496 at this point, oh, missed it. 1514 01:04:41,656 ---> 01:04:44,146 the problem is what? 1515 01:04:44,146 ---> 01:04:44,746 Simple answer. 1516 01:04:46,796 ---> 01:04:49,736 I'm not doing anything with it, yes I created 3 variables, 1517 01:04:49,736 ---> 01:04:52,406 all of them at some point in time, do in fact have numbers 1518 01:04:52,406 ---> 01:04:55,036 like 1 2 and hopefully 3 inside of them 1519 01:04:55,236 ---> 01:04:56,896 but if you don't do anything with it, 1520 01:04:57,166 ---> 01:04:58,986 sort of the tree falling in the woods right? 1521 01:04:58,986 ---> 01:05:01,876 no one knows that the answer you just computed was three unless 1522 01:05:01,876 ---> 01:05:05,046 you print it, save it, do something with it, 1523 01:05:05,266 ---> 01:05:07,096 so hopefully there's a solution here. 1524 01:05:07,096 ---> 01:05:09,386 so let's take a look at the second, slightly fixed version 1525 01:05:09,386 ---> 01:05:11,246 of this, so how do I print out the result? 1526 01:05:11,246 ---> 01:05:13,316 Well again, let's just go into our little tool kit that's going 1527 01:05:13,316 ---> 01:05:15,006 to start being filled with more function still, 1528 01:05:15,286 ---> 01:05:18,086 Print F quote unquotes the quotes are important, 1529 01:05:18,266 ---> 01:05:21,416 the percent means display this as an integer and then I pass 1530 01:05:21,416 ---> 01:05:24,556 in z and so, okay, so now not only have I written a correct 1531 01:05:24,556 ---> 01:05:28,456 program, I'm actually doing something productive with it, 1532 01:05:28,516 ---> 01:05:32,876 a.out, now gives -- hmm, still a little weird, its not incorrect 1533 01:05:32,876 ---> 01:05:34,906 but what's that explanation for this weirdness. 1534 01:05:36,366 ---> 01:05:38,696 Right, so there's just no new line, so let me go in here 1535 01:05:38,946 ---> 01:05:42,056 and I'm going to move over here, add between the quotes 1536 01:05:42,056 ---> 01:05:45,686 but after the percentn let me recompile, 1537 01:05:45,686 ---> 01:05:47,706 another little Linux trick, if you get tired 1538 01:05:47,706 ---> 01:05:49,516 of typing the same commands over and over again, 1539 01:05:49,786 ---> 01:05:52,006 you can either hit up and that will scroll 1540 01:05:52,036 ---> 01:05:53,846 through the commands you previously typed 1541 01:05:53,976 ---> 01:05:55,576 or you can hit ! 1542 01:05:55,746 ---> 01:05:59,746 aka bang and type the first letter or so of the last command 1543 01:05:59,746 ---> 01:06:01,696 that starts with that sequence and hit enter 1544 01:06:01,696 ---> 01:06:04,016 and if I haven't forgotten some other g command I wrote, 1545 01:06:04,306 ---> 01:06:07,086 it should remember, aha, it was gccmath2.c, 1546 01:06:07,086 ---> 01:06:09,136 again little things you pick up over time. 1547 01:06:09,806 ---> 01:06:11,706 and if I run a.out, 1548 01:06:11,706 ---> 01:06:14,106 okay slightly prettier, slightly prettier. 1549 01:06:14,326 ---> 01:06:16,946 So what else can we do here, well there's going 1550 01:06:16,946 ---> 01:06:20,436 to be other data types still but there's also this format strings 1551 01:06:20,436 ---> 01:06:23,206 so let's see if we can't do a little bit 1552 01:06:23,206 ---> 01:06:25,356 of something that's more interesting underneath the hood 1553 01:06:25,356 ---> 01:06:27,736 so it turns out we have format strings, 1554 01:06:28,006 ---> 01:06:29,586 but there's also this approach here, 1555 01:06:29,586 ---> 01:06:30,736 let me go back to the code. 1556 01:06:30,736 ---> 01:06:32,666 This is a program, again, that you have a print out of, 1557 01:06:33,016 ---> 01:06:36,046 called size of, and here's where we finally scratch the surface 1558 01:06:36,046 ---> 01:06:37,786 of actual design issues. 1559 01:06:38,186 ---> 01:06:40,586 Actual questions that you are interested to answer 1560 01:06:40,736 ---> 01:06:43,436 because they really will affect the correctness of your program 1561 01:06:43,436 ---> 01:06:44,726 and what you can do with it. 1562 01:06:44,986 ---> 01:06:47,516 so this program, its not very long, its about half comments 1563 01:06:47,516 ---> 01:06:48,546 and half actual code, 1564 01:06:48,806 ---> 01:06:51,306 notice that at the top here I've declared some variables 1565 01:06:51,306 ---> 01:06:53,656 in advance, so this is kind of interesting. 1566 01:06:54,036 ---> 01:06:56,486 Even though thus far I've very efficiently, 1567 01:06:56,486 ---> 01:06:58,706 very analy been declaring a variable on the left 1568 01:06:58,706 ---> 01:07:01,436 and then doing something with it on the right, all in one line, 1569 01:07:01,836 ---> 01:07:02,916 you don't need to do that. 1570 01:07:02,916 ---> 01:07:04,686 If you know you're going to need a variable, 1571 01:07:04,886 ---> 01:07:07,946 you can declare the variable earlier in your program 1572 01:07:07,996 ---> 01:07:11,286 and then use it later and you'll learn over time when this is 1573 01:07:11,286 ---> 01:07:14,486 and isn't appropriate, but here I've declared three types 1574 01:07:14,486 ---> 01:07:15,046 of values. 1575 01:07:15,046 ---> 01:07:17,466 A char, a double, a float and an int. 1576 01:07:17,976 ---> 01:07:20,356 a float is a floating point value a real number, 1577 01:07:20,356 ---> 01:07:24,276 something with a decimal point, what's a double perhaps? 1578 01:07:25,156 ---> 01:07:26,086 Any ideas? 1579 01:07:27,156 ---> 01:07:28,496 So its twice something, 1580 01:07:28,726 ---> 01:07:31,786 its actually a floating point value that's twice 1581 01:07:31,786 ---> 01:07:32,916 as big potentially. 1582 01:07:33,176 ---> 01:07:35,916 So it turns out that in this language and in some others, 1583 01:07:36,126 ---> 01:07:37,766 there are actually pre defined meanings 1584 01:07:37,766 ---> 01:07:40,486 for how many bits are used to represent a data type, 1585 01:07:40,796 ---> 01:07:44,816 so we have chars or 8 bits on a typical modern computer 1586 01:07:44,856 ---> 01:07:48,396 with intel inside, a double is, and let's come back to that. 1587 01:07:48,396 ---> 01:07:52,036 A float is 32 bits, now that's pretty big, 1588 01:07:52,036 ---> 01:07:55,396 because if an int is also 32 bits, what did we say last week, 1589 01:07:55,396 ---> 01:07:56,646 the biggest number you can represent 1590 01:07:56,646 ---> 01:07:59,726 with 32 bits is, like 4 billion. 1591 01:07:59,726 ---> 01:08:01,866 Now wait a minute, if you're omitting negative numbers 1592 01:08:01,866 ---> 01:08:03,606 so if its negative numbers also then its 1593 01:08:03,606 ---> 01:08:06,746 like negative two billion, to positive 2 billion roughly 1594 01:08:06,746 ---> 01:08:08,056 but a total of 4 billion 1595 01:08:08,336 ---> 01:08:12,106 so floating point values also use 32 bits but they use them 1596 01:08:12,106 ---> 01:08:15,286 to have some numbers before the decimal point, 1597 01:08:15,596 ---> 01:08:18,006 some after so you can't, it's a trade off 1598 01:08:18,006 ---> 01:08:19,686 between how big a number you can represent 1599 01:08:19,686 ---> 01:08:23,066 as a floating point value versus how many points you can have 1600 01:08:23,066 ---> 01:08:26,476 after the decimal point but a double now is not 32 bits, 1601 01:08:26,566 ---> 01:08:29,246 right, I'm kind of beyond the scope of my arms here, 1602 01:08:29,496 ---> 01:08:31,656 its 64 bits and that's pretty damn big 1603 01:08:31,826 ---> 01:08:34,296 so you'll have some discretion there so what does it mean 1604 01:08:34,296 ---> 01:08:38,746 when I run this down here, Print F all right, so print an int 1605 01:08:38,746 ---> 01:08:40,226 and a new line, but what do you want to print? 1606 01:08:40,556 ---> 01:08:43,286 Well there's this operator in c, that's only occasionally useful, 1607 01:08:43,356 ---> 01:08:46,596 today its marginally enlightening, tell me the size 1608 01:08:46,596 ---> 01:08:50,046 of the variable c. tell me the size of the variable d, f, 1609 01:08:50,046 ---> 01:08:53,886 and i so let me go ahead and run a GCC on sizeof.c now I'm going 1610 01:08:54,136 ---> 01:08:58,176 to go ahead and run a.out, wait a minute, those are not 1611 01:08:58,176 ---> 01:09:00,366 in fact values like 32 and 64. 1612 01:09:00,366 ---> 01:09:01,296 what am I seeing? 1613 01:09:02,816 ---> 01:09:03,746 Yeah, I'm seeing bytes. 1614 01:09:03,936 ---> 01:09:06,196 That's kind of interesting, okay, so that's fine, 1615 01:09:06,446 ---> 01:09:07,736 let me take a look at one other thing. 1616 01:09:07,736 ---> 01:09:10,686 I saw on the screen a moment ago that there's this mention 1617 01:09:10,686 ---> 01:09:15,106 of longs so let me say long is an l and let me print 1618 01:09:15,106 ---> 01:09:20,586 out its length here, so long and I think its l, hopefully, %, 1619 01:09:21,336 ---> 01:09:28,166 let's see, GCC sizeofc, oops not l, dammit, what is it? 1620 01:09:28,806 ---> 01:09:29,716 is it also d? 1621 01:09:30,796 ---> 01:09:31,586 yes all right. 1622 01:09:31,916 ---> 01:09:35,576 if I run this now what do you think a long is going to be? 1623 01:09:35,576 ---> 01:09:42,246 So int was 4, also 4, so again, sort of a historical artifact, 1624 01:09:42,386 ---> 01:09:44,206 when I mentioned long longs before, 1625 01:09:44,536 ---> 01:09:46,796 so if you want an int that's bigger than 32 bits, 1626 01:09:46,796 ---> 01:09:49,446 you want a 64 bit int, its not a long, because a long is the same 1627 01:09:49,446 ---> 01:09:51,646 as an int, it's a long long that you want. 1628 01:09:51,646 ---> 01:09:54,536 So again, sort of random c trivia. 1629 01:09:54,886 ---> 01:09:55,796 But I thought we'd end with this. 1630 01:09:56,206 ---> 01:09:59,256 Here's a crazy looking program, I loaded this off the internet, 1631 01:09:59,546 ---> 01:10:02,196 written by a really, either a really brilliant person 1632 01:10:02,196 ---> 01:10:04,486 or a person with really a lot of free time. 1633 01:10:04,836 ---> 01:10:07,836 so this is an obfuscated C program 1634 01:10:08,156 ---> 01:10:10,756 and you are entering a community that you don't have to stay 1635 01:10:10,756 ---> 01:10:14,246 in in perpetuity of people who like writing programs like this, 1636 01:10:14,246 ---> 01:10:16,196 or have the ability to do it and there are contests 1637 01:10:16,336 ---> 01:10:20,146 that people run for obfuscated c contests whereby you challenge 1638 01:10:20,216 ---> 01:10:23,176 people to write the most confusing looking program you 1639 01:10:23,176 ---> 01:10:25,876 could possibly come up with that no human should be able 1640 01:10:25,876 ---> 01:10:28,076 to understand and then you challenge the audience, 1641 01:10:28,116 ---> 01:10:31,116 the other geeks in the room, what that program actually does, 1642 01:10:31,686 ---> 01:10:34,816 do you have any idea what this is going to do? 1643 01:10:34,816 ---> 01:10:37,006 We will not be writing things like this. 1644 01:10:37,216 ---> 01:10:39,606 This is an exercise in the opposite of good design, 1645 01:10:39,886 ---> 01:10:41,986 but its kind of a powerful thing, 1646 01:10:42,196 ---> 01:10:44,146 that if we run the compiler here, the warnings I'm going 1647 01:10:44,146 ---> 01:10:45,276 to ignore for now, that's fine. 1648 01:10:45,566 ---> 01:10:47,026 A.out, I'll give you the results 1649 01:10:47,026 ---> 01:10:50,236 of your first obfuscated c program which is, 1650 01:10:51,546 ---> 01:10:52,866 that's what that does. 1651 01:10:53,036 ---> 01:10:53,796 We'll see you on Friday.