Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,030 --> 00:00:01,439
Hey, welcome to Giraffe Academy
2
00:00:01,439 --> 00:00:02,970
My name is Mike in this course
3
00:00:02,970 --> 00:00:07,710
I'm gonna be teaching you guys everything you need to know to get started in the C programming language
4
00:00:08,139 --> 00:00:13,739
C is an awesome programming language and it's actually one of the oldest programming languages around in fact
5
00:00:13,740 --> 00:00:17,070
A lot of modern programming languages are based off of C
6
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 you're trying to get into something like
7
00:00:22,029 --> 00:00:26,549
C++ to learn the basics and in this course, we're gonna cover everything you need to know
8
00:00:26,550 --> 00:00:32,910
I'm gonna talk to you guys about installing a text editor and using the C compiler and we're gonna write some basic code
9
00:00:32,910 --> 00:00:37,139
I'll talk to you guys about you know, what is a program and how the program's work and how does C
10
00:00:37,809 --> 00:00:41,429
Read the instructions that you give it and we're gonna get into some more advanced stuff
11
00:00:41,430 --> 00:00:45,629
We're gonna look at things like if statements and loops we're gonna create different variables
12
00:00:45,629 --> 00:00:50,249
We're gonna talk about the different types of data that you can use and C then we're gonna get more advanced
13
00:00:50,250 --> 00:00:54,629
We're gonna talk about things like structures and the functions. We're going to talk about pointers
14
00:00:54,629 --> 00:01:00,929
And basically I'm just gonna give you a full overview of all of the core concepts in C
15
00:01:00,969 --> 00:01:05,579
So by the end of this course, you'll have a really good understanding and a really good foundation
16
00:01:05,799 --> 00:01:09,089
Which you can build on you can kind of go forward and learn some more
17
00:01:09,090 --> 00:01:12,869
I'm really excited to be bringing you guys this basic course on C
18
00:01:12,869 --> 00:01:15,929
I'm really excited if you guys to dive in and start using these tutorials
19
00:01:15,930 --> 00:01:20,999
So feel free to click around through all the videos and hopefully you'll learn something awesome about C
20
00:01:24,740 --> 00:01:25,500
In this tutorial
21
00:01:25,500 --> 00:01:32,089
I'm gonna talk to you guys about getting everything set up to start programming in C now in order to program in C
22
00:01:32,090 --> 00:01:34,010
We're actually going to need two things
23
00:01:34,010 --> 00:01:38,690
The first thing we're gonna need is an environment where we can write our C programs
24
00:01:38,909 --> 00:01:43,728
Now there's a bunch of these different environments. Essentially. All you need is just a text editor
25
00:01:43,729 --> 00:01:45,769
so anything that can you know, allow you to
26
00:01:46,350 --> 00:01:51,350
Write text and then save that text in a specific file format is gonna work
27
00:01:51,630 --> 00:01:58,789
although when we're working with C a lot of times it can be useful to use a special environment called an IDE an
28
00:01:59,160 --> 00:02:01,160
IDE stands for integrated
29
00:02:01,410 --> 00:02:08,329
Development environment. Basically, this is a special text editor which makes it a lot easier for us to write our C programs
30
00:02:08,330 --> 00:02:13,969
And so I'm gonna show you guys how to install an IDE called code blocks the second thing
31
00:02:13,970 --> 00:02:20,600
We need to write our C programs is going to be something called a C compiler now C is a programming language
32
00:02:20,600 --> 00:02:22,370
It means we can basically write out
33
00:02:22,370 --> 00:02:28,130
You know instructions that we want to give to the computer but eventually for the computer to be able to execute those
34
00:02:28,440 --> 00:02:30,209
instructions they have to be
35
00:02:30,209 --> 00:02:36,619
Compiled which basically means like translated or transformed into a language that the computer can understand
36
00:02:36,620 --> 00:02:43,009
So we're gonna basically download and install a special program that will do that for us. So let's get started
37
00:02:43,010 --> 00:02:45,200
I am going to come over to my web browser and
38
00:02:46,380 --> 00:02:52,489
I'm just gonna go up to the Google search bar. And I'm gonna search for a program called code block. So just type in
39
00:02:53,130 --> 00:02:58,789
Code blocks C. And this link should come up. It's code blocks org now
40
00:02:58,790 --> 00:03:01,250
This is an integrated development environment
41
00:03:01,250 --> 00:03:08,119
It's basically a text editor that will make it really easy for us to write our C programs. So I'm gonna come down here into
42
00:03:08,670 --> 00:03:10,200
downloads and
43
00:03:10,200 --> 00:03:14,299
There's a bunch of options here one says download the binary or at least download the source code
44
00:03:14,459 --> 00:03:18,469
We want to click on download the binary release. This is going to be the easiest option
45
00:03:18,870 --> 00:03:23,870
So if you're on Windows or Linux or Mac, you can install it from this page
46
00:03:23,870 --> 00:03:28,009
So depending on the operating system that you're on you want to click that. I'm on Windows
47
00:03:28,410 --> 00:03:33,679
So I'm gonna come down here to the windows option. You'll see there's little options for us to download this stuff
48
00:03:33,989 --> 00:03:41,159
So there's a bunch of options here. What we wanted download is going to be this one right here. It says code blocks
49
00:03:41,800 --> 00:03:44,550
And then the version number and then it says min
50
00:03:45,130 --> 00:03:47,080
GW - set up
51
00:03:47,080 --> 00:03:50,850
Basically what this is is it's gonna allow us to install code blocks
52
00:03:50,850 --> 00:03:56,489
So we're gonna be able to install that IDE program, and we're also going to be able to install AC
53
00:03:57,010 --> 00:03:59,640
Compiler. So remember I said we needed those two programs
54
00:03:59,640 --> 00:04:04,770
We needed those two things and this is actually going to give us both of them on Windows
55
00:04:04,900 --> 00:04:09,120
So I'm gonna come over here and click on the link to download this from sourceforge.net
56
00:04:09,340 --> 00:04:12,090
And you can see it's gone ahead and downloaded that
57
00:04:12,130 --> 00:04:17,189
So now let's head over to our downloads folder and I'm just gonna double click on this setup program
58
00:04:17,190 --> 00:04:20,040
so this should open up a window where we can set up the program and
59
00:04:20,169 --> 00:04:21,510
Let's just click through this
60
00:04:21,510 --> 00:04:22,830
So I'm just gonna click Next
61
00:04:22,830 --> 00:04:27,450
I'm gonna agree the license and basically just leave all the options as default and you should be good to go
62
00:04:27,880 --> 00:04:33,359
alright when that's done installing it's gonna ask us to run code blocks so we can just go ahead and do that and
63
00:04:33,550 --> 00:04:37,770
It's giving me this little window here. It says compilers auto detection
64
00:04:38,650 --> 00:04:41,190
So you can just click on the one up here. That's highlighted
65
00:04:41,190 --> 00:04:48,329
It says GNU GCC compiler and I'm just gonna click set as default and click OK and there you go
66
00:04:48,330 --> 00:04:50,430
We now have code blocks installed
67
00:04:50,950 --> 00:04:52,120
so in the next few tutorials
68
00:04:52,120 --> 00:04:58,200
We're gonna be learning about setting up code blocks writing our first C programs and really just getting started with C
69
00:04:58,200 --> 00:05:01,019
But for now, you have everything you need to get started
70
00:05:04,910 --> 00:05:11,059
In this tutorial I'm gonna talk to you guys about getting everything set up to start programming in C on OS X
71
00:05:11,060 --> 00:05:12,380
So if you're using a Mac
72
00:05:12,380 --> 00:05:17,450
Then this video will basically show you how to get everything set up in order to start working with C
73
00:05:17,450 --> 00:05:21,830
We're actually going to need two things. The first thing we're gonna need is a text editor
74
00:05:22,230 --> 00:05:22,730
basically
75
00:05:22,730 --> 00:05:29,089
We're going to use the text editor to write our C programs in and you can use any text editor that you want to write
76
00:05:29,090 --> 00:05:34,910
C programs but there's actually a special type of text editor which is called an IDE and it stands for
77
00:05:35,160 --> 00:05:39,499
Integrated development environment and it's basically a special environment where we can go to
78
00:05:40,320 --> 00:05:43,309
Write and run and manage our C programs
79
00:05:43,350 --> 00:05:49,909
So that's the first thing the second thing we're gonna need is what's called a C compiler now C is a programming language
80
00:05:49,910 --> 00:05:55,910
So essentially what we're doing when we're using it is we're writing instructions for the computer but in C
81
00:05:55,910 --> 00:06:01,790
We're writing instructions that us humans can understand and work with but in order for the computer to be able to carry them out
82
00:06:01,790 --> 00:06:04,099
they have to be compiled or
83
00:06:04,440 --> 00:06:11,210
Translated or transformed into a language that the computer can understand and that's what the C compiler is gonna do
84
00:06:11,210 --> 00:06:16,789
It's gonna take our C programs and essentially just translate them to a language that the computer can understand
85
00:06:17,130 --> 00:06:22,100
So the first thing I'm gonna do is show you guys how to install that compiler and then we'll get a text editor
86
00:06:22,380 --> 00:06:26,689
So what you want to do on your Mac is go over to the search bar and you're just gonna type in
87
00:06:27,420 --> 00:06:28,740
terminal and
88
00:06:28,740 --> 00:06:32,270
Click enter and a window that looks like this should pop up now
89
00:06:32,270 --> 00:06:38,119
The terminal is basically a program that allows us to interact with the computer using text commands
90
00:06:38,120 --> 00:06:41,750
So we're actually gonna need to use the terminal in order to do what we want to do
91
00:06:42,120 --> 00:06:48,709
so the first thing we want to do is check to see if you might already have AC compiler installed so in certain circumstances
92
00:06:48,990 --> 00:06:52,910
You might already have one. So you just want to type in C C
93
00:06:53,670 --> 00:06:55,670
- V and click enter and
94
00:06:56,400 --> 00:07:01,940
You'll see over here. I have AC compiler installed. So I'm getting all of this information. You can see over here
95
00:07:01,940 --> 00:07:05,779
It says like si Lang and it's giving me you know, a bunch of information
96
00:07:06,480 --> 00:07:10,819
If you have that, then you have your C compiler installed and you're ready to go
97
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 it
98
00:07:14,120 --> 00:07:16,120
So all you want to do is just type in
99
00:07:16,590 --> 00:07:17,800
Xcode
100
00:07:17,800 --> 00:07:21,990
select and then the space - - install and
101
00:07:22,450 --> 00:07:28,290
Click enter and you'll see I'm getting an error here because I already have these command line tools installed
102
00:07:28,750 --> 00:07:33,660
But if you don't already have them installed and obviously then you wouldn't have that C compiler
103
00:07:33,660 --> 00:07:39,119
Then this is gonna go off and install everything. You need to get that C compiler working
104
00:07:39,120 --> 00:07:44,609
so once you run that Xcode select command just type in CC - V again and
105
00:07:45,160 --> 00:07:47,489
You should be getting this version number
106
00:07:48,010 --> 00:07:50,519
Alright, so now that we have our C compiler installed
107
00:07:50,520 --> 00:07:52,980
What we want to do is download an IDE
108
00:07:52,980 --> 00:07:58,139
So we want to download a special text editor that we can use to write our C programs in so I'm gonna go over here
109
00:07:58,140 --> 00:08:05,460
To my web browser and I'm up here at a website called code blocks dot org, and this is an IDE called code blocks
110
00:08:05,460 --> 00:08:07,460
It's one of the most popular free
111
00:08:07,690 --> 00:08:13,230
IDE s for programming and C. So we're just gonna come down here to this downloads link and
112
00:08:14,680 --> 00:08:20,850
There should be a few options here download the binary released download the source code. We're gonna click download the binary release and
113
00:08:21,850 --> 00:08:22,830
You'll see down here
114
00:08:22,830 --> 00:08:28,440
We have a bunch of options for windows linux and mac click the mac option and if we come down here
115
00:08:28,440 --> 00:08:32,009
You'll see that here is what we need to install code blocks on the Mac
116
00:08:32,010 --> 00:08:37,890
So if you go over to the right side here, there's a link to SourceForge where we can download it
117
00:08:38,050 --> 00:08:40,050
So I just clicked that link and it should start downloading
118
00:08:40,570 --> 00:08:46,920
Automatically when that finishes downloading, let's head over to our downloads folder and you'll see here we have our zip file
119
00:08:46,920 --> 00:08:48,920
so I'm just gonna double click on this and
120
00:08:49,180 --> 00:08:51,599
They should give us code blocks now
121
00:08:51,600 --> 00:08:56,909
All you want to do is just drag and drop code blocks into your Applications folder and you're ready to go
122
00:08:56,910 --> 00:09:02,939
So you have code blocks installed and ready to start following along with this course and writing some awesome C programs
123
00:09:07,240 --> 00:09:11,799
In this tutorial, I'm gonna talk to you guys about getting our first C file setup
124
00:09:11,800 --> 00:09:17,889
So we're gonna load up a C file and we're gonna be able to get everything working and test out our program
125
00:09:17,889 --> 00:09:20,979
so I'm gonna open up my codeblocks program and this is the
126
00:09:21,679 --> 00:09:24,189
IDE that I'm gonna be using for the rest of this course
127
00:09:24,290 --> 00:09:29,259
So I'm just gonna double click on it and it should start opening once we have codeblocks open. You'll see over here
128
00:09:29,259 --> 00:09:33,128
There's a bunch of different options like create a new project open an existing project
129
00:09:33,470 --> 00:09:36,160
So we're actually going to want to create a new project
130
00:09:36,160 --> 00:09:38,980
So whenever we're gonna start writing some C files
131
00:09:38,980 --> 00:09:41,529
We're gonna want to create a new project in code blocks
132
00:09:41,629 --> 00:09:47,379
so you can either click this button right here, or you can go up to file new and
133
00:09:48,589 --> 00:09:52,539
projects so either way it's gonna bring you to this window over here and
134
00:09:53,089 --> 00:09:54,790
There's gonna be a bunch of options here
135
00:09:54,790 --> 00:09:55,009
basically
136
00:09:55,009 --> 00:10:01,928
Just these are different types of like C projects that we could create what we're gonna be doing is creating a console application
137
00:10:01,929 --> 00:10:06,759
This is like there's a basic C project that you can run on your computer. That's exactly what we need
138
00:10:06,759 --> 00:10:12,759
So click on console application and then click go and I'm just gonna click through this wizard and you'll see over here. It says
139
00:10:13,459 --> 00:10:16,959
C++ or C now C and C++ are both
140
00:10:17,600 --> 00:10:20,949
Different programming languages for this course, we're gonna be working with C
141
00:10:20,949 --> 00:10:24,128
So just highlight C and click Next and we're gonna give this a title
142
00:10:24,129 --> 00:10:27,789
I'm just gonna call mine draft and we're gonna have to put this project
143
00:10:28,009 --> 00:10:34,089
Inside of a folder so I'm gonna open this up and I'm just gonna put this on my desktop. So let's click OK and
144
00:10:34,639 --> 00:10:39,489
Click Next and you can leave all of these options as the default and we'll click finish
145
00:10:39,490 --> 00:10:41,420
so now what we should have is
146
00:10:41,420 --> 00:10:48,189
our first C project in code blocks now over here inside this little File Explorer you'll notice that
147
00:10:48,559 --> 00:10:54,189
Draf is right here. So that was the name of the project that I just created down. Here. We have this source folder
148
00:10:54,189 --> 00:10:58,659
So I'm going to click down and you'll see we have this file here called main dot C
149
00:10:58,660 --> 00:11:02,799
And this is a file that got automatically created for us by code blocks
150
00:11:02,799 --> 00:11:08,829
I'm just gonna right click and open it and you'll see over here. We have some default code so up here
151
00:11:08,829 --> 00:11:12,638
There's these little lines of code that say include down here. This says int main
152
00:11:12,860 --> 00:11:13,910
so this is the
153
00:11:13,910 --> 00:11:19,419
Program that we're given by default when we created our C project and this is sort of like the simplest C program that you can
154
00:11:19,520 --> 00:11:25,009
Right, essentially what this program does is it prints out hello world onto the screen
155
00:11:25,260 --> 00:11:29,569
So in order to run this program and test it just to make sure that everything's working
156
00:11:29,820 --> 00:11:32,360
I'm gonna come up here and you'll see that there's this
157
00:11:32,430 --> 00:11:36,830
Green play button over here and it just says run when I hover over it when I click this button
158
00:11:37,260 --> 00:11:42,020
This file that we have open right here main see is gonna get run
159
00:11:42,020 --> 00:11:46,400
So we're essentially telling code blocks to run this file and execute the program
160
00:11:46,430 --> 00:11:50,300
So when I click the Run button, we're getting this message. It says the project hasn't been built yet
161
00:11:50,300 --> 00:11:52,300
do you want to build it like yes, and
162
00:11:52,620 --> 00:11:55,429
Now over here this little window pops up
163
00:11:55,740 --> 00:12:02,570
Basically, this is the command prompt. So whenever I run a C program this little window is gonna pop up
164
00:12:02,570 --> 00:12:05,150
So if I was to exit out of this and run this program again
165
00:12:05,970 --> 00:12:12,769
You'll see this window pops up again. And it basically just executes the program and in the case of this program
166
00:12:12,770 --> 00:12:14,770
All we're doing is we're printing out
167
00:12:15,240 --> 00:12:22,550
Hello world onto the screen and that's actually what this instruction does over here. When we open up this command prompt. It's essentially just
168
00:12:23,130 --> 00:12:26,090
Executing the program and it's printing out whatever we told it to print out
169
00:12:26,090 --> 00:12:30,439
So it's printing out hello world over here and printing something out onto the screen is really easy
170
00:12:30,440 --> 00:12:33,679
And that's one instruction that we can give to the computer
171
00:12:33,750 --> 00:12:35,300
But as we go through this course
172
00:12:35,300 --> 00:12:39,979
we're going to learn all sorts of instructions that we can use but the purpose of this tutorial was just to get our
173
00:12:40,050 --> 00:12:44,839
C project set up and to test our main dot C file
174
00:12:44,840 --> 00:12:50,389
So as long as everything works here and you're good to go and now we can start really learning how to program in C
175
00:12:54,580 --> 00:12:59,229
In this tutorial I'm going to talk to you guys about the basics of writing a program in C
176
00:12:59,750 --> 00:13:04,089
So if you've been following along with the course up to this point, we've installed a text editor
177
00:13:04,089 --> 00:13:09,819
We've installed a C compiler and we set up our first C program. And our first C project
178
00:13:10,519 --> 00:13:18,159
Inside of code blocks. So now essentially what we have is this file over here. This is that main dot C file
179
00:13:18,160 --> 00:13:22,209
I just kind of want to point out a couple things that are in here first up here
180
00:13:22,209 --> 00:13:29,229
We have these little instructions which are just called include and I'm not gonna get too much into what those do in this tutorial
181
00:13:29,450 --> 00:13:34,809
Later on in the course. We're gonna talk about what these are actually doing and the specifics of you know,
182
00:13:34,810 --> 00:13:39,039
How they're helping our program, but for now just know that in order for us to use our program
183
00:13:39,040 --> 00:13:41,349
We're gonna need to have these guys up there
184
00:13:41,750 --> 00:13:45,309
next thing we have is this little block of code called main and
185
00:13:45,440 --> 00:13:48,789
It just has this name main you can see there's an open and closed parenthesis
186
00:13:49,160 --> 00:13:55,420
And this is actually what's called a method and don't worry too much about what methods are we're gonna again
187
00:13:55,420 --> 00:13:59,589
we're gonna talk more about those in future, but essentially a method is just like a
188
00:14:00,230 --> 00:14:02,740
container where we can put some of our code and
189
00:14:03,140 --> 00:14:08,829
This method main is very important because this is the method that's going to get executed
190
00:14:09,260 --> 00:14:13,779
when we run our program so in C we can actually write out a bunch of
191
00:14:13,850 --> 00:14:18,219
instructions for the computer and then we can run our program so we can tell the computer to
192
00:14:18,770 --> 00:14:22,629
Execute those programs and whenever the computer executes our C program
193
00:14:22,760 --> 00:14:25,119
It's gonna look inside of this main
194
00:14:25,520 --> 00:14:31,569
method in other words that's gonna look inside of all the code inside of these open and close curly brackets and it's gonna
195
00:14:31,730 --> 00:14:35,050
Execute that code so I have this instruction here
196
00:14:35,050 --> 00:14:40,539
It's called printf and basically this is just printing out some text onto the screen when we run our program
197
00:14:40,540 --> 00:14:47,260
It's gonna come into this main method and it's gonna execute all the instructions inside of here namely this printf method
198
00:14:47,260 --> 00:14:51,369
So if I was to run my program then this is gonna work now
199
00:14:51,370 --> 00:14:56,949
Let's talk about actually running a program whenever we want to run a program that we write in C
200
00:14:56,949 --> 00:14:58,339
We actually have to do two things
201
00:14:58,339 --> 00:15:03,009
The first thing we have to do is what's called building a program or compiling a program
202
00:15:03,500 --> 00:15:07,149
And basically what that does is it takes all of this C code
203
00:15:07,730 --> 00:15:12,649
It translates it into a language that the computer is going to be able to understand and execute
204
00:15:12,839 --> 00:15:16,069
So it's essentially taking this C program code
205
00:15:16,529 --> 00:15:20,809
Translating it into computer code and then the computer can execute it and run our program
206
00:15:20,810 --> 00:15:23,750
So the first thing we have to do is always build our C file
207
00:15:23,910 --> 00:15:27,440
The second thing we have to do is run the file that gets built
208
00:15:27,810 --> 00:15:30,079
so once we've built this program
209
00:15:30,079 --> 00:15:35,388
we've compiled it down into code the computer can understand we have to run it basically tell the computer to
210
00:15:35,790 --> 00:15:37,790
execute that code and in code blocks
211
00:15:37,790 --> 00:15:39,790
It's actually really easy to do this. So
212
00:15:39,839 --> 00:15:44,479
we can come over here and this little gear icon will allow us to build our program and
213
00:15:44,550 --> 00:15:49,760
then this play button will allow us to run our program but a lot of times when you're
214
00:15:50,100 --> 00:15:54,110
Programming you're gonna want to build and run your code at the same time, right?
215
00:15:54,110 --> 00:15:56,110
you're just gonna want to build it and run it just to see what
216
00:15:56,490 --> 00:15:57,230
Happened, you know?
217
00:15:57,230 --> 00:15:58,529
see how your program did
218
00:15:58,529 --> 00:16:04,789
So we have this option over here called build and run and this will both build your program and run it at the same time
219
00:16:04,790 --> 00:16:08,209
So generally as you're going through this course you're gonna want to be using this option
220
00:16:08,209 --> 00:16:10,369
That's build and run options when I click this
221
00:16:10,589 --> 00:16:18,018
it'll essentially build my program run it and then it'll show up here on this little window in this little window is basically a
222
00:16:18,540 --> 00:16:22,399
Console window you can call it. So a lot of people would refer to this as the console and
223
00:16:23,040 --> 00:16:24,959
It'll just like output any
224
00:16:24,959 --> 00:16:30,799
Information that we tell it to so if we write a C program that doesn't output information that doesn't print anything to the screen
225
00:16:30,930 --> 00:16:37,130
Then we wouldn't actually see this but since we're using this printf command, that window is gonna pop up
226
00:16:37,170 --> 00:16:42,199
So let's talk about how these programs actually get executed essentially when we write a program
227
00:16:42,240 --> 00:16:44,569
We have a set of instructions
228
00:16:44,610 --> 00:16:52,099
So when I'm writing a C program, I'm basically writing out a set of instructions imagine you were writing like a recipe or something
229
00:16:52,100 --> 00:16:57,740
Right a recipe is essentially just a set of instructions that when executed correctly will result in
230
00:16:57,899 --> 00:17:04,249
You know cooking or baking something delicious, right? And that's basically what these C programs are
231
00:17:04,250 --> 00:17:06,799
It's a set of instructions that when executed correctly
232
00:17:07,110 --> 00:17:11,660
will result in the computer doing something for us and one of the cool things about
233
00:17:11,760 --> 00:17:14,540
computers is that we can program them to do anything and
234
00:17:14,699 --> 00:17:19,579
C is one of the best languages to do that with so let's look at this instruction over here
235
00:17:19,579 --> 00:17:26,089
It's called print F and this prints something out onto the screen. We will notice over here at the end. We have this
236
00:17:26,640 --> 00:17:29,569
Semicolon and this semicolon is actually really important
237
00:17:29,570 --> 00:17:32,150
So whenever we write an instruction in C
238
00:17:32,280 --> 00:17:34,519
We always want to end it off with a semicolon
239
00:17:34,790 --> 00:17:40,069
That tells C that we're done with that instruction and we want to move on to the next one so I could actually copy this
240
00:17:40,260 --> 00:17:43,609
instruction and I could paste it down here and
241
00:17:44,190 --> 00:17:47,690
Now we're basically going to be executing this instruction twice
242
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 only printing it out once but we're also
243
00:17:52,919 --> 00:17:54,320
printing it out twice
244
00:17:54,320 --> 00:18:01,820
So when we run our program the program comes down into this main method and it looks at all the instructions inside of it
245
00:18:01,820 --> 00:18:05,240
So it starts with this first instruction. It executes it, right?
246
00:18:05,240 --> 00:18:09,559
So it prints out hello world onto the screen and then it also prints out a new line
247
00:18:09,559 --> 00:18:14,539
So this backslash n is like a newline character and that'll move the text onto a new line
248
00:18:14,850 --> 00:18:19,669
When it's done with this instruction, it moves on to the next instruction and it'll execute that instruction
249
00:18:20,160 --> 00:18:23,209
So as we go forward and we learn how to write in C
250
00:18:23,210 --> 00:18:25,790
We're gonna learn more and more complex instructions
251
00:18:25,799 --> 00:18:32,689
And we'll be able to use all those instructions in conjunction with each other to specify some seriously complex programs
252
00:18:32,690 --> 00:18:33,380
but for now
253
00:18:33,380 --> 00:18:38,059
This is really the only instruction that we know which is printf so why don't we use this printf?
254
00:18:38,490 --> 00:18:42,530
Instruction in order to draw out a little shape onto the screen
255
00:18:42,600 --> 00:18:46,400
So I'm going to show you guys how we can draw out like a little triangle onto the screen
256
00:18:46,830 --> 00:18:53,240
I'm gonna take this printf and I'm actually just gonna get rid of hello world and I'm gonna copy this a couple times
257
00:18:53,400 --> 00:18:57,739
so I'm just gonna copy it and we'll paste it down here, maybe four times and
258
00:18:58,620 --> 00:19:00,650
What I can do is I can specify
259
00:19:01,320 --> 00:19:04,850
Instructions to the computer that will tell it to draw out a particular shape
260
00:19:04,950 --> 00:19:07,640
So down here I could make like a forward slash and over here
261
00:19:07,640 --> 00:19:13,040
We'll make another one and I'll make another one and I'll make another one and you can see here
262
00:19:13,040 --> 00:19:19,249
I've drawn like a little diagonal line with these forward slashes. Now, I'm gonna draw vertical bars going all the way down
263
00:19:19,250 --> 00:19:21,030
So I'm gonna put one here
264
00:19:21,030 --> 00:19:24,499
I'm gonna put one here. I'll put one here and
265
00:19:25,260 --> 00:19:32,839
finally, why don't we draw some underscores and then we'll put one so I'm basically drawing out a little triangle shape and these
266
00:19:32,909 --> 00:19:39,179
Backslash ends are just printing out a new line. So anytime I print this it's gonna move it on to a new line
267
00:19:39,179 --> 00:19:43,288
So it'll print this guy out now on the line down the line below
268
00:19:43,289 --> 00:19:48,599
So if I was to save my program and execute it now the computer is going to go through it's going to execute each one
269
00:19:48,600 --> 00:19:53,549
Of these instructions in order and we'll be able to print out the shape onto the screen. So let's do that
270
00:19:53,650 --> 00:19:56,400
You see over here. We get our shape and
271
00:19:57,190 --> 00:19:59,760
One thing I really want to stress. Is that order matters?
272
00:19:59,760 --> 00:20:06,329
So if I took this instruction and I placed it up here as the first instruction now
273
00:20:06,330 --> 00:20:08,309
This is gonna get printed out first
274
00:20:08,309 --> 00:20:11,668
So when I run my program you'll see that we're printing out this kind of
275
00:20:11,830 --> 00:20:15,870
Funky looking shape and that's because we changed up the order of the instructions
276
00:20:15,870 --> 00:20:22,319
So the computer changed the order in which it executed those instructions and you'll notice here again, we're using this
277
00:20:22,840 --> 00:20:27,059
Semicolon after each line of code. So that's kind of the basics of writing a program
278
00:20:27,059 --> 00:20:29,489
That's essentially all you need to know when we're writing a program
279
00:20:29,620 --> 00:20:34,049
we're specifying a list of instructions that we want the computer to carry out and
280
00:20:34,450 --> 00:20:41,010
Depending on which instructions we give the computer and which order we give them and how complex the instructions are
281
00:20:41,380 --> 00:20:47,309
That'll determine what the program does and as we go through this course, we're gonna learn more and more complex instructions
282
00:20:47,320 --> 00:20:51,150
we're also going to learn how we can take simple instructions like printf and
283
00:20:51,220 --> 00:20:55,140
Do different things with them to make our programs a lot more powerful?
284
00:20:59,110 --> 00:21:03,250
In this tutorial I want to talk to you guys about variables in C
285
00:21:03,470 --> 00:21:08,589
Now when we're using C programming language a lot of times we're gonna be dealing with data
286
00:21:08,720 --> 00:21:15,970
so there's gonna be different data values or different information that we're gonna want to keep track of and use inside of our programs and
287
00:21:16,429 --> 00:21:24,339
Sometimes it can be difficult to maintain and keep track of all that data and that's where variables come in a variable is
288
00:21:24,590 --> 00:21:30,189
Essentially a container where we can store different pieces of information so different data values
289
00:21:30,290 --> 00:21:33,819
we could sort of things like numbers or texts or characters and
290
00:21:34,100 --> 00:21:39,850
These variables make it a lot easier for us to keep track of and manage all the data in our program
291
00:21:39,850 --> 00:21:45,699
So I'm gonna show you guys basically how variables can be useful and how we can use them in our C programs
292
00:21:45,799 --> 00:21:49,029
But down here. I have a very basic program. I'm basically just
293
00:21:49,549 --> 00:21:53,439
Printing out a little story. It says there once was a man named George
294
00:21:53,960 --> 00:22:01,449
He was 70 years old. He really liked the name George but did not like being 70. So this is a simple program
295
00:22:01,450 --> 00:22:04,059
We're just printing out a bunch of different lines of text
296
00:22:04,059 --> 00:22:05,540
and then when I run this program
297
00:22:05,540 --> 00:22:11,529
You'll see we print out the actual story so over here we have our story and this is great
298
00:22:11,630 --> 00:22:16,989
But let's say that I'm reading through my story. I'm reading through my program and I'm thinking to myself hmmm
299
00:22:16,990 --> 00:22:19,179
I think I want to change the character's name
300
00:22:19,790 --> 00:22:25,149
So instead of calling the character George, why don't we call him like John in order to make that change?
301
00:22:25,150 --> 00:22:29,949
I'm gonna have to look through my entire program and find every place where we mention the character's name
302
00:22:29,950 --> 00:22:36,130
So right here is one change it to John remember keep looking and here's another one. So we'll change this to John, right?
303
00:22:36,130 --> 00:22:40,660
So I had to manually go through and change every instance of the character's name
304
00:22:41,179 --> 00:22:44,798
To John and let's say that okay. That sounds pretty good
305
00:22:44,799 --> 00:22:49,749
But maybe I'm thinking I want to make the character a little bit younger. So instead of 70
306
00:22:49,750 --> 00:22:51,380
Why don't we make him 35?
307
00:22:51,380 --> 00:22:52,240
so now again
308
00:22:52,240 --> 00:22:59,740
I'm gonna have to look through my entire program find every place where we mentioned the character's age and change it to 35
309
00:22:59,740 --> 00:23:02,380
So there and there there you go
310
00:23:02,380 --> 00:23:04,989
I changed the program I updated and modified it now
311
00:23:04,990 --> 00:23:11,959
If I was to run this program then all that information will be updated and we'll have new story. Here's the problem though
312
00:23:12,000 --> 00:23:15,380
I'm dealing with a very short story. I mean, this is four lines
313
00:23:15,380 --> 00:23:17,510
We only mentioned the character's age and name twice
314
00:23:17,669 --> 00:23:23,839
But imagine if I was writing out a huge C program that had hundreds of lines in this story
315
00:23:23,909 --> 00:23:30,109
So story had hundreds of lines and maybe we mentioned the character's name hundreds of times. They're mentioned their age hundreds of times
316
00:23:30,110 --> 00:23:32,510
Well if I wanted to change the characters age your name?
317
00:23:32,510 --> 00:23:39,289
I would have to go through and manually change it in every single location that would take forever and it would be a huge track
318
00:23:39,570 --> 00:23:45,020
Basically, that's not a good way for us to manage and maintain the data and our programs
319
00:23:45,059 --> 00:23:48,319
All right. We basically have two pieces of data that we're working with
320
00:23:49,020 --> 00:23:51,649
Consistently in this program the characters age and the character's name
321
00:23:52,380 --> 00:23:59,779
What I want to show you guys now is how we can use variables to better keep track of and manage these pieces of information
322
00:24:00,539 --> 00:24:04,579
So remember a variable is basically just a container where we can store some data
323
00:24:04,919 --> 00:24:09,288
so I want to create two variables which will store the character's name and
324
00:24:09,480 --> 00:24:14,209
The character's age and I'll show you guys how this can make our program a lot easier to
325
00:24:14,520 --> 00:24:17,419
Manage up here above these print statements
326
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 I want to create a variable
327
00:24:22,049 --> 00:24:27,709
Now whenever we create a variable in C, we have to give C a couple pieces of information
328
00:24:28,020 --> 00:24:30,439
So we have to basically tell C a couple different things
329
00:24:30,600 --> 00:24:36,860
The first thing we have to tell C is what type of information we want this variable to store now
330
00:24:36,860 --> 00:24:42,079
I'm gonna get into the different data types that we can represent and see more in the next tutorial
331
00:24:42,080 --> 00:24:47,179
but for now just know that there's a bunch of different types of data that we can store and C we can store things like
332
00:24:47,190 --> 00:24:53,960
Numbers characters we can store text we can store decimal numbers all sorts of stuff like that in our case
333
00:24:53,960 --> 00:25:00,470
I'm going to store the character's name and the character's name is basically just a collection of characters
334
00:25:00,470 --> 00:25:02,600
So in order to create this variable
335
00:25:02,600 --> 00:25:07,130
The first thing I have to do is tell C that I want to create a variable that's gonna store characters
336
00:25:07,260 --> 00:25:12,319
So to do that, I'm just gonna type out char just like that and that'll basically tell C
337
00:25:12,320 --> 00:25:15,679
Hey, we're gonna store characters inside of this variable
338
00:25:16,289 --> 00:25:17,659
After we type out char
339
00:25:17,659 --> 00:25:20,569
I want to type out the name of the variable that I want to create
340
00:25:20,940 --> 00:25:25,440
Remember of variables that contain and it's a good idea to give these containers
341
00:25:25,780 --> 00:25:32,940
Descriptive names and those names will basically let us know what that variable is storing inside of it. So I'm gonna call this
342
00:25:33,850 --> 00:25:39,120
Character name and now what I want to do is I basically want to store multiple
343
00:25:39,490 --> 00:25:41,190
characters so over here
344
00:25:41,190 --> 00:25:43,349
I'm telling to see that I want to store a character
345
00:25:43,570 --> 00:25:47,789
But in C we can also store instead of just storing like one single character
346
00:25:47,790 --> 00:25:51,029
we can store a bunch of characters and that would be kind of like
347
00:25:51,070 --> 00:25:58,619
you know a name so there's like four characters in here in order to store a bunch of characters inside of this variable after we
348
00:25:58,690 --> 00:26:00,719
Type the character Abel's name
349
00:26:00,940 --> 00:26:03,570
We're gonna have to make an open and closed square bracket
350
00:26:03,700 --> 00:26:08,580
And that's gonna tell C that we want to store a bunch of characters inside this variable
351
00:26:08,800 --> 00:26:14,489
So I'm gonna type char a character name open a close square brackets. I'm gonna set this equal to something
352
00:26:14,490 --> 00:26:21,630
So I'm essentially assigning a value to this variable and I'm just gonna make an open and closed quotation marks and inside of here
353
00:26:21,630 --> 00:26:23,729
We're gonna type out the value that we want to store
354
00:26:23,730 --> 00:26:29,160
So in my case, I'm just gonna store the character's name, which is John and then as always in C
355
00:26:29,160 --> 00:26:31,410
We're gonna have to end this off with a semicolon
356
00:26:32,170 --> 00:26:32,820
All right
357
00:26:32,820 --> 00:26:34,570
so essentially what I did here was I
358
00:26:34,570 --> 00:26:38,759
Created a variable and I told C what I wanted to store in the variable
359
00:26:38,760 --> 00:26:43,499
I wanted to store a character. I gave this variable a name. It was character name and
360
00:26:44,080 --> 00:26:50,580
I used these open and close square brackets to sell C that I wanted to store a bunch of characters. Not just one
361
00:26:51,160 --> 00:26:54,719
Now what we can do is we can make another variable to store the character's age
362
00:26:55,120 --> 00:26:59,430
So in this case, we're gonna want to do something similar, but we're gonna want to store a number
363
00:26:59,770 --> 00:27:04,830
There's a bunch of different ways that we can store numbers and see but for our purposes we're storing a name
364
00:27:04,830 --> 00:27:11,399
so we're gonna use something called an integer and an integer is basically just a whole number so I'm just gonna type out int and
365
00:27:11,650 --> 00:27:15,570
That basically stands for integer. And now we're gonna give this a name so I'm gonna call this
366
00:27:16,330 --> 00:27:20,219
Character age and I'm gonna set this equal to a number
367
00:27:20,500 --> 00:27:23,219
So in our story the character is 35
368
00:27:23,220 --> 00:27:27,330
So I'm just gonna type out 35 and now we can again end this off with a semicolon
369
00:27:27,330 --> 00:27:33,119
So I have two variables here. This one's storing a collection of characters. This one's storing an integer
370
00:27:33,520 --> 00:27:37,679
Now the question becomes how can we use the inside of our program?
371
00:27:37,929 --> 00:27:38,429
well
372
00:27:38,429 --> 00:27:38,640
basically
373
00:27:38,640 --> 00:27:45,239
What we can do is we can print them out alongside of this text and we can actually use this print F
374
00:27:45,669 --> 00:27:50,278
Instruction in order to do that so I'm gonna show you guys how to do this and then we're gonna talk about it
375
00:27:50,279 --> 00:27:53,489
So basically, I'm gonna go over here where we have the character's name
376
00:27:53,490 --> 00:28:01,469
I'm gonna erase this and I'm gonna replace it with a % and an S and now over here I'm gonna make a comma and
377
00:28:02,020 --> 00:28:05,520
I'm just gonna type in the variable name. So I'm gonna type in
378
00:28:06,279 --> 00:28:11,398
character name and basically what's happening is when we use this percent s
379
00:28:11,620 --> 00:28:15,779
We're telling see that inside of this string of text here
380
00:28:15,940 --> 00:28:18,600
We're gonna want to insert a another
381
00:28:19,090 --> 00:28:25,199
String and a string is basically just a collection of characters so I could call this up here a string variable
382
00:28:25,510 --> 00:28:30,179
So when I say % s this is basically a placeholder and I'm telling C
383
00:28:30,179 --> 00:28:33,028
I'm gonna insert a string into here and
384
00:28:33,429 --> 00:28:38,999
Over here I use this comma and then I type out the string that I want to include so in our case
385
00:28:39,159 --> 00:28:42,569
It's character name and so basically over here
386
00:28:42,570 --> 00:28:45,120
It's gonna say there once was a man named
387
00:28:45,520 --> 00:28:50,849
% s and it's gonna replace % s with whatever we specify over here
388
00:28:50,850 --> 00:28:55,890
So it's gonna replace % s with the value that's stored inside that character name variable
389
00:28:55,890 --> 00:28:58,259
so I'm gonna save this and we'll run our program and
390
00:28:58,750 --> 00:29:03,119
Now you'll see that it's still printing out there once was a man named John
391
00:29:03,429 --> 00:29:07,229
Even though we didn't actually type out John over here
392
00:29:07,230 --> 00:29:13,110
We just typed % ass in the character name so I can do this in this other spot too. So down here
393
00:29:13,110 --> 00:29:17,969
We're also using the character's name so I can type % s and over here we can type
394
00:29:19,360 --> 00:29:22,589
Character name and again. This is going to act as a placeholder
395
00:29:23,110 --> 00:29:26,849
For this string of characters that we specify over here
396
00:29:27,010 --> 00:29:31,919
Let's do the same thing with the age so over here we have the age is 35
397
00:29:31,919 --> 00:29:34,379
I'm going to do something similar and I'm gonna say percent
398
00:29:34,630 --> 00:29:39,749
but now because I have a number instead of using percent s I'm gonna use
399
00:29:39,880 --> 00:29:45,030
Percent D and that basically means that we're gonna insert an integer number into here
400
00:29:45,030 --> 00:29:48,809
So I'm saying he was percent D years old and over here. I'm gonna make
401
00:29:49,049 --> 00:29:54,449
, and I'm just gonna type out the name of the variable that I want to be placed inside of here
402
00:29:54,639 --> 00:29:56,639
so I'm just gonna be
403
00:29:56,799 --> 00:30:01,859
Character age and we're going to do this one more time down here. I'm going to type percent D and
404
00:30:04,029 --> 00:30:07,919
Character age so essentially what I'm doing is I'm printing out the value
405
00:30:08,470 --> 00:30:11,610
That's inside of character age where I specify this
406
00:30:12,220 --> 00:30:13,330
placeholders so
407
00:30:13,330 --> 00:30:19,019
Let's go ahead and run our program and now you'll see it's gonna look exactly the same as it did before it's the same story
408
00:30:19,299 --> 00:30:21,719
the same name and the same age
409
00:30:22,239 --> 00:30:27,179
Except I didn't have to actually type out the character's age and the character's name inside the story
410
00:30:27,179 --> 00:30:30,689
I just referred to the variable and what's cool about?
411
00:30:30,909 --> 00:30:34,679
Variables is I could come up here and let's say I wanted to modify the character's age
412
00:30:35,109 --> 00:30:40,829
all I would have to do is modify it in this one spot so we could change the characters a name to like Tom and
413
00:30:40,830 --> 00:30:43,679
We could also change their age to like 67
414
00:30:43,679 --> 00:30:48,179
And now when I run my program all of that's gonna get updated in the story
415
00:30:48,179 --> 00:30:51,209
so now it's using the name Tom and the age of
416
00:30:51,759 --> 00:30:56,248
67 in our case. We only mention the character's name and the character's age a couple times
417
00:30:56,379 --> 00:31:00,868
But you can imagine if I was mentioning this variable like ten or twenty or thirty times
418
00:31:01,389 --> 00:31:05,878
Having to change it in each one of those individual spots would be extremely difficult
419
00:31:05,879 --> 00:31:11,009
So variables are great because we can store the value one time and then if we want to modify it
420
00:31:11,009 --> 00:31:17,309
We just modify it in one place. So another cool thing that we can do with these variables is we can actually modify them
421
00:31:17,309 --> 00:31:22,768
So let's say that halfway through the story. I wanted to change the character's age. I could actually come down here
422
00:31:22,769 --> 00:31:28,618
I can make a new line and right above these print statements. I could give this variable a new value so I could say
423
00:31:29,470 --> 00:31:34,109
character age and I can just set it equal to something else so we can set it equal to like 30 and
424
00:31:34,299 --> 00:31:39,868
Again, we want to include that semicolon. So now halfway through the story. The characters age is actually gonna change
425
00:31:40,330 --> 00:31:44,580
So you'll see it says there once was a man named Tom, he was 67 and then down here
426
00:31:44,580 --> 00:31:51,179
The age is being a 30 so we actually modified the value that was stored inside of the variable
427
00:31:51,609 --> 00:31:55,979
Halfway through printing out our story and that's really why variables are useful
428
00:31:55,980 --> 00:32:01,619
So that's kind of a basic overview and introduction into what variables are why they're useful how we can use them
429
00:32:02,230 --> 00:32:08,380
And over here again, we're storing a collection of characters, which is called a string and we're also storing this integer
430
00:32:08,600 --> 00:32:11,620
But there's some other data types that we can also represent
431
00:32:11,660 --> 00:32:16,540
So in addition to representing like a string or a number we can do some other stuff as well
432
00:32:16,540 --> 00:32:18,729
And I'm going to talk about that in the next video
433
00:32:18,730 --> 00:32:23,650
But for now, this has just been a basic overview and introduction into variables and see
434
00:32:27,290 --> 00:32:33,170
In this tutorial I want to talk to you guys about data types in C and in the C programming language
435
00:32:33,450 --> 00:32:35,180
We're gonna be dealing with a lot of data
436
00:32:35,180 --> 00:32:40,099
So generally when you're writing programs, you're gonna be dealing with all sorts of information and in this tutorial
437
00:32:40,100 --> 00:32:45,410
I just want to talk to you guys about what types of information we can represent and use
438
00:32:45,990 --> 00:32:49,280
Inside of our C program and these are called data types. All right
439
00:32:49,280 --> 00:32:53,629
So what are the different types of data that we can work with in our program?
440
00:32:53,630 --> 00:32:56,810
So I'm gonna show you guys all those different data types right now
441
00:32:56,850 --> 00:33:01,040
So a lot of times in C when we're working with data, we're gonna be storing that data
442
00:33:01,560 --> 00:33:02,940
inside of variables
443
00:33:02,940 --> 00:33:08,570
so I'm just gonna show you guys a bunch of different variables that we can create and all the different types of data that we
444
00:33:08,570 --> 00:33:10,570
Can store inside of those variables?
445
00:33:10,770 --> 00:33:15,410
so remember whenever we create a variable and C we have to tell C a couple pieces of
446
00:33:15,510 --> 00:33:19,640
information and the first piece of information we need to tell C is what type of
447
00:33:20,160 --> 00:33:24,920
Data that we want to store inside of that container inside of that variable
448
00:33:24,920 --> 00:33:26,930
so let's talk about the different types of
449
00:33:27,060 --> 00:33:27,570
data
450
00:33:27,570 --> 00:33:32,269
One of the most basic types of data and probably one of the types of data you're going to be using the most
451
00:33:32,430 --> 00:33:38,599
Gonna be numbers. There's actually two very important types of numbers that we can represent in C
452
00:33:38,970 --> 00:33:43,280
So the first type is called an integer and an integer is basically a whole number
453
00:33:43,280 --> 00:33:46,849
so it's a counting number right if I was counting on my fingers 1 2 3 4 5
454
00:33:47,120 --> 00:33:51,349
I'm just counting out whole numbers and I'm not saying like 1 1 and a half to
455
00:33:52,020 --> 00:33:55,219
2.3 like I'm counting in whole numbers and that's what an integer is
456
00:33:55,220 --> 00:33:57,620
So to create an integer we could just say int
457
00:33:57,750 --> 00:34:03,859
And we'll give this a name so we could say like age and we could set age equal to like 40 or something
458
00:34:04,350 --> 00:34:09,679
Now whenever we want to use an integer in C. We always just want to type out the number
459
00:34:09,679 --> 00:34:13,279
So you'll notice I'm not putting quotation marks or parentheses or anything around this
460
00:34:13,280 --> 00:34:19,429
I'm just typing out the number like that. And now we actually have our variable age
461
00:34:20,040 --> 00:34:22,550
another type of number we can create is a
462
00:34:23,159 --> 00:34:26,689
Decimal number so this would be a number unlike an integer
463
00:34:26,690 --> 00:34:31,460
So it would be like 2.5 or eight point six seven or seven point five three eight, right?
464
00:34:31,460 --> 00:34:36,409
It's a number that has a decimal point associated to it. So there's actually two types of
465
00:34:36,929 --> 00:34:38,809
decimal number in C
466
00:34:38,809 --> 00:34:41,799
there's something called a double and there's something called a
467
00:34:41,899 --> 00:34:45,759
Float, and I'm not gonna get too much into the difference between the two of them
468
00:34:45,770 --> 00:34:48,459
But as a beginner to see for the most part
469
00:34:48,460 --> 00:34:49,659
You're just gonna want to use a double
470
00:34:49,659 --> 00:34:55,689
So I'm just gonna type out double and this could be like someone's GPA. For example, it might be like a
471
00:34:56,929 --> 00:35:02,918
3.7 or whatever so, you know, you can use the double to represent decimal numbers
472
00:35:02,960 --> 00:35:07,510
it's also important to note that I could also represent like the number three so I could just say like
473
00:35:07,940 --> 00:35:12,129
3.0 and that's gonna be a decimal or I could say 40 point O
474
00:35:12,349 --> 00:35:17,409
So the difference between 40 and 40 point O is that forty point O is a double
475
00:35:17,410 --> 00:35:21,069
It's a decimal number and 40 without the decimal point is an integer
476
00:35:21,069 --> 00:35:25,299
So that's something just to keep in mind so you can make this like three point six
477
00:35:25,520 --> 00:35:30,759
So those are the two basic types of numbers integers and doubles and those are gonna come in handy all the time
478
00:35:30,950 --> 00:35:35,230
Now the next type of data that we're gonna want to store or is gonna be characters
479
00:35:35,390 --> 00:35:37,390
So in C we can store
480
00:35:37,550 --> 00:35:40,419
single characters inside of a variable so I could just say
481
00:35:40,550 --> 00:35:44,289
CH AR and that stands for character and we could give this a value
482
00:35:44,290 --> 00:35:48,219
So maybe it'd be like someone's grade and when we create a character in C
483
00:35:48,220 --> 00:35:53,679
We're basically going to specify the character that we want to store inside of single quotation marks
484
00:35:53,680 --> 00:35:59,559
So inside of these single quotation marks, I could put a letter I could put like a a lowercase a we could put h
485
00:36:00,140 --> 00:36:05,950
C d AF basically you can put any character that you want to store
486
00:36:06,049 --> 00:36:13,269
So if I put an A here now, this character variable grade is gonna store the single character a inside of it
487
00:36:13,339 --> 00:36:19,089
It's important to note that you can only store one character so I couldn't put like a whole bunch of characters over here
488
00:36:19,089 --> 00:36:20,480
And that's actually gonna throw an error
489
00:36:20,480 --> 00:36:28,209
All we can do is store one single character, but these three are the basic data types in C
490
00:36:28,210 --> 00:36:35,020
So these are the basic types of information that we can deal with in our C programs and just using these
491
00:36:35,299 --> 00:36:38,409
Three data types, we can represent all sorts of information
492
00:36:38,510 --> 00:36:43,329
I also want to talk to you guys about one sort of extra data type and it's not
493
00:36:43,790 --> 00:36:49,989
Technically a data type in C. It's technically like a modification of the character data type
494
00:36:50,539 --> 00:36:56,109
So you'll notice when we create a character we can only store one letter one single character inside of it
495
00:36:56,109 --> 00:36:59,258
But a lot of times we're gonna want to represent more than one
496
00:36:59,900 --> 00:37:05,920
Character so we're gonna want to represent like a string of characters and generally this is called a string
497
00:37:05,920 --> 00:37:13,029
so it's a collection of characters that are grouped together and we can use this char datatype in order to create a
498
00:37:13,279 --> 00:37:15,758
String so I can basically say char
499
00:37:15,759 --> 00:37:21,219
I can type out the name of the string that I want to create so I could say like phrase and then after this I
500
00:37:21,220 --> 00:37:27,279
can make an open and closed square brackets and this is basically gonna tell see that we want to
501
00:37:27,470 --> 00:37:29,649
string a bunch of characters together and
502
00:37:29,960 --> 00:37:35,799
What we're actually doing is creating something called an array and I'm gonna get into arrays later in the course
503
00:37:35,799 --> 00:37:40,419
But just for now know that we can create a string of characters by using
504
00:37:40,940 --> 00:37:42,230
char
505
00:37:42,230 --> 00:37:49,449
Specifying the name these open and close square brackets, and then we can create this using to open and closed quotation marks
506
00:37:49,450 --> 00:37:51,430
So instead of these single quotes up here
507
00:37:51,430 --> 00:37:55,930
We're just gonna use double quotes and I could type in like a phrase so I could type in whatever
508
00:37:56,089 --> 00:37:59,979
String of text I want here and this will get stored inside of this
509
00:38:00,349 --> 00:38:03,849
Variable which like I said is technically called an array now
510
00:38:03,849 --> 00:38:04,759
It's important to note that
511
00:38:04,759 --> 00:38:11,139
Doing something like this or creating this like string of characters is a little bit different from these guys up here
512
00:38:11,140 --> 00:38:13,930
So like these variables up here we could modify them
513
00:38:13,930 --> 00:38:17,470
We can give them new values but you can't do stuff like that with this
514
00:38:17,470 --> 00:38:23,379
So just know that this is like a special type of data and it's gonna act a little bit differently than these ones up here
515
00:38:23,539 --> 00:38:29,199
But that's one of the basics of working with data and those are all the basic datatypes that we're going to be using in
516
00:38:29,329 --> 00:38:31,329
our C programs
517
00:38:34,990 --> 00:38:38,949
In this tutorial I want to talk to you guys some more about the print F function
518
00:38:38,950 --> 00:38:40,950
so if you've been following along with this tutorial
519
00:38:41,390 --> 00:38:44,650
We've been using an instruction and our C programs called
520
00:38:44,930 --> 00:38:51,520
Printf and printf basically allows us to print different things out onto the screen in this tutorial
521
00:38:51,520 --> 00:38:56,770
We're going to talk more about what printf does and we're gonna look at how we can use it to print out things like variables
522
00:38:56,770 --> 00:39:00,939
And also things like different data like numbers and strings and all that stuff
523
00:39:01,369 --> 00:39:08,079
So in order to use printf we just type out PR int F and we're gonna make an open and closed
524
00:39:08,780 --> 00:39:10,780
parenthesis and a semicolon
525
00:39:11,060 --> 00:39:18,159
Now printf is what's called a function and basically what that means is it performs a specific task or a specific function
526
00:39:18,320 --> 00:39:23,619
in our case printf just print something out onto the screen write the task that it's doing is
527
00:39:23,839 --> 00:39:27,399
Printing something out and it basically works how you'd expect it to work
528
00:39:27,400 --> 00:39:28,630
So if I came in here?
529
00:39:28,630 --> 00:39:30,080
I could print out a
530
00:39:30,080 --> 00:39:33,069
specific piece of information and generally when we're using printf
531
00:39:33,170 --> 00:39:34,430
We're gonna be printing out
532
00:39:34,430 --> 00:39:39,790
Text so I can make an open and closed quotation marks and I could print out like hello world in here
533
00:39:39,859 --> 00:39:43,239
And now this will print out onto the screen. Hello and
534
00:39:43,940 --> 00:39:46,990
Inside of printf I can use all sorts of special characters
535
00:39:46,990 --> 00:39:50,770
so if I wanted to create a new line, for example, I can make a backslash N and
536
00:39:51,290 --> 00:39:55,359
Down here. I could type world and now this will print out a new line
537
00:39:56,000 --> 00:39:58,330
So you'll see hello world on different lines
538
00:39:58,910 --> 00:40:01,629
We can also use these special characters
539
00:40:01,630 --> 00:40:04,480
So for example, I could print out like a quotation mark
540
00:40:04,480 --> 00:40:08,919
So if I just printed a normal quotation mark right here, you'll notice that it's messing this up
541
00:40:08,920 --> 00:40:10,460
so it's sort of like
542
00:40:10,460 --> 00:40:16,839
counting this as the entire string if I wanted to change that I can just put a backslash here and now this will literally
543
00:40:17,000 --> 00:40:18,440
render a
544
00:40:18,440 --> 00:40:25,089
Quotation marks when I run my program you can see we have a quotation mark there so that's kind of like the basic usage
545
00:40:25,089 --> 00:40:25,300
right
546
00:40:25,300 --> 00:40:32,050
you can specify some text that you want to print out onto the screen and it'll do it but this printf function is actually
547
00:40:32,300 --> 00:40:36,249
Extremely powerful and we can use it to print out different pieces of information
548
00:40:36,859 --> 00:40:41,049
So in addition to just printing out plain text inside of these quotation marks
549
00:40:41,170 --> 00:40:44,920
I could also print out other types of information. For example, I could print out
550
00:40:45,809 --> 00:40:52,918
And if I wanted to print out a number I would have to use something called a format specifier a format specifier
551
00:40:53,109 --> 00:40:57,448
Basically tells this printf function that we want to print out a type of data
552
00:40:57,579 --> 00:41:00,178
That's not just plain text like this
553
00:41:00,669 --> 00:41:04,019
So if I said % and then I said D
554
00:41:04,389 --> 00:41:07,079
This would actually allow me to print out a number
555
00:41:07,079 --> 00:41:14,398
So generally when we use these format specifiers uses % and that tells see that you're going to want to print out something
556
00:41:14,529 --> 00:41:20,938
Special and then after that we would type in a specific letter and that'll tell see what exactly we want to print out
557
00:41:20,939 --> 00:41:25,109
so if I said % D this means that we want to print out an integer, so
558
00:41:25,749 --> 00:41:29,638
All I have to do now is I can make a comma and over here
559
00:41:29,639 --> 00:41:36,509
I can specify the integer that I want to print out so I could say like 500 and now this is gonna print out
560
00:41:36,880 --> 00:41:38,759
500 to the screen so you can see over here
561
00:41:38,759 --> 00:41:46,678
we're getting 500 and this is cool because we can interweave these different types of data with text so I could say like my
562
00:41:47,769 --> 00:41:49,769
favorite number is
563
00:41:50,319 --> 00:41:54,508
% D and this acts as a placeholder for this number over here
564
00:41:54,939 --> 00:41:58,109
so when I run my program now it says my favorite number is
565
00:41:58,929 --> 00:42:04,168
500 and that's really cool. We can also use multiple format specifiers
566
00:42:04,169 --> 00:42:11,819
so in addition to just using this percent d I can also specify another one so over here I could say percent ass and
567
00:42:12,279 --> 00:42:20,039
You'll notice that this is structured similarly to percent d % s is basically going to allow us to include some text
568
00:42:20,039 --> 00:42:22,498
So I can include some more text just like this
569
00:42:22,630 --> 00:42:26,639
so if I said % s I can make another comma here and I could say
570
00:42:27,849 --> 00:42:35,488
Number and now this is gonna print out my favorite percent s so it's gonna look for this string of text over here
571
00:42:35,489 --> 00:42:39,629
It's gonna insert that in and it's gonna say 500. So now when we run this
572
00:42:40,149 --> 00:42:42,149
it'll say my favorite number is
573
00:42:42,459 --> 00:42:49,799
500 and you'll notice here that I'm using multiple commas. So I'm saying like % asks % d and over here
574
00:42:49,799 --> 00:42:55,738
I have a comma and I'm specifying this string that I want to print so the string of characters and then the
575
00:42:56,169 --> 00:43:02,069
integer, and so basically the order that you put these format specifiers inside of your
576
00:43:02,319 --> 00:43:06,389
String of text is the order that you need to include them using these commas
577
00:43:06,390 --> 00:43:11,609
So you're going to separate each of them using a comma hoon addition to % s in % D
578
00:43:11,609 --> 00:43:13,269
We can also use a bunch of other ones
579
00:43:13,269 --> 00:43:18,869
And I'm going to talk to you guys about some of the most common % D will print out an integer like 500
580
00:43:18,869 --> 00:43:20,460
but if we wanted to print out a decimal number
581
00:43:20,460 --> 00:43:25,079
for example a double we could say percent F and this is gonna allow us to print out a
582
00:43:25,539 --> 00:43:29,309
Decimal number so I could say like 500 point nine eight seven five four
583
00:43:29,309 --> 00:43:34,679
And now this is gonna print this out on to the screen. Actually. I need to build this
584
00:43:35,769 --> 00:43:39,599
So my favorite number is five hundred point nine eight seven five four zero
585
00:43:39,900 --> 00:43:47,819
So essentially you can use these print FS and you can use these format specifiers in order to include different data types
586
00:43:48,279 --> 00:43:49,799
into your output
587
00:43:49,799 --> 00:43:54,419
So like this is just some general text output and we can use these different guys to interweave
588
00:43:54,549 --> 00:44:01,139
Like numbers or you know other strings and stuff like that and what are these really become useful is when we have variables
589
00:44:01,140 --> 00:44:07,769
So if I was to create like a number variable up here, I can call it like fav num set it equal to 90
590
00:44:07,769 --> 00:44:15,179
I can use this variable and print it out inside of this printf so over here we could make this percent D and
591
00:44:15,579 --> 00:44:20,369
Instead of printing out this number we can just access that variable so I could say fav num
592
00:44:20,369 --> 00:44:26,008
And this will allow me to print out that variable using that printf function. So my favorite number is
593
00:44:26,529 --> 00:44:29,129
90 so that's the basics of using printf
594
00:44:29,130 --> 00:44:34,619
This is a very awesome tool that we can use and printf is basically just used for us to find out information
595
00:44:35,109 --> 00:44:39,209
So when we're running our programs a lot of times we're gonna want to be able to get some information
596
00:44:39,400 --> 00:44:43,769
About what's going on about what they're doing in printf can allow us to do that
597
00:44:43,769 --> 00:44:49,529
So don't be afraid to use these different access modifiers. Like I said % s % d % F
598
00:44:49,529 --> 00:44:55,589
Those are all sort of the common ones and you can also use % C and this will allow you to print out a single
599
00:44:55,779 --> 00:44:59,489
Character so I can come over here create like a character variable
600
00:45:00,430 --> 00:45:04,109
We'll call it my char and just set it equal to like lower case
601
00:45:04,109 --> 00:45:11,788
I and I could print this out using % C. So now we could say my char and you'll see that we print
602
00:45:12,830 --> 00:45:14,570
so
603
00:45:14,570 --> 00:45:20,410
Play around with printf. It's extremely useful and it's really going to come in handy when we're writing more complex programs
604
00:45:25,270 --> 00:45:26,020
In this tutorial
605
00:45:26,020 --> 00:45:32,619
I want to talk to you guys about working with numbers and see now a lot of times when we're writing programs in C
606
00:45:32,619 --> 00:45:38,709
We're gonna want to do different things with numbers whether that's just storing numbers and keeping track of them or adding multiplying
607
00:45:38,869 --> 00:45:43,359
Subtracting doing math with numbers or you know, really just dealing with numbers in general
608
00:45:43,360 --> 00:45:47,889
So I'm just gonna give you guys a basic overview of a lot of the stuff we can do with numbers
609
00:45:47,890 --> 00:45:54,460
We're also gonna talk about how different number types in Iraq. So how like integers and doubles interact and all that fun stuff
610
00:45:55,010 --> 00:46:01,600
So down here I am printing out a number so you can see I'm using this printf function and inside of these quotation marks
611
00:46:01,600 --> 00:46:02,119
I'm
612
00:46:02,119 --> 00:46:03,710
Basically saying % F
613
00:46:03,710 --> 00:46:09,790
And % f stands for like a floating-point number so that could be either a double or a float basically a decimal number
614
00:46:10,190 --> 00:46:15,190
so we put % F in here that's gonna allow us to print out a decimal number just like this and
615
00:46:15,290 --> 00:46:21,459
You can see I can run my program and it'll print out 8.9 and you'll notice that it's printing out not just 8.9
616
00:46:21,460 --> 00:46:23,919
But it's also printing it out to a very precise
617
00:46:24,050 --> 00:46:28,419
decimal point and that's what C is always gonna do so it's always gonna print it out to you know,
618
00:46:28,420 --> 00:46:33,159
A very precise decimal. So using these decimals I can do all sorts of stuff
619
00:46:33,200 --> 00:46:38,919
So obviously I can just print out the decimal but I could also do things like math so I could say like five point zero
620
00:46:39,320 --> 00:46:41,499
plus four point five and
621
00:46:42,140 --> 00:46:45,340
This is gonna go ahead and be able to do that for us when I run my program
622
00:46:45,800 --> 00:46:52,659
Not only is it gonna like print this out but it's gonna do the actual math operation and you see we get nine point five
623
00:46:53,030 --> 00:46:58,359
so in addition to addition we can also use subtraction division, which is going to be a forward slash and
624
00:46:59,240 --> 00:47:04,479
Multiplication which is going to be in asterisks and you can use all these different things to let you know do the four basic math
625
00:47:04,580 --> 00:47:09,429
Operations addition subtraction multiplication and division. So now I want to talk about how we can use
626
00:47:10,040 --> 00:47:12,609
floating-point numbers so like doubles and floats
627
00:47:13,130 --> 00:47:16,690
Alongside integers. So let's say I was going to add an integer
628
00:47:17,300 --> 00:47:20,830
With a double. So if I said 5 plus
629
00:47:21,440 --> 00:47:26,919
4.5. Well, actually when we do this this entire answer is going to get converted into a
630
00:47:27,710 --> 00:47:34,300
Decimal number so when I say 5 which is an integer plus 4.5. We're going to get a floating-point number back
631
00:47:34,300 --> 00:47:35,960
so we're going to get
632
00:47:35,960 --> 00:47:37,150
9.5
633
00:47:37,150 --> 00:47:43,750
and basically any operation that you do between an integer and a floating-point number is going to return a
634
00:47:44,089 --> 00:47:45,799
floating-point number back
635
00:47:45,799 --> 00:47:50,139
So that's important to realize but if I was to change this to four now
636
00:47:50,140 --> 00:47:55,270
This isn't gonna work anymore because this is asking for a floating-point number and we're giving it an integer
637
00:47:55,430 --> 00:47:57,490
So when I do math with two integers
638
00:47:57,490 --> 00:48:02,319
it's going to give us an integer back and if I do math with a floating point number and integer it will give us a
639
00:48:02,319 --> 00:48:08,048
Floating-point number back. It's also important to realize for example if I took five and divided it by four
640
00:48:08,420 --> 00:48:13,569
This should actually be a decimal number right and you'd expect us to get a decimal back
641
00:48:13,569 --> 00:48:20,109
But if I just print it out percent D. So I printed out the result of the integer five divided by the integer four
642
00:48:20,119 --> 00:48:22,029
I'm actually gonna get an integer back
643
00:48:22,029 --> 00:48:28,989
So when I run the program you'll see we're getting a one here, but really this like the answer is one technically
644
00:48:28,990 --> 00:48:33,579
But it's one with a bunch of decimal points after it. Right? It's one plus a bunch of other stuff
645
00:48:34,099 --> 00:48:36,729
But when we just do math with two integers
646
00:48:36,730 --> 00:48:43,389
we're gonna get an integer back if I was to say five divided by four point zero and I've printed out a
647
00:48:43,579 --> 00:48:47,919
Decimal now, we're gonna get the entire answer back. So we're gonna get one point two five
648
00:48:48,529 --> 00:48:52,779
So that's just a little bit about dealing with integers and floating point numbers
649
00:48:52,779 --> 00:48:55,689
We could also take these and put these into variables
650
00:48:55,690 --> 00:49:00,159
so, you know in addition to just having numbers down here I could specify a variable like
651
00:49:00,260 --> 00:49:07,239
You know num set it equal to six and we can go ahead and print num out down here as well
652
00:49:07,240 --> 00:49:09,240
I need to make this a D
653
00:49:09,289 --> 00:49:13,269
So now I will be able to print out that variable with no problem
654
00:49:13,270 --> 00:49:17,829
So it can be really useful sometimes to store these numbers inside of variables
655
00:49:17,829 --> 00:49:20,229
I also want to talk to you guys about using more complex
656
00:49:21,140 --> 00:49:23,440
mathematical functions now in C
657
00:49:23,440 --> 00:49:24,859
We have these things called
658
00:49:24,859 --> 00:49:30,909
Functions which are basically just little blocks of code that we can call and when we call them they're gonna go off and do
659
00:49:31,039 --> 00:49:38,079
Certain things for us. So they're like modify a value or give us information about a value and these functions can be really useful
660
00:49:38,270 --> 00:49:41,709
I'm gonna show you guys a couple functions that we can use with math
661
00:49:42,619 --> 00:49:47,828
So I'm just going to come over here and I can actually start using these functions and all you have to do is just type
662
00:49:47,829 --> 00:49:50,089
Out the name the function that you want to use
663
00:49:50,190 --> 00:49:55,970
So by default C is going to give us access to a bunch of math functions that we can use
664
00:49:55,970 --> 00:49:57,349
So we really don't have to do anything
665
00:49:57,349 --> 00:50:00,739
all we have to do is just type in their names and I'm gonna show you guys want it's just
666
00:50:01,440 --> 00:50:03,140
POWs, so I'm just typing in P
667
00:50:03,140 --> 00:50:07,700
Oh w then I'm gonna type an open and closed parenthesis and inside of these parentheses
668
00:50:07,770 --> 00:50:10,009
I can actually give this two numbers
669
00:50:10,080 --> 00:50:14,749
what this is gonna do is it's gonna take the first number that we give it and it's gonna take it to the
670
00:50:14,880 --> 00:50:17,150
second number so if I said like 2 & 3
671
00:50:18,030 --> 00:50:23,449
And you'll notice I'm separating them with a comma this is basically gonna give us 2 raised to the third power
672
00:50:24,000 --> 00:50:29,659
So to give us 2 cubed so when I run my program and actually we need to print this out as a decimal number
673
00:50:29,670 --> 00:50:34,010
So this is gonna give us a decimal number back. So I need to print this out with F
674
00:50:34,619 --> 00:50:39,439
When I run my program we're gonna get 8 back just like that. So it's basically
675
00:50:40,020 --> 00:50:44,689
Cubing 2 I could say like 4 raised to the third power and now we should get back
676
00:50:45,450 --> 00:50:47,400
64 which we do
677
00:50:47,400 --> 00:50:52,010
So that can be pretty useful. There's a couple other ones. I'll show you another one is SQ RT
678
00:50:52,010 --> 00:50:54,560
So this will allow you to take a number square root
679
00:50:54,560 --> 00:50:59,719
So if I said like 36 in here now, we're gonna print out the square root of 36
680
00:50:59,720 --> 00:51:02,570
Which is gonna be 6 and there's a couple other ones
681
00:51:02,570 --> 00:51:08,059
So let's say that I had a decimal I could say C EIL which stands for ceiling
682
00:51:08,160 --> 00:51:12,680
So if I have like thirty six point seven or 36 point three five six
683
00:51:13,170 --> 00:51:19,159
This is gonna give us the hot the next highest number. So it'll basically round this number up and
684
00:51:19,770 --> 00:51:21,770
this is gonna give us
685
00:51:21,930 --> 00:51:23,670
37 as you can see
686
00:51:23,670 --> 00:51:28,280
We can do another one which is called floor and floor. We'll do the opposite so floor
687
00:51:28,280 --> 00:51:34,519
We'll just round the number down no matter what. So if I have like 36 point six five six, this will round it down to
688
00:51:35,430 --> 00:51:36,720
36
689
00:51:36,720 --> 00:51:39,409
So there's all sorts of little, you know things we can do
690
00:51:39,930 --> 00:51:44,359
little functions that we can call and get information about numbers and like I said these
691
00:51:44,790 --> 00:51:48,979
Functions will either like modify a number so it would like give us the floor or the ceiling
692
00:51:49,320 --> 00:51:52,639
Or sometimes they'll like give us information about numbers
693
00:51:52,640 --> 00:51:55,580
So that's sort of the basics of working with numbers, you know
694
00:51:55,580 --> 00:51:59,769
you can do all sorts of math with numbers you can add in some to multiply and you can even
695
00:52:00,020 --> 00:52:05,050
Multiply add subtract integer numbers and floating-point numbers. So play around with all these
696
00:52:05,300 --> 00:52:09,070
different things with numbers and if you want to find some more of these little math functions
697
00:52:09,070 --> 00:52:13,570
All you have to do is just go online and Google search for C math functions
698
00:52:13,570 --> 00:52:17,979
there's like dozens of these little functions that you can use, but I think these are some of the most
699
00:52:18,530 --> 00:52:20,530
widely used
700
00:52:23,299 --> 00:52:24,079
In this tutorial
701
00:52:24,079 --> 00:52:31,038
I want to talk to you guys about using comments and see well comment is a special block of code and see
702
00:52:31,259 --> 00:52:35,988
Which actually gets ignored when we run our program, so if you want to write a comment
703
00:52:35,989 --> 00:52:39,799
you can use a special starting and ending tag and any of the
704
00:52:40,079 --> 00:52:47,359
Text any of the code anything that you put inside of that tag isn't gonna get executed or really looked at by C
705
00:52:47,519 --> 00:52:50,299
So we can use comments to do all sorts of things, you know
706
00:52:50,299 --> 00:52:55,248
You can leave little notes or little comments inside of your program. You can use comments to
707
00:52:55,919 --> 00:52:59,539
Temporarily disable certain lines of code. They can be really useful
708
00:52:59,880 --> 00:53:05,959
So I'm gonna show you guys the basics down here in my program if I want to create a comment I can start a comment
709
00:53:05,959 --> 00:53:12,979
Using a forward slash and an asterisk and you'll see when I put this asterisks in all the code in my text editor changed color
710
00:53:13,169 --> 00:53:20,029
and basically what this means is anything that comes after this forward slash and Asterix is gonna be considered a comment in
711
00:53:20,130 --> 00:53:25,849
Order to close off the comment. I can use an another asterisk and another forward slash
712
00:53:26,099 --> 00:53:28,998
You'll see the code changed back to normal code
713
00:53:29,189 --> 00:53:35,959
Inside of these two asterisks is I can basically write out anything I want and it's gonna get considered as a comment
714
00:53:35,959 --> 00:53:37,399
so for example
715
00:53:37,399 --> 00:53:44,629
If I you know wrote out like my program and I ran my program this whole thing is gonna get ignored by C
716
00:53:44,819 --> 00:53:47,028
So it's just gonna print out comments or fun
717
00:53:47,029 --> 00:53:49,488
It's not gonna do anything else and you know
718
00:53:49,489 --> 00:53:53,059
Like I said comments are really useful so I could leave a little note up here
719
00:53:53,059 --> 00:53:59,778
I could write something like to do like I have to do something and a lot of times people we use comments to
720
00:54:00,209 --> 00:54:07,069
Explain certain lines of code so I could put a comment here and I could say like this print out text, right?
721
00:54:07,069 --> 00:54:10,638
So maybe you know, this was like a really complex line of code
722
00:54:10,679 --> 00:54:13,819
We could use this text in order to explain it
723
00:54:13,819 --> 00:54:18,079
You can also use comments to do something called commenting out a line of code
724
00:54:18,239 --> 00:54:23,749
So for example, let's say I wanted to run my program but I didn't want to have this line of code in there
725
00:54:23,969 --> 00:54:27,229
Well one thing I could do would just be to delete the entire line of code
726
00:54:27,229 --> 00:54:32,299
Right so I could delete this code and now I can run my program and it's no longer gonna print it out
727
00:54:32,299 --> 00:54:37,399
It's no longer gonna do anything. But here's the problem in order to do that. I had to actually delete the of code
728
00:54:37,560 --> 00:54:42,469
so instead of deleting it I could just bring it back and I could comment it out so I could basically
729
00:54:43,230 --> 00:54:45,770
surround it with these comment tags and
730
00:54:46,320 --> 00:54:51,859
Now the computer is just gonna ignore it. So it's not gonna render it because it's been commented out
731
00:54:52,230 --> 00:54:56,510
So now when I run my program it's gonna do the same thing. It won't execute that line of code
732
00:54:57,060 --> 00:55:00,769
But I didn't have to actually delete the line. I could just comment it out
733
00:55:00,770 --> 00:55:02,869
So a lot of times when you're writing programs
734
00:55:02,910 --> 00:55:06,410
You might want to try to run your program without a certain line or a certain
735
00:55:06,410 --> 00:55:12,530
you know series of lines of code and instead of deleting them and then running the program you can just comment them out and
736
00:55:13,079 --> 00:55:15,769
See will basically ignore them so that can be really useful
737
00:55:16,260 --> 00:55:20,719
So those are the two basic uses for comments and essentially comments are open, right?
738
00:55:20,720 --> 00:55:25,699
It's just a way that we can write out information in our files. That won't get rendered by C
739
00:55:25,740 --> 00:55:29,000
And so you can really use comments to do whatever you want
740
00:55:29,070 --> 00:55:33,109
Now I will say one best practice with comments is to use them sparingly
741
00:55:34,170 --> 00:55:40,940
Generally, you only want to use a comment when you absolutely have to so we can be really annoying if you open up a C
742
00:55:40,940 --> 00:55:45,770
File and there's like comments littered all around and obviously if you want to put a bunch of comments in your code
743
00:55:45,770 --> 00:55:50,089
I mean no one's stopping you but as a best practice going forward
744
00:55:50,089 --> 00:55:56,089
I think generally only using comments when they're absolutely necessary is kind of like a good practice
745
00:55:56,089 --> 00:55:59,209
But again, it's open, you know, you can use comments for whatever you want
746
00:56:03,170 --> 00:56:07,280
In this tutorial I want to talk to you guys about constants in C
747
00:56:07,559 --> 00:56:12,979
Well constant is a special type of variable and C which can't be modified
748
00:56:13,140 --> 00:56:17,809
So when I create a constant and C, I'm basically creating a value that is
749
00:56:18,329 --> 00:56:24,079
Unable to be modified. So I'm gonna show you guys basically how this works out. Let's say I created a variable
750
00:56:24,210 --> 00:56:28,069
Let's say I created an integer and I just called it num and I set it equal to 5
751
00:56:28,140 --> 00:56:34,819
right, and then I printed out that integer onto the screen so we can print this out percent D and
752
00:56:35,460 --> 00:56:40,819
We're just gonna pranaam right so I can create this num variable. I can print it out onto the screen
753
00:56:41,069 --> 00:56:42,290
Let's say that down here
754
00:56:42,290 --> 00:56:46,939
I wanted to modify num so I could basically say num is equal to 8 so I can modify
755
00:56:47,160 --> 00:56:49,520
The value that's stored inside of the num variable
756
00:56:50,369 --> 00:56:53,328
And let's come down here and we'll print this out as well. So
757
00:56:54,420 --> 00:56:55,589
d
758
00:56:55,589 --> 00:56:59,209
And we'll print out num again. And actually I'm gonna print out a new line here
759
00:57:00,270 --> 00:57:05,629
So when I go ahead and run this program and I forgot to send me : when I go ahead and run this program
760
00:57:06,150 --> 00:57:12,650
you'll see we're printing out five and then we modified the value to eight and then we printed out eight so I was able to
761
00:57:13,500 --> 00:57:16,010
modify this value down here
762
00:57:16,040 --> 00:57:17,400
I was Mabel to modify num
763
00:57:17,400 --> 00:57:24,650
But in certain circumstances and see you're gonna want to create variables or you're gonna want to create values that can't be modified
764
00:57:24,990 --> 00:57:28,879
So these would be values that just like can't change and those are called constants
765
00:57:28,920 --> 00:57:32,690
So if I wanted to make a num a constant in other words, let's say for some reason
766
00:57:32,730 --> 00:57:36,799
I didn't want the num variable to be able to be modified in my program
767
00:57:36,799 --> 00:57:38,799
I could come over here and I could say
768
00:57:39,030 --> 00:57:46,429
Const so Co NS T and I'm gonna say that right before I declare the type now, you can also say Const
769
00:57:47,220 --> 00:57:51,409
Right after you declare the type, but I've always preferred to just put it before
770
00:57:52,319 --> 00:57:59,899
So I say Const int num 5 and now this program is actually gonna throw an error. So when I try to run this
771
00:58:00,599 --> 00:58:06,919
You'll see down here. It's getting highlighted in red. That's because I can't modify a constant
772
00:58:06,920 --> 00:58:09,109
So I'm trying to modify a num down here
773
00:58:09,299 --> 00:58:14,569
But I can't because I gave it this Const keyword because it's now considered a constant
774
00:58:14,579 --> 00:58:19,489
So in a lot of cases you want have variables that just can't be modified. Can't be changed and
775
00:58:19,740 --> 00:58:25,130
In a situation like that you want to make them consonants also a lot of times when we make constant variables in C
776
00:58:25,170 --> 00:58:28,850
We'll give them all uppercase names and this isn't required
777
00:58:28,850 --> 00:58:33,199
this is just sort of like what a lot of developers will do so they'll say like instead of n
778
00:58:33,600 --> 00:58:38,959
Um like this they would say like num or maybe this is like your favorite number so you could say fav
779
00:58:39,240 --> 00:58:40,320
underscore num
780
00:58:40,320 --> 00:58:42,830
So that would be how you could like distinguish two words
781
00:58:43,050 --> 00:58:48,919
but generally like constants will be all capital and that's just so it's kind of obvious that they're
782
00:58:49,410 --> 00:58:54,200
Unchangeable that they're constants. But again, you don't have to do that. But that's kind of like a best practice
783
00:58:54,360 --> 00:58:56,569
so this is one way that we can create a
784
00:58:56,820 --> 00:59:03,409
constant, and we basically create a constant variable that can't be changed but a constant could also refer to
785
00:59:03,600 --> 00:59:07,519
Just like any number or any text that we're using in our program
786
00:59:07,520 --> 00:59:12,889
So, for example, I'm gonna get rid of this if I came down here and I just like printed out some text
787
00:59:13,050 --> 00:59:18,979
So let's say I just printed out like Hello. This is actually also gonna be considered a constant
788
00:59:18,980 --> 00:59:21,620
So this is just like a string of characters a string of text
789
00:59:21,900 --> 00:59:28,310
That's also considered a constant because it's just like text on the screen. We can't change it. We can't modify it
790
00:59:28,310 --> 00:59:29,670
It's just kind of there
791
00:59:29,670 --> 00:59:31,380
so you would also consider this a
792
00:59:31,380 --> 00:59:35,420
Constant if I was printing out a number to like let's say I came over here and print out
793
00:59:36,780 --> 00:59:39,860
77 T is also considered a constant
794
00:59:39,960 --> 00:59:45,889
So it's just considered to be like a piece of information or a piece of data in our program
795
00:59:45,890 --> 00:59:49,400
That is sort of unchanging. Right? So 70 this isn't gonna change
796
00:59:49,410 --> 00:59:52,220
I mean unless I physically came in here and changed it like
797
00:59:52,380 --> 00:59:59,060
This value is always gonna be 90 no matter what I can't modify it unless I like manually come in and modify it
798
00:59:59,060 --> 01:00:01,879
So that's also considered a constant
799
01:00:02,370 --> 01:00:07,249
But I would say like for the most part like where this is going to come in handy is obviously doing stuff like this
800
01:00:07,250 --> 01:00:11,239
But creating constant variables variables that can't change
801
01:00:15,730 --> 01:00:20,500
In this tutorial I'm gonna show you guys how to get input from a user in C
802
01:00:20,660 --> 01:00:22,150
So a lot of times in our C program
803
01:00:22,150 --> 01:00:28,119
So we're going to be working with all different types of information and a lot of times we're gonna want to get some of that
804
01:00:28,400 --> 01:00:34,060
Information from the user so I'm going to show you guys how we can prompt the user to enter in some information
805
01:00:34,550 --> 01:00:40,239
We can take that information store it inside of variables and then we can use it inside of our program
806
01:00:40,240 --> 01:00:42,340
So this is gonna be pretty cool. So down here
807
01:00:42,340 --> 01:00:48,009
I will basically write out the code for this and the first thing we want to do whenever we're getting input from the user is
808
01:00:48,140 --> 01:00:52,900
We want to prompt them. So I want to prompt the user, you know, as far as what they should enter in
809
01:00:52,970 --> 01:00:58,389
So I'm just gonna say printf and over here. I'm just gonna print out a prompt. So why don't we
810
01:00:59,180 --> 01:01:04,659
Ask the user to enter in their age. So well, I'll show you guys how we can get a number from the user
811
01:01:04,660 --> 01:01:07,480
so I'm just gonna say enter your age and
812
01:01:08,390 --> 01:01:11,980
Now that we've prompted them to enter their age I need to do two things
813
01:01:12,440 --> 01:01:17,409
So the first thing I want to do is create a variable where we can store
814
01:01:18,020 --> 01:01:20,709
the input that the user puts into the program
815
01:01:20,710 --> 01:01:25,570
So I want to create a variable where we can store the age that the user inputs
816
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 not gonna give this a value
817
01:01:30,430 --> 01:01:32,430
so all I'm gonna do up here is just
818
01:01:32,630 --> 01:01:36,670
Declare the variable. I'm just gonna tell see that I want to use this variable
819
01:01:36,670 --> 01:01:43,690
But I'm not gonna give it a value in other words. I'm gonna allow the user who's inputting the age to give this a value
820
01:01:44,240 --> 01:01:49,270
So down here I want to be able to get input for from the user so I can use another function which is called
821
01:01:49,790 --> 01:01:51,500
scanf and
822
01:01:51,500 --> 01:01:57,340
scanf is basically going to allow the user to enter in some information into our program and
823
01:01:57,619 --> 01:02:02,829
This works similar to printf it's kind of doing the opposite of printf right printf is printing something onto the screen
824
01:02:02,900 --> 01:02:05,139
Scanf is allowing the user to input something
825
01:02:05,660 --> 01:02:10,240
Into the program and we're gonna make an open and close quotation marks and in here
826
01:02:10,240 --> 01:02:16,030
I basically want to tell see what type of information I'm asking the user to enter in
827
01:02:16,030 --> 01:02:19,419
So in our case we're asking for an integer right age is going to be an integer
828
01:02:19,420 --> 01:02:24,100
it's a whole number so I'm gonna accept as an input and integer and
829
01:02:24,740 --> 01:02:28,750
Over here. We want to tell C where we want to put integer
830
01:02:28,850 --> 01:02:35,620
So I'm basically gonna tell see what variable I want to store this in. So I'm gonna store this inside of my age variable and
831
01:02:36,200 --> 01:02:39,400
I can essentially just type out the name of the variable here
832
01:02:39,770 --> 01:02:46,150
But in order to get input from the user I'm actually gonna have to type an ampersand in front of this
833
01:02:46,220 --> 01:02:50,410
So instead of just typing out age. I'm gonna have to type ampersand age
834
01:02:50,570 --> 01:02:56,830
And when I say ampersand age, this is what's called a pointer and we're gonna talk about pointers in a later tutorial
835
01:02:56,830 --> 01:02:59,289
I'm gonna cover everything you need to know about pointers
836
01:02:59,290 --> 01:03:02,529
But for now, that's a little bit beyond what we need to learn
837
01:03:02,530 --> 01:03:07,600
so all you need to know is that when you're using scanf and you want to store information inside of like
838
01:03:07,850 --> 01:03:13,449
An integer or a float or like a character you need to use this ampersand over here
839
01:03:14,090 --> 01:03:19,989
So down here now that we've scanned for the users age. I'm just gonna go ahead and print it out
840
01:03:19,990 --> 01:03:26,620
So we'll just print out like you are and I'll say % D years old and over here
841
01:03:26,620 --> 01:03:31,929
I'm just gonna print out that age variable. So essentially what I'm doing is I'm prompting the user to enter their age
842
01:03:31,930 --> 01:03:38,440
I'm storing whatever age they enter inside of this age variable and then I'm printing it out to them
843
01:03:38,750 --> 01:03:41,919
So let's go ahead and run this program and we'll see how we did
844
01:03:41,920 --> 01:03:47,590
So I'm gonna run the program and it says enter your age. So let's say someone is 50 when I click enter
845
01:03:47,590 --> 01:03:49,120
It's gonna take that value
846
01:03:49,120 --> 01:03:54,489
It's can take that integer that we entered 50 store it inside of that age variable and print it out
847
01:03:54,680 --> 01:03:56,680
You'll see over here. It says you are
848
01:03:57,050 --> 01:03:59,919
50 years old. So that's how we can get an integer from the user
849
01:04:00,200 --> 01:04:03,040
We can also get like a double from the user
850
01:04:03,040 --> 01:04:08,290
so for example, I could say enter your GPA and so now instead of
851
01:04:08,810 --> 01:04:13,090
asking for an integer we're gonna be asking for a double so I can come up here and say like double and
852
01:04:13,700 --> 01:04:18,460
We could call this GPA and now I can do the same thing. But instead of saying D
853
01:04:18,460 --> 01:04:24,730
I want to say L F and L F is basically going to tell this scanf function that we're looking for a
854
01:04:24,980 --> 01:04:30,939
Double and then obviously instead of age. We're just gonna put GPA inside of here. And so now we can just say like
855
01:04:31,910 --> 01:04:38,649
your GPA is and then we can put % F because we're gonna be printing out a double and
856
01:04:39,080 --> 01:04:43,719
I'll just say GPA. So you'll notice here in printf when we want to use a double
857
01:04:43,720 --> 01:04:49,749
We're using percent F to print it out. But when we're using scanf we're going to use percent LF
858
01:04:49,749 --> 01:04:53,499
So that's just like a little difference and now let's go ahead and run this program
859
01:04:53,769 --> 01:04:59,109
And we should be able to get a GPA. So let's say someone's GPA is like 3.1. Now. It says your GPA is
860
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 guys how we can get a character
861
01:05:04,729 --> 01:05:09,549
so why don't we create a character up here and we'll just call it grade and
862
01:05:10,700 --> 01:05:12,700
We'll say enter your grade
863
01:05:13,279 --> 01:05:14,299
and
864
01:05:14,299 --> 01:05:18,728
Now when we want to get a character we can just say percent C and again
865
01:05:18,729 --> 01:05:25,299
I can just come down here and say ampersand grade and then down here we can just say your grade is
866
01:05:26,450 --> 01:05:30,220
Percent C and we'll go ahead and print out the grade
867
01:05:30,220 --> 01:05:34,179
So now we should be able to get a character from the user
868
01:05:34,670 --> 01:05:35,799
so enter the grade
869
01:05:35,799 --> 01:05:42,849
let's say I got an A and it says you're good as a so we can use this scanf to get specific types of
870
01:05:43,489 --> 01:05:48,819
Input from the user we can store those specific types of input inside of variables now
871
01:05:48,819 --> 01:05:53,589
I also want to show you guys one more thing we can do which is we can actually get a string from the user
872
01:05:53,589 --> 01:05:59,528
so in addition to getting numbers and a character, we could also get like a string of characters from the user and
873
01:06:00,170 --> 01:06:02,829
this is going to be a little bit different from
874
01:06:03,680 --> 01:06:09,519
Doing like numbers and characters. So I want to kind of show you guys how we can do this really quick so over here
875
01:06:09,519 --> 01:06:14,289
Let's create a variable. We'll just call it. Let's just call it name. So we'll be storing someone's name and
876
01:06:15,799 --> 01:06:21,369
Whenever we're creating a string of characters, we always need these open and close square brackets and inside of here
877
01:06:21,499 --> 01:06:28,689
I'm actually going to specify how many characters I want to be able to store inside this string of characters
878
01:06:28,690 --> 01:06:30,690
so I'm just gonna put 20 and
879
01:06:30,950 --> 01:06:36,099
In the past in this course when we when we've been creating strings. We haven't put a number in there
880
01:06:36,099 --> 01:06:41,018
We've just kind of said like whatever. All right, we basically just given this a value straight away
881
01:06:41,150 --> 01:06:47,440
But in this particular situation, we're not gonna be giving this variable of value right away in other words
882
01:06:47,440 --> 01:06:51,489
Like I don't know what the users name is gonna be like, I don't know that right up front
883
01:06:51,489 --> 01:06:55,689
They're gonna tell us what that is. And so if I'm not gonna give this a value, right?
884
01:06:55,900 --> 01:07:01,150
I do actually need to tell see how big I want this variable to be in other words
885
01:07:01,150 --> 01:07:07,270
I need to tell see how many characters I want this variable to be able to store and that way si can go ahead and
886
01:07:07,270 --> 01:07:14,319
Allocate enough memory for this variable. So I'm just gonna put 20 and 20 basically means this will be able to store 20 characters
887
01:07:14,839 --> 01:07:19,269
And I think that's enough for a name so down here. We'll just say enter your name and
888
01:07:20,329 --> 01:07:23,109
I can use scanf in order to get input
889
01:07:23,270 --> 01:07:28,089
in the form of a string but instead of saying percent see I'm just gonna say percent s and
890
01:07:28,460 --> 01:07:31,089
Over here instead of saying ampersand grade
891
01:07:31,089 --> 01:07:38,019
I'm just gonna type out the name of the string of characters. So I don't actually need this ampersand here. I can just
892
01:07:38,569 --> 01:07:41,769
Specify the name of the string so down here
893
01:07:41,770 --> 01:07:47,199
it says printf your grade is and actually let's just say your name is
894
01:07:47,839 --> 01:07:53,078
I would have say percent s and then over here we can print out the name
895
01:07:53,720 --> 01:07:57,159
So this should work just like it did in the other cases
896
01:07:57,160 --> 01:08:01,930
So let's go ahead and run this and it says enter your name. So my name is Mike and
897
01:08:02,630 --> 01:08:09,400
You'll see it says your name is Mike. So that works out really well, but there is one problem when we're using
898
01:08:10,010 --> 01:08:12,010
scanf in order to get a
899
01:08:12,260 --> 01:08:13,150
string from the user
900
01:08:13,150 --> 01:08:16,690
So for example, if I came in here, and I said enter your name, and I said my name was john
901
01:08:16,790 --> 01:08:19,029
Smith when I click enter
902
01:08:19,609 --> 01:08:26,139
You'll notice that it's only saying your name is. John, it's not including smith over here and
903
01:08:26,719 --> 01:08:28,989
Here's the problem. This is because
904
01:08:29,509 --> 01:08:37,149
Whenever I use this scanf function and I use it with a string. It's only gonna grab the characters up to the first
905
01:08:37,310 --> 01:08:42,909
Space so once it sees this space it's gonna be like, okay, we're done getting characters, right?
906
01:08:42,909 --> 01:08:48,009
So that's kind of a problem and that's just kind of how scanf works. It's not really, you know scanf fault
907
01:08:48,679 --> 01:08:53,799
There is a way that we can modify scanf in order to be able to get input with spaces
908
01:08:54,140 --> 01:09:00,579
but there's another function which I want to show you guys which we can use to get a line of text from the user and
909
01:09:00,580 --> 01:09:02,509
it's called F gets and
910
01:09:02,509 --> 01:09:03,790
Afghans is another function
911
01:09:03,790 --> 01:09:06,790
it's similar to scanf but f gets is
912
01:09:07,190 --> 01:09:11,840
Basically going to be more generous F gas is essentially just gonna grab like a whole line of text
913
01:09:11,840 --> 01:09:18,590
It's not gonna be able to grab it and store it inside of like an integer or a variable or a character
914
01:09:18,590 --> 01:09:21,590
It's just gonna be able to store it inside of like a string of characters. So
915
01:09:22,320 --> 01:09:24,590
When we're using a forgets the first argument
916
01:09:24,590 --> 01:09:29,059
We want to give it is the name of the variable where we want to store to the line of text
917
01:09:29,060 --> 01:09:31,189
So in our case, it's just gonna be this name variable
918
01:09:31,650 --> 01:09:36,319
The next thing we want to do is specify how many characters we want to be able to input from the user
919
01:09:36,690 --> 01:09:40,760
So this will essentially limit the amount of characters that the user can input
920
01:09:42,060 --> 01:09:46,910
And this is always a good idea when we're trying to get a string from the user because one common problem
921
01:09:46,910 --> 01:09:48,920
is that the user could try to
922
01:09:49,110 --> 01:09:54,049
Enter in like a million characters and they would overflow the like the buffer in other words
923
01:09:54,050 --> 01:09:58,849
Like see wouldn't be able to handle accepting that many characters and the program might break
924
01:09:59,430 --> 01:10:01,370
So over here with F gaps
925
01:10:01,370 --> 01:10:05,059
We can specify how many characters we want to be able to accept so I'm just gonna say 20
926
01:10:05,130 --> 01:10:09,469
Because that's how many characters we can store inside of this variable and over here
927
01:10:09,470 --> 01:10:13,909
I'm just gonna say stdin and this stands for standard input essentially
928
01:10:13,910 --> 01:10:15,950
what we're doing over here is we're telling FCAT s--
929
01:10:16,290 --> 01:10:22,610
where we want to get the information from and standard input is set as essentially just like that little console that we've been using so
930
01:10:23,010 --> 01:10:27,590
Now I'm using F Gatz and this is going to do exactly what scanf did so we'll be able to store
931
01:10:27,840 --> 01:10:31,549
the input inside of this named variable, but now we'll be able to
932
01:10:32,190 --> 01:10:33,360
store
933
01:10:33,360 --> 01:10:38,599
Multiple words instead of just one single word. So let's run this program and it says enter your name
934
01:10:38,600 --> 01:10:40,670
So now if we entered in John Smith
935
01:10:41,100 --> 01:10:43,910
You'll see it's able to enter in John Smith
936
01:10:44,190 --> 01:10:51,440
Now the one downside with just using this F gets is you'll notice that it printed out this new line over here
937
01:10:51,780 --> 01:10:54,259
So let me actually demonstrate this a little bit
938
01:10:54,840 --> 01:10:58,340
So if I was to print out like, you know, just some random text over here
939
01:10:59,670 --> 01:11:04,339
Whenever I get input using scanf, so if I entered in John Smith
940
01:11:04,860 --> 01:11:06,690
When I click the enter key
941
01:11:06,690 --> 01:11:13,760
So when I'm entering this by clicking the Enter key that's actually going to represent a newline character and that's going to get stored
942
01:11:14,640 --> 01:11:19,999
Inside of the string. So when I click enter you'll notice it says your name is John Smith
943
01:11:20,010 --> 01:11:23,859
And then it prints out a newline and it prints out this text
944
01:11:23,900 --> 01:11:29,350
And so that's just something that you need to be aware of when you're using this F gets function
945
01:11:29,750 --> 01:11:32,979
but for the most part, I would say if you're getting input from
946
01:11:33,110 --> 01:11:39,760
A user in the form of a string you can use F gets you can also use scanf and you could use multiple
947
01:11:41,120 --> 01:11:45,189
Percent asses and i'm actually going to show you guys how we could do something like that in a future tutorial
948
01:11:45,590 --> 01:11:50,350
But for the most part I would say whenever you're getting a string from the user you just want to use F
949
01:11:50,630 --> 01:11:53,560
Guess so that's the basics of getting input from the user
950
01:11:53,560 --> 01:11:54,280
like I said
951
01:11:54,280 --> 01:11:56,649
there's a couple other things that we can do and I'm gonna talk about
952
01:11:57,080 --> 01:12:01,780
At least one other way that we can get strings from the user in a future tutorial
953
01:12:01,910 --> 01:12:06,669
But this is kind of the basics and you can kind of play around with doing this stuff in your programs
954
01:12:10,530 --> 01:12:15,059
In this tutorial I'm going to show you guys how to build a basic calculator in C
955
01:12:15,219 --> 01:12:19,469
we're basically gonna build a little program where the user can enter in two numbers and then
956
01:12:19,630 --> 01:12:23,969
Our program will take those two numbers add them together and spit out the answer
957
01:12:23,969 --> 01:12:29,339
So this is gonna be kind of cool and we'll also learn about getting numbers as input from a user
958
01:12:29,979 --> 01:12:33,929
So over here, I want to show you guys exactly how we can do this
959
01:12:33,929 --> 01:12:35,800
the first thing I want to do is
960
01:12:35,800 --> 01:12:43,079
Print out a prompt. So I basically want to prompt the user for some input so over here I can just say printf and
961
01:12:44,349 --> 01:12:49,139
Inside here. We're just gonna give them a little prompt. So I'll say enter first number and
962
01:12:51,699 --> 01:12:58,319
Essentially what we're gonna have them do is enter in two numbers, so I'm actually going to create two variables up here
963
01:12:58,320 --> 01:13:00,869
so we'll make an int and we'll call it num1 and
964
01:13:01,479 --> 01:13:05,999
Then we'll make another inch and we'll call it num2 and i'm not actually going to give these
965
01:13:06,519 --> 01:13:11,069
Values right up front we're gonna end up giving these whatever the user enters in
966
01:13:11,229 --> 01:13:18,089
So after the user enters the first number we need to actually grab that number so I can use another see function called
967
01:13:18,429 --> 01:13:20,110
scanf and
968
01:13:20,110 --> 01:13:25,799
In here, we're basically just gonna say percent d because we want to grab an integer and then over here
969
01:13:25,800 --> 01:13:27,929
I'm going to say the name of the variable
970
01:13:28,119 --> 01:13:34,049
Where I want to store the value that gets entered now if you're following along with this course in the last tutorial
971
01:13:34,329 --> 01:13:38,189
We were able to get a string of characters as input from the user
972
01:13:38,190 --> 01:13:41,729
And basically we just typed in like the name of the variable
973
01:13:41,889 --> 01:13:45,089
But when we're getting input, that's not a string of characters
974
01:13:45,090 --> 01:13:51,299
In other words when we're getting input that's not percent s so if it's like percent d or percent 4 percent C
975
01:13:51,300 --> 01:13:56,789
so if it's a guy a decimal or an integer or a float or a character, we actually need to use a
976
01:13:57,309 --> 01:13:59,639
special symbol this ampersand
977
01:13:59,639 --> 01:14:04,618
So I'm gonna have to say ampersand and then the name of the variable what I want to store this value
978
01:14:04,619 --> 01:14:06,929
so I'm just gonna say ampersand 1 and
979
01:14:07,420 --> 01:14:14,759
Basically what this means is we're accessing the address of num 1 and we're gonna talk about addresses and pointers in a future video
980
01:14:14,760 --> 01:14:18,749
but for now just know that you need this ampersand here in order to
981
01:14:19,329 --> 01:14:22,889
Store the value that gets entered inside of this variable
982
01:14:22,889 --> 01:14:24,150
So once we do that
983
01:14:24,150 --> 01:14:25,570
and now we can move on and
984
01:14:25,570 --> 01:14:31,049
we're basically just gonna do the same exact thing but for the second number so I'm gonna copy these and
985
01:14:31,690 --> 01:14:35,760
I'll paste this and now instead of saying enter first number. We'll say enter
986
01:14:36,340 --> 01:14:40,109
Second number and we're just gonna store this inside of num2
987
01:14:40,930 --> 01:14:46,109
Alright, so once we're done with this, the last thing we want to do is add them together and print out the answer
988
01:14:46,540 --> 01:14:49,320
So I'm just going to come down here. I'm going to say printf and
989
01:14:50,260 --> 01:14:52,979
We'll just say answer and over here
990
01:14:52,980 --> 01:14:59,189
We'll print out the answer so it's gonna be an integer and we're basically just gonna print out num1 plus num2
991
01:14:59,590 --> 01:15:06,929
So we're gonna print out the value of num1 plus num2. So we have our basic calculator. We're getting the first number
992
01:15:06,930 --> 01:15:11,939
We're getting the second number. We're storing them inside of these variables. We're adding them together and printing them out
993
01:15:12,340 --> 01:15:18,029
Let's try to run our calculator. So I'm gonna run my program and you'll see over here. It says enter first number
994
01:15:18,030 --> 01:15:20,030
So let's go ahead and enter 6 enter
995
01:15:20,500 --> 01:15:22,500
Second number will enter
996
01:15:22,660 --> 01:15:27,780
A8 and now we're gonna get 14. So 14 is the correct answer
997
01:15:27,780 --> 01:15:31,769
So our program worked it was able to add the numbers correctly and everything's awesome
998
01:15:32,440 --> 01:15:36,750
But let me show you guys one problem with this program if I wanted to do math on
999
01:15:37,300 --> 01:15:44,550
Multiple non decimal numbers, for example if I said 2 and then over here I said like 6 point 8 when I add these together
1000
01:15:45,250 --> 01:15:49,200
We're not going to get the correct answer. We're gonna get 6 plus 2
1001
01:15:49,720 --> 01:15:55,649
Which is gonna be an integer 8, but we're not gonna get 8 point 8, so we're not getting the correct value
1002
01:15:55,720 --> 01:15:58,979
So instead of letting the user enter in only integers
1003
01:15:58,980 --> 01:16:02,640
Why don't we instead let them enter in doubles so over here?
1004
01:16:02,640 --> 01:16:07,439
I'm going to change these to double so I'm gonna say num one's gonna be a double and num2 is gonna be a double and
1005
01:16:07,660 --> 01:16:11,760
Since we're getting doubles as input, we're gonna have to come down here and modify these
1006
01:16:11,760 --> 01:16:14,760
So right now this is accepting an integer
1007
01:16:15,160 --> 01:16:18,300
but we want to make this accept a double so normally if we're using
1008
01:16:18,460 --> 01:16:23,820
Printf and we wanted to print out a double we would say f and that stands for like floating-point number
1009
01:16:23,920 --> 01:16:27,450
but when we're scanning for a number if we want to use a double we have to say
1010
01:16:27,610 --> 01:16:33,000
LF just like that so down here. I'm also gonna say LF and we'll change this to just F
1011
01:16:33,000 --> 01:16:35,970
so print F is a little bit different than
1012
01:16:36,580 --> 01:16:42,269
Scanf right and scanf if we want to accept a double as input. We need to use LF in printf
1013
01:16:42,610 --> 01:16:47,580
We just use % F. So let's run our program now and we should be able to add
1014
01:16:48,100 --> 01:16:50,879
floating-point numbers so decimal numbers over here
1015
01:16:50,880 --> 01:16:57,750
I will do four point five plus six point seven and we get 11 point two, so that looks good to me
1016
01:16:57,750 --> 01:17:01,919
Looks like everything is working properly. And now we have a basic calculator
1017
01:17:01,920 --> 01:17:07,290
So the user can enter in any numbers that they want and the calculator will be able to handle them
1018
01:17:07,290 --> 01:17:10,080
Now this calculator is not like 100%
1019
01:17:10,690 --> 01:17:14,460
ironclad secure for example, like if I was to come up here and
1020
01:17:15,250 --> 01:17:21,060
Enter in like a string of characters instead of a number you'll see that it's gonna break the program, right?
1021
01:17:21,060 --> 01:17:23,430
So it's just like not doing what we wanted to do
1022
01:17:23,430 --> 01:17:25,410
And as we go further in this course
1023
01:17:25,410 --> 01:17:31,829
We're gonna learn all sorts of ways that we can check to see if certain things are getting entered correctly and we can basically mitigate
1024
01:17:32,200 --> 01:17:39,300
circumstances like that, but for now, this is just kind of an introduction into how we can get numbers as input and do things like
1025
01:17:39,820 --> 01:17:41,879
Addition or multiplication once we have them
1026
01:17:45,630 --> 01:17:51,389
In this tutorial I'm going to talk to you guys about building a little game in C more specifically
1027
01:17:51,389 --> 01:17:53,519
We're gonna be building a mad libs game now
1028
01:17:53,520 --> 01:17:55,110
If you ever played the game mad libs
1029
01:17:55,110 --> 01:17:59,520
it's basically a game where you write down a bunch of random words so it could be like
1030
01:17:59,520 --> 01:18:04,649
You know nouns or verbs or someone's name or you know a verb ending in ing, something like that
1031
01:18:04,750 --> 01:18:07,889
you take all of those words that you enter in and you kind of
1032
01:18:08,079 --> 01:18:14,159
Sprinkle them in into a story and then generally the story is like kind of funny because it has all these random words in it
1033
01:18:14,170 --> 01:18:16,170
It's actually if we head over to my web browser
1034
01:18:16,570 --> 01:18:19,199
You'll see I have a picture of a Madlib up here
1035
01:18:19,199 --> 01:18:24,839
And basically you just add in a bunch of random words into the story and then you'd read the story back and it could be
1036
01:18:24,840 --> 01:18:29,460
Kind of funny so I'm gonna show you guys how we can build something like that in C
1037
01:18:29,500 --> 01:18:35,879
And we're also gonna talk about some more ways that we can use that scanf function that I showed you guys in the last tutorial
1038
01:18:36,400 --> 01:18:42,659
So over here, we have a little story that I printed out. It just says roses are red violets are blue
1039
01:18:42,659 --> 01:18:45,059
I love you kind of like a classic poem
1040
01:18:45,460 --> 01:18:49,980
But I think this poem would be a lot funnier if we turned it into a mad libs
1041
01:18:49,980 --> 01:18:56,909
So that's exactly what we're gonna do. I'm gonna replace roses are red. I'm gonna replace red with a random color
1042
01:18:56,980 --> 01:19:02,069
So we're just gonna have a user enter in a color. I'll replace violets with a plural noun
1043
01:19:02,110 --> 01:19:07,319
So we're gonna have them enter in a plural noun, and I'm gonna have instead of saying I love you
1044
01:19:07,320 --> 01:19:13,259
We're gonna say I love and then a specific celebrity. So I'll just type in celebrity right there
1045
01:19:14,079 --> 01:19:20,939
Alright, so this is basically what we're gonna be printing out we're gonna be printed out roses are and then whatever color they enter in
1046
01:19:21,579 --> 01:19:28,379
Plural nouns are blue and then I love whatever celebrity so let's talk about actually creating this program
1047
01:19:28,380 --> 01:19:30,380
So we're actually going to need to do a couple things
1048
01:19:30,730 --> 01:19:36,779
And actually the first thing I want to do is I want to create variables we're gonna create variables to store
1049
01:19:37,570 --> 01:19:43,380
The color that the user inputs the plural noun that the user inputs and the celebrity the user inputs
1050
01:19:43,380 --> 01:19:47,219
we're going to create three variables and these are basically going to be
1051
01:19:48,130 --> 01:19:50,130
character strings, so they're going to be
1052
01:19:50,380 --> 01:19:52,469
Collections of characters and we can store them in
1053
01:19:53,060 --> 01:19:59,410
Variable so I'm gonna create some of these variables. Why don't we create one for color? And we remember whenever we create a
1054
01:20:00,050 --> 01:20:02,140
String or like a collection of characters?
1055
01:20:02,140 --> 01:20:04,660
We need to make these open and closed square brackets
1056
01:20:04,940 --> 01:20:12,310
And what I also want to do because I'm not gonna be giving color of value right away. In other words. We're letting the user
1057
01:20:12,950 --> 01:20:14,200
Determine the value of color
1058
01:20:14,200 --> 01:20:17,379
I just need to tell see how many characters we want this
1059
01:20:17,570 --> 01:20:23,529
String to be able to store that way si knows how much memory it needs to allocate for this variable
1060
01:20:23,780 --> 01:20:29,140
So I'm just gonna say 20 and we'll basically just say they can enter in a color that is up to 20 characters
1061
01:20:29,420 --> 01:20:32,260
we're gonna do the same thing for plural noun and
1062
01:20:33,230 --> 01:20:36,459
again, we'll let them enter in 20 characters maximum and
1063
01:20:37,850 --> 01:20:43,059
Finally, we're gonna do the same thing for celebrity. So again 20 characters sounds good
1064
01:20:43,280 --> 01:20:48,190
Alright now that we have our variables created. I want to actually get information from the user
1065
01:20:48,190 --> 01:20:49,730
so I want to prompt the user for
1066
01:20:49,730 --> 01:20:54,970
Information and I want to take the information that they entered in and I want to store it inside of each one of these variables
1067
01:20:55,520 --> 01:21:00,819
First order of business is to prompt them for input so I can just say print F and inside here
1068
01:21:00,820 --> 01:21:03,340
We'll basically just type in enter a color
1069
01:21:04,130 --> 01:21:11,230
Once we've prompted them to enter the color we can actually get whatever color they enter and store it inside of a variable
1070
01:21:11,360 --> 01:21:13,360
I'm going to use a function called
1071
01:21:13,670 --> 01:21:16,779
Scanf and over here. I'm gonna accept a
1072
01:21:17,300 --> 01:21:24,760
string so I'm going to accept a string of characters and we're gonna store this inside of our color variable just like that and
1073
01:21:25,160 --> 01:21:27,999
remember if you watched the last tutorial we use the
1074
01:21:28,880 --> 01:21:34,660
Ampersand here when we were getting numbers or also you'd do the same thing if you were getting a single character
1075
01:21:34,910 --> 01:21:40,930
But when we're getting input for a string of characters, we don't need that ampersand so you can just get rid of that
1076
01:21:41,780 --> 01:21:45,040
Let's copy these and I'm gonna paste this two more times
1077
01:21:45,040 --> 01:21:47,740
so the second thing we want to get from them is going to be the
1078
01:21:48,260 --> 01:21:56,169
plural noun and I'm gonna store this inside of the plural noun variable so you can see now we're getting the plural noun and
1079
01:21:56,600 --> 01:22:02,169
Finally, we're gonna do the same thing for celebrity. And again, we're gonna get that celebrity
1080
01:22:03,360 --> 01:22:10,519
Okay, cool. So now I'm getting the color. I'm getting the plural noun and I'm getting the celebrity
1081
01:22:10,519 --> 01:22:15,139
So the last thing we have to do is we have to take all of these variables and put them into our story
1082
01:22:15,139 --> 01:22:19,399
Right, so we need to be able to print out the story with all of those variables
1083
01:22:19,400 --> 01:22:24,919
So I'm gonna come down here and I'm just going to say percent s and over here. We'll pass in the color
1084
01:22:26,159 --> 01:22:29,719
Same thing here. I'm gonna replace the plural noun here with a
1085
01:22:30,630 --> 01:22:32,749
percent s and we'll pass in the
1086
01:22:33,539 --> 01:22:38,029
Plural noun and finally same thing for celebrity down here
1087
01:22:42,869 --> 01:22:48,918
Alright so everything seems to be wired up and you'll notice that I have new lines here so that this story prints out on new
1088
01:22:48,919 --> 01:22:53,179
Lines, let's go ahead and run this program and we'll see how we did
1089
01:22:54,119 --> 01:22:58,098
So over here, it's prompting us for a color. Why don't we enter in magenta?
1090
01:22:58,949 --> 01:23:00,949
Enter a plural noun, let's do
1091
01:23:01,320 --> 01:23:04,969
Microwaves and enter a celebrity. Why don't we just say?
1092
01:23:06,090 --> 01:23:09,979
Prince so when I click enter it's gonna say roses are magenta
1093
01:23:10,559 --> 01:23:16,369
Microwaves are blue. I love prints. So we were able to prompt the user to enter in all of that input
1094
01:23:16,369 --> 01:23:20,149
We took everything that they input we stored it in variables
1095
01:23:20,150 --> 01:23:26,809
Then we printed all those variables out inside of our story and we have our Madeleine the program seems to be working really well
1096
01:23:26,809 --> 01:23:31,969
I do want to show you guys one way that this program could mess up. So let's go ahead and run this again
1097
01:23:32,999 --> 01:23:37,279
So let's enter in a different color. I'm going to enter in like blue enter in a plural noun
1098
01:23:37,280 --> 01:23:38,880
So why don't we enter in?
1099
01:23:38,880 --> 01:23:39,479
phones
1100
01:23:39,479 --> 01:23:45,259
And now enter in a celebrity so I'm gonna show you guys one way that we could actually break this program
1101
01:23:45,479 --> 01:23:51,739
If I entered in a celebrity with a first and a last name like Tom Hanks when I click enter now
1102
01:23:52,139 --> 01:23:58,368
You'll notice that instead of saying I love Tom Hanks. It's only saying I love Tom
1103
01:23:58,889 --> 01:24:06,348
Here's the problem when we use that scanf function scanf is only going to grab characters up to the first
1104
01:24:06,689 --> 01:24:12,109
Whitespace, so essentially when we put this space here. We're telling see that we don't want to grab anymore
1105
01:24:12,449 --> 01:24:15,489
but in reality we want to be able to grab the there's
1106
01:24:15,860 --> 01:24:20,529
Full-name want to be able to grab the celebrities first and last name if need be?
1107
01:24:21,050 --> 01:24:25,180
So this is a situation in C where we would have to modify our a little program
1108
01:24:25,400 --> 01:24:27,080
so what I could do is
1109
01:24:27,080 --> 01:24:33,309
instead of just getting one variable like the celebrity I can actually get to so I could say over here like
1110
01:24:33,560 --> 01:24:38,200
Celebrity F and that'll stand for celebrity first name and then down here
1111
01:24:38,200 --> 01:24:44,709
We can make another variable called celebrity l that'll stand for a last name. So now when we scan
1112
01:24:45,500 --> 01:24:52,509
instead of just scanning for one string of characters I can scan for two strings of characters and we'll have celebrity F and
1113
01:24:53,330 --> 01:24:55,330
then celebrity l and
1114
01:24:55,700 --> 01:25:00,819
Down here. We're gonna want to do the same thing so we can just say celebrity F and
1115
01:25:02,270 --> 01:25:06,189
Celebrity L and we just need to add another percent here
1116
01:25:06,190 --> 01:25:12,850
So it's gonna say I love celebrities first name and celebrities last name. So let's run our program and see how we did
1117
01:25:12,890 --> 01:25:15,099
so I can enter in like red and
1118
01:25:17,660 --> 01:25:24,819
Microphones and now we can enter in Tom Hanks and we're printing out the actors first and last name
1119
01:25:24,890 --> 01:25:31,689
So that's one way that we could remedy this program and make it be able to accept two inputs with a space in the middle
1120
01:25:31,690 --> 01:25:36,160
And it also just shows you guys a little bit more about how scanf works so it's gonna stop scanning
1121
01:25:36,320 --> 01:25:40,360
It's gonna stop getting the input at that first space now
1122
01:25:40,360 --> 01:25:43,960
Here's the thing about this program though if I wanted to enter in a celebrity
1123
01:25:45,110 --> 01:25:46,250
with
1124
01:25:46,250 --> 01:25:47,680
Who only had one name?
1125
01:25:47,680 --> 01:25:51,639
So if I only wanted to enter in one the program actually isn't going to be able to handle that
1126
01:25:51,640 --> 01:25:54,339
so if I said like hats and down here if I said
1127
01:25:54,710 --> 01:26:00,970
Like Gandhi and I click enter you'll notice that it's still waiting for me to enter in a last name
1128
01:26:01,190 --> 01:26:03,879
Right, so I could enter in something here and then the program will work
1129
01:26:04,400 --> 01:26:08,589
But it was waiting for me after I entered in just that one name
1130
01:26:08,590 --> 01:26:12,430
So that's something that you're gonna have to you know play around with in your programs
1131
01:26:12,650 --> 01:26:16,930
Basically C is gonna force you to be very specific about what the user is entering
1132
01:26:16,930 --> 01:26:22,389
so if the user needs to enter two things like two words and you need to specify that if the user is only going to
1133
01:26:22,390 --> 01:26:28,719
Enter in one word. You have to specify that. So you have to be very specific when you're getting input from the user like that
1134
01:26:32,400 --> 01:26:39,690
In this tutorial I'm gonna talk to you guys about using a raisin seed a lot of times when we're writing our C programs
1135
01:26:39,690 --> 01:26:42,690
we're gonna be dealing with a bunch of different data and
1136
01:26:43,000 --> 01:26:48,810
one of the things we can do to control and manage and sort of keep track of and organize that data is
1137
01:26:48,880 --> 01:26:56,370
Put data inside of things called arrays an array is really useful because it's basically a container where we can store a piece of information
1138
01:26:56,680 --> 01:27:01,079
But what happens in your programs when you're dealing with huge amounts of information?
1139
01:27:01,870 --> 01:27:05,610
Specifically huge amounts of information that are related right imagine
1140
01:27:05,610 --> 01:27:10,860
I had a list of like a bunch of names or a list of a bunch of numbers that I needed to keep track of
1141
01:27:11,140 --> 01:27:13,680
Let's say I had like a list of a hundred numbers, right?
1142
01:27:13,680 --> 01:27:16,200
I wouldn't want to have to create a hundred different
1143
01:27:16,420 --> 01:27:23,040
variables to store all of those different numbers and this is where arrays come in an array is essentially a
1144
01:27:23,290 --> 01:27:27,479
Data structure where we can store a bunch of different data values
1145
01:27:27,580 --> 01:27:33,570
So inside of an array unlike a variable where I can only store one value I could store hundreds or thousands or even millions
1146
01:27:33,700 --> 01:27:34,570
of values
1147
01:27:34,570 --> 01:27:41,849
So inside of a single array, I could store like five things or seven things or ten numbers or twenty characters, you know
1148
01:27:41,850 --> 01:27:47,370
I could sort a bunch of different pieces of information and then all that information would be nice and neat and
1149
01:27:47,620 --> 01:27:51,059
Organized in my program so I'm gonna show you guys how to create arrays
1150
01:27:51,370 --> 01:27:53,070
We can create an array a lot
1151
01:27:53,070 --> 01:27:57,539
Like we create a normal variable and a variable in array are very similar
1152
01:27:57,910 --> 01:28:01,169
variable is basically used to define a container that
1153
01:28:01,720 --> 01:28:07,709
Stores a single value and an array is used to define a container that stores, you know any number of values
1154
01:28:07,780 --> 01:28:11,909
So the first thing we're gonna have to do when we create an array is give C some
1155
01:28:12,010 --> 01:28:17,820
Information and the first piece of information is what type of data we want to store inside of the array
1156
01:28:17,820 --> 01:28:19,200
so for example
1157
01:28:19,200 --> 01:28:24,179
I could say int and now I'd be creating an array that would hold integers
1158
01:28:24,180 --> 01:28:27,899
If I said char, I'd be creating an array that would hold characters
1159
01:28:28,450 --> 01:28:32,789
Or if I said like double the same thing but for double numbers, so why don't we create an array of integers?
1160
01:28:32,790 --> 01:28:35,040
And I'm just gonna call this lucky
1161
01:28:35,680 --> 01:28:37,889
numbers whenever I create an array
1162
01:28:37,890 --> 01:28:44,010
I always want to use a special little symbol and this symbol will tell see that instead of just creating a normal
1163
01:28:44,260 --> 01:28:47,739
Variable we want to an array and it's an open and closed
1164
01:28:48,110 --> 01:28:54,399
Square brackets whenever I use these open and close square brackets after the name. It's gonna tell C like, okay
1165
01:28:54,399 --> 01:28:56,399
We want to store multiple pieces of information
1166
01:28:56,840 --> 01:28:59,679
Now there's a bunch of different ways. We can create these arrays
1167
01:28:59,719 --> 01:29:05,948
the easiest one though is to just say equals and I can make an open and closed curly bracket and
1168
01:29:06,260 --> 01:29:08,379
inside of this open and close curly bracket
1169
01:29:08,379 --> 01:29:14,739
I can just start typing out some numbers that I want to put in this array so we could type out like 4 8 15
1170
01:29:15,739 --> 01:29:17,239
16 23
1171
01:29:17,239 --> 01:29:18,469
42
1172
01:29:18,469 --> 01:29:23,589
Now I can basically type out as many numbers as I wanted and I could store them
1173
01:29:23,840 --> 01:29:29,199
Inside of this array so over here, I you know, I printed out six numbers I could print out 600 if I wanted
1174
01:29:29,300 --> 01:29:33,129
You know, the only thing that's limiting me is how many I can actually put in here
1175
01:29:33,129 --> 01:29:37,059
So you'll notice that I put in the piece of data so like 4, for example
1176
01:29:37,219 --> 01:29:42,098
And then I put a comma and a comma is gonna separate all the elements in the array
1177
01:29:42,409 --> 01:29:47,618
So we would say 4 is the first element in the array 8 is the second element in the array
1178
01:29:47,619 --> 01:29:49,659
These are all considered to be elements
1179
01:29:50,239 --> 01:29:55,268
Inside of this array and what's cool about this array is it's now storing all of this information
1180
01:29:55,639 --> 01:30:01,418
So unlike a variable where we can only store one value now in this array. I'm storing 6 different numbers
1181
01:30:01,419 --> 01:30:06,249
so if I wanted I can print this out and I'm actually gonna print this out just to show you guys so
1182
01:30:06,769 --> 01:30:08,769
remember, we're gonna print out a
1183
01:30:08,899 --> 01:30:11,199
Number and I'm gonna show you guys how we can access
1184
01:30:11,899 --> 01:30:14,019
specific elements inside of this array
1185
01:30:14,629 --> 01:30:19,899
So all of these numbers are stored in this lucky numbers array in this array structure
1186
01:30:19,899 --> 01:30:24,039
But the question becomes how do we access them? Right, how can I gain access to one of these?
1187
01:30:24,349 --> 01:30:28,179
well, I could say present' d so I'm gonna print this out and now I can just say
1188
01:30:29,329 --> 01:30:30,159
lucky numbers
1189
01:30:30,159 --> 01:30:35,589
so I'm gonna refer to the name of the array and when I want to access a specific element I can make an open a
1190
01:30:35,590 --> 01:30:39,219
Close square bracket and inside of this open and close square bracket
1191
01:30:39,219 --> 01:30:45,398
I want to put the index of the element that I want to access so all of these elements in here have a specific index
1192
01:30:45,399 --> 01:30:49,149
And I can access them by putting the index inside of this square bracket
1193
01:30:49,149 --> 01:30:52,029
so if I wanted to access this for for example
1194
01:30:52,039 --> 01:30:57,699
I can put a 0 inside of here. And now when I run my program you guys will see
1195
01:30:58,540 --> 01:31:00,540
We'll be able to print out that for
1196
01:31:01,850 --> 01:31:07,870
So over here, I'm printing out four if I wanted to access this 15
1197
01:31:07,870 --> 01:31:13,419
I can put a two in here. So now when I run my program I'll be accessing the
1198
01:31:14,000 --> 01:31:15,650
15 as you can see
1199
01:31:15,650 --> 01:31:19,810
so one thing you might have noticed by now is that we start the
1200
01:31:19,970 --> 01:31:25,329
Indexes and arrays at 0 so when I wanted to access this 4 instead of putting a 1 in here
1201
01:31:25,330 --> 01:31:27,850
I put a 0 in here, right?
1202
01:31:27,850 --> 01:31:32,620
You might think that 4 would be at index position 1 because it's the first element in the array
1203
01:31:33,110 --> 01:31:37,720
but in C we start array indexes at 0 so actually the first
1204
01:31:38,000 --> 01:31:43,060
element this 4 is gonna be at index position 0 so if I was going to write out indexes
1205
01:31:43,060 --> 01:31:49,989
I would say like 4 is at index position 0 this 8 is at index position 1 this 15 is at index position 2
1206
01:31:50,450 --> 01:31:52,250
16 is a 10x position 3 etc
1207
01:31:52,250 --> 01:31:57,009
So that's gonna allow me to access all these elements inside of the array
1208
01:31:57,470 --> 01:32:02,349
Individually, so I could print them out just by referring to the index inside of these square brackets
1209
01:32:02,840 --> 01:32:06,670
Another thing I can do is I can modify some of the elements inside of here
1210
01:32:06,670 --> 01:32:07,100
so
1211
01:32:07,100 --> 01:32:12,399
Let's say I want to modify lucky numbers in x position 1 so let's say I want to modify this 8
1212
01:32:12,500 --> 01:32:19,299
so I don't like this 8 anymore so I can basically just assign this a different value like I normally would so I could say
1213
01:32:19,910 --> 01:32:21,910
lucky numbers and
1214
01:32:21,980 --> 01:32:23,980
I can make an opening close square bracket
1215
01:32:23,980 --> 01:32:29,080
we can say 1 and I can just set it equal to something else so I could set this equal to like 200 and
1216
01:32:29,270 --> 01:32:34,509
Now when we print out lucky numbers 1 instead of printing out an 8. It's gonna be printing out a
1217
01:32:35,239 --> 01:32:39,969
200 so let's go ahead and do that and run my program and you'll see we're printing out
1218
01:32:40,340 --> 01:32:44,890
200 instead of 8 so you can modify any of the individual elements
1219
01:32:45,200 --> 01:32:49,779
Inside the array just by referring to the index and really when you think about this
1220
01:32:50,330 --> 01:32:56,379
Conceptually an array is basically just holding a bunch of variables, but those variables don't have names
1221
01:32:56,380 --> 01:33:01,900
So when I access lucky numbers 1 it's the same as me accessing an integer variable
1222
01:33:02,330 --> 01:33:08,169
It's just that you know, the array is holding potentially hundreds or thousands or millions of these different variables
1223
01:33:08,360 --> 01:33:13,870
So an array is a very useful structure, especially if you're storing data, that is very similar
1224
01:33:14,180 --> 01:33:16,150
So, like I said, I could do this with an N
1225
01:33:16,150 --> 01:33:20,770
I could also do this with a double I could do this with a float I can do this with whatever I wanted
1226
01:33:20,770 --> 01:33:26,950
But there's gonna be situations where you might not necessarily know all the elements that you want to put inside your array
1227
01:33:26,950 --> 01:33:32,349
So let's say I'm creating an array and I don't know what numbers I want to put in it yet
1228
01:33:32,350 --> 01:33:34,330
So maybe I just like want to create it
1229
01:33:34,330 --> 01:33:39,459
I want to tell C that we need it but I don't want to necessarily put anything in it right away
1230
01:33:40,130 --> 01:33:46,299
Well up here. We're basically saying lucky numbers that open and close square brackets, and then we're giving it a bunch of information
1231
01:33:46,310 --> 01:33:49,419
But if we don't want to give it all that information instead
1232
01:33:49,420 --> 01:33:55,270
I can just put a call a semicolon here but inside of these square brackets
1233
01:33:55,280 --> 01:34:01,180
I need to tell see how many elements that this array can hold so I need to tell C
1234
01:34:01,180 --> 01:34:06,339
Like hey, this array can only hold like ten elements or this array can hold 20 elements or whatever
1235
01:34:06,340 --> 01:34:11,770
So let's say we wanted an array that could hold like ten elements. I can put a 10 right here and now the
1236
01:34:12,290 --> 01:34:17,890
Capacity of this integer array is 10 so it has the potential to hold 10 items
1237
01:34:17,960 --> 01:34:23,560
and what I could do is I could come down here and I can start assigning values to those items so I could say like
1238
01:34:24,290 --> 01:34:28,540
Lucky numbers 1 is equal to 80, right?
1239
01:34:28,640 --> 01:34:35,349
so I'm giving lucky numbers 1 of value and now over here I could print that value out onto the screen and
1240
01:34:35,480 --> 01:34:37,130
You'll see we're printing of 80
1241
01:34:37,130 --> 01:34:43,690
but if I try to print out for example lucky numbers 0 so the lucky numbers at in X position 0
1242
01:34:44,930 --> 01:34:52,059
This is gonna give me a negative 2 which basically means that it's not found. So it basically means that there's no
1243
01:34:52,760 --> 01:35:00,460
Element inside of lucky numbers at index position 0 but if I wanted I could do that so I could say like lucky numbers
1244
01:35:01,400 --> 01:35:08,140
0 is equal to 90 and now when I run my program it's gonna be able to do that
1245
01:35:08,140 --> 01:35:09,830
So it'll be able to get that value
1246
01:35:09,830 --> 01:35:11,380
so in a lot of situations
1247
01:35:11,380 --> 01:35:17,080
You're just gonna want to give these arrays values up front right off the bat, but in a lot of other situations
1248
01:35:17,080 --> 01:35:19,839
You're not gonna necessarily know what's supposed to go in there
1249
01:35:20,150 --> 01:35:21,550
But in a situation like that
1250
01:35:21,550 --> 01:35:25,660
you still have to tell see how many elements the array is gonna
1251
01:35:25,900 --> 01:35:28,779
You still have to like rigorously defined like hey
1252
01:35:28,780 --> 01:35:35,590
This array can only hold ten elements and that's just so C is able to allocate enough memory in order to hold all of those
1253
01:35:35,590 --> 01:35:38,949
Elements, so that's kind of the basics of working with arrays now
1254
01:35:38,949 --> 01:35:42,459
I do want to point out one thing that we've been using in this course
1255
01:35:43,340 --> 01:35:44,659
has been
1256
01:35:44,659 --> 01:35:51,249
strings, so I've been creating character strings so I could say like char phrase and I could set this equal to
1257
01:35:52,010 --> 01:35:55,239
Giraffe Academy or other times in the program we created
1258
01:35:55,460 --> 01:35:57,520
Like when we were making our little game
1259
01:35:57,650 --> 01:36:02,440
we created an arrays and I would just say like 20 and then I would get input from the user and
1260
01:36:03,020 --> 01:36:05,919
Put that information in here. This is basically a
1261
01:36:06,469 --> 01:36:12,879
String of characters. So anytime we create a string in C like if I was to create a string over here
1262
01:36:12,880 --> 01:36:15,790
I called it like array whatever. This is an array
1263
01:36:16,610 --> 01:36:19,029
So we've kind of just been taking this for granted
1264
01:36:19,030 --> 01:36:23,050
Like I've just been calling this a string or calling it like a string of characters
1265
01:36:23,330 --> 01:36:25,809
But it's basically just an array of characters
1266
01:36:26,060 --> 01:36:30,489
The only difference is this is such a common thing to use in our programs
1267
01:36:30,739 --> 01:36:31,429
that
1268
01:36:31,429 --> 01:36:35,829
C makes it a little C makes it special and makes it really easy for us to create it
1269
01:36:35,830 --> 01:36:38,620
But just like that array of integers. This is also an array
1270
01:36:38,620 --> 01:36:42,849
So now you kind of have a better understanding of what strings are and what they're actually doing
1271
01:36:46,430 --> 01:36:47,210
In this tutorial
1272
01:36:47,210 --> 01:36:50,540
I'm going to talk to you guys about functions in C a
1273
01:36:50,970 --> 01:36:55,729
Function is basically just a collection of code that performs a specific task
1274
01:36:56,490 --> 01:36:59,449
So what you can do is you can take a bunch of code, you know
1275
01:36:59,450 --> 01:37:05,929
maybe like five or six or twenty lines of code and put it inside of a function and then when you want to access that
1276
01:37:06,150 --> 01:37:07,580
Code that's inside the function
1277
01:37:07,580 --> 01:37:14,390
You can call the function and generally when you create a function you'll design the function to perform a specific task
1278
01:37:14,490 --> 01:37:17,360
So the function will have a specific purpose
1279
01:37:17,790 --> 01:37:22,340
so I'm gonna show you guys how to create functions how to work with them and we'll just talk about the basics of
1280
01:37:22,710 --> 01:37:23,750
using functions
1281
01:37:23,750 --> 01:37:26,540
like I said a function is basically just a collection of code that
1282
01:37:26,940 --> 01:37:30,440
Does a specific task so we can actually create a function here in our C program?
1283
01:37:31,020 --> 01:37:33,680
Now you'll notice up here. We have this block of code
1284
01:37:33,680 --> 01:37:39,170
It says int main and there's an open and closed parenthesis and it open and close curly brackets
1285
01:37:39,170 --> 01:37:44,659
and we've kind of just been using this for the entire course like up to this point the course we've
1286
01:37:44,850 --> 01:37:49,610
Just been kind of using this main little block of code here. But actually this is a
1287
01:37:50,130 --> 01:37:51,600
function and
1288
01:37:51,600 --> 01:37:56,959
Sometimes you'll hear people will call this a method as well. So function and method are two words that
1289
01:37:57,540 --> 01:38:02,090
Essentially mean the same thing, but you generally in C. We're gonna refer to these as functions
1290
01:38:02,220 --> 01:38:05,689
But this is called the main function and the main function is
1291
01:38:06,060 --> 01:38:10,010
Basically a function that's gonna get executed when we start running our program
1292
01:38:10,010 --> 01:38:17,809
And so we've already been using a function this main function, but if you want we can actually create other functions in our program
1293
01:38:17,810 --> 01:38:18,870
So make sure you guys have to do that
1294
01:38:18,870 --> 01:38:23,240
So let's go down and we're gonna go here outside of this function and the way you can tell we're outside
1295
01:38:23,240 --> 01:38:27,349
The function is we're outside of this ending curly bracket. So down here
1296
01:38:27,360 --> 01:38:30,650
I'm going to create a function and when we create a function
1297
01:38:30,650 --> 01:38:34,999
We actually have to give C a couple pieces of information
1298
01:38:35,190 --> 01:38:38,900
The first thing we have to tell C is the return type of the function
1299
01:38:39,120 --> 01:38:43,010
And so actually we're going to talk more about return type in the next tutorial
1300
01:38:43,080 --> 01:38:50,240
But for now just know that return type is basically the type of data that the function is going to return to the user
1301
01:38:50,240 --> 01:38:56,030
So sometimes your function can actually give information back to whoever calls it in our case though
1302
01:38:56,030 --> 01:39:01,670
We're just gonna say void and void basically means that this function isn't to return any information
1303
01:39:01,770 --> 01:39:07,249
That's the first thing that you have to tell see the second thing we have to do is give this function a name
1304
01:39:07,650 --> 01:39:12,829
And generally when we're naming a function, we're gonna want to name the function according to what it does
1305
01:39:13,440 --> 01:39:17,480
So in our case, we're gonna create a function that says hi to the user
1306
01:39:17,520 --> 01:39:21,620
So I'm just gonna call this function say hi because that's what its gonna do. It's gonna say hi
1307
01:39:22,140 --> 01:39:23,420
Whenever I create a function now
1308
01:39:23,420 --> 01:39:29,509
I want to make an open and closed parenthesis and I'm gonna make an open and closed curly bracket. So now any
1309
01:39:30,030 --> 01:39:36,710
Code that goes in between these curly brackets. It's going to be considered code that's inside of this function
1310
01:39:36,710 --> 01:39:41,599
So let me show you guys I'm just gonna make this a very simple function. We're gonna do one thing inside of here
1311
01:39:41,600 --> 01:39:45,710
We're just gonna make a printf and we're gonna print out hello user
1312
01:39:46,230 --> 01:39:52,969
So I've created my function it's called say hi and inside of this function. We're just printing out. Hello user
1313
01:39:53,280 --> 01:39:58,219
So now let's go ahead and run our program and see what happens. So I'm just gonna build and run my program
1314
01:39:59,010 --> 01:40:05,449
But you'll see up here that hello user doesn't get printed out right in other words. I came down here
1315
01:40:05,450 --> 01:40:07,410
I said printf hello user
1316
01:40:07,410 --> 01:40:13,280
But this didn't get printed out and this is the first lesson with functions is that in order to use the code?
1317
01:40:13,560 --> 01:40:15,800
Inside of the function we have to call it
1318
01:40:16,590 --> 01:40:22,429
Calling a function basically means we're telling C that we want to execute all the code inside of it
1319
01:40:22,620 --> 01:40:24,890
So if I want to execute this code I can
1320
01:40:25,140 --> 01:40:26,010
Call this function
1321
01:40:26,010 --> 01:40:30,530
And the way that I do that is just by typing out the name of the function and an open and closed parenthesis
1322
01:40:31,470 --> 01:40:37,880
So now instead of just printing out nothing C is actually going to go over here and execute all the code in the say hi
1323
01:40:38,190 --> 01:40:43,369
Function so let's run our code and you'll see now we're printing out. Hello a user
1324
01:40:43,980 --> 01:40:51,769
So the difference between this main function up here and this say hi function is this main function is a very special function
1325
01:40:51,770 --> 01:40:51,890
right
1326
01:40:51,890 --> 01:40:53,310
when we run our program
1327
01:40:53,310 --> 01:40:57,740
this main function basically gets called but then if we want to use other
1328
01:40:57,930 --> 01:41:03,050
Functions we can call them from inside of this main function and that's sort of how this is gonna work
1329
01:41:03,050 --> 01:41:06,949
So also I want to show you guys one other thing. I just want to illustrate
1330
01:41:08,320 --> 01:41:13,900
The flow that these functions take I want to show you guys how these get executed by the program
1331
01:41:13,900 --> 01:41:19,299
So I'm printing out top and I'm printing out bottom and I'm calling say hi right here
1332
01:41:19,300 --> 01:41:23,349
So now I'm gonna run my program again and you'll see we're printing out top
1333
01:41:23,780 --> 01:41:29,259
And then we're printing out hello user then we're printing out bottom and this might have looked a little bit better if I put new
1334
01:41:29,260 --> 01:41:33,460
Lines, but essentially what's happening is when C goes to execute this program
1335
01:41:33,800 --> 01:41:37,059
It's gonna execute this first line of code printf
1336
01:41:37,059 --> 01:41:41,619
So it's gonna print out top then we're telling C that we want to call
1337
01:41:41,929 --> 01:41:43,280
this a hi
1338
01:41:43,280 --> 01:41:50,320
Function C is now gonna jump over here to the say hi function and it's gonna execute all of the code inside of here
1339
01:41:50,320 --> 01:41:55,750
So in this case, we just have one line of code, but I could put any number of lines of code inside this function
1340
01:41:55,750 --> 01:41:59,829
So if I have like five lines of code, it would execute all five of those lines of code
1341
01:41:59,900 --> 01:42:05,469
Then it's gonna jump back up here and it's gonna move on to the next instruction. So that's kind of how that works
1342
01:42:05,469 --> 01:42:08,979
And that's sort of the basics of writing functions anytime
1343
01:42:08,980 --> 01:42:14,679
You have a block of code or a bunch of code that does one thing or that you want to kind of?
1344
01:42:14,989 --> 01:42:17,739
Encapsulate into its own little container that you can call
1345
01:42:17,929 --> 01:42:22,029
You can put it inside of a function and then whenever you want to access it
1346
01:42:22,030 --> 01:42:25,989
You just call it, but these functions can actually do a lot more
1347
01:42:25,989 --> 01:42:29,169
So one thing we can do is we can give these functions some
1348
01:42:29,449 --> 01:42:35,768
Information so I can actually give this function a piece of information and it can use that information to do different things
1349
01:42:35,869 --> 01:42:42,159
This is what's called a parameter and a parameter is basically just a value that we give to a function
1350
01:42:42,159 --> 01:42:45,729
So let's say in this say hi function instead of just saying hi to the user
1351
01:42:45,730 --> 01:42:48,309
We want it to say hi to someone specifically
1352
01:42:48,679 --> 01:42:49,179
well
1353
01:42:49,179 --> 01:42:53,679
I can actually come down here in these parentheses and I can specify a function
1354
01:42:54,139 --> 01:42:59,649
Parameter and I can specify a parameter a lot like I would specify a variable so I could say for example char
1355
01:43:00,139 --> 01:43:03,189
name and an open and closed square brackets
1356
01:43:03,190 --> 01:43:08,679
and I'm basically what I'm saying down here is this say hi function is going to take as a
1357
01:43:08,960 --> 01:43:11,799
Parameter in other words as an input a string
1358
01:43:12,139 --> 01:43:13,280
of characters
1359
01:43:13,280 --> 01:43:19,059
called name and what I can do is I can come down here and I can actually print that out so I could say
1360
01:43:19,250 --> 01:43:21,830
Percent s and we could print out name
1361
01:43:22,650 --> 01:43:23,880
now
1362
01:43:23,880 --> 01:43:29,210
Over here when I call this say hi function because I specified that it's gonna take a parameter
1363
01:43:29,310 --> 01:43:35,779
I have to give it a string so I could give this a string like Mike like I'll put my name in here
1364
01:43:36,239 --> 01:43:38,928
when I call this say hi function now, I'm
1365
01:43:39,449 --> 01:43:42,319
Passing the value Mike into this function
1366
01:43:42,320 --> 01:43:48,500
So Mike is gonna get stored inside of this name array and we're gonna be able to print it out down here
1367
01:43:48,659 --> 01:43:54,888
Let's go ahead and run our program and you'll see it says hello Mike. So instead of saying hello user now
1368
01:43:54,889 --> 01:43:56,989
We're able to customize what it says
1369
01:43:57,179 --> 01:44:00,049
The cool thing about this is I could copy this line of code
1370
01:44:00,150 --> 01:44:05,449
Like I could call this function a bunch of times with different names. Actually. I'm gonna put in a new line down here
1371
01:44:05,449 --> 01:44:08,749
So this is more obvious so I could say hello Mike. Hello, Tom
1372
01:44:09,270 --> 01:44:10,469
Hello
1373
01:44:10,469 --> 01:44:11,730
Oscar
1374
01:44:11,730 --> 01:44:13,730
now when I run my program
1375
01:44:14,100 --> 01:44:17,509
It's saying hello to each of these different people
1376
01:44:18,270 --> 01:44:22,399
because we define the function and we can pass it different names and
1377
01:44:22,679 --> 01:44:28,399
Depending on what we pass it depending on the parameter that we give it it'll perform its task a little bit differently
1378
01:44:28,949 --> 01:44:35,569
You can also specify multiple parameters. So let's say in addition to accepting the name as a parameter. We also accepted their age
1379
01:44:35,790 --> 01:44:37,790
So I could say char name
1380
01:44:38,250 --> 01:44:44,299
And then down here I can specify int age. And now we're basically saying that this is going to take an age as well
1381
01:44:44,520 --> 01:44:49,549
So I could say hello and then I could say you are and we'll say the age
1382
01:44:50,100 --> 01:44:55,129
So now over here instead of an addition to just saying name we can also say age
1383
01:44:55,710 --> 01:44:57,330
so now when I run this
1384
01:44:57,330 --> 01:45:01,489
Function and when I call it I have to also give this an age so we could say like 40
1385
01:45:03,120 --> 01:45:05,120
23 and
1386
01:45:05,639 --> 01:45:12,079
70 and now this is gonna print out all that information for us. So hello Mike you are 40. Hello. Tom. You're 23
1387
01:45:12,080 --> 01:45:18,439
Hello, Oscar, you're 70 and you can pass as many parameters as you want into one of these functions
1388
01:45:18,750 --> 01:45:22,189
You always just have to make sure though that you're passing a parameter
1389
01:45:22,710 --> 01:45:28,699
When you're calling the function just like that. So that's the basics of using functions and in the next tutorial. We're going to talk about
1390
01:45:29,310 --> 01:45:35,640
This guy over here, which is the return type and we're gonna look at how we can get information back from a function
1391
01:45:39,829 --> 01:45:40,610
In this tutorial
1392
01:45:40,610 --> 01:45:44,179
I'll talk to you guys about return statements in C
1393
01:45:44,489 --> 01:45:51,888
Where a turn statement is a special line of code that we can include in our functions which will allow them to return information
1394
01:45:52,199 --> 01:45:57,529
back to whoever called them so I can write a function and that can have that function give
1395
01:45:57,989 --> 01:46:04,428
information back to whoever called it that could be information like the result of some operation or you know
1396
01:46:04,429 --> 01:46:08,839
It could be it a message telling whoever called it how the function went. It could be anything like that
1397
01:46:09,239 --> 01:46:10,559
So I'm gonna show you guys
1398
01:46:10,559 --> 01:46:15,859
Basically how we can do that and I'll just give you an overview of like return types and all that stuff. So down here
1399
01:46:15,860 --> 01:46:19,130
I'm actually going to create a function and we're gonna create a function that
1400
01:46:19,409 --> 01:46:23,328
Cubes a number so when you cube a number generally you take it to the third power
1401
01:46:23,520 --> 01:46:25,729
So I could say like 2 raised to the third power
1402
01:46:26,340 --> 01:46:31,670
Which is the same as just saying 2 times 2 times 2 this would be cubing a number, right?
1403
01:46:31,670 --> 01:46:33,670
You're taking it to the third power
1404
01:46:33,809 --> 01:46:35,869
So I'm gonna make a function that's gonna do that
1405
01:46:35,909 --> 01:46:36,409
basically
1406
01:46:36,409 --> 01:46:38,340
This function will accept one
1407
01:46:38,340 --> 01:46:44,869
parameter which is gonna be a number and it'll cube that number and then once it's done cubing the number, it'll give
1408
01:46:45,090 --> 01:46:49,309
That value back to the caller and you guys will see how that will work
1409
01:46:49,309 --> 01:46:52,999
So I do want to point out one thing whenever we are
1410
01:46:53,790 --> 01:46:58,009
Returning values in our function. So if you're gonna write a function that's gonna return a value
1411
01:46:58,409 --> 01:47:02,989
You always want to put it above the function that's going to be calling it
1412
01:47:02,989 --> 01:47:07,489
So for example like this main function down here, this is the first function that executes
1413
01:47:07,679 --> 01:47:13,249
So any code we put in here is gonna get executed first if I want to return a value with my function
1414
01:47:13,250 --> 01:47:19,639
I'm gonna define the function here above the main method and there's a couple reasons why we want to do that essentially
1415
01:47:19,639 --> 01:47:21,979
It'll just make sure that everything works correctly
1416
01:47:22,079 --> 01:47:28,308
So we want to make sure that we define this function before we actually call it down here and get a value back
1417
01:47:28,309 --> 01:47:32,599
so the first thing that I need to tell C when I want to create a function is
1418
01:47:32,849 --> 01:47:37,369
What type of data I want to return if you've been following along with this course
1419
01:47:37,369 --> 01:47:39,369
You'll know in the last tutorial
1420
01:47:39,690 --> 01:47:45,500
We use the void return type which basically means we weren't going to return any information
1421
01:47:45,690 --> 01:47:48,109
But now we want to actually return information
1422
01:47:48,890 --> 01:47:50,770
So we want to Cuba number
1423
01:47:50,770 --> 01:47:55,720
so why don't we return a double and you can put any datatype here you want you can also put like int or
1424
01:47:56,390 --> 01:48:00,400
Character or character array, but let's just do double and we're gonna give this a name
1425
01:48:00,400 --> 01:48:06,039
So I'm gonna call this cube and I'm gonna make an open and closed parenthesis and inside of this parenthesis
1426
01:48:06,040 --> 01:48:11,320
We're gonna allow this method or we're gonna allow this function to accept one parameter as input
1427
01:48:11,510 --> 01:48:14,769
So it's just going to be another double and call it num. So
1428
01:48:15,350 --> 01:48:21,460
Basically, what we want to do is we want a cube num and we want to give that value back to the user so over
1429
01:48:21,460 --> 01:48:23,300
Here, I'm just gonna create a variable
1430
01:48:23,300 --> 01:48:27,820
It's just gonna be another double called result and I'm gonna set it equal to num
1431
01:48:29,180 --> 01:48:34,269
Times num times num. So basically this variable result now
1432
01:48:34,910 --> 01:48:39,099
represents or now stores the cubed value of num
1433
01:48:39,920 --> 01:48:43,059
all we need to do to return this value now is just say
1434
01:48:43,970 --> 01:48:45,140
return
1435
01:48:45,140 --> 01:48:49,510
Result and this return keyword is basically gonna do a couple things
1436
01:48:49,510 --> 01:48:54,159
The first thing it does is it breaks us out of the function. So whatever we put that return keyword
1437
01:48:54,160 --> 01:49:00,880
It's gonna break us out of the function and it's gonna return this value back to the caller
1438
01:49:01,070 --> 01:49:04,360
So down here we can actually call this function
1439
01:49:04,820 --> 01:49:10,029
so actually what I want to do is I want to print out the answer that we get back so I can do printf and
1440
01:49:10,610 --> 01:49:12,610
in here, I'm just gonna say
1441
01:49:12,950 --> 01:49:15,820
Answer and we'll print out the answer. So percent F
1442
01:49:16,130 --> 01:49:22,839
so I'm using % F because we're expecting to get a double back from here remembered double is a return type and
1443
01:49:23,600 --> 01:49:29,559
Over here. I can just say cube and we'll pass in a number. Let's pass in 3.0
1444
01:49:30,500 --> 01:49:34,869
Essentially what's gonna happen now is we're going to call this function
1445
01:49:34,870 --> 01:49:41,410
All this code is going to execute and this function is going to get a value back basically the value of cubing
1446
01:49:41,690 --> 01:49:47,949
3.0. So normally like in the past in this course what we wanted to print on a number we'd have to put like 4 or
1447
01:49:48,530 --> 01:49:52,000
76 or you know, whatever we'd have to put a number right there
1448
01:49:52,370 --> 01:49:59,499
But we can just call this function because eventually this is going to get a number back anyway
1449
01:49:59,500 --> 01:50:03,270
So this will contain a number after the functions done being called
1450
01:50:03,640 --> 01:50:09,179
So I can actually just run my program now and this is going to print out the result of cubing 3.0
1451
01:50:09,180 --> 01:50:09,930
So you'll see down here
1452
01:50:09,930 --> 01:50:13,110
we get 27 point 0 0 so
1453
01:50:13,570 --> 01:50:20,159
3 cubed is 27 3 times 3 is 9 9 times 3 27 so looks like our cube function works
1454
01:50:20,160 --> 01:50:22,950
Let's try with another number. Why don't we do 7?
1455
01:50:24,130 --> 01:50:28,830
So let's run this again and we get 343 I'm guessing that's right
1456
01:50:30,100 --> 01:50:38,009
So basically what we did is we created this function and we use this return keyword and we returned back to the caller
1457
01:50:38,140 --> 01:50:40,799
The value of cubing the number and that's really cool
1458
01:50:40,800 --> 01:50:44,820
And actually we can make this a lot simpler so I could actually just get rid of this
1459
01:50:45,250 --> 01:50:47,879
Result variable and I could just straight up return
1460
01:50:48,520 --> 01:50:54,449
Num cubed and this is and this is gonna do the same exact thing so we should get the same answer. Yeah
1461
01:50:55,540 --> 01:50:57,390
So this can be really awesome
1462
01:50:57,390 --> 01:51:02,309
And one thing I want to point out is this return keyword will actually break us out of the function
1463
01:51:02,310 --> 01:51:07,140
So if I came down here and I said like printf and I printed out like here
1464
01:51:07,810 --> 01:51:12,719
This actually this code is actually never gonna get executed. So if I was to run my program
1465
01:51:13,480 --> 01:51:20,489
You'll notice that it's not printing out here. So that never gets printed out in other words this line of code down here
1466
01:51:21,070 --> 01:51:26,670
Print out here. This never gets touched because when we use this return keyword
1467
01:51:27,190 --> 01:51:34,409
This breaks us out of the function. So whenever C sees this return keyword, it just exits the function and goes back
1468
01:51:34,930 --> 01:51:37,080
down here to the normal program
1469
01:51:37,300 --> 01:51:39,300
So just keep in mind that you can't
1470
01:51:39,400 --> 01:51:45,120
really put any code after you use this return keyword because it will break you out of the function and actually want to talk to
1471
01:51:45,120 --> 01:51:48,029
You guys about one more thing really quick you'll notice up here
1472
01:51:48,030 --> 01:51:54,090
I'm creating my cube function above the main function. So I actually created this above here
1473
01:51:54,090 --> 01:51:59,460
and the reason that I did that was because if I was to move this down here below the main function and
1474
01:51:59,980 --> 01:52:01,270
I try to run my program
1475
01:52:01,270 --> 01:52:06,269
you'll notice that we're getting an error over here and actually if we look at the error down here in the
1476
01:52:07,119 --> 01:52:09,119
Output it says error
1477
01:52:09,340 --> 01:52:12,029
Conflicting types for cube now
1478
01:52:12,249 --> 01:52:18,599
Essentially what's happening is when I create this function down here below this main method
1479
01:52:19,059 --> 01:52:23,129
When the main method over here tries to call it. It actually doesn't know
1480
01:52:23,679 --> 01:52:25,139
About this function yet
1481
01:52:25,139 --> 01:52:32,728
like it doesn't necessarily know like what this function is what it's going to return and what parameters it's gonna take because we
1482
01:52:33,010 --> 01:52:38,340
Created it after we created this main method. So what I can do is actually something called
1483
01:52:38,860 --> 01:52:44,579
Prototyping and when I prototype it'll allow me to create this function below this main function
1484
01:52:44,860 --> 01:52:49,829
without getting this error and basically when you prototype I'm basically just going to write out the
1485
01:52:50,289 --> 01:52:56,728
Function signature, so we would call this like the signature basically the return type and the parameters and the name
1486
01:52:56,729 --> 01:52:59,819
So if I was to put this up here and put a semicolon
1487
01:53:00,459 --> 01:53:02,459
now when I run my program
1488
01:53:02,829 --> 01:53:06,748
You'll see that we're not getting this error anymore, and we're actually getting the answer back
1489
01:53:06,749 --> 01:53:14,099
So this is a way that you can create functions below that main function without having any problems and you know
1490
01:53:14,099 --> 01:53:18,299
basically any functions that I created I could put another prototype up there at the top and
1491
01:53:19,030 --> 01:53:21,030
It's gonna be fine
1492
01:53:23,880 --> 01:53:28,859
In this tutorial and talked to you guys about if statements in C an if statement is a
1493
01:53:29,200 --> 01:53:36,780
Programming structure which we can use to help our programs to make decisions by using an if statement our programs can actually respond
1494
01:53:36,780 --> 01:53:41,309
So in certain circumstances, they can do certain things and in other circumstances
1495
01:53:41,310 --> 01:53:43,590
They can do other things and if statements are
1496
01:53:43,960 --> 01:53:50,250
Extremely powerful and they're a great way to add some intelligence to our programs for the purposes of this tutorial
1497
01:53:50,250 --> 01:53:55,260
We're gonna be building a function. So we're gonna build a function and it's gonna be called the max function
1498
01:53:55,660 --> 01:53:58,649
Basically what this function is gonna do is it's gonna take two parameters
1499
01:53:58,900 --> 01:54:04,950
two numbers and it's gonna tell us which number is the biggest so I could give it like four and I can give it a
1500
01:54:04,950 --> 01:54:11,700
Ten and it'll tell us which number is bigger. Let's go ahead and do that. I'm gonna make this function up here and
1501
01:54:12,550 --> 01:54:19,050
It's going to return an integer and I'm just gonna call it max and it's gonna take as parameters two integers
1502
01:54:19,050 --> 01:54:24,059
so it's gonna take an integer num one and it's gonna take another integer num two and
1503
01:54:24,910 --> 01:54:27,599
Now we'll just make the actual function body
1504
01:54:27,600 --> 01:54:34,079
so inside this function our job is to figure out whether num one is the biggest or num two is the biggest and we're going
1505
01:54:34,080 --> 01:54:37,109
To return the biggest to the caller
1506
01:54:37,480 --> 01:54:42,390
so the first thing I'm gonna do is create a variable and it's gonna be an integer and I'm just gonna call it result and
1507
01:54:42,850 --> 01:54:44,850
I'm not gonna give it a value right away
1508
01:54:45,190 --> 01:54:49,830
What we need to do in this function is we need to figure out which one's bigger
1509
01:54:49,830 --> 01:54:51,930
we need to figure out if num one is the biggest or
1510
01:54:52,420 --> 01:54:56,640
We need to figure out if num two is the biggest and this is a perfect situation
1511
01:54:56,830 --> 01:55:01,590
To use an if statement an if statement allows us to check a certain condition
1512
01:55:01,590 --> 01:55:08,459
And if that conditions true we can do one thing and if the conditions false we can do another thing so down here
1513
01:55:08,460 --> 01:55:09,450
I'm gonna use an if statement
1514
01:55:09,450 --> 01:55:14,550
I'm basically just gonna say if and then we're gonna make an open and close parenthesis and I'm gonna make an open and closed
1515
01:55:14,650 --> 01:55:16,650
curly bracket now
1516
01:55:16,810 --> 01:55:20,789
Inside of here inside of these parentheses. We need to specify a condition
1517
01:55:21,340 --> 01:55:27,599
This is something that's either gonna be true or false if the condition inside of these parentheses is true
1518
01:55:27,790 --> 01:55:33,510
We're gonna execute the code inside of these curly brackets. If it's false, then we're just gonna move on
1519
01:55:33,970 --> 01:55:34,840
so
1520
01:55:34,840 --> 01:55:41,590
In order to tell whether or not num1 is bigger than num2 or num 2 is bigger than num 1 we can actually compare them
1521
01:55:41,690 --> 01:55:43,690
so down here I could say if
1522
01:55:43,969 --> 01:55:46,809
Num 1 and I can use this greater than sign
1523
01:55:47,510 --> 01:55:52,390
Num2. I'm basically saying if num 1 is greater than num2
1524
01:55:53,180 --> 01:55:59,409
then I want to do something so down here we can set result equal to num 1 if
1525
01:55:59,900 --> 01:56:03,730
This condition is true. Then we're gonna do this. So only when
1526
01:56:04,340 --> 01:56:11,380
num, 1 is greater than num2 are we gonna set result equal to num 1 so basically by using this if statement we're able to
1527
01:56:11,380 --> 01:56:14,830
Tell which one is bigger but here's the thing what if num 1 isn't?
1528
01:56:14,930 --> 01:56:18,069
Greater than num - what if num 2 is greater than num 1?
1529
01:56:18,290 --> 01:56:18,790
well
1530
01:56:18,790 --> 01:56:23,019
I can use something called an else statement so I can come down here and I could say else and
1531
01:56:23,120 --> 01:56:25,870
I don't actually need to use an open and close parentheses
1532
01:56:25,870 --> 01:56:27,760
I can just make an open and close curly bracket
1533
01:56:27,760 --> 01:56:34,360
And the code inside of this else block is going to execute if the condition up here is false
1534
01:56:34,580 --> 01:56:40,930
so if this condition is false if num 1 is not greater than num 2 then we can just
1535
01:56:41,330 --> 01:56:46,989
Set result equal to num 2 and then down here. Finally. We're gonna return
1536
01:56:47,690 --> 01:56:50,830
Result so depending on whether or not this is true
1537
01:56:51,620 --> 01:56:56,680
result is either gonna set equal to num 1 or it's gonna be set equal to num -
1538
01:56:56,900 --> 01:57:03,339
So let's go down here into our main method and why don't we call this? So I'm actually gonna print out the result
1539
01:57:03,340 --> 01:57:05,060
So I'm just gonna say
1540
01:57:05,060 --> 01:57:07,839
printf and we're gonna be printing out an integer and
1541
01:57:08,719 --> 01:57:14,109
Over here, I'm gonna call this function. So I'm just gonna say Max and I'm gonna pass in two numbers
1542
01:57:14,110 --> 01:57:16,390
So we're passing a 4 it will pass in a 10
1543
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 return to us whichever one is bigger in
1544
01:57:23,420 --> 01:57:29,859
Other words this should return 10. So let's go ahead and run our program and you'll see over here. We're returning 10
1545
01:57:29,860 --> 01:57:35,170
So we got the value of 10 back from this function. Let's try it with the other number
1546
01:57:35,170 --> 01:57:38,170
So let's make the first number bigger. So now we have 40 and 10
1547
01:57:38,300 --> 01:57:44,320
So this should give us 40 back awesome so over here we have an awesome function, right?
1548
01:57:44,320 --> 01:57:49,210
We're basically checking to see if num 1 is greater than num - if it is, then we're gonna return num 1
1549
01:57:49,460 --> 01:57:51,019
otherwise we're going to
1550
01:57:51,019 --> 01:57:55,179
Num2 and it's important to note that even if I made these equal
1551
01:57:55,179 --> 01:57:59,949
So if I made it forty and forty, we're still going to get back the correct answer
1552
01:57:59,949 --> 01:58:04,658
So it's still gonna give us forty back. So this is the basics of using if statements, right?
1553
01:58:04,659 --> 01:58:09,279
We say if we specify a condition if that conditions true
1554
01:58:09,349 --> 01:58:12,459
We're gonna execute this code if that conditions false
1555
01:58:12,590 --> 01:58:14,829
We're gonna execute this code down here
1556
01:58:15,019 --> 01:58:22,089
And that is an extremely powerful structure in our programs and we can really use this to respond to different input
1557
01:58:22,090 --> 01:58:22,239
right
1558
01:58:22,239 --> 01:58:27,308
So depending on what the user puts in num1 and num2 we can do different things
1559
01:58:27,769 --> 01:58:32,739
But this is just sort of the beginning. So why don't we try to make this function a little bit more complex?
1560
01:58:32,900 --> 01:58:36,940
So down here we're allowing the user to input two parameters
1561
01:58:36,940 --> 01:58:43,179
So we're accepting two parameters into this max function num1 and num2 and we're able to spit out which one is bigger
1562
01:58:43,579 --> 01:58:48,879
But let's up the ante a little bit what if we wanted to accept three parameters?
1563
01:58:48,880 --> 01:58:50,920
So instead of just passing in two numbers
1564
01:58:50,960 --> 01:58:57,069
We wanted to be able to pass in three numbers and have the max function spit out the biggest so over here we can modify
1565
01:58:57,070 --> 01:59:04,509
This max function, so I'm just gonna make another parameter. So I'm going to say int and we'll call this one num three
1566
01:59:04,510 --> 01:59:09,519
So now this function is accepting three parameters num1 num2 and num three
1567
01:59:09,889 --> 01:59:16,268
But in order to figure out which one of these is the biggest we're actually gonna need a more complex if statement
1568
01:59:16,269 --> 01:59:20,379
So I'm gonna get rid of this and let's just start from scratch. So let's think about this
1569
01:59:20,380 --> 01:59:23,589
How can we figure out which one of these is the biggest?
1570
01:59:24,019 --> 01:59:28,779
well, what I would recommend is we could say if num 1 is greater than num2 and
1571
01:59:29,570 --> 01:59:33,699
Num, 1 is greater than num 3 then we know num ones the biggest right?
1572
01:59:33,699 --> 01:59:35,768
So if num1 is bigger than num2 and it's bigger than um
1573
01:59:35,769 --> 01:59:39,279
3 we know it's the biggest if num2 is greater than num1 and num2
1574
01:59:40,099 --> 01:59:45,999
Is greater than num 3 then we know num2 is the biggest and otherwise we can just say that num
1575
01:59:46,130 --> 01:59:51,429
3 is the biggest so I'm gonna show you guys how to do this and we're actually going to be introducing a couple more
1576
01:59:51,769 --> 01:59:56,589
Concepts for if statement so I'm just gonna say if and I want to check a condition
1577
01:59:56,590 --> 01:59:59,289
so I basically want to see if num 1 is greater than
1578
01:59:59,780 --> 02:00:04,850
or equal to num 2 and num threes so down here I can say num one is
1579
02:00:05,250 --> 02:00:12,230
Greater than or equal to num2 and now I also want to check to see if num1 is greater than or equal to number three
1580
02:00:12,600 --> 02:00:17,720
Because if num1 is greater than or equal to num2 and it's greater than or equal to number three
1581
02:00:17,730 --> 02:00:24,679
We know num1 is the biggest so I can use this special signal here. This is called a logical operator and
1582
02:00:25,440 --> 02:00:31,639
Basically, this is going to allow us to put another condition in here so I could say num 1 is greater than or equal to
1583
02:00:31,640 --> 02:00:33,210
num 3
1584
02:00:33,210 --> 02:00:38,569
basically what this and does is it allows us to put two conditions so we can check two things and
1585
02:00:38,700 --> 02:00:45,530
This whole thing is only going to be true if this conditions true and this conditions sure
1586
02:00:45,540 --> 02:00:51,110
So if one of these guys is false, then this if statement isn't going to get executed so down here
1587
02:00:51,110 --> 02:00:54,650
I'm just gonna open this up and if this is the case that we want to set result
1588
02:00:55,170 --> 02:01:00,469
equal to num 1 because if num 1 is greater than or equal to num - and
1589
02:01:01,260 --> 02:01:07,159
Num, 1 is also greater than or equal to number 3 then we know num one's the biggest. Here's the thing
1590
02:01:07,160 --> 02:01:10,430
We also want to check to see if num 2 is the biggest
1591
02:01:10,430 --> 02:01:12,420
So if this is false
1592
02:01:12,420 --> 02:01:18,739
We also want to check to see if num 2 is the biggest now we can use something called else if so down here
1593
02:01:18,740 --> 02:01:20,740
I could type out else if and
1594
02:01:21,060 --> 02:01:27,320
I can make another open and closed parenthesis and inside of these parentheses. We can actually check another condition
1595
02:01:28,080 --> 02:01:35,390
So if this stuff up here at this conditions false then else if is saying let's come down here. Let's check another condition
1596
02:01:36,210 --> 02:01:38,210
So here I want to do basically the same thing
1597
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 and
1598
02:01:43,530 --> 02:01:47,420
Num 2 is greater than or equal to num 3
1599
02:01:48,030 --> 02:01:54,050
So if this condition is true if num 2 is greater than or equal to num 1 and it's greater than equal to num 3
1600
02:01:54,210 --> 02:02:00,529
Then we know for a fact that result is gonna be num - because that's the biggest now finally down here
1601
02:02:00,530 --> 02:02:04,130
We can just say else and we can make another to open and close curly bracket
1602
02:02:04,130 --> 02:02:09,259
And remember this code is going to get executed when neither of these conditions is true
1603
02:02:09,360 --> 02:02:13,249
So here we can just set result equal to num 3
1604
02:02:14,130 --> 02:02:16,819
So let me walk you guys through this one more time up here
1605
02:02:16,820 --> 02:02:21,829
We have this if statement and in addition to checking to see if num1 is greater than or equal to num2
1606
02:02:22,140 --> 02:02:29,329
we're also using this special operator called and and we're checking to see if num1 is greater than or equal to num 3 if
1607
02:02:29,430 --> 02:02:33,289
That's the case then result is going to be equal to num 1
1608
02:02:33,990 --> 02:02:36,320
Otherwise if this condition is false
1609
02:02:36,840 --> 02:02:39,380
Then we're gonna come down here and we're going to check this other condition
1610
02:02:39,690 --> 02:02:44,870
So we're gonna check to see if num 2 is bigger than num 1 and if it's bigger than num
1611
02:02:45,060 --> 02:02:47,600
3 if that's true, then we're gonna do this
1612
02:02:48,210 --> 02:02:51,320
Finally if neither of these conditions up here is true
1613
02:02:51,360 --> 02:02:58,550
We're just gonna go ahead and set result equal to num 3 and then we're gonna return results. So this looks good to me
1614
02:02:58,910 --> 02:03:04,700
Let's test this out and we're gonna see if it works. So now when we call max we need to include three numbers
1615
02:03:04,700 --> 02:03:06,360
So why don't we just do 1?
1616
02:03:06,360 --> 02:03:07,860
2 & 3
1617
02:03:07,860 --> 02:03:08,970
and
1618
02:03:08,970 --> 02:03:12,859
This is basically going to spit out the biggest one, which should be 3 so
1619
02:03:12,860 --> 02:03:14,750
Let's run our program and you can see over here
1620
02:03:14,750 --> 02:03:19,370
we get 3 so let's try to make one of the other ones the biggest so we'll make the 1 in the first slot the
1621
02:03:19,370 --> 02:03:24,649
Biggest and let's run this again and you can see that it returns 10. So this function is working
1622
02:03:24,960 --> 02:03:31,609
So that's how we can use these ands and we can also use else--if in order to check additional conditions
1623
02:03:31,610 --> 02:03:36,110
so the and is used to check more than one condition inside of the if
1624
02:03:36,810 --> 02:03:44,330
Parentheses and the else if is used to check another condition when this conditions false and so that is actually gonna work out pretty well
1625
02:03:44,670 --> 02:03:46,940
Now I want to show you guys a couple other things
1626
02:03:46,940 --> 02:03:50,930
There's a couple other things that we can do with these if statements
1627
02:03:50,930 --> 02:03:51,860
so for example
1628
02:03:51,860 --> 02:03:53,860
I'm gonna come down here and just show you guys some of these
1629
02:03:54,000 --> 02:03:57,859
In addition to using that and we can also use something called or so
1630
02:03:57,900 --> 02:04:01,010
I'm just gonna type out a simple if statement I could say if
1631
02:04:01,680 --> 02:04:09,619
3 is greater than 2 and instead of using and like this I can use or and or is basically gonna allow us to
1632
02:04:09,810 --> 02:04:12,919
Check two conditions, and the whole thing is going to be true
1633
02:04:12,920 --> 02:04:19,670
if only one of those conditions true so I could say if 3 is greater than 2 or 2 is greater than 5
1634
02:04:20,220 --> 02:04:25,189
So only one of these is true, right 3 is greater than 2 that's true
1635
02:04:25,190 --> 02:04:30,169
but 2 is not greater than 5 so this guy's false, but when we use or
1636
02:04:30,700 --> 02:04:38,409
One of these two conditions has to be true for the whole thing to be true. So if I like printed something out here like
1637
02:04:39,530 --> 02:04:41,030
true
1638
02:04:41,030 --> 02:04:47,590
Now when I run my program we should print true because we're using that or and you see that we do
1639
02:04:48,230 --> 02:04:52,750
But if I was to make this guy false, so if I made this 3 less than 2 now
1640
02:04:52,750 --> 02:04:56,920
It's not gonna print out true and we can just print out false. So
1641
02:04:57,890 --> 02:05:02,410
Now we're gonna end up renting out false because both of these guys are false
1642
02:05:02,960 --> 02:05:09,430
So you can see down here. We're saying false. So the difference between and and or when we have and like this
1643
02:05:10,100 --> 02:05:15,850
Both of these conditions have to be true in order for the whole thing to be true and for us to execute this code
1644
02:05:16,190 --> 02:05:20,109
When we have or only one of these guys has to be true
1645
02:05:20,330 --> 02:05:24,309
so that's kind of the difference there and I want to show you guys a couple other things that we can use so
1646
02:05:24,830 --> 02:05:27,490
Over here we're using like a less than sign
1647
02:05:27,490 --> 02:05:28,420
so for example
1648
02:05:28,420 --> 02:05:33,879
If 3 is less than 2 I can also use a greater than sign I could use the less than or equal to sign. I
1649
02:05:34,490 --> 02:05:39,070
Could use also a greater than or equal to sign and if I wanted to check for equality
1650
02:05:39,070 --> 02:05:41,860
I could use a double I could use a double equals
1651
02:05:41,860 --> 02:05:48,909
So the double equals will check to see if 3 is equal to 2 we can also use one more which is this
1652
02:05:49,430 --> 02:05:54,010
exclamation point an exclamation point basically means not equals so I could say if
1653
02:05:54,410 --> 02:06:00,369
3 is not equal to 2 then we'll print out true so down here. Let's run this and you'll see that we get true
1654
02:06:01,430 --> 02:06:06,070
So yeah, there you go. One more thing. I want to show you is how we can negate an entire
1655
02:06:06,740 --> 02:06:11,709
Operation. So for example, if I said 3 is greater than 2 this is gonna be true
1656
02:06:11,870 --> 02:06:12,370
right
1657
02:06:12,370 --> 02:06:16,150
so we'd end up printing out true down here because 3 is greater than 2 but
1658
02:06:16,430 --> 02:06:18,610
I could surround this whole thing with
1659
02:06:18,830 --> 02:06:26,740
Parentheses and I could put an exclamation point right before this and this is called the negation operator and it's basically going to negate
1660
02:06:26,930 --> 02:06:29,590
Whatever this ends up being so this is true
1661
02:06:29,590 --> 02:06:34,900
And we put this negation operator here this whole thing is going to be false. And so now you'll see
1662
02:06:35,420 --> 02:06:40,119
Because this is true and we're using the negation operator. We're not going to end up printing out true
1663
02:06:41,780 --> 02:06:43,809
And you can see we just don't print
1664
02:06:44,440 --> 02:06:48,249
But if I put a false condition in here, like if I said three is less than two
1665
02:06:48,470 --> 02:06:53,409
This is false. But because we're negating it now, it's gonna end up being true
1666
02:06:53,410 --> 02:06:56,530
So when I run my program you can see that we get true
1667
02:06:56,530 --> 02:07:01,300
So that's sort of the basics of using if statements and I cover just about everything that you can do
1668
02:07:01,300 --> 02:07:04,809
We can use ands, we can use all these different comparison operators
1669
02:07:04,810 --> 02:07:09,130
We can use ORS and we can use this exclamation point to negate something
1670
02:07:13,840 --> 02:07:20,290
In this tutorial I'm going to teach you guys how to build a calculator in see have you been following along with this course?
1671
02:07:20,290 --> 02:07:25,689
You'll know that in the beginning of the course. We actually created a calculator and it was very basic calculator
1672
02:07:25,690 --> 02:07:26,170
basically
1673
02:07:26,170 --> 02:07:32,350
We let the user input two numbers and we took those numbers we added them together and we printed the answer out onto the screen
1674
02:07:32,350 --> 02:07:33,640
in this tutorial
1675
02:07:33,640 --> 02:07:38,109
I'm gonna take some of the stuff that we've learned since then and show you guys how we can build a fully
1676
02:07:38,330 --> 02:07:40,330
functional for function calculator
1677
02:07:40,330 --> 02:07:42,260
So this calculator will be able to add
1678
02:07:42,260 --> 02:07:47,679
Subtract multiply and divide and we're gonna let the user decide which one they want to do
1679
02:07:47,680 --> 02:07:50,109
So we'll let them decide if they want to add or subtract or whatever
1680
02:07:50,180 --> 02:07:55,030
it's gonna be pretty cool and we're gonna end up using some of the stuff that we've learned recently in the course like if
1681
02:07:55,130 --> 02:08:01,150
statements we're also gonna use like getting input from users and I'm gonna show you guys how you can get characters as
1682
02:08:01,430 --> 02:08:07,059
Input from a user as well. So down here. We want to start making our calculator, basically
1683
02:08:07,060 --> 02:08:09,459
What I'm gonna do is I'm gonna ask the user to enter a number
1684
02:08:09,800 --> 02:08:15,309
Then I'm gonna ask them to enter an operator like plus minus division or subtraction finally
1685
02:08:15,310 --> 02:08:17,740
we're gonna ask them to enter in a third number and then we'll
1686
02:08:18,260 --> 02:08:25,989
Depending on what operator they specify so addition and subtraction whatever we will perform the correct operation and we'll print out the number
1687
02:08:26,120 --> 02:08:28,120
So the first thing I want to do is create
1688
02:08:28,400 --> 02:08:35,050
Variables where we can store the numbers and the operator so I'm gonna make some double variables and I'm just gonna call this one
1689
02:08:35,200 --> 02:08:38,740
Num 1 and then we'll make another double call it num2
1690
02:08:39,950 --> 02:08:45,609
finally, we'll make a variable that will store the operator that they enter so this is just gonna be a char and I'm just gonna
1691
02:08:45,610 --> 02:08:46,910
Call it op4
1692
02:08:46,910 --> 02:08:50,950
Operator. All right. So now we want to actually get input from the user
1693
02:08:50,950 --> 02:08:56,649
I want to figure out what numbers they want to use and then what operator they want to use as well
1694
02:08:56,650 --> 02:08:59,230
So down here. Why don't we get some input?
1695
02:08:59,230 --> 02:09:04,060
The first thing I'm going to do is I'm just gonna print out a prompt so I'm just gonna say enter a number
1696
02:09:05,660 --> 02:09:08,740
And now what we want to do is we want to be able to
1697
02:09:09,140 --> 02:09:14,109
Take the number they give us and store it inside of one of those doubles that we created
1698
02:09:14,110 --> 02:09:16,110
So I'm just gonna use this scanf function
1699
02:09:16,940 --> 02:09:21,220
Scanf and in here I'm gonna specify that we want to get a double
1700
02:09:21,220 --> 02:09:25,629
So remember when we're using scanf if we want to scan for a double we use
1701
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 comma and we'll put the
1702
02:09:32,780 --> 02:09:34,449
Variable that we want to store in here
1703
02:09:34,449 --> 02:09:38,259
so I'm gonna use this ampersand and I'm just gonna say num 1
1704
02:09:38,260 --> 02:09:43,659
So this is exactly what we need to do to be able to get the users input and store it into num 1
1705
02:09:44,090 --> 02:09:49,989
The next thing we want to do is get the operator. So I'm gonna type out another prompt and I'm just gonna say enter
1706
02:09:51,409 --> 02:09:52,630
operator basically
1707
02:09:52,630 --> 02:09:58,390
They'll be entering in plus sign minus sign asterisks or forward slash depending on what they want to do
1708
02:09:58,390 --> 02:10:02,049
and again now we're going to scan for a
1709
02:10:02,390 --> 02:10:07,959
Character now when we're scanning for a character and scanf and actually I don't think i've talked about this yet in this course
1710
02:10:08,120 --> 02:10:14,769
We want to do something special. So I want to put a % and a C. But before I put % C
1711
02:10:14,770 --> 02:10:21,819
I want to put a space so whenever we're getting a character from the user using scanf, we always want to put a space
1712
02:10:21,820 --> 02:10:28,600
Right here before % c otherwise, it's not gonna work. And again we want to store this in that Opie variable
1713
02:10:28,600 --> 02:10:30,600
so I'm just gonna say o P and
1714
02:10:31,010 --> 02:10:36,550
One more time we're gonna get another number. So I'm just gonna copy this paste this down here and
1715
02:10:37,670 --> 02:10:39,819
We're gonna get this one for num2
1716
02:10:40,880 --> 02:10:45,699
So now we should have all of the input for our program. So I'm getting the first number
1717
02:10:45,699 --> 02:10:51,399
I'm getting the operator and I'm getting the second number. The last thing we have to do now is actually do the math
1718
02:10:51,560 --> 02:10:56,289
So we're gonna have to figure out which operator they wanted to use
1719
02:10:56,290 --> 02:11:03,069
Right, so we have this Opie variable and this is storing like a plus sign a minus sign a Asterix or a forward slash
1720
02:11:03,350 --> 02:11:05,919
So depending on what that's storing we want to print something
1721
02:11:06,530 --> 02:11:12,370
Different out so we can actually use an if statement to do this so I can use an if statement to check and see
1722
02:11:12,860 --> 02:11:16,779
What operator is inside of our operator variable and depending on which one it is?
1723
02:11:16,780 --> 02:11:18,020
We can do a different
1724
02:11:18,020 --> 02:11:23,859
Operation so I can say if and the first thing I'm going to do is just check to see if the operator is equal
1725
02:11:23,960 --> 02:11:25,040
to a
1726
02:11:25,040 --> 02:11:26,330
plus sign so
1727
02:11:26,330 --> 02:11:31,359
If the operator that the user entered is equal to a plus sign then we're gonna want to add the two numbers together
1728
02:11:31,460 --> 02:11:36,819
So I'm just gonna print out these two numbers and I'll just say num1 plus num2
1729
02:11:37,340 --> 02:11:41,710
Because we're gonna add both of them together. We can also use an L safe
1730
02:11:41,710 --> 02:11:43,540
So I'm going to want to check a few other conditions
1731
02:11:43,540 --> 02:11:46,330
I'm going to check to see if the operator is equal to a
1732
02:11:46,790 --> 02:11:47,600
minus
1733
02:11:47,600 --> 02:11:53,559
Sign and if the operator is equal to a minus sign then instead of adding the numbers we're going to subtract them so over here
1734
02:11:53,660 --> 02:11:56,559
I'll just copy this and we'll paste this guy down here
1735
02:11:57,110 --> 02:11:59,589
So it's going to be num1 - num2
1736
02:12:00,500 --> 02:12:07,299
And I can actually just copy this whole thing and we'll paste it down here. So it makes more room down here
1737
02:12:07,300 --> 02:12:12,250
We'll paste this other elsif and here we're going to check to see if it's division
1738
02:12:12,250 --> 02:12:16,089
So we'll check for a forward slash. And again, we're just going to want to print out a
1739
02:12:16,700 --> 02:12:19,479
Decimal number so it's going to be num1 divided by num2
1740
02:12:20,420 --> 02:12:25,450
Finally, we're gonna check for multiplication. So if it's multiplication, then we're going to multiply them together
1741
02:12:25,880 --> 02:12:31,540
So now we're checking for each of the operators. We're checking for addition subtraction multiplication and division
1742
02:12:32,030 --> 02:12:36,699
But here's the thing what happens if the user enters in an operator that we didn't want
1743
02:12:36,700 --> 02:12:39,760
so for example if they don't enter in any of these operators
1744
02:12:39,800 --> 02:12:43,930
We're gonna want to tell them so I'm gonna have this like print out a little error message
1745
02:12:44,000 --> 02:12:48,790
We're just gonna say else and basically the code inside this else block will get executed
1746
02:12:48,790 --> 02:12:55,930
If none of these conditions up here true and down here we can just make a print F and I'm just gonna print out invalid
1747
02:12:56,720 --> 02:12:57,890
operator
1748
02:12:57,890 --> 02:13:05,499
So now we have our if statement or if block setup and this should be everything that we need to use our little calculator
1749
02:13:06,110 --> 02:13:09,700
Alright, so let's go ahead and run this program so you can see over here
1750
02:13:09,700 --> 02:13:13,869
It says enter a number so we can enter a number. Let's enter in five point nine and
1751
02:13:15,230 --> 02:13:19,510
Enter an operator we'll add numbers and now let's enter in 4.0
1752
02:13:19,510 --> 02:13:21,760
So we're gonna add five point nine and four
1753
02:13:21,950 --> 02:13:25,030
so when I click enter we should go through all of those if
1754
02:13:25,190 --> 02:13:30,879
Statements and figure out what we need to do and you'll see over here that we're adding five point nine and four together
1755
02:13:30,880 --> 02:13:36,490
And we're getting nine point nine. So looks like the program worked. Let's try it again. We'll try another operator
1756
02:13:36,490 --> 02:13:39,490
Why don't we try to multiply some numbers? So let's multiply
1757
02:13:40,100 --> 02:13:42,100
six and
1758
02:13:42,950 --> 02:13:48,220
Times I don't know five point seven so we get thirty four point two that seems about right
1759
02:13:48,770 --> 02:13:53,829
All right, and then one more time we'll run this and I want to try to enter in an invalid operator
1760
02:13:53,829 --> 02:13:58,869
So we'll say like 5.7 and I'm just gonna enter in a G. So that's an invalid operator
1761
02:13:58,869 --> 02:14:05,259
That's not going to work and I'll enter my number. Let's do 8 and you can see it tells us invalid operator
1762
02:14:05,260 --> 02:14:06,039
so basically
1763
02:14:06,039 --> 02:14:10,419
we have a four function calculator this calculator can multiply divide subtract and
1764
02:14:10,789 --> 02:14:13,869
Add and if you don't enter in a correct operator
1765
02:14:13,869 --> 02:14:17,739
It's smart enough to yell at you and tell you that you have an invalid operator. So
1766
02:14:18,320 --> 02:14:20,559
This seems to work pretty well. I'm pretty happy with it
1767
02:14:20,559 --> 02:14:25,929
And you can see down here like this is a perfect situation for using something like an if statement, right?
1768
02:14:25,929 --> 02:14:29,589
We have this variable opie, right? It's storing some sort of operator
1769
02:14:29,749 --> 02:14:32,499
We don't necessarily know what's inside of there
1770
02:14:32,499 --> 02:14:36,699
But I can use if statements and I can respond to the different scenarios
1771
02:14:37,130 --> 02:14:43,210
So in the situation where it's a plus sign I can respond in the situation where it's a minus sign I can respond etc
1772
02:14:43,210 --> 02:14:49,449
And this just makes our programs a lot smarter and it helps us to do different things when different situations arise
1773
02:14:53,719 --> 02:14:58,459
In this tutorial I want to talk to you guys about using switch statements in C
1774
02:14:58,620 --> 02:15:00,979
now a switch statement is basically a
1775
02:15:01,230 --> 02:15:07,370
Special type of if statement which will allow us to compare one value to a bunch of different values
1776
02:15:07,590 --> 02:15:10,400
And it's essentially doing the same thing as an if statement
1777
02:15:10,400 --> 02:15:14,779
It's allowing us to check different conditions and if different conditions are true
1778
02:15:14,780 --> 02:15:20,540
Then we can do certain things but a switch statement makes it really really easy for us to compare one
1779
02:15:21,060 --> 02:15:24,080
specific value to a bunch of other values now
1780
02:15:24,080 --> 02:15:28,309
I'm gonna show you guys how we can use that in this tutorial to make a little test app
1781
02:15:28,350 --> 02:15:33,949
So I'm gonna build a little program that will take a letter grade and depending on the letter grade
1782
02:15:33,949 --> 02:15:36,889
It will tell you whether or not you did good or bad on a test
1783
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 job
1784
02:15:40,230 --> 02:15:42,770
If you got a B on its house, it'd be like hey you did
1785
02:15:42,770 --> 02:15:46,040
Alright, if you got an F on the test to tell you that you failed the test
1786
02:15:46,219 --> 02:15:52,669
so we're basically gonna have a variable where we'll store a grade that you would get on a test and we'll be able to
1787
02:15:52,860 --> 02:15:56,960
Respond to that grade using a switch statement. So it's gonna be pretty cool. So down here
1788
02:15:56,960 --> 02:16:02,149
the first thing I'm gonna do is create a variable and it's just gonna be a character and I'm just gonna call it grade and
1789
02:16:02,400 --> 02:16:04,520
I'm gonna set it equal to a
1790
02:16:05,190 --> 02:16:10,069
So we're gonna say that whoever took this test got an a on the test and they did really well
1791
02:16:10,530 --> 02:16:14,449
Now what I want to do is I want to create a switch statement. So I'm just gonna say
1792
02:16:15,540 --> 02:16:19,910
Switch and I'm gonna make an open and closed parenthesis and inside of this parenthesis
1793
02:16:19,910 --> 02:16:25,489
I'm gonna put grade so I'm just gonna put grade and I'm gonna make an opening close curly bracket
1794
02:16:25,500 --> 02:16:31,459
So this is sort of like the basic structure for our switch statement. Now, here's what I'm basically gonna do
1795
02:16:31,460 --> 02:16:38,929
I'm gonna take this variable here that I pass into the parentheses grade and I'm gonna compare it to a bunch of different values
1796
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
1797
02:16:41,689 --> 02:16:45,319
then I'm gonna see if it's equal to a different value or a different value or a different value and
1798
02:16:45,570 --> 02:16:51,889
Depending on the value that it ends up being equal to we're gonna do something different and these are what are called
1799
02:16:52,139 --> 02:16:54,589
cases so down here I can say case and
1800
02:16:55,170 --> 02:16:57,170
I just want to type out a
1801
02:16:57,660 --> 02:17:01,879
Character so for example, I could put a right here and then I'm gonna put a colon
1802
02:17:02,790 --> 02:17:09,339
and when I put a right here, basically what this is saying is the case that grade is equal to a
1803
02:17:09,920 --> 02:17:14,950
then I'm gonna put some code down here that I want to execute so I could just put like a print statement and
1804
02:17:15,769 --> 02:17:18,339
I could say like you did great
1805
02:17:19,340 --> 02:17:21,429
because they did great on the test and
1806
02:17:21,830 --> 02:17:22,670
basically
1807
02:17:22,670 --> 02:17:28,750
what this is saying is in the case that the grade is equal to a we're gonna print this out and then I want to
1808
02:17:28,750 --> 02:17:29,800
Put one more thing down here
1809
02:17:29,800 --> 02:17:33,429
Which is called break and I'm gonna explain what break does in a second
1810
02:17:33,830 --> 02:17:37,779
So I could make one of these little case statements for every possibility
1811
02:17:37,779 --> 02:17:43,149
so for every possible grade that we could get so I'm actually just gonna copy this whole thing and
1812
02:17:43,790 --> 02:17:45,790
I'm gonna paste it down here
1813
02:17:46,189 --> 02:17:52,329
So now I can put a B in here. So we're saying in the case that the grade is equal to B
1814
02:17:52,330 --> 02:17:54,910
I'm gonna say you did. All right, and
1815
02:17:56,179 --> 02:17:59,709
Then we're also going to break and down here. I'm gonna make another one
1816
02:17:59,710 --> 02:18:05,620
So I'll say case and this time we'll make it C in the case that they get a C. I'm gonna print out
1817
02:18:06,649 --> 02:18:08,269
portal e and
1818
02:18:08,269 --> 02:18:12,909
There's a couple more that we can check so we can check if they got a D or an F. So in the case
1819
02:18:13,519 --> 02:18:15,519
they got a D and
1820
02:18:16,489 --> 02:18:20,469
I forgot to put a break statement here and I'm gonna put one down here, too
1821
02:18:20,469 --> 02:18:24,308
And finally, we're just gonna do one more for if they failed the test
1822
02:18:24,309 --> 02:18:26,309
So I'm just gonna say in the case that they got an F
1823
02:18:26,840 --> 02:18:28,840
We will print out
1824
02:18:29,000 --> 02:18:30,679
You failed
1825
02:18:30,679 --> 02:18:31,760
so
1826
02:18:31,760 --> 02:18:35,500
Essentially, what I'm doing here is I'm writing out all of these different cases
1827
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 you failed and the case they got a D
1828
02:18:40,120 --> 02:18:41,649
I'm gonna print out you did very bad
1829
02:18:41,649 --> 02:18:48,429
And then we also have this break statement here and this break statement basically tells our switch statement that we want to leave
1830
02:18:48,710 --> 02:18:53,200
So whenever we're executing this switch statement whenever C is executing the switch statement
1831
02:18:53,389 --> 02:18:57,159
It's checking to see if grade is equal to each one of these cases
1832
02:18:57,439 --> 02:19:01,778
So for example, let's say the grade is equal to a it's gonna execute this print F
1833
02:19:02,000 --> 02:19:07,660
But if I don't put this break statement in here, then it's just gonna keep going and keep checking these different cases
1834
02:19:08,029 --> 02:19:13,238
So it's always important to put a break statement in there because I basically just want to break out of this switch statement
1835
02:19:13,239 --> 02:19:18,339
I don't want to be in here anymore. So finally we can check one more thing in the case that the user
1836
02:19:19,179 --> 02:19:24,759
Or that we get an invalid grade. So for example, if someone entered in like a t or
1837
02:19:25,340 --> 02:19:29,830
G or something that's not a valid grade so I can use something called default and
1838
02:19:30,200 --> 02:19:33,759
Default is basically like an else so default is going to execute
1839
02:19:34,219 --> 02:19:38,649
whenever none of these conditions up here or Matt so then I can just put something like
1840
02:19:39,860 --> 02:19:45,489
Invalid grade or something and this will basically be like, hey, you got an invalid grade
1841
02:19:45,889 --> 02:19:51,099
So let's take a look at this switch statement and we'll just kind of talk a little bit more about what it's doing
1842
02:19:51,800 --> 02:19:59,679
Basically, I'm passing this in a variable or a value or something and I'm gonna check this value against
1843
02:19:59,680 --> 02:20:01,400
all of these different conditions
1844
02:20:01,400 --> 02:20:02,811
so in the case
1845
02:20:02,811 --> 02:20:04,610
the grade is equal to a
1846
02:20:04,610 --> 02:20:09,669
We're gonna print you did great and the case that B is equal to grade will print you did. All right, etc
1847
02:20:09,710 --> 02:20:16,450
and this is basically doing what an if statement doing so I can replicate this entire statement this entire switch statement as
1848
02:20:16,760 --> 02:20:20,770
An if statement I can write out if etc else--if else--if else--if
1849
02:20:21,290 --> 02:20:22,540
But this just makes it a lot easier
1850
02:20:22,540 --> 02:20:25,870
So I don't have to constantly write out all of that if statement stuff
1851
02:20:26,090 --> 02:20:31,719
So hopefully we did everything right and let's actually check and see how our program does
1852
02:20:32,570 --> 02:20:37,119
So grade is equal to a let's run our program and see what our message is
1853
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 instead of an A
1854
02:20:43,400 --> 02:20:45,520
Now it should tell us that we failed
1855
02:20:46,340 --> 02:20:49,990
So it says you failed if I made this a C
1856
02:20:50,690 --> 02:20:54,699
It'll tell us that we did poorly and it says you did poorly
1857
02:20:54,700 --> 02:21:00,759
So this is a super easy way for us to check one single value against a bunch of different conditions
1858
02:21:00,760 --> 02:21:05,860
And that's really the point of a switch statement is you're checking one single value like grade
1859
02:21:06,500 --> 02:21:10,480
Against a bunch of other values and that's where it can be extremely useful
1860
02:21:11,300 --> 02:21:13,300
So consider using the switch statements
1861
02:21:13,300 --> 02:21:20,350
They're not appropriate in every situation and there's I'd say like very select few situations where switch statements will come in handy
1862
02:21:20,630 --> 02:21:25,509
But when they are appropriate switch statements will save you a lot of time and a lot of hassle
1863
02:21:29,750 --> 02:21:30,500
In this tutorial
1864
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 structure where we can store
1865
02:21:38,430 --> 02:21:40,250
groups of data types
1866
02:21:40,250 --> 02:21:44,299
so inside of a struct I could store like an integer alongside of a
1867
02:21:44,640 --> 02:21:50,930
String alongside a character alongside a double I could sort all these different data types in one single data
1868
02:21:51,210 --> 02:21:57,500
Structure and there's tons of uses for structs and one of the things we can do with them is model real world entities
1869
02:21:57,930 --> 02:21:59,930
So I could basically like a model
1870
02:22:00,120 --> 02:22:04,729
Something in the real world inside of my program and I'm gonna show you guys how to do that in this tutorial
1871
02:22:05,189 --> 02:22:10,999
We're gonna be looking at how we can use a struct to represent a student inside of our program
1872
02:22:11,100 --> 02:22:18,229
So imagine we were writing a piece of software that was using like students. So it was like storing records of students or something
1873
02:22:18,229 --> 02:22:24,469
I could use a struct to represent a student in my program so over here in my program
1874
02:22:24,470 --> 02:22:29,929
I'm just gonna come up here above the main method and I'm actually gonna create a struct you guys will see like how these work
1875
02:22:29,930 --> 02:22:31,939
and how we can use them so I'm just gonna say
1876
02:22:33,359 --> 02:22:35,100
struct and
1877
02:22:35,100 --> 02:22:37,729
Over here, I'm gonna give this a name so I'm gonna call this
1878
02:22:38,370 --> 02:22:42,499
Student and a lot of times in C when we're making structs. You're gonna give them a
1879
02:22:43,020 --> 02:22:47,330
Capital letter just like that and I'm gonna make an open and closed curly bracket now
1880
02:22:47,880 --> 02:22:54,979
Inside of this struct I can start specifying what types of data are gonna make up a student in my program
1881
02:22:55,109 --> 02:23:00,439
So basically I can define like different attributes of a student and place them in here
1882
02:23:00,439 --> 02:23:03,439
And this will kind of act as a template and you'll see how we can use this later
1883
02:23:04,229 --> 02:23:07,039
So let's think about different attributes of a student
1884
02:23:07,040 --> 02:23:13,969
Well one thing would be like the student's name so I could say char name and I'm gonna make this a array of characters
1885
02:23:14,520 --> 02:23:20,420
so this is gonna represent the name and actually why don't we give this a number so I'm gonna say 50 so this
1886
02:23:21,210 --> 02:23:26,960
String can hold 50 characters maximum and now we're gonna make another one for major
1887
02:23:27,090 --> 02:23:30,920
So we're gonna want to store the students major and again, we'll make this 50
1888
02:23:30,920 --> 02:23:35,239
I also want to make an integer for the students age and
1889
02:23:36,060 --> 02:23:38,600
a double for the students GPA
1890
02:23:39,210 --> 02:23:42,049
So I have this struct student and in here
1891
02:23:42,050 --> 02:23:46,420
I have a bunch of after buttes of a student right have like the students name their major
1892
02:23:47,000 --> 02:23:50,680
An integer that stores their age and a double for their GPA
1893
02:23:51,290 --> 02:23:55,509
Essentially what I did was I created like a type of student data type
1894
02:23:55,550 --> 02:24:00,970
So I basically am allowing myself now to represent a student inside of my program
1895
02:24:00,971 --> 02:24:04,930
so let's come down here to this main method and I'll show you guys how we can use this so I can basically
1896
02:24:05,330 --> 02:24:10,780
Create an instance of this student structure so I can create like an actual student
1897
02:24:11,300 --> 02:24:14,200
Inside of my program and the way that I do that is just by saying
1898
02:24:14,780 --> 02:24:20,199
Struct and I want to type out the name of the struct that I want to make in our case
1899
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 just gonna call it student 1 and
1900
02:24:26,330 --> 02:24:28,749
I can just use a semicolon here
1901
02:24:29,150 --> 02:24:35,859
basically, what I did now was I created a container called student 1 that's gonna be able to store a
1902
02:24:36,471 --> 02:24:40,000
Name a major in age and a GPA
1903
02:24:40,521 --> 02:24:46,870
So if you're familiar with arrays in C, you'll know an array is a special structure that can hold multiple pieces of information
1904
02:24:47,210 --> 02:24:53,650
But all the pieces of information inside of an array needs to be of the same data type and also they don't have names
1905
02:24:53,840 --> 02:24:54,440
with a
1906
02:24:54,440 --> 02:25:01,390
Struct I can have a bunch of different data types like this and I can also give them names like name major age and GPA
1907
02:25:01,700 --> 02:25:04,930
So now let me show you guys how we can assign some values to these
1908
02:25:05,030 --> 02:25:09,280
So for this particular student student 1 I can give them a name
1909
02:25:09,280 --> 02:25:13,539
I can give them a major a GPA and an age so I could say
1910
02:25:14,601 --> 02:25:22,540
student 1 dot age and I can set this equal to something so I could say let's say student ones age is 22 so basically
1911
02:25:22,641 --> 02:25:29,410
Inside of this student 1 container I'm saying the age of this particular student is gonna be 22
1912
02:25:29,410 --> 02:25:35,829
I can do the same for the GPA so I can say student GPA is equal to maybe they have a 3.2
1913
02:25:36,080 --> 02:25:42,850
So I'm saying this particular student's GPA is a 3.2. I can also do the same thing for those strings
1914
02:25:42,851 --> 02:25:47,410
So for the name and the major now, here's the thing about working with strings
1915
02:25:47,721 --> 02:25:54,059
Remember in C a string is actually just an array of characters and when we have an array
1916
02:25:54,060 --> 02:25:56,159
We can't like give it a particular value
1917
02:25:56,159 --> 02:26:02,309
So for example, if I wanted to give this students name of value, this is just an array of characters
1918
02:26:02,619 --> 02:26:08,459
So I can't just come down here and say student name is equal to something
1919
02:26:08,560 --> 02:26:11,760
That's not gonna work because you can't do that with an array
1920
02:26:11,760 --> 02:26:17,760
I can use something called the string copy function and this is a function that will take a
1921
02:26:18,249 --> 02:26:24,419
String and it'll give it a value that we specify so it'll basically do what we want to do like what we did over here
1922
02:26:24,850 --> 02:26:28,829
with the age and the GPA but with the name so I could say like
1923
02:26:29,409 --> 02:26:34,049
Str. C py and inside of here. I need to pass this two parameters
1924
02:26:34,149 --> 02:26:38,309
The first thing I want to pass is the destination for the string. So we're gonna pass in
1925
02:26:38,949 --> 02:26:42,389
student 1 dot name the second thing I want to pass in is the
1926
02:26:43,119 --> 02:26:49,259
String that I want to store inside of student 1 name. So in our case, let's just name it him Jim
1927
02:26:50,439 --> 02:26:56,818
So now we have the student 1 names value is equal to Jim and I can do the same thing for major
1928
02:26:56,819 --> 02:27:03,569
so I'm just gonna copy this and we'll come down here and I could say student 1 major and we're gonna set this equal to
1929
02:27:04,539 --> 02:27:06,659
Business, so let's say Jim is a business major
1930
02:27:07,510 --> 02:27:13,859
Essentially what I've done here is I've created a student and that student had all the attributes that we defined up here
1931
02:27:13,859 --> 02:27:18,328
so this particular student had a name a major in age and a GPA and
1932
02:27:18,850 --> 02:27:20,939
I gave all of those values
1933
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
1934
02:27:26,739 --> 02:27:32,489
So now what I can actually do is I can print out all these different values that are stored inside of this structure
1935
02:27:32,489 --> 02:27:34,489
so if I wanted for example, I could like
1936
02:27:34,869 --> 02:27:37,229
Print out the GPA so I could print out
1937
02:27:38,289 --> 02:27:41,729
Student 1 GPA and now we're gonna be printing out
1938
02:27:42,489 --> 02:27:46,948
3.2. And actually it looks like I have a typo here. This should be student 1 not student
1939
02:27:48,640 --> 02:27:53,340
Alright so here we're printing out 3.2. So we're printing out the value of the student's GPA
1940
02:27:53,340 --> 02:28:00,449
I can also print out like their name. So why don't we do that student 1 name and now this is gonna print out
1941
02:28:01,979 --> 02:28:03,840
So you see we prints out over there
1942
02:28:03,840 --> 02:28:06,559
So a struct is a really useful structure
1943
02:28:06,560 --> 02:28:09,890
And another cool thing we can do with structs is we could actually create another
1944
02:28:10,109 --> 02:28:16,879
Student so I could create like another instance of that student structure. So I'm actually just gonna copy all this stuff
1945
02:28:16,880 --> 02:28:20,089
I'll show you how we can do this. I'll just come down here and paste it and
1946
02:28:20,729 --> 02:28:27,769
Instead of student 1 why don't we call this one student 2 and we can set this student's age to be like maybe 20
1947
02:28:27,989 --> 02:28:30,529
Maybe their GPA is a 2.5
1948
02:28:31,229 --> 02:28:34,398
Let's say their name is Pam and they're studying art
1949
02:28:35,640 --> 02:28:38,599
So now I have a completely different student
1950
02:28:38,600 --> 02:28:42,620
And if I wanted I could print out this students attributes so I can print out like student 2
1951
02:28:43,020 --> 02:28:48,439
Dot name and now we're gonna get Pam instead of Jim so you can see how that works right there
1952
02:28:48,899 --> 02:28:52,729
So I could create as many of these students as I want and this is what's cool about
1953
02:28:53,460 --> 02:28:59,750
Structs is I can just define the basic template for a student in my program and then I can create
1954
02:28:59,910 --> 02:29:05,089
Individual students down here that I can work with. So now I have this like student variable here
1955
02:29:05,090 --> 02:29:07,939
I could do whatever I want with it. I could pass it into a function
1956
02:29:07,939 --> 02:29:09,890
I could print it out onto the screen
1957
02:29:09,890 --> 02:29:14,510
I could use it in something like an if statement I could do basically anything I want with it
1958
02:29:14,510 --> 02:29:17,599
It's it acts a lot like a variable or an array
1959
02:29:17,600 --> 02:29:19,500
so remember variables and arrays or just
1960
02:29:19,500 --> 02:29:24,229
Containers we can do just about anything we want with them and that's the same with a struct
1961
02:29:24,229 --> 02:29:26,389
so this has just been kind of an introduction and
1962
02:29:26,520 --> 02:29:31,069
What you could do as an exercise is think of other things you might want to model in your program
1963
02:29:31,069 --> 02:29:36,019
So maybe something like a book or something like a phone you could model a phone using a struck
1964
02:29:36,630 --> 02:29:38,609
basically any type of
1965
02:29:38,609 --> 02:29:42,049
Information you could store using a struct just like this
1966
02:29:46,110 --> 02:29:49,980
In this tutorial I want to talk to you guys about while loops in C
1967
02:29:50,140 --> 02:29:56,760
now a while loop is basically a structure in the C programming language that we can use to loop over and
1968
02:29:57,250 --> 02:30:02,310
Continually execute a specific block of code until a certain condition is false
1969
02:30:02,590 --> 02:30:09,090
So I can specify a condition and then I can basically just loop over a bunch of code until that condition is no longer true
1970
02:30:09,340 --> 02:30:10,630
and
1971
02:30:10,630 --> 02:30:12,120
Loops can be really useful
1972
02:30:12,120 --> 02:30:14,789
there's a lot of times in our programs or we're gonna want to let
1973
02:30:14,980 --> 02:30:19,139
Continually do something while something else is true or while a certain condition
1974
02:30:19,450 --> 02:30:25,079
Is met and I'm gonna show you guys a really basic while loop in this tutorial and then in the next tutorial
1975
02:30:25,080 --> 02:30:29,429
We're actually gonna build a really cool guessing game using while loops so it should be pretty fun
1976
02:30:29,430 --> 02:30:32,070
And you should get a pretty good idea of like how these things work
1977
02:30:32,620 --> 02:30:34,271
The first thing I want to do
1978
02:30:34,271 --> 02:30:38,550
And this isn't directly related to creating a while loop though is I want to create an integer
1979
02:30:38,550 --> 02:30:42,599
So I'm just gonna create an integer called index and I'm gonna set it equal to 1 now
1980
02:30:43,240 --> 02:30:47,699
What I want to do is I want to actually create a while loop and you guys will see where this index comes into play
1981
02:30:47,860 --> 02:30:49,350
So in order to create a while loop
1982
02:30:49,350 --> 02:30:54,630
I can just type out while and open and close parentheses and then an open and close curly bracket
1983
02:30:54,630 --> 02:30:59,099
And this is basically like the outline sort of like the template for our while loop
1984
02:30:59,200 --> 02:31:02,369
Basically how this is gonna work is up here in these parentheses
1985
02:31:02,530 --> 02:31:07,499
I'm gonna specify a condition and as long as that condition is true
1986
02:31:07,630 --> 02:31:12,120
I'm gonna loop through all of the code inside of these curly brackets
1987
02:31:12,120 --> 02:31:17,969
So inside of these curly brackets, I'm gonna put a bunch of code. And as long as the condition in these parentheses is true
1988
02:31:17,970 --> 02:31:19,970
I'm gonna execute all that code so
1989
02:31:20,290 --> 02:31:26,790
Up here. We're going to specify a condition. I'm gonna specify a condition that index is less than or equal to
1990
02:31:27,760 --> 02:31:34,980
5 so while index is less than or equal to 5, I want to execute all the code inside of here
1991
02:31:35,080 --> 02:31:39,330
So I'm gonna put some code in here. The first thing I'm going to do is I'm just gonna print out the value of
1992
02:31:40,780 --> 02:31:42,150
index so over here
1993
02:31:42,150 --> 02:31:45,719
I'm just gonna say we're gonna print out an integer and
1994
02:31:46,061 --> 02:31:51,870
I'm just gonna print out index so we'll be printing out index and actually why don't we print out a new line every time to
1995
02:31:52,600 --> 02:31:54,600
There we go. And
1996
02:31:54,730 --> 02:32:02,060
Finally after this. I want to increment the index variable so I can just say in X is equal to index plus one
1997
02:32:02,611 --> 02:32:08,600
Basically, I'm adding one onto the index every time and actually I want to show you guys something in C
1998
02:32:08,730 --> 02:32:09,931
there's gonna be a lot of
1999
02:32:09,931 --> 02:32:15,801
Situations where you're gonna want to do something like this basically add one to an existing variable and in C
2000
02:32:15,801 --> 02:32:20,451
there's actually a shortcut we can use so I can come over here and I could just say index plus plus and
2001
02:32:20,551 --> 02:32:24,081
This is gonna do the same thing. So this is just gonna add one to the index
2002
02:32:24,661 --> 02:32:32,000
Essentially, what I'm doing is I'm saying I want to print out index and then I want to increment index and I'm gonna keep looping
2003
02:32:32,001 --> 02:32:36,081
Through this code while index is less than or equal to five
2004
02:32:36,451 --> 02:32:40,701
So let's go ahead and run this program and maybe you can predict what's going to happen
2005
02:32:41,551 --> 02:32:46,161
You'll see over here. I'm printing out the numbers one through five
2006
02:32:46,621 --> 02:32:48,651
So I'm printing out one two three
2007
02:32:48,651 --> 02:32:53,840
Four and five and let me walk you guys through exactly what's happening in this loop over here
2008
02:32:53,841 --> 02:32:56,810
we create this index variable and we set it equal to one and
2009
02:32:57,331 --> 02:33:01,790
Basically when C goes to execute this program the first thing it's gonna do before it
2010
02:33:01,791 --> 02:33:08,210
does anything else is it's gonna look at this condition and it's gonna check the condition and it's gonna say okay is index less than
2011
02:33:08,211 --> 02:33:14,331
Or equal to five. Well, the first time through index is equal to one. So it's definitely less than five C's gonna go through
2012
02:33:14,331 --> 02:33:16,850
It's gonna execute this line of code printing out one
2013
02:33:16,980 --> 02:33:20,510
Then it's gonna execute this line of code incrementing index
2014
02:33:20,701 --> 02:33:26,180
Then what C's gonna do is it's gonna come all the way back up here and it's gonna check this condition again
2015
02:33:26,341 --> 02:33:30,291
so it's gonna say okay is index less than or equal to five and
2016
02:33:30,691 --> 02:33:33,650
Index at this point is equal to two so it's less than five
2017
02:33:33,841 --> 02:33:39,621
so it's gonna go through execute this line of code execute this line of code now index is equal to three and
2018
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 condition again
2019
02:33:44,301 --> 02:33:47,241
So before C goes through this loop
2020
02:33:47,491 --> 02:33:54,260
It's always gonna it's always gonna check the condition first. So before it's allowed to go and execute that code again
2021
02:33:54,261 --> 02:33:56,600
It has to check the condition to make sure that it's true
2022
02:33:57,361 --> 02:34:02,240
Eventually, we're gonna get to a situation where index gets incremented and it gets incremented up to 6
2023
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 is less than or equal to 5 it's gonna be
2024
02:34:08,541 --> 02:34:13,820
False and we're gonna move with our lives in the program and that's the basics of while loops
2025
02:34:13,830 --> 02:34:18,799
So this seems very simple and that's because it is all we do is specify condition
2026
02:34:18,800 --> 02:34:20,330
As long as that condition is true
2027
02:34:20,330 --> 02:34:26,660
We keep doing all this stuff in here and there's tons of situations where while loops are gonna come in handy
2028
02:34:26,670 --> 02:34:30,140
This is just one of them where we can print out like numbers between 1 and 5
2029
02:34:30,141 --> 02:34:32,141
I also do want to point out one thing
2030
02:34:32,311 --> 02:34:37,460
That you might want to watch out for which is called an infinite loop and an infinite loop is a situation
2031
02:34:37,710 --> 02:34:41,419
Where this condition up here never becomes false
2032
02:34:41,940 --> 02:34:48,079
So this condition will just always stay true and there are certain circumstances where you're gonna want to use something like an infinite loop
2033
02:34:48,150 --> 02:34:52,699
But in a lot of circumstances, you're not gonna want to go anywhere near something like that
2034
02:34:52,700 --> 02:34:59,480
So let me just demonstrate so you guys can see so if I stopped incrementing this if I just got rid of index plus plus
2035
02:34:59,790 --> 02:35:04,370
Now when I run my program you'll see that it's just continually printing out ones
2036
02:35:04,370 --> 02:35:09,140
I mean look at all these ones that are getting printed out you can see like the text editor is kind of going crazy
2037
02:35:09,391 --> 02:35:14,120
This program is just it keeps executing through that loop and it's still going right now
2038
02:35:14,120 --> 02:35:20,360
So this would keep going forever if I let it keep going infinitely. Hence the name, so I'm just gonna terminate this
2039
02:35:20,360 --> 02:35:24,919
but that'll kind of show you like how an infinite loop works and you definitely want to watch out for that because
2040
02:35:25,080 --> 02:35:30,770
you'll get into situations as you work with while loops where they'll just become infinite and you you know you need to watch out for
2041
02:35:30,771 --> 02:35:37,610
That especially if you're doing something a little bit more advanced where you're like siphoning off memory or you're creating processes or something it can
2042
02:35:38,550 --> 02:35:42,800
Slow your computer down quite a bit. So this is a while loop and this is a really great loop
2043
02:35:42,800 --> 02:35:48,110
there's also one other type of loop that's similar to a while loop that I want to talk to you guys about and
2044
02:35:48,271 --> 02:35:51,740
Before I do that, I just want to do something really quick. So I'm gonna set this
2045
02:35:52,320 --> 02:35:53,880
index equal to 6
2046
02:35:53,880 --> 02:35:54,471
All right
2047
02:35:54,471 --> 02:36:00,530
I'm gonna set this equal to 6 and I'm gonna run my program and what you're gonna notice is nothing's gonna get printed out
2048
02:36:00,530 --> 02:36:02,659
so the code inside of this while loop
2049
02:36:02,910 --> 02:36:08,780
Isn't gonna print out because the first thing we always do when we were on this while loop is we check this condition
2050
02:36:09,061 --> 02:36:16,221
So the first thing C's gonna do before it prints anything before it increments anything it's gonna check this condition and if it's false
2051
02:36:16,221 --> 02:36:17,931
It's not gonna do any of this stuff
2052
02:36:17,931 --> 02:36:23,220
So when I run this program, you'll see nothing's gonna get printed out and that's exactly what happens
2053
02:36:23,470 --> 02:36:26,970
There's another type of a loop that's similar to a while loop
2054
02:36:27,040 --> 02:36:30,059
Which is called a do-while loop and I'm going to show you how to make one
2055
02:36:30,060 --> 02:36:36,000
All you have to do is take this wild thing up here and paste it down here below and then up here
2056
02:36:36,000 --> 02:36:37,960
I'm just gonna say do and
2057
02:36:37,960 --> 02:36:40,919
A do-while loop is similar to a while loop
2058
02:36:40,920 --> 02:36:47,549
But the first thing a do-while loop does instead of checking this condition down here. Is it executes all of this code?
2059
02:36:47,859 --> 02:36:50,189
So even though index is equal to 6
2060
02:36:50,319 --> 02:36:54,089
we're still gonna print out 6 and we're still going to increment it and
2061
02:36:54,460 --> 02:36:57,660
Then we're gonna check the condition to see if we can keep looping
2062
02:36:57,880 --> 02:37:03,359
So let me show you guys this and this should illustrate what this is doing. And actually I need to put a semicolon here
2063
02:37:04,330 --> 02:37:05,460
So you can see over here
2064
02:37:05,460 --> 02:37:12,269
We're printing out 6 so unlike before when we just use that while loop now because we're using a do-while loop
2065
02:37:12,270 --> 02:37:20,069
We're doing whatever was inside of that loop before we're checking the condition and there's gonna be certain circumstances where a do-while loop will
2066
02:37:20,350 --> 02:37:27,090
Come in handy. I would say by far while loops are way more common, but just you know, it's good to know about do-while
2067
02:37:27,090 --> 02:37:29,819
Loops because there are certain circumstances where they'll come in handy
2068
02:37:30,130 --> 02:37:35,460
Basically any time you don't want to be checking the condition first, you can use a do-while loop
2069
02:37:35,729 --> 02:37:40,349
But those are the two basic types and there's actually another type of loop that we'll talk about later in the course
2070
02:37:40,350 --> 02:37:42,450
Which is called a for loop, but for now
2071
02:37:42,450 --> 02:37:46,499
We're gonna have some fun with while loops and in the next tutorial, we're gonna be building a little guessing game
2072
02:37:50,739 --> 02:37:51,600
In this tutorial
2073
02:37:51,600 --> 02:37:55,049
I'm gonna teach you guys how to build a little guessing game and see
2074
02:37:55,180 --> 02:38:00,449
This is gonna be pretty cool because we're gonna use a lot of the stuff that we've learned up to this point in the course
2075
02:38:00,449 --> 02:38:03,838
In order to build it, so let's get started basically
2076
02:38:03,839 --> 02:38:07,949
This guessing game is going to allow the user to guess a secret number
2077
02:38:08,020 --> 02:38:11,819
so we're gonna define a secret number and we're gonna give the user the
2078
02:38:12,159 --> 02:38:17,878
opportunity to try and guess it so if they'll try to guess it if they get it right then they'll win the game and if
2079
02:38:17,879 --> 02:38:18,930
They don't get it right then
2080
02:38:18,930 --> 02:38:20,039
We'll let them guess again
2081
02:38:20,039 --> 02:38:25,439
Right, so we'll basically let them keep guessing what the secret number is until they get it so down here
2082
02:38:25,439 --> 02:38:27,538
We want to create a couple variables
2083
02:38:27,729 --> 02:38:31,708
The first variable I want to create is gonna be an integer and it's gonna be the secret number
2084
02:38:31,709 --> 02:38:37,468
So I'm just gonna call it secret number and why don't we just set it equal to five the next variable
2085
02:38:37,469 --> 02:38:40,589
We want to create is gonna store the user's guess
2086
02:38:40,589 --> 02:38:46,679
so this is also gonna be an integer and I'm just gonna call it guess and I'm not gonna give it a value yet because
2087
02:38:46,930 --> 02:38:50,129
Right off the bat. The user wouldn't have guessed a number
2088
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 enter in the number until they get it
2089
02:38:56,759 --> 02:39:00,509
Right. So as long as the user hasn't entered in the correct number
2090
02:39:00,579 --> 02:39:06,239
We want to keep asking them to enter it in in order to do that in order to like keep asking them to
2091
02:39:06,399 --> 02:39:09,658
Enter it until they've guessed it and we can use something called a while loop
2092
02:39:09,659 --> 02:39:15,628
So I'm just gonna type while and when making open and close parentheses and an open and closed curly brackets
2093
02:39:15,909 --> 02:39:20,338
Let's think about a good condition for our game, basically
2094
02:39:20,339 --> 02:39:25,919
We want to keep asking the user to enter an input as long as they haven't guessed the secret number
2095
02:39:26,169 --> 02:39:31,739
So it's right there we could keep looping as long as the guess is not equal to
2096
02:39:32,409 --> 02:39:34,439
The secret number right?
2097
02:39:34,439 --> 02:39:37,229
and basically this is always going to be true until
2098
02:39:37,659 --> 02:39:41,279
They guess the correct number now inside of here
2099
02:39:41,279 --> 02:39:46,168
we want to prompt them to enter their guests and then we want to take whatever they enter and
2100
02:39:46,390 --> 02:39:52,799
Store it inside of our guest variable. So let's do this first we'll say printf and I'm just gonna say
2101
02:39:53,680 --> 02:39:55,739
enter a number and
2102
02:39:56,859 --> 02:39:58,859
Then down here. I'm gonna use
2103
02:39:59,289 --> 02:40:04,019
Scanf in order to get whatever number they enter and I'm gonna store it
2104
02:40:04,660 --> 02:40:09,809
inside of that guest variable so I'm just gonna say at guess and this is gonna take whatever number
2105
02:40:09,939 --> 02:40:14,459
Or whatever integer they enter and it's gonna store it inside of our guest variable
2106
02:40:14,530 --> 02:40:18,269
So basically what's happening in this loop is every time through the loop
2107
02:40:18,270 --> 02:40:18,700
we're
2108
02:40:18,700 --> 02:40:24,209
getting a different number and then we're gonna come up here and we're gonna check to see if it's equal to the secret number if
2109
02:40:24,250 --> 02:40:27,929
It's not then we're gonna go through again and we're gonna keep doing this
2110
02:40:28,090 --> 02:40:34,019
But if the guest is equal to the secret number, then we'll break out of the loop and that means we'll be down here
2111
02:40:34,240 --> 02:40:38,609
So I'm just gonna print out a success message and I'll basically just say like hey you win
2112
02:40:40,300 --> 02:40:44,040
All right, awesome so you can see this isn't actually that many lines of code
2113
02:40:44,040 --> 02:40:49,889
I mean, it's probably like ten lines of code to write out this whole thing and it looks like everything's set up
2114
02:40:49,890 --> 02:40:54,240
So this should actually work properly. So let's try to run this and we'll see if we can guess the secret number
2115
02:40:54,240 --> 02:40:56,399
So remember a secret number is five
2116
02:40:57,790 --> 02:41:02,639
So I'm gonna run the program it says enter a number. So let's just kind of botch the guesses two
2117
02:41:03,370 --> 02:41:05,370
Seven eight nine
2118
02:41:05,680 --> 02:41:09,840
three so you'll notice I can keep entering in numbers and
2119
02:41:10,390 --> 02:41:14,640
It'll keep prompting me to enter a number as long as I haven't guessed correctly
2120
02:41:14,710 --> 02:41:16,320
So now let's type in five
2121
02:41:16,320 --> 02:41:23,219
And remember five is the secret number when I click enter now that loop condition is going to end up being false, right?
2122
02:41:23,220 --> 02:41:30,059
Because the guess is gonna be equal to the secret number and it's gonna execute and it's gonna say hey you win, right?
2123
02:41:30,060 --> 02:41:34,560
So we broke out of that loop we executed the print statement that said you win and the program
2124
02:41:34,930 --> 02:41:37,830
Terminated and that is a really cool little game
2125
02:41:37,830 --> 02:41:39,830
so one of the cool things about this game and
2126
02:41:39,850 --> 02:41:44,580
It's different from programs that we've written in this course in the past is it'll keep running
2127
02:41:45,370 --> 02:41:48,450
Essentially infinitely until we enter in that correct guessed
2128
02:41:48,450 --> 02:41:53,099
So it's gonna keep going and keep going keep asking us to do something and to enter in a value
2129
02:41:53,620 --> 02:41:57,059
Until we entered correctly. So this is pretty cool now
2130
02:41:57,060 --> 02:41:59,519
This is a pretty nice game and it's it's pretty simple
2131
02:41:59,520 --> 02:42:04,380
But I want to maybe make it a little more complex one of the things that sucks about our game right now
2132
02:42:04,390 --> 02:42:08,040
Is that the user gets unlimited guesses, right?
2133
02:42:08,040 --> 02:42:13,410
So basically like they can just guess every single number and eventually they're gonna get it, right, you know
2134
02:42:13,720 --> 02:42:16,800
So why don't we do this? We should impose a guess limit
2135
02:42:16,910 --> 02:42:22,969
So why don't we say like the user can only guess three times and if they can't get the secret number in three tries
2136
02:42:23,130 --> 02:42:28,309
Then they'll lose the game. So let's think about how we can go ahead and add that functionality
2137
02:42:28,890 --> 02:42:32,269
Into this little app, I'm gonna walk you guys through how we can do that
2138
02:42:32,270 --> 02:42:35,210
The first thing we're gonna need to do is we're gonna need to create some
2139
02:42:35,850 --> 02:42:41,149
variables I'm going to create another variable up here and I'm just gonna call it guess count and
2140
02:42:41,490 --> 02:42:44,629
Guess count is basically going to tell us how many times
2141
02:42:44,910 --> 02:42:50,419
the user has tried to guess the secret number and I'm just gonna set this equal to zero because
2142
02:42:50,850 --> 02:42:53,540
Initially, the user will have guessed zero times
2143
02:42:53,910 --> 02:42:58,669
so remember this guest count variable is basically gonna get incremented every time they guess
2144
02:42:59,040 --> 02:43:03,799
Right, so I'm gonna come down here. Every time we go through this loop. The user is guessing
2145
02:43:04,110 --> 02:43:08,599
So every time we go through this loop, I'm gonna increment the guest count. So I'm just gonna say
2146
02:43:09,240 --> 02:43:13,369
Guest count plus plus and that means every time the user guesses
2147
02:43:13,370 --> 02:43:19,399
The guest count will get incremented and we'll have a accurate count of how many times the user has guessed
2148
02:43:19,830 --> 02:43:20,450
All right
2149
02:43:20,450 --> 02:43:22,490
the second variable that I want to make is
2150
02:43:22,920 --> 02:43:29,689
Going to be another integer and I'm just gonna call it guess limit and this is gonna tell us how many times the user
2151
02:43:29,910 --> 02:43:34,399
Can guess so this will basically determine like how many guesses the user gets
2152
02:43:34,500 --> 02:43:36,140
So why don't we set this equal to three?
2153
02:43:36,140 --> 02:43:40,970
I think three guesses is a pretty good limit and finally we want to create one more
2154
02:43:41,729 --> 02:43:45,648
variable and this is also gonna be an integer and I'm just gonna call this out of
2155
02:43:46,590 --> 02:43:50,059
guesses and I'm gonna set out of guesses equal to
2156
02:43:50,490 --> 02:43:52,909
zero and out of guesses will basically
2157
02:43:53,100 --> 02:43:58,820
Tell us whether or not the user has any more guesses and this might not be super clear what this is doing right now
2158
02:43:59,550 --> 02:44:04,580
But keep following with the video and you'll see how this comes into play. This is actually gonna be really important
2159
02:44:04,580 --> 02:44:09,410
So we're actually gonna have to modify our code a little bit so down here we have this set up
2160
02:44:09,410 --> 02:44:13,999
So the user can keep guessing as long as the guess isn't equal to the secret number
2161
02:44:14,189 --> 02:44:18,379
But we want to be able to limit the amount of times that the user can guess and actually down here
2162
02:44:18,380 --> 02:44:22,399
I'm going to create an if statement and every time we go through this loop
2163
02:44:22,399 --> 02:44:28,009
I want to check and see if the user has any more guesses left. I want to in other words
2164
02:44:28,010 --> 02:44:32,939
I want to see if they've reached their guessed limit so I'm gonna make an if statement and I'm gonna check to see if
2165
02:44:33,100 --> 02:44:35,100
the guest count is
2166
02:44:35,470 --> 02:44:37,449
less than
2167
02:44:37,449 --> 02:44:39,449
the guests limit as
2168
02:44:39,520 --> 02:44:42,419
Long as the guest count is less than the guests limit
2169
02:44:42,419 --> 02:44:46,559
That means they haven't guessed as many times as the limit specifies
2170
02:44:46,959 --> 02:44:50,999
Basically, they can guess again, right so down here inside of this if statement
2171
02:44:50,999 --> 02:44:55,168
I'm gonna put all of this code and I'm just gonna surround this with curly brackets
2172
02:44:55,899 --> 02:45:02,729
so basically the user can only guess the number this code will only get executed when the
2173
02:45:03,010 --> 02:45:06,899
Guest count is less than the guessed limit when the user has
2174
02:45:07,239 --> 02:45:11,609
Additional guesses right when they haven't guessed as many times as the limit specifies
2175
02:45:12,520 --> 02:45:16,560
Otherwise though, so if they have guessed too many times
2176
02:45:16,770 --> 02:45:20,399
Right if the guest count is either equal to or greater than the guessed limit
2177
02:45:20,399 --> 02:45:25,738
I'm actually gonna set this out of guesses variable equal to one so I'm gonna say out of
2178
02:45:26,439 --> 02:45:28,799
guesses is equal to one and
2179
02:45:29,439 --> 02:45:33,778
Basically what this is going to tell us is if out of guesses is greater than zero
2180
02:45:33,909 --> 02:45:37,379
Then that'll tell us that the user is out of guesses. So
2181
02:45:37,899 --> 02:45:40,979
Essentially if out of guesses has a value that's more than zero
2182
02:45:40,979 --> 02:45:43,379
we'll know that they've run out of guesses and
2183
02:45:43,569 --> 02:45:49,229
If you ever use any other programming languages before this is what we would refer to as like a boolean variable
2184
02:45:49,300 --> 02:45:54,959
It's basically storing like a true or a false value, which we're representing with zero or one
2185
02:45:55,689 --> 02:46:00,059
So if the guest counts less than the guessed limit they can guess again
2186
02:46:00,220 --> 02:46:06,630
Otherwise we're gonna say out of guesses is equal to one. In other words. We're gonna say that they're out of guesses
2187
02:46:06,819 --> 02:46:10,859
There's one more thing we have to do though is we have to come up here and we have to change this
2188
02:46:10,989 --> 02:46:17,639
Condition because right now we're gonna keep looping through this code as long as the guest is not equal to the secret number
2189
02:46:18,310 --> 02:46:21,749
But there's actually two situations where we want to break out of the loop
2190
02:46:21,749 --> 02:46:24,479
The first is when they've guessed the number correctly
2191
02:46:24,640 --> 02:46:27,390
Right, then. We want to come down here and print out you win
2192
02:46:27,760 --> 02:46:31,739
But we also want to break out of the loop when they've run out of guesses
2193
02:46:31,930 --> 02:46:35,159
So I want to check to see whether or not they're out of guesses
2194
02:46:35,159 --> 02:46:37,529
so I'm gonna say guess is not equal to secret number and
2195
02:46:38,260 --> 02:46:40,260
out of
2196
02:46:40,630 --> 02:46:42,459
guesses is
2197
02:46:42,459 --> 02:46:43,961
equal to Z
2198
02:46:43,961 --> 02:46:46,980
So as long as out of guesses is equal to zero
2199
02:46:46,980 --> 02:46:51,090
That means that they're not out of guesses and they still have some guesses left
2200
02:46:51,190 --> 02:46:57,180
But if the guest counts less than the guessed limit and out of guesses gets set equal to one
2201
02:46:57,581 --> 02:47:02,640
Then out of guesses isn't going to be equal to zero anymore. So we're gonna break out of the loop
2202
02:47:02,641 --> 02:47:06,391
So basically, there's two scenarios now that will break us out of the loop either
2203
02:47:06,391 --> 02:47:08,761
They get the word right or they run out of guesses
2204
02:47:08,761 --> 02:47:13,950
So one more thing we have to do in this program is come down here and you'll see down here
2205
02:47:13,950 --> 02:47:18,180
We're always printing out you win. So no matter what like no matter what happens
2206
02:47:19,271 --> 02:47:25,171
We're printing out you win and that's because before if they made it down here if they made it out of that loop
2207
02:47:25,331 --> 02:47:27,810
That means that they guessed the secret number correctly
2208
02:47:28,030 --> 02:47:28,961
but
2209
02:47:28,961 --> 02:47:30,761
There's actually two scenarios
2210
02:47:30,761 --> 02:47:36,030
Down here where they would have broken out of the loop where there are they're out of guesses or they guess the secret word
2211
02:47:36,160 --> 02:47:40,200
So I want to actually check to see which one we're dealing with. So I'm going to check to see if
2212
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 it's equal to one
2213
02:47:47,471 --> 02:47:51,121
That means that they lost and they ran out of guesses. So I'm just gonna say
2214
02:47:51,971 --> 02:47:53,110
of
2215
02:47:53,110 --> 02:47:54,280
guesses
2216
02:47:54,280 --> 02:47:57,840
otherwise though, it means that they won so I'm gonna say
2217
02:47:58,421 --> 02:48:01,530
You win and this will basically be like hey you won the game
2218
02:48:02,110 --> 02:48:04,110
awesome
2219
02:48:04,450 --> 02:48:06,030
So this is a lot of code
2220
02:48:06,030 --> 02:48:09,930
I know that and let's just run this see how we did
2221
02:48:10,200 --> 02:48:14,909
And we'll see if our games working properly and then I'll kind of explain all the code to you guys
2222
02:48:15,131 --> 02:48:20,700
And actually you know what? I just noticed the typo down here. I said out of guesses double equals one
2223
02:48:20,700 --> 02:48:25,740
This is just have to be a single equals. So we're assigning this of value. So yeah, that was a little typo
2224
02:48:25,740 --> 02:48:29,640
I had two equals that needs to be one. So let's go over here
2225
02:48:29,641 --> 02:48:33,780
We'll run our program and it says enter a number. So why don't we try to lose the game?
2226
02:48:33,780 --> 02:48:39,570
I'm just gonna say two two and now we're on our third and final guess if I don't get it this time
2227
02:48:39,791 --> 02:48:41,101
That means we're gonna lose the game
2228
02:48:41,101 --> 02:48:44,730
So I'm gonna enter and you'll see it says out of guesses
2229
02:48:44,730 --> 02:48:49,560
So we weren't able to guess it within our three tries and the game told us that we lost
2230
02:48:49,721 --> 02:48:53,310
Let's run it one more time. We'll try to win so I'll say like two one now
2231
02:48:53,311 --> 02:48:55,051
We're on our third and final guess
2232
02:48:55,051 --> 02:48:56,190
So if I don't get it this time
2233
02:48:56,190 --> 02:49:01,318
we're going to I'm gonna guess of five and we guessed right so it's gonna tell us that we want so that's sort of
2234
02:49:01,659 --> 02:49:04,799
Basically how this little program works and I'm gonna walk you guys through one more time
2235
02:49:04,800 --> 02:49:07,590
so we have all these variables up here secret number is
2236
02:49:07,989 --> 02:49:12,989
Just storing the number they need to guess the guest keeps track of what they're guessing each time
2237
02:49:13,270 --> 02:49:16,319
The guest count is gonna tell us how many times they've guessed the number
2238
02:49:16,899 --> 02:49:20,519
guess limit is gonna tell us how many times they can guess and
2239
02:49:20,829 --> 02:49:26,429
Out of guesses is going to tell us whether or not the user is out of guesses. So down here
2240
02:49:26,430 --> 02:49:31,919
We're checking two conditions for this while loop. We're checking to see if the guess is not equal to the secret number and
2241
02:49:32,439 --> 02:49:37,139
We're gonna keep looping as long as out of guesses is equal to zero every time we go through this loop
2242
02:49:37,270 --> 02:49:40,229
We check to see if the guest count is less than the guessed limit
2243
02:49:40,229 --> 02:49:44,488
If it is then we ask them to guess we increment the guest count
2244
02:49:45,010 --> 02:49:50,849
Otherwise we say out of guesses is equal to one and that's gonna break us out of the loop down here
2245
02:49:50,850 --> 02:49:51,879
There's two situations
2246
02:49:51,879 --> 02:49:56,759
the first situation is that they ran out of guesses the second situation is that they won the game and
2247
02:49:56,859 --> 02:49:59,818
We're using this if statement to check both of those
2248
02:49:59,819 --> 02:50:06,778
so that's basically how this works and this kind of shows you how we can use things like while loops and if statements and also,
2249
02:50:07,119 --> 02:50:10,019
variables together in like one single program
2250
02:50:14,051 --> 02:50:18,160
In this tutorial I'm going to teach you guys how to use for loops in C
2251
02:50:18,230 --> 02:50:24,550
now a for loop is a special type of loop that we can use in C which allows us to use something called an
2252
02:50:24,801 --> 02:50:28,900
indexing variable and this indexing variable will basically tell us what
2253
02:50:29,301 --> 02:50:35,051
iteration of the loop we're currently on and we can use that indexing variable to do a bunch of stuff like we could
2254
02:50:35,240 --> 02:50:42,369
Loop through an array of items we could do all sorts of stuff inside of a loop. So down here in my program
2255
02:50:42,440 --> 02:50:44,529
You'll see that I have a while loop
2256
02:50:44,601 --> 02:50:49,330
Set up and I have just sort of a basic while loop and I want to show you guys what it's doing. So
2257
02:50:49,940 --> 02:50:53,949
Essentially up here I'm saying int I is equal to 1 so I'm giving this integer
2258
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
2259
02:50:59,421 --> 02:51:05,380
I'm gonna print out the value of I and then I'm gonna increment I so this is a very simple wire loop
2260
02:51:05,511 --> 02:51:10,180
basically, we have this variable I and every time we go through the loop, we're printing it out until it's
2261
02:51:10,700 --> 02:51:14,800
Greater than 5 so let's run this program and we'll see what it does
2262
02:51:15,530 --> 02:51:23,020
So you'll see over here. We're basically printing out values between 1 and 5 and I want to point out what's actually happening
2263
02:51:23,021 --> 02:51:26,801
so the first time that we go through this while loop we're printing out 1
2264
02:51:27,021 --> 02:51:33,431
The second time we're printing out to the third time. We're printing out 3 in other words this variable I is
2265
02:51:34,190 --> 02:51:37,720
Basically telling us what iteration of the loop are currently on
2266
02:51:38,061 --> 02:51:40,301
so on the first iteration of the loop I is
2267
02:51:40,521 --> 02:51:45,341
telling us that we're on the first iteration of the loop, right the first time we go through the loop I is equal to 1
2268
02:51:45,921 --> 02:51:47,171
Second time we go through the loop
2269
02:51:47,171 --> 02:51:49,930
I is equal to 2 third time I is equal to 3
2270
02:51:50,330 --> 02:51:55,960
So this variable I over here is basically telling us how many times we've gone through the loop
2271
02:51:55,961 --> 02:52:00,461
So on the third time, it's telling us 3 fourth time for etc, and believe it or not
2272
02:52:00,461 --> 02:52:05,080
This is actually a very useful thing for us to have when we're looping
2273
02:52:05,080 --> 02:52:11,080
It's there's a lot of situations where you're gonna want to know what iteration of the loop you're currently on
2274
02:52:11,511 --> 02:52:17,950
when you're looping so with a normal while loop like I can basically just specify a condition up here and I can do whatever I
2275
02:52:17,950 --> 02:52:19,360
Want right? It's very open
2276
02:52:19,360 --> 02:52:21,131
You can kind of just do whatever you want with it
2277
02:52:21,131 --> 02:52:27,939
but in a lot of situations and there's tons of these situations we're going to have a variable just like I
2278
02:52:28,310 --> 02:52:32,380
That will tell us what iteration of the loop that we're currently on or you know
2279
02:52:32,380 --> 02:52:37,719
Basically just a variable that's gonna keep changing every time we go through the loop and this is such a common
2280
02:52:38,450 --> 02:52:46,149
situation in see that there's actually something called a for loop and a for loop allows us to take all of this code and
2281
02:52:46,460 --> 02:52:53,229
Condense it into its own single loop. So it's taking this situation where we want to have a variable like I and
2282
02:52:53,840 --> 02:52:57,069
It's basically allowing us to do it a lot easier and a lot cleaner
2283
02:52:57,069 --> 02:52:59,379
So I'm gonna show you guys how we can create a for loop
2284
02:53:00,080 --> 02:53:04,389
I'm gonna come down here below this while loop and I'm gonna create a basic for loop structure
2285
02:53:04,390 --> 02:53:09,399
So I'm just gonna say for I'm gonna make an open and closed parenthesis and an open and closed curly bracket now
2286
02:53:10,069 --> 02:53:12,669
the difference between a for loop in a while loop is
2287
02:53:13,370 --> 02:53:20,229
Basically gonna happen inside of these parentheses. So in the parentheses of a while loop, we have our loop condition or our loop guard
2288
02:53:20,229 --> 02:53:25,959
Right. This is specifying whether or not we can keep looping through inside this for loop
2289
02:53:25,960 --> 02:53:29,769
We're gonna have three different things. So instead of just one loop guard
2290
02:53:29,770 --> 02:53:33,640
We're actually gonna have three different things that we want to put inside of here. So
2291
02:53:34,340 --> 02:53:37,210
with a for loop the first thing I'm gonna put in here is
2292
02:53:37,640 --> 02:53:40,569
This variable I so you'll notice in the while loop
2293
02:53:40,569 --> 02:53:43,209
we have our variable I up here and this is basically
2294
02:53:43,609 --> 02:53:48,309
Allowing us to loop through and keep track of how many times we've gone through the loop
2295
02:53:48,350 --> 02:53:53,470
So what I can do down here is I can do something similar I could say like int I and I'm not gonna give this
2296
02:53:53,470 --> 02:53:54,970
a value inside of here
2297
02:53:54,970 --> 02:54:01,479
I'm gonna say I is equal to 1 so basically I'm gonna take my variable I and I'm gonna give it an initial value
2298
02:54:01,700 --> 02:54:08,409
Of 1 and now I have my I variable I have my variable that's gonna change every time we go through the loop
2299
02:54:08,780 --> 02:54:13,389
The next thing I want to do is include my looping condition so up here
2300
02:54:13,390 --> 02:54:18,099
We have I as less than or equal to 5, that's our condition. I can put the same thing down here
2301
02:54:18,100 --> 02:54:20,470
So I'm gonna say I is less than or equal to 5
2302
02:54:21,050 --> 02:54:25,569
The third thing I want to do is increment I so you'll notice down here
2303
02:54:25,569 --> 02:54:33,189
Every time we go through this loop, we're incrementing that variable. I I'm gonna do the same thing over here in this little third quadrant
2304
02:54:33,830 --> 02:54:38,049
So you'll notice I have these little like sections. Here's the first section
2305
02:54:38,050 --> 02:54:41,859
We're initializing the very I were saying I is equal to one
2306
02:54:42,409 --> 02:54:45,368
Here we're specifying our looping conditions
2307
02:54:45,369 --> 02:54:48,969
so i'm saying we're gonna keep looping while I is less than or equal to five and
2308
02:54:49,189 --> 02:54:54,309
Over here is a little line of code. That's gonna get executed. Every time we go through the loop
2309
02:54:54,439 --> 02:55:00,129
So here I'm saying I plus plus I could also say like I minus minus and that would decrement
2310
02:55:00,130 --> 02:55:03,819
I I could say like I is equal to I plus two
2311
02:55:04,369 --> 02:55:09,639
Etc. Like I could do basically anything I wanted over here. Let's just keep it at I plus plus for now
2312
02:55:09,640 --> 02:55:17,170
so you'll see I was basically able to take all of this code and condense it into its own little type of loop and now
2313
02:55:17,170 --> 02:55:22,209
Instead of having to like print this out and create this variable up here. I can do all of that
2314
02:55:22,819 --> 02:55:27,248
Inside of this for loop now, I can basically take this line of code
2315
02:55:27,249 --> 02:55:31,449
I can paste it down into here and we essentially have the same thing
2316
02:55:31,449 --> 02:55:37,089
So right now this block of code and this block of code are 100% equivalent
2317
02:55:37,090 --> 02:55:39,130
They're doing exactly the same thing
2318
02:55:39,260 --> 02:55:44,649
So let's go ahead and get rid of all this code and let's test out our shiny new for loop
2319
02:55:44,810 --> 02:55:50,649
So I'm just gonna run my program and you'll see over here. We're getting exactly the same thing that we got before
2320
02:55:50,899 --> 02:55:55,568
We're printing out one two, three, four five. So it's the same exact program
2321
02:55:56,090 --> 02:56:01,359
doing the same exact thing and that's why for loops are great because we can take that little
2322
02:56:01,459 --> 02:56:05,739
Structure like where we have an indexing variable and we can use it with something like this
2323
02:56:05,989 --> 02:56:09,879
so I want to show you another situation where these four loops can come in handy and
2324
02:56:10,100 --> 02:56:14,979
We can use them to loop through all the elements inside of an array so actually up here
2325
02:56:14,979 --> 02:56:20,769
I have this array that I created. It's called lucky numbers and I'm just gonna grab this and bring it down here. So
2326
02:56:21,829 --> 02:56:26,109
We have this lucky numbers array. It has 4 8 15 16 23 42 and
2327
02:56:26,930 --> 02:56:31,659
What I could actually do is I could loop through all of the elements inside of this array
2328
02:56:32,300 --> 02:56:35,920
From this for loop so I'm gonna show you guys how we can do that now
2329
02:56:35,920 --> 02:56:37,720
Let's first off see how many elements we have
2330
02:56:37,720 --> 02:56:43,090
we have 1 2 3 4 5 6 so we have 6 elements in this an array so
2331
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 you'll see why we need to do this in a
2332
02:56:48,760 --> 02:56:51,969
Second but basically array indexes start at 0
2333
02:56:52,039 --> 02:56:55,088
So this first element in the array is at index position
2334
02:56:55,159 --> 02:57:00,969
zero and just for a little refresher if I wanted to access one of these elements from inside the array I could say like
2335
02:57:01,369 --> 02:57:02,959
lucky numbers
2336
02:57:02,959 --> 02:57:07,269
Was zero and this is going to give me access to this element
2337
02:57:07,270 --> 02:57:12,069
If I said lucky numbers two, then I'll get access to this element this 15
2338
02:57:12,260 --> 02:57:16,179
So this is basically how we can access an element inside the array
2339
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 while I is less than six and
2340
02:57:23,720 --> 02:57:26,649
Six was how how many elements we had in the array?
2341
02:57:26,649 --> 02:57:30,189
So I want to keep looping as long as we're less than six and I'm gonna say I plus plus
2342
02:57:30,709 --> 02:57:37,599
Now down here. I'm gonna do this same exact thing but instead of printing out I I'm gonna print out lucky
2343
02:57:38,149 --> 02:57:40,149
numbers I
2344
02:57:40,399 --> 02:57:45,939
So I'm gonna print out the array element at index position. I and lucky numbers
2345
02:57:46,220 --> 02:57:48,909
Basically the first time we go through this loop
2346
02:57:48,979 --> 02:57:51,338
We're gonna be printing out lucky numbers zero
2347
02:57:51,589 --> 02:57:55,209
Because I is going to be equal to zero the second time we go through the loop
2348
02:57:55,279 --> 02:58:00,069
We're gonna be printing out lucky numbers one because I is gonna be equal to one etc
2349
02:58:00,069 --> 02:58:01,909
And we're gonna keep doing that
2350
02:58:01,909 --> 02:58:06,878
Until we get all the way up to five which is gonna be the last element, which is this 42
2351
02:58:07,159 --> 02:58:10,719
So let's go ahead and do that. So I need to put this back to I and
2352
02:58:11,239 --> 02:58:13,689
Let's run this program. So you'll see over here
2353
02:58:14,239 --> 02:58:19,029
We're basically doing exactly what I said. So the first time through the loop were printing out four
2354
02:58:19,039 --> 02:58:24,009
We're printing out that first element the second time through the loop are printing out eight, which is the second element
2355
02:58:24,739 --> 02:58:31,958
15 16 23 and 40 - so we're printing out all of those elements in turn as we go through this for loop and
2356
02:58:32,419 --> 02:58:35,289
There's a lot of situations where for loops are gonna come in handy
2357
02:58:35,289 --> 02:58:37,628
but this is a very very very very
2358
02:58:37,970 --> 02:58:42,429
common situation where we want to loop through all the elements in an array and either print them out or
2359
02:58:42,680 --> 02:58:44,439
You know do something to them, whatever
2360
02:58:44,439 --> 02:58:47,979
so that's the basics of working with for loops and I do just want to say like
2361
02:58:48,169 --> 02:58:51,459
Anything that you do with a for loop you could do with a while loop, you know
2362
02:58:51,459 --> 02:58:56,979
I basically showed you guys how we transform that while loop into a for loop the thing with for loops though is that they're very
2363
02:58:57,589 --> 02:59:03,969
Convenient so it makes it really easy to do something like this without having to go through all the trouble of creating a while loop
2364
02:59:07,800 --> 02:59:15,539
In this tutorial I'm gonna talk to you guys about two topics in C the first topic we're to talk about our two-dimensional arrays a
2365
02:59:16,180 --> 02:59:23,489
Two-dimensional array is basically a situation where we have an array where all of the elements in the array are actually arrays themselves
2366
02:59:24,220 --> 02:59:27,659
The second thing I want to talk to you guys about is nested. Loops
2367
02:59:27,659 --> 02:59:31,859
So we're gonna look at how we can use a looping structure where we have a loop
2368
02:59:32,380 --> 02:59:34,079
Inside of loops. It's gonna be pretty cool
2369
02:59:34,079 --> 02:59:40,859
and actually these two topics can go together really well and I'm gonna show you guys how we can use nested loops and
2370
02:59:41,350 --> 02:59:45,329
Two-dimensional arrays in order to make an awesome program. So let's get started
2371
02:59:45,329 --> 02:59:47,079
the first thing I want to show you guys is
2372
02:59:47,079 --> 02:59:49,169
two-dimensional arrays so down here
2373
02:59:49,170 --> 02:59:55,289
we can create a 2d array and actually the concepts that I'm showing you in this video can apply to
2374
02:59:56,409 --> 03:00:02,099
Multi-dimensional arrays so not only two dimensions but three four or five really as many dimensions as your heart desires
2375
03:00:02,199 --> 03:00:05,969
So in order to create a two-dimensional array, I'm just gonna make an array of numbers
2376
03:00:05,979 --> 03:00:10,679
So I'm just gonna say int and we'll give this a name. So why don't we just say nums and
2377
03:00:12,279 --> 03:00:17,158
Normally when we create an array after we'd say the name of the array we'd make an open and close square bracket
2378
03:00:17,500 --> 03:00:19,350
But when we create a two-dimensional array
2379
03:00:19,350 --> 03:00:27,329
we're gonna make two open and closed square brackets just like that these two open and closed square brackets will basically represent like the
2380
03:00:27,699 --> 03:00:31,769
Width and the height of our array so you guys will see what I mean in a second
2381
03:00:31,770 --> 03:00:33,609
but basically we're gonna have like
2382
03:00:33,609 --> 03:00:38,729
Elements in the array and then each of those elements is gonna be an array and we'll have elements inside of it
2383
03:00:38,729 --> 03:00:44,788
So these two squares will allow us to like manipulate all that stuff. So what I'm gonna do is I'm gonna set this equal to
2384
03:00:45,579 --> 03:00:50,609
An open and closed curly bracket now, this is normally how we would create an array
2385
03:00:50,609 --> 03:00:55,139
So normally I would just say like 1 2 3 4 whatever I could put all my numbers in here
2386
03:00:55,390 --> 03:01:02,220
But with a two-dimensional array all of the elements are gonna be arrays. So instead of just saying like 1 2 3
2387
03:01:03,489 --> 03:01:05,489
We're actually gonna create
2388
03:01:05,680 --> 03:01:13,289
Arrays inside of here. So for example, I'll put an array right here and this text editor is so annoying with making these
2389
03:01:13,930 --> 03:01:19,979
so for example the first element in this array, I could just make an array so I'm gonna say this is like 1
2390
03:01:20,810 --> 03:01:22,810
So you can see here. I have two elements
2391
03:01:23,850 --> 03:01:30,830
Inside of this array which is itself the first element of the nums array. So let's make another one
2392
03:01:30,830 --> 03:01:35,719
We'll come down here and we'll make another one and this keeps trying to format differently
2393
03:01:36,510 --> 03:01:38,540
So now we're gonna say three four
2394
03:01:39,000 --> 03:01:44,899
So this thing right here this whole array, that's the first element in the nums array
2395
03:01:44,899 --> 03:01:49,849
Right and that array has two elements inside of it. Same thing with this one
2396
03:01:49,850 --> 03:01:55,069
This is the second element in the numbers array and it has two elements inside of it. So it's basically an array
2397
03:01:56,130 --> 03:01:57,990
within an array
2398
03:01:57,990 --> 03:01:59,990
let's make one more and
2399
03:02:00,540 --> 03:02:02,870
So this is going to be five six
2400
03:02:03,569 --> 03:02:07,669
So now we have three array elements right one
2401
03:02:08,340 --> 03:02:09,390
two
2402
03:02:09,390 --> 03:02:13,370
Three and each of them has two elements inside of them
2403
03:02:13,370 --> 03:02:16,609
So whenever we create a two dimensional array like this
2404
03:02:16,609 --> 03:02:22,369
We always have to specify the number of elements and then the number of elements inside each array
2405
03:02:22,370 --> 03:02:26,990
so in our case, we're going to have one two three elements in the array and
2406
03:02:27,540 --> 03:02:30,799
Each array has two elements inside of it
2407
03:02:30,800 --> 03:02:36,410
So it's gonna look like that so we would say three and then two and that's basically how we created
2408
03:02:36,410 --> 03:02:41,629
So now let's talk about accessing these elements. So I'm gonna create a little print statement here and
2409
03:02:42,180 --> 03:02:44,809
We're gonna print out some of this stuff. So I'm gonna
2410
03:02:45,510 --> 03:02:49,580
Say % D and I'm gonna show you guys how we can access individual elements
2411
03:02:49,580 --> 03:02:55,789
So basically I'm gonna say nums and let's say that I wanted to access this top-left element this one
2412
03:02:56,069 --> 03:02:59,509
the first thing I want to do is specify the index where
2413
03:03:00,540 --> 03:03:02,749
The value that I want to access is stored
2414
03:03:02,750 --> 03:03:08,089
So like this would be index position 0 this would be index position 1 this would be index position 2
2415
03:03:08,100 --> 03:03:10,100
so we're gonna say 0 and
2416
03:03:10,470 --> 03:03:13,220
then I want to specify the index position of the
2417
03:03:13,470 --> 03:03:20,750
individual element inside of 0 so I could say like this is element 0 this is element 1 so why don't we access element 0
2418
03:03:21,090 --> 03:03:25,040
So I print out nums 0 0 and this is gonna print out that
2419
03:03:25,560 --> 03:03:29,390
Numbers coming out. It's gonna print out that one for us. You can see over here we get that one
2420
03:03:29,939 --> 03:03:35,779
So let's try a different one. Let's try to grab this for right here. So this is going to be an index position
2421
03:03:36,960 --> 03:03:43,339
One and then one so this would be at 1 1 and now we should get that for let's see
2422
03:03:43,500 --> 03:03:45,500
Yeah, cool. So we get the 4
2423
03:03:45,750 --> 03:03:46,340
All right
2424
03:03:46,340 --> 03:03:51,469
so that's basically how we can access elements inside of these arrays and also just want to point out that
2425
03:03:51,750 --> 03:03:54,140
if I didn't want to give this an initial value
2426
03:03:54,141 --> 03:03:57,320
I could just like put a semicolon here and I could just like
2427
03:03:57,690 --> 03:04:03,140
Manually define each index location so I could say like 0 0 is equal to 7 or something
2428
03:04:03,750 --> 03:04:05,750
like I don't have to give it a
2429
03:04:06,570 --> 03:04:10,369
Value like right up front although in our case. Let's just do that because it's a lot easier
2430
03:04:11,010 --> 03:04:15,320
All right, so we have our numbers array and we figured out how we can print out the elements
2431
03:04:15,601 --> 03:04:17,990
So now I want to talk to you guys about another
2432
03:04:18,181 --> 03:04:22,850
Concept which is called a nested for loop and you guys will see in a second why I'm teaching this alongside
2433
03:04:23,460 --> 03:04:24,840
2d arrays
2434
03:04:24,840 --> 03:04:28,250
but a nested for loop is a situation where we have a for loop and
2435
03:04:28,920 --> 03:04:30,920
inside of that for loop we have
2436
03:04:31,080 --> 03:04:35,270
Another loop so I'm gonna show you guys this really quick. Let's say we create a for loop
2437
03:04:35,400 --> 03:04:39,469
I'm gonna create two variables over here in I and int J and
2438
03:04:39,931 --> 03:04:42,681
I don't know if I showed you guys this in the course yet
2439
03:04:42,681 --> 03:04:45,320
But if I want to just like declare two variables
2440
03:04:45,690 --> 03:04:52,699
I can just say I comma J and that will declare both the variables. I'm not giving them actual values yet
2441
03:04:52,700 --> 03:04:54,700
We're gonna do that inside these four loops
2442
03:04:54,840 --> 03:05:00,049
So I want to show you guys how we can use a nested for loop in order to print out all of the elements
2443
03:05:00,300 --> 03:05:03,920
inside of this two-dimensional array, so I'm gonna say for I
2444
03:05:04,530 --> 03:05:06,300
Is equal to zero?
2445
03:05:06,300 --> 03:05:09,650
and we're gonna keep looping as long as I is less than
2446
03:05:10,230 --> 03:05:16,999
Three and the reason I'm saying three here is because that's how many elements are inside of this nums array
2447
03:05:17,730 --> 03:05:22,549
so I'm gonna keep looping as long as I is less than three and then I'm gonna say I plus plus
2448
03:05:23,550 --> 03:05:27,140
Now inside of these curly brackets, I want to create another
2449
03:05:27,750 --> 03:05:35,360
loop, so every time we go through this one iteration of this top loop, we're gonna fully execute through another loop so I'm gonna say,
2450
03:05:35,970 --> 03:05:37,530
for
2451
03:05:37,530 --> 03:05:39,450
J is equal to 0
2452
03:05:39,450 --> 03:05:45,799
J is less than 2 and the reason I'm saying 2 here is because that's how many elements
2453
03:05:46,160 --> 03:05:48,160
Are inside each array
2454
03:05:48,271 --> 03:05:51,141
Inside of the nums array and you guys will see in a second. Why?
2455
03:05:51,990 --> 03:05:58,369
this is gonna work and then I'm gonna say J plus plus so now I'm gonna make some more open and close curly brackets and
2456
03:05:58,830 --> 03:06:04,879
Down here. I'm gonna actually be able to print out all of the elements inside of this array
2457
03:06:04,880 --> 03:06:06,880
So I'm gonna go ahead and do that
2458
03:06:07,470 --> 03:06:15,410
I'm just gonna say printf and we're gonna be printing out an integer because these are all integers and we're gonna be printing out nums
2459
03:06:15,870 --> 03:06:18,980
and I'm gonna print out nums I and
2460
03:06:19,830 --> 03:06:21,150
J
2461
03:06:21,150 --> 03:06:26,119
So whatever the value of I is and whatever the value of J is that's the index position
2462
03:06:26,220 --> 03:06:29,870
inside of nums that we're gonna print out and now just so this is
2463
03:06:30,420 --> 03:06:34,339
Formatted a little bit better. Why don't we put a comma?
2464
03:06:35,940 --> 03:06:40,909
Right there and then I'm also gonna put another printf over here that's just gonna print out a new line
2465
03:06:42,180 --> 03:06:48,530
And you'll notice that this printf is outside of this inner for loop so I'm gonna run this program
2466
03:06:48,540 --> 03:06:53,749
We're gonna see what it does. You guys will see exactly what's happening and then I'll sort of walk you through a little bit more
2467
03:06:54,480 --> 03:06:56,480
What is going on, so?
2468
03:06:57,180 --> 03:06:59,101
Let's run this program
2469
03:06:59,101 --> 03:07:02,240
And you'll see over here when we ran the program
2470
03:07:02,670 --> 03:07:09,440
We're basically getting this entire 2d array printed out. So I'm printed out 1 2 3 4 5 6
2471
03:07:09,630 --> 03:07:11,839
So let's talk about why that happens
2472
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 plus
2473
03:07:17,010 --> 03:07:21,320
So I'm going through this particular four loop three times
2474
03:07:21,330 --> 03:07:25,759
I'm gonna execute all the code inside of this four loop right here
2475
03:07:26,460 --> 03:07:32,660
Three times that's as many times as there are elements inside of our 2d array
2476
03:07:33,360 --> 03:07:39,140
Now every single time I go through this top loop every single time I go through it
2477
03:07:39,141 --> 03:07:45,770
I'm going to execute this loop in its entirety. So I will loop through this loop all the way through
2478
03:07:45,771 --> 03:07:51,410
I'll go through all of its iterations and this loop says J is equal to 0 J is less than 2 and
2479
03:07:51,990 --> 03:07:57,490
2 over here is how many elements are inside of each one of these raise?
2480
03:07:57,490 --> 03:08:02,350
So this element in the noms array has one two elements inside of it
2481
03:08:02,480 --> 03:08:06,220
This element in the numbers array has one two elements inside of it, right?
2482
03:08:06,530 --> 03:08:08,530
That's where this two is coming from
2483
03:08:08,780 --> 03:08:16,360
Then I'm incrementing J. And I'm gonna print out so I'm saying print F and I'm printing out nums at index position
2484
03:08:16,360 --> 03:08:18,909
I and index position J
2485
03:08:19,101 --> 03:08:24,520
so the first time we go through this for loop I is going to be equal to zero and
2486
03:08:24,650 --> 03:08:29,049
Remember that first time we're actually going to be going through this loop two times
2487
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 we're coming down here and printing a new line
2488
03:08:37,550 --> 03:08:44,560
the next time we go through this I loop we're gonna come down here and print nums 1 0 and nums 1 1
2489
03:08:45,110 --> 03:08:50,680
Finally the third and final time. We're gonna print out nums to 0 and nums to 1
2490
03:08:51,110 --> 03:08:53,409
That's basically how this is working. So
2491
03:08:53,931 --> 03:08:55,550
two-dimensional arrays and
2492
03:08:55,550 --> 03:08:59,320
Nested for loops are a match made in heaven and there's a lot of situations
2493
03:08:59,391 --> 03:09:03,641
Where you know besides just looping through 2d arrays that we're going to use nested loops
2494
03:09:03,710 --> 03:09:09,040
But hopefully this gives you a little bit of a introduction into what they are and why they're useful
2495
03:09:13,091 --> 03:09:13,900
In this tutorial
2496
03:09:13,900 --> 03:09:18,460
I'm gonna talk to you guys about accessing memory addresses in C
2497
03:09:18,891 --> 03:09:24,370
Now in the C programming language a lot of times we're gonna want to store different information
2498
03:09:24,370 --> 03:09:26,890
Right, and there's a bunch of different ways that we can store information
2499
03:09:26,900 --> 03:09:30,670
We can use things like variables we could use arrays we could use structs
2500
03:09:30,921 --> 03:09:36,160
but the basic point is that whenever we're using c we're gonna want to keep track of and
2501
03:09:36,471 --> 03:09:42,430
Maintain a bunch of different pieces of information and one of the easiest ways to do that is by creating
2502
03:09:43,070 --> 03:09:45,850
Variables so down here you'll notice that I have a bunch of variables
2503
03:09:45,851 --> 03:09:49,601
I have this integer called age and it's just has the value of 30
2504
03:09:49,601 --> 03:09:56,351
We have this double GPA 3.4 and we have this character grade and it has the value of the a character
2505
03:09:56,351 --> 03:09:57,740
I have a bunch of different
2506
03:09:57,740 --> 03:10:03,100
variables in my program and it allows me to keep track of data really easily and I can use these to
2507
03:10:03,530 --> 03:10:08,619
You know maintain and I can even modify these different informations variables are great
2508
03:10:08,620 --> 03:10:15,550
But I want to talk to you guys a little bit about how these work in the actual physical memory of our computer
2509
03:10:15,771 --> 03:10:19,990
So whenever I create a variable for example when I create a variable called age
2510
03:10:19,990 --> 03:10:26,260
And I give it a value of 30 this value 30 actually gets stored on our computer
2511
03:10:26,541 --> 03:10:28,541
so all computers have
2512
03:10:28,730 --> 03:10:32,170
Memory, so a lot of times you'll hear people refer to this as RAM
2513
03:10:32,240 --> 03:10:39,369
It's called random access memory and basically RAM is the memory that your computer is going to use when it's running programs
2514
03:10:39,620 --> 03:10:45,340
So for example, if I was to run this C program my computer would use Ram
2515
03:10:45,341 --> 03:10:49,751
It would use that memory in order to store and keep track of all this different information
2516
03:10:50,511 --> 03:10:55,930
Right. So when I create a variable like int age C is actually gonna store this value
2517
03:10:56,660 --> 03:10:59,320
30 at a specific memory
2518
03:10:59,900 --> 03:11:07,480
Location so it's gonna take that value 30 and it's gonna store it somewhere in RAM right when I create this double called GPA
2519
03:11:07,700 --> 03:11:15,040
C's gonna take this value 3.4 and it's gonna store it inside of the physical memory on our computer when I create this grade
2520
03:11:15,261 --> 03:11:19,751
C is gonna store this character on the physical memory in our computer
2521
03:11:19,751 --> 03:11:25,600
Now here's the thing when I create these variables I give them descriptive names, right? So I gave this a descriptive name
2522
03:11:26,479 --> 03:11:28,459
So I know what's stored inside of it, right?
2523
03:11:28,459 --> 03:11:32,989
And when I want to access this value, I can just refer to the name, right?
2524
03:11:32,989 --> 03:11:36,498
I can just refer to age I could modify and I could print it out
2525
03:11:36,499 --> 03:11:37,850
I could do something else to it
2526
03:11:37,850 --> 03:11:37,999
right
2527
03:11:37,999 --> 03:11:41,539
I can do whatever I want with it and the way that I can access this
2528
03:11:41,939 --> 03:11:45,979
variable in other words the way that I can access the value 30 is
2529
03:11:46,260 --> 03:11:51,499
Just by referring to the name of the variable same goes down here if I wanted to access the grade
2530
03:11:51,840 --> 03:11:56,569
Right if I wanted to access this capital a I could just refer to the name of the variable
2531
03:11:56,970 --> 03:11:58,430
But again, here's the thing
2532
03:11:58,430 --> 03:12:03,499
All of this information is stored in our physical memory somewhere
2533
03:12:03,499 --> 03:12:11,059
And so whenever C needs to access that information whenever our computer needs to access this value 30 or this value
2534
03:12:11,609 --> 03:12:16,939
3.4. It's actually going to refer to a specific memory address
2535
03:12:17,039 --> 03:12:22,609
Right. So when I want to access this variable age, I can just type out age and then I can use this variable
2536
03:12:22,859 --> 03:12:27,229
But when C wants to refer to this value, it's not actually using age
2537
03:12:27,229 --> 03:12:33,108
It's gonna use the memory address where this value is stored inside of our physical memory
2538
03:12:33,300 --> 03:12:39,199
So all of these values are stored inside of a physical address in our memory
2539
03:12:39,199 --> 03:12:45,618
So all of these values have an address where they're stored in memory and when C needs to access them it can use that address
2540
03:12:46,229 --> 03:12:51,948
So I'm gonna show you guys how we can access that address how we can print out the address on to the screen
2541
03:12:52,050 --> 03:12:56,569
And basically just wanted to give you guys an introduction into what memory addresses were. So
2542
03:12:57,149 --> 03:12:57,829
down here
2543
03:12:57,829 --> 03:13:03,889
I'm gonna say printf and I'm gonna show you guys how I can actually print out the physical memory address
2544
03:13:04,199 --> 03:13:07,729
Where each one of these values is stored on our computer?
2545
03:13:08,340 --> 03:13:14,479
So down here if I want to print out a memory address. I need to use a % and a P
2546
03:13:14,479 --> 03:13:14,939
so
2547
03:13:14,939 --> 03:13:19,998
normally if I was just gonna print out like a number I could say like % D or I can say % C for a
2548
03:13:20,010 --> 03:13:23,389
Character when we want to print out a physical memory address
2549
03:13:23,390 --> 03:13:30,169
We're gonna say % P and this actually stands for pointer. We're not gonna talk about pointers in this tutorial
2550
03:13:30,169 --> 03:13:31,669
We're just gonna talk about memory addresses
2551
03:13:31,669 --> 03:13:38,149
But just know that you need to say % P and now I'm going to type out the name of the variable whose memory
2552
03:13:38,300 --> 03:13:42,139
I want to access so I'm gonna access the memory address of age and
2553
03:13:42,660 --> 03:13:44,660
Before the variable. I'm just gonna say
2554
03:13:45,180 --> 03:13:46,290
ampersand
2555
03:13:46,290 --> 03:13:50,660
so I'm gonna make an ampersand age and I'm gonna say percent P and
2556
03:13:51,240 --> 03:13:57,409
Now we should be able to print out the memory address where the variable age is stored
2557
03:13:57,410 --> 03:14:03,799
In other words, we can print out the memory address where this value 30 is stored. So let's go ahead and run our program and
2558
03:14:04,439 --> 03:14:07,398
You'll see over here. We're getting this number
2559
03:14:07,399 --> 03:14:12,919
So I'm getting this number 0 0 6 0 FF 2 0 0 so this would be like a hexadecimal number
2560
03:14:12,920 --> 03:14:16,250
I guess it's not technically a number. It's like I think it's hexadecimal
2561
03:14:16,460 --> 03:14:22,939
But basically this is the physical memory address where the value of 30 is stored in other words
2562
03:14:22,939 --> 03:14:27,259
It's the place where C stored the value inside of this age variable
2563
03:14:27,260 --> 03:14:29,359
I could do the same thing for these other variables
2564
03:14:29,359 --> 03:14:35,359
so actually why don't I format this a little bit I'm gonna stay here age and then I'm gonna make a new line and
2565
03:14:36,000 --> 03:14:38,000
we're just gonna say GPA and
2566
03:14:38,490 --> 03:14:43,909
again, I'm gonna print out another one of these addresses and we'll do one for grade and
2567
03:14:44,819 --> 03:14:46,819
We'll print out the address
2568
03:14:46,890 --> 03:14:54,500
so now I can just come over here and I can sort of modify this a little bit so I can just say GPA and
2569
03:14:56,520 --> 03:15:01,640
Grade and you'll notice for each of these I'm using this ampersand before I type out the name of the variable
2570
03:15:01,979 --> 03:15:06,379
So now when we run our program we should get a nice little list of all of these
2571
03:15:07,050 --> 03:15:10,880
variables and their corresponding addresses so you'll see over here we
2572
03:15:11,430 --> 03:15:15,979
have age and it's stored up memory address 0 0 6 0 FF OC
2573
03:15:16,410 --> 03:15:22,009
GPA is stored at this memory address and grade is stored at this memory address
2574
03:15:22,140 --> 03:15:26,090
so if you were to like go into our computer or go into my computer and
2575
03:15:26,310 --> 03:15:34,040
Go to memory address is 0 0 6 0 FF 0 C that's actually going to store the value of
2576
03:15:34,260 --> 03:15:38,059
30 it's gonna store the value that's inside of that age variable
2577
03:15:38,060 --> 03:15:40,910
If you were to go to this memory address you would see the value
2578
03:15:40,979 --> 03:15:46,818
Inside of GPA if you're going to this memory address you would see the letter which was a capital A
2579
03:15:47,220 --> 03:15:50,429
so basically all of these variables are store at
2580
03:15:50,770 --> 03:15:55,650
Different memory addresses when I want to access the information in the variable
2581
03:15:55,650 --> 03:16:00,690
I can just refer to the variables name so I could say a age or I could say GPA
2582
03:16:01,000 --> 03:16:06,570
But when C wants to access the information and the variable it has to use these
2583
03:16:07,210 --> 03:16:11,669
Addresses so C would use this address C would use this address and this address?
2584
03:16:11,670 --> 03:16:17,700
I can just use the variable names and that's why C is awesome because it allows us to do stuff like that
2585
03:16:17,980 --> 03:16:24,749
But when C and when our computer wants to access these values it has to use these memory addresses
2586
03:16:24,750 --> 03:16:26,750
and so that's why these are useful and
2587
03:16:26,950 --> 03:16:28,440
When we're programming and C
2588
03:16:28,440 --> 03:16:31,589
it can actually be pretty useful to be able to know the
2589
03:16:31,960 --> 03:16:37,110
Addresses like the actual physical addresses of these certain variables in memory
2590
03:16:37,180 --> 03:16:39,870
So a lot of times in C when we want to work with these
2591
03:16:40,000 --> 03:16:44,160
Variables we can just refer to them by name and we can do all sorts of stuff with them
2592
03:16:44,160 --> 03:16:51,240
We can access them we can modify them. But in other circumstances, we're not just gonna want to be able to access the value
2593
03:16:51,240 --> 03:16:55,919
We're also gonna want to be able to access their physical memory address in other words
2594
03:16:55,920 --> 03:17:01,259
we're gonna want to be able to know like what that address is and we can use that for a bunch of stuff and
2595
03:17:01,360 --> 03:17:05,279
In future tutorials, I'm gonna talk about why that can be useful, but for now
2596
03:17:05,280 --> 03:17:09,419
I really just wanted to kind of give you guys an overview of what memory addresses are
2597
03:17:09,610 --> 03:17:12,839
How we're using them in our program and how we can print them out
2598
03:17:12,840 --> 03:17:13,200
so again
2599
03:17:13,200 --> 03:17:19,709
I just use this ampersand and then I type the name of the variable and I use this percent P to print it out
2600
03:17:23,400 --> 03:17:27,359
In this tutorial I want to talk to you guys about pointers in C
2601
03:17:27,880 --> 03:17:34,049
Now a pointer is basically just a type of data that we can use inside of our programs
2602
03:17:34,240 --> 03:17:39,089
So up until this point in the course, we've been using a bunch of different types of data, right?
2603
03:17:39,101 --> 03:17:46,650
we've been using things like integers and an integer is just a whole number we've been using things like doubles and a double is a
2604
03:17:46,960 --> 03:17:48,960
decimal number we've been using
2605
03:17:49,090 --> 03:17:55,830
Chars and a char is basically just a character and now I want to introduce you guys to another type of data
2606
03:17:55,830 --> 03:18:03,240
which is called a pointer and a pointer is basically just a memory address and a memory address it basically refers to a
2607
03:18:03,700 --> 03:18:07,950
you know a physical address inside of the memory of our computer where we're
2608
03:18:08,351 --> 03:18:11,310
Storing a value right and I think pointers
2609
03:18:11,561 --> 03:18:18,090
Tend to confuse a lot of people who learn about them and this is sort of one of those topics and see that everybody dreads
2610
03:18:18,370 --> 03:18:23,759
Because it's you know, traditionally very confusing to understand but actually pointers are
2611
03:18:24,340 --> 03:18:27,990
Extremely simple and the problem is that they just get over complicated
2612
03:18:28,090 --> 03:18:33,060
So what I want you guys to do is I just want you guys to think of a pointer as a type of data
2613
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 in our programs
2614
03:18:37,230 --> 03:18:40,259
It doesn't have to be any more complicated than that, right?
2615
03:18:40,320 --> 03:18:44,610
I can work with an integer in my program an integer is a whole number, right?
2616
03:18:44,610 --> 03:18:48,419
I can work with a double in my program a double is a decimal number
2617
03:18:48,420 --> 03:18:52,350
I can work with a char in my program a char is just a character
2618
03:18:52,351 --> 03:18:56,460
I can work with a pointer in my program a pointer is just a memory address
2619
03:18:56,980 --> 03:19:01,679
That's it. A pointer is just another type of data that I can use and I can work with
2620
03:19:02,200 --> 03:19:09,119
Inside of my programs and it just happens to be a memory address inside of the memory or the RAM in our computers
2621
03:19:09,120 --> 03:19:10,021
That's that's all it is
2622
03:19:10,021 --> 03:19:16,170
It's very simple people overcomplicate pointers and they don't have to be over complicated because it's just another type of data
2623
03:19:16,480 --> 03:19:22,469
Just like an integer or a double but instead of being like a whole number or a decimal number. It's a memory address
2624
03:19:22,570 --> 03:19:25,589
So hopefully that makes sense and I hope you think you guys don't
2625
03:19:25,811 --> 03:19:30,450
Try to over complicate this too much because it doesn't have to be so down here in my program
2626
03:19:30,450 --> 03:19:34,860
I have an integer that I've created and it's called age. So I just said in age and
2627
03:19:35,950 --> 03:19:39,420
Inside of this integer. I'm storing a whole number
2628
03:19:39,421 --> 03:19:40,921
I'm storing an integer right third
2629
03:19:40,921 --> 03:19:43,590
And if you've been following along with this course in the last tutorial
2630
03:19:43,591 --> 03:19:51,270
We talked about memory addresses and we talked about how all of these variables are actually storing these values at
2631
03:19:51,490 --> 03:19:55,020
Physical addresses on our computer's memory so down here
2632
03:19:55,021 --> 03:20:02,311
I have this print statement and I'm just printing out ages memory address and I'm using this percent P and I'm just printing out
2633
03:20:02,650 --> 03:20:08,999
Ampersand age and we talked about how this ampersand when I put this in front of the variable basically
2634
03:20:09,000 --> 03:20:14,790
It's going to give me the physical address in memory where this variable in other words where this
2635
03:20:15,341 --> 03:20:21,360
Thermia value is stored. So if I run this program you guys will see over here. It says ages memory address and it's this
2636
03:20:21,940 --> 03:20:25,649
Hexadecimal number right? This is the physical memory address of
2637
03:20:26,230 --> 03:20:27,820
eight the age variable
2638
03:20:27,820 --> 03:20:32,340
What did we just talk about before remember what I told you pointers are?
2639
03:20:33,190 --> 03:20:34,801
Memory addresses that's what they are
2640
03:20:34,801 --> 03:20:38,220
it's a type of data a pointer is a type of data in our program and
2641
03:20:38,530 --> 03:20:43,590
It's a memory address right when I use this ampersand and I type out the name of a variable
2642
03:20:43,591 --> 03:20:50,310
This is giving me the memory address right I'm able to print out the memory address so really this is a pointer
2643
03:20:51,040 --> 03:20:56,790
This is a type of data in our program. That is a memory address just like an integer is a type of data
2644
03:20:56,790 --> 03:21:02,460
That's a whole number and a double is a type of data. That's a decimal number and a char is a type of data
2645
03:21:02,461 --> 03:21:07,740
That's a character a pointer is a type of data. That's a memory address and this right here
2646
03:21:07,740 --> 03:21:13,560
This guy right here is a pointer. So it's just the memory address of the age
2647
03:21:14,351 --> 03:21:15,900
Variable that's it
2648
03:21:15,900 --> 03:21:19,260
and when I want to print out a pointer I can use this percent P and
2649
03:21:19,391 --> 03:21:22,350
It'll allow me to print it out onto the screen and everyone's happy
2650
03:21:22,381 --> 03:21:26,610
So essentially what I'm doing here is I'm printing out a pointer when I say percent P
2651
03:21:26,610 --> 03:21:32,909
I'm telling this print F function that I want to put on a pointer and then I give it a pointer over here I give
2652
03:21:32,910 --> 03:21:35,820
It a physical memory address. That's what a pointer is now
2653
03:21:35,820 --> 03:21:41,130
We understand and hopefully understand that a pointer is just a type of data, right? It's just a memory address and
2654
03:21:41,980 --> 03:21:48,180
We can access the memory addresses of specific variables by using this ampersand and then type
2655
03:21:48,439 --> 03:21:51,249
The name of the variable and that can be pretty useful
2656
03:21:51,590 --> 03:21:56,859
Now that we understand that I want to show you guys how we can work with these pointers so over here
2657
03:21:56,859 --> 03:22:04,659
I have an integer variable called age and this integer variable is storing an integer value 30
2658
03:22:05,300 --> 03:22:12,790
I'm gonna say that one more time. I have an integer variable and this integer variable is storing an integer value 30
2659
03:22:12,790 --> 03:22:17,649
I could also create a double variable so I could create a double called GPA and
2660
03:22:18,680 --> 03:22:25,809
Inside of this double variable. I could store a double value like 3.4. I could also create a char variable
2661
03:22:26,570 --> 03:22:31,449
Called grade and inside of this char variable. I can store a character a I
2662
03:22:32,210 --> 03:22:35,080
could also create a pointer variable and
2663
03:22:36,260 --> 03:22:39,849
Inside of that pointer variable I could store a pointer
2664
03:22:40,310 --> 03:22:41,149
Okay
2665
03:22:41,149 --> 03:22:48,819
So just like I stored a character inside of this character variable and I stored a double inside of this double variable
2666
03:22:48,820 --> 03:22:53,559
I could store a pointer inside of a pointer variable so we can actually store these pointers
2667
03:22:54,080 --> 03:22:56,080
inside of a pointer variable
2668
03:22:56,450 --> 03:22:59,499
Now here's where this is going to get a little bit tricky
2669
03:23:00,320 --> 03:23:02,320
When we create a pointer variable
2670
03:23:02,930 --> 03:23:07,450
we actually need a physical memory address right so when I create this
2671
03:23:08,030 --> 03:23:11,919
Integer variable like I can just come up with a number right off the top of my head, right?
2672
03:23:11,920 --> 03:23:14,620
I can just say like 30 when I create a
2673
03:23:15,260 --> 03:23:18,370
Double variable I can just store whatever number I want to store
2674
03:23:18,500 --> 03:23:21,009
but when we create a pointer variable remember
2675
03:23:21,010 --> 03:23:24,639
We're gonna be storing a memory address and I don't necessarily
2676
03:23:24,800 --> 03:23:30,609
Know any memory addresses like right off the top of my head at least. I don't know any meaningful memory addresses
2677
03:23:30,609 --> 03:23:37,239
So when I create a pointer variable, what I want to do is store the memory address of a variable
2678
03:23:38,030 --> 03:23:43,959
That's already in a program. So I'm gonna say that one more time when I'm creating a pointer variable
2679
03:23:45,020 --> 03:23:46,960
inside of that pointer variable
2680
03:23:46,960 --> 03:23:52,660
I'm gonna store the memory address of a another variable inside of our program
2681
03:23:52,670 --> 03:23:56,830
so let's say that I wanted to create a pointer that would store the memory address of
2682
03:23:57,740 --> 03:24:01,960
This variable over here so thats or the memory adjusts of age, right? I
2683
03:24:02,961 --> 03:24:06,130
can create a pointer just like this so I could say int and
2684
03:24:06,650 --> 03:24:09,940
asterisks and now I'm gonna give this a name so I'm gonna give this
2685
03:24:10,131 --> 03:24:17,171
Pointer variable a name and remember this pointer variable is gonna store the memory address of the age
2686
03:24:17,360 --> 03:24:22,330
variable and generally when you're naming a pointer, you're gonna start with a lowercase P and
2687
03:24:22,851 --> 03:24:30,370
Then you're gonna type the name of the variable whose memory address you're storing in the pointer variable
2688
03:24:30,591 --> 03:24:37,331
So I'm gonna say P age right because this pointer variable is storing the physical memory address of the age variable
2689
03:24:38,030 --> 03:24:42,580
Now what I want to do is set this equal to the memory address of the age variable
2690
03:24:42,801 --> 03:24:46,870
Which we can access using this ampersand so I could say ampersand age
2691
03:24:47,421 --> 03:24:50,140
so now this pointer variable is
2692
03:24:50,811 --> 03:24:53,591
Storing the memory address of the age variable
2693
03:24:54,141 --> 03:24:58,120
I'm gonna do the same thing down here for this GPA
2694
03:24:58,341 --> 03:25:05,770
So if I wanted to create a pointer variable that would store the memory address of the GPA variable. I could say double
2695
03:25:06,351 --> 03:25:13,931
asterisks and I'm just gonna say P GPA just like that and I'm gonna set this equal to the memory address of
2696
03:25:14,240 --> 03:25:20,770
This double variable which we can access using this ampersand and then the name of the variable like that
2697
03:25:20,811 --> 03:25:24,610
I'm gonna do the same thing down here for this character variable
2698
03:25:24,610 --> 03:25:28,840
So I want to create a pointer variable which is going to store the memory address of
2699
03:25:29,421 --> 03:25:32,860
The character variable so I'm just gonna say char
2700
03:25:34,341 --> 03:25:36,110
Asterix a
2701
03:25:36,110 --> 03:25:38,140
grade, and I'm gonna set this equal to
2702
03:25:38,870 --> 03:25:42,520
Ampersand grade. Okay, so that's all I'm doing, right?
2703
03:25:42,521 --> 03:25:47,200
So now I have this integer variable and I have this pointer variable
2704
03:25:47,631 --> 03:25:53,650
Which is storing the memory location or the memory address of the age variable
2705
03:25:53,650 --> 03:26:00,550
I have this double variable which is storing a double and I have this pointer variable which is storing a pointer and
2706
03:26:00,921 --> 03:26:05,800
The pointer just happens to be the memory address of the GPA variable
2707
03:26:05,801 --> 03:26:08,650
I have this char variable down here
2708
03:26:08,650 --> 03:26:14,470
And then I have this pointer variable, which is storing a pointer which just happens to be
2709
03:26:15,050 --> 03:26:17,080
the physical memory address of
2710
03:26:17,720 --> 03:26:20,349
the great variable in our memory
2711
03:26:20,560 --> 03:26:27,670
Okay, and that's basically all you need to know about pointers to get started a pointer is just a type of data
2712
03:26:27,830 --> 03:26:31,299
It's just a type of information that we can work with in our programs
2713
03:26:31,390 --> 03:26:33,550
it just happens to be a memory addresses write an
2714
03:26:33,950 --> 03:26:39,219
integer is a type of data that we can work with in our programs and it just happens to be a whole number a
2715
03:26:39,620 --> 03:26:44,439
double is a type of data that we can work with in our programs and it's a decimal number a
2716
03:26:44,930 --> 03:26:49,269
char is a type of data that we can work with in our programs and it's a character a
2717
03:26:49,790 --> 03:26:54,129
Pointer is a type of data that we can work with in our programs and it's a memory address
2718
03:26:54,260 --> 03:26:59,920
Hopefully that makes sense and just like we create integer variables and double variables we can also create
2719
03:27:00,560 --> 03:27:04,510
Pointer variables the only difference is whenever we create a pointer variable
2720
03:27:04,970 --> 03:27:09,400
We're gonna store the memory address of another variable in our program
2721
03:27:09,400 --> 03:27:14,889
So so you'll see over here when I create this pointer variable. I'm using the data type of
2722
03:27:15,620 --> 03:27:22,450
The variable whose address I'm storing so over here when I'm storing the address of an integer variable I'm saying int here
2723
03:27:22,670 --> 03:27:28,540
When I'm storing the address of a double variable I'm saying double here when I'm storing the address of a char variable
2724
03:27:28,540 --> 03:27:33,459
I'm saying char here and that's the basics of using pointers and
2725
03:27:34,520 --> 03:27:40,390
Also creating pointer variables and storing memory addresses inside of variables
2726
03:27:44,270 --> 03:27:46,270
In this tutorial I want to talk to you guys about
2727
03:27:46,890 --> 03:27:48,890
dereferencing pointers in C
2728
03:27:49,109 --> 03:27:55,729
Now a pointer is basically just a type of data that we can work with in our programs. That is a memory address
2729
03:27:55,770 --> 03:27:59,989
So sometimes in our programs we're gonna want to work with memory addresses
2730
03:27:59,989 --> 03:28:05,748
We're gonna want to use them in our programs and do certain things with them and those memory addresses are basically called
2731
03:28:06,060 --> 03:28:10,310
pointers in our programs and when you dereference a pointer
2732
03:28:10,919 --> 03:28:15,258
Essentially what you're doing is you're going to the memory address of that
2733
03:28:15,899 --> 03:28:20,328
Pointer and you're grabbing the piece of information from there. All right
2734
03:28:20,329 --> 03:28:23,809
So all points are as our memory addresses and when we dereference a pointer
2735
03:28:24,270 --> 03:28:30,770
We go to that physical memory address and we get the information or we get the value that's stored inside of there
2736
03:28:30,779 --> 03:28:34,458
so I'm gonna show you guys how we can do that and see it's actually really easy and
2737
03:28:35,069 --> 03:28:37,069
can be pretty useful so
2738
03:28:37,169 --> 03:28:39,169
Down here. I have this integer and
2739
03:28:40,229 --> 03:28:47,269
It's an integer variable and it's storing the integer value 30 and over here. I have a pointer variable which is storing the
2740
03:28:47,850 --> 03:28:54,199
memory address of 30 so it's storing the memory address of our age variable as you can see right here and
2741
03:28:55,049 --> 03:28:58,069
Down here. I'm just printing out this pointer
2742
03:28:58,069 --> 03:29:02,028
So I'm printing out pH I'm using this percent P and I'm just printing it out to the screen
2743
03:29:02,399 --> 03:29:05,809
So why don't we take a look and see what we get. So you'll see over here
2744
03:29:05,909 --> 03:29:11,659
We just get this memory address probably exactly what you expected to get so over here
2745
03:29:11,659 --> 03:29:14,809
I want to show you guys how we can dereference a pointer
2746
03:29:15,329 --> 03:29:21,588
So this pointer variable P age is storing the memory address of this age variable, right?
2747
03:29:21,600 --> 03:29:22,819
So when we printed it out
2748
03:29:22,819 --> 03:29:29,028
We basically were given the physical memory address where this numbered 30 was stored
2749
03:29:29,220 --> 03:29:36,919
So P age is storing the memory address of 30. It's storing the memory address where this value is
2750
03:29:37,649 --> 03:29:42,078
Inside of our computer's memory if I want to dereference a pointer
2751
03:29:43,049 --> 03:29:50,089
Basically, what I'm gonna be doing is I'm going to be going to that actual physical memory address and grabbing that piece of information
2752
03:29:50,310 --> 03:29:54,560
So the way that I can dereference a pointer is by using the asterisks
2753
03:29:54,560 --> 03:29:59,300
So when I'm printing this point, I can dereference it by using this asterisk
2754
03:29:59,459 --> 03:30:03,168
But now over here since I'm dereferencing this pointer
2755
03:30:03,169 --> 03:30:09,709
I need to print out a number instead of a pointer because when I dereference the pointer
2756
03:30:09,989 --> 03:30:14,839
This is no longer a pointer. This is no longer a memory address now
2757
03:30:15,029 --> 03:30:22,488
This is gonna be whatever was stored at the memory address. The pointer was pointing to so this is going to be an integer because
2758
03:30:23,279 --> 03:30:25,229
this pointer
2759
03:30:25,229 --> 03:30:28,099
Variable was storing the address of an integer
2760
03:30:28,680 --> 03:30:31,010
Right, so when I dereference it
2761
03:30:31,590 --> 03:30:33,079
This is actually gonna be an integer
2762
03:30:33,079 --> 03:30:37,099
So let me show you guys I'm gonna print out or I'm gonna run our program and it should print out
2763
03:30:37,709 --> 03:30:38,909
30
2764
03:30:38,909 --> 03:30:43,399
So you'll see that we get a 30. So basically what's happening here is we're
2765
03:30:44,010 --> 03:30:45,689
dereferencing the pH
2766
03:30:45,689 --> 03:30:52,519
Pointer variable we're dereferencing the pH pointer which basically means we're going and grabbing the actual, you know
2767
03:30:53,010 --> 03:30:59,209
Physical value that's stored at the memory address that the pointer was pointing to and that's why we get 30
2768
03:30:59,550 --> 03:31:03,050
And that's basically what dereferencing is, you know, you're you have a pointer
2769
03:31:03,050 --> 03:31:06,289
You can dereference it and you basically just get the value that it was pointing to
2770
03:31:07,020 --> 03:31:09,919
It's essentially all we're doing so I want to show you guys how we could do this
2771
03:31:10,590 --> 03:31:13,039
Just normally so for example
2772
03:31:13,039 --> 03:31:15,378
I have this age variable if I just typed this out
2773
03:31:15,779 --> 03:31:20,179
If I wanted to get the address of this age variable I could use this ampersand, right?
2774
03:31:20,180 --> 03:31:22,169
That's basically what we did up here
2775
03:31:22,169 --> 03:31:27,619
but what I could do is I could just dereference this so I can say ampersand age and then I can put a
2776
03:31:27,840 --> 03:31:29,840
asterisks here and this is actually gonna
2777
03:31:30,029 --> 03:31:32,029
dereference that entire thing
2778
03:31:32,249 --> 03:31:38,539
So now when I run my program it's gonna print out 30, so it's not gonna print out the memory address
2779
03:31:38,539 --> 03:31:42,378
It's just gonna print out 30 so you can see over here. We get 30
2780
03:31:42,899 --> 03:31:48,349
so I could actually do this like as many times as I want and this will maybe just illustrate what's happening so I could put
2781
03:31:48,350 --> 03:31:53,059
An ampersand here and now I'm gonna get that memory address again
2782
03:31:55,050 --> 03:32:02,749
So you'll see we're getting this memory address and I can do the same thing so I could dereference this one more time and
2783
03:32:05,609 --> 03:32:08,778
You'll see now we're getting that 30 value again
2784
03:32:08,779 --> 03:32:09,050
so
2785
03:32:09,050 --> 03:32:16,140
I mean basically what I'm doing I'm constantly like like here I'm getting the memory address where the age variables stored
2786
03:32:16,421 --> 03:32:23,070
Then I'm dereferencing and getting the value then. I'm getting the memory address again, and then I'm dereferencing it again
2787
03:32:23,070 --> 03:32:26,400
So, you know, hopefully that makes sense when you dereference a pointer
2788
03:32:26,860 --> 03:32:33,150
you're basically just getting the value that that pointer is pointing to or you're getting the value that's stored in the
2789
03:32:33,341 --> 03:32:35,520
memory address of the pointer
2790
03:32:39,811 --> 03:32:44,280
In this tutorial I want to teach you guys how to write two files in C
2791
03:32:44,471 --> 03:32:50,761
One of the cool things we can do in C is we can actually modify and change and also create new files
2792
03:32:51,250 --> 03:32:54,089
So in this tutorial, I'll just basically show you guys how to do that
2793
03:32:54,090 --> 03:32:58,830
We'll talk about creating files writing files and appending on to files
2794
03:32:58,830 --> 03:33:02,910
So let's go ahead and do that down here in my main function
2795
03:33:03,910 --> 03:33:08,490
We're actually gonna create a file. So I'm going to create a file and I'm gonna store some information
2796
03:33:09,221 --> 03:33:10,870
inside of that file and
2797
03:33:10,870 --> 03:33:15,900
the way that we can do that is we can just type out file all in caps just like that and
2798
03:33:16,601 --> 03:33:22,620
essentially what we're doing here is we're creating a pointer to a physical file on our
2799
03:33:23,261 --> 03:33:24,641
machine, so
2800
03:33:24,641 --> 03:33:28,530
I'm just gonna say file and I'm gonna make an Asterix and I'm just gonna give this a name
2801
03:33:28,530 --> 03:33:30,221
So I'm just gonna call this F
2802
03:33:30,221 --> 03:33:35,640
Pointer and that'll just stand for file pointer, and I want to set this equal to F
2803
03:33:36,250 --> 03:33:41,310
Open an F open is actually a function that's gonna open a file for us
2804
03:33:41,830 --> 03:33:45,930
And so inside of these parentheses, I'm gonna give this a couple of different parameters
2805
03:33:46,120 --> 03:33:50,789
The first parameter is gonna be the name of the file that I want to open. So I'm just gonna call this
2806
03:33:52,150 --> 03:33:55,919
employees dot text and the second parameter is gonna be a
2807
03:33:56,200 --> 03:34:00,959
File mode and a file mode is basically just going to tell C
2808
03:34:01,240 --> 03:34:03,629
What we want to do with the file that we're gonna open
2809
03:34:03,790 --> 03:34:10,200
And there's a bunch of different file modes. The three most basic types are R and R stands for read
2810
03:34:10,300 --> 03:34:13,290
So if I put R in there, I mean we want to read the file
2811
03:34:13,870 --> 03:34:16,709
W stands for right so if I put right in there
2812
03:34:16,710 --> 03:34:21,900
it means I want to either create a new file or write over an existing file and
2813
03:34:22,870 --> 03:34:28,410
There's also a which stands for append and that means I want to append information on the file in this tutorial
2814
03:34:28,410 --> 03:34:31,890
we're gonna look at W and a so what I want to do over here is
2815
03:34:32,440 --> 03:34:34,391
put in W and
2816
03:34:34,391 --> 03:34:37,950
Right now employees dot text. This file doesn't actually exist
2817
03:34:38,500 --> 03:34:41,700
So what I'm gonna do is I'm actually going to create that file
2818
03:34:41,700 --> 03:34:48,240
We're gonna put some information inside that file and then that file gets saved onto my machine and in order to do that
2819
03:34:48,240 --> 03:34:50,011
We're gonna need this write mode
2820
03:34:50,011 --> 03:34:51,820
So it's just going to be a W
2821
03:34:51,820 --> 03:34:57,989
Anytime that we're open file in see you always want to make sure that you close the file so over here I'm saying F open
2822
03:34:58,180 --> 03:34:59,250
And down here
2823
03:34:59,250 --> 03:35:02,070
I want to use another function called F close and
2824
03:35:02,530 --> 03:35:05,219
Basically what this is going to do is it's going to close off the file
2825
03:35:05,220 --> 03:35:08,760
so it's gonna remove the file from the memory on our computer and
2826
03:35:09,130 --> 03:35:12,719
if we do make any changes it'll kind of like save the file and
2827
03:35:12,910 --> 03:35:17,490
Everything will just be kind of like locked in so you always want to make sure whenever you're working with a file that you close
2828
03:35:17,490 --> 03:35:22,019
It and inside of this F close function. We just want to pass in this F pointer
2829
03:35:22,600 --> 03:35:24,550
So I'm just gonna say F
2830
03:35:24,550 --> 03:35:30,540
Pointer right there. So let's talk about what's happening here file is basically just like
2831
03:35:31,090 --> 03:35:37,860
Essentially like a data type I guess and we're creating a pointer to a physical file on our computer
2832
03:35:38,380 --> 03:35:41,460
So if you remember a pointer is basically just a memory address
2833
03:35:41,950 --> 03:35:46,799
Right a pointer is a type of data that we can use in our programs. It's essentially just a memory address and
2834
03:35:47,470 --> 03:35:54,660
this pointer is storing the memory address of this new employees dot text file that we're gonna create so
2835
03:35:55,060 --> 03:35:59,189
Essentially what's going to happen is we're gonna create this employee's dot txt file and F
2836
03:35:59,410 --> 03:36:04,889
Pointer is going to be pointing to the memory address of that file on our computer
2837
03:36:05,920 --> 03:36:11,670
So that's essentially what's happening here. And what I want to do is I actually want to write some information
2838
03:36:12,189 --> 03:36:17,759
To this file. So actually let me just show you guys really quick. What's happening here
2839
03:36:17,760 --> 03:36:24,870
So without doing anything else just using this line right here when I run my program a new file called
2840
03:36:25,870 --> 03:36:28,349
Employees dot text is going to get created so over here
2841
03:36:28,350 --> 03:36:30,840
I'm in my little directory and
2842
03:36:31,180 --> 03:36:36,750
If you don't specify an absolute path or like a relative path for this
2843
03:36:36,910 --> 03:36:40,169
It's just gonna create it in the directory that your C files in
2844
03:36:40,270 --> 03:36:43,470
So if I wanted to create this on my C Drive, I could say like C
2845
03:36:44,680 --> 03:36:51,210
Whatever and then like employees out text and this will get created in my C Drive, but if I don't specify like an absolute path
2846
03:36:52,030 --> 03:36:54,269
Or even if I don't specify a relative path
2847
03:36:54,580 --> 03:36:58,769
then this is just going to get created in the same directory as our C file and
2848
03:36:59,439 --> 03:37:01,210
You'll see over here
2849
03:37:01,210 --> 03:37:04,379
My main dot C file is right here. It's in this folder
2850
03:37:04,840 --> 03:37:10,440
So when I run this program, let's watch over here and we can see this employees doubt text file get created
2851
03:37:10,440 --> 03:37:15,149
So I'm gonna run my program and you'll see over here that this employees file got created
2852
03:37:15,150 --> 03:37:18,600
So if I actually bring this out a little bit you'll see it's a text document
2853
03:37:19,181 --> 03:37:24,330
Its employees dot tax and there's zero kilobytes inside of this so we haven't put any information in there
2854
03:37:24,460 --> 03:37:30,570
But this file got created. So now let's talk about putting some info into that file inside this file
2855
03:37:30,570 --> 03:37:34,829
I'm basically just gonna list out a bunch of like employees that might work in an office and
2856
03:37:35,080 --> 03:37:38,580
The easiest way to write information to a file is just to say F
2857
03:37:39,311 --> 03:37:41,761
Print F and this is a special
2858
03:37:43,631 --> 03:37:47,220
Function in C, which actually works a lot like the printf function
2859
03:37:47,221 --> 03:37:50,190
So if you've used the printf function and if you're following along with this course
2860
03:37:50,190 --> 03:37:57,600
We've been using it throughout the entire course the printf function basically allows you to like print information out onto that console window F
2861
03:37:58,271 --> 03:38:02,221
Printf is it can allow us to essentially just write information to a file?
2862
03:38:02,221 --> 03:38:07,051
I need to give this a couple parameters. The first one is gonna be this file pointer in other words
2863
03:38:07,051 --> 03:38:12,900
I need to tell this function, you know what it should be writing to so I'm just gonna say f pointer and
2864
03:38:13,540 --> 03:38:20,040
Then over here we can just give this some information so I can just use like after we put in this file pointer. I
2865
03:38:20,290 --> 03:38:26,130
Can just use this like I normally would the printf function so I could just print out like some employees
2866
03:38:26,320 --> 03:38:28,890
So I could say like let's say one of the employees is Jim
2867
03:38:29,410 --> 03:38:33,630
And we'll put like what they do. So like I can say sales men and
2868
03:38:34,660 --> 03:38:40,320
then we could say actually let's make a new line and we'll create another employee Pam and she is the
2869
03:38:41,290 --> 03:38:44,220
receptionist and then we can make some other employees so
2870
03:38:45,610 --> 03:38:52,860
So now I have three employees that I'm going to basically write out to my employees file. So now when I run this program and
2871
03:38:54,190 --> 03:38:56,249
We can go over here to this employees file
2872
03:38:56,860 --> 03:39:00,989
You'll see when I open this up that we have these three lines in here
2873
03:39:00,990 --> 03:39:04,530
So we have Jim salesman Pam receptionist and Oscar accounting
2874
03:39:04,530 --> 03:39:08,159
So basically I was able to create this file and write all this
2875
03:39:08,320 --> 03:39:13,829
Information in the file from down here just from down here in this program. So that's pretty cool
2876
03:39:14,141 --> 03:39:18,091
And I want to show you guys with right so when I'm using this, right?
2877
03:39:19,030 --> 03:39:25,180
This is actually going to overwrite a file. So for example if I change this to from all this text and I just put like
2878
03:39:28,701 --> 03:39:32,020
Overridden in here now when I run this program and
2879
03:39:32,871 --> 03:39:36,460
We open this employee's dot text file. So let me reopen this
2880
03:39:37,431 --> 03:39:42,520
You'll see it just says overridden so I've actually overridden all the text that was in there
2881
03:39:42,521 --> 03:39:48,041
And that's basically what happens when we just write to a file. So let's go back to what we had before
2882
03:39:48,041 --> 03:39:50,500
I'm just gonna run this again. So we have this text
2883
03:39:52,641 --> 03:39:58,421
And you'll see now we're back with the employees so let's say that um inside of this file
2884
03:39:58,421 --> 03:40:00,730
We wanted to add an employee on to the end of it
2885
03:40:00,730 --> 03:40:04,300
So we wanted to add like a new employee on to this file, right?
2886
03:40:04,881 --> 03:40:12,010
Well, I can use something called append and what append is gonna do is it's going to allow us to append a new line on
2887
03:40:12,141 --> 03:40:18,190
To the end of this file or actually just append text on to the end of the file instead of saying w
2888
03:40:18,190 --> 03:40:25,030
I'm just gonna say a and you'll notice I'm still keeping employees text here and down here inside of this F printf
2889
03:40:25,031 --> 03:40:26,751
I'm just gonna print out
2890
03:40:26,751 --> 03:40:28,451
another employee into the file
2891
03:40:28,451 --> 03:40:34,600
The first thing I'm gonna do is print out a new line because and actually let me show you guys this in this employees filed
2892
03:40:36,381 --> 03:40:42,340
There actually isn't a line after this last line so the last like position in this file is over here
2893
03:40:42,341 --> 03:40:44,921
I can't actually get on to the line below Oscar
2894
03:40:45,501 --> 03:40:50,590
So I'm gonna want to go ahead and print a new line and this will move the next employee on to the next line
2895
03:40:50,591 --> 03:40:52,591
we could say like
2896
03:40:52,641 --> 03:40:55,391
Kelly and she's gonna work in customer
2897
03:40:56,181 --> 03:41:03,041
Service. So now when I run my program instead of overriding what was in the employees text file now?
2898
03:41:03,041 --> 03:41:08,800
This is just gonna append this new employee onto the file. So I'll come up here run the program
2899
03:41:09,801 --> 03:41:17,531
And let's open this employee's file and you'll see now we have another entry Kelly from customer service. So when we write a file
2900
03:41:18,741 --> 03:41:24,220
We either create a new file and add content onto it or we just override the content in the file
2901
03:41:24,741 --> 03:41:30,340
At least using this F printf function and when we append, we're sub pending information onto the end of the file
2902
03:41:30,471 --> 03:41:32,560
so just using those two things, I mean you can
2903
03:41:33,111 --> 03:41:35,111
essentially write out any you want
2904
03:41:35,190 --> 03:41:38,839
and it's important to note that you can write more than just text file so I could write like an
2905
03:41:38,940 --> 03:41:41,659
HTML file or I could write like a CSS file
2906
03:41:42,930 --> 03:41:46,370
you could write really I mean any type of file that you'd want and
2907
03:41:47,070 --> 03:41:50,989
That's basically how we can write to a file and append it to the end of a file
2908
03:41:54,830 --> 03:41:55,520
In this tutorial
2909
03:41:55,520 --> 03:41:57,649
I will not teach you guys how to read the
2910
03:41:58,020 --> 03:42:03,590
Information from a file in C in the last tutorial we talked about writing to files
2911
03:42:03,590 --> 03:42:10,399
So we looked at how we could create a file overwrite a file and append information on to a file in this tutorial
2912
03:42:10,399 --> 03:42:12,399
I'm gonna show you guys how you can read
2913
03:42:12,569 --> 03:42:17,869
Information from a file so down here you'll notice that I have my file pointer setup
2914
03:42:17,870 --> 03:42:22,759
So I just say file and the asterisks and I'd called it FP and remember
2915
03:42:22,760 --> 03:42:26,000
this is just a pointer which you know and a pointer is just a
2916
03:42:26,640 --> 03:42:31,760
Memory address and it's the memory address of this file in
2917
03:42:32,280 --> 03:42:39,649
Memory, so it's the memory address of this employees dot txt file which we created in the last tutorial and I'm using this F open
2918
03:42:39,649 --> 03:42:41,649
function which basically
2919
03:42:42,000 --> 03:42:46,759
Opens this file and then I'm specifying a file mode
2920
03:42:46,830 --> 03:42:50,419
We talked about the W file mode and the a file mode now
2921
03:42:50,420 --> 03:42:53,750
I want to talk to you guys about the our file mode and this stands for read
2922
03:42:54,060 --> 03:42:55,729
So when I use the our file mode
2923
03:42:55,729 --> 03:43:00,259
I'm actually able to read information from the file and this can be pretty cool
2924
03:43:00,689 --> 03:43:03,379
So in order to read the information in the file
2925
03:43:03,390 --> 03:43:07,430
I'm actually going to show you guys how we can read like the individual lines of the file
2926
03:43:07,430 --> 03:43:09,430
So actually first let's look at the file
2927
03:43:09,750 --> 03:43:16,639
This is employees dot txt. It just has you know, basically a bunch of employees their names and then what they do and
2928
03:43:17,520 --> 03:43:20,210
We're gonna be able to read this file inside of our program
2929
03:43:20,210 --> 03:43:22,309
so the first thing I want to do is
2930
03:43:22,649 --> 03:43:27,259
create a variable and I'm gonna create this variable up here before we create the file pointer and
2931
03:43:28,319 --> 03:43:32,209
it's just gonna be an array of characters and I'm just gonna call it line and
2932
03:43:32,970 --> 03:43:39,920
I'm gonna give this an initial size of 255 so be able to store 255 characters and
2933
03:43:40,380 --> 03:43:42,380
then down here in
2934
03:43:42,600 --> 03:43:46,850
Order to read this file. I can actually read the file one line at a time and
2935
03:43:47,460 --> 03:43:49,460
We can use a special
2936
03:43:49,649 --> 03:43:50,520
function
2937
03:43:50,520 --> 03:43:51,600
Called F
2938
03:43:51,600 --> 03:43:58,790
Gets and F gets basically is going to allow us to read the lines of the file one by one so I could say F
2939
03:43:58,790 --> 03:44:03,019
Gets and I need to give this a few different parameters the first parameter
2940
03:44:03,020 --> 03:44:07,189
I need to give it is a place to store the information that it gets
2941
03:44:07,189 --> 03:44:12,199
So this function is going to go and it's going to read the information from the file and it's going to store it somewhere
2942
03:44:12,270 --> 03:44:16,640
So the first parameter we need to give this function is where it should store that information
2943
03:44:16,770 --> 03:44:17,600
so in our case
2944
03:44:17,600 --> 03:44:24,260
We're going to store it inside of that align variable that we created up there. The second thing we need to give this is a
2945
03:44:24,780 --> 03:44:27,049
size and this is basically just gonna be like
2946
03:44:27,990 --> 03:44:35,990
The maximum size that we can read in so I'm just gonna say 255. Basically this number should match this number up here and
2947
03:44:36,990 --> 03:44:39,589
Then we also want to give it that file pointer
2948
03:44:39,590 --> 03:44:41,580
so I'm just gonna say f
2949
03:44:41,580 --> 03:44:48,200
Pointer and basically what this is gonna do is it's going to read the first line of the file and it's going to store it
2950
03:44:48,930 --> 03:44:55,370
Inside of this line variable. So if I was to come down here and say print F and
2951
03:44:56,189 --> 03:44:58,189
print it out a string and
2952
03:44:58,890 --> 03:45:01,700
Just print out line now when I print out a line
2953
03:45:01,700 --> 03:45:07,429
It's actually going to be have the value of the first line in the file. So let's run this program and
2954
03:45:08,100 --> 03:45:10,910
You'll see over here. We're printing out Jim salesman
2955
03:45:10,910 --> 03:45:15,799
And if you didn't remember that was the first line inside of that employees dot text
2956
03:45:16,109 --> 03:45:23,029
So basically what F gets is doing is it's reading the first line. And another thing that it's doing is it's moving this pointer
2957
03:45:23,189 --> 03:45:25,189
so remember this pointer is
2958
03:45:25,859 --> 03:45:33,049
Essentially pointing to where the file is in our memory and whenever we use F gets this pointer is gonna get incremented
2959
03:45:33,050 --> 03:45:36,530
So it's basically gonna move past the first line in the file
2960
03:45:37,140 --> 03:45:39,530
So if I was to use this again?
2961
03:45:40,290 --> 03:45:42,499
For example if I said f gets again
2962
03:45:43,800 --> 03:45:44,930
when I use this
2963
03:45:44,930 --> 03:45:48,979
It's gonna store the value of the first line of the file inside of line
2964
03:45:48,979 --> 03:45:54,559
And when I use it again, it's now gonna grab the second line in the file. So if I print this out now
2965
03:45:55,319 --> 03:46:01,909
This should actually be storing the second line in the file. So when I run my program you'll see it's storing the second line Pam
2966
03:46:02,460 --> 03:46:06,379
Receptionist I can basically do this for all of the lines in the file
2967
03:46:06,420 --> 03:46:11,780
So that's basically how we can use F gets to read the individual lines inside of a file
333317
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.