All language subtitles for C-Programming-Tutorial-for-Beginners_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,970 --> 00:00:07,710 I'm gonna be teaching you guys everything you need to know to get 2 00:00:08,138 --> 00:00:13,738 C is an awesome programming language and it's actually one of the 3 00:00:13,740 --> 00:00:17,070 A lot of modern programming languages are based off of C 4 00:00:17,170 --> 00:00:21,660 so it's a really good idea if you want to get into C or even if 5 00:00:22,028 --> 00:00:26,548 C++ to learn the basics and in this course, we're gonna cover 6 00:00:26,550 --> 00:00:32,910 I'm gonna talk to you guys about installing a text editor and 7 00:00:32,909 --> 00:00:37,138 I'll talk to you guys about you know, what is a program and how 8 00:00:37,808 --> 00:00:41,428 Read the instructions that you give it and we're gonna get into 9 00:00:41,429 --> 00:00:45,628 We're gonna look at things like if statements and loops we're 10 00:00:45,628 --> 00:00:50,248 We're gonna talk about the different types of data that you can 11 00:00:50,250 --> 00:00:54,628 We're gonna talk about things like structures and the functions. 12 00:00:54,628 --> 00:01:00,928 And basically I'm just gonna give you a full overview of all of 13 00:01:00,969 --> 00:01:05,579 So by the end of this course, you'll have a really good 14 00:01:05,799 --> 00:01:09,089 Which you can build on you can kind of go forward and learn some 15 00:01:09,090 --> 00:01:12,868 I'm really excited to be bringing you guys this basic course on C 16 00:01:12,868 --> 00:01:15,929 I'm really excited if you guys to dive in and start using these 17 00:01:15,930 --> 00:01:20,999 So feel free to click around through all the videos and hopefully 18 00:01:25,500 --> 00:01:32,088 I'm gonna talk to you guys about getting everything set up to 19 00:01:32,090 --> 00:01:34,010 We're actually going to need two things 20 00:01:34,010 --> 00:01:38,689 The first thing we're gonna need is an environment where we can 21 00:01:38,909 --> 00:01:43,728 Now there's a bunch of these different environments. Essentially. 22 00:01:43,728 --> 00:01:45,769 so anything that can you know, allow you to 23 00:01:46,349 --> 00:01:51,349 Write text and then save that text in a specific file format is 24 00:01:51,629 --> 00:01:58,789 although when we're working with C a lot of times it can be useful 25 00:02:01,409 --> 00:02:08,329 Development environment. Basically, this is a special text editor 26 00:02:08,330 --> 00:02:13,969 And so I'm gonna show you guys how to install an IDE called code 27 00:02:13,969 --> 00:02:20,599 We need to write our C programs is going to be something called a 28 00:02:20,599 --> 00:02:22,370 It means we can basically write out 29 00:02:22,370 --> 00:02:28,129 You know instructions that we want to give to the computer but 30 00:02:30,209 --> 00:02:36,618 Compiled which basically means like translated or transformed into 31 00:02:36,620 --> 00:02:43,009 So we're gonna basically download and install a special program 32 00:02:43,009 --> 00:02:45,199 I am going to come over to my web browser and 33 00:02:46,379 --> 00:02:52,489 I'm just gonna go up to the Google search bar. And I'm gonna 34 00:02:53,129 --> 00:02:58,788 Code blocks C. And this link should come up. It's code blocks org 35 00:02:58,789 --> 00:03:01,250 This is an integrated development environment 36 00:03:01,250 --> 00:03:08,118 It's basically a text editor that will make it really easy for us 37 00:03:10,199 --> 00:03:14,298 There's a bunch of options here one says download the binary or at 38 00:03:14,459 --> 00:03:18,468 We want to click on download the binary release. This is going to 39 00:03:18,870 --> 00:03:23,870 So if you're on Windows or Linux or Mac, you can install it from 40 00:03:23,870 --> 00:03:28,009 So depending on the operating system that you're on you want to 41 00:03:28,409 --> 00:03:33,679 So I'm gonna come down here to the windows option. You'll see 42 00:03:33,989 --> 00:03:41,158 So there's a bunch of options here. What we wanted download is 43 00:03:41,800 --> 00:03:44,550 And then the version number and then it says min 44 00:03:47,080 --> 00:03:50,850 Basically what this is is it's gonna allow us to install code 45 00:03:50,849 --> 00:03:56,489 So we're gonna be able to install that IDE program, and we're also 46 00:03:57,009 --> 00:03:59,639 Compiler. So remember I said we needed those two programs 47 00:03:59,639 --> 00:04:04,769 We needed those two things and this is actually going to give us 48 00:04:04,900 --> 00:04:09,120 So I'm gonna come over here and click on the link to download this 49 00:04:09,340 --> 00:04:12,090 And you can see it's gone ahead and downloaded that 50 00:04:12,129 --> 00:04:17,189 So now let's head over to our downloads folder and I'm just gonna 51 00:04:17,189 --> 00:04:20,040 so this should open up a window where we can set up the program 52 00:04:20,168 --> 00:04:21,509 Let's just click through this 53 00:04:22,829 --> 00:04:27,449 I'm gonna agree the license and basically just leave all the 54 00:04:27,879 --> 00:04:33,358 alright when that's done installing it's gonna ask us to run code 55 00:04:33,550 --> 00:04:37,770 It's giving me this little window here. It says compilers auto 56 00:04:38,649 --> 00:04:41,189 So you can just click on the one up here. That's highlighted 57 00:04:41,189 --> 00:04:48,329 It says GNU GCC compiler and I'm just gonna click set as default 58 00:04:48,329 --> 00:04:50,430 We now have code blocks installed 59 00:04:52,120 --> 00:04:58,199 We're gonna be learning about setting up code blocks writing our 60 00:04:58,199 --> 00:05:01,019 But for now, you have everything you need to get started 61 00:05:04,910 --> 00:05:11,059 In this tutorial I'm gonna talk to you guys about getting 62 00:05:12,379 --> 00:05:17,449 Then this video will basically show you how to get everything set 63 00:05:17,449 --> 00:05:21,829 We're actually going to need two things. The first thing we're 64 00:05:22,730 --> 00:05:29,088 We're going to use the text editor to write our C programs in and 65 00:05:29,089 --> 00:05:34,909 C programs but there's actually a special type of text editor 66 00:05:35,160 --> 00:05:39,499 Integrated development environment and it's basically a special 67 00:05:40,319 --> 00:05:43,308 Write and run and manage our C programs 68 00:05:43,350 --> 00:05:49,909 So that's the first thing the second thing we're gonna need is 69 00:05:49,910 --> 00:05:55,910 So essentially what we're doing when we're using it is we're 70 00:05:55,910 --> 00:06:01,790 We're writing instructions that us humans can understand and work 71 00:06:04,439 --> 00:06:11,209 Translated or transformed into a language that the computer can 72 00:06:11,209 --> 00:06:16,788 It's gonna take our C programs and essentially just translate them 73 00:06:17,129 --> 00:06:22,100 So the first thing I'm gonna do is show you guys how to install 74 00:06:22,379 --> 00:06:26,689 So what you want to do on your Mac is go over to the search bar 75 00:06:28,740 --> 00:06:32,269 Click enter and a window that looks like this should pop up now 76 00:06:32,269 --> 00:06:38,118 The terminal is basically a program that allows us to interact 77 00:06:38,120 --> 00:06:41,750 So we're actually gonna need to use the terminal in order to do 78 00:06:42,120 --> 00:06:48,709 so the first thing we want to do is check to see if you might 79 00:06:48,990 --> 00:06:52,910 You might already have one. So you just want to type in C C 80 00:06:56,399 --> 00:07:01,939 You'll see over here. I have AC compiler installed. So I'm getting 81 00:07:01,939 --> 00:07:05,778 It says like si Lang and it's giving me you know, a bunch of 82 00:07:06,480 --> 00:07:10,819 If you have that, then you have your C compiler installed and 83 00:07:11,100 --> 00:07:14,119 But if you don't have that I'm going to show you guys how to get 84 00:07:14,120 --> 00:07:16,120 So all you want to do is just type in 85 00:07:17,800 --> 00:07:21,990 select and then the space - - install and 86 00:07:22,449 --> 00:07:28,289 Click enter and you'll see I'm getting an error here because I 87 00:07:28,750 --> 00:07:33,660 But if you don't already have them installed and obviously then 88 00:07:33,660 --> 00:07:39,119 Then this is gonna go off and install everything. You need to get 89 00:07:39,120 --> 00:07:44,608 so once you run that Xcode select command just type in CC - V 90 00:07:45,160 --> 00:07:47,489 You should be getting this version number 91 00:07:48,009 --> 00:07:50,519 Alright, so now that we have our C compiler installed 92 00:07:50,519 --> 00:07:52,979 What we want to do is download an IDE 93 00:07:52,980 --> 00:07:58,139 So we want to download a special text editor that we can use to 94 00:07:58,139 --> 00:08:05,459 To my web browser and I'm up here at a website called code blocks 95 00:08:05,459 --> 00:08:07,459 It's one of the most popular free 96 00:08:07,689 --> 00:08:13,230 IDE s for programming and C. So we're just gonna come down here to 97 00:08:14,680 --> 00:08:20,850 There should be a few options here download the binary released 98 00:08:22,829 --> 00:08:28,439 We have a bunch of options for windows linux and mac click the mac 99 00:08:28,439 --> 00:08:32,009 You'll see that here is what we need to install code blocks on the 100 00:08:32,009 --> 00:08:37,889 So if you go over to the right side here, there's a link to 101 00:08:38,049 --> 00:08:40,049 So I just clicked that link and it should start downloading 102 00:08:40,570 --> 00:08:46,920 Automatically when that finishes downloading, let's head over to 103 00:08:46,919 --> 00:08:48,919 so I'm just gonna double click on this and 104 00:08:49,179 --> 00:08:51,598 They should give us code blocks now 105 00:08:51,600 --> 00:08:56,908 All you want to do is just drag and drop code blocks into your 106 00:08:56,909 --> 00:09:02,938 So you have code blocks installed and ready to start following 107 00:09:07,240 --> 00:09:11,798 In this tutorial, I'm gonna talk to you guys about getting our 108 00:09:11,799 --> 00:09:17,889 So we're gonna load up a C file and we're gonna be able to get 109 00:09:17,889 --> 00:09:20,979 so I'm gonna open up my codeblocks program and this is the 110 00:09:21,678 --> 00:09:24,188 IDE that I'm gonna be using for the rest of this course 111 00:09:24,289 --> 00:09:29,259 So I'm just gonna double click on it and it should start opening 112 00:09:29,259 --> 00:09:33,128 There's a bunch of different options like create a new project 113 00:09:33,470 --> 00:09:36,160 So we're actually going to want to create a new project 114 00:09:36,159 --> 00:09:38,980 So whenever we're gonna start writing some C files 115 00:09:38,980 --> 00:09:41,528 We're gonna want to create a new project in code blocks 116 00:09:41,629 --> 00:09:47,379 so you can either click this button right here, or you can go up 117 00:09:48,589 --> 00:09:52,539 projects so either way it's gonna bring you to this window over 118 00:09:53,089 --> 00:09:54,790 There's gonna be a bunch of options here 119 00:09:55,009 --> 00:10:01,927 Just these are different types of like C projects that we could 120 00:10:01,928 --> 00:10:06,759 This is like there's a basic C project that you can run on your 121 00:10:06,759 --> 00:10:12,759 So click on console application and then click go and I'm just 122 00:10:13,458 --> 00:10:16,958 C++ or C now C and C++ are both 123 00:10:17,600 --> 00:10:20,949 Different programming languages for this course, we're gonna be 124 00:10:20,948 --> 00:10:24,127 So just highlight C and click Next and we're gonna give this a 125 00:10:24,129 --> 00:10:27,788 I'm just gonna call mine draft and we're gonna have to put this 126 00:10:28,009 --> 00:10:34,089 Inside of a folder so I'm gonna open this up and I'm just gonna 127 00:10:34,639 --> 00:10:39,489 Click Next and you can leave all of these options as the default 128 00:10:41,419 --> 00:10:48,188 our first C project in code blocks now over here inside this 129 00:10:48,558 --> 00:10:54,188 Draf is right here. So that was the name of the project that I 130 00:10:54,188 --> 00:10:58,658 So I'm going to click down and you'll see we have this file here 131 00:10:58,659 --> 00:11:02,798 And this is a file that got automatically created for us by code 132 00:11:02,798 --> 00:11:08,828 I'm just gonna right click and open it and you'll see over here. 133 00:11:08,828 --> 00:11:12,637 There's these little lines of code that say include down here. 134 00:11:13,909 --> 00:11:19,418 Program that we're given by default when we created our C project 135 00:11:19,519 --> 00:11:25,009 Right, essentially what this program does is it prints out hello 136 00:11:25,259 --> 00:11:29,568 So in order to run this program and test it just to make sure that 137 00:11:29,820 --> 00:11:32,360 I'm gonna come up here and you'll see that there's this 138 00:11:32,429 --> 00:11:36,829 Green play button over here and it just says run when I hover over 139 00:11:37,259 --> 00:11:42,019 This file that we have open right here main see is gonna get run 140 00:11:42,019 --> 00:11:46,399 So we're essentially telling code blocks to run this file and 141 00:11:46,429 --> 00:11:50,299 So when I click the Run button, we're getting this message. It 142 00:11:50,299 --> 00:11:52,299 do you want to build it like yes, and 143 00:11:52,620 --> 00:11:55,428 Now over here this little window pops up 144 00:11:55,740 --> 00:12:02,570 Basically, this is the command prompt. So whenever I run a C 145 00:12:02,570 --> 00:12:05,150 So if I was to exit out of this and run this program again 146 00:12:05,970 --> 00:12:12,769 You'll see this window pops up again. And it basically just 147 00:12:12,769 --> 00:12:14,769 All we're doing is we're printing out 148 00:12:15,240 --> 00:12:22,549 Hello world onto the screen and that's actually what this 149 00:12:23,129 --> 00:12:26,090 Executing the program and it's printing out whatever we told it to 150 00:12:26,090 --> 00:12:30,439 So it's printing out hello world over here and printing something 151 00:12:30,440 --> 00:12:33,679 And that's one instruction that we can give to the computer 152 00:12:33,750 --> 00:12:35,299 But as we go through this course 153 00:12:35,299 --> 00:12:39,978 we're going to learn all sorts of instructions that we can use but 154 00:12:40,049 --> 00:12:44,838 C project set up and to test our main dot C file 155 00:12:44,840 --> 00:12:50,389 So as long as everything works here and you're good to go and now 156 00:12:54,580 --> 00:12:59,229 In this tutorial I'm going to talk to you guys about the basics of 157 00:12:59,750 --> 00:13:04,089 So if you've been following along with the course up to this 158 00:13:04,089 --> 00:13:09,819 We've installed a C compiler and we set up our first C program. 159 00:13:10,519 --> 00:13:18,158 Inside of code blocks. So now essentially what we have is this 160 00:13:18,159 --> 00:13:22,208 I just kind of want to point out a couple things that are in here 161 00:13:22,208 --> 00:13:29,228 We have these little instructions which are just called include 162 00:13:29,450 --> 00:13:34,809 Later on in the course. We're gonna talk about what these are 163 00:13:34,809 --> 00:13:39,038 How they're helping our program, but for now just know that in 164 00:13:39,039 --> 00:13:41,348 We're gonna need to have these guys up there 165 00:13:41,750 --> 00:13:45,308 next thing we have is this little block of code called main and 166 00:13:45,440 --> 00:13:48,789 It just has this name main you can see there's an open and closed 167 00:13:49,159 --> 00:13:55,419 And this is actually what's called a method and don't worry too 168 00:13:55,419 --> 00:13:59,588 we're gonna talk more about those in future, but essentially a 169 00:14:00,230 --> 00:14:02,740 container where we can put some of our code and 170 00:14:03,139 --> 00:14:08,828 This method main is very important because this is the method 171 00:14:09,259 --> 00:14:13,778 when we run our program so in C we can actually write out a bunch 172 00:14:13,850 --> 00:14:18,219 instructions for the computer and then we can run our program so 173 00:14:18,769 --> 00:14:22,629 Execute those programs and whenever the computer executes our C 174 00:14:22,759 --> 00:14:25,119 It's gonna look inside of this main 175 00:14:25,519 --> 00:14:31,568 method in other words that's gonna look inside of all the code 176 00:14:31,730 --> 00:14:35,050 Execute that code so I have this instruction here 177 00:14:35,049 --> 00:14:40,538 It's called printf and basically this is just printing out some 178 00:14:40,539 --> 00:14:47,259 It's gonna come into this main method and it's gonna execute all 179 00:14:47,259 --> 00:14:51,369 So if I was to run my program then this is gonna work now 180 00:14:51,370 --> 00:14:56,948 Let's talk about actually running a program whenever we want to 181 00:14:56,948 --> 00:14:58,338 We actually have to do two things 182 00:14:58,339 --> 00:15:03,009 The first thing we have to do is what's called building a program 183 00:15:03,500 --> 00:15:07,149 And basically what that does is it takes all of this C code 184 00:15:07,730 --> 00:15:12,649 It translates it into a language that the computer is going to be 185 00:15:12,839 --> 00:15:16,069 So it's essentially taking this C program code 186 00:15:16,528 --> 00:15:20,808 Translating it into computer code and then the computer can 187 00:15:20,809 --> 00:15:23,750 So the first thing we have to do is always build our C file 188 00:15:23,909 --> 00:15:27,439 The second thing we have to do is run the file that gets built 189 00:15:27,809 --> 00:15:30,078 so once we've built this program 190 00:15:30,078 --> 00:15:35,387 we've compiled it down into code the computer can understand we 191 00:15:35,789 --> 00:15:37,789 execute that code and in code blocks 192 00:15:37,789 --> 00:15:39,789 It's actually really easy to do this. So 193 00:15:39,839 --> 00:15:44,479 we can come over here and this little gear icon will allow us to 194 00:15:44,549 --> 00:15:49,759 then this play button will allow us to run our program but a lot 195 00:15:50,100 --> 00:15:54,110 Programming you're gonna want to build and run your code at the 196 00:15:54,110 --> 00:15:56,110 you're just gonna want to build it and run it just to see what 197 00:15:58,528 --> 00:16:04,788 So we have this option over here called build and run and this 198 00:16:04,789 --> 00:16:08,208 So generally as you're going through this course you're gonna want 199 00:16:08,208 --> 00:16:10,368 That's build and run options when I click this 200 00:16:10,589 --> 00:16:18,018 it'll essentially build my program run it and then it'll show up 201 00:16:18,539 --> 00:16:22,399 Console window you can call it. So a lot of people would refer to 202 00:16:24,958 --> 00:16:30,798 Information that we tell it to so if we write a C program that 203 00:16:30,929 --> 00:16:37,129 Then we wouldn't actually see this but since we're using this 204 00:16:37,169 --> 00:16:42,198 So let's talk about how these programs actually get executed 205 00:16:42,240 --> 00:16:44,568 We have a set of instructions 206 00:16:44,610 --> 00:16:52,099 So when I'm writing a C program, I'm basically writing out a set 207 00:16:52,100 --> 00:16:57,740 Right a recipe is essentially just a set of instructions that when 208 00:16:57,899 --> 00:17:04,249 You know cooking or baking something delicious, right? And that's 209 00:17:04,250 --> 00:17:06,798 It's a set of instructions that when executed correctly 210 00:17:07,109 --> 00:17:11,659 will result in the computer doing something for us and one of the 211 00:17:11,759 --> 00:17:14,539 computers is that we can program them to do anything and 212 00:17:14,699 --> 00:17:19,579 C is one of the best languages to do that with so let's look at 213 00:17:19,578 --> 00:17:26,088 It's called print F and this prints something out onto the screen. 214 00:17:26,640 --> 00:17:29,569 Semicolon and this semicolon is actually really important 215 00:17:29,569 --> 00:17:32,149 So whenever we write an instruction in C 216 00:17:32,279 --> 00:17:34,519 We always want to end it off with a semicolon 217 00:17:34,789 --> 00:17:40,068 That tells C that we're done with that instruction and we want to 218 00:17:40,259 --> 00:17:43,608 instruction and I could paste it down here and 219 00:17:44,190 --> 00:17:47,690 Now we're basically going to be executing this instruction twice 220 00:17:47,690 --> 00:17:52,669 so if I was to run and build my program you'll see that we're not 221 00:17:54,319 --> 00:18:01,819 So when we run our program the program comes down into this main 222 00:18:01,819 --> 00:18:05,240 So it starts with this first instruction. It executes it, right? 223 00:18:05,240 --> 00:18:09,558 So it prints out hello world onto the screen and then it also 224 00:18:09,558 --> 00:18:14,538 So this backslash n is like a newline character and that'll move 225 00:18:14,849 --> 00:18:19,668 When it's done with this instruction, it moves on to the next 226 00:18:20,160 --> 00:18:23,209 So as we go forward and we learn how to write in C 227 00:18:23,210 --> 00:18:25,789 We're gonna learn more and more complex instructions 228 00:18:25,798 --> 00:18:32,689 And we'll be able to use all those instructions in conjunction 229 00:18:33,380 --> 00:18:38,059 This is really the only instruction that we know which is printf 230 00:18:38,490 --> 00:18:42,529 Instruction in order to draw out a little shape onto the screen 231 00:18:42,599 --> 00:18:46,399 So I'm going to show you guys how we can draw out like a little 232 00:18:46,829 --> 00:18:53,240 I'm gonna take this printf and I'm actually just gonna get rid of 233 00:18:53,400 --> 00:18:57,739 so I'm just gonna copy it and we'll paste it down here, maybe four 234 00:18:58,619 --> 00:19:00,649 What I can do is I can specify 235 00:19:01,319 --> 00:19:04,849 Instructions to the computer that will tell it to draw out a 236 00:19:04,950 --> 00:19:07,640 So down here I could make like a forward slash and over here 237 00:19:07,640 --> 00:19:13,040 We'll make another one and I'll make another one and I'll make 238 00:19:13,039 --> 00:19:19,249 I've drawn like a little diagonal line with these forward slashes. 239 00:19:21,029 --> 00:19:24,499 I'm gonna put one here. I'll put one here and 240 00:19:25,259 --> 00:19:32,838 finally, why don't we draw some underscores and then we'll put one 241 00:19:32,909 --> 00:19:39,179 Backslash ends are just printing out a new line. So anytime I 242 00:19:39,179 --> 00:19:43,288 So it'll print this guy out now on the line down the line below 243 00:19:43,288 --> 00:19:48,598 So if I was to save my program and execute it now the computer is 244 00:19:48,599 --> 00:19:53,548 Of these instructions in order and we'll be able to print out the 245 00:19:53,650 --> 00:19:56,400 You see over here. We get our shape and 246 00:19:57,190 --> 00:19:59,759 One thing I really want to stress. Is that order matters? 247 00:19:59,759 --> 00:20:06,328 So if I took this instruction and I placed it up here as the first 248 00:20:06,329 --> 00:20:08,308 This is gonna get printed out first 249 00:20:08,308 --> 00:20:11,667 So when I run my program you'll see that we're printing out this 250 00:20:11,829 --> 00:20:15,869 Funky looking shape and that's because we changed up the order of 251 00:20:15,869 --> 00:20:22,318 So the computer changed the order in which it executed those 252 00:20:22,839 --> 00:20:27,058 Semicolon after each line of code. So that's kind of the basics of 253 00:20:27,058 --> 00:20:29,489 That's essentially all you need to know when we're writing a 254 00:20:29,619 --> 00:20:34,048 we're specifying a list of instructions that we want the computer 255 00:20:34,450 --> 00:20:41,009 Depending on which instructions we give the computer and which 256 00:20:41,380 --> 00:20:47,309 That'll determine what the program does and as we go through this 257 00:20:47,319 --> 00:20:51,149 we're also going to learn how we can take simple instructions like 258 00:20:51,220 --> 00:20:55,140 Do different things with them to make our programs a lot more 259 00:20:59,109 --> 00:21:03,250 In this tutorial I want to talk to you guys about variables in C 260 00:21:03,470 --> 00:21:08,588 Now when we're using C programming language a lot of times we're 261 00:21:08,720 --> 00:21:15,970 so there's gonna be different data values or different information 262 00:21:16,429 --> 00:21:24,339 Sometimes it can be difficult to maintain and keep track of all 263 00:21:24,589 --> 00:21:30,188 Essentially a container where we can store different pieces of 264 00:21:30,289 --> 00:21:33,818 we could sort of things like numbers or texts or characters and 265 00:21:34,099 --> 00:21:39,849 These variables make it a lot easier for us to keep track of and 266 00:21:39,849 --> 00:21:45,698 So I'm gonna show you guys basically how variables can be useful 267 00:21:45,798 --> 00:21:49,028 But down here. I have a very basic program. I'm basically just 268 00:21:49,548 --> 00:21:53,439 Printing out a little story. It says there once was a man named 269 00:21:53,960 --> 00:22:01,449 He was 70 years old. He really liked the name George but did not 270 00:22:01,450 --> 00:22:04,058 We're just printing out a bunch of different lines of text 271 00:22:04,058 --> 00:22:05,539 and then when I run this program 272 00:22:05,539 --> 00:22:11,528 You'll see we print out the actual story so over here we have our 273 00:22:11,630 --> 00:22:16,989 But let's say that I'm reading through my story. I'm reading 274 00:22:16,990 --> 00:22:19,179 I think I want to change the character's name 275 00:22:19,789 --> 00:22:25,148 So instead of calling the character George, why don't we call him 276 00:22:25,150 --> 00:22:29,949 I'm gonna have to look through my entire program and find every 277 00:22:29,950 --> 00:22:36,130 So right here is one change it to John remember keep looking and 278 00:22:36,130 --> 00:22:40,660 So I had to manually go through and change every instance of the 279 00:22:41,179 --> 00:22:44,798 To John and let's say that okay. That sounds pretty good 280 00:22:44,798 --> 00:22:49,749 But maybe I'm thinking I want to make the character a little bit 281 00:22:52,240 --> 00:22:59,740 I'm gonna have to look through my entire program find every place 282 00:22:59,740 --> 00:23:02,380 So there and there there you go 283 00:23:02,380 --> 00:23:04,989 I changed the program I updated and modified it now 284 00:23:04,990 --> 00:23:11,959 If I was to run this program then all that information will be 285 00:23:12,000 --> 00:23:15,380 I'm dealing with a very short story. I mean, this is four lines 286 00:23:15,380 --> 00:23:17,510 We only mentioned the character's age and name twice 287 00:23:17,669 --> 00:23:23,839 But imagine if I was writing out a huge C program that had 288 00:23:23,909 --> 00:23:30,109 So story had hundreds of lines and maybe we mentioned the 289 00:23:30,109 --> 00:23:32,509 Well if I wanted to change the characters age your name? 290 00:23:32,509 --> 00:23:39,288 I would have to go through and manually change it in every single 291 00:23:39,569 --> 00:23:45,019 Basically, that's not a good way for us to manage and maintain the 292 00:23:45,058 --> 00:23:48,318 All right. We basically have two pieces of data that we're working 293 00:23:49,019 --> 00:23:51,648 Consistently in this program the characters age and the 294 00:23:52,380 --> 00:23:59,778 What I want to show you guys now is how we can use variables to 295 00:24:00,538 --> 00:24:04,578 So remember a variable is basically just a container where we can 296 00:24:04,919 --> 00:24:09,288 so I want to create two variables which will store the character's 297 00:24:09,480 --> 00:24:14,209 The character's age and I'll show you guys how this can make our 298 00:24:14,519 --> 00:24:17,418 Manage up here above these print statements 299 00:24:17,419 --> 00:24:21,769 I'm just going to make a new line and I'm gonna come over here and 300 00:24:22,048 --> 00:24:27,709 Now whenever we create a variable in C, we have to give C a couple 301 00:24:28,019 --> 00:24:30,439 So we have to basically tell C a couple different things 302 00:24:30,599 --> 00:24:36,859 The first thing we have to tell C is what type of information we 303 00:24:36,859 --> 00:24:42,078 I'm gonna get into the different data types that we can represent 304 00:24:42,079 --> 00:24:47,178 but for now just know that there's a bunch of different types of 305 00:24:47,190 --> 00:24:53,960 Numbers characters we can store text we can store decimal numbers 306 00:24:53,960 --> 00:25:00,470 I'm going to store the character's name and the character's name 307 00:25:00,470 --> 00:25:02,600 So in order to create this variable 308 00:25:02,599 --> 00:25:07,129 The first thing I have to do is tell C that I want to create a 309 00:25:07,259 --> 00:25:12,318 So to do that, I'm just gonna type out char just like that and 310 00:25:12,319 --> 00:25:15,678 Hey, we're gonna store characters inside of this variable 311 00:25:17,659 --> 00:25:20,569 I want to type out the name of the variable that I want to create 312 00:25:20,940 --> 00:25:25,440 Remember of variables that contain and it's a good idea to give 313 00:25:25,779 --> 00:25:32,940 Descriptive names and those names will basically let us know what 314 00:25:33,849 --> 00:25:39,119 Character name and now what I want to do is I basically want to 315 00:25:41,190 --> 00:25:43,349 I'm telling to see that I want to store a character 316 00:25:43,569 --> 00:25:47,788 But in C we can also store instead of just storing like one single 317 00:25:47,789 --> 00:25:51,028 we can store a bunch of characters and that would be kind of like 318 00:25:51,069 --> 00:25:58,618 you know a name so there's like four characters in here in order 319 00:25:58,690 --> 00:26:00,719 Type the character Abel's name 320 00:26:00,940 --> 00:26:03,570 We're gonna have to make an open and closed square bracket 321 00:26:03,700 --> 00:26:08,580 And that's gonna tell C that we want to store a bunch of 322 00:26:08,799 --> 00:26:14,489 So I'm gonna type char a character name open a close square 323 00:26:14,490 --> 00:26:21,630 So I'm essentially assigning a value to this variable and I'm just 324 00:26:21,630 --> 00:26:23,729 We're gonna type out the value that we want to store 325 00:26:23,730 --> 00:26:29,160 So in my case, I'm just gonna store the character's name, which is 326 00:26:29,160 --> 00:26:31,410 We're gonna have to end this off with a semicolon 327 00:26:32,819 --> 00:26:34,569 so essentially what I did here was I 328 00:26:34,569 --> 00:26:38,759 Created a variable and I told C what I wanted to store in the 329 00:26:38,759 --> 00:26:43,499 I wanted to store a character. I gave this variable a name. It was 330 00:26:44,079 --> 00:26:50,579 I used these open and close square brackets to sell C that I 331 00:26:51,160 --> 00:26:54,719 Now what we can do is we can make another variable to store the 332 00:26:55,119 --> 00:26:59,429 So in this case, we're gonna want to do something similar, but 333 00:26:59,769 --> 00:27:04,829 There's a bunch of different ways that we can store numbers and 334 00:27:04,829 --> 00:27:11,398 so we're gonna use something called an integer and an integer is 335 00:27:11,650 --> 00:27:15,570 That basically stands for integer. And now we're gonna give this a 336 00:27:16,329 --> 00:27:20,218 Character age and I'm gonna set this equal to a number 337 00:27:20,500 --> 00:27:23,219 So in our story the character is 35 338 00:27:23,220 --> 00:27:27,329 So I'm just gonna type out 35 and now we can again end this off 339 00:27:27,329 --> 00:27:33,118 So I have two variables here. This one's storing a collection of 340 00:27:33,519 --> 00:27:37,679 Now the question becomes how can we use the inside of our 341 00:27:38,640 --> 00:27:45,239 What we can do is we can print them out alongside of this text and 342 00:27:45,669 --> 00:27:50,278 Instruction in order to do that so I'm gonna show you guys how to 343 00:27:50,278 --> 00:27:53,489 So basically, I'm gonna go over here where we have the character's 344 00:27:53,490 --> 00:28:01,469 I'm gonna erase this and I'm gonna replace it with a % and an S 345 00:28:02,019 --> 00:28:05,519 I'm just gonna type in the variable name. So I'm gonna type in 346 00:28:06,278 --> 00:28:11,397 character name and basically what's happening is when we use this 347 00:28:11,619 --> 00:28:15,778 We're telling see that inside of this string of text here 348 00:28:15,940 --> 00:28:18,600 We're gonna want to insert a another 349 00:28:19,089 --> 00:28:25,198 String and a string is basically just a collection of characters 350 00:28:25,509 --> 00:28:30,179 So when I say % s this is basically a placeholder and I'm telling 351 00:28:30,179 --> 00:28:33,028 I'm gonna insert a string into here and 352 00:28:33,429 --> 00:28:38,999 Over here I use this comma and then I type out the string that I 353 00:28:39,159 --> 00:28:42,569 It's character name and so basically over here 354 00:28:42,569 --> 00:28:45,119 It's gonna say there once was a man named 355 00:28:45,519 --> 00:28:50,848 % s and it's gonna replace % s with whatever we specify over here 356 00:28:50,849 --> 00:28:55,889 So it's gonna replace % s with the value that's stored inside that 357 00:28:55,890 --> 00:28:58,259 so I'm gonna save this and we'll run our program and 358 00:28:58,750 --> 00:29:03,118 Now you'll see that it's still printing out there once was a man 359 00:29:03,429 --> 00:29:07,229 Even though we didn't actually type out John over here 360 00:29:07,230 --> 00:29:13,110 We just typed % ass in the character name so I can do this in this 361 00:29:13,109 --> 00:29:17,968 We're also using the character's name so I can type % s and over 362 00:29:19,359 --> 00:29:22,588 Character name and again. This is going to act as a placeholder 363 00:29:23,109 --> 00:29:26,848 For this string of characters that we specify over here 364 00:29:27,009 --> 00:29:31,919 Let's do the same thing with the age so over here we have the age 365 00:29:31,919 --> 00:29:34,379 I'm going to do something similar and I'm gonna say percent 366 00:29:34,630 --> 00:29:39,749 but now because I have a number instead of using percent s I'm 367 00:29:39,880 --> 00:29:45,030 Percent D and that basically means that we're gonna insert an 368 00:29:45,029 --> 00:29:48,808 So I'm saying he was percent D years old and over here. I'm gonna 369 00:29:49,048 --> 00:29:54,449 , and I'm just gonna type out the name of the variable that I want 370 00:29:56,798 --> 00:30:01,858 Character age and we're going to do this one more time down here. 371 00:30:04,028 --> 00:30:07,919 Character age so essentially what I'm doing is I'm printing out 372 00:30:08,470 --> 00:30:11,610 That's inside of character age where I specify this 373 00:30:13,329 --> 00:30:19,019 Let's go ahead and run our program and now you'll see it's gonna 374 00:30:19,298 --> 00:30:21,719 the same name and the same age 375 00:30:22,239 --> 00:30:27,179 Except I didn't have to actually type out the character's age and 376 00:30:27,179 --> 00:30:30,689 I just referred to the variable and what's cool about? 377 00:30:30,909 --> 00:30:34,679 Variables is I could come up here and let's say I wanted to modify 378 00:30:35,108 --> 00:30:40,828 all I would have to do is modify it in this one spot so we could 379 00:30:40,829 --> 00:30:43,678 We could also change their age to like 67 380 00:30:43,679 --> 00:30:48,179 And now when I run my program all of that's gonna get updated in 381 00:30:48,179 --> 00:30:51,209 so now it's using the name Tom and the age of 382 00:30:51,759 --> 00:30:56,248 67 in our case. We only mention the character's name and the 383 00:30:56,378 --> 00:31:00,867 But you can imagine if I was mentioning this variable like ten or 384 00:31:01,388 --> 00:31:05,877 Having to change it in each one of those individual spots would be 385 00:31:05,878 --> 00:31:11,009 So variables are great because we can store the value one time and 386 00:31:11,009 --> 00:31:17,308 We just modify it in one place. So another cool thing that we can 387 00:31:17,308 --> 00:31:22,768 So let's say that halfway through the story. I wanted to change 388 00:31:22,769 --> 00:31:28,617 I can make a new line and right above these print statements. I 389 00:31:29,470 --> 00:31:34,108 character age and I can just set it equal to something else so we 390 00:31:34,298 --> 00:31:39,867 Again, we want to include that semicolon. So now halfway through 391 00:31:40,329 --> 00:31:44,579 So you'll see it says there once was a man named Tom, he was 67 392 00:31:44,579 --> 00:31:51,178 The age is being a 30 so we actually modified the value that was 393 00:31:51,608 --> 00:31:55,978 Halfway through printing out our story and that's really why 394 00:31:55,980 --> 00:32:01,618 So that's kind of a basic overview and introduction into what 395 00:32:02,230 --> 00:32:08,380 And over here again, we're storing a collection of characters, 396 00:32:08,599 --> 00:32:11,619 But there's some other data types that we can also represent 397 00:32:11,660 --> 00:32:16,540 So in addition to representing like a string or a number we can do 398 00:32:16,539 --> 00:32:18,729 And I'm going to talk about that in the next video 399 00:32:18,730 --> 00:32:23,650 But for now, this has just been a basic overview and introduction 400 00:32:27,289 --> 00:32:33,170 In this tutorial I want to talk to you guys about data types in C 401 00:32:33,450 --> 00:32:35,180 We're gonna be dealing with a lot of data 402 00:32:35,180 --> 00:32:40,099 So generally when you're writing programs, you're gonna be dealing 403 00:32:40,099 --> 00:32:45,409 I just want to talk to you guys about what types of information we 404 00:32:45,990 --> 00:32:49,279 Inside of our C program and these are called data types. All 405 00:32:49,279 --> 00:32:53,628 So what are the different types of data that we can work with in 406 00:32:53,630 --> 00:32:56,810 So I'm gonna show you guys all those different data types right 407 00:32:56,849 --> 00:33:01,039 So a lot of times in C when we're working with data, we're gonna 408 00:33:02,940 --> 00:33:08,570 so I'm just gonna show you guys a bunch of different variables 409 00:33:08,569 --> 00:33:10,569 Can store inside of those variables? 410 00:33:10,769 --> 00:33:15,410 so remember whenever we create a variable and C we have to tell C 411 00:33:15,509 --> 00:33:19,640 information and the first piece of information we need to tell C 412 00:33:20,160 --> 00:33:24,920 Data that we want to store inside of that container inside of that 413 00:33:24,920 --> 00:33:26,930 so let's talk about the different types of 414 00:33:27,569 --> 00:33:32,269 One of the most basic types of data and probably one of the types 415 00:33:32,430 --> 00:33:38,599 Gonna be numbers. There's actually two very important types of 416 00:33:38,970 --> 00:33:43,279 So the first type is called an integer and an integer is basically 417 00:33:43,279 --> 00:33:46,848 so it's a counting number right if I was counting on my fingers 1 418 00:33:47,119 --> 00:33:51,348 I'm just counting out whole numbers and I'm not saying like 1 1 419 00:33:52,019 --> 00:33:55,219 2.3 like I'm counting in whole numbers and that's what an integer 420 00:33:55,220 --> 00:33:57,620 So to create an integer we could just say int 421 00:33:57,750 --> 00:34:03,858 And we'll give this a name so we could say like age and we could 422 00:34:04,349 --> 00:34:09,679 Now whenever we want to use an integer in C. We always just want 423 00:34:09,679 --> 00:34:13,278 So you'll notice I'm not putting quotation marks or parentheses or 424 00:34:13,280 --> 00:34:19,429 I'm just typing out the number like that. And now we actually have 425 00:34:20,039 --> 00:34:22,550 another type of number we can create is a 426 00:34:23,159 --> 00:34:26,689 Decimal number so this would be a number unlike an integer 427 00:34:26,690 --> 00:34:31,460 So it would be like 2.5 or eight point six seven or seven point 428 00:34:31,460 --> 00:34:36,409 It's a number that has a decimal point associated to it. So 429 00:34:38,809 --> 00:34:41,798 there's something called a double and there's something called a 430 00:34:41,898 --> 00:34:45,759 Float, and I'm not gonna get too much into the difference between 431 00:34:45,769 --> 00:34:48,458 But as a beginner to see for the most part 432 00:34:48,460 --> 00:34:49,659 You're just gonna want to use a double 433 00:34:49,659 --> 00:34:55,689 So I'm just gonna type out double and this could be like someone's 434 00:34:56,929 --> 00:35:02,918 3.7 or whatever so, you know, you can use the double to represent 435 00:35:02,960 --> 00:35:07,510 it's also important to note that I could also represent like the 436 00:35:07,940 --> 00:35:12,128 3.0 and that's gonna be a decimal or I could say 40 point O 437 00:35:12,349 --> 00:35:17,409 So the difference between 40 and 40 point O is that forty point O 438 00:35:17,409 --> 00:35:21,068 It's a decimal number and 40 without the decimal point is an 439 00:35:21,068 --> 00:35:25,298 So that's something just to keep in mind so you can make this like 440 00:35:25,519 --> 00:35:30,759 So those are the two basic types of numbers integers and doubles 441 00:35:30,949 --> 00:35:35,230 Now the next type of data that we're gonna want to store or is 442 00:35:37,550 --> 00:35:40,419 single characters inside of a variable so I could just say 443 00:35:40,550 --> 00:35:44,289 CH AR and that stands for character and we could give this a 444 00:35:44,289 --> 00:35:48,219 So maybe it'd be like someone's grade and when we create a 445 00:35:48,219 --> 00:35:53,678 We're basically going to specify the character that we want to 446 00:35:53,679 --> 00:35:59,558 So inside of these single quotation marks, I could put a letter I 447 00:36:00,139 --> 00:36:05,949 C d AF basically you can put any character that you want to store 448 00:36:06,048 --> 00:36:13,268 So if I put an A here now, this character variable grade is gonna 449 00:36:13,338 --> 00:36:19,088 It's important to note that you can only store one character so I 450 00:36:19,088 --> 00:36:20,480 And that's actually gonna throw an error 451 00:36:20,480 --> 00:36:28,208 All we can do is store one single character, but these three are 452 00:36:28,210 --> 00:36:35,019 So these are the basic types of information that we can deal with 453 00:36:35,298 --> 00:36:38,409 Three data types, we can represent all sorts of information 454 00:36:38,510 --> 00:36:43,329 I also want to talk to you guys about one sort of extra data type 455 00:36:43,789 --> 00:36:49,989 Technically a data type in C. It's technically like a modification 456 00:36:50,539 --> 00:36:56,109 So you'll notice when we create a character we can only store one 457 00:36:56,108 --> 00:36:59,257 But a lot of times we're gonna want to represent more than one 458 00:36:59,900 --> 00:37:05,920 Character so we're gonna want to represent like a string of 459 00:37:05,920 --> 00:37:13,028 so it's a collection of characters that are grouped together and 460 00:37:13,278 --> 00:37:15,757 String so I can basically say char 461 00:37:15,759 --> 00:37:21,219 I can type out the name of the string that I want to create so I 462 00:37:21,219 --> 00:37:27,278 can make an open and closed square brackets and this is basically 463 00:37:27,469 --> 00:37:29,648 string a bunch of characters together and 464 00:37:29,960 --> 00:37:35,798 What we're actually doing is creating something called an array 465 00:37:35,798 --> 00:37:40,418 But just for now know that we can create a string of characters by 466 00:37:42,230 --> 00:37:49,449 Specifying the name these open and close square brackets, and then 467 00:37:49,449 --> 00:37:51,429 So instead of these single quotes up here 468 00:37:51,429 --> 00:37:55,929 We're just gonna use double quotes and I could type in like a 469 00:37:56,088 --> 00:37:59,978 String of text I want here and this will get stored inside of 470 00:38:00,349 --> 00:38:03,849 Variable which like I said is technically called an array now 471 00:38:04,759 --> 00:38:11,139 Doing something like this or creating this like string of 472 00:38:11,139 --> 00:38:13,929 So like these variables up here we could modify them 473 00:38:13,929 --> 00:38:17,469 We can give them new values but you can't do stuff like that with 474 00:38:17,469 --> 00:38:23,378 So just know that this is like a special type of data and it's 475 00:38:23,539 --> 00:38:29,199 But that's one of the basics of working with data and those are 476 00:38:34,989 --> 00:38:38,948 In this tutorial I want to talk to you guys some more about the 477 00:38:38,949 --> 00:38:40,949 so if you've been following along with this tutorial 478 00:38:41,389 --> 00:38:44,650 We've been using an instruction and our C programs called 479 00:38:44,929 --> 00:38:51,519 Printf and printf basically allows us to print different things 480 00:38:51,519 --> 00:38:56,769 We're going to talk more about what printf does and we're gonna 481 00:38:56,769 --> 00:39:00,938 And also things like different data like numbers and strings and 482 00:39:01,369 --> 00:39:08,079 So in order to use printf we just type out PR int F and we're 483 00:39:11,059 --> 00:39:18,159 Now printf is what's called a function and basically what that 484 00:39:18,320 --> 00:39:23,619 in our case printf just print something out onto the screen write 485 00:39:23,838 --> 00:39:27,398 Printing something out and it basically works how you'd expect it 486 00:39:30,079 --> 00:39:33,068 specific piece of information and generally when we're using 487 00:39:34,429 --> 00:39:39,789 Text so I can make an open and closed quotation marks and I could 488 00:39:39,858 --> 00:39:43,239 And now this will print out onto the screen. Hello and 489 00:39:43,940 --> 00:39:46,990 Inside of printf I can use all sorts of special characters 490 00:39:46,989 --> 00:39:50,769 so if I wanted to create a new line, for example, I can make a 491 00:39:51,289 --> 00:39:55,358 Down here. I could type world and now this will print out a new 492 00:39:56,000 --> 00:39:58,329 So you'll see hello world on different lines 493 00:39:58,909 --> 00:40:01,628 We can also use these special characters 494 00:40:01,630 --> 00:40:04,480 So for example, I could print out like a quotation mark 495 00:40:04,480 --> 00:40:08,918 So if I just printed a normal quotation mark right here, you'll 496 00:40:10,460 --> 00:40:16,838 counting this as the entire string if I wanted to change that I 497 00:40:18,440 --> 00:40:25,088 Quotation marks when I run my program you can see we have a 498 00:40:25,300 --> 00:40:32,050 you can specify some text that you want to print out onto the 499 00:40:32,300 --> 00:40:36,249 Extremely powerful and we can use it to print out different pieces 500 00:40:36,858 --> 00:40:41,048 So in addition to just printing out plain text inside of these 501 00:40:41,170 --> 00:40:44,920 I could also print out other types of information. For example, I 502 00:40:45,809 --> 00:40:52,918 And if I wanted to print out a number I would have to use 503 00:40:53,108 --> 00:40:57,447 Basically tells this printf function that we want to print out a 504 00:40:57,579 --> 00:41:00,178 That's not just plain text like this 505 00:41:00,668 --> 00:41:04,018 So if I said % and then I said D 506 00:41:04,389 --> 00:41:07,079 This would actually allow me to print out a number 507 00:41:07,079 --> 00:41:14,398 So generally when we use these format specifiers uses % and that 508 00:41:14,528 --> 00:41:20,938 Special and then after that we would type in a specific letter and 509 00:41:20,938 --> 00:41:25,108 so if I said % D this means that we want to print out an integer, 510 00:41:25,748 --> 00:41:29,637 All I have to do now is I can make a comma and over here 511 00:41:29,639 --> 00:41:36,509 I can specify the integer that I want to print out so I could say 512 00:41:36,880 --> 00:41:38,759 500 to the screen so you can see over here 513 00:41:38,759 --> 00:41:46,677 we're getting 500 and this is cool because we can interweave these 514 00:41:50,318 --> 00:41:54,507 % D and this acts as a placeholder for this number over here 515 00:41:54,938 --> 00:41:58,108 so when I run my program now it says my favorite number is 516 00:41:58,929 --> 00:42:04,168 500 and that's really cool. We can also use multiple format 517 00:42:04,168 --> 00:42:11,818 so in addition to just using this percent d I can also specify 518 00:42:12,278 --> 00:42:20,039 You'll notice that this is structured similarly to percent d % s 519 00:42:20,039 --> 00:42:22,498 So I can include some more text just like this 520 00:42:22,630 --> 00:42:26,639 so if I said % s I can make another comma here and I could say 521 00:42:27,849 --> 00:42:35,488 Number and now this is gonna print out my favorite percent s so 522 00:42:35,489 --> 00:42:39,628 It's gonna insert that in and it's gonna say 500. So now when we 523 00:42:40,148 --> 00:42:42,148 it'll say my favorite number is 524 00:42:42,458 --> 00:42:49,798 500 and you'll notice here that I'm using multiple commas. So I'm 525 00:42:49,798 --> 00:42:55,737 I have a comma and I'm specifying this string that I want to print 526 00:42:56,168 --> 00:43:02,068 integer, and so basically the order that you put these format 527 00:43:02,318 --> 00:43:06,389 String of text is the order that you need to include them using 528 00:43:06,389 --> 00:43:11,608 So you're going to separate each of them using a comma hoon 529 00:43:11,608 --> 00:43:13,268 We can also use a bunch of other ones 530 00:43:13,268 --> 00:43:18,868 And I'm going to talk to you guys about some of the most common % 531 00:43:18,869 --> 00:43:20,460 but if we wanted to print out a decimal number 532 00:43:20,460 --> 00:43:25,079 for example a double we could say percent F and this is gonna 533 00:43:25,539 --> 00:43:29,309 Decimal number so I could say like 500 point nine eight seven five 534 00:43:29,309 --> 00:43:34,679 And now this is gonna print this out on to the screen. Actually. I 535 00:43:35,768 --> 00:43:39,598 So my favorite number is five hundred point nine eight seven five 536 00:43:39,900 --> 00:43:47,818 So essentially you can use these print FS and you can use these 537 00:43:49,798 --> 00:43:54,418 So like this is just some general text output and we can use these 538 00:43:54,548 --> 00:44:01,139 Like numbers or you know other strings and stuff like that and 539 00:44:01,139 --> 00:44:07,768 So if I was to create like a number variable up here, I can call 540 00:44:07,768 --> 00:44:15,178 I can use this variable and print it out inside of this printf so 541 00:44:15,579 --> 00:44:20,369 Instead of printing out this number we can just access that 542 00:44:20,369 --> 00:44:26,008 And this will allow me to print out that variable using that 543 00:44:26,528 --> 00:44:29,128 90 so that's the basics of using printf 544 00:44:29,130 --> 00:44:34,619 This is a very awesome tool that we can use and printf is 545 00:44:35,108 --> 00:44:39,208 So when we're running our programs a lot of times we're gonna want 546 00:44:39,400 --> 00:44:43,769 About what's going on about what they're doing in printf can allow 547 00:44:43,768 --> 00:44:49,528 So don't be afraid to use these different access modifiers. Like I 548 00:44:49,528 --> 00:44:55,588 Those are all sort of the common ones and you can also use % C and 549 00:44:55,778 --> 00:44:59,489 Character so I can come over here create like a character 550 00:45:00,429 --> 00:45:04,108 We'll call it my char and just set it equal to like lower case 551 00:45:04,108 --> 00:45:11,788 I and I could print this out using % C. So now we could say my 552 00:45:14,570 --> 00:45:20,410 Play around with printf. It's extremely useful and it's really 553 00:45:26,019 --> 00:45:32,619 I want to talk to you guys about working with numbers and see now 554 00:45:32,619 --> 00:45:38,709 We're gonna want to do different things with numbers whether 555 00:45:38,869 --> 00:45:43,358 Subtracting doing math with numbers or you know, really just 556 00:45:43,360 --> 00:45:47,889 So I'm just gonna give you guys a basic overview of a lot of the 557 00:45:47,889 --> 00:45:54,460 We're also gonna talk about how different number types in Iraq. So 558 00:45:55,010 --> 00:46:01,600 So down here I am printing out a number so you can see I'm using 559 00:46:03,710 --> 00:46:09,789 And % f stands for like a floating-point number so that could be 560 00:46:10,190 --> 00:46:15,190 so we put % F in here that's gonna allow us to print out a decimal 561 00:46:15,289 --> 00:46:21,458 You can see I can run my program and it'll print out 8.9 and 562 00:46:21,460 --> 00:46:23,918 But it's also printing it out to a very precise 563 00:46:24,050 --> 00:46:28,419 decimal point and that's what C is always gonna do so it's always 564 00:46:28,420 --> 00:46:33,159 A very precise decimal. So using these decimals I can do all sorts 565 00:46:33,199 --> 00:46:38,918 So obviously I can just print out the decimal but I could also do 566 00:46:42,139 --> 00:46:45,339 This is gonna go ahead and be able to do that for us when I run my 567 00:46:45,800 --> 00:46:52,659 Not only is it gonna like print this out but it's gonna do the 568 00:46:53,030 --> 00:46:58,359 so in addition to addition we can also use subtraction division, 569 00:46:59,239 --> 00:47:04,478 Multiplication which is going to be in asterisks and you can use 570 00:47:04,579 --> 00:47:09,429 Operations addition subtraction multiplication and division. So 571 00:47:10,039 --> 00:47:12,608 floating-point numbers so like doubles and floats 572 00:47:13,130 --> 00:47:16,690 Alongside integers. So let's say I was going to add an integer 573 00:47:17,300 --> 00:47:20,830 With a double. So if I said 5 plus 574 00:47:21,440 --> 00:47:26,918 4.5. Well, actually when we do this this entire answer is going to 575 00:47:27,710 --> 00:47:34,300 Decimal number so when I say 5 which is an integer plus 4.5. We're 576 00:47:37,150 --> 00:47:43,750 and basically any operation that you do between an integer and a 577 00:47:45,798 --> 00:47:50,139 So that's important to realize but if I was to change this to four 578 00:47:50,139 --> 00:47:55,269 This isn't gonna work anymore because this is asking for a 579 00:47:55,429 --> 00:47:57,489 So when I do math with two integers 580 00:47:57,489 --> 00:48:02,318 it's going to give us an integer back and if I do math with a 581 00:48:02,318 --> 00:48:08,047 Floating-point number back. It's also important to realize for 582 00:48:08,420 --> 00:48:13,568 This should actually be a decimal number right and you'd expect us 583 00:48:13,568 --> 00:48:20,108 But if I just print it out percent D. So I printed out the result 584 00:48:20,119 --> 00:48:22,028 I'm actually gonna get an integer back 585 00:48:22,028 --> 00:48:28,989 So when I run the program you'll see we're getting a one here, but 586 00:48:28,989 --> 00:48:33,578 But it's one with a bunch of decimal points after it. Right? It's 587 00:48:34,099 --> 00:48:36,729 But when we just do math with two integers 588 00:48:36,730 --> 00:48:43,389 we're gonna get an integer back if I was to say five divided by 589 00:48:43,579 --> 00:48:47,919 Decimal now, we're gonna get the entire answer back. So we're 590 00:48:48,528 --> 00:48:52,778 So that's just a little bit about dealing with integers and 591 00:48:52,778 --> 00:48:55,688 We could also take these and put these into variables 592 00:48:55,690 --> 00:49:00,159 so, you know in addition to just having numbers down here I could 593 00:49:00,260 --> 00:49:07,239 You know num set it equal to six and we can go ahead and print num 594 00:49:09,289 --> 00:49:13,269 So now I will be able to print out that variable with no problem 595 00:49:13,269 --> 00:49:17,829 So it can be really useful sometimes to store these numbers inside 596 00:49:17,829 --> 00:49:20,229 I also want to talk to you guys about using more complex 597 00:49:21,139 --> 00:49:23,440 mathematical functions now in C 598 00:49:24,858 --> 00:49:30,909 Functions which are basically just little blocks of code that we 599 00:49:31,039 --> 00:49:38,079 Certain things for us. So they're like modify a value or give us 600 00:49:38,269 --> 00:49:41,708 I'm gonna show you guys a couple functions that we can use with 601 00:49:42,619 --> 00:49:47,827 So I'm just going to come over here and I can actually start using 602 00:49:47,829 --> 00:49:50,089 Out the name the function that you want to use 603 00:49:50,190 --> 00:49:55,970 So by default C is going to give us access to a bunch of math 604 00:49:55,969 --> 00:49:57,348 So we really don't have to do anything 605 00:49:57,349 --> 00:50:00,739 all we have to do is just type in their names and I'm gonna show 606 00:50:03,139 --> 00:50:07,699 Oh w then I'm gonna type an open and closed parenthesis and inside 607 00:50:07,769 --> 00:50:10,009 I can actually give this two numbers 608 00:50:10,079 --> 00:50:14,748 what this is gonna do is it's gonna take the first number that we 609 00:50:14,880 --> 00:50:17,150 second number so if I said like 2 & 3 610 00:50:18,030 --> 00:50:23,449 And you'll notice I'm separating them with a comma this is 611 00:50:24,000 --> 00:50:29,659 So to give us 2 cubed so when I run my program and actually we 612 00:50:29,670 --> 00:50:34,010 So this is gonna give us a decimal number back. So I need to print 613 00:50:34,619 --> 00:50:39,439 When I run my program we're gonna get 8 back just like that. So 614 00:50:40,019 --> 00:50:44,688 Cubing 2 I could say like 4 raised to the third power and now we 615 00:50:47,400 --> 00:50:52,010 So that can be pretty useful. There's a couple other ones. I'll 616 00:50:52,010 --> 00:50:54,560 So this will allow you to take a number square root 617 00:50:54,559 --> 00:50:59,719 So if I said like 36 in here now, we're gonna print out the square 618 00:50:59,719 --> 00:51:02,569 Which is gonna be 6 and there's a couple other ones 619 00:51:02,570 --> 00:51:08,059 So let's say that I had a decimal I could say C EIL which stands 620 00:51:08,159 --> 00:51:12,679 So if I have like thirty six point seven or 36 point three five 621 00:51:13,170 --> 00:51:19,159 This is gonna give us the hot the next highest number. So it'll 622 00:51:23,670 --> 00:51:28,280 We can do another one which is called floor and floor. We'll do 623 00:51:28,280 --> 00:51:34,519 We'll just round the number down no matter what. So if I have like 624 00:51:36,719 --> 00:51:39,408 So there's all sorts of little, you know things we can do 625 00:51:39,929 --> 00:51:44,358 little functions that we can call and get information about 626 00:51:44,789 --> 00:51:48,978 Functions will either like modify a number so it would like give 627 00:51:49,320 --> 00:51:52,639 Or sometimes they'll like give us information about numbers 628 00:51:52,639 --> 00:51:55,579 So that's sort of the basics of working with numbers, you know 629 00:51:55,579 --> 00:51:59,768 you can do all sorts of math with numbers you can add in some to 630 00:52:00,019 --> 00:52:05,050 Multiply add subtract integer numbers and floating-point numbers. 631 00:52:05,300 --> 00:52:09,070 different things with numbers and if you want to find some more of 632 00:52:09,070 --> 00:52:13,570 All you have to do is just go online and Google search for C math 633 00:52:13,570 --> 00:52:17,979 there's like dozens of these little functions that you can use, 634 00:52:24,079 --> 00:52:31,038 I want to talk to you guys about using comments and see well 635 00:52:31,259 --> 00:52:35,987 Which actually gets ignored when we run our program, so if you 636 00:52:35,989 --> 00:52:39,798 you can use a special starting and ending tag and any of the 637 00:52:40,079 --> 00:52:47,359 Text any of the code anything that you put inside of that tag 638 00:52:47,518 --> 00:52:50,298 So we can use comments to do all sorts of things, you know 639 00:52:50,298 --> 00:52:55,248 You can leave little notes or little comments inside of your 640 00:52:55,918 --> 00:52:59,538 Temporarily disable certain lines of code. They can be really 641 00:52:59,880 --> 00:53:05,959 So I'm gonna show you guys the basics down here in my program if I 642 00:53:05,958 --> 00:53:12,978 Using a forward slash and an asterisk and you'll see when I put 643 00:53:13,168 --> 00:53:20,028 and basically what this means is anything that comes after this 644 00:53:20,130 --> 00:53:25,849 Order to close off the comment. I can use an another asterisk and 645 00:53:26,099 --> 00:53:28,998 You'll see the code changed back to normal code 646 00:53:29,188 --> 00:53:35,958 Inside of these two asterisks is I can basically write out 647 00:53:37,398 --> 00:53:44,628 If I you know wrote out like my program and I ran my program this 648 00:53:44,818 --> 00:53:47,027 So it's just gonna print out comments or fun 649 00:53:47,028 --> 00:53:49,487 It's not gonna do anything else and you know 650 00:53:49,489 --> 00:53:53,059 Like I said comments are really useful so I could leave a little 651 00:53:53,059 --> 00:53:59,778 I could write something like to do like I have to do something and 652 00:54:00,208 --> 00:54:07,068 Explain certain lines of code so I could put a comment here and I 653 00:54:07,068 --> 00:54:10,637 So maybe you know, this was like a really complex line of code 654 00:54:10,679 --> 00:54:13,818 We could use this text in order to explain it 655 00:54:13,818 --> 00:54:18,079 You can also use comments to do something called commenting out a 656 00:54:18,239 --> 00:54:23,749 So for example, let's say I wanted to run my program but I didn't 657 00:54:23,969 --> 00:54:27,229 Well one thing I could do would just be to delete the entire line 658 00:54:27,228 --> 00:54:32,298 Right so I could delete this code and now I can run my program and 659 00:54:32,298 --> 00:54:37,398 It's no longer gonna do anything. But here's the problem in order 660 00:54:37,559 --> 00:54:42,469 so instead of deleting it I could just bring it back and I could 661 00:54:43,230 --> 00:54:45,769 surround it with these comment tags and 662 00:54:46,320 --> 00:54:51,859 Now the computer is just gonna ignore it. So it's not gonna render 663 00:54:52,230 --> 00:54:56,510 So now when I run my program it's gonna do the same thing. It 664 00:54:57,059 --> 00:55:00,768 But I didn't have to actually delete the line. I could just 665 00:55:00,769 --> 00:55:02,869 So a lot of times when you're writing programs 666 00:55:02,909 --> 00:55:06,409 You might want to try to run your program without a certain line 667 00:55:06,409 --> 00:55:12,529 you know series of lines of code and instead of deleting them and 668 00:55:13,079 --> 00:55:15,769 See will basically ignore them so that can be really useful 669 00:55:16,260 --> 00:55:20,719 So those are the two basic uses for comments and essentially 670 00:55:20,719 --> 00:55:25,698 It's just a way that we can write out information in our files. 671 00:55:25,739 --> 00:55:29,000 And so you can really use comments to do whatever you want 672 00:55:29,070 --> 00:55:33,109 Now I will say one best practice with comments is to use them 673 00:55:34,170 --> 00:55:40,940 Generally, you only want to use a comment when you absolutely have 674 00:55:40,940 --> 00:55:45,769 File and there's like comments littered all around and obviously 675 00:55:45,769 --> 00:55:50,088 I mean no one's stopping you but as a best practice going forward 676 00:55:50,088 --> 00:55:56,088 I think generally only using comments when they're absolutely 677 00:55:56,088 --> 00:55:59,208 But again, it's open, you know, you can use comments for whatever 678 00:56:03,170 --> 00:56:07,280 In this tutorial I want to talk to you guys about constants in C 679 00:56:07,559 --> 00:56:12,979 Well constant is a special type of variable and C which can't be 680 00:56:13,139 --> 00:56:17,808 So when I create a constant and C, I'm basically creating a value 681 00:56:18,329 --> 00:56:24,079 Unable to be modified. So I'm gonna show you guys basically how 682 00:56:24,210 --> 00:56:28,068 Let's say I created an integer and I just called it num and I set 683 00:56:28,139 --> 00:56:34,818 right, and then I printed out that integer onto the screen so we 684 00:56:35,460 --> 00:56:40,818 We're just gonna pranaam right so I can create this num variable. 685 00:56:42,289 --> 00:56:46,938 I wanted to modify num so I could basically say num is equal to 8 686 00:56:47,159 --> 00:56:49,519 The value that's stored inside of the num variable 687 00:56:50,369 --> 00:56:53,327 And let's come down here and we'll print this out as well. So 688 00:56:55,588 --> 00:56:59,208 And we'll print out num again. And actually I'm gonna print out a 689 00:57:00,269 --> 00:57:05,628 So when I go ahead and run this program and I forgot to send me : 690 00:57:06,150 --> 00:57:12,650 you'll see we're printing out five and then we modified the value 691 00:57:17,400 --> 00:57:24,650 But in certain circumstances and see you're gonna want to create 692 00:57:24,989 --> 00:57:28,878 So these would be values that just like can't change and those are 693 00:57:28,920 --> 00:57:32,690 So if I wanted to make a num a constant in other words, let's say 694 00:57:32,730 --> 00:57:36,798 I didn't want the num variable to be able to be modified in my 695 00:57:36,798 --> 00:57:38,798 I could come over here and I could say 696 00:57:39,030 --> 00:57:46,429 Const so Co NS T and I'm gonna say that right before I declare the 697 00:57:47,219 --> 00:57:51,408 Right after you declare the type, but I've always preferred to 698 00:57:52,318 --> 00:57:59,898 So I say Const int num 5 and now this program is actually gonna 699 00:58:00,599 --> 00:58:06,919 You'll see down here. It's getting highlighted in red. That's 700 00:58:06,920 --> 00:58:09,108 So I'm trying to modify a num down here 701 00:58:09,298 --> 00:58:14,568 But I can't because I gave it this Const keyword because it's now 702 00:58:14,579 --> 00:58:19,489 So in a lot of cases you want have variables that just can't be 703 00:58:19,739 --> 00:58:25,129 In a situation like that you want to make them consonants also a 704 00:58:25,170 --> 00:58:28,849 We'll give them all uppercase names and this isn't required 705 00:58:28,849 --> 00:58:33,199 this is just sort of like what a lot of developers will do so 706 00:58:33,599 --> 00:58:38,958 Um like this they would say like num or maybe this is like your 707 00:58:40,320 --> 00:58:42,830 So that would be how you could like distinguish two words 708 00:58:43,050 --> 00:58:48,919 but generally like constants will be all capital and that's just 709 00:58:49,409 --> 00:58:54,199 Unchangeable that they're constants. But again, you don't have to 710 00:58:54,360 --> 00:58:56,568 so this is one way that we can create a 711 00:58:56,820 --> 00:59:03,409 constant, and we basically create a constant variable that can't 712 00:59:03,599 --> 00:59:07,518 Just like any number or any text that we're using in our program 713 00:59:07,519 --> 00:59:12,889 So, for example, I'm gonna get rid of this if I came down here and 714 00:59:13,050 --> 00:59:18,979 So let's say I just printed out like Hello. This is actually also 715 00:59:18,980 --> 00:59:21,619 So this is just like a string of characters a string of text 716 00:59:21,900 --> 00:59:28,309 That's also considered a constant because it's just like text on 717 00:59:29,670 --> 00:59:31,380 so you would also consider this a 718 00:59:31,380 --> 00:59:35,420 Constant if I was printing out a number to like let's say I came 719 00:59:36,780 --> 00:59:39,860 77 T is also considered a constant 720 00:59:39,960 --> 00:59:45,889 So it's just considered to be like a piece of information or a 721 00:59:45,889 --> 00:59:49,400 That is sort of unchanging. Right? So 70 this isn't gonna change 722 00:59:49,409 --> 00:59:52,219 I mean unless I physically came in here and changed it like 723 00:59:52,380 --> 00:59:59,059 This value is always gonna be 90 no matter what I can't modify it 724 00:59:59,059 --> 01:00:01,878 So that's also considered a constant 725 01:00:02,369 --> 01:00:07,248 But I would say like for the most part like where this is going to 726 01:00:07,250 --> 01:00:11,239 But creating constant variables variables that can't change 727 01:00:15,730 --> 01:00:20,500 In this tutorial I'm gonna show you guys how to get input from a 728 01:00:20,659 --> 01:00:22,149 So a lot of times in our C program 729 01:00:22,150 --> 01:00:28,119 So we're going to be working with all different types of 730 01:00:28,400 --> 01:00:34,059 Information from the user so I'm going to show you guys how we can 731 01:00:34,550 --> 01:00:40,239 We can take that information store it inside of variables and then 732 01:00:40,239 --> 01:00:42,339 So this is gonna be pretty cool. So down here 733 01:00:42,340 --> 01:00:48,009 I will basically write out the code for this and the first thing 734 01:00:48,139 --> 01:00:52,900 We want to prompt them. So I want to prompt the user, you know, as 735 01:00:52,969 --> 01:00:58,388 So I'm just gonna say printf and over here. I'm just gonna print 736 01:00:59,179 --> 01:01:04,658 Ask the user to enter in their age. So well, I'll show you guys 737 01:01:04,659 --> 01:01:07,480 so I'm just gonna say enter your age and 738 01:01:08,389 --> 01:01:11,980 Now that we've prompted them to enter their age I need to do two 739 01:01:12,440 --> 01:01:17,409 So the first thing I want to do is create a variable where we can 740 01:01:18,019 --> 01:01:20,708 the input that the user puts into the program 741 01:01:20,710 --> 01:01:25,570 So I want to create a variable where we can store the age that the 742 01:01:25,570 --> 01:01:30,429 So I'm gonna create an inn over here. Oh, it's called age. And I'm 743 01:01:30,429 --> 01:01:32,429 so all I'm gonna do up here is just 744 01:01:32,630 --> 01:01:36,670 Declare the variable. I'm just gonna tell see that I want to use 745 01:01:36,670 --> 01:01:43,690 But I'm not gonna give it a value in other words. I'm gonna allow 746 01:01:44,239 --> 01:01:49,269 So down here I want to be able to get input for from the user so I 747 01:01:51,500 --> 01:01:57,340 scanf is basically going to allow the user to enter in some 748 01:01:57,619 --> 01:02:02,829 This works similar to printf it's kind of doing the opposite of 749 01:02:02,900 --> 01:02:05,139 Scanf is allowing the user to input something 750 01:02:05,659 --> 01:02:10,239 Into the program and we're gonna make an open and close quotation 751 01:02:10,239 --> 01:02:16,029 I basically want to tell see what type of information I'm asking 752 01:02:16,030 --> 01:02:19,419 So in our case we're asking for an integer right age is going to 753 01:02:19,420 --> 01:02:24,099 it's a whole number so I'm gonna accept as an input and integer 754 01:02:24,739 --> 01:02:28,750 Over here. We want to tell C where we want to put integer 755 01:02:28,849 --> 01:02:35,619 So I'm basically gonna tell see what variable I want to store this 756 01:02:36,199 --> 01:02:39,399 I can essentially just type out the name of the variable here 757 01:02:39,769 --> 01:02:46,150 But in order to get input from the user I'm actually gonna have to 758 01:02:46,219 --> 01:02:50,409 So instead of just typing out age. I'm gonna have to type 759 01:02:50,570 --> 01:02:56,830 And when I say ampersand age, this is what's called a pointer and 760 01:02:56,829 --> 01:02:59,288 I'm gonna cover everything you need to know about pointers 761 01:02:59,289 --> 01:03:02,528 But for now, that's a little bit beyond what we need to learn 762 01:03:02,530 --> 01:03:07,600 so all you need to know is that when you're using scanf and you 763 01:03:07,849 --> 01:03:13,449 An integer or a float or like a character you need to use this 764 01:03:14,090 --> 01:03:19,989 So down here now that we've scanned for the users age. I'm just 765 01:03:19,989 --> 01:03:26,619 So we'll just print out like you are and I'll say % D years old 766 01:03:26,619 --> 01:03:31,929 I'm just gonna print out that age variable. So essentially what 767 01:03:31,929 --> 01:03:38,440 I'm storing whatever age they enter inside of this age variable 768 01:03:38,750 --> 01:03:41,918 So let's go ahead and run this program and we'll see how we did 769 01:03:41,920 --> 01:03:47,590 So I'm gonna run the program and it says enter your age. So let's 770 01:03:49,119 --> 01:03:54,489 It's can take that integer that we entered 50 store it inside of 771 01:03:54,679 --> 01:03:56,679 You'll see over here. It says you are 772 01:03:57,050 --> 01:03:59,919 50 years old. So that's how we can get an integer from the user 773 01:04:00,199 --> 01:04:03,039 We can also get like a double from the user 774 01:04:03,039 --> 01:04:08,289 so for example, I could say enter your GPA and so now instead of 775 01:04:08,809 --> 01:04:13,090 asking for an integer we're gonna be asking for a double so I can 776 01:04:13,699 --> 01:04:18,460 We could call this GPA and now I can do the same thing. But 777 01:04:18,460 --> 01:04:24,730 I want to say L F and L F is basically going to tell this scanf 778 01:04:24,980 --> 01:04:30,938 Double and then obviously instead of age. We're just gonna put GPA 779 01:04:31,909 --> 01:04:38,648 your GPA is and then we can put % F because we're gonna be 780 01:04:39,079 --> 01:04:43,719 I'll just say GPA. So you'll notice here in printf when we want to 781 01:04:43,719 --> 01:04:49,748 We're using percent F to print it out. But when we're using scanf 782 01:04:49,748 --> 01:04:53,498 So that's just like a little difference and now let's go ahead and 783 01:04:53,768 --> 01:04:59,108 And we should be able to get a GPA. So let's say someone's GPA is 784 01:04:59,809 --> 01:05:04,119 3.1. So that's how we can get a double. I also want to show you 785 01:05:04,728 --> 01:05:09,548 so why don't we create a character up here and we'll just call it 786 01:05:14,298 --> 01:05:18,728 Now when we want to get a character we can just say percent C and 787 01:05:18,728 --> 01:05:25,298 I can just come down here and say ampersand grade and then down 788 01:05:26,449 --> 01:05:30,219 Percent C and we'll go ahead and print out the grade 789 01:05:30,219 --> 01:05:34,178 So now we should be able to get a character from the user 790 01:05:35,798 --> 01:05:42,849 let's say I got an A and it says you're good as a so we can use 791 01:05:43,489 --> 01:05:48,818 Input from the user we can store those specific types of input 792 01:05:48,818 --> 01:05:53,588 I also want to show you guys one more thing we can do which is we 793 01:05:53,588 --> 01:05:59,527 so in addition to getting numbers and a character, we could also 794 01:06:00,170 --> 01:06:02,829 this is going to be a little bit different from 795 01:06:03,679 --> 01:06:09,518 Doing like numbers and characters. So I want to kind of show you 796 01:06:09,518 --> 01:06:14,288 Let's create a variable. We'll just call it. Let's just call it 797 01:06:15,798 --> 01:06:21,369 Whenever we're creating a string of characters, we always need 798 01:06:21,498 --> 01:06:28,688 I'm actually going to specify how many characters I want to be 799 01:06:30,949 --> 01:06:36,098 In the past in this course when we when we've been creating 800 01:06:36,099 --> 01:06:41,018 We've just kind of said like whatever. All right, we basically 801 01:06:41,150 --> 01:06:47,440 But in this particular situation, we're not gonna be giving this 802 01:06:47,440 --> 01:06:51,489 Like I don't know what the users name is gonna be like, I don't 803 01:06:51,489 --> 01:06:55,688 They're gonna tell us what that is. And so if I'm not gonna give 804 01:06:55,900 --> 01:07:01,150 I do actually need to tell see how big I want this variable to be 805 01:07:01,150 --> 01:07:07,269 I need to tell see how many characters I want this variable to be 806 01:07:07,269 --> 01:07:14,318 Allocate enough memory for this variable. So I'm just gonna put 20 807 01:07:14,838 --> 01:07:19,268 And I think that's enough for a name so down here. We'll just say 808 01:07:20,329 --> 01:07:23,109 I can use scanf in order to get input 809 01:07:23,269 --> 01:07:28,088 in the form of a string but instead of saying percent see I'm just 810 01:07:28,460 --> 01:07:31,088 Over here instead of saying ampersand grade 811 01:07:31,088 --> 01:07:38,018 I'm just gonna type out the name of the string of characters. So I 812 01:07:38,568 --> 01:07:41,768 Specify the name of the string so down here 813 01:07:41,769 --> 01:07:47,199 it says printf your grade is and actually let's just say your name 814 01:07:47,838 --> 01:07:53,077 I would have say percent s and then over here we can print out the 815 01:07:53,719 --> 01:07:57,158 So this should work just like it did in the other cases 816 01:07:57,159 --> 01:08:01,929 So let's go ahead and run this and it says enter your name. So my 817 01:08:02,630 --> 01:08:09,400 You'll see it says your name is Mike. So that works out really 818 01:08:13,150 --> 01:08:16,690 So for example, if I came in here, and I said enter your name, and 819 01:08:19,609 --> 01:08:26,139 You'll notice that it's only saying your name is. John, it's not 820 01:08:26,719 --> 01:08:28,989 Here's the problem. This is because 821 01:08:29,509 --> 01:08:37,149 Whenever I use this scanf function and I use it with a string. 822 01:08:37,310 --> 01:08:42,909 Space so once it sees this space it's gonna be like, okay, we're 823 01:08:42,908 --> 01:08:48,009 So that's kind of a problem and that's just kind of how scanf 824 01:08:48,679 --> 01:08:53,798 There is a way that we can modify scanf in order to be able to get 825 01:08:54,140 --> 01:09:00,579 but there's another function which I want to show you guys which 826 01:09:03,789 --> 01:09:06,789 it's similar to scanf but f gets is 827 01:09:07,189 --> 01:09:11,839 Basically going to be more generous F gas is essentially just 828 01:09:11,840 --> 01:09:18,590 It's not gonna be able to grab it and store it inside of like an 829 01:09:18,590 --> 01:09:21,590 It's just gonna be able to store it inside of like a string of 830 01:09:22,319 --> 01:09:24,590 When we're using a forgets the first argument 831 01:09:24,590 --> 01:09:29,059 We want to give it is the name of the variable where we want to 832 01:09:29,060 --> 01:09:31,189 So in our case, it's just gonna be this name variable 833 01:09:31,649 --> 01:09:36,318 The next thing we want to do is specify how many characters we 834 01:09:36,689 --> 01:09:40,759 So this will essentially limit the amount of characters that the 835 01:09:42,060 --> 01:09:46,910 And this is always a good idea when we're trying to get a string 836 01:09:49,109 --> 01:09:54,048 Enter in like a million characters and they would overflow the 837 01:09:54,050 --> 01:09:58,849 Like see wouldn't be able to handle accepting that many characters 838 01:10:01,369 --> 01:10:05,059 We can specify how many characters we want to be able to accept so 839 01:10:05,130 --> 01:10:09,469 Because that's how many characters we can store inside of this 840 01:10:09,470 --> 01:10:13,909 I'm just gonna say stdin and this stands for standard input 841 01:10:13,909 --> 01:10:15,949 what we're doing over here is we're telling FCAT s-- 842 01:10:16,289 --> 01:10:22,609 where we want to get the information from and standard input is 843 01:10:23,010 --> 01:10:27,590 Now I'm using F Gatz and this is going to do exactly what scanf 844 01:10:27,840 --> 01:10:31,548 the input inside of this named variable, but now we'll be able to 845 01:10:33,359 --> 01:10:38,598 Multiple words instead of just one single word. So let's run this 846 01:10:38,600 --> 01:10:40,670 So now if we entered in John Smith 847 01:10:41,100 --> 01:10:43,910 You'll see it's able to enter in John Smith 848 01:10:44,189 --> 01:10:51,439 Now the one downside with just using this F gets is you'll notice 849 01:10:51,779 --> 01:10:54,259 So let me actually demonstrate this a little bit 850 01:10:54,840 --> 01:10:58,340 So if I was to print out like, you know, just some random text 851 01:10:59,670 --> 01:11:04,338 Whenever I get input using scanf, so if I entered in John Smith 852 01:11:06,689 --> 01:11:13,759 So when I'm entering this by clicking the Enter key that's 853 01:11:14,640 --> 01:11:19,999 Inside of the string. So when I click enter you'll notice it says 854 01:11:20,010 --> 01:11:23,859 And then it prints out a newline and it prints out this text 855 01:11:23,899 --> 01:11:29,349 And so that's just something that you need to be aware of when 856 01:11:29,750 --> 01:11:32,979 but for the most part, I would say if you're getting input from 857 01:11:33,109 --> 01:11:39,759 A user in the form of a string you can use F gets you can also use 858 01:11:41,119 --> 01:11:45,189 Percent asses and i'm actually going to show you guys how we could 859 01:11:45,590 --> 01:11:50,350 But for the most part I would say whenever you're getting a string 860 01:11:50,630 --> 01:11:53,560 Guess so that's the basics of getting input from the user 861 01:11:54,279 --> 01:11:56,648 there's a couple other things that we can do and I'm gonna talk 862 01:11:57,079 --> 01:12:01,779 At least one other way that we can get strings from the user in a 863 01:12:01,909 --> 01:12:06,668 But this is kind of the basics and you can kind of play around 864 01:12:10,529 --> 01:12:15,059 In this tutorial I'm going to show you guys how to build a basic 865 01:12:15,219 --> 01:12:19,469 we're basically gonna build a little program where the user can 866 01:12:19,630 --> 01:12:23,969 Our program will take those two numbers add them together and spit 867 01:12:23,969 --> 01:12:29,338 So this is gonna be kind of cool and we'll also learn about 868 01:12:29,979 --> 01:12:33,929 So over here, I want to show you guys exactly how we can do this 869 01:12:33,929 --> 01:12:35,800 the first thing I want to do is 870 01:12:35,800 --> 01:12:43,079 Print out a prompt. So I basically want to prompt the user for 871 01:12:44,349 --> 01:12:49,139 Inside here. We're just gonna give them a little prompt. So I'll 872 01:12:51,698 --> 01:12:58,318 Essentially what we're gonna have them do is enter in two numbers, 873 01:12:58,319 --> 01:13:00,868 so we'll make an int and we'll call it num1 and 874 01:13:01,479 --> 01:13:05,999 Then we'll make another inch and we'll call it num2 and i'm not 875 01:13:06,519 --> 01:13:11,069 Values right up front we're gonna end up giving these whatever the 876 01:13:11,229 --> 01:13:18,088 So after the user enters the first number we need to actually grab 877 01:13:20,109 --> 01:13:25,798 In here, we're basically just gonna say percent d because we want 878 01:13:25,800 --> 01:13:27,929 I'm going to say the name of the variable 879 01:13:28,118 --> 01:13:34,048 Where I want to store the value that gets entered now if you're 880 01:13:34,328 --> 01:13:38,188 We were able to get a string of characters as input from the user 881 01:13:38,189 --> 01:13:41,728 And basically we just typed in like the name of the variable 882 01:13:41,889 --> 01:13:45,088 But when we're getting input, that's not a string of characters 883 01:13:45,090 --> 01:13:51,298 In other words when we're getting input that's not percent s so if 884 01:13:51,300 --> 01:13:56,788 so if it's a guy a decimal or an integer or a float or a 885 01:13:57,309 --> 01:13:59,639 special symbol this ampersand 886 01:13:59,639 --> 01:14:04,618 So I'm gonna have to say ampersand and then the name of the 887 01:14:04,618 --> 01:14:06,929 so I'm just gonna say ampersand 1 and 888 01:14:07,420 --> 01:14:14,759 Basically what this means is we're accessing the address of num 1 889 01:14:14,760 --> 01:14:18,748 but for now just know that you need this ampersand here in order 890 01:14:19,328 --> 01:14:22,889 Store the value that gets entered inside of this variable 891 01:14:25,569 --> 01:14:31,048 we're basically just gonna do the same exact thing but for the 892 01:14:31,689 --> 01:14:35,759 I'll paste this and now instead of saying enter first number. 893 01:14:36,340 --> 01:14:40,109 Second number and we're just gonna store this inside of num2 894 01:14:40,930 --> 01:14:46,109 Alright, so once we're done with this, the last thing we want to 895 01:14:46,539 --> 01:14:49,319 So I'm just going to come down here. I'm going to say printf and 896 01:14:50,260 --> 01:14:52,979 We'll just say answer and over here 897 01:14:52,979 --> 01:14:59,188 We'll print out the answer so it's gonna be an integer and we're 898 01:14:59,590 --> 01:15:06,929 So we're gonna print out the value of num1 plus num2. So we have 899 01:15:06,930 --> 01:15:11,939 We're getting the second number. We're storing them inside of 900 01:15:12,340 --> 01:15:18,029 Let's try to run our calculator. So I'm gonna run my program and 901 01:15:18,029 --> 01:15:20,029 So let's go ahead and enter 6 enter 902 01:15:22,659 --> 01:15:27,779 A8 and now we're gonna get 14. So 14 is the correct answer 903 01:15:27,779 --> 01:15:31,768 So our program worked it was able to add the numbers correctly and 904 01:15:32,439 --> 01:15:36,750 But let me show you guys one problem with this program if I wanted 905 01:15:37,300 --> 01:15:44,550 Multiple non decimal numbers, for example if I said 2 and then 906 01:15:45,250 --> 01:15:49,199 We're not going to get the correct answer. We're gonna get 6 plus 907 01:15:49,720 --> 01:15:55,649 Which is gonna be an integer 8, but we're not gonna get 8 point 8, 908 01:15:55,720 --> 01:15:58,979 So instead of letting the user enter in only integers 909 01:15:58,979 --> 01:16:02,639 Why don't we instead let them enter in doubles so over here? 910 01:16:02,640 --> 01:16:07,439 I'm going to change these to double so I'm gonna say num one's 911 01:16:07,659 --> 01:16:11,760 Since we're getting doubles as input, we're gonna have to come 912 01:16:11,760 --> 01:16:14,760 So right now this is accepting an integer 913 01:16:15,159 --> 01:16:18,300 but we want to make this accept a double so normally if we're 914 01:16:18,460 --> 01:16:23,819 Printf and we wanted to print out a double we would say f and that 915 01:16:23,920 --> 01:16:27,449 but when we're scanning for a number if we want to use a double we 916 01:16:27,609 --> 01:16:33,000 LF just like that so down here. I'm also gonna say LF and we'll 917 01:16:33,000 --> 01:16:35,970 so print F is a little bit different than 918 01:16:36,579 --> 01:16:42,269 Scanf right and scanf if we want to accept a double as input. We 919 01:16:42,609 --> 01:16:47,579 We just use % F. So let's run our program now and we should be 920 01:16:48,100 --> 01:16:50,879 floating-point numbers so decimal numbers over here 921 01:16:50,880 --> 01:16:57,750 I will do four point five plus six point seven and we get 11 point 922 01:16:57,750 --> 01:17:01,918 Looks like everything is working properly. And now we have a basic 923 01:17:01,920 --> 01:17:07,289 So the user can enter in any numbers that they want and the 924 01:17:07,289 --> 01:17:10,079 Now this calculator is not like 100% 925 01:17:10,689 --> 01:17:14,460 ironclad secure for example, like if I was to come up here and 926 01:17:15,250 --> 01:17:21,060 Enter in like a string of characters instead of a number you'll 927 01:17:21,060 --> 01:17:23,430 So it's just like not doing what we wanted to do 928 01:17:23,430 --> 01:17:25,409 And as we go further in this course 929 01:17:25,409 --> 01:17:31,828 We're gonna learn all sorts of ways that we can check to see if 930 01:17:32,199 --> 01:17:39,300 circumstances like that, but for now, this is just kind of an 931 01:17:39,819 --> 01:17:41,878 Addition or multiplication once we have them 932 01:17:45,630 --> 01:17:51,389 In this tutorial I'm going to talk to you guys about building a 933 01:17:51,389 --> 01:17:53,519 We're gonna be building a mad libs game now 934 01:17:53,520 --> 01:17:55,110 If you ever played the game mad libs 935 01:17:55,109 --> 01:17:59,519 it's basically a game where you write down a bunch of random words 936 01:17:59,520 --> 01:18:04,649 You know nouns or verbs or someone's name or you know a verb 937 01:18:04,750 --> 01:18:07,889 you take all of those words that you enter in and you kind of 938 01:18:08,078 --> 01:18:14,158 Sprinkle them in into a story and then generally the story is like 939 01:18:14,170 --> 01:18:16,170 It's actually if we head over to my web browser 940 01:18:16,569 --> 01:18:19,198 You'll see I have a picture of a Madlib up here 941 01:18:19,198 --> 01:18:24,838 And basically you just add in a bunch of random words into the 942 01:18:24,840 --> 01:18:29,460 Kind of funny so I'm gonna show you guys how we can build 943 01:18:29,500 --> 01:18:35,878 And we're also gonna talk about some more ways that we can use 944 01:18:36,399 --> 01:18:42,658 So over here, we have a little story that I printed out. It just 945 01:18:42,658 --> 01:18:45,058 I love you kind of like a classic poem 946 01:18:45,460 --> 01:18:49,980 But I think this poem would be a lot funnier if we turned it into 947 01:18:49,979 --> 01:18:56,908 So that's exactly what we're gonna do. I'm gonna replace roses are 948 01:18:56,979 --> 01:19:02,068 So we're just gonna have a user enter in a color. I'll replace 949 01:19:02,109 --> 01:19:07,318 So we're gonna have them enter in a plural noun, and I'm gonna 950 01:19:07,319 --> 01:19:13,259 We're gonna say I love and then a specific celebrity. So I'll just 951 01:19:14,078 --> 01:19:20,938 Alright, so this is basically what we're gonna be printing out 952 01:19:21,578 --> 01:19:28,378 Plural nouns are blue and then I love whatever celebrity so let's 953 01:19:28,380 --> 01:19:30,380 So we're actually going to need to do a couple things 954 01:19:30,729 --> 01:19:36,778 And actually the first thing I want to do is I want to create 955 01:19:37,569 --> 01:19:43,380 The color that the user inputs the plural noun that the user 956 01:19:43,380 --> 01:19:47,219 we're going to create three variables and these are basically 957 01:19:48,130 --> 01:19:50,130 character strings, so they're going to be 958 01:19:50,380 --> 01:19:52,469 Collections of characters and we can store them in 959 01:19:53,060 --> 01:19:59,410 Variable so I'm gonna create some of these variables. Why don't we 960 01:20:00,050 --> 01:20:02,140 String or like a collection of characters? 961 01:20:02,140 --> 01:20:04,660 We need to make these open and closed square brackets 962 01:20:04,939 --> 01:20:12,309 And what I also want to do because I'm not gonna be giving color 963 01:20:14,199 --> 01:20:17,378 I just need to tell see how many characters we want this 964 01:20:17,569 --> 01:20:23,528 String to be able to store that way si knows how much memory it 965 01:20:23,779 --> 01:20:29,139 So I'm just gonna say 20 and we'll basically just say they can 966 01:20:29,420 --> 01:20:32,260 we're gonna do the same thing for plural noun and 967 01:20:33,229 --> 01:20:36,458 again, we'll let them enter in 20 characters maximum and 968 01:20:37,850 --> 01:20:43,059 Finally, we're gonna do the same thing for celebrity. So again 20 969 01:20:43,279 --> 01:20:48,189 Alright now that we have our variables created. I want to actually 970 01:20:48,189 --> 01:20:49,729 so I want to prompt the user for 971 01:20:49,729 --> 01:20:54,969 Information and I want to take the information that they entered 972 01:20:55,520 --> 01:21:00,819 First order of business is to prompt them for input so I can just 973 01:21:00,819 --> 01:21:03,340 We'll basically just type in enter a color 974 01:21:04,130 --> 01:21:11,230 Once we've prompted them to enter the color we can actually get 975 01:21:11,359 --> 01:21:13,359 I'm going to use a function called 976 01:21:13,670 --> 01:21:16,779 Scanf and over here. I'm gonna accept a 977 01:21:17,300 --> 01:21:24,760 string so I'm going to accept a string of characters and we're 978 01:21:25,159 --> 01:21:27,998 remember if you watched the last tutorial we use the 979 01:21:28,880 --> 01:21:34,659 Ampersand here when we were getting numbers or also you'd do the 980 01:21:34,909 --> 01:21:40,930 But when we're getting input for a string of characters, we don't 981 01:21:41,779 --> 01:21:45,039 Let's copy these and I'm gonna paste this two more times 982 01:21:45,039 --> 01:21:47,739 so the second thing we want to get from them is going to be the 983 01:21:48,260 --> 01:21:56,168 plural noun and I'm gonna store this inside of the plural noun 984 01:21:56,600 --> 01:22:02,169 Finally, we're gonna do the same thing for celebrity. And again, 985 01:22:03,359 --> 01:22:10,518 Okay, cool. So now I'm getting the color. I'm getting the plural 986 01:22:10,519 --> 01:22:15,139 So the last thing we have to do is we have to take all of these 987 01:22:15,139 --> 01:22:19,399 Right, so we need to be able to print out the story with all of 988 01:22:19,399 --> 01:22:24,918 So I'm gonna come down here and I'm just going to say percent s 989 01:22:26,158 --> 01:22:29,719 Same thing here. I'm gonna replace the plural noun here with a 990 01:22:30,630 --> 01:22:32,748 percent s and we'll pass in the 991 01:22:33,538 --> 01:22:38,028 Plural noun and finally same thing for celebrity down here 992 01:22:42,868 --> 01:22:48,917 Alright so everything seems to be wired up and you'll notice that 993 01:22:48,918 --> 01:22:53,179 Lines, let's go ahead and run this program and we'll see how we 994 01:22:54,118 --> 01:22:58,097 So over here, it's prompting us for a color. Why don't we enter in 995 01:23:01,319 --> 01:23:04,969 Microwaves and enter a celebrity. Why don't we just say? 996 01:23:06,090 --> 01:23:09,979 Prince so when I click enter it's gonna say roses are magenta 997 01:23:10,559 --> 01:23:16,369 Microwaves are blue. I love prints. So we were able to prompt the 998 01:23:16,368 --> 01:23:20,148 We took everything that they input we stored it in variables 999 01:23:20,149 --> 01:23:26,808 Then we printed all those variables out inside of our story and we 1000 01:23:26,809 --> 01:23:31,969 I do want to show you guys one way that this program could mess 1001 01:23:32,998 --> 01:23:37,278 So let's enter in a different color. I'm going to enter in like 1002 01:23:39,479 --> 01:23:45,259 And now enter in a celebrity so I'm gonna show you guys one way 1003 01:23:45,479 --> 01:23:51,739 If I entered in a celebrity with a first and a last name like Tom 1004 01:23:52,139 --> 01:23:58,368 You'll notice that instead of saying I love Tom Hanks. It's only 1005 01:23:58,889 --> 01:24:06,347 Here's the problem when we use that scanf function scanf is only 1006 01:24:06,689 --> 01:24:12,109 Whitespace, so essentially when we put this space here. We're 1007 01:24:12,448 --> 01:24:15,488 but in reality we want to be able to grab the there's 1008 01:24:15,859 --> 01:24:20,528 Full-name want to be able to grab the celebrities first and last 1009 01:24:21,050 --> 01:24:25,180 So this is a situation in C where we would have to modify our a 1010 01:24:27,079 --> 01:24:33,309 instead of just getting one variable like the celebrity I can 1011 01:24:33,560 --> 01:24:38,200 Celebrity F and that'll stand for celebrity first name and then 1012 01:24:38,199 --> 01:24:44,708 We can make another variable called celebrity l that'll stand for 1013 01:24:45,500 --> 01:24:52,509 instead of just scanning for one string of characters I can scan 1014 01:24:55,699 --> 01:25:00,818 Down here. We're gonna want to do the same thing so we can just 1015 01:25:02,270 --> 01:25:06,189 Celebrity L and we just need to add another percent here 1016 01:25:06,189 --> 01:25:12,849 So it's gonna say I love celebrities first name and celebrities 1017 01:25:12,890 --> 01:25:15,099 so I can enter in like red and 1018 01:25:17,659 --> 01:25:24,818 Microphones and now we can enter in Tom Hanks and we're printing 1019 01:25:24,890 --> 01:25:31,689 So that's one way that we could remedy this program and make it be 1020 01:25:31,689 --> 01:25:36,159 And it also just shows you guys a little bit more about how scanf 1021 01:25:36,319 --> 01:25:40,359 It's gonna stop getting the input at that first space now 1022 01:25:40,359 --> 01:25:43,960 Here's the thing about this program though if I wanted to enter in 1023 01:25:47,680 --> 01:25:51,639 So if I only wanted to enter in one the program actually isn't 1024 01:25:51,640 --> 01:25:54,338 so if I said like hats and down here if I said 1025 01:25:54,710 --> 01:26:00,970 Like Gandhi and I click enter you'll notice that it's still 1026 01:26:01,189 --> 01:26:03,878 Right, so I could enter in something here and then the program 1027 01:26:04,399 --> 01:26:08,588 But it was waiting for me after I entered in just that one name 1028 01:26:08,590 --> 01:26:12,430 So that's something that you're gonna have to you know play around 1029 01:26:12,649 --> 01:26:16,929 Basically C is gonna force you to be very specific about what the 1030 01:26:16,930 --> 01:26:22,389 so if the user needs to enter two things like two words and you 1031 01:26:22,390 --> 01:26:28,719 Enter in one word. You have to specify that. So you have to be 1032 01:26:32,399 --> 01:26:39,689 In this tutorial I'm gonna talk to you guys about using a raisin 1033 01:26:39,689 --> 01:26:42,689 we're gonna be dealing with a bunch of different data and 1034 01:26:43,000 --> 01:26:48,810 one of the things we can do to control and manage and sort of keep 1035 01:26:48,880 --> 01:26:56,369 Put data inside of things called arrays an array is really useful 1036 01:26:56,680 --> 01:27:01,079 But what happens in your programs when you're dealing with huge 1037 01:27:01,869 --> 01:27:05,609 Specifically huge amounts of information that are related right 1038 01:27:05,609 --> 01:27:10,859 I had a list of like a bunch of names or a list of a bunch of 1039 01:27:11,140 --> 01:27:13,680 Let's say I had like a list of a hundred numbers, right? 1040 01:27:13,680 --> 01:27:16,200 I wouldn't want to have to create a hundred different 1041 01:27:16,420 --> 01:27:23,039 variables to store all of those different numbers and this is 1042 01:27:23,289 --> 01:27:27,479 Data structure where we can store a bunch of different data 1043 01:27:27,579 --> 01:27:33,569 So inside of an array unlike a variable where I can only store one 1044 01:27:34,569 --> 01:27:41,849 So inside of a single array, I could store like five things or 1045 01:27:41,850 --> 01:27:47,370 I could sort a bunch of different pieces of information and then 1046 01:27:47,619 --> 01:27:51,059 Organized in my program so I'm gonna show you guys how to create 1047 01:27:53,069 --> 01:27:57,538 Like we create a normal variable and a variable in array are very 1048 01:27:57,909 --> 01:28:01,168 variable is basically used to define a container that 1049 01:28:01,720 --> 01:28:07,708 Stores a single value and an array is used to define a container 1050 01:28:07,779 --> 01:28:11,908 So the first thing we're gonna have to do when we create an array 1051 01:28:12,010 --> 01:28:17,820 Information and the first piece of information is what type of 1052 01:28:19,199 --> 01:28:24,179 I could say int and now I'd be creating an array that would hold 1053 01:28:24,180 --> 01:28:27,899 If I said char, I'd be creating an array that would hold 1054 01:28:28,449 --> 01:28:32,788 Or if I said like double the same thing but for double numbers, so 1055 01:28:32,789 --> 01:28:35,039 And I'm just gonna call this lucky 1056 01:28:35,680 --> 01:28:37,889 numbers whenever I create an array 1057 01:28:37,890 --> 01:28:44,010 I always want to use a special little symbol and this symbol will 1058 01:28:44,260 --> 01:28:47,739 Variable we want to an array and it's an open and closed 1059 01:28:48,109 --> 01:28:54,398 Square brackets whenever I use these open and close square 1060 01:28:54,399 --> 01:28:56,399 We want to store multiple pieces of information 1061 01:28:56,840 --> 01:28:59,679 Now there's a bunch of different ways. We can create these arrays 1062 01:28:59,719 --> 01:29:05,948 the easiest one though is to just say equals and I can make an 1063 01:29:06,260 --> 01:29:08,378 inside of this open and close curly bracket 1064 01:29:08,378 --> 01:29:14,738 I can just start typing out some numbers that I want to put in 1065 01:29:18,469 --> 01:29:23,588 Now I can basically type out as many numbers as I wanted and I 1066 01:29:23,840 --> 01:29:29,199 Inside of this array so over here, I you know, I printed out six 1067 01:29:29,300 --> 01:29:33,128 You know, the only thing that's limiting me is how many I can 1068 01:29:33,128 --> 01:29:37,059 So you'll notice that I put in the piece of data so like 4, for 1069 01:29:37,219 --> 01:29:42,097 And then I put a comma and a comma is gonna separate all the 1070 01:29:42,408 --> 01:29:47,617 So we would say 4 is the first element in the array 8 is the 1071 01:29:47,618 --> 01:29:49,658 These are all considered to be elements 1072 01:29:50,238 --> 01:29:55,268 Inside of this array and what's cool about this array is it's now 1073 01:29:55,639 --> 01:30:01,418 So unlike a variable where we can only store one value now in this 1074 01:30:01,418 --> 01:30:06,248 so if I wanted I can print this out and I'm actually gonna print 1075 01:30:06,769 --> 01:30:08,769 remember, we're gonna print out a 1076 01:30:08,899 --> 01:30:11,199 Number and I'm gonna show you guys how we can access 1077 01:30:11,899 --> 01:30:14,019 specific elements inside of this array 1078 01:30:14,628 --> 01:30:19,899 So all of these numbers are stored in this lucky numbers array in 1079 01:30:19,899 --> 01:30:24,039 But the question becomes how do we access them? Right, how can I 1080 01:30:24,349 --> 01:30:28,179 well, I could say present' d so I'm gonna print this out and now I 1081 01:30:30,158 --> 01:30:35,588 so I'm gonna refer to the name of the array and when I want to 1082 01:30:35,590 --> 01:30:39,219 Close square bracket and inside of this open and close square 1083 01:30:39,219 --> 01:30:45,398 I want to put the index of the element that I want to access so 1084 01:30:45,399 --> 01:30:49,149 And I can access them by putting the index inside of this square 1085 01:30:49,149 --> 01:30:52,029 so if I wanted to access this for for example 1086 01:30:52,038 --> 01:30:57,698 I can put a 0 inside of here. And now when I run my program you 1087 01:30:58,539 --> 01:31:00,539 We'll be able to print out that for 1088 01:31:01,850 --> 01:31:07,870 So over here, I'm printing out four if I wanted to access this 15 1089 01:31:07,869 --> 01:31:13,418 I can put a two in here. So now when I run my program I'll be 1090 01:31:15,649 --> 01:31:19,809 so one thing you might have noticed by now is that we start the 1091 01:31:19,970 --> 01:31:25,329 Indexes and arrays at 0 so when I wanted to access this 4 instead 1092 01:31:27,850 --> 01:31:32,620 You might think that 4 would be at index position 1 because it's 1093 01:31:33,109 --> 01:31:37,719 but in C we start array indexes at 0 so actually the first 1094 01:31:38,000 --> 01:31:43,060 element this 4 is gonna be at index position 0 so if I was going 1095 01:31:43,060 --> 01:31:49,989 I would say like 4 is at index position 0 this 8 is at index 1096 01:31:52,250 --> 01:31:57,009 So that's gonna allow me to access all these elements inside of 1097 01:31:57,470 --> 01:32:02,349 Individually, so I could print them out just by referring to the 1098 01:32:02,840 --> 01:32:06,670 Another thing I can do is I can modify some of the elements inside 1099 01:32:07,100 --> 01:32:12,399 Let's say I want to modify lucky numbers in x position 1 so let's 1100 01:32:12,500 --> 01:32:19,298 so I don't like this 8 anymore so I can basically just assign this 1101 01:32:21,979 --> 01:32:23,979 I can make an opening close square bracket 1102 01:32:23,979 --> 01:32:29,079 we can say 1 and I can just set it equal to something else so I 1103 01:32:29,270 --> 01:32:34,509 Now when we print out lucky numbers 1 instead of printing out an 1104 01:32:35,238 --> 01:32:39,968 200 so let's go ahead and do that and run my program and you'll 1105 01:32:40,340 --> 01:32:44,890 200 instead of 8 so you can modify any of the individual elements 1106 01:32:45,199 --> 01:32:49,778 Inside the array just by referring to the index and really when 1107 01:32:50,329 --> 01:32:56,378 Conceptually an array is basically just holding a bunch of 1108 01:32:56,380 --> 01:33:01,900 So when I access lucky numbers 1 it's the same as me accessing an 1109 01:33:02,329 --> 01:33:08,168 It's just that you know, the array is holding potentially hundreds 1110 01:33:08,359 --> 01:33:13,869 So an array is a very useful structure, especially if you're 1111 01:33:14,180 --> 01:33:16,150 So, like I said, I could do this with an N 1112 01:33:16,149 --> 01:33:20,769 I could also do this with a double I could do this with a float I 1113 01:33:20,770 --> 01:33:26,950 But there's gonna be situations where you might not necessarily 1114 01:33:26,949 --> 01:33:32,349 So let's say I'm creating an array and I don't know what numbers I 1115 01:33:32,350 --> 01:33:34,329 So maybe I just like want to create it 1116 01:33:34,329 --> 01:33:39,458 I want to tell C that we need it but I don't want to necessarily 1117 01:33:40,130 --> 01:33:46,298 Well up here. We're basically saying lucky numbers that open and 1118 01:33:46,310 --> 01:33:49,419 But if we don't want to give it all that information instead 1119 01:33:49,420 --> 01:33:55,270 I can just put a call a semicolon here but inside of these square 1120 01:33:55,279 --> 01:34:01,179 I need to tell see how many elements that this array can hold so I 1121 01:34:01,180 --> 01:34:06,338 Like hey, this array can only hold like ten elements or this array 1122 01:34:06,340 --> 01:34:11,770 So let's say we wanted an array that could hold like ten elements. 1123 01:34:12,289 --> 01:34:17,890 Capacity of this integer array is 10 so it has the potential to 1124 01:34:17,960 --> 01:34:23,560 and what I could do is I could come down here and I can start 1125 01:34:24,289 --> 01:34:28,539 Lucky numbers 1 is equal to 80, right? 1126 01:34:28,640 --> 01:34:35,349 so I'm giving lucky numbers 1 of value and now over here I could 1127 01:34:35,479 --> 01:34:37,129 You'll see we're printing of 80 1128 01:34:37,130 --> 01:34:43,690 but if I try to print out for example lucky numbers 0 so the lucky 1129 01:34:44,930 --> 01:34:52,059 This is gonna give me a negative 2 which basically means that it's 1130 01:34:52,760 --> 01:35:00,460 Element inside of lucky numbers at index position 0 but if I 1131 01:35:01,399 --> 01:35:08,139 0 is equal to 90 and now when I run my program it's gonna be able 1132 01:35:08,140 --> 01:35:09,829 So it'll be able to get that value 1133 01:35:11,380 --> 01:35:17,079 You're just gonna want to give these arrays values up front right 1134 01:35:17,079 --> 01:35:19,838 You're not gonna necessarily know what's supposed to go in there 1135 01:35:21,550 --> 01:35:25,659 you still have to tell see how many elements the array is gonna 1136 01:35:25,899 --> 01:35:28,778 You still have to like rigorously defined like hey 1137 01:35:28,779 --> 01:35:35,590 This array can only hold ten elements and that's just so C is able 1138 01:35:35,590 --> 01:35:38,949 Elements, so that's kind of the basics of working with arrays now 1139 01:35:38,948 --> 01:35:42,458 I do want to point out one thing that we've been using in this 1140 01:35:44,658 --> 01:35:51,248 strings, so I've been creating character strings so I could say 1141 01:35:52,010 --> 01:35:55,239 Giraffe Academy or other times in the program we created 1142 01:35:55,460 --> 01:35:57,520 Like when we were making our little game 1143 01:35:57,649 --> 01:36:02,439 we created an arrays and I would just say like 20 and then I would 1144 01:36:03,020 --> 01:36:05,919 Put that information in here. This is basically a 1145 01:36:06,469 --> 01:36:12,878 String of characters. So anytime we create a string in C like if I 1146 01:36:12,880 --> 01:36:15,789 I called it like array whatever. This is an array 1147 01:36:16,609 --> 01:36:19,028 So we've kind of just been taking this for granted 1148 01:36:19,029 --> 01:36:23,050 Like I've just been calling this a string or calling it like a 1149 01:36:23,329 --> 01:36:25,809 But it's basically just an array of characters 1150 01:36:26,060 --> 01:36:30,489 The only difference is this is such a common thing to use in our 1151 01:36:31,429 --> 01:36:35,828 C makes it a little C makes it special and makes it really easy 1152 01:36:35,829 --> 01:36:38,619 But just like that array of integers. This is also an array 1153 01:36:38,619 --> 01:36:42,849 So now you kind of have a better understanding of what strings are 1154 01:36:47,210 --> 01:36:50,539 I'm going to talk to you guys about functions in C a 1155 01:36:50,970 --> 01:36:55,729 Function is basically just a collection of code that performs a 1156 01:36:56,489 --> 01:36:59,448 So what you can do is you can take a bunch of code, you know 1157 01:36:59,449 --> 01:37:05,929 maybe like five or six or twenty lines of code and put it inside 1158 01:37:06,149 --> 01:37:07,579 Code that's inside the function 1159 01:37:07,579 --> 01:37:14,390 You can call the function and generally when you create a function 1160 01:37:14,489 --> 01:37:17,359 So the function will have a specific purpose 1161 01:37:17,789 --> 01:37:22,340 so I'm gonna show you guys how to create functions how to work 1162 01:37:23,750 --> 01:37:26,539 like I said a function is basically just a collection of code 1163 01:37:26,939 --> 01:37:30,439 Does a specific task so we can actually create a function here in 1164 01:37:31,020 --> 01:37:33,680 Now you'll notice up here. We have this block of code 1165 01:37:33,680 --> 01:37:39,170 It says int main and there's an open and closed parenthesis and it 1166 01:37:39,170 --> 01:37:44,658 and we've kind of just been using this for the entire course like 1167 01:37:44,850 --> 01:37:49,610 Just been kind of using this main little block of code here. But 1168 01:37:51,600 --> 01:37:56,959 Sometimes you'll hear people will call this a method as well. So 1169 01:37:57,539 --> 01:38:02,090 Essentially mean the same thing, but you generally in C. We're 1170 01:38:02,220 --> 01:38:05,689 But this is called the main function and the main function is 1171 01:38:06,060 --> 01:38:10,010 Basically a function that's gonna get executed when we start 1172 01:38:10,010 --> 01:38:17,809 And so we've already been using a function this main function, but 1173 01:38:17,810 --> 01:38:18,870 So make sure you guys have to do that 1174 01:38:18,869 --> 01:38:23,239 So let's go down and we're gonna go here outside of this function 1175 01:38:23,239 --> 01:38:27,349 The function is we're outside of this ending curly bracket. So 1176 01:38:27,359 --> 01:38:30,649 I'm going to create a function and when we create a function 1177 01:38:30,649 --> 01:38:34,998 We actually have to give C a couple pieces of information 1178 01:38:35,189 --> 01:38:38,899 The first thing we have to tell C is the return type of the 1179 01:38:39,119 --> 01:38:43,010 And so actually we're going to talk more about return type in the 1180 01:38:43,079 --> 01:38:50,239 But for now just know that return type is basically the type of 1181 01:38:50,239 --> 01:38:56,029 So sometimes your function can actually give information back to 1182 01:38:56,029 --> 01:39:01,670 We're just gonna say void and void basically means that this 1183 01:39:01,770 --> 01:39:07,249 That's the first thing that you have to tell see the second thing 1184 01:39:07,649 --> 01:39:12,828 And generally when we're naming a function, we're gonna want to 1185 01:39:13,439 --> 01:39:17,479 So in our case, we're gonna create a function that says hi to the 1186 01:39:17,520 --> 01:39:21,620 So I'm just gonna call this function say hi because that's what 1187 01:39:22,140 --> 01:39:23,420 Whenever I create a function now 1188 01:39:23,420 --> 01:39:29,509 I want to make an open and closed parenthesis and I'm gonna make 1189 01:39:30,029 --> 01:39:36,710 Code that goes in between these curly brackets. It's going to be 1190 01:39:36,710 --> 01:39:41,599 So let me show you guys I'm just gonna make this a very simple 1191 01:39:41,600 --> 01:39:45,710 We're just gonna make a printf and we're gonna print out hello 1192 01:39:46,229 --> 01:39:52,968 So I've created my function it's called say hi and inside of this 1193 01:39:53,279 --> 01:39:58,219 So now let's go ahead and run our program and see what happens. So 1194 01:39:59,010 --> 01:40:05,449 But you'll see up here that hello user doesn't get printed out 1195 01:40:07,409 --> 01:40:13,279 But this didn't get printed out and this is the first lesson with 1196 01:40:13,560 --> 01:40:15,800 Inside of the function we have to call it 1197 01:40:16,590 --> 01:40:22,429 Calling a function basically means we're telling C that we want to 1198 01:40:22,619 --> 01:40:24,890 So if I want to execute this code I can 1199 01:40:26,010 --> 01:40:30,530 And the way that I do that is just by typing out the name of the 1200 01:40:31,470 --> 01:40:37,880 So now instead of just printing out nothing C is actually going to 1201 01:40:38,189 --> 01:40:43,368 Function so let's run our code and you'll see now we're printing 1202 01:40:43,979 --> 01:40:51,768 So the difference between this main function up here and this say 1203 01:40:53,310 --> 01:40:57,740 this main function basically gets called but then if we want to 1204 01:40:57,930 --> 01:41:03,050 Functions we can call them from inside of this main function and 1205 01:41:03,050 --> 01:41:06,949 So also I want to show you guys one other thing. I just want to 1206 01:41:08,319 --> 01:41:13,899 The flow that these functions take I want to show you guys how 1207 01:41:13,899 --> 01:41:19,298 So I'm printing out top and I'm printing out bottom and I'm 1208 01:41:19,300 --> 01:41:23,349 So now I'm gonna run my program again and you'll see we're 1209 01:41:23,779 --> 01:41:29,259 And then we're printing out hello user then we're printing out 1210 01:41:29,260 --> 01:41:33,460 Lines, but essentially what's happening is when C goes to execute 1211 01:41:33,800 --> 01:41:37,059 It's gonna execute this first line of code printf 1212 01:41:37,059 --> 01:41:41,619 So it's gonna print out top then we're telling C that we want to 1213 01:41:43,279 --> 01:41:50,319 Function C is now gonna jump over here to the say hi function and 1214 01:41:50,319 --> 01:41:55,750 So in this case, we just have one line of code, but I could put 1215 01:41:55,750 --> 01:41:59,828 So if I have like five lines of code, it would execute all five of 1216 01:41:59,899 --> 01:42:05,469 Then it's gonna jump back up here and it's gonna move on to the 1217 01:42:05,469 --> 01:42:08,979 And that's sort of the basics of writing functions anytime 1218 01:42:08,979 --> 01:42:14,678 You have a block of code or a bunch of code that does one thing or 1219 01:42:14,988 --> 01:42:17,738 Encapsulate into its own little container that you can call 1220 01:42:17,929 --> 01:42:22,029 You can put it inside of a function and then whenever you want to 1221 01:42:22,029 --> 01:42:25,988 You just call it, but these functions can actually do a lot more 1222 01:42:25,988 --> 01:42:29,168 So one thing we can do is we can give these functions some 1223 01:42:29,448 --> 01:42:35,768 Information so I can actually give this function a piece of 1224 01:42:35,868 --> 01:42:42,158 This is what's called a parameter and a parameter is basically 1225 01:42:42,158 --> 01:42:45,728 So let's say in this say hi function instead of just saying hi to 1226 01:42:45,729 --> 01:42:48,308 We want it to say hi to someone specifically 1227 01:42:49,179 --> 01:42:53,679 I can actually come down here in these parentheses and I can 1228 01:42:54,139 --> 01:42:59,649 Parameter and I can specify a parameter a lot like I would specify 1229 01:43:00,139 --> 01:43:03,189 name and an open and closed square brackets 1230 01:43:03,189 --> 01:43:08,678 and I'm basically what I'm saying down here is this say hi 1231 01:43:08,960 --> 01:43:11,798 Parameter in other words as an input a string 1232 01:43:13,279 --> 01:43:19,059 called name and what I can do is I can come down here and I can 1233 01:43:19,250 --> 01:43:21,829 Percent s and we could print out name 1234 01:43:23,880 --> 01:43:29,210 Over here when I call this say hi function because I specified 1235 01:43:29,310 --> 01:43:35,779 I have to give it a string so I could give this a string like Mike 1236 01:43:36,238 --> 01:43:38,927 when I call this say hi function now, I'm 1237 01:43:39,448 --> 01:43:42,318 Passing the value Mike into this function 1238 01:43:42,319 --> 01:43:48,500 So Mike is gonna get stored inside of this name array and we're 1239 01:43:48,658 --> 01:43:54,887 Let's go ahead and run our program and you'll see it says hello 1240 01:43:54,889 --> 01:43:56,989 We're able to customize what it says 1241 01:43:57,179 --> 01:44:00,048 The cool thing about this is I could copy this line of code 1242 01:44:00,149 --> 01:44:05,448 Like I could call this function a bunch of times with different 1243 01:44:05,448 --> 01:44:08,748 So this is more obvious so I could say hello Mike. Hello, Tom 1244 01:44:14,100 --> 01:44:17,509 It's saying hello to each of these different people 1245 01:44:18,270 --> 01:44:22,399 because we define the function and we can pass it different names 1246 01:44:22,679 --> 01:44:28,399 Depending on what we pass it depending on the parameter that we 1247 01:44:28,948 --> 01:44:35,568 You can also specify multiple parameters. So let's say in addition 1248 01:44:38,250 --> 01:44:44,298 And then down here I can specify int age. And now we're basically 1249 01:44:44,520 --> 01:44:49,549 So I could say hello and then I could say you are and we'll say 1250 01:44:50,100 --> 01:44:55,129 So now over here instead of an addition to just saying name we can 1251 01:44:57,329 --> 01:45:01,488 Function and when I call it I have to also give this an age so we 1252 01:45:05,639 --> 01:45:12,078 70 and now this is gonna print out all that information for us. So 1253 01:45:12,079 --> 01:45:18,439 Hello, Oscar, you're 70 and you can pass as many parameters as you 1254 01:45:18,750 --> 01:45:22,189 You always just have to make sure though that you're passing a 1255 01:45:22,710 --> 01:45:28,698 When you're calling the function just like that. So that's the 1256 01:45:29,310 --> 01:45:35,640 This guy over here, which is the return type and we're gonna look 1257 01:45:40,609 --> 01:45:44,179 I'll talk to you guys about return statements in C 1258 01:45:44,488 --> 01:45:51,887 Where a turn statement is a special line of code that we can 1259 01:45:52,198 --> 01:45:57,528 back to whoever called them so I can write a function and that can 1260 01:45:57,988 --> 01:46:04,427 information back to whoever called it that could be information 1261 01:46:04,429 --> 01:46:08,838 It could be it a message telling whoever called it how the 1262 01:46:10,559 --> 01:46:15,859 Basically how we can do that and I'll just give you an overview of 1263 01:46:15,859 --> 01:46:19,130 I'm actually going to create a function and we're gonna create a 1264 01:46:19,408 --> 01:46:23,327 Cubes a number so when you cube a number generally you take it to 1265 01:46:23,520 --> 01:46:25,729 So I could say like 2 raised to the third power 1266 01:46:26,340 --> 01:46:31,670 Which is the same as just saying 2 times 2 times 2 this would be 1267 01:46:31,670 --> 01:46:33,670 You're taking it to the third power 1268 01:46:33,809 --> 01:46:35,869 So I'm gonna make a function that's gonna do that 1269 01:46:36,408 --> 01:46:38,339 This function will accept one 1270 01:46:38,340 --> 01:46:44,869 parameter which is gonna be a number and it'll cube that number 1271 01:46:45,090 --> 01:46:49,309 That value back to the caller and you guys will see how that will 1272 01:46:49,309 --> 01:46:52,998 So I do want to point out one thing whenever we are 1273 01:46:53,789 --> 01:46:58,009 Returning values in our function. So if you're gonna write a 1274 01:46:58,408 --> 01:47:02,988 You always want to put it above the function that's going to be 1275 01:47:02,988 --> 01:47:07,488 So for example like this main function down here, this is the 1276 01:47:07,679 --> 01:47:13,248 So any code we put in here is gonna get executed first if I want 1277 01:47:13,250 --> 01:47:19,639 I'm gonna define the function here above the main method and 1278 01:47:19,639 --> 01:47:21,979 It'll just make sure that everything works correctly 1279 01:47:22,078 --> 01:47:28,307 So we want to make sure that we define this function before we 1280 01:47:28,309 --> 01:47:32,599 so the first thing that I need to tell C when I want to create a 1281 01:47:32,849 --> 01:47:37,369 What type of data I want to return if you've been following along 1282 01:47:37,368 --> 01:47:39,368 You'll know in the last tutorial 1283 01:47:39,689 --> 01:47:45,500 We use the void return type which basically means we weren't going 1284 01:47:45,689 --> 01:47:48,108 But now we want to actually return information 1285 01:47:50,770 --> 01:47:55,720 so why don't we return a double and you can put any datatype here 1286 01:47:56,390 --> 01:48:00,400 Character or character array, but let's just do double and we're 1287 01:48:00,399 --> 01:48:06,038 So I'm gonna call this cube and I'm gonna make an open and closed 1288 01:48:06,039 --> 01:48:11,319 We're gonna allow this method or we're gonna allow this function 1289 01:48:11,510 --> 01:48:14,769 So it's just going to be another double and call it num. So 1290 01:48:15,350 --> 01:48:21,460 Basically, what we want to do is we want a cube num and we want to 1291 01:48:21,460 --> 01:48:23,300 Here, I'm just gonna create a variable 1292 01:48:23,300 --> 01:48:27,820 It's just gonna be another double called result and I'm gonna set 1293 01:48:29,180 --> 01:48:34,269 Times num times num. So basically this variable result now 1294 01:48:34,909 --> 01:48:39,099 represents or now stores the cubed value of num 1295 01:48:39,920 --> 01:48:43,059 all we need to do to return this value now is just say 1296 01:48:45,140 --> 01:48:49,510 Result and this return keyword is basically gonna do a couple 1297 01:48:49,510 --> 01:48:54,159 The first thing it does is it breaks us out of the function. So 1298 01:48:54,159 --> 01:49:00,880 It's gonna break us out of the function and it's gonna return this 1299 01:49:01,069 --> 01:49:04,359 So down here we can actually call this function 1300 01:49:04,819 --> 01:49:10,028 so actually what I want to do is I want to print out the answer 1301 01:49:12,949 --> 01:49:15,819 Answer and we'll print out the answer. So percent F 1302 01:49:16,130 --> 01:49:22,838 so I'm using % F because we're expecting to get a double back from 1303 01:49:23,600 --> 01:49:29,559 Over here. I can just say cube and we'll pass in a number. Let's 1304 01:49:30,500 --> 01:49:34,868 Essentially what's gonna happen now is we're going to call this 1305 01:49:34,869 --> 01:49:41,409 All this code is going to execute and this function is going to 1306 01:49:41,689 --> 01:49:47,948 3.0. So normally like in the past in this course what we wanted to 1307 01:49:48,529 --> 01:49:52,000 76 or you know, whatever we'd have to put a number right there 1308 01:49:52,369 --> 01:49:59,498 But we can just call this function because eventually this is 1309 01:49:59,500 --> 01:50:03,270 So this will contain a number after the functions done being 1310 01:50:03,640 --> 01:50:09,179 So I can actually just run my program now and this is going to 1311 01:50:13,569 --> 01:50:20,158 3 cubed is 27 3 times 3 is 9 9 times 3 27 so looks like our cube 1312 01:50:20,159 --> 01:50:22,949 Let's try with another number. Why don't we do 7? 1313 01:50:24,130 --> 01:50:28,829 So let's run this again and we get 343 I'm guessing that's right 1314 01:50:30,100 --> 01:50:38,009 So basically what we did is we created this function and we use 1315 01:50:38,140 --> 01:50:40,798 The value of cubing the number and that's really cool 1316 01:50:40,800 --> 01:50:44,820 And actually we can make this a lot simpler so I could actually 1317 01:50:45,250 --> 01:50:47,878 Result variable and I could just straight up return 1318 01:50:48,520 --> 01:50:54,449 Num cubed and this is and this is gonna do the same exact thing so 1319 01:50:57,390 --> 01:51:02,309 And one thing I want to point out is this return keyword will 1320 01:51:02,310 --> 01:51:07,140 So if I came down here and I said like printf and I printed out 1321 01:51:07,810 --> 01:51:12,719 This actually this code is actually never gonna get executed. So 1322 01:51:13,479 --> 01:51:20,488 You'll notice that it's not printing out here. So that never gets 1323 01:51:21,069 --> 01:51:26,670 Print out here. This never gets touched because when we use this 1324 01:51:27,189 --> 01:51:34,408 This breaks us out of the function. So whenever C sees this return 1325 01:51:34,930 --> 01:51:37,079 down here to the normal program 1326 01:51:37,300 --> 01:51:39,300 So just keep in mind that you can't 1327 01:51:39,399 --> 01:51:45,119 really put any code after you use this return keyword because it 1328 01:51:45,119 --> 01:51:48,028 You guys about one more thing really quick you'll notice up here 1329 01:51:48,029 --> 01:51:54,090 I'm creating my cube function above the main function. So I 1330 01:51:54,090 --> 01:51:59,460 and the reason that I did that was because if I was to move this 1331 01:52:01,270 --> 01:52:06,269 you'll notice that we're getting an error over here and actually 1332 01:52:09,340 --> 01:52:12,029 Conflicting types for cube now 1333 01:52:12,248 --> 01:52:18,599 Essentially what's happening is when I create this function down 1334 01:52:19,059 --> 01:52:23,128 When the main method over here tries to call it. It actually 1335 01:52:25,139 --> 01:52:32,728 like it doesn't necessarily know like what this function is what 1336 01:52:33,010 --> 01:52:38,340 Created it after we created this main method. So what I can do is 1337 01:52:38,859 --> 01:52:44,578 Prototyping and when I prototype it'll allow me to create this 1338 01:52:44,859 --> 01:52:49,828 without getting this error and basically when you prototype I'm 1339 01:52:50,288 --> 01:52:56,728 Function signature, so we would call this like the signature 1340 01:52:56,729 --> 01:52:59,819 So if I was to put this up here and put a semicolon 1341 01:53:02,828 --> 01:53:06,747 You'll see that we're not getting this error anymore, and we're 1342 01:53:06,748 --> 01:53:14,099 So this is a way that you can create functions below that main 1343 01:53:14,099 --> 01:53:18,298 basically any functions that I created I could put another 1344 01:53:23,880 --> 01:53:28,859 In this tutorial and talked to you guys about if statements in C 1345 01:53:29,199 --> 01:53:36,779 Programming structure which we can use to help our programs to 1346 01:53:36,779 --> 01:53:41,309 So in certain circumstances, they can do certain things and in 1347 01:53:41,310 --> 01:53:43,590 They can do other things and if statements are 1348 01:53:43,960 --> 01:53:50,250 Extremely powerful and they're a great way to add some 1349 01:53:50,250 --> 01:53:55,260 We're gonna be building a function. So we're gonna build a 1350 01:53:55,659 --> 01:53:58,648 Basically what this function is gonna do is it's gonna take two 1351 01:53:58,899 --> 01:54:04,949 two numbers and it's gonna tell us which number is the biggest so 1352 01:54:04,949 --> 01:54:11,699 Ten and it'll tell us which number is bigger. Let's go ahead and 1353 01:54:12,550 --> 01:54:19,050 It's going to return an integer and I'm just gonna call it max and 1354 01:54:19,050 --> 01:54:24,059 so it's gonna take an integer num one and it's gonna take another 1355 01:54:24,909 --> 01:54:27,599 Now we'll just make the actual function body 1356 01:54:27,600 --> 01:54:34,079 so inside this function our job is to figure out whether num one 1357 01:54:34,079 --> 01:54:37,109 To return the biggest to the caller 1358 01:54:37,479 --> 01:54:42,389 so the first thing I'm gonna do is create a variable and it's 1359 01:54:42,850 --> 01:54:44,850 I'm not gonna give it a value right away 1360 01:54:45,189 --> 01:54:49,829 What we need to do in this function is we need to figure out which 1361 01:54:49,829 --> 01:54:51,930 we need to figure out if num one is the biggest or 1362 01:54:52,420 --> 01:54:56,640 We need to figure out if num two is the biggest and this is a 1363 01:54:56,829 --> 01:55:01,590 To use an if statement an if statement allows us to check a 1364 01:55:01,590 --> 01:55:08,458 And if that conditions true we can do one thing and if the 1365 01:55:09,449 --> 01:55:14,550 I'm basically just gonna say if and then we're gonna make an open 1366 01:55:16,810 --> 01:55:20,789 Inside of here inside of these parentheses. We need to specify a 1367 01:55:21,340 --> 01:55:27,599 This is something that's either gonna be true or false if the 1368 01:55:27,789 --> 01:55:33,510 We're gonna execute the code inside of these curly brackets. If 1369 01:55:34,840 --> 01:55:41,590 In order to tell whether or not num1 is bigger than num2 or num 2 1370 01:55:43,969 --> 01:55:46,809 Num 1 and I can use this greater than sign 1371 01:55:47,510 --> 01:55:52,390 Num2. I'm basically saying if num 1 is greater than num2 1372 01:55:53,180 --> 01:55:59,409 then I want to do something so down here we can set result equal 1373 01:55:59,899 --> 01:56:03,729 This condition is true. Then we're gonna do this. So only when 1374 01:56:04,340 --> 01:56:11,380 num, 1 is greater than num2 are we gonna set result equal to num 1 1375 01:56:11,380 --> 01:56:14,829 Tell which one is bigger but here's the thing what if num 1 1376 01:56:14,930 --> 01:56:18,069 Greater than num - what if num 2 is greater than num 1? 1377 01:56:18,789 --> 01:56:23,019 I can use something called an else statement so I can come down 1378 01:56:23,119 --> 01:56:25,869 I don't actually need to use an open and close parentheses 1379 01:56:25,869 --> 01:56:27,760 I can just make an open and close curly bracket 1380 01:56:27,760 --> 01:56:34,360 And the code inside of this else block is going to execute if the 1381 01:56:34,579 --> 01:56:40,930 so if this condition is false if num 1 is not greater than num 2 1382 01:56:41,329 --> 01:56:46,988 Set result equal to num 2 and then down here. Finally. We're gonna 1383 01:56:47,689 --> 01:56:50,829 Result so depending on whether or not this is true 1384 01:56:51,619 --> 01:56:56,680 result is either gonna set equal to num 1 or it's gonna be set 1385 01:56:56,899 --> 01:57:03,338 So let's go down here into our main method and why don't we call 1386 01:57:05,060 --> 01:57:07,839 printf and we're gonna be printing out an integer and 1387 01:57:08,719 --> 01:57:14,109 Over here, I'm gonna call this function. So I'm just gonna say Max 1388 01:57:14,109 --> 01:57:16,389 So we're passing a 4 it will pass in a 10 1389 01:57:16,390 --> 01:57:22,930 So I'm passing in a 4 and I'm passing in a 10 and this is going to 1390 01:57:23,420 --> 01:57:29,859 Other words this should return 10. So let's go ahead and run our 1391 01:57:29,859 --> 01:57:35,170 So we got the value of 10 back from this function. Let's try it 1392 01:57:35,170 --> 01:57:38,170 So let's make the first number bigger. So now we have 40 and 10 1393 01:57:38,300 --> 01:57:44,320 So this should give us 40 back awesome so over here we have an 1394 01:57:44,319 --> 01:57:49,210 We're basically checking to see if num 1 is greater than num - if 1395 01:57:51,019 --> 01:57:55,179 Num2 and it's important to note that even if I made these equal 1396 01:57:55,179 --> 01:57:59,949 So if I made it forty and forty, we're still going to get back the 1397 01:57:59,948 --> 01:58:04,657 So it's still gonna give us forty back. So this is the basics of 1398 01:58:04,658 --> 01:58:09,278 We say if we specify a condition if that conditions true 1399 01:58:09,349 --> 01:58:12,458 We're gonna execute this code if that conditions false 1400 01:58:12,590 --> 01:58:14,828 We're gonna execute this code down here 1401 01:58:15,019 --> 01:58:22,088 And that is an extremely powerful structure in our programs and we 1402 01:58:22,238 --> 01:58:27,307 So depending on what the user puts in num1 and num2 we can do 1403 01:58:27,769 --> 01:58:32,739 But this is just sort of the beginning. So why don't we try to 1404 01:58:32,899 --> 01:58:36,939 So down here we're allowing the user to input two parameters 1405 01:58:36,939 --> 01:58:43,178 So we're accepting two parameters into this max function num1 and 1406 01:58:43,578 --> 01:58:48,878 But let's up the ante a little bit what if we wanted to accept 1407 01:58:48,880 --> 01:58:50,920 So instead of just passing in two numbers 1408 01:58:50,960 --> 01:58:57,069 We wanted to be able to pass in three numbers and have the max 1409 01:58:57,069 --> 01:59:04,509 This max function, so I'm just gonna make another parameter. So 1410 01:59:04,510 --> 01:59:09,519 So now this function is accepting three parameters num1 num2 and 1411 01:59:09,889 --> 01:59:16,268 But in order to figure out which one of these is the biggest we're 1412 01:59:16,269 --> 01:59:20,378 So I'm gonna get rid of this and let's just start from scratch. So 1413 01:59:20,380 --> 01:59:23,588 How can we figure out which one of these is the biggest? 1414 01:59:24,019 --> 01:59:28,779 well, what I would recommend is we could say if num 1 is greater 1415 01:59:29,569 --> 01:59:33,698 Num, 1 is greater than num 3 then we know num ones the biggest 1416 01:59:33,698 --> 01:59:35,768 So if num1 is bigger than num2 and it's bigger than um 1417 01:59:35,769 --> 01:59:39,279 3 we know it's the biggest if num2 is greater than num1 and num2 1418 01:59:40,099 --> 01:59:45,998 Is greater than num 3 then we know num2 is the biggest and 1419 01:59:46,130 --> 01:59:51,429 3 is the biggest so I'm gonna show you guys how to do this and 1420 01:59:51,769 --> 01:59:56,588 Concepts for if statement so I'm just gonna say if and I want to 1421 01:59:56,590 --> 01:59:59,288 so I basically want to see if num 1 is greater than 1422 01:59:59,779 --> 02:00:04,849 or equal to num 2 and num threes so down here I can say num one 1423 02:00:05,250 --> 02:00:12,229 Greater than or equal to num2 and now I also want to check to see 1424 02:00:12,600 --> 02:00:17,720 Because if num1 is greater than or equal to num2 and it's greater 1425 02:00:17,729 --> 02:00:24,678 We know num1 is the biggest so I can use this special signal here. 1426 02:00:25,439 --> 02:00:31,638 Basically, this is going to allow us to put another condition in 1427 02:00:33,210 --> 02:00:38,569 basically what this and does is it allows us to put two conditions 1428 02:00:38,699 --> 02:00:45,529 This whole thing is only going to be true if this conditions true 1429 02:00:45,539 --> 02:00:51,109 So if one of these guys is false, then this if statement isn't 1430 02:00:51,109 --> 02:00:54,649 I'm just gonna open this up and if this is the case that we want 1431 02:00:55,170 --> 02:01:00,469 equal to num 1 because if num 1 is greater than or equal to num - 1432 02:01:01,260 --> 02:01:07,159 Num, 1 is also greater than or equal to number 3 then we know num 1433 02:01:07,159 --> 02:01:10,430 We also want to check to see if num 2 is the biggest 1434 02:01:12,420 --> 02:01:18,739 We also want to check to see if num 2 is the biggest now we can 1435 02:01:21,060 --> 02:01:27,320 I can make another open and closed parenthesis and inside of these 1436 02:01:28,079 --> 02:01:35,390 So if this stuff up here at this conditions false then else if is 1437 02:01:36,210 --> 02:01:38,210 So here I want to do basically the same thing 1438 02:01:38,210 --> 02:01:41,719 I'm gonna check to see if num 2 is greater than or equal to num 1 1439 02:01:43,529 --> 02:01:47,420 Num 2 is greater than or equal to num 3 1440 02:01:48,029 --> 02:01:54,050 So if this condition is true if num 2 is greater than or equal to 1441 02:01:54,210 --> 02:02:00,529 Then we know for a fact that result is gonna be num - because 1442 02:02:00,529 --> 02:02:04,130 We can just say else and we can make another to open and close 1443 02:02:04,130 --> 02:02:09,259 And remember this code is going to get executed when neither of 1444 02:02:09,359 --> 02:02:13,248 So here we can just set result equal to num 3 1445 02:02:14,130 --> 02:02:16,819 So let me walk you guys through this one more time up here 1446 02:02:16,819 --> 02:02:21,828 We have this if statement and in addition to checking to see if 1447 02:02:22,140 --> 02:02:29,329 we're also using this special operator called and and we're 1448 02:02:29,430 --> 02:02:33,289 That's the case then result is going to be equal to num 1 1449 02:02:33,989 --> 02:02:36,319 Otherwise if this condition is false 1450 02:02:36,840 --> 02:02:39,380 Then we're gonna come down here and we're going to check this 1451 02:02:39,689 --> 02:02:44,869 So we're gonna check to see if num 2 is bigger than num 1 and if 1452 02:02:45,060 --> 02:02:47,600 3 if that's true, then we're gonna do this 1453 02:02:48,210 --> 02:02:51,319 Finally if neither of these conditions up here is true 1454 02:02:51,359 --> 02:02:58,549 We're just gonna go ahead and set result equal to num 3 and then 1455 02:02:58,909 --> 02:03:04,699 Let's test this out and we're gonna see if it works. So now when 1456 02:03:08,970 --> 02:03:12,859 This is basically going to spit out the biggest one, which should 1457 02:03:12,859 --> 02:03:14,750 Let's run our program and you can see over here 1458 02:03:14,750 --> 02:03:19,369 we get 3 so let's try to make one of the other ones the biggest so 1459 02:03:19,369 --> 02:03:24,649 Biggest and let's run this again and you can see that it returns 1460 02:03:24,960 --> 02:03:31,609 So that's how we can use these ands and we can also use else--if 1461 02:03:31,609 --> 02:03:36,109 so the and is used to check more than one condition inside of the 1462 02:03:36,810 --> 02:03:44,330 Parentheses and the else if is used to check another condition 1463 02:03:44,670 --> 02:03:46,940 Now I want to show you guys a couple other things 1464 02:03:46,939 --> 02:03:50,929 There's a couple other things that we can do with these if 1465 02:03:51,859 --> 02:03:53,859 I'm gonna come down here and just show you guys some of these 1466 02:03:54,000 --> 02:03:57,859 In addition to using that and we can also use something called or 1467 02:03:57,899 --> 02:04:01,009 I'm just gonna type out a simple if statement I could say if 1468 02:04:01,680 --> 02:04:09,619 3 is greater than 2 and instead of using and like this I can use 1469 02:04:09,810 --> 02:04:12,919 Check two conditions, and the whole thing is going to be true 1470 02:04:12,920 --> 02:04:19,670 if only one of those conditions true so I could say if 3 is 1471 02:04:20,220 --> 02:04:25,189 So only one of these is true, right 3 is greater than 2 that's 1472 02:04:25,189 --> 02:04:30,168 but 2 is not greater than 5 so this guy's false, but when we use 1473 02:04:30,699 --> 02:04:38,408 One of these two conditions has to be true for the whole thing to 1474 02:04:41,029 --> 02:04:47,590 Now when I run my program we should print true because we're using 1475 02:04:48,229 --> 02:04:52,750 But if I was to make this guy false, so if I made this 3 less than 1476 02:04:52,750 --> 02:04:56,920 It's not gonna print out true and we can just print out false. So 1477 02:04:57,890 --> 02:05:02,410 Now we're gonna end up renting out false because both of these 1478 02:05:02,960 --> 02:05:09,430 So you can see down here. We're saying false. So the difference 1479 02:05:10,100 --> 02:05:15,850 Both of these conditions have to be true in order for the whole 1480 02:05:16,189 --> 02:05:20,108 When we have or only one of these guys has to be true 1481 02:05:20,329 --> 02:05:24,309 so that's kind of the difference there and I want to show you guys 1482 02:05:24,829 --> 02:05:27,489 Over here we're using like a less than sign 1483 02:05:28,420 --> 02:05:33,878 If 3 is less than 2 I can also use a greater than sign I could use 1484 02:05:34,489 --> 02:05:39,069 Could use also a greater than or equal to sign and if I wanted to 1485 02:05:39,069 --> 02:05:41,859 I could use a double I could use a double equals 1486 02:05:41,859 --> 02:05:48,908 So the double equals will check to see if 3 is equal to 2 we can 1487 02:05:49,430 --> 02:05:54,010 exclamation point an exclamation point basically means not equals 1488 02:05:54,409 --> 02:06:00,368 3 is not equal to 2 then we'll print out true so down here. Let's 1489 02:06:01,430 --> 02:06:06,070 So yeah, there you go. One more thing. I want to show you is how 1490 02:06:06,739 --> 02:06:11,708 Operation. So for example, if I said 3 is greater than 2 this is 1491 02:06:12,369 --> 02:06:16,149 so we'd end up printing out true down here because 3 is greater 1492 02:06:16,430 --> 02:06:18,610 I could surround this whole thing with 1493 02:06:18,829 --> 02:06:26,739 Parentheses and I could put an exclamation point right before this 1494 02:06:26,930 --> 02:06:29,590 Whatever this ends up being so this is true 1495 02:06:29,590 --> 02:06:34,900 And we put this negation operator here this whole thing is going 1496 02:06:35,420 --> 02:06:40,118 Because this is true and we're using the negation operator. We're 1497 02:06:41,779 --> 02:06:43,809 And you can see we just don't print 1498 02:06:44,439 --> 02:06:48,248 But if I put a false condition in here, like if I said three is 1499 02:06:48,470 --> 02:06:53,409 This is false. But because we're negating it now, it's gonna end 1500 02:06:53,409 --> 02:06:56,529 So when I run my program you can see that we get true 1501 02:06:56,529 --> 02:07:01,300 So that's sort of the basics of using if statements and I cover 1502 02:07:01,300 --> 02:07:04,809 We can use ands, we can use all these different comparison 1503 02:07:04,810 --> 02:07:09,130 We can use ORS and we can use this exclamation point to negate 1504 02:07:13,840 --> 02:07:20,289 In this tutorial I'm going to teach you guys how to build a 1505 02:07:20,289 --> 02:07:25,689 You'll know that in the beginning of the course. We actually 1506 02:07:26,170 --> 02:07:32,350 We let the user input two numbers and we took those numbers we 1507 02:07:33,640 --> 02:07:38,109 I'm gonna take some of the stuff that we've learned since then and 1508 02:07:38,329 --> 02:07:40,329 functional for function calculator 1509 02:07:40,329 --> 02:07:42,260 So this calculator will be able to add 1510 02:07:42,260 --> 02:07:47,679 Subtract multiply and divide and we're gonna let the user decide 1511 02:07:47,680 --> 02:07:50,109 So we'll let them decide if they want to add or subtract or 1512 02:07:50,180 --> 02:07:55,030 it's gonna be pretty cool and we're gonna end up using some of the 1513 02:07:55,130 --> 02:08:01,150 statements we're also gonna use like getting input from users and 1514 02:08:01,430 --> 02:08:07,059 Input from a user as well. So down here. We want to start making 1515 02:08:07,060 --> 02:08:09,459 What I'm gonna do is I'm gonna ask the user to enter a number 1516 02:08:09,800 --> 02:08:15,309 Then I'm gonna ask them to enter an operator like plus minus 1517 02:08:15,310 --> 02:08:17,740 we're gonna ask them to enter in a third number and then we'll 1518 02:08:18,260 --> 02:08:25,989 Depending on what operator they specify so addition and 1519 02:08:26,119 --> 02:08:28,119 So the first thing I want to do is create 1520 02:08:28,399 --> 02:08:35,049 Variables where we can store the numbers and the operator so I'm 1521 02:08:35,199 --> 02:08:38,739 Num 1 and then we'll make another double call it num2 1522 02:08:39,949 --> 02:08:45,608 finally, we'll make a variable that will store the operator that 1523 02:08:46,909 --> 02:08:50,949 Operator. All right. So now we want to actually get input from the 1524 02:08:50,949 --> 02:08:56,648 I want to figure out what numbers they want to use and then what 1525 02:08:56,649 --> 02:08:59,229 So down here. Why don't we get some input? 1526 02:08:59,229 --> 02:09:04,059 The first thing I'm going to do is I'm just gonna print out a 1527 02:09:05,659 --> 02:09:08,739 And now what we want to do is we want to be able to 1528 02:09:09,140 --> 02:09:14,109 Take the number they give us and store it inside of one of those 1529 02:09:14,109 --> 02:09:16,109 So I'm just gonna use this scanf function 1530 02:09:16,939 --> 02:09:21,219 Scanf and in here I'm gonna specify that we want to get a double 1531 02:09:21,220 --> 02:09:25,628 So remember when we're using scanf if we want to scan for a double 1532 02:09:25,960 --> 02:09:32,529 F and L F is going to scan for a double and now I'm going to use a 1533 02:09:32,779 --> 02:09:34,448 Variable that we want to store in here 1534 02:09:34,448 --> 02:09:38,259 so I'm gonna use this ampersand and I'm just gonna say num 1 1535 02:09:38,260 --> 02:09:43,659 So this is exactly what we need to do to be able to get the users 1536 02:09:44,090 --> 02:09:49,989 The next thing we want to do is get the operator. So I'm gonna 1537 02:09:52,630 --> 02:09:58,390 They'll be entering in plus sign minus sign asterisks or forward 1538 02:09:58,390 --> 02:10:02,048 and again now we're going to scan for a 1539 02:10:02,390 --> 02:10:07,959 Character now when we're scanning for a character and scanf and 1540 02:10:08,119 --> 02:10:14,769 We want to do something special. So I want to put a % and a C. But 1541 02:10:14,770 --> 02:10:21,819 I want to put a space so whenever we're getting a character from 1542 02:10:21,819 --> 02:10:28,599 Right here before % c otherwise, it's not gonna work. And again we 1543 02:10:31,010 --> 02:10:36,550 One more time we're gonna get another number. So I'm just gonna 1544 02:10:37,670 --> 02:10:39,819 We're gonna get this one for num2 1545 02:10:40,880 --> 02:10:45,699 So now we should have all of the input for our program. So I'm 1546 02:10:45,698 --> 02:10:51,398 I'm getting the operator and I'm getting the second number. The 1547 02:10:51,560 --> 02:10:56,289 So we're gonna have to figure out which operator they wanted to 1548 02:10:56,289 --> 02:11:03,069 Right, so we have this Opie variable and this is storing like a 1549 02:11:03,350 --> 02:11:05,919 So depending on what that's storing we want to print something 1550 02:11:06,529 --> 02:11:12,369 Different out so we can actually use an if statement to do this so 1551 02:11:12,859 --> 02:11:16,778 What operator is inside of our operator variable and depending on 1552 02:11:18,020 --> 02:11:23,859 Operation so I can say if and the first thing I'm going to do is 1553 02:11:26,329 --> 02:11:31,359 If the operator that the user entered is equal to a plus sign then 1554 02:11:31,460 --> 02:11:36,819 So I'm just gonna print out these two numbers and I'll just say 1555 02:11:37,340 --> 02:11:41,710 Because we're gonna add both of them together. We can also use an 1556 02:11:41,710 --> 02:11:43,539 So I'm going to want to check a few other conditions 1557 02:11:43,539 --> 02:11:46,329 I'm going to check to see if the operator is equal to a 1558 02:11:47,600 --> 02:11:53,559 Sign and if the operator is equal to a minus sign then instead of 1559 02:11:53,659 --> 02:11:56,559 I'll just copy this and we'll paste this guy down here 1560 02:11:57,109 --> 02:11:59,588 So it's going to be num1 - num2 1561 02:12:00,500 --> 02:12:07,298 And I can actually just copy this whole thing and we'll paste it 1562 02:12:07,300 --> 02:12:12,250 We'll paste this other elsif and here we're going to check to see 1563 02:12:12,250 --> 02:12:16,088 So we'll check for a forward slash. And again, we're just going to 1564 02:12:16,699 --> 02:12:19,479 Decimal number so it's going to be num1 divided by num2 1565 02:12:20,420 --> 02:12:25,449 Finally, we're gonna check for multiplication. So if it's 1566 02:12:25,880 --> 02:12:31,539 So now we're checking for each of the operators. We're checking 1567 02:12:32,029 --> 02:12:36,698 But here's the thing what happens if the user enters in an 1568 02:12:36,699 --> 02:12:39,760 so for example if they don't enter in any of these operators 1569 02:12:39,800 --> 02:12:43,930 We're gonna want to tell them so I'm gonna have this like print 1570 02:12:44,000 --> 02:12:48,789 We're just gonna say else and basically the code inside this else 1571 02:12:48,789 --> 02:12:55,930 If none of these conditions up here true and down here we can just 1572 02:12:57,890 --> 02:13:05,499 So now we have our if statement or if block setup and this should 1573 02:13:06,109 --> 02:13:09,699 Alright, so let's go ahead and run this program so you can see 1574 02:13:09,699 --> 02:13:13,868 It says enter a number so we can enter a number. Let's enter in 1575 02:13:15,229 --> 02:13:19,509 Enter an operator we'll add numbers and now let's enter in 4.0 1576 02:13:19,510 --> 02:13:21,760 So we're gonna add five point nine and four 1577 02:13:21,949 --> 02:13:25,029 so when I click enter we should go through all of those if 1578 02:13:25,189 --> 02:13:30,878 Statements and figure out what we need to do and you'll see over 1579 02:13:30,880 --> 02:13:36,489 And we're getting nine point nine. So looks like the program 1580 02:13:36,489 --> 02:13:39,489 Why don't we try to multiply some numbers? So let's multiply 1581 02:13:42,949 --> 02:13:48,220 Times I don't know five point seven so we get thirty four point 1582 02:13:48,770 --> 02:13:53,829 All right, and then one more time we'll run this and I want to try 1583 02:13:53,828 --> 02:13:58,868 So we'll say like 5.7 and I'm just gonna enter in a G. So that's 1584 02:13:58,868 --> 02:14:05,259 That's not going to work and I'll enter my number. Let's do 8 and 1585 02:14:06,038 --> 02:14:10,418 we have a four function calculator this calculator can multiply 1586 02:14:10,788 --> 02:14:13,868 Add and if you don't enter in a correct operator 1587 02:14:13,868 --> 02:14:17,738 It's smart enough to yell at you and tell you that you have an 1588 02:14:18,319 --> 02:14:20,558 This seems to work pretty well. I'm pretty happy with it 1589 02:14:20,559 --> 02:14:25,929 And you can see down here like this is a perfect situation for 1590 02:14:25,929 --> 02:14:29,588 We have this variable opie, right? It's storing some sort of 1591 02:14:29,748 --> 02:14:32,498 We don't necessarily know what's inside of there 1592 02:14:32,498 --> 02:14:36,698 But I can use if statements and I can respond to the different 1593 02:14:37,130 --> 02:14:43,210 So in the situation where it's a plus sign I can respond in the 1594 02:14:43,210 --> 02:14:49,448 And this just makes our programs a lot smarter and it helps us to 1595 02:14:53,719 --> 02:14:58,458 In this tutorial I want to talk to you guys about using switch 1596 02:14:58,619 --> 02:15:00,979 now a switch statement is basically a 1597 02:15:01,229 --> 02:15:07,369 Special type of if statement which will allow us to compare one 1598 02:15:07,590 --> 02:15:10,400 And it's essentially doing the same thing as an if statement 1599 02:15:10,399 --> 02:15:14,778 It's allowing us to check different conditions and if different 1600 02:15:14,779 --> 02:15:20,539 Then we can do certain things but a switch statement makes it 1601 02:15:21,060 --> 02:15:24,080 specific value to a bunch of other values now 1602 02:15:24,079 --> 02:15:28,309 I'm gonna show you guys how we can use that in this tutorial to 1603 02:15:28,350 --> 02:15:33,949 So I'm gonna build a little program that will take a letter grade 1604 02:15:33,948 --> 02:15:36,888 It will tell you whether or not you did good or bad on a test 1605 02:15:37,020 --> 02:15:40,100 So if you got an a on the test it would say hey you did a good 1606 02:15:40,229 --> 02:15:42,769 If you got a B on its house, it'd be like hey you did 1607 02:15:42,770 --> 02:15:46,040 Alright, if you got an F on the test to tell you that you failed 1608 02:15:46,219 --> 02:15:52,668 so we're basically gonna have a variable where we'll store a grade 1609 02:15:52,859 --> 02:15:56,960 Respond to that grade using a switch statement. So it's gonna be 1610 02:15:56,960 --> 02:16:02,149 the first thing I'm gonna do is create a variable and it's just 1611 02:16:05,189 --> 02:16:10,068 So we're gonna say that whoever took this test got an a on the 1612 02:16:10,529 --> 02:16:14,448 Now what I want to do is I want to create a switch statement. So 1613 02:16:15,539 --> 02:16:19,909 Switch and I'm gonna make an open and closed parenthesis and 1614 02:16:19,909 --> 02:16:25,488 I'm gonna put grade so I'm just gonna put grade and I'm gonna make 1615 02:16:25,500 --> 02:16:31,458 So this is sort of like the basic structure for our switch 1616 02:16:31,460 --> 02:16:38,929 I'm gonna take this variable here that I pass into the parentheses 1617 02:16:38,929 --> 02:16:41,688 So I'm gonna take this I'm gonna see if it's equal to one value 1618 02:16:41,689 --> 02:16:45,318 then I'm gonna see if it's equal to a different value or a 1619 02:16:45,569 --> 02:16:51,888 Depending on the value that it ends up being equal to we're gonna 1620 02:16:52,138 --> 02:16:54,588 cases so down here I can say case and 1621 02:16:57,659 --> 02:17:01,878 Character so for example, I could put a right here and then I'm 1622 02:17:02,790 --> 02:17:09,339 and when I put a right here, basically what this is saying is the 1623 02:17:09,920 --> 02:17:14,950 then I'm gonna put some code down here that I want to execute so I 1624 02:17:15,769 --> 02:17:18,338 I could say like you did great 1625 02:17:19,340 --> 02:17:21,429 because they did great on the test and 1626 02:17:22,670 --> 02:17:28,750 what this is saying is in the case that the grade is equal to a 1627 02:17:29,799 --> 02:17:33,429 Which is called break and I'm gonna explain what break does in a 1628 02:17:33,829 --> 02:17:37,779 So I could make one of these little case statements for every 1629 02:17:37,779 --> 02:17:43,149 so for every possible grade that we could get so I'm actually just 1630 02:17:46,189 --> 02:17:52,328 So now I can put a B in here. So we're saying in the case that the 1631 02:17:52,329 --> 02:17:54,909 I'm gonna say you did. All right, and 1632 02:17:56,179 --> 02:17:59,709 Then we're also going to break and down here. I'm gonna make 1633 02:17:59,709 --> 02:18:05,619 So I'll say case and this time we'll make it C in the case that 1634 02:18:08,269 --> 02:18:12,908 There's a couple more that we can check so we can check if they 1635 02:18:16,488 --> 02:18:20,468 I forgot to put a break statement here and I'm gonna put one down 1636 02:18:20,468 --> 02:18:24,307 And finally, we're just gonna do one more for if they failed the 1637 02:18:24,308 --> 02:18:26,308 So I'm just gonna say in the case that they got an F 1638 02:18:31,760 --> 02:18:35,500 Essentially, what I'm doing here is I'm writing out all of these 1639 02:18:35,500 --> 02:18:40,120 So I'm saying in the case that you got an F. I'm gonna print out 1640 02:18:40,120 --> 02:18:41,649 I'm gonna print out you did very bad 1641 02:18:41,648 --> 02:18:48,429 And then we also have this break statement here and this break 1642 02:18:48,709 --> 02:18:53,199 So whenever we're executing this switch statement whenever C is 1643 02:18:53,388 --> 02:18:57,158 It's checking to see if grade is equal to each one of these cases 1644 02:18:57,439 --> 02:19:01,778 So for example, let's say the grade is equal to a it's gonna 1645 02:19:02,000 --> 02:19:07,659 But if I don't put this break statement in here, then it's just 1646 02:19:08,029 --> 02:19:13,238 So it's always important to put a break statement in there because 1647 02:19:13,238 --> 02:19:18,338 I don't want to be in here anymore. So finally we can check one 1648 02:19:19,179 --> 02:19:24,759 Or that we get an invalid grade. So for example, if someone 1649 02:19:25,340 --> 02:19:29,829 G or something that's not a valid grade so I can use something 1650 02:19:30,200 --> 02:19:33,759 Default is basically like an else so default is going to execute 1651 02:19:34,218 --> 02:19:38,648 whenever none of these conditions up here or Matt so then I can 1652 02:19:39,860 --> 02:19:45,489 Invalid grade or something and this will basically be like, hey, 1653 02:19:45,888 --> 02:19:51,098 So let's take a look at this switch statement and we'll just kind 1654 02:19:51,799 --> 02:19:59,679 Basically, I'm passing this in a variable or a value or something 1655 02:19:59,680 --> 02:20:01,399 all of these different conditions 1656 02:20:04,610 --> 02:20:09,668 We're gonna print you did great and the case that B is equal to 1657 02:20:09,709 --> 02:20:16,449 and this is basically doing what an if statement doing so I can 1658 02:20:16,760 --> 02:20:20,770 An if statement I can write out if etc else--if else--if else--if 1659 02:20:21,290 --> 02:20:22,540 But this just makes it a lot easier 1660 02:20:22,540 --> 02:20:25,870 So I don't have to constantly write out all of that if statement 1661 02:20:26,090 --> 02:20:31,719 So hopefully we did everything right and let's actually check and 1662 02:20:32,569 --> 02:20:37,119 So grade is equal to a let's run our program and see what our 1663 02:20:37,370 --> 02:20:42,789 It says hey, you did great because we got an A if I made this an F 1664 02:20:43,399 --> 02:20:45,520 Now it should tell us that we failed 1665 02:20:46,340 --> 02:20:49,989 So it says you failed if I made this a C 1666 02:20:50,690 --> 02:20:54,699 It'll tell us that we did poorly and it says you did poorly 1667 02:20:54,700 --> 02:21:00,759 So this is a super easy way for us to check one single value 1668 02:21:00,760 --> 02:21:05,860 And that's really the point of a switch statement is you're 1669 02:21:06,500 --> 02:21:10,479 Against a bunch of other values and that's where it can be 1670 02:21:11,299 --> 02:21:13,299 So consider using the switch statements 1671 02:21:13,299 --> 02:21:20,349 They're not appropriate in every situation and there's I'd say 1672 02:21:20,629 --> 02:21:25,509 But when they are appropriate switch statements will save you a 1673 02:21:30,500 --> 02:21:37,849 I want to talk to you guys about strux in C now a struct is a data 1674 02:21:40,250 --> 02:21:44,299 so inside of a struct I could store like an integer alongside of 1675 02:21:44,639 --> 02:21:50,930 String alongside a character alongside a double I could sort all 1676 02:21:51,209 --> 02:21:57,500 Structure and there's tons of uses for structs and one of the 1677 02:21:57,930 --> 02:21:59,930 So I could basically like a model 1678 02:22:00,120 --> 02:22:04,729 Something in the real world inside of my program and I'm gonna 1679 02:22:05,189 --> 02:22:10,998 We're gonna be looking at how we can use a struct to represent a 1680 02:22:11,100 --> 02:22:18,229 So imagine we were writing a piece of software that was using like 1681 02:22:18,228 --> 02:22:24,468 I could use a struct to represent a student in my program so over 1682 02:22:24,469 --> 02:22:29,929 I'm just gonna come up here above the main method and I'm actually 1683 02:22:29,930 --> 02:22:31,939 and how we can use them so I'm just gonna say 1684 02:22:35,100 --> 02:22:37,729 Over here, I'm gonna give this a name so I'm gonna call this 1685 02:22:38,370 --> 02:22:42,499 Student and a lot of times in C when we're making structs. You're 1686 02:22:43,020 --> 02:22:47,329 Capital letter just like that and I'm gonna make an open and 1687 02:22:47,879 --> 02:22:54,978 Inside of this struct I can start specifying what types of data 1688 02:22:55,109 --> 02:23:00,439 So basically I can define like different attributes of a student 1689 02:23:00,439 --> 02:23:03,439 And this will kind of act as a template and you'll see how we can 1690 02:23:04,228 --> 02:23:07,038 So let's think about different attributes of a student 1691 02:23:07,040 --> 02:23:13,969 Well one thing would be like the student's name so I could say 1692 02:23:14,520 --> 02:23:20,420 so this is gonna represent the name and actually why don't we give 1693 02:23:21,209 --> 02:23:26,959 String can hold 50 characters maximum and now we're gonna make 1694 02:23:27,090 --> 02:23:30,920 So we're gonna want to store the students major and again, we'll 1695 02:23:30,920 --> 02:23:35,238 I also want to make an integer for the students age and 1696 02:23:39,209 --> 02:23:42,048 So I have this struct student and in here 1697 02:23:42,049 --> 02:23:46,420 I have a bunch of after buttes of a student right have like the 1698 02:23:47,000 --> 02:23:50,680 An integer that stores their age and a double for their GPA 1699 02:23:51,290 --> 02:23:55,509 Essentially what I did was I created like a type of student data 1700 02:23:55,549 --> 02:24:00,969 So I basically am allowing myself now to represent a student 1701 02:24:00,970 --> 02:24:04,930 so let's come down here to this main method and I'll show you guys 1702 02:24:05,329 --> 02:24:10,780 Create an instance of this student structure so I can create like 1703 02:24:11,299 --> 02:24:14,199 Inside of my program and the way that I do that is just by saying 1704 02:24:14,780 --> 02:24:20,199 Struct and I want to type out the name of the struct that I want 1705 02:24:20,200 --> 02:24:25,780 It's gonna be a student and I want to give this a name. So I'm 1706 02:24:26,329 --> 02:24:28,748 I can just use a semicolon here 1707 02:24:29,149 --> 02:24:35,859 basically, what I did now was I created a container called student 1708 02:24:36,470 --> 02:24:40,000 Name a major in age and a GPA 1709 02:24:40,521 --> 02:24:46,870 So if you're familiar with arrays in C, you'll know an array is a 1710 02:24:47,209 --> 02:24:53,649 But all the pieces of information inside of an array needs to be 1711 02:24:54,440 --> 02:25:01,390 Struct I can have a bunch of different data types like this and I 1712 02:25:01,700 --> 02:25:04,930 So now let me show you guys how we can assign some values to 1713 02:25:05,030 --> 02:25:09,280 So for this particular student student 1 I can give them a name 1714 02:25:09,280 --> 02:25:13,539 I can give them a major a GPA and an age so I could say 1715 02:25:14,601 --> 02:25:22,540 student 1 dot age and I can set this equal to something so I could 1716 02:25:22,640 --> 02:25:29,409 Inside of this student 1 container I'm saying the age of this 1717 02:25:29,409 --> 02:25:35,828 I can do the same for the GPA so I can say student GPA is equal to 1718 02:25:36,079 --> 02:25:42,850 So I'm saying this particular student's GPA is a 3.2. I can also 1719 02:25:42,851 --> 02:25:47,409 So for the name and the major now, here's the thing about working 1720 02:25:47,720 --> 02:25:54,058 Remember in C a string is actually just an array of characters and 1721 02:25:54,059 --> 02:25:56,158 We can't like give it a particular value 1722 02:25:56,158 --> 02:26:02,308 So for example, if I wanted to give this students name of value, 1723 02:26:02,619 --> 02:26:08,459 So I can't just come down here and say student name is equal to 1724 02:26:08,559 --> 02:26:11,760 That's not gonna work because you can't do that with an array 1725 02:26:11,760 --> 02:26:17,760 I can use something called the string copy function and this is a 1726 02:26:18,248 --> 02:26:24,418 String and it'll give it a value that we specify so it'll 1727 02:26:24,850 --> 02:26:28,828 with the age and the GPA but with the name so I could say like 1728 02:26:29,408 --> 02:26:34,048 Str. C py and inside of here. I need to pass this two parameters 1729 02:26:34,148 --> 02:26:38,308 The first thing I want to pass is the destination for the string. 1730 02:26:38,949 --> 02:26:42,389 student 1 dot name the second thing I want to pass in is the 1731 02:26:43,119 --> 02:26:49,259 String that I want to store inside of student 1 name. So in our 1732 02:26:50,439 --> 02:26:56,818 So now we have the student 1 names value is equal to Jim and I can 1733 02:26:56,818 --> 02:27:03,568 so I'm just gonna copy this and we'll come down here and I could 1734 02:27:04,539 --> 02:27:06,659 Business, so let's say Jim is a business major 1735 02:27:07,510 --> 02:27:13,859 Essentially what I've done here is I've created a student and that 1736 02:27:13,859 --> 02:27:18,327 so this particular student had a name a major in age and a GPA 1737 02:27:20,939 --> 02:27:26,459 So I assigned this student 1 an age of 20 to a GPA of 3.2 etc 1738 02:27:26,738 --> 02:27:32,488 So now what I can actually do is I can print out all these 1739 02:27:32,488 --> 02:27:34,488 so if I wanted for example, I could like 1740 02:27:34,869 --> 02:27:37,229 Print out the GPA so I could print out 1741 02:27:38,289 --> 02:27:41,729 Student 1 GPA and now we're gonna be printing out 1742 02:27:42,488 --> 02:27:46,948 3.2. And actually it looks like I have a typo here. This should be 1743 02:27:48,639 --> 02:27:53,340 Alright so here we're printing out 3.2. So we're printing out the 1744 02:27:53,340 --> 02:28:00,449 I can also print out like their name. So why don't we do that 1745 02:28:01,978 --> 02:28:03,840 So you see we prints out over there 1746 02:28:03,840 --> 02:28:06,558 So a struct is a really useful structure 1747 02:28:06,559 --> 02:28:09,889 And another cool thing we can do with structs is we could actually 1748 02:28:10,109 --> 02:28:16,879 Student so I could create like another instance of that student 1749 02:28:16,879 --> 02:28:20,088 I'll show you how we can do this. I'll just come down here and 1750 02:28:20,728 --> 02:28:27,769 Instead of student 1 why don't we call this one student 2 and we 1751 02:28:31,228 --> 02:28:34,397 Let's say their name is Pam and they're studying art 1752 02:28:35,639 --> 02:28:38,599 So now I have a completely different student 1753 02:28:38,600 --> 02:28:42,620 And if I wanted I could print out this students attributes so I 1754 02:28:43,020 --> 02:28:48,439 Dot name and now we're gonna get Pam instead of Jim so you can see 1755 02:28:48,898 --> 02:28:52,728 So I could create as many of these students as I want and this is 1756 02:28:53,459 --> 02:28:59,750 Structs is I can just define the basic template for a student in 1757 02:28:59,909 --> 02:29:05,088 Individual students down here that I can work with. So now I have 1758 02:29:05,090 --> 02:29:07,939 I could do whatever I want with it. I could pass it into a 1759 02:29:07,939 --> 02:29:09,889 I could print it out onto the screen 1760 02:29:09,889 --> 02:29:14,510 I could use it in something like an if statement I could do 1761 02:29:14,510 --> 02:29:17,599 It's it acts a lot like a variable or an array 1762 02:29:17,600 --> 02:29:19,500 so remember variables and arrays or just 1763 02:29:19,500 --> 02:29:24,228 Containers we can do just about anything we want with them and 1764 02:29:24,228 --> 02:29:26,388 so this has just been kind of an introduction and 1765 02:29:26,520 --> 02:29:31,069 What you could do as an exercise is think of other things you 1766 02:29:31,068 --> 02:29:36,019 So maybe something like a book or something like a phone you could 1767 02:29:38,609 --> 02:29:42,049 Information you could store using a struct just like this 1768 02:29:46,110 --> 02:29:49,980 In this tutorial I want to talk to you guys about while loops in 1769 02:29:50,139 --> 02:29:56,760 now a while loop is basically a structure in the C programming 1770 02:29:57,250 --> 02:30:02,309 Continually execute a specific block of code until a certain 1771 02:30:02,590 --> 02:30:09,090 So I can specify a condition and then I can basically just loop 1772 02:30:12,120 --> 02:30:14,789 there's a lot of times in our programs or we're gonna want to let 1773 02:30:14,979 --> 02:30:19,138 Continually do something while something else is true or while a 1774 02:30:19,450 --> 02:30:25,079 Is met and I'm gonna show you guys a really basic while loop in 1775 02:30:25,079 --> 02:30:29,429 We're actually gonna build a really cool guessing game using while 1776 02:30:29,430 --> 02:30:32,069 And you should get a pretty good idea of like how these things 1777 02:30:34,271 --> 02:30:38,550 And this isn't directly related to creating a while loop though is 1778 02:30:38,549 --> 02:30:42,599 So I'm just gonna create an integer called index and I'm gonna set 1779 02:30:43,239 --> 02:30:47,699 What I want to do is I want to actually create a while loop and 1780 02:30:47,860 --> 02:30:49,350 So in order to create a while loop 1781 02:30:49,350 --> 02:30:54,630 I can just type out while and open and close parentheses and then 1782 02:30:54,629 --> 02:30:59,098 And this is basically like the outline sort of like the template 1783 02:30:59,200 --> 02:31:02,369 Basically how this is gonna work is up here in these parentheses 1784 02:31:02,530 --> 02:31:07,498 I'm gonna specify a condition and as long as that condition is 1785 02:31:07,629 --> 02:31:12,119 I'm gonna loop through all of the code inside of these curly 1786 02:31:12,120 --> 02:31:17,969 So inside of these curly brackets, I'm gonna put a bunch of code. 1787 02:31:17,969 --> 02:31:19,969 I'm gonna execute all that code so 1788 02:31:20,290 --> 02:31:26,790 Up here. We're going to specify a condition. I'm gonna specify a 1789 02:31:27,760 --> 02:31:34,979 5 so while index is less than or equal to 5, I want to execute all 1790 02:31:35,079 --> 02:31:39,329 So I'm gonna put some code in here. The first thing I'm going to 1791 02:31:42,149 --> 02:31:45,718 I'm just gonna say we're gonna print out an integer and 1792 02:31:46,060 --> 02:31:51,869 I'm just gonna print out index so we'll be printing out index and 1793 02:31:54,729 --> 02:32:02,059 Finally after this. I want to increment the index variable so I 1794 02:32:02,611 --> 02:32:08,600 Basically, I'm adding one onto the index every time and actually I 1795 02:32:09,931 --> 02:32:15,801 Situations where you're gonna want to do something like this 1796 02:32:15,800 --> 02:32:20,450 there's actually a shortcut we can use so I can come over here and 1797 02:32:20,550 --> 02:32:24,081 This is gonna do the same thing. So this is just gonna add one to 1798 02:32:24,661 --> 02:32:32,000 Essentially, what I'm doing is I'm saying I want to print out 1799 02:32:32,001 --> 02:32:36,081 Through this code while index is less than or equal to five 1800 02:32:36,450 --> 02:32:40,700 So let's go ahead and run this program and maybe you can predict 1801 02:32:41,550 --> 02:32:46,161 You'll see over here. I'm printing out the numbers one through 1802 02:32:46,620 --> 02:32:48,650 So I'm printing out one two three 1803 02:32:48,650 --> 02:32:53,840 Four and five and let me walk you guys through exactly what's 1804 02:32:53,841 --> 02:32:56,809 we create this index variable and we set it equal to one and 1805 02:32:57,331 --> 02:33:01,790 Basically when C goes to execute this program the first thing it's 1806 02:33:01,790 --> 02:33:08,209 does anything else is it's gonna look at this condition and it's 1807 02:33:08,210 --> 02:33:14,331 Or equal to five. Well, the first time through index is equal to 1808 02:33:14,331 --> 02:33:16,850 It's gonna execute this line of code printing out one 1809 02:33:16,979 --> 02:33:20,510 Then it's gonna execute this line of code incrementing index 1810 02:33:20,700 --> 02:33:26,179 Then what C's gonna do is it's gonna come all the way back up here 1811 02:33:26,341 --> 02:33:30,291 so it's gonna say okay is index less than or equal to five and 1812 02:33:30,691 --> 02:33:33,650 Index at this point is equal to two so it's less than five 1813 02:33:33,841 --> 02:33:39,621 so it's gonna go through execute this line of code execute this 1814 02:33:39,751 --> 02:33:44,300 C's gonna go all the way back up to the top it's gonna check this 1815 02:33:44,300 --> 02:33:47,240 So before C goes through this loop 1816 02:33:47,490 --> 02:33:54,260 It's always gonna it's always gonna check the condition first. So 1817 02:33:54,261 --> 02:33:56,600 It has to check the condition to make sure that it's true 1818 02:33:57,361 --> 02:34:02,240 Eventually, we're gonna get to a situation where index gets 1819 02:34:02,521 --> 02:34:08,541 then C's gonna come back up here and it's gonna check to see if 6 1820 02:34:08,540 --> 02:34:13,819 False and we're gonna move with our lives in the program and 1821 02:34:13,829 --> 02:34:18,798 So this seems very simple and that's because it is all we do is 1822 02:34:18,799 --> 02:34:20,329 As long as that condition is true 1823 02:34:20,329 --> 02:34:26,659 We keep doing all this stuff in here and there's tons of 1824 02:34:26,670 --> 02:34:30,139 This is just one of them where we can print out like numbers 1825 02:34:30,140 --> 02:34:32,140 I also do want to point out one thing 1826 02:34:32,310 --> 02:34:37,459 That you might want to watch out for which is called an infinite 1827 02:34:37,709 --> 02:34:41,418 Where this condition up here never becomes false 1828 02:34:41,940 --> 02:34:48,078 So this condition will just always stay true and there are certain 1829 02:34:48,149 --> 02:34:52,699 But in a lot of circumstances, you're not gonna want to go 1830 02:34:52,700 --> 02:34:59,480 So let me just demonstrate so you guys can see so if I stopped 1831 02:34:59,790 --> 02:35:04,370 Now when I run my program you'll see that it's just continually 1832 02:35:04,370 --> 02:35:09,140 I mean look at all these ones that are getting printed out you can 1833 02:35:09,390 --> 02:35:14,119 This program is just it keeps executing through that loop and it's 1834 02:35:14,120 --> 02:35:20,360 So this would keep going forever if I let it keep going 1835 02:35:20,360 --> 02:35:24,918 but that'll kind of show you like how an infinite loop works and 1836 02:35:25,079 --> 02:35:30,770 you'll get into situations as you work with while loops where 1837 02:35:30,771 --> 02:35:37,610 That especially if you're doing something a little bit more 1838 02:35:38,549 --> 02:35:42,799 Slow your computer down quite a bit. So this is a while loop and 1839 02:35:42,799 --> 02:35:48,109 there's also one other type of loop that's similar to a while loop 1840 02:35:48,271 --> 02:35:51,739 Before I do that, I just want to do something really quick. So I'm 1841 02:35:54,470 --> 02:36:00,529 I'm gonna set this equal to 6 and I'm gonna run my program and 1842 02:36:00,530 --> 02:36:02,659 so the code inside of this while loop 1843 02:36:02,909 --> 02:36:08,780 Isn't gonna print out because the first thing we always do when we 1844 02:36:09,060 --> 02:36:16,220 So the first thing C's gonna do before it prints anything before 1845 02:36:16,220 --> 02:36:17,930 It's not gonna do any of this stuff 1846 02:36:17,931 --> 02:36:23,220 So when I run this program, you'll see nothing's gonna get printed 1847 02:36:23,469 --> 02:36:26,969 There's another type of a loop that's similar to a while loop 1848 02:36:27,040 --> 02:36:30,059 Which is called a do-while loop and I'm going to show you how to 1849 02:36:30,059 --> 02:36:36,000 All you have to do is take this wild thing up here and paste it 1850 02:36:37,959 --> 02:36:40,918 A do-while loop is similar to a while loop 1851 02:36:40,920 --> 02:36:47,549 But the first thing a do-while loop does instead of checking this 1852 02:36:47,859 --> 02:36:50,189 So even though index is equal to 6 1853 02:36:50,318 --> 02:36:54,088 we're still gonna print out 6 and we're still going to increment 1854 02:36:54,459 --> 02:36:57,659 Then we're gonna check the condition to see if we can keep 1855 02:36:57,879 --> 02:37:03,358 So let me show you guys this and this should illustrate what this 1856 02:37:05,459 --> 02:37:12,268 We're printing out 6 so unlike before when we just use that while 1857 02:37:12,270 --> 02:37:20,069 We're doing whatever was inside of that loop before we're checking 1858 02:37:20,350 --> 02:37:27,090 Come in handy. I would say by far while loops are way more common, 1859 02:37:27,090 --> 02:37:29,818 Loops because there are certain circumstances where they'll come 1860 02:37:30,129 --> 02:37:35,459 Basically any time you don't want to be checking the condition 1861 02:37:35,728 --> 02:37:40,349 But those are the two basic types and there's actually another 1862 02:37:40,350 --> 02:37:42,450 Which is called a for loop, but for now 1863 02:37:42,450 --> 02:37:46,499 We're gonna have some fun with while loops and in the next 1864 02:37:51,600 --> 02:37:55,049 I'm gonna teach you guys how to build a little guessing game and 1865 02:37:55,180 --> 02:38:00,449 This is gonna be pretty cool because we're gonna use a lot of the 1866 02:38:00,449 --> 02:38:03,837 In order to build it, so let's get started basically 1867 02:38:03,838 --> 02:38:07,949 This guessing game is going to allow the user to guess a secret 1868 02:38:08,020 --> 02:38:11,819 so we're gonna define a secret number and we're gonna give the 1869 02:38:12,158 --> 02:38:17,877 opportunity to try and guess it so if they'll try to guess it if 1870 02:38:17,879 --> 02:38:18,930 They don't get it right then 1871 02:38:20,039 --> 02:38:25,439 Right, so we'll basically let them keep guessing what the secret 1872 02:38:25,439 --> 02:38:27,538 We want to create a couple variables 1873 02:38:27,728 --> 02:38:31,707 The first variable I want to create is gonna be an integer and 1874 02:38:31,709 --> 02:38:37,468 So I'm just gonna call it secret number and why don't we just set 1875 02:38:37,468 --> 02:38:40,588 We want to create is gonna store the user's guess 1876 02:38:40,588 --> 02:38:46,679 so this is also gonna be an integer and I'm just gonna call it 1877 02:38:46,930 --> 02:38:50,129 Right off the bat. The user wouldn't have guessed a number 1878 02:38:50,799 --> 02:38:56,758 So what we want to do is we want to be able to prompt the user to 1879 02:38:56,759 --> 02:39:00,509 Right. So as long as the user hasn't entered in the correct 1880 02:39:00,578 --> 02:39:06,238 We want to keep asking them to enter it in in order to do that in 1881 02:39:06,398 --> 02:39:09,657 Enter it until they've guessed it and we can use something called 1882 02:39:09,658 --> 02:39:15,627 So I'm just gonna type while and when making open and close 1883 02:39:15,908 --> 02:39:20,337 Let's think about a good condition for our game, basically 1884 02:39:20,338 --> 02:39:25,918 We want to keep asking the user to enter an input as long as they 1885 02:39:26,168 --> 02:39:31,738 So it's right there we could keep looping as long as the guess is 1886 02:39:34,439 --> 02:39:37,229 and basically this is always going to be true until 1887 02:39:37,658 --> 02:39:41,279 They guess the correct number now inside of here 1888 02:39:41,279 --> 02:39:46,167 we want to prompt them to enter their guests and then we want to 1889 02:39:46,389 --> 02:39:52,798 Store it inside of our guest variable. So let's do this first 1890 02:39:59,289 --> 02:40:04,019 Scanf in order to get whatever number they enter and I'm gonna 1891 02:40:04,659 --> 02:40:09,808 inside of that guest variable so I'm just gonna say at guess and 1892 02:40:09,939 --> 02:40:14,459 Or whatever integer they enter and it's gonna store it inside of 1893 02:40:14,530 --> 02:40:18,269 So basically what's happening in this loop is every time through 1894 02:40:18,700 --> 02:40:24,209 getting a different number and then we're gonna come up here and 1895 02:40:24,250 --> 02:40:27,929 It's not then we're gonna go through again and we're gonna keep 1896 02:40:28,090 --> 02:40:34,019 But if the guest is equal to the secret number, then we'll break 1897 02:40:34,239 --> 02:40:38,609 So I'm just gonna print out a success message and I'll basically 1898 02:40:40,299 --> 02:40:44,039 All right, awesome so you can see this isn't actually that many 1899 02:40:44,040 --> 02:40:49,889 I mean, it's probably like ten lines of code to write out this 1900 02:40:49,889 --> 02:40:54,239 So this should actually work properly. So let's try to run this 1901 02:40:54,239 --> 02:40:56,398 So remember a secret number is five 1902 02:40:57,790 --> 02:41:02,639 So I'm gonna run the program it says enter a number. So let's just 1903 02:41:05,680 --> 02:41:09,840 three so you'll notice I can keep entering in numbers and 1904 02:41:10,389 --> 02:41:14,639 It'll keep prompting me to enter a number as long as I haven't 1905 02:41:16,319 --> 02:41:23,218 And remember five is the secret number when I click enter now that 1906 02:41:23,219 --> 02:41:30,058 Because the guess is gonna be equal to the secret number and it's 1907 02:41:30,059 --> 02:41:34,559 So we broke out of that loop we executed the print statement that 1908 02:41:34,930 --> 02:41:37,829 Terminated and that is a really cool little game 1909 02:41:37,829 --> 02:41:39,829 so one of the cool things about this game and 1910 02:41:39,850 --> 02:41:44,579 It's different from programs that we've written in this course in 1911 02:41:45,370 --> 02:41:48,450 Essentially infinitely until we enter in that correct guessed 1912 02:41:48,450 --> 02:41:53,099 So it's gonna keep going and keep going keep asking us to do 1913 02:41:53,620 --> 02:41:57,059 Until we entered correctly. So this is pretty cool now 1914 02:41:57,059 --> 02:41:59,519 This is a pretty nice game and it's it's pretty simple 1915 02:41:59,520 --> 02:42:04,380 But I want to maybe make it a little more complex one of the 1916 02:42:04,389 --> 02:42:08,039 Is that the user gets unlimited guesses, right? 1917 02:42:08,040 --> 02:42:13,410 So basically like they can just guess every single number and 1918 02:42:13,719 --> 02:42:16,799 So why don't we do this? We should impose a guess limit 1919 02:42:16,909 --> 02:42:22,968 So why don't we say like the user can only guess three times and 1920 02:42:23,129 --> 02:42:28,308 Then they'll lose the game. So let's think about how we can go 1921 02:42:28,889 --> 02:42:32,269 Into this little app, I'm gonna walk you guys through how we can 1922 02:42:32,270 --> 02:42:35,210 The first thing we're gonna need to do is we're gonna need to 1923 02:42:35,850 --> 02:42:41,149 variables I'm going to create another variable up here and I'm 1924 02:42:41,489 --> 02:42:44,628 Guess count is basically going to tell us how many times 1925 02:42:44,909 --> 02:42:50,418 the user has tried to guess the secret number and I'm just gonna 1926 02:42:50,850 --> 02:42:53,540 Initially, the user will have guessed zero times 1927 02:42:53,909 --> 02:42:58,668 so remember this guest count variable is basically gonna get 1928 02:42:59,040 --> 02:43:03,799 Right, so I'm gonna come down here. Every time we go through this 1929 02:43:04,110 --> 02:43:08,599 So every time we go through this loop, I'm gonna increment the 1930 02:43:09,239 --> 02:43:13,369 Guest count plus plus and that means every time the user guesses 1931 02:43:13,370 --> 02:43:19,399 The guest count will get incremented and we'll have a accurate 1932 02:43:20,450 --> 02:43:22,490 the second variable that I want to make is 1933 02:43:22,920 --> 02:43:29,689 Going to be another integer and I'm just gonna call it guess limit 1934 02:43:29,909 --> 02:43:34,398 Can guess so this will basically determine like how many guesses 1935 02:43:34,500 --> 02:43:36,139 So why don't we set this equal to three? 1936 02:43:36,139 --> 02:43:40,969 I think three guesses is a pretty good limit and finally we want 1937 02:43:41,728 --> 02:43:45,647 variable and this is also gonna be an integer and I'm just gonna 1938 02:43:46,590 --> 02:43:50,058 guesses and I'm gonna set out of guesses equal to 1939 02:43:50,489 --> 02:43:52,908 zero and out of guesses will basically 1940 02:43:53,100 --> 02:43:58,819 Tell us whether or not the user has any more guesses and this 1941 02:43:59,549 --> 02:44:04,579 But keep following with the video and you'll see how this comes 1942 02:44:04,579 --> 02:44:09,409 So we're actually gonna have to modify our code a little bit so 1943 02:44:09,409 --> 02:44:13,998 So the user can keep guessing as long as the guess isn't equal to 1944 02:44:14,189 --> 02:44:18,379 But we want to be able to limit the amount of times that the user 1945 02:44:18,379 --> 02:44:22,398 I'm going to create an if statement and every time we go through 1946 02:44:22,398 --> 02:44:28,009 I want to check and see if the user has any more guesses left. I 1947 02:44:28,010 --> 02:44:32,939 I want to see if they've reached their guessed limit so I'm gonna 1948 02:44:39,520 --> 02:44:42,418 Long as the guest count is less than the guests limit 1949 02:44:42,418 --> 02:44:46,558 That means they haven't guessed as many times as the limit 1950 02:44:46,959 --> 02:44:50,999 Basically, they can guess again, right so down here inside of this 1951 02:44:50,998 --> 02:44:55,167 I'm gonna put all of this code and I'm just gonna surround this 1952 02:44:55,898 --> 02:45:02,728 so basically the user can only guess the number this code will 1953 02:45:03,010 --> 02:45:06,898 Guest count is less than the guessed limit when the user has 1954 02:45:07,238 --> 02:45:11,609 Additional guesses right when they haven't guessed as many times 1955 02:45:12,520 --> 02:45:16,560 Otherwise though, so if they have guessed too many times 1956 02:45:16,770 --> 02:45:20,399 Right if the guest count is either equal to or greater than the 1957 02:45:20,398 --> 02:45:25,737 I'm actually gonna set this out of guesses variable equal to one 1958 02:45:29,439 --> 02:45:33,778 Basically what this is going to tell us is if out of guesses is 1959 02:45:33,908 --> 02:45:37,378 Then that'll tell us that the user is out of guesses. So 1960 02:45:37,898 --> 02:45:40,978 Essentially if out of guesses has a value that's more than zero 1961 02:45:40,978 --> 02:45:43,378 we'll know that they've run out of guesses and 1962 02:45:43,568 --> 02:45:49,228 If you ever use any other programming languages before this is 1963 02:45:49,299 --> 02:45:54,958 It's basically storing like a true or a false value, which we're 1964 02:45:55,689 --> 02:46:00,059 So if the guest counts less than the guessed limit they can guess 1965 02:46:00,219 --> 02:46:06,629 Otherwise we're gonna say out of guesses is equal to one. In other 1966 02:46:06,818 --> 02:46:10,859 There's one more thing we have to do though is we have to come up 1967 02:46:10,988 --> 02:46:17,638 Condition because right now we're gonna keep looping through this 1968 02:46:18,309 --> 02:46:21,748 But there's actually two situations where we want to break out of 1969 02:46:21,748 --> 02:46:24,478 The first is when they've guessed the number correctly 1970 02:46:24,639 --> 02:46:27,389 Right, then. We want to come down here and print out you win 1971 02:46:27,760 --> 02:46:31,738 But we also want to break out of the loop when they've run out of 1972 02:46:31,930 --> 02:46:35,158 So I want to check to see whether or not they're out of guesses 1973 02:46:35,158 --> 02:46:37,529 so I'm gonna say guess is not equal to secret number and 1974 02:46:43,960 --> 02:46:46,979 So as long as out of guesses is equal to zero 1975 02:46:46,979 --> 02:46:51,090 That means that they're not out of guesses and they still have 1976 02:46:51,190 --> 02:46:57,180 But if the guest counts less than the guessed limit and out of 1977 02:46:57,581 --> 02:47:02,639 Then out of guesses isn't going to be equal to zero anymore. So 1978 02:47:02,640 --> 02:47:06,390 So basically, there's two scenarios now that will break us out of 1979 02:47:06,390 --> 02:47:08,761 They get the word right or they run out of guesses 1980 02:47:08,761 --> 02:47:13,950 So one more thing we have to do in this program is come down here 1981 02:47:13,950 --> 02:47:18,180 We're always printing out you win. So no matter what like no 1982 02:47:19,271 --> 02:47:25,171 We're printing out you win and that's because before if they made 1983 02:47:25,331 --> 02:47:27,809 That means that they guessed the secret number correctly 1984 02:47:28,960 --> 02:47:30,760 There's actually two scenarios 1985 02:47:30,761 --> 02:47:36,030 Down here where they would have broken out of the loop where there 1986 02:47:36,159 --> 02:47:40,200 So I want to actually check to see which one we're dealing with. 1987 02:47:40,751 --> 02:47:46,710 Of guesses and I'm gonna check to see if it's equal to one and if 1988 02:47:47,470 --> 02:47:51,120 That means that they lost and they ran out of guesses. So I'm just 1989 02:47:54,280 --> 02:47:57,840 otherwise though, it means that they won so I'm gonna say 1990 02:47:58,421 --> 02:48:01,530 You win and this will basically be like hey you won the game 1991 02:48:06,030 --> 02:48:09,930 I know that and let's just run this see how we did 1992 02:48:10,200 --> 02:48:14,909 And we'll see if our games working properly and then I'll kind of 1993 02:48:15,130 --> 02:48:20,699 And actually you know what? I just noticed the typo down here. I 1994 02:48:20,700 --> 02:48:25,740 This is just have to be a single equals. So we're assigning this 1995 02:48:25,739 --> 02:48:29,639 I had two equals that needs to be one. So let's go over here 1996 02:48:29,640 --> 02:48:33,779 We'll run our program and it says enter a number. So why don't we 1997 02:48:33,780 --> 02:48:39,570 I'm just gonna say two two and now we're on our third and final 1998 02:48:39,790 --> 02:48:41,100 That means we're gonna lose the game 1999 02:48:41,101 --> 02:48:44,730 So I'm gonna enter and you'll see it says out of guesses 2000 02:48:44,729 --> 02:48:49,559 So we weren't able to guess it within our three tries and the game 2001 02:48:49,720 --> 02:48:53,309 Let's run it one more time. We'll try to win so I'll say like two 2002 02:48:53,310 --> 02:48:55,050 We're on our third and final guess 2003 02:48:55,050 --> 02:48:56,189 So if I don't get it this time 2004 02:48:56,190 --> 02:49:01,318 we're going to I'm gonna guess of five and we guessed right so 2005 02:49:01,658 --> 02:49:04,798 Basically how this little program works and I'm gonna walk you 2006 02:49:04,799 --> 02:49:07,590 so we have all these variables up here secret number is 2007 02:49:07,988 --> 02:49:12,988 Just storing the number they need to guess the guest keeps track 2008 02:49:13,270 --> 02:49:16,319 The guest count is gonna tell us how many times they've guessed 2009 02:49:16,898 --> 02:49:20,519 guess limit is gonna tell us how many times they can guess and 2010 02:49:20,828 --> 02:49:26,429 Out of guesses is going to tell us whether or not the user is out 2011 02:49:26,430 --> 02:49:31,918 We're checking two conditions for this while loop. We're checking 2012 02:49:32,439 --> 02:49:37,139 We're gonna keep looping as long as out of guesses is equal to 2013 02:49:37,270 --> 02:49:40,229 We check to see if the guest count is less than the guessed limit 2014 02:49:40,228 --> 02:49:44,487 If it is then we ask them to guess we increment the guest count 2015 02:49:45,010 --> 02:49:50,849 Otherwise we say out of guesses is equal to one and that's gonna 2016 02:49:51,879 --> 02:49:56,759 the first situation is that they ran out of guesses the second 2017 02:49:56,859 --> 02:49:59,818 We're using this if statement to check both of those 2018 02:49:59,818 --> 02:50:06,778 so that's basically how this works and this kind of shows you how 2019 02:50:07,119 --> 02:50:10,019 variables together in like one single program 2020 02:50:14,050 --> 02:50:18,159 In this tutorial I'm going to teach you guys how to use for loops 2021 02:50:18,229 --> 02:50:24,549 now a for loop is a special type of loop that we can use in C 2022 02:50:24,800 --> 02:50:28,899 indexing variable and this indexing variable will basically tell 2023 02:50:29,300 --> 02:50:35,050 iteration of the loop we're currently on and we can use that 2024 02:50:35,239 --> 02:50:42,369 Loop through an array of items we could do all sorts of stuff 2025 02:50:42,440 --> 02:50:44,529 You'll see that I have a while loop 2026 02:50:44,601 --> 02:50:49,329 Set up and I have just sort of a basic while loop and I want to 2027 02:50:49,940 --> 02:50:53,949 Essentially up here I'm saying int I is equal to 1 so I'm giving 2028 02:50:53,950 --> 02:50:58,900 I the value of 1 and I'm saying wow I is less than or equal to 5 2029 02:50:59,421 --> 02:51:05,380 I'm gonna print out the value of I and then I'm gonna increment I 2030 02:51:05,511 --> 02:51:10,180 basically, we have this variable I and every time we go through 2031 02:51:10,700 --> 02:51:14,800 Greater than 5 so let's run this program and we'll see what it 2032 02:51:15,530 --> 02:51:23,020 So you'll see over here. We're basically printing out values 2033 02:51:23,021 --> 02:51:26,801 so the first time that we go through this while loop we're 2034 02:51:27,021 --> 02:51:33,431 The second time we're printing out to the third time. We're 2035 02:51:34,190 --> 02:51:37,720 Basically telling us what iteration of the loop are currently on 2036 02:51:38,060 --> 02:51:40,300 so on the first iteration of the loop I is 2037 02:51:40,521 --> 02:51:45,341 telling us that we're on the first iteration of the loop, right 2038 02:51:45,921 --> 02:51:47,171 Second time we go through the loop 2039 02:51:47,171 --> 02:51:49,930 I is equal to 2 third time I is equal to 3 2040 02:51:50,329 --> 02:51:55,959 So this variable I over here is basically telling us how many 2041 02:51:55,960 --> 02:52:00,460 So on the third time, it's telling us 3 fourth time for etc, and 2042 02:52:00,460 --> 02:52:05,079 This is actually a very useful thing for us to have when we're 2043 02:52:05,079 --> 02:52:11,079 It's there's a lot of situations where you're gonna want to know 2044 02:52:11,511 --> 02:52:17,950 when you're looping so with a normal while loop like I can 2045 02:52:19,360 --> 02:52:21,131 You can kind of just do whatever you want with it 2046 02:52:21,130 --> 02:52:27,939 but in a lot of situations and there's tons of these situations 2047 02:52:28,309 --> 02:52:32,379 That will tell us what iteration of the loop that we're currently 2048 02:52:32,379 --> 02:52:37,718 Basically just a variable that's gonna keep changing every time we 2049 02:52:38,450 --> 02:52:46,149 situation in see that there's actually something called a for loop 2050 02:52:46,459 --> 02:52:53,228 Condense it into its own single loop. So it's taking this 2051 02:52:53,840 --> 02:52:57,068 It's basically allowing us to do it a lot easier and a lot 2052 02:52:57,068 --> 02:52:59,378 So I'm gonna show you guys how we can create a for loop 2053 02:53:00,079 --> 02:53:04,388 I'm gonna come down here below this while loop and I'm gonna 2054 02:53:04,389 --> 02:53:09,398 So I'm just gonna say for I'm gonna make an open and closed 2055 02:53:10,068 --> 02:53:12,668 the difference between a for loop in a while loop is 2056 02:53:13,370 --> 02:53:20,229 Basically gonna happen inside of these parentheses. So in the 2057 02:53:20,228 --> 02:53:25,958 Right. This is specifying whether or not we can keep looping 2058 02:53:25,959 --> 02:53:29,768 We're gonna have three different things. So instead of just one 2059 02:53:29,770 --> 02:53:33,640 We're actually gonna have three different things that we want to 2060 02:53:34,340 --> 02:53:37,210 with a for loop the first thing I'm gonna put in here is 2061 02:53:37,639 --> 02:53:40,568 This variable I so you'll notice in the while loop 2062 02:53:40,568 --> 02:53:43,208 we have our variable I up here and this is basically 2063 02:53:43,609 --> 02:53:48,309 Allowing us to loop through and keep track of how many times we've 2064 02:53:48,350 --> 02:53:53,470 So what I can do down here is I can do something similar I could 2065 02:53:54,969 --> 02:54:01,478 I'm gonna say I is equal to 1 so basically I'm gonna take my 2066 02:54:01,700 --> 02:54:08,409 Of 1 and now I have my I variable I have my variable that's gonna 2067 02:54:08,780 --> 02:54:13,389 The next thing I want to do is include my looping condition so up 2068 02:54:13,389 --> 02:54:18,099 We have I as less than or equal to 5, that's our condition. I can 2069 02:54:18,100 --> 02:54:20,470 So I'm gonna say I is less than or equal to 5 2070 02:54:21,049 --> 02:54:25,568 The third thing I want to do is increment I so you'll notice down 2071 02:54:25,568 --> 02:54:33,189 Every time we go through this loop, we're incrementing that 2072 02:54:33,829 --> 02:54:38,048 So you'll notice I have these little like sections. Here's the 2073 02:54:38,049 --> 02:54:41,858 We're initializing the very I were saying I is equal to one 2074 02:54:42,408 --> 02:54:45,368 Here we're specifying our looping conditions 2075 02:54:45,369 --> 02:54:48,969 so i'm saying we're gonna keep looping while I is less than or 2076 02:54:49,189 --> 02:54:54,309 Over here is a little line of code. That's gonna get executed. 2077 02:54:54,439 --> 02:55:00,129 So here I'm saying I plus plus I could also say like I minus minus 2078 02:55:00,129 --> 02:55:03,818 I I could say like I is equal to I plus two 2079 02:55:04,369 --> 02:55:09,639 Etc. Like I could do basically anything I wanted over here. Let's 2080 02:55:09,639 --> 02:55:17,170 so you'll see I was basically able to take all of this code and 2081 02:55:17,170 --> 02:55:22,209 Instead of having to like print this out and create this variable 2082 02:55:22,818 --> 02:55:27,247 Inside of this for loop now, I can basically take this line of 2083 02:55:27,248 --> 02:55:31,449 I can paste it down into here and we essentially have the same 2084 02:55:31,449 --> 02:55:37,088 So right now this block of code and this block of code are 100% 2085 02:55:37,090 --> 02:55:39,130 They're doing exactly the same thing 2086 02:55:39,260 --> 02:55:44,648 So let's go ahead and get rid of all this code and let's test out 2087 02:55:44,809 --> 02:55:50,648 So I'm just gonna run my program and you'll see over here. We're 2088 02:55:50,898 --> 02:55:55,567 We're printing out one two, three, four five. So it's the same 2089 02:55:56,090 --> 02:56:01,359 doing the same exact thing and that's why for loops are great 2090 02:56:01,459 --> 02:56:05,739 Structure like where we have an indexing variable and we can use 2091 02:56:05,988 --> 02:56:09,878 so I want to show you another situation where these four loops can 2092 02:56:10,100 --> 02:56:14,979 We can use them to loop through all the elements inside of an 2093 02:56:14,978 --> 02:56:20,769 I have this array that I created. It's called lucky numbers and 2094 02:56:21,828 --> 02:56:26,109 We have this lucky numbers array. It has 4 8 15 16 23 42 and 2095 02:56:26,930 --> 02:56:31,658 What I could actually do is I could loop through all of the 2096 02:56:32,299 --> 02:56:35,920 From this for loop so I'm gonna show you guys how we can do that 2097 02:56:35,920 --> 02:56:37,719 Let's first off see how many elements we have 2098 02:56:37,719 --> 02:56:43,090 we have 1 2 3 4 5 6 so we have 6 elements in this an array so 2099 02:56:43,789 --> 02:56:48,759 what I'm gonna do is I'm actually gonna say I is equal to 0 and 2100 02:56:48,760 --> 02:56:51,969 Second but basically array indexes start at 0 2101 02:56:52,039 --> 02:56:55,088 So this first element in the array is at index position 2102 02:56:55,158 --> 02:57:00,968 zero and just for a little refresher if I wanted to access one of 2103 02:57:02,959 --> 02:57:07,269 Was zero and this is going to give me access to this element 2104 02:57:07,270 --> 02:57:12,069 If I said lucky numbers two, then I'll get access to this element 2105 02:57:12,260 --> 02:57:16,179 So this is basically how we can access an element inside the 2106 02:57:16,430 --> 02:57:22,869 so I'm gonna set I equal to zero and I'm gonna say I want to loop 2107 02:57:23,719 --> 02:57:26,648 Six was how how many elements we had in the array? 2108 02:57:26,648 --> 02:57:30,189 So I want to keep looping as long as we're less than six and I'm 2109 02:57:30,709 --> 02:57:37,599 Now down here. I'm gonna do this same exact thing but instead of 2110 02:57:40,398 --> 02:57:45,939 So I'm gonna print out the array element at index position. I and 2111 02:57:46,219 --> 02:57:48,908 Basically the first time we go through this loop 2112 02:57:48,978 --> 02:57:51,337 We're gonna be printing out lucky numbers zero 2113 02:57:51,588 --> 02:57:55,209 Because I is going to be equal to zero the second time we go 2114 02:57:55,279 --> 02:58:00,069 We're gonna be printing out lucky numbers one because I is gonna 2115 02:58:00,068 --> 02:58:01,908 And we're gonna keep doing that 2116 02:58:01,908 --> 02:58:06,877 Until we get all the way up to five which is gonna be the last 2117 02:58:07,158 --> 02:58:10,718 So let's go ahead and do that. So I need to put this back to I 2118 02:58:11,238 --> 02:58:13,689 Let's run this program. So you'll see over here 2119 02:58:14,238 --> 02:58:19,029 We're basically doing exactly what I said. So the first time 2120 02:58:19,039 --> 02:58:24,009 We're printing out that first element the second time through the 2121 02:58:24,738 --> 02:58:31,957 15 16 23 and 40 - so we're printing out all of those elements in 2122 02:58:32,418 --> 02:58:35,289 There's a lot of situations where for loops are gonna come in 2123 02:58:35,289 --> 02:58:37,628 but this is a very very very very 2124 02:58:37,969 --> 02:58:42,429 common situation where we want to loop through all the elements in 2125 02:58:42,680 --> 02:58:44,439 You know do something to them, whatever 2126 02:58:44,439 --> 02:58:47,979 so that's the basics of working with for loops and I do just want 2127 02:58:48,168 --> 02:58:51,459 Anything that you do with a for loop you could do with a while 2128 02:58:51,459 --> 02:58:56,979 I basically showed you guys how we transform that while loop into 2129 02:58:57,588 --> 02:59:03,968 Convenient so it makes it really easy to do something like this 2130 02:59:07,799 --> 02:59:15,538 In this tutorial I'm gonna talk to you guys about two topics in C 2131 02:59:16,180 --> 02:59:23,488 Two-dimensional array is basically a situation where we have an 2132 02:59:24,219 --> 02:59:27,658 The second thing I want to talk to you guys about is nested. 2133 02:59:27,658 --> 02:59:31,859 So we're gonna look at how we can use a looping structure where we 2134 02:59:32,379 --> 02:59:34,078 Inside of loops. It's gonna be pretty cool 2135 02:59:34,078 --> 02:59:40,859 and actually these two topics can go together really well and I'm 2136 02:59:41,350 --> 02:59:45,328 Two-dimensional arrays in order to make an awesome program. So 2137 02:59:45,328 --> 02:59:47,078 the first thing I want to show you guys is 2138 02:59:47,078 --> 02:59:49,168 two-dimensional arrays so down here 2139 02:59:49,170 --> 02:59:55,289 we can create a 2d array and actually the concepts that I'm 2140 02:59:56,408 --> 03:00:02,099 Multi-dimensional arrays so not only two dimensions but three four 2141 03:00:02,199 --> 03:00:05,969 So in order to create a two-dimensional array, I'm just gonna make 2142 03:00:05,978 --> 03:00:10,679 So I'm just gonna say int and we'll give this a name. So why don't 2143 03:00:12,279 --> 03:00:17,158 Normally when we create an array after we'd say the name of the 2144 03:00:17,500 --> 03:00:19,350 But when we create a two-dimensional array 2145 03:00:19,350 --> 03:00:27,328 we're gonna make two open and closed square brackets just like 2146 03:00:27,699 --> 03:00:31,769 Width and the height of our array so you guys will see what I mean 2147 03:00:31,770 --> 03:00:33,609 but basically we're gonna have like 2148 03:00:33,609 --> 03:00:38,729 Elements in the array and then each of those elements is gonna be 2149 03:00:38,728 --> 03:00:44,787 So these two squares will allow us to like manipulate all that 2150 03:00:45,578 --> 03:00:50,609 An open and closed curly bracket now, this is normally how we 2151 03:00:50,609 --> 03:00:55,139 So normally I would just say like 1 2 3 4 whatever I could put all 2152 03:00:55,389 --> 03:01:02,219 But with a two-dimensional array all of the elements are gonna be 2153 03:01:05,680 --> 03:01:13,289 Arrays inside of here. So for example, I'll put an array right 2154 03:01:13,930 --> 03:01:19,979 so for example the first element in this array, I could just make 2155 03:01:20,809 --> 03:01:22,809 So you can see here. I have two elements 2156 03:01:23,850 --> 03:01:30,829 Inside of this array which is itself the first element of the nums 2157 03:01:30,829 --> 03:01:35,718 We'll come down here and we'll make another one and this keeps 2158 03:01:36,510 --> 03:01:38,540 So now we're gonna say three four 2159 03:01:39,000 --> 03:01:44,898 So this thing right here this whole array, that's the first 2160 03:01:44,898 --> 03:01:49,849 Right and that array has two elements inside of it. Same thing 2161 03:01:49,850 --> 03:01:55,068 This is the second element in the numbers array and it has two 2162 03:02:00,540 --> 03:02:02,870 So this is going to be five six 2163 03:02:03,568 --> 03:02:07,668 So now we have three array elements right one 2164 03:02:09,389 --> 03:02:13,369 Three and each of them has two elements inside of them 2165 03:02:13,370 --> 03:02:16,609 So whenever we create a two dimensional array like this 2166 03:02:16,609 --> 03:02:22,369 We always have to specify the number of elements and then the 2167 03:02:22,370 --> 03:02:26,990 so in our case, we're going to have one two three elements in the 2168 03:02:27,540 --> 03:02:30,799 Each array has two elements inside of it 2169 03:02:30,799 --> 03:02:36,409 So it's gonna look like that so we would say three and then two 2170 03:02:36,409 --> 03:02:41,628 So now let's talk about accessing these elements. So I'm gonna 2171 03:02:42,180 --> 03:02:44,809 We're gonna print out some of this stuff. So I'm gonna 2172 03:02:45,510 --> 03:02:49,579 Say % D and I'm gonna show you guys how we can access individual 2173 03:02:49,579 --> 03:02:55,789 So basically I'm gonna say nums and let's say that I wanted to 2174 03:02:56,068 --> 03:02:59,509 the first thing I want to do is specify the index where 2175 03:03:00,540 --> 03:03:02,749 The value that I want to access is stored 2176 03:03:02,750 --> 03:03:08,088 So like this would be index position 0 this would be index 2177 03:03:10,469 --> 03:03:13,219 then I want to specify the index position of the 2178 03:03:13,469 --> 03:03:20,750 individual element inside of 0 so I could say like this is element 2179 03:03:21,090 --> 03:03:25,040 So I print out nums 0 0 and this is gonna print out that 2180 03:03:25,559 --> 03:03:29,389 Numbers coming out. It's gonna print out that one for us. You can 2181 03:03:29,939 --> 03:03:35,779 So let's try a different one. Let's try to grab this for right 2182 03:03:36,959 --> 03:03:43,338 One and then one so this would be at 1 1 and now we should get 2183 03:03:46,340 --> 03:03:51,469 so that's basically how we can access elements inside of these 2184 03:03:51,750 --> 03:03:54,139 if I didn't want to give this an initial value 2185 03:03:54,140 --> 03:03:57,319 I could just like put a semicolon here and I could just like 2186 03:03:57,690 --> 03:04:03,140 Manually define each index location so I could say like 0 0 is 2187 03:04:03,750 --> 03:04:05,750 like I don't have to give it a 2188 03:04:06,569 --> 03:04:10,369 Value like right up front although in our case. Let's just do that 2189 03:04:11,010 --> 03:04:15,319 All right, so we have our numbers array and we figured out how we 2190 03:04:15,601 --> 03:04:17,989 So now I want to talk to you guys about another 2191 03:04:18,181 --> 03:04:22,850 Concept which is called a nested for loop and you guys will see in 2192 03:04:24,840 --> 03:04:28,250 but a nested for loop is a situation where we have a for loop and 2193 03:04:28,920 --> 03:04:30,920 inside of that for loop we have 2194 03:04:31,079 --> 03:04:35,270 Another loop so I'm gonna show you guys this really quick. Let's 2195 03:04:35,399 --> 03:04:39,468 I'm gonna create two variables over here in I and int J and 2196 03:04:39,931 --> 03:04:42,681 I don't know if I showed you guys this in the course yet 2197 03:04:42,681 --> 03:04:45,319 But if I want to just like declare two variables 2198 03:04:45,690 --> 03:04:52,699 I can just say I comma J and that will declare both the variables. 2199 03:04:52,700 --> 03:04:54,700 We're gonna do that inside these four loops 2200 03:04:54,840 --> 03:05:00,049 So I want to show you guys how we can use a nested for loop in 2201 03:05:00,299 --> 03:05:03,920 inside of this two-dimensional array, so I'm gonna say for I 2202 03:05:06,299 --> 03:05:09,649 and we're gonna keep looping as long as I is less than 2203 03:05:10,229 --> 03:05:16,998 Three and the reason I'm saying three here is because that's how 2204 03:05:17,729 --> 03:05:22,548 so I'm gonna keep looping as long as I is less than three and then 2205 03:05:23,549 --> 03:05:27,139 Now inside of these curly brackets, I want to create another 2206 03:05:27,750 --> 03:05:35,360 loop, so every time we go through this one iteration of this top 2207 03:05:39,450 --> 03:05:45,799 J is less than 2 and the reason I'm saying 2 here is because 2208 03:05:48,271 --> 03:05:51,141 Inside of the nums array and you guys will see in a second. Why? 2209 03:05:51,989 --> 03:05:58,369 this is gonna work and then I'm gonna say J plus plus so now I'm 2210 03:05:58,829 --> 03:06:04,879 Down here. I'm gonna actually be able to print out all of the 2211 03:06:04,879 --> 03:06:06,879 So I'm gonna go ahead and do that 2212 03:06:07,469 --> 03:06:15,409 I'm just gonna say printf and we're gonna be printing out an 2213 03:06:15,870 --> 03:06:18,980 and I'm gonna print out nums I and 2214 03:06:21,149 --> 03:06:26,118 So whatever the value of I is and whatever the value of J is 2215 03:06:26,219 --> 03:06:29,869 inside of nums that we're gonna print out and now just so this is 2216 03:06:30,420 --> 03:06:34,338 Formatted a little bit better. Why don't we put a comma? 2217 03:06:35,940 --> 03:06:40,908 Right there and then I'm also gonna put another printf over here 2218 03:06:42,180 --> 03:06:48,530 And you'll notice that this printf is outside of this inner for 2219 03:06:48,540 --> 03:06:53,749 We're gonna see what it does. You guys will see exactly what's 2220 03:06:59,101 --> 03:07:02,239 And you'll see over here when we ran the program 2221 03:07:02,670 --> 03:07:09,440 We're basically getting this entire 2d array printed out. So I'm 2222 03:07:09,629 --> 03:07:11,838 So let's talk about why that happens 2223 03:07:12,420 --> 03:07:16,549 Over here I'm saying for I is equal to 0 I is less than 3 I plus 2224 03:07:17,010 --> 03:07:21,319 So I'm going through this particular four loop three times 2225 03:07:21,329 --> 03:07:25,759 I'm gonna execute all the code inside of this four loop right 2226 03:07:26,459 --> 03:07:32,659 Three times that's as many times as there are elements inside of 2227 03:07:33,360 --> 03:07:39,140 Now every single time I go through this top loop every single time 2228 03:07:39,140 --> 03:07:45,770 I'm going to execute this loop in its entirety. So I will loop 2229 03:07:45,771 --> 03:07:51,409 I'll go through all of its iterations and this loop says J is 2230 03:07:51,989 --> 03:07:57,489 2 over here is how many elements are inside of each one of these 2231 03:07:57,489 --> 03:08:02,350 So this element in the noms array has one two elements inside of 2232 03:08:02,479 --> 03:08:06,219 This element in the numbers array has one two elements inside of 2233 03:08:06,530 --> 03:08:08,530 That's where this two is coming from 2234 03:08:08,780 --> 03:08:16,360 Then I'm incrementing J. And I'm gonna print out so I'm saying 2235 03:08:19,101 --> 03:08:24,520 so the first time we go through this for loop I is going to be 2236 03:08:24,649 --> 03:08:29,048 Remember that first time we're actually going to be going through 2237 03:08:29,450 --> 03:08:37,150 So we're going to be printing out nums 0 0 and nums 0 1 and then 2238 03:08:37,549 --> 03:08:44,559 the next time we go through this I loop we're gonna come down here 2239 03:08:45,110 --> 03:08:50,680 Finally the third and final time. We're gonna print out nums to 0 2240 03:08:51,110 --> 03:08:53,409 That's basically how this is working. So 2241 03:08:55,549 --> 03:08:59,319 Nested for loops are a match made in heaven and there's a lot of 2242 03:08:59,390 --> 03:09:03,640 Where you know besides just looping through 2d arrays that we're 2243 03:09:03,709 --> 03:09:09,039 But hopefully this gives you a little bit of a introduction into 2244 03:09:13,899 --> 03:09:18,459 I'm gonna talk to you guys about accessing memory addresses in C 2245 03:09:18,890 --> 03:09:24,369 Now in the C programming language a lot of times we're gonna want 2246 03:09:24,370 --> 03:09:26,890 Right, and there's a bunch of different ways that we can store 2247 03:09:26,899 --> 03:09:30,670 We can use things like variables we could use arrays we could use 2248 03:09:30,921 --> 03:09:36,159 but the basic point is that whenever we're using c we're gonna 2249 03:09:36,470 --> 03:09:42,430 Maintain a bunch of different pieces of information and one of the 2250 03:09:43,069 --> 03:09:45,850 Variables so down here you'll notice that I have a bunch of 2251 03:09:45,851 --> 03:09:49,601 I have this integer called age and it's just has the value of 30 2252 03:09:49,601 --> 03:09:56,351 We have this double GPA 3.4 and we have this character grade and 2253 03:09:56,351 --> 03:09:57,739 I have a bunch of different 2254 03:09:57,739 --> 03:10:03,100 variables in my program and it allows me to keep track of data 2255 03:10:03,530 --> 03:10:08,619 You know maintain and I can even modify these different 2256 03:10:08,620 --> 03:10:15,550 But I want to talk to you guys a little bit about how these work 2257 03:10:15,771 --> 03:10:19,989 So whenever I create a variable for example when I create a 2258 03:10:19,989 --> 03:10:26,260 And I give it a value of 30 this value 30 actually gets stored on 2259 03:10:28,729 --> 03:10:32,170 Memory, so a lot of times you'll hear people refer to this as RAM 2260 03:10:32,239 --> 03:10:39,369 It's called random access memory and basically RAM is the memory 2261 03:10:39,620 --> 03:10:45,340 So for example, if I was to run this C program my computer would 2262 03:10:45,341 --> 03:10:49,751 It would use that memory in order to store and keep track of all 2263 03:10:50,511 --> 03:10:55,930 Right. So when I create a variable like int age C is actually 2264 03:10:59,899 --> 03:11:07,479 Location so it's gonna take that value 30 and it's gonna store it 2265 03:11:07,700 --> 03:11:15,040 C's gonna take this value 3.4 and it's gonna store it inside of 2266 03:11:15,261 --> 03:11:19,751 C is gonna store this character on the physical memory in our 2267 03:11:19,751 --> 03:11:25,600 Now here's the thing when I create these variables I give them 2268 03:11:26,478 --> 03:11:28,458 So I know what's stored inside of it, right? 2269 03:11:28,459 --> 03:11:32,989 And when I want to access this value, I can just refer to the 2270 03:11:32,988 --> 03:11:36,497 I can just refer to age I could modify and I could print it out 2271 03:11:36,498 --> 03:11:37,850 I could do something else to it 2272 03:11:37,998 --> 03:11:41,539 I can do whatever I want with it and the way that I can access 2273 03:11:41,939 --> 03:11:45,979 variable in other words the way that I can access the value 30 is 2274 03:11:46,260 --> 03:11:51,498 Just by referring to the name of the variable same goes down here 2275 03:11:51,840 --> 03:11:56,568 Right if I wanted to access this capital a I could just refer to 2276 03:11:58,430 --> 03:12:03,498 All of this information is stored in our physical memory 2277 03:12:03,498 --> 03:12:11,058 And so whenever C needs to access that information whenever our 2278 03:12:11,609 --> 03:12:16,939 3.4. It's actually going to refer to a specific memory address 2279 03:12:17,039 --> 03:12:22,609 Right. So when I want to access this variable age, I can just type 2280 03:12:22,859 --> 03:12:27,229 But when C wants to refer to this value, it's not actually using 2281 03:12:27,228 --> 03:12:33,108 It's gonna use the memory address where this value is stored 2282 03:12:33,299 --> 03:12:39,198 So all of these values are stored inside of a physical address in 2283 03:12:39,199 --> 03:12:45,618 So all of these values have an address where they're stored in 2284 03:12:46,228 --> 03:12:51,947 So I'm gonna show you guys how we can access that address how we 2285 03:12:52,049 --> 03:12:56,568 And basically just wanted to give you guys an introduction into 2286 03:12:57,828 --> 03:13:03,888 I'm gonna say printf and I'm gonna show you guys how I can 2287 03:13:04,199 --> 03:13:07,729 Where each one of these values is stored on our computer? 2288 03:13:08,340 --> 03:13:14,478 So down here if I want to print out a memory address. I need to 2289 03:13:14,939 --> 03:13:19,997 normally if I was just gonna print out like a number I could say 2290 03:13:20,010 --> 03:13:23,389 Character when we want to print out a physical memory address 2291 03:13:23,389 --> 03:13:30,168 We're gonna say % P and this actually stands for pointer. We're 2292 03:13:30,168 --> 03:13:31,668 We're just gonna talk about memory addresses 2293 03:13:31,668 --> 03:13:38,148 But just know that you need to say % P and now I'm going to type 2294 03:13:38,299 --> 03:13:42,138 I want to access so I'm gonna access the memory address of age 2295 03:13:42,659 --> 03:13:44,659 Before the variable. I'm just gonna say 2296 03:13:46,290 --> 03:13:50,660 so I'm gonna make an ampersand age and I'm gonna say percent P 2297 03:13:51,239 --> 03:13:57,408 Now we should be able to print out the memory address where the 2298 03:13:57,409 --> 03:14:03,798 In other words, we can print out the memory address where this 2299 03:14:04,439 --> 03:14:07,398 You'll see over here. We're getting this number 2300 03:14:07,398 --> 03:14:12,918 So I'm getting this number 0 0 6 0 FF 2 0 0 so this would be like 2301 03:14:12,920 --> 03:14:16,250 I guess it's not technically a number. It's like I think it's 2302 03:14:16,459 --> 03:14:22,938 But basically this is the physical memory address where the value 2303 03:14:22,939 --> 03:14:27,259 It's the place where C stored the value inside of this age 2304 03:14:27,260 --> 03:14:29,359 I could do the same thing for these other variables 2305 03:14:29,359 --> 03:14:35,359 so actually why don't I format this a little bit I'm gonna stay 2306 03:14:38,489 --> 03:14:43,908 again, I'm gonna print out another one of these addresses and 2307 03:14:46,889 --> 03:14:54,500 so now I can just come over here and I can sort of modify this a 2308 03:14:56,520 --> 03:15:01,640 Grade and you'll notice for each of these I'm using this ampersand 2309 03:15:01,978 --> 03:15:06,378 So now when we run our program we should get a nice little list of 2310 03:15:07,049 --> 03:15:10,879 variables and their corresponding addresses so you'll see over 2311 03:15:11,430 --> 03:15:15,979 have age and it's stored up memory address 0 0 6 0 FF OC 2312 03:15:16,409 --> 03:15:22,009 GPA is stored at this memory address and grade is stored at this 2313 03:15:22,139 --> 03:15:26,090 so if you were to like go into our computer or go into my computer 2314 03:15:26,309 --> 03:15:34,039 Go to memory address is 0 0 6 0 FF 0 C that's actually going to 2315 03:15:34,260 --> 03:15:38,059 30 it's gonna store the value that's inside of that age variable 2316 03:15:38,059 --> 03:15:40,909 If you were to go to this memory address you would see the value 2317 03:15:40,978 --> 03:15:46,817 Inside of GPA if you're going to this memory address you would see 2318 03:15:47,219 --> 03:15:50,429 so basically all of these variables are store at 2319 03:15:50,770 --> 03:15:55,649 Different memory addresses when I want to access the information 2320 03:15:55,649 --> 03:16:00,690 I can just refer to the variables name so I could say a age or I 2321 03:16:01,000 --> 03:16:06,569 But when C wants to access the information and the variable it has 2322 03:16:07,209 --> 03:16:11,668 Addresses so C would use this address C would use this address and 2323 03:16:11,670 --> 03:16:17,700 I can just use the variable names and that's why C is awesome 2324 03:16:17,979 --> 03:16:24,748 But when C and when our computer wants to access these values it 2325 03:16:24,750 --> 03:16:26,750 and so that's why these are useful and 2326 03:16:28,440 --> 03:16:31,588 it can actually be pretty useful to be able to know the 2327 03:16:31,959 --> 03:16:37,109 Addresses like the actual physical addresses of these certain 2328 03:16:37,180 --> 03:16:39,870 So a lot of times in C when we want to work with these 2329 03:16:40,000 --> 03:16:44,159 Variables we can just refer to them by name and we can do all 2330 03:16:44,159 --> 03:16:51,239 We can access them we can modify them. But in other circumstances, 2331 03:16:51,239 --> 03:16:55,918 We're also gonna want to be able to access their physical memory 2332 03:16:55,920 --> 03:17:01,259 we're gonna want to be able to know like what that address is and 2333 03:17:01,360 --> 03:17:05,279 In future tutorials, I'm gonna talk about why that can be useful, 2334 03:17:05,280 --> 03:17:09,418 I really just wanted to kind of give you guys an overview of what 2335 03:17:09,610 --> 03:17:12,838 How we're using them in our program and how we can print them out 2336 03:17:13,200 --> 03:17:19,709 I just use this ampersand and then I type the name of the variable 2337 03:17:23,399 --> 03:17:27,359 In this tutorial I want to talk to you guys about pointers in C 2338 03:17:27,879 --> 03:17:34,048 Now a pointer is basically just a type of data that we can use 2339 03:17:34,239 --> 03:17:39,088 So up until this point in the course, we've been using a bunch of 2340 03:17:39,101 --> 03:17:46,650 we've been using things like integers and an integer is just a 2341 03:17:46,959 --> 03:17:48,959 decimal number we've been using 2342 03:17:49,090 --> 03:17:55,829 Chars and a char is basically just a character and now I want to 2343 03:17:55,829 --> 03:18:03,239 which is called a pointer and a pointer is basically just a memory 2344 03:18:03,700 --> 03:18:07,950 you know a physical address inside of the memory of our computer 2345 03:18:08,351 --> 03:18:11,310 Storing a value right and I think pointers 2346 03:18:11,560 --> 03:18:18,090 Tend to confuse a lot of people who learn about them and this is 2347 03:18:18,370 --> 03:18:23,759 Because it's you know, traditionally very confusing to understand 2348 03:18:24,340 --> 03:18:27,989 Extremely simple and the problem is that they just get over 2349 03:18:28,090 --> 03:18:33,059 So what I want you guys to do is I just want you guys to think of 2350 03:18:33,370 --> 03:18:37,230 That's all it is. It's just a type of data that we can work with 2351 03:18:37,229 --> 03:18:40,259 It doesn't have to be any more complicated than that, right? 2352 03:18:40,319 --> 03:18:44,610 I can work with an integer in my program an integer is a whole 2353 03:18:44,610 --> 03:18:48,418 I can work with a double in my program a double is a decimal 2354 03:18:48,420 --> 03:18:52,350 I can work with a char in my program a char is just a character 2355 03:18:52,351 --> 03:18:56,460 I can work with a pointer in my program a pointer is just a memory 2356 03:18:56,979 --> 03:19:01,679 That's it. A pointer is just another type of data that I can use 2357 03:19:02,200 --> 03:19:09,119 Inside of my programs and it just happens to be a memory address 2358 03:19:10,021 --> 03:19:16,170 It's very simple people overcomplicate pointers and they don't 2359 03:19:16,479 --> 03:19:22,468 Just like an integer or a double but instead of being like a whole 2360 03:19:22,569 --> 03:19:25,588 So hopefully that makes sense and I hope you think you guys don't 2361 03:19:25,810 --> 03:19:30,449 Try to over complicate this too much because it doesn't have to be 2362 03:19:30,450 --> 03:19:34,860 I have an integer that I've created and it's called age. So I just 2363 03:19:35,950 --> 03:19:39,420 Inside of this integer. I'm storing a whole number 2364 03:19:39,421 --> 03:19:40,921 I'm storing an integer right third 2365 03:19:40,921 --> 03:19:43,590 And if you've been following along with this course in the last 2366 03:19:43,591 --> 03:19:51,270 We talked about memory addresses and we talked about how all of 2367 03:19:51,489 --> 03:19:55,020 Physical addresses on our computer's memory so down here 2368 03:19:55,021 --> 03:20:02,311 I have this print statement and I'm just printing out ages memory 2369 03:20:02,649 --> 03:20:08,998 Ampersand age and we talked about how this ampersand when I put 2370 03:20:09,000 --> 03:20:14,790 It's going to give me the physical address in memory where this 2371 03:20:15,341 --> 03:20:21,360 Thermia value is stored. So if I run this program you guys will 2372 03:20:21,940 --> 03:20:25,649 Hexadecimal number right? This is the physical memory address of 2373 03:20:27,819 --> 03:20:32,340 What did we just talk about before remember what I told you 2374 03:20:33,190 --> 03:20:34,801 Memory addresses that's what they are 2375 03:20:34,800 --> 03:20:38,219 it's a type of data a pointer is a type of data in our program 2376 03:20:38,530 --> 03:20:43,590 It's a memory address right when I use this ampersand and I type 2377 03:20:43,591 --> 03:20:50,309 This is giving me the memory address right I'm able to print out 2378 03:20:51,040 --> 03:20:56,790 This is a type of data in our program. That is a memory address 2379 03:20:56,790 --> 03:21:02,460 That's a whole number and a double is a type of data. That's a 2380 03:21:02,460 --> 03:21:07,739 That's a character a pointer is a type of data. That's a memory 2381 03:21:07,739 --> 03:21:13,559 This guy right here is a pointer. So it's just the memory address 2382 03:21:15,899 --> 03:21:19,260 and when I want to print out a pointer I can use this percent P 2383 03:21:19,390 --> 03:21:22,350 It'll allow me to print it out onto the screen and everyone's 2384 03:21:22,380 --> 03:21:26,609 So essentially what I'm doing here is I'm printing out a pointer 2385 03:21:26,610 --> 03:21:32,909 I'm telling this print F function that I want to put on a pointer 2386 03:21:32,909 --> 03:21:35,819 It a physical memory address. That's what a pointer is now 2387 03:21:35,819 --> 03:21:41,129 We understand and hopefully understand that a pointer is just a 2388 03:21:41,979 --> 03:21:48,180 We can access the memory addresses of specific variables by using 2389 03:21:48,439 --> 03:21:51,248 The name of the variable and that can be pretty useful 2390 03:21:51,590 --> 03:21:56,859 Now that we understand that I want to show you guys how we can 2391 03:21:56,859 --> 03:22:04,658 I have an integer variable called age and this integer variable is 2392 03:22:05,299 --> 03:22:12,789 I'm gonna say that one more time. I have an integer variable and 2393 03:22:12,790 --> 03:22:17,649 I could also create a double variable so I could create a double 2394 03:22:18,680 --> 03:22:25,809 Inside of this double variable. I could store a double value like 2395 03:22:26,569 --> 03:22:31,449 Called grade and inside of this char variable. I can store a 2396 03:22:32,209 --> 03:22:35,079 could also create a pointer variable and 2397 03:22:36,260 --> 03:22:39,849 Inside of that pointer variable I could store a pointer 2398 03:22:41,148 --> 03:22:48,818 So just like I stored a character inside of this character 2399 03:22:48,819 --> 03:22:53,558 I could store a pointer inside of a pointer variable so we can 2400 03:22:56,450 --> 03:22:59,499 Now here's where this is going to get a little bit tricky 2401 03:23:00,319 --> 03:23:02,319 When we create a pointer variable 2402 03:23:02,930 --> 03:23:07,450 we actually need a physical memory address right so when I create 2403 03:23:08,030 --> 03:23:11,918 Integer variable like I can just come up with a number right off 2404 03:23:11,920 --> 03:23:14,620 I can just say like 30 when I create a 2405 03:23:15,260 --> 03:23:18,370 Double variable I can just store whatever number I want to store 2406 03:23:18,500 --> 03:23:21,009 but when we create a pointer variable remember 2407 03:23:21,010 --> 03:23:24,639 We're gonna be storing a memory address and I don't necessarily 2408 03:23:24,799 --> 03:23:30,608 Know any memory addresses like right off the top of my head at 2409 03:23:30,609 --> 03:23:37,238 So when I create a pointer variable, what I want to do is store 2410 03:23:38,030 --> 03:23:43,959 That's already in a program. So I'm gonna say that one more time 2411 03:23:45,020 --> 03:23:46,960 inside of that pointer variable 2412 03:23:46,959 --> 03:23:52,659 I'm gonna store the memory address of a another variable inside of 2413 03:23:52,670 --> 03:23:56,829 so let's say that I wanted to create a pointer that would store 2414 03:23:57,739 --> 03:24:01,959 This variable over here so thats or the memory adjusts of age, 2415 03:24:02,960 --> 03:24:06,129 can create a pointer just like this so I could say int and 2416 03:24:06,649 --> 03:24:09,940 asterisks and now I'm gonna give this a name so I'm gonna give 2417 03:24:10,130 --> 03:24:17,171 Pointer variable a name and remember this pointer variable is 2418 03:24:17,360 --> 03:24:22,329 variable and generally when you're naming a pointer, you're gonna 2419 03:24:22,851 --> 03:24:30,370 Then you're gonna type the name of the variable whose memory 2420 03:24:30,591 --> 03:24:37,331 So I'm gonna say P age right because this pointer variable is 2421 03:24:38,030 --> 03:24:42,579 Now what I want to do is set this equal to the memory address of 2422 03:24:42,800 --> 03:24:46,869 Which we can access using this ampersand so I could say ampersand 2423 03:24:47,421 --> 03:24:50,139 so now this pointer variable is 2424 03:24:50,810 --> 03:24:53,591 Storing the memory address of the age variable 2425 03:24:54,140 --> 03:24:58,119 I'm gonna do the same thing down here for this GPA 2426 03:24:58,341 --> 03:25:05,770 So if I wanted to create a pointer variable that would store the 2427 03:25:06,351 --> 03:25:13,931 asterisks and I'm just gonna say P GPA just like that and I'm 2428 03:25:14,239 --> 03:25:20,770 This double variable which we can access using this ampersand and 2429 03:25:20,810 --> 03:25:24,610 I'm gonna do the same thing down here for this character variable 2430 03:25:24,610 --> 03:25:28,840 So I want to create a pointer variable which is going to store the 2431 03:25:29,421 --> 03:25:32,860 The character variable so I'm just gonna say char 2432 03:25:36,110 --> 03:25:38,140 grade, and I'm gonna set this equal to 2433 03:25:38,870 --> 03:25:42,520 Ampersand grade. Okay, so that's all I'm doing, right? 2434 03:25:42,521 --> 03:25:47,200 So now I have this integer variable and I have this pointer 2435 03:25:47,630 --> 03:25:53,649 Which is storing the memory location or the memory address of the 2436 03:25:53,649 --> 03:26:00,549 I have this double variable which is storing a double and I have 2437 03:26:00,921 --> 03:26:05,800 The pointer just happens to be the memory address of the GPA 2438 03:26:05,800 --> 03:26:08,649 I have this char variable down here 2439 03:26:08,649 --> 03:26:14,469 And then I have this pointer variable, which is storing a pointer 2440 03:26:15,049 --> 03:26:17,079 the physical memory address of 2441 03:26:17,719 --> 03:26:20,349 the great variable in our memory 2442 03:26:20,559 --> 03:26:27,670 Okay, and that's basically all you need to know about pointers to 2443 03:26:27,829 --> 03:26:31,298 It's just a type of information that we can work with in our 2444 03:26:31,389 --> 03:26:33,549 it just happens to be a memory addresses write an 2445 03:26:33,950 --> 03:26:39,219 integer is a type of data that we can work with in our programs 2446 03:26:39,620 --> 03:26:44,439 double is a type of data that we can work with in our programs and 2447 03:26:44,930 --> 03:26:49,269 char is a type of data that we can work with in our programs and 2448 03:26:49,790 --> 03:26:54,129 Pointer is a type of data that we can work with in our programs 2449 03:26:54,260 --> 03:26:59,920 Hopefully that makes sense and just like we create integer 2450 03:27:00,559 --> 03:27:04,510 Pointer variables the only difference is whenever we create a 2451 03:27:04,969 --> 03:27:09,399 We're gonna store the memory address of another variable in our 2452 03:27:09,399 --> 03:27:14,888 So so you'll see over here when I create this pointer variable. 2453 03:27:15,620 --> 03:27:22,450 The variable whose address I'm storing so over here when I'm 2454 03:27:22,670 --> 03:27:28,540 When I'm storing the address of a double variable I'm saying 2455 03:27:28,540 --> 03:27:33,459 I'm saying char here and that's the basics of using pointers and 2456 03:27:34,520 --> 03:27:40,390 Also creating pointer variables and storing memory addresses 2457 03:27:44,270 --> 03:27:46,270 In this tutorial I want to talk to you guys about 2458 03:27:49,109 --> 03:27:55,729 Now a pointer is basically just a type of data that we can work 2459 03:27:55,770 --> 03:27:59,988 So sometimes in our programs we're gonna want to work with memory 2460 03:27:59,988 --> 03:28:05,747 We're gonna want to use them in our programs and do certain things 2461 03:28:06,059 --> 03:28:10,309 pointers in our programs and when you dereference a pointer 2462 03:28:10,918 --> 03:28:15,257 Essentially what you're doing is you're going to the memory 2463 03:28:15,898 --> 03:28:20,327 Pointer and you're grabbing the piece of information from there. 2464 03:28:20,328 --> 03:28:23,808 So all points are as our memory addresses and when we dereference 2465 03:28:24,270 --> 03:28:30,770 We go to that physical memory address and we get the information 2466 03:28:30,779 --> 03:28:34,458 so I'm gonna show you guys how we can do that and see it's 2467 03:28:37,168 --> 03:28:39,168 Down here. I have this integer and 2468 03:28:40,228 --> 03:28:47,269 It's an integer variable and it's storing the integer value 30 and 2469 03:28:47,850 --> 03:28:54,199 memory address of 30 so it's storing the memory address of our age 2470 03:28:55,049 --> 03:28:58,069 Down here. I'm just printing out this pointer 2471 03:28:58,068 --> 03:29:02,028 So I'm printing out pH I'm using this percent P and I'm just 2472 03:29:02,398 --> 03:29:05,808 So why don't we take a look and see what we get. So you'll see 2473 03:29:05,908 --> 03:29:11,658 We just get this memory address probably exactly what you expected 2474 03:29:11,658 --> 03:29:14,808 I want to show you guys how we can dereference a pointer 2475 03:29:15,328 --> 03:29:21,587 So this pointer variable P age is storing the memory address of 2476 03:29:22,818 --> 03:29:29,028 We basically were given the physical memory address where this 2477 03:29:29,219 --> 03:29:36,918 So P age is storing the memory address of 30. It's storing the 2478 03:29:37,648 --> 03:29:42,077 Inside of our computer's memory if I want to dereference a 2479 03:29:43,049 --> 03:29:50,089 Basically, what I'm gonna be doing is I'm going to be going to 2480 03:29:50,309 --> 03:29:54,559 So the way that I can dereference a pointer is by using the 2481 03:29:54,559 --> 03:29:59,299 So when I'm printing this point, I can dereference it by using 2482 03:29:59,459 --> 03:30:03,168 But now over here since I'm dereferencing this pointer 2483 03:30:03,168 --> 03:30:09,709 I need to print out a number instead of a pointer because when I 2484 03:30:09,988 --> 03:30:14,838 This is no longer a pointer. This is no longer a memory address 2485 03:30:15,029 --> 03:30:22,488 This is gonna be whatever was stored at the memory address. The 2486 03:30:25,228 --> 03:30:28,099 Variable was storing the address of an integer 2487 03:30:28,680 --> 03:30:31,010 Right, so when I dereference it 2488 03:30:31,590 --> 03:30:33,078 This is actually gonna be an integer 2489 03:30:33,078 --> 03:30:37,099 So let me show you guys I'm gonna print out or I'm gonna run our 2490 03:30:38,908 --> 03:30:43,398 So you'll see that we get a 30. So basically what's happening here 2491 03:30:45,689 --> 03:30:52,519 Pointer variable we're dereferencing the pH pointer which 2492 03:30:53,010 --> 03:30:59,209 Physical value that's stored at the memory address that the 2493 03:30:59,549 --> 03:31:03,049 And that's basically what dereferencing is, you know, you're you 2494 03:31:03,049 --> 03:31:06,288 You can dereference it and you basically just get the value that 2495 03:31:07,020 --> 03:31:09,918 It's essentially all we're doing so I want to show you guys how we 2496 03:31:10,590 --> 03:31:13,039 Just normally so for example 2497 03:31:13,039 --> 03:31:15,378 I have this age variable if I just typed this out 2498 03:31:15,779 --> 03:31:20,179 If I wanted to get the address of this age variable I could use 2499 03:31:20,180 --> 03:31:22,168 That's basically what we did up here 2500 03:31:22,168 --> 03:31:27,619 but what I could do is I could just dereference this so I can say 2501 03:31:27,840 --> 03:31:29,840 asterisks here and this is actually gonna 2502 03:31:30,029 --> 03:31:32,029 dereference that entire thing 2503 03:31:32,248 --> 03:31:38,539 So now when I run my program it's gonna print out 30, so it's not 2504 03:31:38,539 --> 03:31:42,378 It's just gonna print out 30 so you can see over here. We get 30 2505 03:31:42,898 --> 03:31:48,349 so I could actually do this like as many times as I want and this 2506 03:31:48,350 --> 03:31:53,059 An ampersand here and now I'm gonna get that memory address again 2507 03:31:55,049 --> 03:32:02,748 So you'll see we're getting this memory address and I can do the 2508 03:32:05,609 --> 03:32:08,778 You'll see now we're getting that 30 value again 2509 03:32:09,049 --> 03:32:16,139 I mean basically what I'm doing I'm constantly like like here I'm 2510 03:32:16,421 --> 03:32:23,069 Then I'm dereferencing and getting the value then. I'm getting the 2511 03:32:23,069 --> 03:32:26,399 So, you know, hopefully that makes sense when you dereference a 2512 03:32:26,860 --> 03:32:33,150 you're basically just getting the value that that pointer is 2513 03:32:33,341 --> 03:32:35,520 memory address of the pointer 2514 03:32:39,810 --> 03:32:44,279 In this tutorial I want to teach you guys how to write two files 2515 03:32:44,470 --> 03:32:50,761 One of the cool things we can do in C is we can actually modify 2516 03:32:51,250 --> 03:32:54,088 So in this tutorial, I'll just basically show you guys how to do 2517 03:32:54,090 --> 03:32:58,829 We'll talk about creating files writing files and appending on to 2518 03:32:58,829 --> 03:33:02,909 So let's go ahead and do that down here in my main function 2519 03:33:03,909 --> 03:33:08,489 We're actually gonna create a file. So I'm going to create a file 2520 03:33:10,870 --> 03:33:15,900 the way that we can do that is we can just type out file all in 2521 03:33:16,601 --> 03:33:22,620 essentially what we're doing here is we're creating a pointer to a 2522 03:33:24,640 --> 03:33:28,529 I'm just gonna say file and I'm gonna make an Asterix and I'm just 2523 03:33:28,530 --> 03:33:30,221 So I'm just gonna call this F 2524 03:33:30,220 --> 03:33:35,639 Pointer and that'll just stand for file pointer, and I want to set 2525 03:33:36,250 --> 03:33:41,309 Open an F open is actually a function that's gonna open a file for 2526 03:33:41,829 --> 03:33:45,930 And so inside of these parentheses, I'm gonna give this a couple 2527 03:33:46,120 --> 03:33:50,789 The first parameter is gonna be the name of the file that I want 2528 03:33:52,149 --> 03:33:55,918 employees dot text and the second parameter is gonna be a 2529 03:33:56,200 --> 03:34:00,959 File mode and a file mode is basically just going to tell C 2530 03:34:01,239 --> 03:34:03,628 What we want to do with the file that we're gonna open 2531 03:34:03,790 --> 03:34:10,200 And there's a bunch of different file modes. The three most basic 2532 03:34:10,299 --> 03:34:13,289 So if I put R in there, I mean we want to read the file 2533 03:34:13,870 --> 03:34:16,709 W stands for right so if I put right in there 2534 03:34:16,709 --> 03:34:21,899 it means I want to either create a new file or write over an 2535 03:34:22,870 --> 03:34:28,410 There's also a which stands for append and that means I want to 2536 03:34:28,409 --> 03:34:31,889 we're gonna look at W and a so what I want to do over here is 2537 03:34:34,390 --> 03:34:37,949 Right now employees dot text. This file doesn't actually exist 2538 03:34:38,500 --> 03:34:41,700 So what I'm gonna do is I'm actually going to create that file 2539 03:34:41,700 --> 03:34:48,240 We're gonna put some information inside that file and then that 2540 03:34:48,239 --> 03:34:50,011 We're gonna need this write mode 2541 03:34:50,011 --> 03:34:51,819 So it's just going to be a W 2542 03:34:51,819 --> 03:34:57,988 Anytime that we're open file in see you always want to make sure 2543 03:34:59,250 --> 03:35:02,069 I want to use another function called F close and 2544 03:35:02,530 --> 03:35:05,219 Basically what this is going to do is it's going to close off the 2545 03:35:05,219 --> 03:35:08,760 so it's gonna remove the file from the memory on our computer and 2546 03:35:09,129 --> 03:35:12,718 if we do make any changes it'll kind of like save the file and 2547 03:35:12,909 --> 03:35:17,489 Everything will just be kind of like locked in so you always want 2548 03:35:17,489 --> 03:35:22,019 It and inside of this F close function. We just want to pass in 2549 03:35:24,549 --> 03:35:30,539 Pointer right there. So let's talk about what's happening here 2550 03:35:31,090 --> 03:35:37,860 Essentially like a data type I guess and we're creating a pointer 2551 03:35:38,379 --> 03:35:41,459 So if you remember a pointer is basically just a memory address 2552 03:35:41,950 --> 03:35:46,799 Right a pointer is a type of data that we can use in our programs. 2553 03:35:47,469 --> 03:35:54,659 this pointer is storing the memory address of this new employees 2554 03:35:55,059 --> 03:35:59,189 Essentially what's going to happen is we're gonna create this 2555 03:35:59,409 --> 03:36:04,888 Pointer is going to be pointing to the memory address of that file 2556 03:36:05,920 --> 03:36:11,670 So that's essentially what's happening here. And what I want to do 2557 03:36:12,189 --> 03:36:17,759 To this file. So actually let me just show you guys really quick. 2558 03:36:17,760 --> 03:36:24,870 So without doing anything else just using this line right here 2559 03:36:25,870 --> 03:36:28,349 Employees dot text is going to get created so over here 2560 03:36:28,350 --> 03:36:30,840 I'm in my little directory and 2561 03:36:31,180 --> 03:36:36,750 If you don't specify an absolute path or like a relative path for 2562 03:36:36,909 --> 03:36:40,168 It's just gonna create it in the directory that your C files in 2563 03:36:40,270 --> 03:36:43,470 So if I wanted to create this on my C Drive, I could say like C 2564 03:36:44,680 --> 03:36:51,210 Whatever and then like employees out text and this will get 2565 03:36:52,030 --> 03:36:54,269 Or even if I don't specify a relative path 2566 03:36:54,579 --> 03:36:58,769 then this is just going to get created in the same directory as 2567 03:37:01,209 --> 03:37:04,378 My main dot C file is right here. It's in this folder 2568 03:37:04,840 --> 03:37:10,440 So when I run this program, let's watch over here and we can see 2569 03:37:10,440 --> 03:37:15,149 So I'm gonna run my program and you'll see over here that this 2570 03:37:15,149 --> 03:37:18,600 So if I actually bring this out a little bit you'll see it's a 2571 03:37:19,181 --> 03:37:24,329 Its employees dot tax and there's zero kilobytes inside of this so 2572 03:37:24,459 --> 03:37:30,569 But this file got created. So now let's talk about putting some 2573 03:37:30,569 --> 03:37:34,828 I'm basically just gonna list out a bunch of like employees that 2574 03:37:35,079 --> 03:37:38,579 The easiest way to write information to a file is just to say F 2575 03:37:39,310 --> 03:37:41,761 Print F and this is a special 2576 03:37:43,630 --> 03:37:47,219 Function in C, which actually works a lot like the printf 2577 03:37:47,220 --> 03:37:50,189 So if you've used the printf function and if you're following 2578 03:37:50,190 --> 03:37:57,600 We've been using it throughout the entire course the printf 2579 03:37:58,271 --> 03:38:02,221 Printf is it can allow us to essentially just write information to 2580 03:38:02,220 --> 03:38:07,050 I need to give this a couple parameters. The first one is gonna be 2581 03:38:07,050 --> 03:38:12,899 I need to tell this function, you know what it should be writing 2582 03:38:13,540 --> 03:38:20,040 Then over here we can just give this some information so I can 2583 03:38:20,290 --> 03:38:26,130 Can just use this like I normally would the printf function so I 2584 03:38:26,319 --> 03:38:28,889 So I could say like let's say one of the employees is Jim 2585 03:38:29,409 --> 03:38:33,629 And we'll put like what they do. So like I can say sales men and 2586 03:38:34,659 --> 03:38:40,319 then we could say actually let's make a new line and we'll create 2587 03:38:41,290 --> 03:38:44,220 receptionist and then we can make some other employees so 2588 03:38:45,610 --> 03:38:52,860 So now I have three employees that I'm going to basically write 2589 03:38:54,190 --> 03:38:56,248 We can go over here to this employees file 2590 03:38:56,860 --> 03:39:00,989 You'll see when I open this up that we have these three lines in 2591 03:39:00,989 --> 03:39:04,530 So we have Jim salesman Pam receptionist and Oscar accounting 2592 03:39:04,530 --> 03:39:08,159 So basically I was able to create this file and write all this 2593 03:39:08,319 --> 03:39:13,828 Information in the file from down here just from down here in this 2594 03:39:14,140 --> 03:39:18,091 And I want to show you guys with right so when I'm using this, 2595 03:39:19,030 --> 03:39:25,180 This is actually going to overwrite a file. So for example if I 2596 03:39:28,700 --> 03:39:32,019 Overridden in here now when I run this program and 2597 03:39:32,870 --> 03:39:36,459 We open this employee's dot text file. So let me reopen this 2598 03:39:37,431 --> 03:39:42,520 You'll see it just says overridden so I've actually overridden all 2599 03:39:42,521 --> 03:39:48,041 And that's basically what happens when we just write to a file. So 2600 03:39:48,040 --> 03:39:50,500 I'm just gonna run this again. So we have this text 2601 03:39:52,640 --> 03:39:58,421 And you'll see now we're back with the employees so let's say that 2602 03:39:58,421 --> 03:40:00,729 We wanted to add an employee on to the end of it 2603 03:40:00,729 --> 03:40:04,299 So we wanted to add like a new employee on to this file, right? 2604 03:40:04,880 --> 03:40:12,010 Well, I can use something called append and what append is gonna 2605 03:40:12,140 --> 03:40:18,190 To the end of this file or actually just append text on to the end 2606 03:40:18,190 --> 03:40:25,030 I'm just gonna say a and you'll notice I'm still keeping employees 2607 03:40:26,751 --> 03:40:28,451 another employee into the file 2608 03:40:28,450 --> 03:40:34,599 The first thing I'm gonna do is print out a new line because and 2609 03:40:36,380 --> 03:40:42,340 There actually isn't a line after this last line so the last like 2610 03:40:42,341 --> 03:40:44,921 I can't actually get on to the line below Oscar 2611 03:40:45,501 --> 03:40:50,590 So I'm gonna want to go ahead and print a new line and this will 2612 03:40:52,640 --> 03:40:55,390 Kelly and she's gonna work in customer 2613 03:40:56,181 --> 03:41:03,041 Service. So now when I run my program instead of overriding what 2614 03:41:03,040 --> 03:41:08,799 This is just gonna append this new employee onto the file. So I'll 2615 03:41:09,800 --> 03:41:17,530 And let's open this employee's file and you'll see now we have 2616 03:41:18,740 --> 03:41:24,219 We either create a new file and add content onto it or we just 2617 03:41:24,740 --> 03:41:30,340 At least using this F printf function and when we append, we're 2618 03:41:30,470 --> 03:41:32,559 so just using those two things, I mean you can 2619 03:41:33,111 --> 03:41:35,111 essentially write out any you want 2620 03:41:35,190 --> 03:41:38,838 and it's important to note that you can write more than just text 2621 03:41:38,940 --> 03:41:41,658 HTML file or I could write like a CSS file 2622 03:41:42,930 --> 03:41:46,370 you could write really I mean any type of file that you'd want 2623 03:41:47,069 --> 03:41:50,988 That's basically how we can write to a file and append it to the 2624 03:41:55,520 --> 03:41:57,649 I will not teach you guys how to read the 2625 03:41:58,020 --> 03:42:03,590 Information from a file in C in the last tutorial we talked about 2626 03:42:03,590 --> 03:42:10,398 So we looked at how we could create a file overwrite a file and 2627 03:42:10,398 --> 03:42:12,398 I'm gonna show you guys how you can read 2628 03:42:12,568 --> 03:42:17,868 Information from a file so down here you'll notice that I have my 2629 03:42:17,870 --> 03:42:22,759 So I just say file and the asterisks and I'd called it FP and 2630 03:42:22,760 --> 03:42:26,000 this is just a pointer which you know and a pointer is just a 2631 03:42:26,639 --> 03:42:31,760 Memory address and it's the memory address of this file in 2632 03:42:32,280 --> 03:42:39,649 Memory, so it's the memory address of this employees dot txt file 2633 03:42:42,000 --> 03:42:46,759 Opens this file and then I'm specifying a file mode 2634 03:42:46,829 --> 03:42:50,418 We talked about the W file mode and the a file mode now 2635 03:42:50,420 --> 03:42:53,750 I want to talk to you guys about the our file mode and this stands 2636 03:42:54,059 --> 03:42:55,728 So when I use the our file mode 2637 03:42:55,728 --> 03:43:00,259 I'm actually able to read information from the file and this can 2638 03:43:00,689 --> 03:43:03,379 So in order to read the information in the file 2639 03:43:03,389 --> 03:43:07,430 I'm actually going to show you guys how we can read like the 2640 03:43:07,430 --> 03:43:09,430 So actually first let's look at the file 2641 03:43:09,750 --> 03:43:16,638 This is employees dot txt. It just has you know, basically a bunch 2642 03:43:17,520 --> 03:43:20,210 We're gonna be able to read this file inside of our program 2643 03:43:20,209 --> 03:43:22,308 so the first thing I want to do is 2644 03:43:22,648 --> 03:43:27,259 create a variable and I'm gonna create this variable up here 2645 03:43:28,318 --> 03:43:32,208 it's just gonna be an array of characters and I'm just gonna call 2646 03:43:32,969 --> 03:43:39,920 I'm gonna give this an initial size of 255 so be able to store 255 2647 03:43:42,600 --> 03:43:46,850 Order to read this file. I can actually read the file one line at 2648 03:43:51,600 --> 03:43:58,790 Gets and F gets basically is going to allow us to read the lines 2649 03:43:58,790 --> 03:44:03,019 Gets and I need to give this a few different parameters the first 2650 03:44:03,020 --> 03:44:07,189 I need to give it is a place to store the information that it 2651 03:44:07,189 --> 03:44:12,199 So this function is going to go and it's going to read the 2652 03:44:12,270 --> 03:44:16,640 So the first parameter we need to give this function is where it 2653 03:44:17,600 --> 03:44:24,260 We're going to store it inside of that align variable that we 2654 03:44:24,780 --> 03:44:27,049 size and this is basically just gonna be like 2655 03:44:27,989 --> 03:44:35,989 The maximum size that we can read in so I'm just gonna say 255. 2656 03:44:36,989 --> 03:44:39,588 Then we also want to give it that file pointer 2657 03:44:41,579 --> 03:44:48,200 Pointer and basically what this is gonna do is it's going to read 2658 03:44:48,930 --> 03:44:55,370 Inside of this line variable. So if I was to come down here and 2659 03:44:58,889 --> 03:45:01,699 Just print out line now when I print out a line 2660 03:45:01,700 --> 03:45:07,429 It's actually going to be have the value of the first line in the 2661 03:45:08,100 --> 03:45:10,909 You'll see over here. We're printing out Jim salesman 2662 03:45:10,909 --> 03:45:15,798 And if you didn't remember that was the first line inside of that 2663 03:45:16,109 --> 03:45:23,029 So basically what F gets is doing is it's reading the first line. 2664 03:45:25,859 --> 03:45:33,049 Essentially pointing to where the file is in our memory and 2665 03:45:33,049 --> 03:45:36,529 So it's basically gonna move past the first line in the file 2666 03:45:37,139 --> 03:45:39,529 So if I was to use this again? 2667 03:45:40,290 --> 03:45:42,499 For example if I said f gets again 2668 03:45:44,930 --> 03:45:48,979 It's gonna store the value of the first line of the file inside of 2669 03:45:48,978 --> 03:45:54,558 And when I use it again, it's now gonna grab the second line in 2670 03:45:55,318 --> 03:46:01,908 This should actually be storing the second line in the file. So 2671 03:46:02,459 --> 03:46:06,378 Receptionist I can basically do this for all of the lines in the 2672 03:46:06,420 --> 03:46:11,780 So that's basically how we can use F gets to read the individual 257491

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.