Enter the URL of the YouTube video to download subtitles in many different formats and languages.
Hei, bine ai venit la Academia Giraffe
O să vă învăț tot ce trebuie să știți pentru a începe în limba de
C este un limbaj de programare minunat și, de fapt, este unul
Multe limbi de programare moderne se bazează pe C
deci este o idee foarte bună dacă vrei să intri în C sau chiar
C ++ pentru a afla elementele de bază și, în acest curs, vom
O să vorbesc cu voi despre instalarea unui editor de text și
Voi vorbi cu voi despre voi, ce este un program și cum
Citiți instrucțiunile pe care le dați și vom ajunge la niște
Ne vom uita la lucruri ca și în cazul în care declarațiile și
Vom vorbi despre diferitele tipuri de date pe care le puteți
Vom vorbi despre lucruri precum structurile și funcțiile. Vom
Și, în principiu, vă voi oferi o privire de ansamblu asupra
Deci, până la sfârșitul acestui curs veți avea o înțelegere foarte
Pe care le puteți construi pe voi, puteți merge mai departe și
Sunt foarte încântat să vă aduc cursul de bază pentru C
Sunt foarte încântat dacă băieți să vă aruncați în apă și să
Deci, nu ezitați să faceți clic pe toate videoclipurile și,
O să vorbesc cu voi despre cum ați pus totul la dispoziție pentru
De fapt, vom avea nevoie de două lucruri
Primul lucru de care avem nevoie este un mediu în care putem scrie
Acum există o grămadă de astfel de medii diferite. In esenta. Tot
astfel încât tot ce puteți să știți, permiteți-vă
Scrieți text și apoi salvați textul într-un anumit format de
deși atunci când lucrăm cu C de multe ori, poate fi util să
Mediu de dezvoltare. Practic, acesta este un editor de text
Așa că vă voi arăta cum să instalați un IDE numit cod blocând al
Trebuie să scriem programele noastre C va fi ceva numit compilator
Înseamnă că putem scrie în esență
Știți instrucțiunile pe care vrem să le dăm calculatorului, dar în
Compilate, care înseamnă în esență tradus sau transformat într-o
Deci, vom descărca și instala un program special care va face asta
Voi reveni la browserul meu web și
Mă duc doar la bara de căutare Google. Și voi căuta un program
Blocurile de coduri C. Și această legătură ar trebui să apară.
Acesta este un mediu de dezvoltare integrat
Este un editor de text care va face foarte usor sa scriem
Există o mulțime de opțiuni aici, unul spune că descărcați
Vrem să faceți clic pe descărcarea binarelor. Aceasta va fi cea
Deci, dacă sunteți pe Windows sau Linux sau Mac, puteți să-l
Deci, în funcție de sistemul de operare pe care doriți să faceți
Așa că o să vin aici la opțiunea de ferestre. Veți vedea că sunt
Deci există o grămadă de opțiuni aici. Ceea ce vrem să descărcăm
Și apoi numărul de versiune și apoi se spune min
Practic, ce este asta este că ne va permite să instalăm blocuri de
Așa că vom putea instala programul IDE și vom putea instala AC
Compilator. Amintește-ți că am spus că avem nevoie de cele două
Aveam nevoie de cele două lucruri, iar asta ne va da amândoi pe
Așa că voi veni aici și dă click pe link pentru a descărca acest
Și puteți vedea că a trecut și a descărcat asta
Acum, hai să ne îndreptăm spre dosarul nostru de descărcări și voi
astfel încât aceasta ar trebui să deschidă o fereastră în care să
Așa că am să fac clic pe Următorul
O să fiu de acord cu licența și practic lăsați toate opțiunile în
Bine, atunci când sa terminat instalarea, o să ne cerem să ruleze
Îmi dă această fereastră mică aici. Se spune că detectarea
Deci, puteți să faceți clic pe cel de aici. Asta e subliniat
Se spune compilatorul GNU GCC și voi face doar clic pe setați ca
Acum avem blocuri de coduri instalate
astfel încât în \u200b\u200burmătoarele câteva tutoriale
Vom învăța să înființeze blocuri de coduri care să scrie primele
Dar pentru moment, aveți tot ce aveți nevoie pentru a începe
În acest tutorial o să vorbesc cu voi despre cum ați pus totul la
Apoi, acest videoclip vă va arăta cum să obțineți totul pentru a
De fapt, vom avea nevoie de două lucruri. Primul lucru de care
Vom folosi editorul de text pentru a scrie programele noastre C și
C, dar există, de fapt, un tip special de editor de text care se
Mediul de dezvoltare integrat și este, în esență, un mediu special
Scrieți și rulați și gestionați programele noastre C
Deci, asta e primul lucru, al doilea lucru de care avem nevoie
Deci, în esență, ceea ce facem atunci când îl folosim este că
Vom scrie instrucțiuni pe care oamenii noștri le pot înțelege și
acestea trebuie să fie compilate sau
Tradus sau transformat într-o limbă pe care computerul o poate
O să ia programele noastre C și, în esență, să le traducă doar
Deci, primul lucru pe care îl voi face este să-ți arăt cum să
Deci, ceea ce vrei să faci pe Mac este să mergi la bara de căutare
Faceți clic pe Enter și o fereastră care arată astfel ar trebui să
Terminalul este în esență un program care ne permite să
Deci, de fapt, trebuie să folosim terminalul pentru a face ceea ce
așa că primul lucru pe care vrem să-l facem este să verificăm dacă
S-ar putea să aveți deja unul. Deci, vrei doar să introduci în CC
Veți vedea aici. Am instalat un compilator AC. Așa că primesc
Se spune ca si Lang și îmi dă o șansă de informații
Dacă ai asta, atunci ai instalat compilatorul tău C și ești gata
Dar dacă nu aveți asta, vă voi arăta cum să obțineți
Deci, tot ce vrei să faci este să introduci
selectați și apoi spațiul - - install and
Faceți clic pe Enter și veți vedea că primesc o eroare aici pentru
Dar dacă nu aveți deja instalate și, evident, atunci nu ați avea
Apoi, asta va disparea și va instala totul. Trebuie să obțineți
astfel încât, odată ce rulați comanda selectați Xcode, tastați din
Ar trebui să primești numărul acestei versiuni
Bine, deci acum că avem instalat compilatorul nostru C
Ceea ce vrem să facem este să descărcați un IDE
Deci, vrem să descărcăm un editor de text special pe care îl putem
În browser-ul meu web și eu sunt aici pe un site web numit bloc de
Este una dintre cele mai populare libere
IDE-uri pentru programare și C. Deci, vom veni aici doar la acest
Ar trebui să existe câteva opțiuni aici pentru descărcarea
Avem o grămadă de opțiuni pentru linux linux și mac faceți clic pe
Veți vedea că aici este ceea ce trebuie să instalați blocuri de
Deci, dacă mergeți la partea dreaptă aici, există un link către
Așa că am făcut clic pe acea legătură și ar trebui să înceapă
În mod automat, atunci când se termină descărcarea, hai să ne
așa că am să dau dublu clic pe asta și
Ar trebui să ne dea blocuri de coduri acum
Tot ce doriți să faceți este să glisați și să lăsați blocurile de
Deci aveți blocuri de cod instalate și sunteți gata să începeți să
În acest tutorial, o să vorbesc cu voi despre obținerea primului
Așa că vom încărca un fișier C și vom putea să facem totul de
așa că o să-mi deschid programul de coduri și acesta este
IDE pe care o voi folosi pentru restul acestui curs
Așa că am să dau dublu clic pe el și ar trebui să înceapă să se
Există o grămadă de opțiuni diferite, cum ar fi crearea unui nou
Deci, de fapt, vom dori să creați un nou proiect
Deci, ori de câte ori vom începe să scriem niște fișiere C
Vom dori să creăm un nou proiect în blocuri de coduri
astfel încât puteți fie să faceți clic pe acest buton chiar aici,
proiecte astfel încât să vă aducă la fereastra de aici și
O să fie o grămadă de opțiuni aici
pe scurt\n
Acest lucru este ca un proiect de bază C pe care îl puteți rula pe
Deci, faceți clic pe aplicația consola și apoi faceți clic pe
C ++ sau C acum C și C ++ sunt ambele
Diferite limbi de programare pentru acest curs, vom lucra cu C
Deci, evidențiați C și faceți clic pe Următorul și vom da un
O să-i sun pe proiectul meu și va trebui să punem acest proiect
În interiorul unui dosar, așa că o să deschid acest lucru și o să
Faceți clic pe Următorul și puteți lăsa toate aceste opțiuni ca
primul nostru proiect C în blocuri de cod acum peste aici în
Draf este chiar aici. Așa a fost numele proiectului pe care tocmai
Așa că am să dau un clic și vei vedea că avem dosarul numit aici
Și acesta este un fișier creat automat pentru noi prin blocuri de
Voi face doar clic și deschideți-o și veți vedea aici. Avem un cod
Sunt acele mici linii de cod care spun că includ aici. Acest lucru
Programul pe care ni l-am dat în mod implicit când am creat
Corect, în esență, ceea ce face acest program este printarea
Deci, pentru a rula acest program și a testa-o doar pentru a vă
Voi veni aici și vei vedea că e vorba de asta
Butonul de joc verde de aici și doar spune că alerga când trec
Acest fișier pe care l-am deschis chiar aici, pe care-l vedeți, va
Deci, în mod esențial, spunem blocuri de cod pentru a rula acest
Atunci când fac clic pe butonul Executare, primim acest mesaj. Se
Acum, această fereastră mică apare
Practic, acesta este linia de comandă. Deci, ori de câte ori
Deci, dacă aș ieși din asta și voi rula din nou acest program
Veți vedea că această fereastră va apărea din nou. Și în esență
Salut lumea pe ecran și asta este ceea ce face această
Executarea programului și imprimarea a ceea ce i-am spus să
Prin urmare, imprimarea unui salut pe lume și imprimarea pe ecran
Și aceasta este o instrucțiune pe care o putem da computerului
Dar pe măsură ce trecem prin acest curs
vom învăța tot felul de instrucțiuni pe care le putem folosi, dar
C și a testa fișierul nostru principal de puncte C
Deci atata timp cat totul functioneaza aici si esti bine sa pleci
În acest tutorial voi vorbi cu voi despre principiile de scriere a
Deci, dacă ați urmat cursul până acum, am instalat un editor de
Am instalat un compilator C și am creat primul nostru program C.
În interiorul blocurilor de coduri. Deci acum, în esență, ceea ce
Vreau doar să arăt câteva lucruri care sunt aici mai întâi aici
Avem aceste mici instrucțiuni, care sunt doar numite includ și nu
Mai târziu în curs. Vom vorbi despre ceea ce fac de fapt și despre
Cum ne ajută programul, dar deocamdată știm că pentru a ne folosi
Va trebui să-i avem pe tipii ăia acolo
următorul lucru pe care îl avem este acest mic bloc de cod numit
Are doar numele principal pe care îl puteți vedea acolo este o
Și aceasta este de fapt ceea ce se numește o metodă și nu vă
vom vorbi mai mult despre aceștia în viitor, dar, în esență, o
container unde putem pune ceva din codul nostru și
Această metodă principală este foarte importantă deoarece aceasta
când ne conducem programul atât de în C, putem scrie într-adevăr o
instrucțiuni pentru computer și apoi putem rula programul nostru
Executați aceste programe și de fiecare dată când computerul
O să se uite în interiorul acestui principiu
cu alte cuvinte, care va arăta în interiorul întregului cod din
Executați acest cod, așa că am această instrucțiune aici
Se numește printf și, practic, aceasta este doar imprimarea unui
Va intra în această metodă principală și va executa toate
Deci, dacă trebuia să-mi conduc programul, atunci asta va
Să vorbim de fapt de a rula un program de fiecare dată când vrem
Primul lucru pe care trebuie să-l facem este ceea ce se numește
Și în principiu ceea ce face este să ia tot acest cod C
Aceasta o traduce într-o limbă pe care computerul o va putea
Prin urmare, este, în esență, acest cod de program C
Translatându-l în codul computerului și apoi calculatorul îl poate
Deci, primul lucru pe care trebuie să-l facem este întotdeauna să
Al doilea lucru pe care trebuie să-l facem este să rulați fișierul
așa că odată ce am construit acest program
am compilat-o în jos în codul pe care computerul poate înțelege că
executa acest cod și în blocuri de cod
De fapt este foarte ușor să faci asta. Asa de
putem veni aici și această mică pictogramă a uneltei ne va permite
atunci acest buton de redare ne va permite să rulați programul
Programare, veți dori să construiți și să executați codul în
vrei doar să o construiești și să o conduci doar pentru a vedea
vedeți cum a făcut programul dvs.
Deci, avem această opțiune pe aici numită construi și rulați și
Deci, în general, pe măsură ce treceți prin acest curs, veți dori
Aceasta este construirea și rularea opțiunilor când fac clic pe
va construi în esență programul meu rulați-l și apoi va apărea
Fereastra consolei pe care o puteți numi. Deci, o mulțime de
Informațiile pe care le spunem așa dacă scriem un program C care
Atunci nu am vedea asta, dar din moment ce folosim această comandă
Deci, hai să vorbim despre modul în care aceste programe se
Deci, atunci când scriu un program C, scriu în esență un set de
Corect o rețetă este, în esență, doar un set de instrucțiuni care,
Știți să gățiți sau să coaceți ceva delicios, nu? Și asta este în
Este un set de instrucțiuni care atunci când sunt executate
va determina computerul să facă ceva pentru noi și unul dintre
calculatoarele este că le putem programa să facă orice și
C este una dintre cele mai bune limbi pentru a face acest lucru cu
Se numește print F, iar asta imprimă ceva pe ecran. Vom observa
Semicolon și acest punct și virgulă este de fapt foarte important
Deci, ori de câte ori scriem o instrucțiune în C
Vrem întotdeauna să terminăm cu o punct și virgulă
Asta spune C că am terminat cu acea instrucțiune și vrem să trecem
instruire și l-aș putea lipi aici și
Acum vom executa de două ori această instrucțiune
așa că dacă am să alerg și să-mi construiesc programul, veți vedea
Așa că atunci când ne conducem programul programul vine în jos în
Deci, începe cu această primă instrucțiune. Îl execută, nu?
Prin urmare, imprimă salut lume pe ecran și apoi imprimă și o
Deci, acest backslash n este ca un caracter de linie nouă și care
Când se face acest lucru, se trece la următoarea instrucțiune și
Asa cum mergem mai departe si invatam sa scriem in C
Vom învăța instrucțiuni mai complexe și mai complexe
Și vom putea folosi toate aceste instrucțiuni împreună pentru a
Aceasta este singura instrucțiune pe care o știm care este printf,
Instrucțiune pentru a desena o mică formă pe ecran
Așa că vă voi arăta cum putem trage pe ecran ca un triunghi mic
O să iau acest tipar și de fapt o să scap de lumea bună și o voi
așa că o să o copiez și o vom lipi aici, poate de patru ori și
Ce pot să fac este să pot specifica
Instrucțiuni pentru computer care îi vor spune să deseneze o
Deci, în jos aici aș putea face ca un slash înainte și aici
Vom face un altul și o voi face una și o voi face și o veți vedea
Am desenat ca o mică linie diagonală cu aceste șorici în față.
O să pun unul aici. O să pun unul aici și
în cele din urmă, de ce nu tragem niște sublinieri și apoi o vom
Spatele de spate sunt doar imprimarea unei noi linii. Deci, ori de
Deci îl va tipi pe acest tip pe linia de jos a liniei de mai jos
Deci, dacă aș salva programul meu și îl voi executa acum,
Din aceste instrucțiuni în ordine și vom putea tipări forma pe
Vedeți aici. Avem forma noastră și
Un lucru pe care chiar vreau să-l subliniez. Este ordinea asta
Deci, dacă am luat această instrucțiune și am pus-o aici ca prima
Asta va fi imprimată mai întâi
Deci, când îmi conduc programul, veți vedea că tipărim acest tip
Funny look aspect și asta pentru că am schimbat ordinea de
Deci calculatorul a schimbat ordinea în care a executat aceste
Semicolon după fiecare linie de cod. Deci, asta e genul de bază
În esență, tot ce trebuie să știți când scriem un program
specificăm o listă de instrucțiuni pe care doriți ca computerul să
În funcție de instrucțiunile pe care le oferim calculatorului și
Asta va determina ce face programul și când vom trece prin acest
vom afla, de asemenea, cum putem lua instrucțiuni simple precum
Faceți lucruri diferite cu ei pentru a face programele noastre
În acest tutorial vreau să vorbesc cu voi despre variabilele din
Acum când folosim limbajul de programare C, de multe ori ne vom
astfel încât vor fi valori diferite de date sau informații
Uneori poate fi dificil să se mențină și să se țină evidența
În esență, un container în care se pot stoca diferite informații
am putea sorta lucruri precum numere, texte sau caractere
Aceste variabile ne fac mult mai ușor să urmărim și să gestionăm
Așadar, vă voi arăta în esență cum pot fi variabilele utile și cum
Dar aici jos. Am un program de bază. Sunt practic doar
Imprimarea unei povesti. Se spune că a existat odată un om pe nume
Avea 70 de ani. Îi plăcea cu adevărat numele de George, dar nu-i
Tocmai imprimăm o grămadă de linii diferite de text
și apoi când conduc acest program
Veți vedea că vom imprima povestea reală, așa că aici avem
Dar să spun că citesc povestea mea. Citesc programul și mă gândesc
Cred că vreau să schimb numele de personaj
Deci, în loc să numim George personajul, de ce nu-l numim ca John
Trebuie să mă uit prin întregul meu program și să găsesc fiecare
Așa că aici este o schimbare a lui Ioan amintiți continuați să
Așa că a trebuit să trec manual și să schimbe fiecare instanță a
Pentru John și să spunem bine. Sună destul de bine
Dar poate mă gândesc că vreau să-l fac pe personaj un pic mai mic.
Trebuie să mă uit prin întregul meu program pentru a găsi toate
Am schimbat programul pe care l-am actualizat și l-am modificat
Dacă trebuia să rulez acest program, atunci toate informațiile vor
Am de-a face cu o poveste foarte scurtă. Adică, sunt patru linii
Am menționat doar vârsta și numele personajului de două ori
Dar imaginați-vă dacă scrisesem un program imens C care avea sute
Așa că povestea avea sute de linii și poate că am menționat numele
Ei bine, dacă aș fi vrut să schimb caracterele de vârstă, numele
Trebuia să trec prin ea și să o schimbe manual în fiecare locație
Practic, aceasta nu este o modalitate bună pentru noi să gestionăm
In regula. În principiu avem două piese de date cu care lucrăm
În mod consecvent, în acest program, vârsta caracterelor și numele
Ceea ce vreau să vă arăt acum este modul în care putem folosi
Așadar, amintiți-vă că o variabilă este doar un container în care
așa că vreau să creez două variabile care vor stoca numele
Vârsta personajului și vă vom arăta cum acest lucru poate face
Gestionați-vă aici deasupra acestor declarații de tipărire
Voi face o nouă linie și voi veni aici și vreau să creez o
Acum, ori de câte ori creăm o variabilă în C, trebuie să îi oferim
Așa că trebuie să-i spunem C câteva lucruri diferite
Primul lucru pe care trebuie să-l spunem C este tipul de
Voi intra în diferitele tipuri de date pe care le putem reprezenta
dar pentru moment doar știu că există o mulțime de tipuri diferite
Numerele de caractere pe care le putem stoca textul putem stoca
Am de gând să stochez numele personajului și numele personajului
Deci, pentru a crea această variabilă
Primul lucru pe care trebuie să-l fac este să-i spun lui C că
Deci, pentru a face asta, eu doar o să scriu caracterele așa și
Hei, vom stoca caractere în interiorul acestei variabile
După ce introducem caracterele
Vreau să scriu numele variabilei pe care vreau să o creez
Amintiți-vă de variabilele care conțin și este o idee bună să dați
Denumirile descriptive și acele nume ne vor comunica, în esență,
Numele de caractere și acum ceea ce vreau să fac este că vreau în
Spun să văd că vreau să stochez un personaj
Dar în C putem de asemenea să stăm în loc să depozităm doar un
putem stoca o grămadă de caractere și asta ar fi cam așa
știi un nume, așa că aici sunt patru personaje pentru a stoca o
Introduceți caracterul lui Abel
Va trebui să facem un braț deschis și închis
Și asta o să-i spunem lui C că vrem să stocăm o grămadă de
Deci voi tasta caracterul unui nume de caractere deschideți
Deci, în mod esențial, am atribuit o valoare acestei variabile și
Vom scrie valoarea pe care dorim să o păstrăm
Deci, în cazul meu, voi stoca doar numele personajului, care este
Va trebui să terminăm cu un punct și virgulă
așa că în esență ceea ce am făcut aici a fost eu
A creat o variabilă și i-am spus C ceea ce am vrut să stochez în
Am vrut să stochez un personaj. Am dat numele acestei variabile. A
Am folosit aceste paranteze deschise și strânse pentru a vinde C
Acum, ceea ce putem face este că putem face o altă variabilă
Deci, în acest caz, vom dori să facem ceva similar, dar vom dori
Există o mulțime de moduri diferite în care putem stoca numere și
așa că vom folosi ceva numit un număr întreg și un număr întreg
Aceasta înseamnă de fapt întregul. Și acum o să-i dăm un nume, așa
Veacul de caractere și voi stabili acest număr egal cu un număr
Deci, în povestea noastră personajul este de 35 de ani
Așa că voi termina 35 și acum putem să terminăm din nou cu un
Așa că am două variabile aici. Acesta stochează o colecție de
Acum, întrebarea este cum putem folosi interiorul programului
pe scurt\n
Instrucțiuni pentru a face acest lucru, așa că vă voi arăta cum să
Deci, în principiu, voi merge aici, unde avem numele personajului
Voi șterge acest lucru și o voi înlocui cu un% și un S și acum de
Voi scrie doar numele variabilei. Deci voi intra
numele personajului și, în esență, ce se întâmplă atunci când
Vom spune să vedeți că în interiorul acestui șir de text aici
Vom dori să introducem un altul
Stringul și un șir este, în principiu, doar o colecție de
Deci, atunci când spun că e% s, acesta este de fapt un substituent
Voi introduce un șir aici și
Aici folosesc această virgulă și apoi scriu șirul pe care vreau să
Este un nume de caractere și așa mai departe de aici
Va spune că odată a fost un bărbat numit
% s și va înlocui% s cu ceea ce specificăm aici
Deci, va înlocui% s cu valoarea stocată în interiorul acelei
așa că am să salvez asta și ne vom rula programul și
Acum veți vedea că încă mai tipărește o dată când era un bărbat pe
Chiar dacă nu l-am scos pe John aici
Tocmai am tastat% fundul în numele personajului, astfel încât să
De asemenea, folosim numele personajului, astfel încât să pot
Numele caracterelor și din nou. Aceasta va acționa ca un
Pentru acest șir de caractere pe care le specificăm aici
Să facem același lucru cu vârsta, așa că aici avem vîrsta de 35 de
I'm going to do something similar and I'm gonna say percent
but now because I have a number instead of using percent s I'm
Percent D and that basically means that we're gonna insert an
So I'm saying he was percent D years old and over here. I'm gonna
, and I'm just gonna type out the name of the variable that I want
Character age and we're going to do this one more time down here.
Character age so essentially what I'm doing is I'm printing out
That's inside of character age where I specify this
Let's go ahead and run our program and now you'll see it's gonna
the same name and the same age
Except I didn't have to actually type out the character's age and
I just referred to the variable and what's cool about?
Variables is I could come up here and let's say I wanted to modify
all I would have to do is modify it in this one spot so we could
We could also change their age to like 67
And now when I run my program all of that's gonna get updated in
so now it's using the name Tom and the age of
67 in our case. We only mention the character's name and the
But you can imagine if I was mentioning this variable like ten or
Having to change it in each one of those individual spots would be
So variables are great because we can store the value one time and
We just modify it in one place. So another cool thing that we can
So let's say that halfway through the story. I wanted to change
I can make a new line and right above these print statements. I
character age and I can just set it equal to something else so we
Again, we want to include that semicolon. So now halfway through
So you'll see it says there once was a man named Tom, he was 67
The age is being a 30 so we actually modified the value that was
Halfway through printing out our story and that's really why
So that's kind of a basic overview and introduction into what
And over here again, we're storing a collection of characters,
But there's some other data types that we can also represent
So in addition to representing like a string or a number we can do
And I'm going to talk about that in the next video
But for now, this has just been a basic overview and introduction
In this tutorial I want to talk to you guys about data types in C
We're gonna be dealing with a lot of data
So generally when you're writing programs, you're gonna be dealing
I just want to talk to you guys about what types of information we
Inside of our C program and these are called data types. All
So what are the different types of data that we can work with in
So I'm gonna show you guys all those different data types right
So a lot of times in C when we're working with data, we're gonna
so I'm just gonna show you guys a bunch of different variables
Can store inside of those variables?
so remember whenever we create a variable and C we have to tell C
information and the first piece of information we need to tell C
Data that we want to store inside of that container inside of that
so let's talk about the different types of
One of the most basic types of data and probably one of the types
Gonna be numbers. There's actually two very important types of
So the first type is called an integer and an integer is basically
so it's a counting number right if I was counting on my fingers 1
I'm just counting out whole numbers and I'm not saying like 1 1
2.3 like I'm counting in whole numbers and that's what an integer
So to create an integer we could just say int
And we'll give this a name so we could say like age and we could
Now whenever we want to use an integer in C. We always just want
So you'll notice I'm not putting quotation marks or parentheses or
I'm just typing out the number like that. And now we actually have
another type of number we can create is a
Decimal number so this would be a number unlike an integer
So it would be like 2.5 or eight point six seven or seven point
It's a number that has a decimal point associated to it. So
there's something called a double and there's something called a
Float, and I'm not gonna get too much into the difference between
But as a beginner to see for the most part
You're just gonna want to use a double
So I'm just gonna type out double and this could be like someone's
3.7 or whatever so, you know, you can use the double to represent
it's also important to note that I could also represent like the
3.0 and that's gonna be a decimal or I could say 40 point O
So the difference between 40 and 40 point O is that forty point O
It's a decimal number and 40 without the decimal point is an
So that's something just to keep in mind so you can make this like
So those are the two basic types of numbers integers and doubles
Now the next type of data that we're gonna want to store or is
single characters inside of a variable so I could just say
CH AR and that stands for character and we could give this a
So maybe it'd be like someone's grade and when we create a
We're basically going to specify the character that we want to
So inside of these single quotation marks, I could put a letter I
C d AF basically you can put any character that you want to store
So if I put an A here now, this character variable grade is gonna
It's important to note that you can only store one character so I
And that's actually gonna throw an error
All we can do is store one single character, but these three are
So these are the basic types of information that we can deal with
Three data types, we can represent all sorts of information
I also want to talk to you guys about one sort of extra data type
Technically a data type in C. It's technically like a modification
So you'll notice when we create a character we can only store one
But a lot of times we're gonna want to represent more than one
Character so we're gonna want to represent like a string of
so it's a collection of characters that are grouped together and
String so I can basically say char
I can type out the name of the string that I want to create so I
can make an open and closed square brackets and this is basically
string a bunch of characters together and
What we're actually doing is creating something called an array
But just for now know that we can create a string of characters by
Specifying the name these open and close square brackets, and then
So instead of these single quotes up here
We're just gonna use double quotes and I could type in like a
String of text I want here and this will get stored inside of
Variable which like I said is technically called an array now
Doing something like this or creating this like string of
So like these variables up here we could modify them
We can give them new values but you can't do stuff like that with
So just know that this is like a special type of data and it's
But that's one of the basics of working with data and those are
In this tutorial I want to talk to you guys some more about the
so if you've been following along with this tutorial
We've been using an instruction and our C programs called
Printf and printf basically allows us to print different things
We're going to talk more about what printf does and we're gonna
And also things like different data like numbers and strings and
So in order to use printf we just type out PR int F and we're
Now printf is what's called a function and basically what that
in our case printf just print something out onto the screen write
Printing something out and it basically works how you'd expect it
specific piece of information and generally when we're using
Text so I can make an open and closed quotation marks and I could
And now this will print out onto the screen. Hello and
Inside of printf I can use all sorts of special characters
so if I wanted to create a new line, for example, I can make a
Down here. I could type world and now this will print out a new
So you'll see hello world on different lines
We can also use these special characters
So for example, I could print out like a quotation mark
So if I just printed a normal quotation mark right here, you'll
counting this as the entire string if I wanted to change that I
Quotation marks when I run my program you can see we have a
dreapta\n
Extremely powerful and we can use it to print out different pieces
So in addition to just printing out plain text inside of these
I could also print out other types of information. For example, I
And if I wanted to print out a number I would have to use
Basically tells this printf function that we want to print out a
That's not just plain text like this
So if I said % and then I said D
This would actually allow me to print out a number
So generally when we use these format specifiers uses % and that
Special and then after that we would type in a specific letter and
so if I said % D this means that we want to print out an integer,
All I have to do now is I can make a comma and over here
I can specify the integer that I want to print out so I could say
500 to the screen so you can see over here
we're getting 500 and this is cool because we can interweave these
% D and this acts as a placeholder for this number over here
so when I run my program now it says my favorite number is
500 and that's really cool. We can also use multiple format
so in addition to just using this percent d I can also specify
You'll notice that this is structured similarly to percent d % s
So I can include some more text just like this
so if I said % s I can make another comma here and I could say
Number and now this is gonna print out my favorite percent s so
It's gonna insert that in and it's gonna say 500. So now when we
it'll say my favorite number is
500 and you'll notice here that I'm using multiple commas. So I'm
I have a comma and I'm specifying this string that I want to print
integer, and so basically the order that you put these format
String of text is the order that you need to include them using
So you're going to separate each of them using a comma hoon
We can also use a bunch of other ones
And I'm going to talk to you guys about some of the most common %
but if we wanted to print out a decimal number
for example a double we could say percent F and this is gonna
Decimal number so I could say like 500 point nine eight seven five
And now this is gonna print this out on to the screen. Actually. I
So my favorite number is five hundred point nine eight seven five
So essentially you can use these print FS and you can use these
So like this is just some general text output and we can use these
Like numbers or you know other strings and stuff like that and
So if I was to create like a number variable up here, I can call
I can use this variable and print it out inside of this printf so
Instead of printing out this number we can just access that
And this will allow me to print out that variable using that
90 so that's the basics of using printf
This is a very awesome tool that we can use and printf is
So when we're running our programs a lot of times we're gonna want
About what's going on about what they're doing in printf can allow
So don't be afraid to use these different access modifiers. Like I
Those are all sort of the common ones and you can also use % C and
Character so I can come over here create like a character
We'll call it my char and just set it equal to like lower case
I and I could print this out using % C. So now we could say my
Play around with printf. It's extremely useful and it's really
I want to talk to you guys about working with numbers and see now
We're gonna want to do different things with numbers whether
Subtracting doing math with numbers or you know, really just
So I'm just gonna give you guys a basic overview of a lot of the
We're also gonna talk about how different number types in Iraq. So
So down here I am printing out a number so you can see I'm using
And % f stands for like a floating-point number so that could be
so we put % F in here that's gonna allow us to print out a decimal
You can see I can run my program and it'll print out 8.9 and
But it's also printing it out to a very precise
decimal point and that's what C is always gonna do so it's always
A very precise decimal. So using these decimals I can do all sorts
So obviously I can just print out the decimal but I could also do
This is gonna go ahead and be able to do that for us when I run my
Not only is it gonna like print this out but it's gonna do the
so in addition to addition we can also use subtraction division,
Multiplication which is going to be in asterisks and you can use
Operations addition subtraction multiplication and division. So
floating-point numbers so like doubles and floats
Alongside integers. So let's say I was going to add an integer
With a double. So if I said 5 plus
4.5. Well, actually when we do this this entire answer is going to
Decimal number so when I say 5 which is an integer plus 4.5. We're
and basically any operation that you do between an integer and a
So that's important to realize but if I was to change this to four
This isn't gonna work anymore because this is asking for a
So when I do math with two integers
it's going to give us an integer back and if I do math with a
Floating-point number back. It's also important to realize for
This should actually be a decimal number right and you'd expect us
But if I just print it out percent D. So I printed out the result
I'm actually gonna get an integer back
So when I run the program you'll see we're getting a one here, but
But it's one with a bunch of decimal points after it. Dreapta?
But when we just do math with two integers
we're gonna get an integer back if I was to say five divided by
Decimal now, we're gonna get the entire answer back. So we're
So that's just a little bit about dealing with integers and
We could also take these and put these into variables
so, you know in addition to just having numbers down here I could
You know num set it equal to six and we can go ahead and print num
So now I will be able to print out that variable with no problem
So it can be really useful sometimes to store these numbers inside
I also want to talk to you guys about using more complex
mathematical functions now in C
Functions which are basically just little blocks of code that we
Certain things for us. So they're like modify a value or give us
I'm gonna show you guys a couple functions that we can use with
So I'm just going to come over here and I can actually start using
Out the name the function that you want to use
So by default C is going to give us access to a bunch of math
So we really don't have to do anything
all we have to do is just type in their names and I'm gonna show
Oh w then I'm gonna type an open and closed parenthesis and inside
I can actually give this two numbers
what this is gonna do is it's gonna take the first number that we
second number so if I said like 2 & 3
And you'll notice I'm separating them with a comma this is
So to give us 2 cubed so when I run my program and actually we
So this is gonna give us a decimal number back. So I need to print
When I run my program we're gonna get 8 back just like that. So
Cubing 2 I could say like 4 raised to the third power and now we
So that can be pretty useful. There's a couple other ones. I'll
So this will allow you to take a number square root
So if I said like 36 in here now, we're gonna print out the square
Which is gonna be 6 and there's a couple other ones
So let's say that I had a decimal I could say C EIL which stands
So if I have like thirty six point seven or 36 point three five
This is gonna give us the hot the next highest number. So it'll
We can do another one which is called floor and floor. We'll do
We'll just round the number down no matter what. So if I have like
So there's all sorts of little, you know things we can do
little functions that we can call and get information about
Functions will either like modify a number so it would like give
Or sometimes they'll like give us information about numbers
So that's sort of the basics of working with numbers, you know
you can do all sorts of math with numbers you can add in some to
Multiply add subtract integer numbers and floating-point numbers.
different things with numbers and if you want to find some more of
All you have to do is just go online and Google search for C math
there's like dozens of these little functions that you can use,
I want to talk to you guys about using comments and see well
Which actually gets ignored when we run our program, so if you
you can use a special starting and ending tag and any of the
Text any of the code anything that you put inside of that tag
So we can use comments to do all sorts of things, you know
You can leave little notes or little comments inside of your
Temporarily disable certain lines of code. They can be really
So I'm gonna show you guys the basics down here in my program if I
Using a forward slash and an asterisk and you'll see when I put
and basically what this means is anything that comes after this
Order to close off the comment. I can use an another asterisk and
You'll see the code changed back to normal code
Inside of these two asterisks is I can basically write out
If I you know wrote out like my program and I ran my program this
So it's just gonna print out comments or fun
It's not gonna do anything else and you know
Like I said comments are really useful so I could leave a little
I could write something like to do like I have to do something and
Explain certain lines of code so I could put a comment here and I
So maybe you know, this was like a really complex line of code
We could use this text in order to explain it
You can also use comments to do something called commenting out a
So for example, let's say I wanted to run my program but I didn't
Well one thing I could do would just be to delete the entire line
Right so I could delete this code and now I can run my program and
It's no longer gonna do anything. But here's the problem in order
so instead of deleting it I could just bring it back and I could
surround it with these comment tags and
Now the computer is just gonna ignore it. So it's not gonna render
So now when I run my program it's gonna do the same thing. It
But I didn't have to actually delete the line. I could just
So a lot of times when you're writing programs
You might want to try to run your program without a certain line
you know series of lines of code and instead of deleting them and
See will basically ignore them so that can be really useful
So those are the two basic uses for comments and essentially
It's just a way that we can write out information in our files.
And so you can really use comments to do whatever you want
Now I will say one best practice with comments is to use them
Generally, you only want to use a comment when you absolutely have
File and there's like comments littered all around and obviously
I mean no one's stopping you but as a best practice going forward
I think generally only using comments when they're absolutely
But again, it's open, you know, you can use comments for whatever
In this tutorial I want to talk to you guys about constants in C
Well constant is a special type of variable and C which can't be
So when I create a constant and C, I'm basically creating a value
Unable to be modified. So I'm gonna show you guys basically how
Let's say I created an integer and I just called it num and I set
right, and then I printed out that integer onto the screen so we
We're just gonna pranaam right so I can create this num variable.
I wanted to modify num so I could basically say num is equal to 8
The value that's stored inside of the num variable
And let's come down here and we'll print this out as well. So
And we'll print out num again. And actually I'm gonna print out a
So when I go ahead and run this program and I forgot to send me :
you'll see we're printing out five and then we modified the value
But in certain circumstances and see you're gonna want to create
So these would be values that just like can't change and those are
So if I wanted to make a num a constant in other words, let's say
I didn't want the num variable to be able to be modified in my
I could come over here and I could say
Const so Co NS T and I'm gonna say that right before I declare the
Right after you declare the type, but I've always preferred to
So I say Const int num 5 and now this program is actually gonna
You'll see down here. It's getting highlighted in red. That's
So I'm trying to modify a num down here
But I can't because I gave it this Const keyword because it's now
So in a lot of cases you want have variables that just can't be
In a situation like that you want to make them consonants also a
We'll give them all uppercase names and this isn't required
this is just sort of like what a lot of developers will do so
Um like this they would say like num or maybe this is like your
So that would be how you could like distinguish two words
but generally like constants will be all capital and that's just
Unchangeable that they're constants. But again, you don't have to
so this is one way that we can create a
constant, and we basically create a constant variable that can't
Just like any number or any text that we're using in our program
So, for example, I'm gonna get rid of this if I came down here and
So let's say I just printed out like Hello. This is actually also
So this is just like a string of characters a string of text
That's also considered a constant because it's just like text on
so you would also consider this a
Constant if I was printing out a number to like let's say I came
77 T is also considered a constant
So it's just considered to be like a piece of information or a
That is sort of unchanging. Dreapta? So 70 this isn't gonna
I mean unless I physically came in here and changed it like
This value is always gonna be 90 no matter what I can't modify it
So that's also considered a constant
But I would say like for the most part like where this is going to
But creating constant variables variables that can't change
In this tutorial I'm gonna show you guys how to get input from a
So a lot of times in our C program
So we're going to be working with all different types of
Information from the user so I'm going to show you guys how we can
We can take that information store it inside of variables and then
So this is gonna be pretty cool. So down here
I will basically write out the code for this and the first thing
We want to prompt them. So I want to prompt the user, you know, as
So I'm just gonna say printf and over here. I'm just gonna print
Ask the user to enter in their age. So well, I'll show you guys
so I'm just gonna say enter your age and
Now that we've prompted them to enter their age I need to do two
So the first thing I want to do is create a variable where we can
the input that the user puts into the program
So I want to create a variable where we can store the age that the
So I'm gonna create an inn over here. Oh, it's called age. And I'm
so all I'm gonna do up here is just
Declare the variable. I'm just gonna tell see that I want to use
But I'm not gonna give it a value in other words. I'm gonna allow
So down here I want to be able to get input for from the user so I
scanf is basically going to allow the user to enter in some
This works similar to printf it's kind of doing the opposite of
Scanf is allowing the user to input something
Into the program and we're gonna make an open and close quotation
I basically want to tell see what type of information I'm asking
So in our case we're asking for an integer right age is going to
it's a whole number so I'm gonna accept as an input and integer
Over here. We want to tell C where we want to put integer
So I'm basically gonna tell see what variable I want to store this
I can essentially just type out the name of the variable here
But in order to get input from the user I'm actually gonna have to
So instead of just typing out age. I'm gonna have to type
And when I say ampersand age, this is what's called a pointer and
I'm gonna cover everything you need to know about pointers
But for now, that's a little bit beyond what we need to learn
so all you need to know is that when you're using scanf and you
An integer or a float or like a character you need to use this
So down here now that we've scanned for the users age. I'm just
So we'll just print out like you are and I'll say % D years old
I'm just gonna print out that age variable. So essentially what
I'm storing whatever age they enter inside of this age variable
So let's go ahead and run this program and we'll see how we did
So I'm gonna run the program and it says enter your age. So let's
It's can take that integer that we entered 50 store it inside of
You'll see over here. It says you are
50 years old. So that's how we can get an integer from the user
We can also get like a double from the user
so for example, I could say enter your GPA and so now instead of
asking for an integer we're gonna be asking for a double so I can
We could call this GPA and now I can do the same thing. But
I want to say LF and LF is basically going to tell this scanf
Double and then obviously instead of age. We're just gonna put GPA
your GPA is and then we can put % F because we're gonna be
I'll just say GPA. So you'll notice here in printf when we want to
We're using percent F to print it out. But when we're using scanf
So that's just like a little difference and now let's go ahead and
And we should be able to get a GPA. So let's say someone's GPA is
3.1. So that's how we can get a double. I also want to show you
so why don't we create a character up here and we'll just call it
Now when we want to get a character we can just say percent C and
I can just come down here and say ampersand grade and then down
Percent C and we'll go ahead and print out the grade
So now we should be able to get a character from the user
let's say I got an A and it says you're good as a so we can use
Input from the user we can store those specific types of input
I also want to show you guys one more thing we can do which is we
so in addition to getting numbers and a character, we could also
this is going to be a little bit different from
Doing like numbers and characters. So I want to kind of show you
Let's create a variable. We'll just call it. Let's just call it
Whenever we're creating a string of characters, we always need
I'm actually going to specify how many characters I want to be
In the past in this course when we when we've been creating
We've just kind of said like whatever. All right, we basically
But in this particular situation, we're not gonna be giving this
Like I don't know what the users name is gonna be like, I don't
They're gonna tell us what that is. And so if I'm not gonna give
I do actually need to tell see how big I want this variable to be
I need to tell see how many characters I want this variable to be
Allocate enough memory for this variable. So I'm just gonna put 20
And I think that's enough for a name so down here. We'll just say
I can use scanf in order to get input
in the form of a string but instead of saying percent see I'm just
Over here instead of saying ampersand grade
I'm just gonna type out the name of the string of characters. So I
Specify the name of the string so down here
it says printf your grade is and actually let's just say your name
I would have say percent s and then over here we can print out the
So this should work just like it did in the other cases
So let's go ahead and run this and it says enter your name. So my
You'll see it says your name is Mike. So that works out really
So for example, if I came in here, and I said enter your name, and
You'll notice that it's only saying your name is. John, it's not
Here's the problem. This is because
Whenever I use this scanf function and I use it with a string.
Space so once it sees this space it's gonna be like, okay, we're
So that's kind of a problem and that's just kind of how scanf
There is a way that we can modify scanf in order to be able to get
but there's another function which I want to show you guys which
it's similar to scanf but f gets is
Basically going to be more generous F gas is essentially just
It's not gonna be able to grab it and store it inside of like an
It's just gonna be able to store it inside of like a string of
When we're using a forgets the first argument
We want to give it is the name of the variable where we want to
So in our case, it's just gonna be this name variable
The next thing we want to do is specify how many characters we
So this will essentially limit the amount of characters that the
And this is always a good idea when we're trying to get a string
Enter in like a million characters and they would overflow the
Like see wouldn't be able to handle accepting that many characters
We can specify how many characters we want to be able to accept so
Because that's how many characters we can store inside of this
I'm just gonna say stdin and this stands for standard input
what we're doing over here is we're telling FCAT s--
where we want to get the information from and standard input is
Now I'm using F Gatz and this is going to do exactly what scanf
the input inside of this named variable, but now we'll be able to
Multiple words instead of just one single word. So let's run this
So now if we entered in John Smith
You'll see it's able to enter in John Smith
Now the one downside with just using this F gets is you'll notice
So let me actually demonstrate this a little bit
So if I was to print out like, you know, just some random text
Whenever I get input using scanf, so if I entered in John Smith
So when I'm entering this by clicking the Enter key that's
Inside of the string. So when I click enter you'll notice it says
And then it prints out a newline and it prints out this text
And so that's just something that you need to be aware of when
but for the most part, I would say if you're getting input from
A user in the form of a string you can use F gets you can also use
Percent asses and i'm actually going to show you guys how we could
But for the most part I would say whenever you're getting a string
Guess so that's the basics of getting input from the user
there's a couple other things that we can do and I'm gonna talk
At least one other way that we can get strings from the user in a
But this is kind of the basics and you can kind of play around
In this tutorial I'm going to show you guys how to build a basic
we're basically gonna build a little program where the user can
Our program will take those two numbers add them together and spit
So this is gonna be kind of cool and we'll also learn about
So over here, I want to show you guys exactly how we can do this
the first thing I want to do is
Print out a prompt. So I basically want to prompt the user for
Inside here. We're just gonna give them a little prompt. So I'll
Essentially what we're gonna have them do is enter in two numbers,
so we'll make an int and we'll call it num1 and
Then we'll make another inch and we'll call it num2 and i'm not
Values right up front we're gonna end up giving these whatever the
So after the user enters the first number we need to actually grab
In here, we're basically just gonna say percent d because we want
I'm going to say the name of the variable
Where I want to store the value that gets entered now if you're
We were able to get a string of characters as input from the user
And basically we just typed in like the name of the variable
But when we're getting input, that's not a string of characters
In other words when we're getting input that's not percent s so if
so if it's a guy a decimal or an integer or a float or a
special symbol this ampersand
So I'm gonna have to say ampersand and then the name of the
so I'm just gonna say ampersand 1 and
Basically what this means is we're accessing the address of num 1
but for now just know that you need this ampersand here in order
Store the value that gets entered inside of this variable
we're basically just gonna do the same exact thing but for the
I'll paste this and now instead of saying enter first number.
Second number and we're just gonna store this inside of num2
Alright, so once we're done with this, the last thing we want to
So I'm just going to come down here. I'm going to say printf and
We'll just say answer and over here
We'll print out the answer so it's gonna be an integer and we're
So we're gonna print out the value of num1 plus num2. So we have
We're getting the second number. We're storing them inside of
Let's try to run our calculator. So I'm gonna run my program and
So let's go ahead and enter 6 enter
A8 and now we're gonna get 14. So 14 is the correct answer
So our program worked it was able to add the numbers correctly and
But let me show you guys one problem with this program if I wanted
Multiple non decimal numbers, for example if I said 2 and then
We're not going to get the correct answer. We're gonna get 6 plus
Which is gonna be an integer 8, but we're not gonna get 8 point 8,
So instead of letting the user enter in only integers
Why don't we instead let them enter in doubles so over here?
I'm going to change these to double so I'm gonna say num one's
Since we're getting doubles as input, we're gonna have to come
So right now this is accepting an integer
but we want to make this accept a double so normally if we're
Printf and we wanted to print out a double we would say f and that
but when we're scanning for a number if we want to use a double we
LF just like that so down here. I'm also gonna say LF and we'll
so print F is a little bit different than
Scanf right and scanf if we want to accept a double as input. We
We just use % F. So let's run our program now and we should be
floating-point numbers so decimal numbers over here
I will do four point five plus six point seven and we get 11 point
Looks like everything is working properly. And now we have a basic
So the user can enter in any numbers that they want and the
Now this calculator is not like 100%
ironclad secure for example, like if I was to come up here and
Enter in like a string of characters instead of a number you'll
So it's just like not doing what we wanted to do
And as we go further in this course
We're gonna learn all sorts of ways that we can check to see if
circumstances like that, but for now, this is just kind of an
Addition or multiplication once we have them
In this tutorial I'm going to talk to you guys about building a
We're gonna be building a mad libs game now
If you ever played the game mad libs
it's basically a game where you write down a bunch of random words
You know nouns or verbs or someone's name or you know a verb
you take all of those words that you enter in and you kind of
Sprinkle them in into a story and then generally the story is like
It's actually if we head over to my web browser
You'll see I have a picture of a Madlib up here
And basically you just add in a bunch of random words into the
Kind of funny so I'm gonna show you guys how we can build
And we're also gonna talk about some more ways that we can use
So over here, we have a little story that I printed out. It just
I love you kind of like a classic poem
But I think this poem would be a lot funnier if we turned it into
So that's exactly what we're gonna do. I'm gonna replace roses are
So we're just gonna have a user enter in a color. I'll replace
So we're gonna have them enter in a plural noun, and I'm gonna
We're gonna say I love and then a specific celebrity. So I'll just
Alright, so this is basically what we're gonna be printing out
Plural nouns are blue and then I love whatever celebrity so let's
So we're actually going to need to do a couple things
And actually the first thing I want to do is I want to create
The color that the user inputs the plural noun that the user
we're going to create three variables and these are basically
character strings, so they're going to be
Collections of characters and we can store them in
Variable so I'm gonna create some of these variables. Why don't we
String or like a collection of characters?
We need to make these open and closed square brackets
And what I also want to do because I'm not gonna be giving color
I just need to tell see how many characters we want this
String to be able to store that way si knows how much memory it
So I'm just gonna say 20 and we'll basically just say they can
we're gonna do the same thing for plural noun and
again, we'll let them enter in 20 characters maximum and
Finally, we're gonna do the same thing for celebrity. So again 20
Alright now that we have our variables created. I want to actually
so I want to prompt the user for
Information and I want to take the information that they entered
First order of business is to prompt them for input so I can just
We'll basically just type in enter a color
Once we've prompted them to enter the color we can actually get
I'm going to use a function called
Scanf and over here. I'm gonna accept a
string so I'm going to accept a string of characters and we're
remember if you watched the last tutorial we use the
Ampersand here when we were getting numbers or also you'd do the
But when we're getting input for a string of characters, we don't
Let's copy these and I'm gonna paste this two more times
so the second thing we want to get from them is going to be the
plural noun and I'm gonna store this inside of the plural noun
Finally, we're gonna do the same thing for celebrity. And again,
Okay, cool. So now I'm getting the color. I'm getting the plural
So the last thing we have to do is we have to take all of these
Right, so we need to be able to print out the story with all of
So I'm gonna come down here and I'm just going to say percent s
Same thing here. I'm gonna replace the plural noun here with a
percent s and we'll pass in the
Plural noun and finally same thing for celebrity down here
Alright so everything seems to be wired up and you'll notice that
Lines, let's go ahead and run this program and we'll see how we
So over here, it's prompting us for a color. Why don't we enter in
Microwaves and enter a celebrity. Why don't we just say?
Prince so when I click enter it's gonna say roses are magenta
Microwaves are blue. I love prints. So we were able to prompt the
We took everything that they input we stored it in variables
Then we printed all those variables out inside of our story and we
I do want to show you guys one way that this program could mess
So let's enter in a different color. I'm going to enter in like
And now enter in a celebrity so I'm gonna show you guys one way
If I entered in a celebrity with a first and a last name like Tom
You'll notice that instead of saying I love Tom Hanks. It's only
Here's the problem when we use that scanf function scanf is only
Whitespace, so essentially when we put this space here. We're
but in reality we want to be able to grab the there's
Full-name want to be able to grab the celebrities first and last
So this is a situation in C where we would have to modify our a
instead of just getting one variable like the celebrity I can
Celebrity F and that'll stand for celebrity first name and then
We can make another variable called celebrity l that'll stand for
instead of just scanning for one string of characters I can scan
Down here. We're gonna want to do the same thing so we can just
Celebrity L and we just need to add another percent here
So it's gonna say I love celebrities first name and celebrities
so I can enter in like red and
Microphones and now we can enter in Tom Hanks and we're printing
So that's one way that we could remedy this program and make it be
And it also just shows you guys a little bit more about how scanf
It's gonna stop getting the input at that first space now
Here's the thing about this program though if I wanted to enter in
So if I only wanted to enter in one the program actually isn't
so if I said like hats and down here if I said
Like Gandhi and I click enter you'll notice that it's still
Right, so I could enter in something here and then the program
But it was waiting for me after I entered in just that one name
So that's something that you're gonna have to you know play around
Basically C is gonna force you to be very specific about what the
so if the user needs to enter two things like two words and you
Enter in one word. You have to specify that. So you have to be
In this tutorial I'm gonna talk to you guys about using a raisin
we're gonna be dealing with a bunch of different data and
one of the things we can do to control and manage and sort of keep
Put data inside of things called arrays an array is really useful
But what happens in your programs when you're dealing with huge
Specifically huge amounts of information that are related right
I had a list of like a bunch of names or a list of a bunch of
Let's say I had like a list of a hundred numbers, right?
I wouldn't want to have to create a hundred different
variables to store all of those different numbers and this is
Data structure where we can store a bunch of different data
So inside of an array unlike a variable where I can only store one
So inside of a single array, I could store like five things or
I could sort a bunch of different pieces of information and then
Organized in my program so I'm gonna show you guys how to create
Like we create a normal variable and a variable in array are very
variable is basically used to define a container that
Stores a single value and an array is used to define a container
So the first thing we're gonna have to do when we create an array
Information and the first piece of information is what type of
I could say int and now I'd be creating an array that would hold
If I said char, I'd be creating an array that would hold
Or if I said like double the same thing but for double numbers, so
And I'm just gonna call this lucky
numbers whenever I create an array
I always want to use a special little symbol and this symbol will
Variable we want to an array and it's an open and closed
Square brackets whenever I use these open and close square
We want to store multiple pieces of information
Now there's a bunch of different ways. We can create these arrays
the easiest one though is to just say equals and I can make an
inside of this open and close curly bracket
I can just start typing out some numbers that I want to put in
Now I can basically type out as many numbers as I wanted and I
Inside of this array so over here, I you know, I printed out six
You know, the only thing that's limiting me is how many I can
So you'll notice that I put in the piece of data so like 4, for
And then I put a comma and a comma is gonna separate all the
So we would say 4 is the first element in the array 8 is the
These are all considered to be elements
Inside of this array and what's cool about this array is it's now
So unlike a variable where we can only store one value now in this
so if I wanted I can print this out and I'm actually gonna print
remember, we're gonna print out a
Number and I'm gonna show you guys how we can access
specific elements inside of this array
So all of these numbers are stored in this lucky numbers array in
But the question becomes how do we access them? Right, how can I
well, I could say present' d so I'm gonna print this out and now I
so I'm gonna refer to the name of the array and when I want to
Close square bracket and inside of this open and close square
I want to put the index of the element that I want to access so
And I can access them by putting the index inside of this square
so if I wanted to access this for for example
I can put a 0 inside of here. And now when I run my program you
We'll be able to print out that for
So over here, I'm printing out four if I wanted to access this 15
I can put a two in here. So now when I run my program I'll be
so one thing you might have noticed by now is that we start the
Indexes and arrays at 0 so when I wanted to access this 4 instead
You might think that 4 would be at index position 1 because it's
but in C we start array indexes at 0 so actually the first
element this 4 is gonna be at index position 0 so if I was going
I would say like 4 is at index position 0 this 8 is at index
So that's gonna allow me to access all these elements inside of
Individually, so I could print them out just by referring to the
Another thing I can do is I can modify some of the elements inside
Let's say I want to modify lucky numbers in x position 1 so let's
so I don't like this 8 anymore so I can basically just assign this
I can make an opening close square bracket
we can say 1 and I can just set it equal to something else so I
Now when we print out lucky numbers 1 instead of printing out an
200 so let's go ahead and do that and run my program and you'll
200 instead of 8 so you can modify any of the individual elements
Inside the array just by referring to the index and really when
Conceptually an array is basically just holding a bunch of
So when I access lucky numbers 1 it's the same as me accessing an
It's just that you know, the array is holding potentially hundreds
So an array is a very useful structure, especially if you're
So, like I said, I could do this with an N
I could also do this with a double I could do this with a float I
But there's gonna be situations where you might not necessarily
So let's say I'm creating an array and I don't know what numbers I
So maybe I just like want to create it
I want to tell C that we need it but I don't want to necessarily
Well up here. We're basically saying lucky numbers that open and
But if we don't want to give it all that information instead
I can just put a call a semicolon here but inside of these square
I need to tell see how many elements that this array can hold so I
Like hey, this array can only hold like ten elements or this array
So let's say we wanted an array that could hold like ten elements.
Capacity of this integer array is 10 so it has the potential to
and what I could do is I could come down here and I can start
Lucky numbers 1 is equal to 80, right?
so I'm giving lucky numbers 1 of value and now over here I could
You'll see we're printing of 80
but if I try to print out for example lucky numbers 0 so the lucky
This is gonna give me a negative 2 which basically means that it's
Element inside of lucky numbers at index position 0 but if I
0 is equal to 90 and now when I run my program it's gonna be able
So it'll be able to get that value
You're just gonna want to give these arrays values up front right
You're not gonna necessarily know what's supposed to go in there
you still have to tell see how many elements the array is gonna
You still have to like rigorously defined like hey
This array can only hold ten elements and that's just so C is able
Elements, so that's kind of the basics of working with arrays now
I do want to point out one thing that we've been using in this
strings, so I've been creating character strings so I could say
Giraffe Academy or other times in the program we created
Like when we were making our little game
we created an arrays and I would just say like 20 and then I would
Put that information in here. This is basically a
String of characters. So anytime we create a string in C like if I
I called it like array whatever. This is an array
So we've kind of just been taking this for granted
Like I've just been calling this a string or calling it like a
But it's basically just an array of characters
The only difference is this is such a common thing to use in our
C makes it a little C makes it special and makes it really easy
But just like that array of integers. This is also an array
So now you kind of have a better understanding of what strings are
I'm going to talk to you guys about functions in C a
Function is basically just a collection of code that performs a
So what you can do is you can take a bunch of code, you know
maybe like five or six or twenty lines of code and put it inside
Code that's inside the function
You can call the function and generally when you create a function
So the function will have a specific purpose
so I'm gonna show you guys how to create functions how to work
like I said a function is basically just a collection of code
Does a specific task so we can actually create a function here in
Now you'll notice up here. We have this block of code
It says int main and there's an open and closed parenthesis and it
and we've kind of just been using this for the entire course like
Just been kind of using this main little block of code here. But
Sometimes you'll hear people will call this a method as well. So
Essentially mean the same thing, but you generally in C. We're
But this is called the main function and the main function is
Basically a function that's gonna get executed when we start
And so we've already been using a function this main function, but
So make sure you guys have to do that
So let's go down and we're gonna go here outside of this function
The function is we're outside of this ending curly bracket. So
I'm going to create a function and when we create a function
We actually have to give C a couple pieces of information
The first thing we have to tell C is the return type of the
And so actually we're going to talk more about return type in the
But for now just know that return type is basically the type of
So sometimes your function can actually give information back to
We're just gonna say void and void basically means that this
That's the first thing that you have to tell see the second thing
And generally when we're naming a function, we're gonna want to
So in our case, we're gonna create a function that says hi to the
So I'm just gonna call this function say hi because that's what
Whenever I create a function now
I want to make an open and closed parenthesis and I'm gonna make
Code that goes in between these curly brackets. It's going to be
So let me show you guys I'm just gonna make this a very simple
We're just gonna make a printf and we're gonna print out hello
So I've created my function it's called say hi and inside of this
So now let's go ahead and run our program and see what happens. So
But you'll see up here that hello user doesn't get printed out
But this didn't get printed out and this is the first lesson with
Inside of the function we have to call it
Calling a function basically means we're telling C that we want to
So if I want to execute this code I can
And the way that I do that is just by typing out the name of the
So now instead of just printing out nothing C is actually going to
Function so let's run our code and you'll see now we're printing
So the difference between this main function up here and this say
dreapta\nwhen we run our program
this main function basically gets called but then if we want to
Functions we can call them from inside of this main function and
So also I want to show you guys one other thing. I just want to
The flow that these functions take I want to show you guys how
So I'm printing out top and I'm printing out bottom and I'm
So now I'm gonna run my program again and you'll see we're
And then we're printing out hello user then we're printing out
Lines, but essentially what's happening is when C goes to execute
It's gonna execute this first line of code printf
So it's gonna print out top then we're telling C that we want to
Function C is now gonna jump over here to the say hi function and
So in this case, we just have one line of code, but I could put
So if I have like five lines of code, it would execute all five of
Then it's gonna jump back up here and it's gonna move on to the
And that's sort of the basics of writing functions anytime
You have a block of code or a bunch of code that does one thing or
Encapsulate into its own little container that you can call
You can put it inside of a function and then whenever you want to
You just call it, but these functions can actually do a lot more
So one thing we can do is we can give these functions some
Information so I can actually give this function a piece of
This is what's called a parameter and a parameter is basically
So let's say in this say hi function instead of just saying hi to
We want it to say hi to someone specifically
I can actually come down here in these parentheses and I can
Parameter and I can specify a parameter a lot like I would specify
name and an open and closed square brackets
and I'm basically what I'm saying down here is this say hi
Parameter in other words as an input a string
called name and what I can do is I can come down here and I can
Percent s and we could print out name
Over here when I call this say hi function because I specified
I have to give it a string so I could give this a string like Mike
when I call this say hi function now, I'm
Passing the value Mike into this function
So Mike is gonna get stored inside of this name array and we're
Let's go ahead and run our program and you'll see it says hello
We're able to customize what it says
The cool thing about this is I could copy this line of code
Like I could call this function a bunch of times with different
So this is more obvious so I could say hello Mike. Hello, Tom
It's saying hello to each of these different people
because we define the function and we can pass it different names
Depending on what we pass it depending on the parameter that we
You can also specify multiple parameters. So let's say in addition
And then down here I can specify int age. And now we're basically
So I could say hello and then I could say you are and we'll say
So now over here instead of an addition to just saying name we can
Function and when I call it I have to also give this an age so we
70 and now this is gonna print out all that information for us. So
Hello, Oscar, you're 70 and you can pass as many parameters as you
You always just have to make sure though that you're passing a
When you're calling the function just like that. So that's the
This guy over here, which is the return type and we're gonna look
I'll talk to you guys about return statements in C
Where a turn statement is a special line of code that we can
back to whoever called them so I can write a function and that can
information back to whoever called it that could be information
It could be it a message telling whoever called it how the
Basically how we can do that and I'll just give you an overview of
I'm actually going to create a function and we're gonna create a
Cubes a number so when you cube a number generally you take it to
So I could say like 2 raised to the third power
Which is the same as just saying 2 times 2 times 2 this would be
You're taking it to the third power
So I'm gonna make a function that's gonna do that
This function will accept one
parameter which is gonna be a number and it'll cube that number
That value back to the caller and you guys will see how that will
So I do want to point out one thing whenever we are
Returning values in our function. So if you're gonna write a
You always want to put it above the function that's going to be
So for example like this main function down here, this is the
So any code we put in here is gonna get executed first if I want
I'm gonna define the function here above the main method and
It'll just make sure that everything works correctly
So we want to make sure that we define this function before we
so the first thing that I need to tell C when I want to create a
What type of data I want to return if you've been following along
You'll know in the last tutorial
We use the void return type which basically means we weren't going
But now we want to actually return information
so why don't we return a double and you can put any datatype here
Character or character array, but let's just do double and we're
So I'm gonna call this cube and I'm gonna make an open and closed
We're gonna allow this method or we're gonna allow this function
So it's just going to be another double and call it num. So
Basically, what we want to do is we want a cube num and we want to
Here, I'm just gonna create a variable
It's just gonna be another double called result and I'm gonna set
Times num times num. So basically this variable result now
represents or now stores the cubed value of num
all we need to do to return this value now is just say
Result and this return keyword is basically gonna do a couple
The first thing it does is it breaks us out of the function. So
It's gonna break us out of the function and it's gonna return this
So down here we can actually call this function
so actually what I want to do is I want to print out the answer
Answer and we'll print out the answer. So percent F
so I'm using % F because we're expecting to get a double back from
Over here. I can just say cube and we'll pass in a number. Let's
Essentially what's gonna happen now is we're going to call this
All this code is going to execute and this function is going to
3.0. So normally like in the past in this course what we wanted to
76 or you know, whatever we'd have to put a number right there
But we can just call this function because eventually this is
So this will contain a number after the functions done being
So I can actually just run my program now and this is going to
3 cubed is 27 3 times 3 is 9 9 times 3 27 so looks like our cube
Let's try with another number. Why don't we do 7?
So let's run this again and we get 343 I'm guessing that's right
So basically what we did is we created this function and we use
The value of cubing the number and that's really cool
And actually we can make this a lot simpler so I could actually
Result variable and I could just straight up return
Num cubed and this is and this is gonna do the same exact thing so
And one thing I want to point out is this return keyword will
So if I came down here and I said like printf and I printed out
This actually this code is actually never gonna get executed. So
You'll notice that it's not printing out here. So that never gets
Print out here. This never gets touched because when we use this
This breaks us out of the function. So whenever C sees this return
down here to the normal program
So just keep in mind that you can't
really put any code after you use this return keyword because it
You guys about one more thing really quick you'll notice up here
I'm creating my cube function above the main function. So I
and the reason that I did that was because if I was to move this
you'll notice that we're getting an error over here and actually
Conflicting types for cube now
Essentially what's happening is when I create this function down
When the main method over here tries to call it. It actually
like it doesn't necessarily know like what this function is what
Created it after we created this main method. So what I can do is
Prototyping and when I prototype it'll allow me to create this
without getting this error and basically when you prototype I'm
Function signature, so we would call this like the signature
So if I was to put this up here and put a semicolon
You'll see that we're not getting this error anymore, and we're
So this is a way that you can create functions below that main
basically any functions that I created I could put another
In this tutorial and talked to you guys about if statements in C
Programming structure which we can use to help our programs to
So in certain circumstances, they can do certain things and in
They can do other things and if statements are
Extremely powerful and they're a great way to add some
We're gonna be building a function. So we're gonna build a
Basically what this function is gonna do is it's gonna take two
two numbers and it's gonna tell us which number is the biggest so
Ten and it'll tell us which number is bigger. Let's go ahead and
It's going to return an integer and I'm just gonna call it max and
so it's gonna take an integer num one and it's gonna take another
Now we'll just make the actual function body
so inside this function our job is to figure out whether num one
To return the biggest to the caller
so the first thing I'm gonna do is create a variable and it's
I'm not gonna give it a value right away
What we need to do in this function is we need to figure out which
we need to figure out if num one is the biggest or
We need to figure out if num two is the biggest and this is a
To use an if statement an if statement allows us to check a
And if that conditions true we can do one thing and if the
I'm basically just gonna say if and then we're gonna make an open
Inside of here inside of these parentheses. We need to specify a
This is something that's either gonna be true or false if the
We're gonna execute the code inside of these curly brackets. If
In order to tell whether or not num1 is bigger than num2 or num 2
Num 1 and I can use this greater than sign
Num2. I'm basically saying if num 1 is greater than num2
then I want to do something so down here we can set result equal
This condition is true. Then we're gonna do this. So only when
num, 1 is greater than num2 are we gonna set result equal to num 1
Tell which one is bigger but here's the thing what if num 1
Greater than num - what if num 2 is greater than num 1?
I can use something called an else statement so I can come down
I don't actually need to use an open and close parentheses
I can just make an open and close curly bracket
And the code inside of this else block is going to execute if the
so if this condition is false if num 1 is not greater than num 2
Set result equal to num 2 and then down here. Finally. We're gonna
Result so depending on whether or not this is true
result is either gonna set equal to num 1 or it's gonna be set
So let's go down here into our main method and why don't we call
printf and we're gonna be printing out an integer and
Over here, I'm gonna call this function. So I'm just gonna say Max
So we're passing a 4 it will pass in a 10
So I'm passing in a 4 and I'm passing in a 10 and this is going to
Other words this should return 10. So let's go ahead and run our
So we got the value of 10 back from this function. Let's try it
So let's make the first number bigger. So now we have 40 and 10
So this should give us 40 back awesome so over here we have an
We're basically checking to see if num 1 is greater than num - if
Num2 and it's important to note that even if I made these equal
So if I made it forty and forty, we're still going to get back the
So it's still gonna give us forty back. So this is the basics of
We say if we specify a condition if that conditions true
We're gonna execute this code if that conditions false
We're gonna execute this code down here
And that is an extremely powerful structure in our programs and we
dreapta\n
But this is just sort of the beginning. So why don't we try to
So down here we're allowing the user to input two parameters
So we're accepting two parameters into this max function num1 and
But let's up the ante a little bit what if we wanted to accept
So instead of just passing in two numbers
We wanted to be able to pass in three numbers and have the max
This max function, so I'm just gonna make another parameter. So
So now this function is accepting three parameters num1 num2 and
But in order to figure out which one of these is the biggest we're
So I'm gonna get rid of this and let's just start from scratch. So
How can we figure out which one of these is the biggest?
well, what I would recommend is we could say if num 1 is greater
Num, 1 is greater than num 3 then we know num ones the biggest
So if num1 is bigger than num2 and it's bigger than um
3 we know it's the biggest if num2 is greater than num1 and num2
Is greater than num 3 then we know num2 is the biggest and
3 is the biggest so I'm gonna show you guys how to do this and
Concepts for if statement so I'm just gonna say if and I want to
so I basically want to see if num 1 is greater than
or equal to num 2 and num threes so down here I can say num one
Greater than or equal to num2 and now I also want to check to see
Because if num1 is greater than or equal to num2 and it's greater
We know num1 is the biggest so I can use this special signal here.
Basically, this is going to allow us to put another condition in
basically what this and does is it allows us to put two conditions
This whole thing is only going to be true if this conditions true
So if one of these guys is false, then this if statement isn't
I'm just gonna open this up and if this is the case that we want
equal to num 1 because if num 1 is greater than or equal to num -
Num, 1 is also greater than or equal to number 3 then we know num
We also want to check to see if num 2 is the biggest
We also want to check to see if num 2 is the biggest now we can
I can make another open and closed parenthesis and inside of these
So if this stuff up here at this conditions false then else if is
So here I want to do basically the same thing
I'm gonna check to see if num 2 is greater than or equal to num 1
Num 2 is greater than or equal to num 3
So if this condition is true if num 2 is greater than or equal to
Then we know for a fact that result is gonna be num - because
We can just say else and we can make another to open and close
And remember this code is going to get executed when neither of
So here we can just set result equal to num 3
So let me walk you guys through this one more time up here
We have this if statement and in addition to checking to see if
we're also using this special operator called and and we're
That's the case then result is going to be equal to num 1
Otherwise if this condition is false
Then we're gonna come down here and we're going to check this
So we're gonna check to see if num 2 is bigger than num 1 and if
3 if that's true, then we're gonna do this
Finally if neither of these conditions up here is true
We're just gonna go ahead and set result equal to num 3 and then
Let's test this out and we're gonna see if it works. So now when
This is basically going to spit out the biggest one, which should
Let's run our program and you can see over here
we get 3 so let's try to make one of the other ones the biggest so
Biggest and let's run this again and you can see that it returns
So that's how we can use these ands and we can also use else--if
so the and is used to check more than one condition inside of the
Parentheses and the else if is used to check another condition
Now I want to show you guys a couple other things
There's a couple other things that we can do with these if
I'm gonna come down here and just show you guys some of these
In addition to using that and we can also use something called or
I'm just gonna type out a simple if statement I could say if
3 is greater than 2 and instead of using and like this I can use
Check two conditions, and the whole thing is going to be true
if only one of those conditions true so I could say if 3 is
So only one of these is true, right 3 is greater than 2 that's
but 2 is not greater than 5 so this guy's false, but when we use
One of these two conditions has to be true for the whole thing to
Now when I run my program we should print true because we're using
But if I was to make this guy false, so if I made this 3 less than
It's not gonna print out true and we can just print out false. So
Now we're gonna end up renting out false because both of these
So you can see down here. We're saying false. So the difference
Both of these conditions have to be true in order for the whole
When we have or only one of these guys has to be true
so that's kind of the difference there and I want to show you guys
Over here we're using like a less than sign
If 3 is less than 2 I can also use a greater than sign I could use
Could use also a greater than or equal to sign and if I wanted to
I could use a double I could use a double equals
So the double equals will check to see if 3 is equal to 2 we can
exclamation point an exclamation point basically means not equals
3 is not equal to 2 then we'll print out true so down here. Let's
Deci da, acolo te duci. One more thing. I want to show you is how
Operation. So for example, if I said 3 is greater than 2 this is
so we'd end up printing out true down here because 3 is greater
I could surround this whole thing with
Parentheses and I could put an exclamation point right before this
Whatever this ends up being so this is true
And we put this negation operator here this whole thing is going
Because this is true and we're using the negation operator. We're
And you can see we just don't print
But if I put a false condition in here, like if I said three is
This is false. But because we're negating it now, it's gonna end
So when I run my program you can see that we get true
So that's sort of the basics of using if statements and I cover
We can use ands, we can use all these different comparison
We can use ORS and we can use this exclamation point to negate
In this tutorial I'm going to teach you guys how to build a
You'll know that in the beginning of the course. We actually
We let the user input two numbers and we took those numbers we
I'm gonna take some of the stuff that we've learned since then and
functional for function calculator
So this calculator will be able to add
Subtract multiply and divide and we're gonna let the user decide
So we'll let them decide if they want to add or subtract or
it's gonna be pretty cool and we're gonna end up using some of the
statements we're also gonna use like getting input from users and
Input from a user as well. So down here. We want to start making
What I'm gonna do is I'm gonna ask the user to enter a number
Then I'm gonna ask them to enter an operator like plus minus
we're gonna ask them to enter in a third number and then we'll
Depending on what operator they specify so addition and
So the first thing I want to do is create
Variables where we can store the numbers and the operator so I'm
Num 1 and then we'll make another double call it num2
finally, we'll make a variable that will store the operator that
Operator. In regula. So now we want to actually get input from the
I want to figure out what numbers they want to use and then what
So down here. Why don't we get some input?
The first thing I'm going to do is I'm just gonna print out a
And now what we want to do is we want to be able to
Take the number they give us and store it inside of one of those
So I'm just gonna use this scanf function
Scanf and in here I'm gonna specify that we want to get a double
So remember when we're using scanf if we want to scan for a double
F and LF is going to scan for a double and now I'm going to use a
Variable that we want to store in here
so I'm gonna use this ampersand and I'm just gonna say num 1
So this is exactly what we need to do to be able to get the users
The next thing we want to do is get the operator. So I'm gonna
They'll be entering in plus sign minus sign asterisks or forward
and again now we're going to scan for a
Character now when we're scanning for a character and scanf and
We want to do something special. So I want to put a % and a C. But
I want to put a space so whenever we're getting a character from
Right here before % c otherwise, it's not gonna work. And again we
One more time we're gonna get another number. So I'm just gonna
We're gonna get this one for num2
So now we should have all of the input for our program. So I'm
I'm getting the operator and I'm getting the second number. The
So we're gonna have to figure out which operator they wanted to
Right, so we have this Opie variable and this is storing like a
So depending on what that's storing we want to print something
Different out so we can actually use an if statement to do this so
What operator is inside of our operator variable and depending on
Operation so I can say if and the first thing I'm going to do is
If the operator that the user entered is equal to a plus sign then
So I'm just gonna print out these two numbers and I'll just say
Because we're gonna add both of them together. We can also use an
So I'm going to want to check a few other conditions
I'm going to check to see if the operator is equal to a
Sign and if the operator is equal to a minus sign then instead of
I'll just copy this and we'll paste this guy down here
So it's going to be num1 - num2
And I can actually just copy this whole thing and we'll paste it
We'll paste this other elsif and here we're going to check to see
So we'll check for a forward slash. And again, we're just going to
Decimal number so it's going to be num1 divided by num2
Finally, we're gonna check for multiplication. So if it's
So now we're checking for each of the operators. We're checking
But here's the thing what happens if the user enters in an
so for example if they don't enter in any of these operators
We're gonna want to tell them so I'm gonna have this like print
We're just gonna say else and basically the code inside this else
If none of these conditions up here true and down here we can just
So now we have our if statement or if block setup and this should
Alright, so let's go ahead and run this program so you can see
It says enter a number so we can enter a number. Let's enter in
Enter an operator we'll add numbers and now let's enter in 4.0
So we're gonna add five point nine and four
so when I click enter we should go through all of those if
Statements and figure out what we need to do and you'll see over
And we're getting nine point nine. So looks like the program
Why don't we try to multiply some numbers? So let's multiply
Times I don't know five point seven so we get thirty four point
All right, and then one more time we'll run this and I want to try
So we'll say like 5.7 and I'm just gonna enter in a G. So that's
That's not going to work and I'll enter my number. Let's do 8 and
we have a four function calculator this calculator can multiply
Add and if you don't enter in a correct operator
It's smart enough to yell at you and tell you that you have an
This seems to work pretty well. I'm pretty happy with it
And you can see down here like this is a perfect situation for
We have this variable opie, right? It's storing some sort of
We don't necessarily know what's inside of there
But I can use if statements and I can respond to the different
So in the situation where it's a plus sign I can respond in the
And this just makes our programs a lot smarter and it helps us to
In this tutorial I want to talk to you guys about using switch
now a switch statement is basically a
Special type of if statement which will allow us to compare one
And it's essentially doing the same thing as an if statement
It's allowing us to check different conditions and if different
Then we can do certain things but a switch statement makes it
specific value to a bunch of other values now
I'm gonna show you guys how we can use that in this tutorial to
So I'm gonna build a little program that will take a letter grade
It will tell you whether or not you did good or bad on a test
So if you got an a on the test it would say hey you did a good
If you got a B on its house, it'd be like hey you did
Alright, if you got an F on the test to tell you that you failed
so we're basically gonna have a variable where we'll store a grade
Respond to that grade using a switch statement. So it's gonna be
the first thing I'm gonna do is create a variable and it's just
So we're gonna say that whoever took this test got an a on the
Now what I want to do is I want to create a switch statement. So
Switch and I'm gonna make an open and closed parenthesis and
I'm gonna put grade so I'm just gonna put grade and I'm gonna make
So this is sort of like the basic structure for our switch
I'm gonna take this variable here that I pass into the parentheses
So I'm gonna take this I'm gonna see if it's equal to one value
then I'm gonna see if it's equal to a different value or a
Depending on the value that it ends up being equal to we're gonna
cases so down here I can say case and
Character so for example, I could put a right here and then I'm
and when I put a right here, basically what this is saying is the
then I'm gonna put some code down here that I want to execute so I
I could say like you did great
because they did great on the test and
what this is saying is in the case that the grade is equal to a
Which is called break and I'm gonna explain what break does in a
So I could make one of these little case statements for every
so for every possible grade that we could get so I'm actually just
So now I can put a B in here. So we're saying in the case that the
I'm gonna say you did. All right, and
Then we're also going to break and down here. I'm gonna make
So I'll say case and this time we'll make it C in the case that
There's a couple more that we can check so we can check if they
I forgot to put a break statement here and I'm gonna put one down
And finally, we're just gonna do one more for if they failed the
So I'm just gonna say in the case that they got an F
Essentially, what I'm doing here is I'm writing out all of these
So I'm saying in the case that you got an F. I'm gonna print out
I'm gonna print out you did very bad
And then we also have this break statement here and this break
So whenever we're executing this switch statement whenever C is
It's checking to see if grade is equal to each one of these cases
So for example, let's say the grade is equal to a it's gonna
But if I don't put this break statement in here, then it's just
So it's always important to put a break statement in there because
I don't want to be in here anymore. So finally we can check one
Or that we get an invalid grade. So for example, if someone
G or something that's not a valid grade so I can use something
Default is basically like an else so default is going to execute
whenever none of these conditions up here or Matt so then I can
Invalid grade or something and this will basically be like, hey,
So let's take a look at this switch statement and we'll just kind
Basically, I'm passing this in a variable or a value or something
all of these different conditions
We're gonna print you did great and the case that B is equal to
and this is basically doing what an if statement doing so I can
An if statement I can write out if etc else--if else--if else--if
But this just makes it a lot easier
So I don't have to constantly write out all of that if statement
So hopefully we did everything right and let's actually check and
So grade is equal to a let's run our program and see what our
It says hey, you did great because we got an A if I made this an F
Now it should tell us that we failed
So it says you failed if I made this a C
It'll tell us that we did poorly and it says you did poorly
So this is a super easy way for us to check one single value
And that's really the point of a switch statement is you're
Against a bunch of other values and that's where it can be
So consider using the switch statements
They're not appropriate in every situation and there's I'd say
But when they are appropriate switch statements will save you a
I want to talk to you guys about strux in C now a struct is a data
so inside of a struct I could store like an integer alongside of
String alongside a character alongside a double I could sort all
Structure and there's tons of uses for structs and one of the
So I could basically like a model
Something in the real world inside of my program and I'm gonna
We're gonna be looking at how we can use a struct to represent a
So imagine we were writing a piece of software that was using like
I could use a struct to represent a student in my program so over
I'm just gonna come up here above the main method and I'm actually
and how we can use them so I'm just gonna say
Over here, I'm gonna give this a name so I'm gonna call this
Student and a lot of times in C when we're making structs. You're
Capital letter just like that and I'm gonna make an open and
Inside of this struct I can start specifying what types of data
So basically I can define like different attributes of a student
And this will kind of act as a template and you'll see how we can
So let's think about different attributes of a student
Well one thing would be like the student's name so I could say
so this is gonna represent the name and actually why don't we give
String can hold 50 characters maximum and now we're gonna make
So we're gonna want to store the students major and again, we'll
I also want to make an integer for the students age and
So I have this struct student and in here
I have a bunch of after buttes of a student right have like the
An integer that stores their age and a double for their GPA
Essentially what I did was I created like a type of student data
So I basically am allowing myself now to represent a student
so let's come down here to this main method and I'll show you guys
Create an instance of this student structure so I can create like
Inside of my program and the way that I do that is just by saying
Struct and I want to type out the name of the struct that I want
It's gonna be a student and I want to give this a name. So I'm
I can just use a semicolon here
basically, what I did now was I created a container called student
Name a major in age and a GPA
So if you're familiar with arrays in C, you'll know an array is a
But all the pieces of information inside of an array needs to be
Struct I can have a bunch of different data types like this and I
So now let me show you guys how we can assign some values to
So for this particular student student 1 I can give them a name
I can give them a major a GPA and an age so I could say
student 1 dot age and I can set this equal to something so I could
Inside of this student 1 container I'm saying the age of this
I can do the same for the GPA so I can say student GPA is equal to
So I'm saying this particular student's GPA is a 3.2. I can also
So for the name and the major now, here's the thing about working
Remember in C a string is actually just an array of characters and
We can't like give it a particular value
So for example, if I wanted to give this students name of value,
So I can't just come down here and say student name is equal to
That's not gonna work because you can't do that with an array
I can use something called the string copy function and this is a
String and it'll give it a value that we specify so it'll
with the age and the GPA but with the name so I could say like
Str. C py and inside of here. I need to pass this two parameters
The first thing I want to pass is the destination for the string.
student 1 dot name the second thing I want to pass in is the
String that I want to store inside of student 1 name. So in our
So now we have the student 1 names value is equal to Jim and I can
so I'm just gonna copy this and we'll come down here and I could
Business, so let's say Jim is a business major
Essentially what I've done here is I've created a student and that
so this particular student had a name a major in age and a GPA
So I assigned this student 1 an age of 20 to a GPA of 3.2 etc
So now what I can actually do is I can print out all these
so if I wanted for example, I could like
Print out the GPA so I could print out
Student 1 GPA and now we're gonna be printing out
3.2. And actually it looks like I have a typo here. This should be
Alright so here we're printing out 3.2. So we're printing out the
I can also print out like their name. So why don't we do that
So you see we prints out over there
So a struct is a really useful structure
And another cool thing we can do with structs is we could actually
Student so I could create like another instance of that student
I'll show you how we can do this. I'll just come down here and
Instead of student 1 why don't we call this one student 2 and we
Let's say their name is Pam and they're studying art
So now I have a completely different student
And if I wanted I could print out this students attributes so I
Dot name and now we're gonna get Pam instead of Jim so you can see
So I could create as many of these students as I want and this is
Structs is I can just define the basic template for a student in
Individual students down here that I can work with. So now I have
I could do whatever I want with it. I could pass it into a
I could print it out onto the screen
I could use it in something like an if statement I could do
It's it acts a lot like a variable or an array
so remember variables and arrays or just
Containers we can do just about anything we want with them and
so this has just been kind of an introduction and
What you could do as an exercise is think of other things you
So maybe something like a book or something like a phone you could
Information you could store using a struct just like this
In this tutorial I want to talk to you guys about while loops in
now a while loop is basically a structure in the C programming
Continually execute a specific block of code until a certain
So I can specify a condition and then I can basically just loop
there's a lot of times in our programs or we're gonna want to let
Continually do something while something else is true or while a
Is met and I'm gonna show you guys a really basic while loop in
We're actually gonna build a really cool guessing game using while
And you should get a pretty good idea of like how these things
And this isn't directly related to creating a while loop though is
So I'm just gonna create an integer called index and I'm gonna set
What I want to do is I want to actually create a while loop and
So in order to create a while loop
I can just type out while and open and close parentheses and then
And this is basically like the outline sort of like the template
Basically how this is gonna work is up here in these parentheses
I'm gonna specify a condition and as long as that condition is
I'm gonna loop through all of the code inside of these curly
So inside of these curly brackets, I'm gonna put a bunch of code.
I'm gonna execute all that code so
Up here. We're going to specify a condition. I'm gonna specify a
5 so while index is less than or equal to 5, I want to execute all
So I'm gonna put some code in here. The first thing I'm going to
I'm just gonna say we're gonna print out an integer and
I'm just gonna print out index so we'll be printing out index and
Finally after this. I want to increment the index variable so I
Basically, I'm adding one onto the index every time and actually I
Situations where you're gonna want to do something like this
there's actually a shortcut we can use so I can come over here and
This is gonna do the same thing. So this is just gonna add one to
Essentially, what I'm doing is I'm saying I want to print out
Through this code while index is less than or equal to five
So let's go ahead and run this program and maybe you can predict
You'll see over here. I'm printing out the numbers one through
So I'm printing out one two three
Four and five and let me walk you guys through exactly what's
we create this index variable and we set it equal to one and
Basically when C goes to execute this program the first thing it's
does anything else is it's gonna look at this condition and it's
Or equal to five. Well, the first time through index is equal to
It's gonna execute this line of code printing out one
Then it's gonna execute this line of code incrementing index
Then what C's gonna do is it's gonna come all the way back up here
so it's gonna say okay is index less than or equal to five and
Index at this point is equal to two so it's less than five
so it's gonna go through execute this line of code execute this
C's gonna go all the way back up to the top it's gonna check this
So before C goes through this loop
It's always gonna it's always gonna check the condition first. So
It has to check the condition to make sure that it's true
Eventually, we're gonna get to a situation where index gets
then C's gonna come back up here and it's gonna check to see if 6
False and we're gonna move with our lives in the program and
So this seems very simple and that's because it is all we do is
As long as that condition is true
We keep doing all this stuff in here and there's tons of
This is just one of them where we can print out like numbers
I also do want to point out one thing
That you might want to watch out for which is called an infinite
Where this condition up here never becomes false
So this condition will just always stay true and there are certain
But in a lot of circumstances, you're not gonna want to go
So let me just demonstrate so you guys can see so if I stopped
Now when I run my program you'll see that it's just continually
I mean look at all these ones that are getting printed out you can
This program is just it keeps executing through that loop and it's
So this would keep going forever if I let it keep going
but that'll kind of show you like how an infinite loop works and
you'll get into situations as you work with while loops where
That especially if you're doing something a little bit more
Slow your computer down quite a bit. So this is a while loop and
there's also one other type of loop that's similar to a while loop
Before I do that, I just want to do something really quick. So I'm
I'm gonna set this equal to 6 and I'm gonna run my program and
so the code inside of this while loop
Isn't gonna print out because the first thing we always do when we
So the first thing C's gonna do before it prints anything before
It's not gonna do any of this stuff
So when I run this program, you'll see nothing's gonna get printed
There's another type of a loop that's similar to a while loop
Which is called a do-while loop and I'm going to show you how to
All you have to do is take this wild thing up here and paste it
A do-while loop is similar to a while loop
But the first thing a do-while loop does instead of checking this
So even though index is equal to 6
we're still gonna print out 6 and we're still going to increment
Then we're gonna check the condition to see if we can keep
So let me show you guys this and this should illustrate what this
We're printing out 6 so unlike before when we just use that while
We're doing whatever was inside of that loop before we're checking
Come in handy. I would say by far while loops are way more common,
Loops because there are certain circumstances where they'll come
Basically any time you don't want to be checking the condition
But those are the two basic types and there's actually another
Which is called a for loop, but for now
We're gonna have some fun with while loops and in the next
I'm gonna teach you guys how to build a little guessing game and
This is gonna be pretty cool because we're gonna use a lot of the
In order to build it, so let's get started basically
This guessing game is going to allow the user to guess a secret
so we're gonna define a secret number and we're gonna give the
opportunity to try and guess it so if they'll try to guess it if
They don't get it right then
Right, so we'll basically let them keep guessing what the secret
We want to create a couple variables
The first variable I want to create is gonna be an integer and
So I'm just gonna call it secret number and why don't we just set
We want to create is gonna store the user's guess
so this is also gonna be an integer and I'm just gonna call it
Right off the bat. The user wouldn't have guessed a number
So what we want to do is we want to be able to prompt the user to
Dreapta. So as long as the user hasn't entered in the correct
We want to keep asking them to enter it in in order to do that in
Enter it until they've guessed it and we can use something called
So I'm just gonna type while and when making open and close
Let's think about a good condition for our game, basically
We want to keep asking the user to enter an input as long as they
So it's right there we could keep looping as long as the guess is
and basically this is always going to be true until
They guess the correct number now inside of here
we want to prompt them to enter their guests and then we want to
Store it inside of our guest variable. So let's do this first
Scanf in order to get whatever number they enter and I'm gonna
inside of that guest variable so I'm just gonna say at guess and
Or whatever integer they enter and it's gonna store it inside of
So basically what's happening in this loop is every time through
getting a different number and then we're gonna come up here and
It's not then we're gonna go through again and we're gonna keep
But if the guest is equal to the secret number, then we'll break
So I'm just gonna print out a success message and I'll basically
All right, awesome so you can see this isn't actually that many
I mean, it's probably like ten lines of code to write out this
So this should actually work properly. So let's try to run this
So remember a secret number is five
So I'm gonna run the program it says enter a number. So let's just
three so you'll notice I can keep entering in numbers and
It'll keep prompting me to enter a number as long as I haven't
And remember five is the secret number when I click enter now that
Because the guess is gonna be equal to the secret number and it's
So we broke out of that loop we executed the print statement that
Terminated and that is a really cool little game
so one of the cool things about this game and
It's different from programs that we've written in this course in
Essentially infinitely until we enter in that correct guessed
So it's gonna keep going and keep going keep asking us to do
Until we entered correctly. So this is pretty cool now
This is a pretty nice game and it's it's pretty simple
But I want to maybe make it a little more complex one of the
Is that the user gets unlimited guesses, right?
So basically like they can just guess every single number and
So why don't we do this? We should impose a guess limit
So why don't we say like the user can only guess three times and
Then they'll lose the game. So let's think about how we can go
Into this little app, I'm gonna walk you guys through how we can
The first thing we're gonna need to do is we're gonna need to
variables I'm going to create another variable up here and I'm
Guess count is basically going to tell us how many times
the user has tried to guess the secret number and I'm just gonna
Initially, the user will have guessed zero times
so remember this guest count variable is basically gonna get
Right, so I'm gonna come down here. Every time we go through this
So every time we go through this loop, I'm gonna increment the
Guest count plus plus and that means every time the user guesses
The guest count will get incremented and we'll have a accurate
the second variable that I want to make is
Going to be another integer and I'm just gonna call it guess limit
Can guess so this will basically determine like how many guesses
So why don't we set this equal to three?
I think three guesses is a pretty good limit and finally we want
variable and this is also gonna be an integer and I'm just gonna
guesses and I'm gonna set out of guesses equal to
zero and out of guesses will basically
Tell us whether or not the user has any more guesses and this
But keep following with the video and you'll see how this comes
So we're actually gonna have to modify our code a little bit so
So the user can keep guessing as long as the guess isn't equal to
But we want to be able to limit the amount of times that the user
I'm going to create an if statement and every time we go through
I want to check and see if the user has any more guesses left. I
I want to see if they've reached their guessed limit so I'm gonna
Long as the guest count is less than the guests limit
That means they haven't guessed as many times as the limit
Basically, they can guess again, right so down here inside of this
I'm gonna put all of this code and I'm just gonna surround this
so basically the user can only guess the number this code will
Guest count is less than the guessed limit when the user has
Additional guesses right when they haven't guessed as many times
Otherwise though, so if they have guessed too many times
Right if the guest count is either equal to or greater than the
I'm actually gonna set this out of guesses variable equal to one
Basically what this is going to tell us is if out of guesses is
Then that'll tell us that the user is out of guesses. So
Essentially if out of guesses has a value that's more than zero
we'll know that they've run out of guesses and
If you ever use any other programming languages before this is
It's basically storing like a true or a false value, which we're
So if the guest counts less than the guessed limit they can guess
Otherwise we're gonna say out of guesses is equal to one. In other
There's one more thing we have to do though is we have to come up
Condition because right now we're gonna keep looping through this
But there's actually two situations where we want to break out of
The first is when they've guessed the number correctly
Right, then. We want to come down here and print out you win
But we also want to break out of the loop when they've run out of
So I want to check to see whether or not they're out of guesses
so I'm gonna say guess is not equal to secret number and
So as long as out of guesses is equal to zero
That means that they're not out of guesses and they still have
But if the guest counts less than the guessed limit and out of
Then out of guesses isn't going to be equal to zero anymore. So
So basically, there's two scenarios now that will break us out of
They get the word right or they run out of guesses
So one more thing we have to do in this program is come down here
We're always printing out you win. So no matter what like no
We're printing out you win and that's because before if they made
That means that they guessed the secret number correctly
There's actually two scenarios
Down here where they would have broken out of the loop where there
So I want to actually check to see which one we're dealing with.
Of guesses and I'm gonna check to see if it's equal to one and if
That means that they lost and they ran out of guesses. So I'm just
otherwise though, it means that they won so I'm gonna say
You win and this will basically be like hey you won the game
I know that and let's just run this see how we did
And we'll see if our games working properly and then I'll kind of
And actually you know what? I just noticed the typo down here. I
This is just have to be a single equals. So we're assigning this
I had two equals that needs to be one. So let's go over here
We'll run our program and it says enter a number. So why don't we
I'm just gonna say two two and now we're on our third and final
That means we're gonna lose the game
So I'm gonna enter and you'll see it says out of guesses
So we weren't able to guess it within our three tries and the game
Let's run it one more time. We'll try to win so I'll say like two
We're on our third and final guess
So if I don't get it this time
we're going to I'm gonna guess of five and we guessed right so
Basically how this little program works and I'm gonna walk you
so we have all these variables up here secret number is
Just storing the number they need to guess the guest keeps track
The guest count is gonna tell us how many times they've guessed
guess limit is gonna tell us how many times they can guess and
Out of guesses is going to tell us whether or not the user is out
We're checking two conditions for this while loop. We're checking
We're gonna keep looping as long as out of guesses is equal to
We check to see if the guest count is less than the guessed limit
If it is then we ask them to guess we increment the guest count
Otherwise we say out of guesses is equal to one and that's gonna
the first situation is that they ran out of guesses the second
We're using this if statement to check both of those
so that's basically how this works and this kind of shows you how
variables together in like one single program
In this tutorial I'm going to teach you guys how to use for loops
now a for loop is a special type of loop that we can use in C
indexing variable and this indexing variable will basically tell
iteration of the loop we're currently on and we can use that
Loop through an array of items we could do all sorts of stuff
You'll see that I have a while loop
Set up and I have just sort of a basic while loop and I want to
Essentially up here I'm saying int I is equal to 1 so I'm giving
I the value of 1 and I'm saying wow I is less than or equal to 5
I'm gonna print out the value of I and then I'm gonna increment I
basically, we have this variable I and every time we go through
Greater than 5 so let's run this program and we'll see what it
So you'll see over here. We're basically printing out values
so the first time that we go through this while loop we're
The second time we're printing out to the third time. We're
Basically telling us what iteration of the loop are currently on
so on the first iteration of the loop I is
telling us that we're on the first iteration of the loop, right
Second time we go through the loop
I is equal to 2 third time I is equal to 3
So this variable I over here is basically telling us how many
So on the third time, it's telling us 3 fourth time for etc, and
This is actually a very useful thing for us to have when we're
It's there's a lot of situations where you're gonna want to know
when you're looping so with a normal while loop like I can
You can kind of just do whatever you want with it
but in a lot of situations and there's tons of these situations
That will tell us what iteration of the loop that we're currently
Basically just a variable that's gonna keep changing every time we
situation in see that there's actually something called a for loop
Condense it into its own single loop. So it's taking this
It's basically allowing us to do it a lot easier and a lot
So I'm gonna show you guys how we can create a for loop
I'm gonna come down here below this while loop and I'm gonna
So I'm just gonna say for I'm gonna make an open and closed
the difference between a for loop in a while loop is
Basically gonna happen inside of these parentheses. So in the
Dreapta. This is specifying whether or not we can keep looping
We're gonna have three different things. So instead of just one
We're actually gonna have three different things that we want to
with a for loop the first thing I'm gonna put in here is
This variable I so you'll notice in the while loop
we have our variable I up here and this is basically
Allowing us to loop through and keep track of how many times we've
So what I can do down here is I can do something similar I could
I'm gonna say I is equal to 1 so basically I'm gonna take my
Of 1 and now I have my I variable I have my variable that's gonna
The next thing I want to do is include my looping condition so up
We have I as less than or equal to 5, that's our condition. I can
So I'm gonna say I is less than or equal to 5
The third thing I want to do is increment I so you'll notice down
Every time we go through this loop, we're incrementing that
So you'll notice I have these little like sections. Here's the
We're initializing the very I were saying I is equal to one
Here we're specifying our looping conditions
so i'm saying we're gonna keep looping while I is less than or
Over here is a little line of code. That's gonna get executed.
So here I'm saying I plus plus I could also say like I minus minus
II could say like I is equal to I plus two
Etc. Like I could do basically anything I wanted over here. Let's
so you'll see I was basically able to take all of this code and
Instead of having to like print this out and create this variable
Inside of this for loop now, I can basically take this line of
I can paste it down into here and we essentially have the same
So right now this block of code and this block of code are 100%
They're doing exactly the same thing
So let's go ahead and get rid of all this code and let's test out
So I'm just gonna run my program and you'll see over here. We're
We're printing out one two, three, four five. So it's the same
doing the same exact thing and that's why for loops are great
Structure like where we have an indexing variable and we can use
so I want to show you another situation where these four loops can
We can use them to loop through all the elements inside of an
I have this array that I created. It's called lucky numbers and
We have this lucky numbers array. It has 4 8 15 16 23 42 and
What I could actually do is I could loop through all of the
From this for loop so I'm gonna show you guys how we can do that
Let's first off see how many elements we have
we have 1 2 3 4 5 6 so we have 6 elements in this an array so
what I'm gonna do is I'm actually gonna say I is equal to 0 and
Second but basically array indexes start at 0
So this first element in the array is at index position
zero and just for a little refresher if I wanted to access one of
Was zero and this is going to give me access to this element
If I said lucky numbers two, then I'll get access to this element
So this is basically how we can access an element inside the
so I'm gonna set I equal to zero and I'm gonna say I want to loop
Six was how how many elements we had in the array?
So I want to keep looping as long as we're less than six and I'm
Now down here. I'm gonna do this same exact thing but instead of
So I'm gonna print out the array element at index position. I and
Basically the first time we go through this loop
We're gonna be printing out lucky numbers zero
Because I is going to be equal to zero the second time we go
We're gonna be printing out lucky numbers one because I is gonna
And we're gonna keep doing that
Until we get all the way up to five which is gonna be the last
So let's go ahead and do that. So I need to put this back to I
Let's run this program. So you'll see over here
We're basically doing exactly what I said. So the first time
We're printing out that first element the second time through the
15 16 23 and 40 - so we're printing out all of those elements in
There's a lot of situations where for loops are gonna come in
but this is a very very very very
common situation where we want to loop through all the elements in
You know do something to them, whatever
so that's the basics of working with for loops and I do just want
Anything that you do with a for loop you could do with a while
I basically showed you guys how we transform that while loop into
Convenient so it makes it really easy to do something like this
In this tutorial I'm gonna talk to you guys about two topics in C
Two-dimensional array is basically a situation where we have an
The second thing I want to talk to you guys about is nested.
So we're gonna look at how we can use a looping structure where we
Inside of loops. It's gonna be pretty cool
and actually these two topics can go together really well and I'm
Two-dimensional arrays in order to make an awesome program. So
the first thing I want to show you guys is
two-dimensional arrays so down here
we can create a 2d array and actually the concepts that I'm
Multi-dimensional arrays so not only two dimensions but three four
So in order to create a two-dimensional array, I'm just gonna make
So I'm just gonna say int and we'll give this a name. So why don't
Normally when we create an array after we'd say the name of the
But when we create a two-dimensional array
we're gonna make two open and closed square brackets just like
Width and the height of our array so you guys will see what I mean
but basically we're gonna have like
Elements in the array and then each of those elements is gonna be
So these two squares will allow us to like manipulate all that
An open and closed curly bracket now, this is normally how we
So normally I would just say like 1 2 3 4 whatever I could put all
But with a two-dimensional array all of the elements are gonna be
Arrays inside of here. So for example, I'll put an array right
so for example the first element in this array, I could just make
So you can see here. I have two elements
Inside of this array which is itself the first element of the nums
We'll come down here and we'll make another one and this keeps
So now we're gonna say three four
So this thing right here this whole array, that's the first
Right and that array has two elements inside of it. Same thing
This is the second element in the numbers array and it has two
So this is going to be five six
So now we have three array elements right one
Three and each of them has two elements inside of them
So whenever we create a two dimensional array like this
We always have to specify the number of elements and then the
so in our case, we're going to have one two three elements in the
Each array has two elements inside of it
So it's gonna look like that so we would say three and then two
So now let's talk about accessing these elements. So I'm gonna
We're gonna print out some of this stuff. So I'm gonna
Say % D and I'm gonna show you guys how we can access individual
So basically I'm gonna say nums and let's say that I wanted to
the first thing I want to do is specify the index where
The value that I want to access is stored
So like this would be index position 0 this would be index
then I want to specify the index position of the
individual element inside of 0 so I could say like this is element
So I print out nums 0 0 and this is gonna print out that
Numbers coming out. It's gonna print out that one for us. You can
So let's try a different one. Let's try to grab this for right
One and then one so this would be at 1 1 and now we should get
so that's basically how we can access elements inside of these
if I didn't want to give this an initial value
I could just like put a semicolon here and I could just like
Manually define each index location so I could say like 0 0 is
like I don't have to give it a
Value like right up front although in our case. Let's just do that
All right, so we have our numbers array and we figured out how we
So now I want to talk to you guys about another
Concept which is called a nested for loop and you guys will see in
but a nested for loop is a situation where we have a for loop and
inside of that for loop we have
Another loop so I'm gonna show you guys this really quick. Let's
I'm gonna create two variables over here in I and int J and
I don't know if I showed you guys this in the course yet
But if I want to just like declare two variables
I can just say I comma J and that will declare both the variables.
We're gonna do that inside these four loops
So I want to show you guys how we can use a nested for loop in
inside of this two-dimensional array, so I'm gonna say for I
and we're gonna keep looping as long as I is less than
Three and the reason I'm saying three here is because that's how
so I'm gonna keep looping as long as I is less than three and then
Now inside of these curly brackets, I want to create another
loop, so every time we go through this one iteration of this top
J is less than 2 and the reason I'm saying 2 here is because
Inside of the nums array and you guys will see in a second. De
this is gonna work and then I'm gonna say J plus plus so now I'm
Down here. I'm gonna actually be able to print out all of the
So I'm gonna go ahead and do that
I'm just gonna say printf and we're gonna be printing out an
and I'm gonna print out nums I and
So whatever the value of I is and whatever the value of J is
inside of nums that we're gonna print out and now just so this is
Formatted a little bit better. Why don't we put a comma?
Right there and then I'm also gonna put another printf over here
And you'll notice that this printf is outside of this inner for
We're gonna see what it does. You guys will see exactly what's
And you'll see over here when we ran the program
We're basically getting this entire 2d array printed out. So I'm
So let's talk about why that happens
Over here I'm saying for I is equal to 0 I is less than 3 I plus
So I'm going through this particular four loop three times
I'm gonna execute all the code inside of this four loop right
Three times that's as many times as there are elements inside of
Now every single time I go through this top loop every single time
I'm going to execute this loop in its entirety. So I will loop
I'll go through all of its iterations and this loop says J is
2 over here is how many elements are inside of each one of these
So this element in the noms array has one two elements inside of
This element in the numbers array has one two elements inside of
That's where this two is coming from
Then I'm incrementing J. And I'm gonna print out so I'm saying
so the first time we go through this for loop I is going to be
Remember that first time we're actually going to be going through
So we're going to be printing out nums 0 0 and nums 0 1 and then
the next time we go through this I loop we're gonna come down here
Finally the third and final time. We're gonna print out nums to 0
That's basically how this is working. So
Nested for loops are a match made in heaven and there's a lot of
Where you know besides just looping through 2d arrays that we're
But hopefully this gives you a little bit of a introduction into
I'm gonna talk to you guys about accessing memory addresses in C
Now in the C programming language a lot of times we're gonna want
Right, and there's a bunch of different ways that we can store
We can use things like variables we could use arrays we could use
but the basic point is that whenever we're using c we're gonna
Maintain a bunch of different pieces of information and one of the
Variables so down here you'll notice that I have a bunch of
I have this integer called age and it's just has the value of 30
We have this double GPA 3.4 and we have this character grade and
I have a bunch of different
variables in my program and it allows me to keep track of data
You know maintain and I can even modify these different
But I want to talk to you guys a little bit about how these work
So whenever I create a variable for example when I create a
And I give it a value of 30 this value 30 actually gets stored on
Memory, so a lot of times you'll hear people refer to this as RAM
It's called random access memory and basically RAM is the memory
So for example, if I was to run this C program my computer would
It would use that memory in order to store and keep track of all
Dreapta. So when I create a variable like int age C is actually
Location so it's gonna take that value 30 and it's gonna store it
C's gonna take this value 3.4 and it's gonna store it inside of
C is gonna store this character on the physical memory in our
Now here's the thing when I create these variables I give them
So I know what's stored inside of it, right?
And when I want to access this value, I can just refer to the
I can just refer to age I could modify and I could print it out
I could do something else to it
dreapta\n
variable in other words the way that I can access the value 30 is
Just by referring to the name of the variable same goes down here
Right if I wanted to access this capital a I could just refer to
All of this information is stored in our physical memory
And so whenever C needs to access that information whenever our
3.4. It's actually going to refer to a specific memory address
Dreapta. So when I want to access this variable age, I can just
But when C wants to refer to this value, it's not actually using
It's gonna use the memory address where this value is stored
So all of these values are stored inside of a physical address in
So all of these values have an address where they're stored in
So I'm gonna show you guys how we can access that address how we
And basically just wanted to give you guys an introduction into
I'm gonna say printf and I'm gonna show you guys how I can
Where each one of these values is stored on our computer?
So down here if I want to print out a memory address. I need to
normally if I was just gonna print out like a number I could say
Character when we want to print out a physical memory address
We're gonna say % P and this actually stands for pointer. We're
We're just gonna talk about memory addresses
But just know that you need to say % P and now I'm going to type
I want to access so I'm gonna access the memory address of age
Before the variable. I'm just gonna say
so I'm gonna make an ampersand age and I'm gonna say percent P
Now we should be able to print out the memory address where the
In other words, we can print out the memory address where this
You'll see over here. We're getting this number
So I'm getting this number 0 0 6 0 FF 2 0 0 so this would be like
I guess it's not technically a number. It's like I think it's
But basically this is the physical memory address where the value
It's the place where C stored the value inside of this age
I could do the same thing for these other variables
so actually why don't I format this a little bit I'm gonna stay
again, I'm gonna print out another one of these addresses and
so now I can just come over here and I can sort of modify this a
Grade and you'll notice for each of these I'm using this ampersand
So now when we run our program we should get a nice little list of
variables and their corresponding addresses so you'll see over
have age and it's stored up memory address 0 0 6 0 FF OC
GPA is stored at this memory address and grade is stored at this
so if you were to like go into our computer or go into my computer
Go to memory address is 0 0 6 0 FF 0 C that's actually going to
30 it's gonna store the value that's inside of that age variable
If you were to go to this memory address you would see the value
Inside of GPA if you're going to this memory address you would see
so basically all of these variables are store at
Different memory addresses when I want to access the information
I can just refer to the variables name so I could say a age or I
But when C wants to access the information and the variable it has
Addresses so C would use this address C would use this address and
I can just use the variable names and that's why C is awesome
But when C and when our computer wants to access these values it
and so that's why these are useful and
it can actually be pretty useful to be able to know the
Addresses like the actual physical addresses of these certain
So a lot of times in C when we want to work with these
Variables we can just refer to them by name and we can do all
We can access them we can modify them. But in other circumstances,
We're also gonna want to be able to access their physical memory
we're gonna want to be able to know like what that address is and
In future tutorials, I'm gonna talk about why that can be useful,
I really just wanted to kind of give you guys an overview of what
How we're using them in our program and how we can print them out
I just use this ampersand and then I type the name of the variable
In this tutorial I want to talk to you guys about pointers in C
Now a pointer is basically just a type of data that we can use
So up until this point in the course, we've been using a bunch of
we've been using things like integers and an integer is just a
decimal number we've been using
Chars and a char is basically just a character and now I want to
which is called a pointer and a pointer is basically just a memory
you know a physical address inside of the memory of our computer
Storing a value right and I think pointers
Tend to confuse a lot of people who learn about them and this is
Because it's you know, traditionally very confusing to understand
Extremely simple and the problem is that they just get over
So what I want you guys to do is I just want you guys to think of
That's all it is. It's just a type of data that we can work with
It doesn't have to be any more complicated than that, right?
I can work with an integer in my program an integer is a whole
I can work with a double in my program a double is a decimal
I can work with a char in my program a char is just a character
I can work with a pointer in my program a pointer is just a memory
Asta e. A pointer is just another type of data that I can use and
Inside of my programs and it just happens to be a memory address
It's very simple people overcomplicate pointers and they don't
Just like an integer or a double but instead of being like a whole
So hopefully that makes sense and I hope you think you guys don't
Try to over complicate this too much because it doesn't have to be
I have an integer that I've created and it's called age. So I just
Inside of this integer. I'm storing a whole number
I'm storing an integer right third
And if you've been following along with this course in the last
We talked about memory addresses and we talked about how all of
Physical addresses on our computer's memory so down here
I have this print statement and I'm just printing out ages memory
Ampersand age and we talked about how this ampersand when I put
It's going to give me the physical address in memory where this
Thermia value is stored. So if I run this program you guys will
Hexadecimal number right? This is the physical memory address of
What did we just talk about before remember what I told you
Memory addresses that's what they are
it's a type of data a pointer is a type of data in our program
It's a memory address right when I use this ampersand and I type
This is giving me the memory address right I'm able to print out
This is a type of data in our program. That is a memory address
That's a whole number and a double is a type of data. That's a
That's a character a pointer is a type of data. That's a memory
This guy right here is a pointer. So it's just the memory address
and when I want to print out a pointer I can use this percent P
It'll allow me to print it out onto the screen and everyone's
So essentially what I'm doing here is I'm printing out a pointer
I'm telling this print F function that I want to put on a pointer
It a physical memory address. That's what a pointer is now
We understand and hopefully understand that a pointer is just a
We can access the memory addresses of specific variables by using
The name of the variable and that can be pretty useful
Now that we understand that I want to show you guys how we can
I have an integer variable called age and this integer variable is
I'm gonna say that one more time. I have an integer variable and
I could also create a double variable so I could create a double
Inside of this double variable. I could store a double value like
Called grade and inside of this char variable. I can store a
could also create a pointer variable and
Inside of that pointer variable I could store a pointer
So just like I stored a character inside of this character
I could store a pointer inside of a pointer variable so we can
Now here's where this is going to get a little bit tricky
When we create a pointer variable
we actually need a physical memory address right so when I create
Integer variable like I can just come up with a number right off
I can just say like 30 when I create a
Double variable I can just store whatever number I want to store
but when we create a pointer variable remember
We're gonna be storing a memory address and I don't necessarily
Know any memory addresses like right off the top of my head at
So when I create a pointer variable, what I want to do is store
That's already in a program. So I'm gonna say that one more time
inside of that pointer variable
I'm gonna store the memory address of a another variable inside of
so let's say that I wanted to create a pointer that would store
This variable over here so thats or the memory adjusts of age,
can create a pointer just like this so I could say int and
asterisks and now I'm gonna give this a name so I'm gonna give
Pointer variable a name and remember this pointer variable is
variable and generally when you're naming a pointer, you're gonna
Then you're gonna type the name of the variable whose memory
So I'm gonna say P age right because this pointer variable is
Now what I want to do is set this equal to the memory address of
Which we can access using this ampersand so I could say ampersand
so now this pointer variable is
Storing the memory address of the age variable
I'm gonna do the same thing down here for this GPA
So if I wanted to create a pointer variable that would store the
asterisks and I'm just gonna say P GPA just like that and I'm
This double variable which we can access using this ampersand and
I'm gonna do the same thing down here for this character variable
So I want to create a pointer variable which is going to store the
The character variable so I'm just gonna say char
grade, and I'm gonna set this equal to
Ampersand grade. Okay, so that's all I'm doing, right?
So now I have this integer variable and I have this pointer
Which is storing the memory location or the memory address of the
I have this double variable which is storing a double and I have
The pointer just happens to be the memory address of the GPA
I have this char variable down here
And then I have this pointer variable, which is storing a pointer
the physical memory address of
the great variable in our memory
Okay, and that's basically all you need to know about pointers to
It's just a type of information that we can work with in our
it just happens to be a memory addresses write an
integer is a type of data that we can work with in our programs
double is a type of data that we can work with in our programs and
char is a type of data that we can work with in our programs and
Pointer is a type of data that we can work with in our programs
Hopefully that makes sense and just like we create integer
Pointer variables the only difference is whenever we create a
We're gonna store the memory address of another variable in our
So so you'll see over here when I create this pointer variable.
The variable whose address I'm storing so over here when I'm
When I'm storing the address of a double variable I'm saying
I'm saying char here and that's the basics of using pointers and
Also creating pointer variables and storing memory addresses
In this tutorial I want to talk to you guys about
Now a pointer is basically just a type of data that we can work
So sometimes in our programs we're gonna want to work with memory
We're gonna want to use them in our programs and do certain things
pointers in our programs and when you dereference a pointer
Essentially what you're doing is you're going to the memory
Pointer and you're grabbing the piece of information from there.
So all points are as our memory addresses and when we dereference
We go to that physical memory address and we get the information
so I'm gonna show you guys how we can do that and see it's
Down here. I have this integer and
It's an integer variable and it's storing the integer value 30 and
memory address of 30 so it's storing the memory address of our age
Down here. I'm just printing out this pointer
So I'm printing out pH I'm using this percent P and I'm just
So why don't we take a look and see what we get. So you'll see
We just get this memory address probably exactly what you expected
I want to show you guys how we can dereference a pointer
So this pointer variable P age is storing the memory address of
We basically were given the physical memory address where this
So P age is storing the memory address of 30. It's storing the
Inside of our computer's memory if I want to dereference a
Basically, what I'm gonna be doing is I'm going to be going to
So the way that I can dereference a pointer is by using the
So when I'm printing this point, I can dereference it by using
But now over here since I'm dereferencing this pointer
I need to print out a number instead of a pointer because when I
This is no longer a pointer. This is no longer a memory address
This is gonna be whatever was stored at the memory address. The
Variable was storing the address of an integer
Right, so when I dereference it
This is actually gonna be an integer
So let me show you guys I'm gonna print out or I'm gonna run our
So you'll see that we get a 30. So basically what's happening here
Pointer variable we're dereferencing the pH pointer which
Physical value that's stored at the memory address that the
And that's basically what dereferencing is, you know, you're you
You can dereference it and you basically just get the value that
It's essentially all we're doing so I want to show you guys how we
Just normally so for example
I have this age variable if I just typed this out
If I wanted to get the address of this age variable I could use
That's basically what we did up here
but what I could do is I could just dereference this so I can say
asterisks here and this is actually gonna
dereference that entire thing
So now when I run my program it's gonna print out 30, so it's not
It's just gonna print out 30 so you can see over here. We get 30
so I could actually do this like as many times as I want and this
An ampersand here and now I'm gonna get that memory address again
So you'll see we're getting this memory address and I can do the
You'll see now we're getting that 30 value again
asa de\n
Then I'm dereferencing and getting the value then. I'm getting the
So, you know, hopefully that makes sense when you dereference a
you're basically just getting the value that that pointer is
memory address of the pointer
In this tutorial I want to teach you guys how to write two files
One of the cool things we can do in C is we can actually modify
So in this tutorial, I'll just basically show you guys how to do
We'll talk about creating files writing files and appending on to
So let's go ahead and do that down here in my main function
We're actually gonna create a file. So I'm going to create a file
the way that we can do that is we can just type out file all in
essentially what we're doing here is we're creating a pointer to a
I'm just gonna say file and I'm gonna make an Asterix and I'm just
So I'm just gonna call this F
Pointer and that'll just stand for file pointer, and I want to set
Open an F open is actually a function that's gonna open a file for
And so inside of these parentheses, I'm gonna give this a couple
The first parameter is gonna be the name of the file that I want
employees dot text and the second parameter is gonna be a
File mode and a file mode is basically just going to tell C
What we want to do with the file that we're gonna open
And there's a bunch of different file modes. The three most basic
So if I put R in there, I mean we want to read the file
W stands for right so if I put right in there
it means I want to either create a new file or write over an
There's also a which stands for append and that means I want to
we're gonna look at W and a so what I want to do over here is
Right now employees dot text. This file doesn't actually exist
So what I'm gonna do is I'm actually going to create that file
We're gonna put some information inside that file and then that
We're gonna need this write mode
So it's just going to be a W
Anytime that we're open file in see you always want to make sure
I want to use another function called F close and
Basically what this is going to do is it's going to close off the
so it's gonna remove the file from the memory on our computer and
if we do make any changes it'll kind of like save the file and
Everything will just be kind of like locked in so you always want
It and inside of this F close function. We just want to pass in
Pointer right there. So let's talk about what's happening here
Essentially like a data type I guess and we're creating a pointer
So if you remember a pointer is basically just a memory address
Right a pointer is a type of data that we can use in our programs.
this pointer is storing the memory address of this new employees
Essentially what's going to happen is we're gonna create this
Pointer is going to be pointing to the memory address of that file
So that's essentially what's happening here. And what I want to do
To this file. So actually let me just show you guys really quick.
So without doing anything else just using this line right here
Employees dot text is going to get created so over here
I'm in my little directory and
If you don't specify an absolute path or like a relative path for
It's just gonna create it in the directory that your C files in
So if I wanted to create this on my C Drive, I could say like C
Whatever and then like employees out text and this will get
Or even if I don't specify a relative path
then this is just going to get created in the same directory as
My main dot C file is right here. It's in this folder
So when I run this program, let's watch over here and we can see
So I'm gonna run my program and you'll see over here that this
So if I actually bring this out a little bit you'll see it's a
Its employees dot tax and there's zero kilobytes inside of this so
But this file got created. So now let's talk about putting some
I'm basically just gonna list out a bunch of like employees that
The easiest way to write information to a file is just to say F
Print F and this is a special
Function in C, which actually works a lot like the printf
So if you've used the printf function and if you're following
We've been using it throughout the entire course the printf
Printf is it can allow us to essentially just write information to
I need to give this a couple parameters. The first one is gonna be
I need to tell this function, you know what it should be writing
Then over here we can just give this some information so I can
Can just use this like I normally would the printf function so I
So I could say like let's say one of the employees is Jim
And we'll put like what they do. So like I can say sales men and
then we could say actually let's make a new line and we'll create
receptionist and then we can make some other employees so
So now I have three employees that I'm going to basically write
We can go over here to this employees file
You'll see when I open this up that we have these three lines in
So we have Jim salesman Pam receptionist and Oscar accounting
So basically I was able to create this file and write all this
Information in the file from down here just from down here in this
And I want to show you guys with right so when I'm using this,
This is actually going to overwrite a file. So for example if I
Overridden in here now when I run this program and
We open this employee's dot text file. So let me reopen this
You'll see it just says overridden so I've actually overridden all
And that's basically what happens when we just write to a file. So
I'm just gonna run this again. So we have this text
And you'll see now we're back with the employees so let's say that
We wanted to add an employee on to the end of it
So we wanted to add like a new employee on to this file, right?
Well, I can use something called append and what append is gonna
To the end of this file or actually just append text on to the end
I'm just gonna say a and you'll notice I'm still keeping employees
another employee into the file
The first thing I'm gonna do is print out a new line because and
There actually isn't a line after this last line so the last like
I can't actually get on to the line below Oscar
So I'm gonna want to go ahead and print a new line and this will
Kelly and she's gonna work in customer
Service. So now when I run my program instead of overriding what
This is just gonna append this new employee onto the file. So I'll
And let's open this employee's file and you'll see now we have
We either create a new file and add content onto it or we just
At least using this F printf function and when we append, we're
so just using those two things, I mean you can
essentially write out any you want
and it's important to note that you can write more than just text
HTML file or I could write like a CSS file
you could write really I mean any type of file that you'd want
That's basically how we can write to a file and append it to the
I will not teach you guys how to read the
Information from a file in C in the last tutorial we talked about
So we looked at how we could create a file overwrite a file and
I'm gonna show you guys how you can read
Information from a file so down here you'll notice that I have my
So I just say file and the asterisks and I'd called it FP and
this is just a pointer which you know and a pointer is just a
Memory address and it's the memory address of this file in
Memory, so it's the memory address of this employees dot txt file
Opens this file and then I'm specifying a file mode
We talked about the W file mode and the a file mode now
I want to talk to you guys about the our file mode and this stands
So when I use the our file mode
I'm actually able to read information from the file and this can
So in order to read the information in the file
I'm actually going to show you guys how we can read like the
So actually first let's look at the file
This is employees dot txt. It just has you know, basically a bunch
We're gonna be able to read this file inside of our program
so the first thing I want to do is
create a variable and I'm gonna create this variable up here
it's just gonna be an array of characters and I'm just gonna call
I'm gonna give this an initial size of 255 so be able to store 255
Order to read this file. I can actually read the file one line at
Gets and F gets basically is going to allow us to read the lines
Gets and I need to give this a few different parameters the first
I need to give it is a place to store the information that it
So this function is going to go and it's going to read the
So the first parameter we need to give this function is where it
We're going to store it inside of that align variable that we
size and this is basically just gonna be like
The maximum size that we can read in so I'm just gonna say 255.
Then we also want to give it that file pointer
Pointer and basically what this is gonna do is it's going to read
Inside of this line variable. So if I was to come down here and
Just print out line now when I print out a line
It's actually going to be have the value of the first line in the
You'll see over here. We're printing out Jim salesman
And if you didn't remember that was the first line inside of that
So basically what F gets is doing is it's reading the first line.
Essentially pointing to where the file is in our memory and
So it's basically gonna move past the first line in the file
So if I was to use this again?
For example if I said f gets again
It's gonna store the value of the first line of the file inside of
And when I use it again, it's now gonna grab the second line in
This should actually be storing the second line in the file. So
Receptionist I can basically do this for all of the lines in the
So that's basically how we can use F gets to read the individual