Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,160 --> 00:00:03,679
all right what's going on everybody it's
2
00:00:02,080 --> 00:00:05,359
you bro hope you're doing well and in
3
00:00:03,679 --> 00:00:08,639
this video i'm going to explain how we
4
00:00:05,359 --> 00:00:12,559
can get started writing code in c so sit
5
00:00:08,639 --> 00:00:12,559
back relax and enjoy the show
6
00:00:12,799 --> 00:00:18,080
if you wouldn't mind please like comment
7
00:00:15,360 --> 00:00:20,320
and subscribe one like equals one prayer
8
00:00:18,080 --> 00:00:22,320
for the youtube algorithm
9
00:00:20,320 --> 00:00:25,519
i'm going to tell you why you need to
10
00:00:22,320 --> 00:00:28,480
learn c c is a middle level language
11
00:00:25,519 --> 00:00:30,880
that originated in the 1970s and it is
12
00:00:28,480 --> 00:00:33,680
one of the most widely used programming
13
00:00:30,880 --> 00:00:35,760
languages to date on a spectrum of
14
00:00:33,680 --> 00:00:39,280
high-level languages to low-level
15
00:00:35,760 --> 00:00:41,600
languages c is a middle level language
16
00:00:39,280 --> 00:00:44,160
low-level languages are efficient they
17
00:00:41,600 --> 00:00:46,160
work closely with machine architecture
18
00:00:44,160 --> 00:00:48,800
they consume less memory and they're
19
00:00:46,160 --> 00:00:51,039
fast as however they're difficult
20
00:00:48,800 --> 00:00:53,760
to understand and it takes more time to
21
00:00:51,039 --> 00:00:56,079
write code that's in a low level format
22
00:00:53,760 --> 00:00:58,320
high-level languages are easier to work
23
00:00:56,079 --> 00:01:00,960
with easier to understand they allow for
24
00:00:58,320 --> 00:01:03,199
more abstraction but they're slower they
25
00:01:00,960 --> 00:01:05,439
use more memory and they're abstract so
26
00:01:03,199 --> 00:01:08,479
it's difficult to work with low level
27
00:01:05,439 --> 00:01:10,640
hardware and systems a major advantage
28
00:01:08,479 --> 00:01:13,280
of middle level languages is that they
29
00:01:10,640 --> 00:01:16,400
can act as a bridge between high-level
30
00:01:13,280 --> 00:01:18,960
software and applications as well as
31
00:01:16,400 --> 00:01:21,680
low-level hardware and embedded systems
32
00:01:18,960 --> 00:01:24,000
most compilers kernels and operating
33
00:01:21,680 --> 00:01:26,640
systems are written in c
34
00:01:24,000 --> 00:01:29,920
nearly all programming languages are
35
00:01:26,640 --> 00:01:31,759
influenced by c in some way c is what
36
00:01:29,920 --> 00:01:34,960
provided many of the original
37
00:01:31,759 --> 00:01:38,799
programming concepts such as variables
38
00:01:34,960 --> 00:01:41,600
data types loops arrays functions etc in
39
00:01:38,799 --> 00:01:44,640
fact the python language is written with
40
00:01:41,600 --> 00:01:47,119
c the default implementation is known as
41
00:01:44,640 --> 00:01:49,360
c python if you're already familiar with
42
00:01:47,119 --> 00:01:51,439
the programming language learning c will
43
00:01:49,360 --> 00:01:54,560
give you an even deeper understanding of
44
00:01:51,439 --> 00:01:57,040
how those operate c is literally
45
00:01:54,560 --> 00:01:59,680
everywhere from databases to
46
00:01:57,040 --> 00:02:02,000
self-driving cars operating systems to
47
00:01:59,680 --> 00:02:04,719
embedded systems it's been around for so
48
00:02:02,000 --> 00:02:06,320
long and used for so many purposes here
49
00:02:04,719 --> 00:02:09,119
are some important notes before we get
50
00:02:06,320 --> 00:02:12,480
started c is not an object-oriented
51
00:02:09,119 --> 00:02:14,879
language it's procedural not abstract c
52
00:02:12,480 --> 00:02:18,160
plus plus is an object-oriented
53
00:02:14,879 --> 00:02:21,280
extension of c if you know c you already
54
00:02:18,160 --> 00:02:23,680
know some c plus plus however c is a
55
00:02:21,280 --> 00:02:25,440
difficult language for beginners don't
56
00:02:23,680 --> 00:02:28,080
get discouraged you can do it so what
57
00:02:25,440 --> 00:02:30,720
you'll need you'll need an ide an
58
00:02:28,080 --> 00:02:33,680
integrated development environment which
59
00:02:30,720 --> 00:02:37,360
is basically a fancy text editor to help
60
00:02:33,680 --> 00:02:39,920
us write c code as well as a gnu
61
00:02:37,360 --> 00:02:44,160
compiler collection which we abbreviate
62
00:02:39,920 --> 00:02:47,040
to simply gcc this compiles or otherwise
63
00:02:44,160 --> 00:02:49,040
converts c code to machine code you know
64
00:02:47,040 --> 00:02:51,440
all those ones and zeros that a machine
65
00:02:49,040 --> 00:02:54,959
can read let's begin by downloading an
66
00:02:51,440 --> 00:02:56,879
ide i recommend vs code it's flexible
67
00:02:54,959 --> 00:02:57,840
and you can use vs code for more than
68
00:02:56,879 --> 00:02:59,760
just c
69
00:02:57,840 --> 00:03:02,800
all right what you're gonna do is head
70
00:02:59,760 --> 00:03:04,720
to code.visualstudio.com
71
00:03:02,800 --> 00:03:06,640
and then look for this drop down menu to
72
00:03:04,720 --> 00:03:08,400
install for your operating system i'm
73
00:03:06,640 --> 00:03:11,519
running windows i'm going to install for
74
00:03:08,400 --> 00:03:12,720
windows and then i will simply just open
75
00:03:11,519 --> 00:03:16,319
when done
76
00:03:12,720 --> 00:03:18,720
okay accept the license agreement next
77
00:03:16,319 --> 00:03:20,800
you can create a desktop icon and add to
78
00:03:18,720 --> 00:03:22,720
path next
79
00:03:20,800 --> 00:03:24,319
then install
80
00:03:22,720 --> 00:03:26,640
and then give it a second
81
00:03:24,319 --> 00:03:28,400
or a couple minutes
82
00:03:26,640 --> 00:03:30,319
then you can launch this if you prefer i
83
00:03:28,400 --> 00:03:32,480
think i will okay we are now within
84
00:03:30,319 --> 00:03:34,640
visual studio head to the left hand menu
85
00:03:32,480 --> 00:03:36,879
for extensions we're going to install
86
00:03:34,640 --> 00:03:39,599
two extensions c
87
00:03:36,879 --> 00:03:41,280
c plus plus that contains intellisense
88
00:03:39,599 --> 00:03:42,879
and a couple other useful things so
89
00:03:41,280 --> 00:03:45,200
install that
90
00:03:42,879 --> 00:03:47,920
and then next we will install code
91
00:03:45,200 --> 00:03:49,840
runner code runner
92
00:03:47,920 --> 00:03:51,920
install after installing these
93
00:03:49,840 --> 00:03:54,239
extensions you may need to restart vs
94
00:03:51,920 --> 00:03:54,239
code
95
00:03:54,959 --> 00:03:58,720
okay then we are going to add a new
96
00:03:56,640 --> 00:04:00,560
folder go to the left-hand menu add
97
00:03:58,720 --> 00:04:04,640
folder i'll create a new folder on my
98
00:04:00,560 --> 00:04:06,080
desktop so i'll right click go to new
99
00:04:04,640 --> 00:04:09,599
folder
100
00:04:06,080 --> 00:04:11,519
i'll name this c files
101
00:04:09,599 --> 00:04:12,799
then add
102
00:04:11,519 --> 00:04:14,720
i think you might have to click within
103
00:04:12,799 --> 00:04:17,919
the folder
104
00:04:14,720 --> 00:04:20,079
okay we now have a folder named c files
105
00:04:17,919 --> 00:04:22,079
then to create a new c file go to new
106
00:04:20,079 --> 00:04:24,800
file after clicking this folder
107
00:04:22,079 --> 00:04:27,120
i'll name this hello world and make sure
108
00:04:24,800 --> 00:04:28,960
that it ends with the c extension
109
00:04:27,120 --> 00:04:30,960
helloworld.c
110
00:04:28,960 --> 00:04:33,440
and we now have a c file that we can
111
00:04:30,960 --> 00:04:36,479
work with and on this tab at the top
112
00:04:33,440 --> 00:04:38,639
this says hello world dot c now the next
113
00:04:36,479 --> 00:04:41,360
thing that we'll need is that gcc
114
00:04:38,639 --> 00:04:43,360
compiler to convert c code to machine
115
00:04:41,360 --> 00:04:45,520
code now if you're running windows this
116
00:04:43,360 --> 00:04:47,680
is how to check to see if you have a gcc
117
00:04:45,520 --> 00:04:51,120
compiler already installed
118
00:04:47,680 --> 00:04:54,560
so you're going to open command prompt
119
00:04:51,120 --> 00:04:57,600
and enter this command g plus plus
120
00:04:54,560 --> 00:04:59,440
dash dash version i already have a gcc
121
00:04:57,600 --> 00:05:00,800
compiler already installed if you're
122
00:04:59,440 --> 00:05:02,479
getting an error then you'll probably
123
00:05:00,800 --> 00:05:04,960
have to download one here's an
124
00:05:02,479 --> 00:05:06,880
interruption from future bro i traveled
125
00:05:04,960 --> 00:05:09,039
from the future to the past to deliver
126
00:05:06,880 --> 00:05:12,639
you this message so if you need to
127
00:05:09,039 --> 00:05:14,240
install gcc on a mac operating system
128
00:05:12,639 --> 00:05:16,160
what you're going to do within a
129
00:05:14,240 --> 00:05:20,080
terminal window is enter the following
130
00:05:16,160 --> 00:05:22,400
command c lang dash dash version if c
131
00:05:20,080 --> 00:05:26,160
lang isn't installed enter the following
132
00:05:22,400 --> 00:05:28,400
command xcode dash select dash dash
133
00:05:26,160 --> 00:05:29,919
install and that's all there is to it if
134
00:05:28,400 --> 00:05:32,360
you need additional assistance you can
135
00:05:29,919 --> 00:05:34,160
always visit this webpage
136
00:05:32,360 --> 00:05:37,280
code.visualstudio.com
137
00:05:34,160 --> 00:05:40,000
docs slash cpp and if you're running on
138
00:05:37,280 --> 00:05:43,120
linux within a terminal window you'll
139
00:05:40,000 --> 00:05:46,479
enter this command instead gcc
140
00:05:43,120 --> 00:05:47,440
dash v if gcc isn't installed run this
141
00:05:46,479 --> 00:05:51,600
command
142
00:05:47,440 --> 00:05:53,680
sudo apt-get update and then next type
143
00:05:51,600 --> 00:05:55,600
in this long command and if you need any
144
00:05:53,680 --> 00:05:57,680
additional assistance or documentation
145
00:05:55,600 --> 00:06:01,880
you can always visit this webpage so
146
00:05:57,680 --> 00:06:04,720
google this min gw dash w64
147
00:06:01,880 --> 00:06:07,039
install.exe and the first link is for
148
00:06:04,720 --> 00:06:09,280
sourceforge so click on that
149
00:06:07,039 --> 00:06:12,680
then you can find this underneath home
150
00:06:09,280 --> 00:06:16,319
browse development compilers
151
00:06:12,680 --> 00:06:18,560
mingw64 for 32 and 64-bit windows and
152
00:06:16,319 --> 00:06:21,840
the download should start automatically
153
00:06:18,560 --> 00:06:24,639
so click next
154
00:06:21,840 --> 00:06:26,639
change the architecture to
155
00:06:24,639 --> 00:06:28,880
x8664
156
00:06:26,639 --> 00:06:30,479
next and then copy this path for the
157
00:06:28,880 --> 00:06:32,080
destination folder it's going to be
158
00:06:30,479 --> 00:06:34,880
relevant later
159
00:06:32,080 --> 00:06:37,039
then next next and finish
160
00:06:34,880 --> 00:06:39,840
now what we're going to do is add our
161
00:06:37,039 --> 00:06:44,639
path to our gcc underneath environment
162
00:06:39,840 --> 00:06:44,639
variables so open up control panel
163
00:06:45,759 --> 00:06:50,960
then go to system and security
164
00:06:48,479 --> 00:06:54,240
then system
165
00:06:50,960 --> 00:06:56,240
scroll down to advanced system settings
166
00:06:54,240 --> 00:06:58,800
underneath the advanced tab
167
00:06:56,240 --> 00:07:01,919
go to environment variables
168
00:06:58,800 --> 00:07:03,599
underneath path we are going to edit i
169
00:07:01,919 --> 00:07:05,599
already have this path configured so i'm
170
00:07:03,599 --> 00:07:08,319
going to delete this you probably won't
171
00:07:05,599 --> 00:07:12,479
have this setup and then new
172
00:07:08,319 --> 00:07:16,800
paste that file path to the gcc compiler
173
00:07:12,479 --> 00:07:18,240
then add slash min 64 slash bin then
174
00:07:16,800 --> 00:07:19,360
okay
175
00:07:18,240 --> 00:07:22,960
okay
176
00:07:19,360 --> 00:07:22,960
and then you can close out of everything
177
00:07:23,440 --> 00:07:29,280
and now we need to configure our build
178
00:07:25,599 --> 00:07:31,280
task so go to a terminal
179
00:07:29,280 --> 00:07:32,960
your default build task if nothing
180
00:07:31,280 --> 00:07:35,919
appears within the search box you may
181
00:07:32,960 --> 00:07:38,720
need to restart vs code i think i do
182
00:07:35,919 --> 00:07:40,560
so i'm going to restart it
183
00:07:38,720 --> 00:07:42,639
and then let's try that again terminal
184
00:07:40,560 --> 00:07:45,280
configure default build task and there
185
00:07:42,639 --> 00:07:47,759
it is i will select that
186
00:07:45,280 --> 00:07:50,240
this creates a json file that tells vs
187
00:07:47,759 --> 00:07:52,639
code how to compile the program and with
188
00:07:50,240 --> 00:07:54,800
that out of the way we can begin coding
189
00:07:52,639 --> 00:07:56,639
now before we do start coding anything
190
00:07:54,800 --> 00:07:58,800
i'm going to increase the font size
191
00:07:56,639 --> 00:08:01,680
because as you can see this font size is
192
00:07:58,800 --> 00:08:05,120
really small so within vs code to change
193
00:08:01,680 --> 00:08:07,360
the font size go to file preferences
194
00:08:05,120 --> 00:08:10,160
settings and you can change that here
195
00:08:07,360 --> 00:08:11,759
let's try maybe 20. you can also change
196
00:08:10,160 --> 00:08:12,720
the font family too if you want and
197
00:08:11,759 --> 00:08:14,400
everything
198
00:08:12,720 --> 00:08:15,440
uh but let's close out of that and try
199
00:08:14,400 --> 00:08:17,360
that again
200
00:08:15,440 --> 00:08:19,680
okay that isn't too bad i'll stick with
201
00:08:17,360 --> 00:08:21,599
this font size for now i also recommend
202
00:08:19,680 --> 00:08:22,960
enabling autosave that's going to save
203
00:08:21,599 --> 00:08:25,360
you a lot of headaches later in the
204
00:08:22,960 --> 00:08:26,639
future so go to file
205
00:08:25,360 --> 00:08:28,240
auto save
206
00:08:26,639 --> 00:08:31,120
okay the first thing that we're going to
207
00:08:28,240 --> 00:08:32,880
include within our c program is the word
208
00:08:31,120 --> 00:08:36,000
hashtag include
209
00:08:32,880 --> 00:08:38,240
so this is a pre-processor command that
210
00:08:36,000 --> 00:08:40,959
tells the compiler to include the
211
00:08:38,240 --> 00:08:43,200
contents of a file and the file that we
212
00:08:40,959 --> 00:08:44,320
would like to include is within angle
213
00:08:43,200 --> 00:08:49,680
brackets
214
00:08:44,320 --> 00:08:51,839
std for standard io input output dot h
215
00:08:49,680 --> 00:08:53,920
this file contains some useful functions
216
00:08:51,839 --> 00:08:54,880
related to input and output so we'll
217
00:08:53,920 --> 00:08:57,120
need that
218
00:08:54,880 --> 00:08:59,279
now the entry point of our program is
219
00:08:57,120 --> 00:09:02,560
the main function type int
220
00:08:59,279 --> 00:09:04,959
main parentheses curly braces anything
221
00:09:02,560 --> 00:09:06,959
within our main function is read
222
00:09:04,959 --> 00:09:09,200
procedurally starting from the top and
223
00:09:06,959 --> 00:09:11,200
working its way down so anything within
224
00:09:09,200 --> 00:09:13,200
this set of curly braces is within the
225
00:09:11,200 --> 00:09:14,560
main function and at the end of our main
226
00:09:13,200 --> 00:09:18,560
function we're going to add the
227
00:09:14,560 --> 00:09:20,640
statement return 0 semicolon a semicolon
228
00:09:18,560 --> 00:09:22,640
is used to terminate statements kind of
229
00:09:20,640 --> 00:09:24,399
like a period at the end of a sentence
230
00:09:22,640 --> 00:09:27,279
at the end of our main function we have
231
00:09:24,399 --> 00:09:30,000
this return zero statement this returns
232
00:09:27,279 --> 00:09:32,720
the exit status of our program we return
233
00:09:30,000 --> 00:09:35,120
a zero if our program runs successfully
234
00:09:32,720 --> 00:09:37,680
with no errors if there is an error then
235
00:09:35,120 --> 00:09:39,519
we'll return a 1. so now we can add
236
00:09:37,680 --> 00:09:42,160
anything that we want within this main
237
00:09:39,519 --> 00:09:44,399
function but we'll need return 0 at the
238
00:09:42,160 --> 00:09:46,320
end to check for any errors
239
00:09:44,399 --> 00:09:48,800
so let's print something to our console
240
00:09:46,320 --> 00:09:51,760
as output so to display something we're
241
00:09:48,800 --> 00:09:53,680
going to type print f parentheses
242
00:09:51,760 --> 00:09:55,600
semicolon because we end our statements
243
00:09:53,680 --> 00:09:57,519
with a semicolon and within the
244
00:09:55,600 --> 00:09:59,839
parentheses add a set of double quotes
245
00:09:57,519 --> 00:10:01,440
because we would like to literally print
246
00:09:59,839 --> 00:10:02,720
something and you can type in whatever
247
00:10:01,440 --> 00:10:04,880
you want let's say
248
00:10:02,720 --> 00:10:06,480
i like pizza
249
00:10:04,880 --> 00:10:08,800
then to run this code you can either
250
00:10:06,480 --> 00:10:11,680
right click then go to run code
251
00:10:08,800 --> 00:10:14,000
alternatively there is a run code button
252
00:10:11,680 --> 00:10:16,480
in the top right corner of the s code so
253
00:10:14,000 --> 00:10:19,279
after running this code this displays my
254
00:10:16,480 --> 00:10:21,200
output i like pizza so what if i would
255
00:10:19,279 --> 00:10:23,360
like to add a second line well i would
256
00:10:21,200 --> 00:10:25,440
just follow these steps again so i need
257
00:10:23,360 --> 00:10:29,200
another printf statement
258
00:10:25,440 --> 00:10:30,720
print f parentheses semicolon any text i
259
00:10:29,200 --> 00:10:33,200
would like to display i'll place that
260
00:10:30,720 --> 00:10:36,240
within a set of double quotes this time
261
00:10:33,200 --> 00:10:38,000
let's add a second line i like pizza
262
00:10:36,240 --> 00:10:39,279
it's really
263
00:10:38,000 --> 00:10:40,800
good
264
00:10:39,279 --> 00:10:42,640
and then save
265
00:10:40,800 --> 00:10:44,800
all right and this is what this looks
266
00:10:42,640 --> 00:10:47,680
like
267
00:10:44,800 --> 00:10:50,240
i like pizza it's really good so this is
268
00:10:47,680 --> 00:10:52,720
all one long line what if i would like
269
00:10:50,240 --> 00:10:55,200
my second line of text on the next line
270
00:10:52,720 --> 00:10:57,760
well i can add an escape sequence for a
271
00:10:55,200 --> 00:10:59,519
new line character so at the end of my
272
00:10:57,760 --> 00:11:01,360
printf statement within the double
273
00:10:59,519 --> 00:11:04,480
quotes i'll add
274
00:11:01,360 --> 00:11:06,720
backslash n for a new line character
275
00:11:04,480 --> 00:11:09,279
and let's try that again so i'm going to
276
00:11:06,720 --> 00:11:11,600
clear my output make sure i save and
277
00:11:09,279 --> 00:11:14,079
then run this again
278
00:11:11,600 --> 00:11:15,760
i like pizza it's really good and then
279
00:11:14,079 --> 00:11:17,600
we have that extra space at the bottom
280
00:11:15,760 --> 00:11:19,519
because we added an additional new line
281
00:11:17,600 --> 00:11:21,440
character which is optional
282
00:11:19,519 --> 00:11:25,120
also take notice too that we have this
283
00:11:21,440 --> 00:11:27,040
message exited with code equal zero so
284
00:11:25,120 --> 00:11:29,200
if there are no errors and your program
285
00:11:27,040 --> 00:11:31,680
runs successfully this function will
286
00:11:29,200 --> 00:11:34,079
return zero if there is an error well
287
00:11:31,680 --> 00:11:36,160
then this will return one so let's
288
00:11:34,079 --> 00:11:38,240
misspell something let's say instead of
289
00:11:36,160 --> 00:11:42,000
printf we just have print
290
00:11:38,240 --> 00:11:43,600
so save and then run this again
291
00:11:42,000 --> 00:11:46,079
okay it looks like we have an error
292
00:11:43,600 --> 00:11:48,320
exited with code equals one
293
00:11:46,079 --> 00:11:50,560
all right people well that's your first
294
00:11:48,320 --> 00:11:53,360
c program in the next video we'll cover
295
00:11:50,560 --> 00:11:55,120
escape sequences and comments i'll post
296
00:11:53,360 --> 00:11:56,959
this code to the comments section down
297
00:11:55,120 --> 00:11:59,440
below and pin it to the top if you would
298
00:11:56,959 --> 00:12:02,399
like a copy for yourself but yeah that
299
00:11:59,440 --> 00:12:04,800
is your first c program
300
00:12:02,399 --> 00:12:07,040
hey you yeah i'm talking to you if you
301
00:12:04,800 --> 00:12:09,760
learned something new then help me help
302
00:12:07,040 --> 00:12:12,079
you in three easy steps by smashing that
303
00:12:09,760 --> 00:12:13,839
like button drop a comment down below
304
00:12:12,079 --> 00:12:17,160
and subscribe if you'd like to become a
305
00:12:13,839 --> 00:12:17,160
fellow bro
306
00:12:18,210 --> 00:12:36,110
[Music]
307
00:12:38,639 --> 00:12:42,480
all right everybody welcome back in this
308
00:12:41,040 --> 00:12:45,279
video i'm going to show you all how we
309
00:12:42,480 --> 00:12:48,000
can compile and run a c program using
310
00:12:45,279 --> 00:12:50,639
command prompt in my text editor i have
311
00:12:48,000 --> 00:12:52,480
a simple c program that prints i love
312
00:12:50,639 --> 00:12:55,040
pizza it's really good what we'll need
313
00:12:52,480 --> 00:12:56,480
to do is open command prompt now before
314
00:12:55,040 --> 00:12:59,200
we begin we should make sure that we
315
00:12:56,480 --> 00:13:01,120
have a gcc compiler and to check that
316
00:12:59,200 --> 00:13:04,000
just type gcc
317
00:13:01,120 --> 00:13:05,680
minus minus version
318
00:13:04,000 --> 00:13:08,240
and it looks like i have one so if
319
00:13:05,680 --> 00:13:09,839
you're missing a gcc compiler check the
320
00:13:08,240 --> 00:13:12,240
first video in the series and i'll show
321
00:13:09,839 --> 00:13:14,399
you how to download one now step one to
322
00:13:12,240 --> 00:13:16,720
compiling a c file is that we need to
323
00:13:14,399 --> 00:13:18,079
change our active working directory so
324
00:13:16,720 --> 00:13:20,000
that it's pointing to the folder
325
00:13:18,079 --> 00:13:21,920
containing our c file an easy way to
326
00:13:20,000 --> 00:13:23,519
change that is that we need the file
327
00:13:21,920 --> 00:13:26,320
location so i'm going to right click on
328
00:13:23,519 --> 00:13:28,320
my c file go to properties copy this
329
00:13:26,320 --> 00:13:30,800
location and within command prompt i
330
00:13:28,320 --> 00:13:33,120
will type cd to change directory and
331
00:13:30,800 --> 00:13:35,040
then paste that location so our current
332
00:13:33,120 --> 00:13:37,040
active directory is pointing to that
333
00:13:35,040 --> 00:13:39,519
folder containing our c file and to
334
00:13:37,040 --> 00:13:40,880
compile a c file you type
335
00:13:39,519 --> 00:13:42,480
gcc
336
00:13:40,880 --> 00:13:45,680
the name of the file
337
00:13:42,480 --> 00:13:47,920
and mine is hello world dot c then hit
338
00:13:45,680 --> 00:13:49,600
enter so nothing appears to happen and
339
00:13:47,920 --> 00:13:51,920
that's good so let's take a look at the
340
00:13:49,600 --> 00:13:53,920
c folder again this is the file that we
341
00:13:51,920 --> 00:13:56,079
compiled it is an executable and all we
342
00:13:53,920 --> 00:13:58,240
have to do is run that so that is the
343
00:13:56,079 --> 00:14:00,320
third step a
344
00:13:58,240 --> 00:14:03,040
exe enter
345
00:14:00,320 --> 00:14:05,920
i love pizza it's really good so yeah
346
00:14:03,040 --> 00:14:07,839
that's how to compile and run a c file
347
00:14:05,920 --> 00:14:10,160
in command prompt first make sure that
348
00:14:07,839 --> 00:14:12,959
you have a gcc compiler that would be i
349
00:14:10,160 --> 00:14:14,720
guess step zero step one is to change
350
00:14:12,959 --> 00:14:16,800
the active working directory to the
351
00:14:14,720 --> 00:14:20,000
folder containing your c file compile
352
00:14:16,800 --> 00:14:23,519
the c file with gcc the name of the file
353
00:14:20,000 --> 00:14:26,000
and then run the compiled file a.exe so
354
00:14:23,519 --> 00:14:27,920
yeah that is how to compile and run a c
355
00:14:26,000 --> 00:14:29,600
file with command prompt if you found
356
00:14:27,920 --> 00:14:31,279
this video helpful please be sure to
357
00:14:29,600 --> 00:14:32,959
smash that like button leave a random
358
00:14:31,279 --> 00:14:35,360
comment down below and subscribe if
359
00:14:32,959 --> 00:14:37,279
you'd like to become a fellow bro
360
00:14:35,360 --> 00:14:38,880
hey y'all what's going on everybody it's
361
00:14:37,279 --> 00:14:40,800
you bro hope you're doing well and in
362
00:14:38,880 --> 00:14:43,920
this video i'm going to explain to both
363
00:14:40,800 --> 00:14:48,160
comments and escape sequences in c so
364
00:14:43,920 --> 00:14:48,160
sit back relax and enjoy the show
365
00:14:49,040 --> 00:14:54,160
all right welcome back so we have to
366
00:14:51,120 --> 00:14:57,279
discuss comments and escape sequences so
367
00:14:54,160 --> 00:15:00,079
a comment is some text that is ignored
368
00:14:57,279 --> 00:15:03,440
by the compiler that is used as an
369
00:15:00,079 --> 00:15:05,680
explanation description or a note for
370
00:15:03,440 --> 00:15:07,920
yourself or anyone else reading over
371
00:15:05,680 --> 00:15:08,720
your code so try to comment you will
372
00:15:07,920 --> 00:15:10,880
type
373
00:15:08,720 --> 00:15:13,440
two forward slashes then anything
374
00:15:10,880 --> 00:15:16,720
afterwards is considered a comment and
375
00:15:13,440 --> 00:15:17,519
will be ignored by the compiler this is
376
00:15:16,720 --> 00:15:19,199
a
377
00:15:17,519 --> 00:15:22,000
comment
378
00:15:19,199 --> 00:15:23,760
blah so if i was to run this
379
00:15:22,000 --> 00:15:25,760
this text will be ignored by the
380
00:15:23,760 --> 00:15:28,079
compiler and we do not see that as
381
00:15:25,760 --> 00:15:29,839
output i tend to use a lot of comments
382
00:15:28,079 --> 00:15:31,600
when explaining things so this is
383
00:15:29,839 --> 00:15:33,600
something you'll see fairly often in my
384
00:15:31,600 --> 00:15:35,680
videos now if you need a multi-line
385
00:15:33,600 --> 00:15:37,279
comment this is only for a single line
386
00:15:35,680 --> 00:15:39,920
comment if i was to type this again
387
00:15:37,279 --> 00:15:42,079
without those forward slashes this is a
388
00:15:39,920 --> 00:15:44,160
comment well our program thinks that
389
00:15:42,079 --> 00:15:47,040
this is some sort of code if we need a
390
00:15:44,160 --> 00:15:49,519
multi-line comment you will instead type
391
00:15:47,040 --> 00:15:51,360
forward slash asterisk then anything
392
00:15:49,519 --> 00:15:53,759
after is considered a comment you can
393
00:15:51,360 --> 00:15:56,639
see that this is all green now anything
394
00:15:53,759 --> 00:15:58,959
up to a asterisk and forward slash will
395
00:15:56,639 --> 00:15:59,920
be a multi-line comment
396
00:15:58,959 --> 00:16:01,680
this
397
00:15:59,920 --> 00:16:04,240
is a
398
00:16:01,680 --> 00:16:05,519
multi-line
399
00:16:04,240 --> 00:16:07,279
comment
400
00:16:05,519 --> 00:16:10,240
and again this is ignored by the
401
00:16:07,279 --> 00:16:13,360
compiler so if you need to write a note
402
00:16:10,240 --> 00:16:15,120
description or explanation for yourself
403
00:16:13,360 --> 00:16:17,600
or for somebody else you can write that
404
00:16:15,120 --> 00:16:19,839
within a single line comment or a
405
00:16:17,600 --> 00:16:21,839
multi-line comment and i use a lot of
406
00:16:19,839 --> 00:16:23,839
these for teaching purposes because i
407
00:16:21,839 --> 00:16:26,240
have a lot of explaining to do right
408
00:16:23,839 --> 00:16:28,959
okay let's move on to part two of this
409
00:16:26,240 --> 00:16:31,440
video we have escape sequences an escape
410
00:16:28,959 --> 00:16:34,800
sequence is a character combination
411
00:16:31,440 --> 00:16:37,519
consisting of a backslash followed by a
412
00:16:34,800 --> 00:16:40,399
letter or combination of digits they
413
00:16:37,519 --> 00:16:42,880
specify actions within a line of text
414
00:16:40,399 --> 00:16:44,560
otherwise known as a string so we
415
00:16:42,880 --> 00:16:47,199
learned in the last video that we can
416
00:16:44,560 --> 00:16:50,160
end our printf statement with a
417
00:16:47,199 --> 00:16:52,560
backslash n to create a new line this is
418
00:16:50,160 --> 00:16:55,040
the escape sequence for a new line and
419
00:16:52,560 --> 00:16:57,680
within a string of text a line of text
420
00:16:55,040 --> 00:17:00,240
and within a string of text we can add a
421
00:16:57,680 --> 00:17:02,480
new line wherever we want and as many as
422
00:17:00,240 --> 00:17:04,959
we want let's say that after each word
423
00:17:02,480 --> 00:17:08,079
within my line of text i would like to
424
00:17:04,959 --> 00:17:10,480
add each word to a new line so i can use
425
00:17:08,079 --> 00:17:12,400
the escape sequence for new line after
426
00:17:10,480 --> 00:17:15,760
each of these words so that would look
427
00:17:12,400 --> 00:17:18,559
like this i like pizza but you may have
428
00:17:15,760 --> 00:17:20,799
to work on the spacing though
429
00:17:18,559 --> 00:17:23,839
that's a little bit better so wherever
430
00:17:20,799 --> 00:17:26,640
you place a backslash n that will create
431
00:17:23,839 --> 00:17:29,840
a new line character another escape
432
00:17:26,640 --> 00:17:31,520
sequence is backslash t for a tab
433
00:17:29,840 --> 00:17:34,240
so let's say i have a few numbers here
434
00:17:31,520 --> 00:17:36,240
one two three and i would like to create
435
00:17:34,240 --> 00:17:38,960
even spacing between these numbers i can
436
00:17:36,240 --> 00:17:42,640
just add an escape sequence for a tab
437
00:17:38,960 --> 00:17:44,400
character one backslash t two backslash
438
00:17:42,640 --> 00:17:47,600
t
439
00:17:44,400 --> 00:17:49,440
and these numbers are spaced evenly
440
00:17:47,600 --> 00:17:50,640
or i can get really fancy and add a new
441
00:17:49,440 --> 00:17:52,480
line character
442
00:17:50,640 --> 00:17:55,120
then maybe a four
443
00:17:52,480 --> 00:17:57,440
tab five tab
444
00:17:55,120 --> 00:17:59,120
six new line character
445
00:17:57,440 --> 00:18:01,200
then maybe a7
446
00:17:59,120 --> 00:18:04,559
tab 8
447
00:18:01,200 --> 00:18:04,559
tab and then a 9.
448
00:18:05,039 --> 00:18:10,480
so now we have a grid of numbers all
449
00:18:07,600 --> 00:18:12,559
spaced evenly so that is the new line
450
00:18:10,480 --> 00:18:14,880
escape sequence and the tab escape
451
00:18:12,559 --> 00:18:17,039
sequence you can use them wherever and
452
00:18:14,880 --> 00:18:19,520
however many you want within a string of
453
00:18:17,039 --> 00:18:21,520
text within a printf statement what if
454
00:18:19,520 --> 00:18:22,880
we need to display quotes like we're
455
00:18:21,520 --> 00:18:25,520
quoting somebody
456
00:18:22,880 --> 00:18:27,840
i like pizza
457
00:18:25,520 --> 00:18:29,200
this is a quote from
458
00:18:27,840 --> 00:18:30,880
abraham
459
00:18:29,200 --> 00:18:34,559
lincoln
460
00:18:30,880 --> 00:18:36,160
probably i need to place quotes around i
461
00:18:34,559 --> 00:18:38,000
and pizza
462
00:18:36,160 --> 00:18:39,840
so if i were to write it like that well
463
00:18:38,000 --> 00:18:42,240
our program doesn't know where our
464
00:18:39,840 --> 00:18:44,880
string of text begins and ends it's kind
465
00:18:42,240 --> 00:18:47,200
of confused if we need to literally
466
00:18:44,880 --> 00:18:48,640
print double quotes we will add an
467
00:18:47,200 --> 00:18:51,360
escape sequence
468
00:18:48,640 --> 00:18:53,600
backslash then double quote
469
00:18:51,360 --> 00:18:56,720
and then add that here as well so this
470
00:18:53,600 --> 00:18:58,799
allows us to literally print some quotes
471
00:18:56,720 --> 00:19:02,559
as i'll put
472
00:18:58,799 --> 00:19:04,640
i like pizza abraham lincoln probably or
473
00:19:02,559 --> 00:19:07,919
if you need to display single quotes
474
00:19:04,640 --> 00:19:10,240
backslash single quotes
475
00:19:07,919 --> 00:19:12,320
so that's how to display single quotes
476
00:19:10,240 --> 00:19:15,440
and if you need to display a backslash
477
00:19:12,320 --> 00:19:18,720
that would be double backslashes
478
00:19:15,440 --> 00:19:20,880
this will literally print backslashes
479
00:19:18,720 --> 00:19:23,039
so yeah those are just a few escape
480
00:19:20,880 --> 00:19:24,960
sequences here's a list of a bunch of
481
00:19:23,039 --> 00:19:27,520
them but a lot of these really aren't
482
00:19:24,960 --> 00:19:30,000
going to be relevant to us so yeah those
483
00:19:27,520 --> 00:19:31,760
are comments and escape sequences and
484
00:19:30,000 --> 00:19:33,600
see if you found this video helpful
485
00:19:31,760 --> 00:19:35,919
please be sure to destroy that like
486
00:19:33,600 --> 00:19:37,600
button drop a random comment down below
487
00:19:35,919 --> 00:19:39,440
and subscribe if you'd like to become a
488
00:19:37,600 --> 00:19:41,520
fellow bro
489
00:19:39,440 --> 00:19:43,600
hey yeah it's you bro hope you're doing
490
00:19:41,520 --> 00:19:47,200
well and yeah we're doing stuff with
491
00:19:43,600 --> 00:19:50,559
variables today in c so sit back relax
492
00:19:47,200 --> 00:19:50,559
and well enjoy the show
493
00:19:50,880 --> 00:19:56,000
welcome back to another video so
494
00:19:53,120 --> 00:19:58,799
variables variables are allocated space
495
00:19:56,000 --> 00:20:01,440
and memory to store a value we refer to
496
00:19:58,799 --> 00:20:02,480
a variable's name to access the stored
497
00:20:01,440 --> 00:20:05,200
value
498
00:20:02,480 --> 00:20:07,440
that variable now behaves as if it was
499
00:20:05,200 --> 00:20:09,600
the value that it contains but to create
500
00:20:07,440 --> 00:20:11,840
a variable we first need to declare a
501
00:20:09,600 --> 00:20:13,600
name for a variable and then precede it
502
00:20:11,840 --> 00:20:15,440
with the type of data that we are
503
00:20:13,600 --> 00:20:18,080
storing creating a variable is done in
504
00:20:15,440 --> 00:20:20,320
two steps declaration and initialization
505
00:20:18,080 --> 00:20:22,320
so we need to first declare a variable
506
00:20:20,320 --> 00:20:25,200
to allocate some space in memory to
507
00:20:22,320 --> 00:20:27,679
store a value so we need to precede our
508
00:20:25,200 --> 00:20:30,000
variable name with the data type of what
509
00:20:27,679 --> 00:20:32,159
we plan on storing within this variable
510
00:20:30,000 --> 00:20:34,880
if i need to store a whole integer we
511
00:20:32,159 --> 00:20:37,120
would precede our variable name withint
512
00:20:34,880 --> 00:20:38,720
int for integer and let's say that this
513
00:20:37,120 --> 00:20:41,679
is variable x
514
00:20:38,720 --> 00:20:43,600
so this step is declaration we are
515
00:20:41,679 --> 00:20:46,320
creating space and memory to store a
516
00:20:43,600 --> 00:20:49,039
value and to actually store a value that
517
00:20:46,320 --> 00:20:50,480
step is initialization so we would take
518
00:20:49,039 --> 00:20:52,960
our variable name
519
00:20:50,480 --> 00:20:55,600
x in this example and set it equal to
520
00:20:52,960 --> 00:20:57,919
some value so we declared that this
521
00:20:55,600 --> 00:21:00,159
variable is an integer we can only store
522
00:20:57,919 --> 00:21:01,120
whole integers maybe the number one two
523
00:21:00,159 --> 00:21:02,360
three
524
00:21:01,120 --> 00:21:04,480
so this is
525
00:21:02,360 --> 00:21:06,640
initialization or you could combine
526
00:21:04,480 --> 00:21:10,720
these steps together and let's create
527
00:21:06,640 --> 00:21:13,600
into y into y equals 3 2 1. this is both
528
00:21:10,720 --> 00:21:15,520
declaration and initialization so
529
00:21:13,600 --> 00:21:18,640
creating and storing a variable takes
530
00:21:15,520 --> 00:21:20,159
two steps declaration and initialization
531
00:21:18,640 --> 00:21:22,559
and in order to create a variable you
532
00:21:20,159 --> 00:21:24,159
have to precede the variable name with
533
00:21:22,559 --> 00:21:26,559
the data type of what you plan on
534
00:21:24,159 --> 00:21:28,159
storing within that variable int for a
535
00:21:26,559 --> 00:21:29,679
whole integer but there's other data
536
00:21:28,159 --> 00:21:32,240
types too let's create some more
537
00:21:29,679 --> 00:21:34,080
variables what about int age with
538
00:21:32,240 --> 00:21:36,720
variable names you're not limited to
539
00:21:34,080 --> 00:21:38,080
only just x and y you can really name it
540
00:21:36,720 --> 00:21:40,159
whatever you want within some
541
00:21:38,080 --> 00:21:42,240
limitations but make sure that the
542
00:21:40,159 --> 00:21:44,880
variable name is descriptive of what it
543
00:21:42,240 --> 00:21:48,000
does so age is going to store an edge
544
00:21:44,880 --> 00:21:50,720
let's say that i am 21 years old so this
545
00:21:48,000 --> 00:21:52,640
is an integer a whole number if we need
546
00:21:50,720 --> 00:21:55,039
a number containing a decimal portion
547
00:21:52,640 --> 00:21:58,000
that would be a float for floating point
548
00:21:55,039 --> 00:22:00,240
number so one example of a variable that
549
00:21:58,000 --> 00:22:03,039
could contain a floating point number is
550
00:22:00,240 --> 00:22:05,559
a gpa grade point average let's say that
551
00:22:03,039 --> 00:22:10,400
my grade point average is a
552
00:22:05,559 --> 00:22:12,559
2.05 so this is a floating point number
553
00:22:10,400 --> 00:22:14,799
it's a number that contains a decimal
554
00:22:12,559 --> 00:22:17,039
portion we can also store single
555
00:22:14,799 --> 00:22:19,360
characters with the char data type like
556
00:22:17,039 --> 00:22:22,080
you're pronouncing charizard and this
557
00:22:19,360 --> 00:22:23,520
will be a letter grade let's say now to
558
00:22:22,080 --> 00:22:25,760
store a single character we have to
559
00:22:23,520 --> 00:22:28,640
place it within single quotes when we
560
00:22:25,760 --> 00:22:31,520
initialize it with my grade variable
561
00:22:28,640 --> 00:22:34,720
what about a c like my average grade is
562
00:22:31,520 --> 00:22:38,400
a c remember everybody c's get degrees
563
00:22:34,720 --> 00:22:41,520
so char stores a single character
564
00:22:38,400 --> 00:22:44,080
now c isn't an object-oriented language
565
00:22:41,520 --> 00:22:46,799
so there is no string data type because
566
00:22:44,080 --> 00:22:48,960
strings are technically objects so if we
567
00:22:46,799 --> 00:22:51,600
would like to store like somebody's name
568
00:22:48,960 --> 00:22:53,760
we need a series of characters so we can
569
00:22:51,600 --> 00:22:55,440
create what is called an array and to
570
00:22:53,760 --> 00:22:57,840
create an array we would follow our
571
00:22:55,440 --> 00:23:00,400
variable name with a set of square
572
00:22:57,840 --> 00:23:02,320
brackets and then assign this equal to
573
00:23:00,400 --> 00:23:04,400
some string of text some series of
574
00:23:02,320 --> 00:23:06,240
characters place your series of
575
00:23:04,400 --> 00:23:08,400
characters within double quotes and we
576
00:23:06,240 --> 00:23:11,120
can store more than one character so
577
00:23:08,400 --> 00:23:14,000
this data type would be technically an
578
00:23:11,120 --> 00:23:16,080
array of characters i'll create a whole
579
00:23:14,000 --> 00:23:18,400
separate video on arrays this is
580
00:23:16,080 --> 00:23:20,559
basically how you can emulate a string
581
00:23:18,400 --> 00:23:22,480
it's really just a whole combination of
582
00:23:20,559 --> 00:23:24,480
single characters there's still a lot
583
00:23:22,480 --> 00:23:26,000
more data types than just these four i
584
00:23:24,480 --> 00:23:28,480
thought i would cover just some of the
585
00:23:26,000 --> 00:23:30,480
more basic data types just because in
586
00:23:28,480 --> 00:23:32,960
this video we're going to focus more on
587
00:23:30,480 --> 00:23:35,120
variables than data types i'm planning a
588
00:23:32,960 --> 00:23:37,200
separate video just dedicated to data
589
00:23:35,120 --> 00:23:39,760
types because there's way more data
590
00:23:37,200 --> 00:23:42,799
types than just these four there's bytes
591
00:23:39,760 --> 00:23:45,200
there's doubles there's longs etc now
592
00:23:42,799 --> 00:23:47,200
how can we display the value stored
593
00:23:45,200 --> 00:23:49,279
within a variable within a printf
594
00:23:47,200 --> 00:23:52,320
statement here's how we have to use what
595
00:23:49,279 --> 00:23:55,200
is referred to as a format specifier
596
00:23:52,320 --> 00:23:57,360
let's say we have a printf statement
597
00:23:55,200 --> 00:23:59,600
and i would like to display my age
598
00:23:57,360 --> 00:24:01,840
within a message so let's create some
599
00:23:59,600 --> 00:24:05,360
text you are
600
00:24:01,840 --> 00:24:07,200
age years old if i would like to display
601
00:24:05,360 --> 00:24:10,080
the value contained within my edge
602
00:24:07,200 --> 00:24:12,400
variable wherever i would like to insert
603
00:24:10,080 --> 00:24:14,640
that value i will place a format
604
00:24:12,400 --> 00:24:17,440
specifier which is represented by a
605
00:24:14,640 --> 00:24:19,440
percent sign and then follow this with a
606
00:24:17,440 --> 00:24:22,000
secret character that represents the
607
00:24:19,440 --> 00:24:24,799
data type of what we're inserting so if
608
00:24:22,000 --> 00:24:27,440
i need to display my age variable i will
609
00:24:24,799 --> 00:24:30,720
use a percent sign as a placeholder
610
00:24:27,440 --> 00:24:32,960
followed by d for decimal and then after
611
00:24:30,720 --> 00:24:35,679
my string of text outside of the double
612
00:24:32,960 --> 00:24:37,679
quotes add comma then the name of the
613
00:24:35,679 --> 00:24:41,120
variable you would like to insert at
614
00:24:37,679 --> 00:24:44,640
this placeholder so at this location i
615
00:24:41,120 --> 00:24:47,600
will insert age and then let's try this
616
00:24:44,640 --> 00:24:49,520
you are 21 years old so let's try that
617
00:24:47,600 --> 00:24:51,679
again with a different variable let's
618
00:24:49,520 --> 00:24:54,960
say let's go with name
619
00:24:51,679 --> 00:24:56,799
i'll add a second printf statement print
620
00:24:54,960 --> 00:24:59,200
f
621
00:24:56,799 --> 00:25:01,760
and then let's say hello
622
00:24:59,200 --> 00:25:04,640
and i would like to insert my name here
623
00:25:01,760 --> 00:25:07,520
so use a percent sign as a placeholder
624
00:25:04,640 --> 00:25:09,760
it's a format specifier then to display
625
00:25:07,520 --> 00:25:10,720
a character array that would be s for
626
00:25:09,760 --> 00:25:13,039
string
627
00:25:10,720 --> 00:25:15,440
and then add comma
628
00:25:13,039 --> 00:25:17,600
name
629
00:25:15,440 --> 00:25:19,120
oh then we may need to add a new line
630
00:25:17,600 --> 00:25:21,840
character to the end of these because i
631
00:25:19,120 --> 00:25:24,720
forgot let's try that again
632
00:25:21,840 --> 00:25:26,840
hello bro you are 21 years old okay
633
00:25:24,720 --> 00:25:30,400
let's display our
634
00:25:26,840 --> 00:25:32,320
grade printf
635
00:25:30,400 --> 00:25:34,960
then within quotes
636
00:25:32,320 --> 00:25:36,000
your average
637
00:25:34,960 --> 00:25:36,960
grade
638
00:25:36,000 --> 00:25:39,120
is
639
00:25:36,960 --> 00:25:42,720
then to display a character variable
640
00:25:39,120 --> 00:25:44,880
that would be percent c for character
641
00:25:42,720 --> 00:25:46,799
then outside of our double quotes add
642
00:25:44,880 --> 00:25:49,440
comma the name of the variable we would
643
00:25:46,799 --> 00:25:50,480
like to insert at this location so comma
644
00:25:49,440 --> 00:25:52,000
grid
645
00:25:50,480 --> 00:25:53,279
i think i'm just going to space these
646
00:25:52,000 --> 00:25:55,440
out a little bit
647
00:25:53,279 --> 00:25:58,480
okay then i will add a new line
648
00:25:55,440 --> 00:25:58,480
character to the end of this
649
00:25:59,120 --> 00:26:04,720
okay hello bro you are 21 years old your
650
00:26:01,760 --> 00:26:07,120
average grade is c
651
00:26:04,720 --> 00:26:08,559
then to display a float that would be
652
00:26:07,120 --> 00:26:11,679
percent f
653
00:26:08,559 --> 00:26:13,679
print f
654
00:26:11,679 --> 00:26:15,039
your gpa
655
00:26:13,679 --> 00:26:16,880
is
656
00:26:15,039 --> 00:26:19,760
percent f
657
00:26:16,880 --> 00:26:21,840
then i'll add a new line character
658
00:26:19,760 --> 00:26:24,240
so follow this with comma the name of
659
00:26:21,840 --> 00:26:28,080
the variable gpa
660
00:26:24,240 --> 00:26:30,320
okay your gpa is 2.05
661
00:26:28,080 --> 00:26:32,240
so later on we'll discuss more about
662
00:26:30,320 --> 00:26:34,320
format specifiers there's ways that we
663
00:26:32,240 --> 00:26:36,159
can format how our variables are
664
00:26:34,320 --> 00:26:38,880
displayed i just realized that i
665
00:26:36,159 --> 00:26:41,360
misspelled average twice so yeah those
666
00:26:38,880 --> 00:26:44,240
are variables they are allocated space
667
00:26:41,360 --> 00:26:47,200
and memory to store a value we refer to
668
00:26:44,240 --> 00:26:48,159
a variable's name to access the stored
669
00:26:47,200 --> 00:26:51,039
value
670
00:26:48,159 --> 00:26:52,960
that variable now behaves as if it was
671
00:26:51,039 --> 00:26:54,880
the value that it contains
672
00:26:52,960 --> 00:26:56,799
but to declare a variable we have to
673
00:26:54,880 --> 00:26:59,120
state what type of data that we are
674
00:26:56,799 --> 00:27:01,279
storing within that variable so yeah
675
00:26:59,120 --> 00:27:03,120
those are variables if this video helped
676
00:27:01,279 --> 00:27:04,880
you out help me out by smashing that
677
00:27:03,120 --> 00:27:06,799
like button leave a random comment down
678
00:27:04,880 --> 00:27:09,840
below and subscribe if you'd like to
679
00:27:06,799 --> 00:27:09,840
become a fellow bro
680
00:27:11,120 --> 00:27:14,480
all right what's going on people let's
681
00:27:12,720 --> 00:27:15,919
discuss more about data types we
682
00:27:14,480 --> 00:27:17,120
discussed a few in the last video but
683
00:27:15,919 --> 00:27:19,279
there's a few more that you should be
684
00:27:17,120 --> 00:27:22,000
made aware of so chars they store a
685
00:27:19,279 --> 00:27:24,080
single character and use the percent c
686
00:27:22,000 --> 00:27:25,840
format specifier to display a single
687
00:27:24,080 --> 00:27:28,320
character there's an array of characters
688
00:27:25,840 --> 00:27:30,720
which can store one or more characters
689
00:27:28,320 --> 00:27:32,880
then to display that you use percent s
690
00:27:30,720 --> 00:27:35,840
as the format specifier floats they will
691
00:27:32,880 --> 00:27:38,480
store a decimal number and we use the
692
00:27:35,840 --> 00:27:40,799
percent f format specifier to display a
693
00:27:38,480 --> 00:27:42,960
floating point number and then we have
694
00:27:40,799 --> 00:27:44,960
integers which only store a whole
695
00:27:42,960 --> 00:27:47,440
integer there is no decimal portion and
696
00:27:44,960 --> 00:27:49,440
we use percent d to display an integer
697
00:27:47,440 --> 00:27:51,679
now along with floats we have doubles
698
00:27:49,440 --> 00:27:53,840
doubles have double the precision of a
699
00:27:51,679 --> 00:27:56,240
float we can store even more significant
700
00:27:53,840 --> 00:27:58,399
digits floats use four bytes of memory
701
00:27:56,240 --> 00:28:00,080
they have 32 bits of precision and we
702
00:27:58,399 --> 00:28:02,559
can store between six to seven
703
00:28:00,080 --> 00:28:04,640
significant digits doubles they have
704
00:28:02,559 --> 00:28:07,279
eight bytes of memory double that of
705
00:28:04,640 --> 00:28:09,840
floats and they have 64 bits of
706
00:28:07,279 --> 00:28:12,399
precision and we can store between 15 to
707
00:28:09,840 --> 00:28:14,880
16 significant digits with my float and
708
00:28:12,399 --> 00:28:17,120
my double i'm storing the first several
709
00:28:14,880 --> 00:28:20,159
digits of pi i'm going to attempt to
710
00:28:17,120 --> 00:28:22,320
display as many digits of pi as i can
711
00:28:20,159 --> 00:28:25,600
with a float so i'm going to display
712
00:28:22,320 --> 00:28:29,200
these so to display a float use percent
713
00:28:25,600 --> 00:28:31,520
f and lf for a double which means long
714
00:28:29,200 --> 00:28:33,600
float now by default when i use printf
715
00:28:31,520 --> 00:28:36,080
to display a floating point number or a
716
00:28:33,600 --> 00:28:38,159
double this will only display the first
717
00:28:36,080 --> 00:28:39,760
six to seven digits but we can actually
718
00:28:38,159 --> 00:28:41,600
change that we'll discuss more about
719
00:28:39,760 --> 00:28:43,600
these in the next video on format
720
00:28:41,600 --> 00:28:45,919
specifiers if i would like to display
721
00:28:43,600 --> 00:28:47,840
even more digits after the decimal i
722
00:28:45,919 --> 00:28:49,760
will add zero point and the amount of
723
00:28:47,840 --> 00:28:52,159
digits i would like to display so i
724
00:28:49,760 --> 00:28:54,559
would like to display 15 digits after my
725
00:28:52,159 --> 00:28:57,840
decimal and i'll do that for my double
726
00:28:54,559 --> 00:29:00,399
as well so after the percent signed 0.1
727
00:28:57,840 --> 00:29:03,360
f then add lf and let's take a look at
728
00:29:00,399 --> 00:29:06,159
these numbers okay after my two which is
729
00:29:03,360 --> 00:29:08,480
i believe the sixth digit after the
730
00:29:06,159 --> 00:29:10,640
decimal we actually lose our precision
731
00:29:08,480 --> 00:29:12,480
these numbers are not the same but our
732
00:29:10,640 --> 00:29:15,440
double will actually retain these
733
00:29:12,480 --> 00:29:17,200
numbers so point being a double is even
734
00:29:15,440 --> 00:29:19,200
more accurate than a floating point
735
00:29:17,200 --> 00:29:21,520
number there is more precision but it
736
00:29:19,200 --> 00:29:23,200
uses more memory a double uses eight
737
00:29:21,520 --> 00:29:24,799
bytes of memory because of this reason
738
00:29:23,200 --> 00:29:26,799
we tend to use doubles a lot more than
739
00:29:24,799 --> 00:29:28,880
floats just because they're more precise
740
00:29:26,799 --> 00:29:30,559
we don't want to lose our precision next
741
00:29:28,880 --> 00:29:33,279
up we have booleans to work with
742
00:29:30,559 --> 00:29:38,559
booleans and c include this at the top
743
00:29:33,279 --> 00:29:41,120
std bool.h booleans store true or false
744
00:29:38,559 --> 00:29:43,600
so they work in binary one represents
745
00:29:41,120 --> 00:29:45,679
true and zero represents false so when
746
00:29:43,600 --> 00:29:48,080
you need to declare a boolean variable
747
00:29:45,679 --> 00:29:50,720
you type bool then a variable name and
748
00:29:48,080 --> 00:29:53,120
you set it equal to true or false
749
00:29:50,720 --> 00:29:56,000
technically we only need one bit to
750
00:29:53,120 --> 00:29:58,480
represent true or false one for true and
751
00:29:56,000 --> 00:30:00,720
zero for false but this still uses up
752
00:29:58,480 --> 00:30:03,520
one byte of memory and then to display a
753
00:30:00,720 --> 00:30:05,840
boolean you can use percent d so if i
754
00:30:03,520 --> 00:30:09,440
was to display this boolean variable i
755
00:30:05,840 --> 00:30:12,559
would use percent d so one corresponds
756
00:30:09,440 --> 00:30:14,000
to true and zero corresponds to false
757
00:30:12,559 --> 00:30:15,600
although there are some tricks that we
758
00:30:14,000 --> 00:30:17,440
can do in the future where we could
759
00:30:15,600 --> 00:30:19,600
display the word to true or the word
760
00:30:17,440 --> 00:30:22,240
false but for now we're going to stick
761
00:30:19,600 --> 00:30:24,720
with percent d as the format specifier
762
00:30:22,240 --> 00:30:26,880
so these work in binary one for true
763
00:30:24,720 --> 00:30:29,440
zero for false now another thing that we
764
00:30:26,880 --> 00:30:31,679
can do with chars is that we can store a
765
00:30:29,440 --> 00:30:35,760
whole integer between the range of
766
00:30:31,679 --> 00:30:38,159
negative 128 to positive 127. so in this
767
00:30:35,760 --> 00:30:40,720
example we have char f and i will store
768
00:30:38,159 --> 00:30:43,039
the integer number 100 we can display
769
00:30:40,720 --> 00:30:46,240
this number as either a decimal an
770
00:30:43,039 --> 00:30:48,880
integer or a character so if i was to
771
00:30:46,240 --> 00:30:51,600
display this number as a character we
772
00:30:48,880 --> 00:30:54,399
will use the ascii table to convert this
773
00:30:51,600 --> 00:30:57,519
number to a character representation the
774
00:30:54,399 --> 00:30:59,840
ascii table has a range between 0 to 127
775
00:30:57,519 --> 00:31:01,919
so if i was to display this number as a
776
00:30:59,840 --> 00:31:04,480
decimal using the percent d format
777
00:31:01,919 --> 00:31:07,200
specifier of course this will display as
778
00:31:04,480 --> 00:31:09,840
100 but if i was to convert this to a
779
00:31:07,200 --> 00:31:12,240
character using the percent c format
780
00:31:09,840 --> 00:31:14,720
specifier this has a corresponding
781
00:31:12,240 --> 00:31:16,080
character and that would be lowercase d
782
00:31:14,720 --> 00:31:18,240
so i'm actually going to change this to
783
00:31:16,080 --> 00:31:21,600
something else what about i don't know
784
00:31:18,240 --> 00:31:23,919
uh 120 so let's see what the character
785
00:31:21,600 --> 00:31:25,039
representation of that number is and
786
00:31:23,919 --> 00:31:28,240
that would be
787
00:31:25,039 --> 00:31:30,320
a lowercase x so you can use chars to
788
00:31:28,240 --> 00:31:32,640
store more than single characters you
789
00:31:30,320 --> 00:31:34,880
can also use them to store a whole
790
00:31:32,640 --> 00:31:38,799
integer however the range is between
791
00:31:34,880 --> 00:31:41,120
negative 128 to positive 127 because
792
00:31:38,799 --> 00:31:43,760
they have one byte of memory now there
793
00:31:41,120 --> 00:31:46,399
is a keyword unsigned so when you
794
00:31:43,760 --> 00:31:49,279
declare a variable that is unsigned we
795
00:31:46,399 --> 00:31:51,600
disregard any negative numbers so
796
00:31:49,279 --> 00:31:54,159
effectively this doubles our range with
797
00:31:51,600 --> 00:31:56,799
our positive numbers so if we have
798
00:31:54,159 --> 00:32:00,799
unsigned char we can store a number
799
00:31:56,799 --> 00:32:02,559
between 0 to positive 255 because we
800
00:32:00,799 --> 00:32:05,360
know we're not going to store a negative
801
00:32:02,559 --> 00:32:07,519
number so then if you need to display an
802
00:32:05,360 --> 00:32:10,799
unsigned character we can use just
803
00:32:07,519 --> 00:32:13,200
percent d i'm going to store 255 within
804
00:32:10,799 --> 00:32:16,159
my unsigned chart and that would be of
805
00:32:13,200 --> 00:32:18,799
course 255. however if we go beyond this
806
00:32:16,159 --> 00:32:19,760
range this will overflow and go back to
807
00:32:18,799 --> 00:32:22,080
zero
808
00:32:19,760 --> 00:32:25,440
so if i was to display this we have a
809
00:32:22,080 --> 00:32:28,000
warning unsigned conversion from int to
810
00:32:25,440 --> 00:32:30,559
unsigned chart so then this resets back
811
00:32:28,000 --> 00:32:32,640
to zero so if you go beyond the maximum
812
00:32:30,559 --> 00:32:34,799
range this will reset all the way back
813
00:32:32,640 --> 00:32:36,799
to zero whatever the beginning is so if
814
00:32:34,799 --> 00:32:39,919
you add this keyword unsigned you can
815
00:32:36,799 --> 00:32:41,679
effectively double the range of positive
816
00:32:39,919 --> 00:32:44,480
numbers that you can store within a
817
00:32:41,679 --> 00:32:46,880
variable by default most data types are
818
00:32:44,480 --> 00:32:49,679
already signed but we don't need to
819
00:32:46,880 --> 00:32:51,600
explicitly type that so point being with
820
00:32:49,679 --> 00:32:54,240
chars you can store more than a single
821
00:32:51,600 --> 00:32:57,919
character you can store a whole integer
822
00:32:54,240 --> 00:33:01,200
between ranges negative 128 to positive
823
00:32:57,919 --> 00:33:04,480
127 if it's signed if it's unsigned you
824
00:33:01,200 --> 00:33:06,640
can store numbers between 0 to 255. you
825
00:33:04,480 --> 00:33:09,279
can display them as an integer by using
826
00:33:06,640 --> 00:33:11,440
the percent d format specifier or you
827
00:33:09,279 --> 00:33:14,399
could convert them to a character using
828
00:33:11,440 --> 00:33:17,679
the ascii table by using the percent c
829
00:33:14,399 --> 00:33:20,799
format specifier next we have short
830
00:33:17,679 --> 00:33:23,279
hints short ins use two bytes of memory
831
00:33:20,799 --> 00:33:24,519
they can store a number between negative
832
00:33:23,279 --> 00:33:28,200
32
833
00:33:24,519 --> 00:33:30,720
768 to positive 32
834
00:33:28,200 --> 00:33:33,039
767 because while they use two bytes of
835
00:33:30,720 --> 00:33:36,080
memory they can only store a number so
836
00:33:33,039 --> 00:33:41,679
large and if it's an unsigned short int
837
00:33:36,080 --> 00:33:43,760
the range is instead between 0 to 65 535
838
00:33:41,679 --> 00:33:46,080
and we use the percent d format
839
00:33:43,760 --> 00:33:48,159
specifier to display a short in so
840
00:33:46,080 --> 00:33:50,000
within my printf statement i'm going to
841
00:33:48,159 --> 00:33:53,679
display these two numbers so i will
842
00:33:50,000 --> 00:33:56,320
display variable h and i h is a short
843
00:33:53,679 --> 00:33:57,279
integer and i is an unsigned short
844
00:33:56,320 --> 00:33:59,919
integer
845
00:33:57,279 --> 00:34:02,080
so these are the maximum values for a
846
00:33:59,919 --> 00:34:04,640
short integer and an unsigned short
847
00:34:02,080 --> 00:34:06,799
integer and like i discussed with chars
848
00:34:04,640 --> 00:34:09,200
if we go beyond this range we will
849
00:34:06,799 --> 00:34:14,040
encounter an overflow so i'm going to
850
00:34:09,200 --> 00:34:14,040
change this short end to 32768
851
00:34:14,240 --> 00:34:19,839
and let's see what number displays
852
00:34:17,119 --> 00:34:22,240
so this will overflow and reset this
853
00:34:19,839 --> 00:34:25,399
value back to the minimum value which in
854
00:34:22,240 --> 00:34:27,760
this case is negative 32
855
00:34:25,399 --> 00:34:30,320
768 and if you do the same thing with
856
00:34:27,760 --> 00:34:32,800
the unsigned short integer that would be
857
00:34:30,320 --> 00:34:35,919
zero because that's the minimum value
858
00:34:32,800 --> 00:34:38,000
for an unsigned short integer so those
859
00:34:35,919 --> 00:34:39,520
are short integers they use two bytes of
860
00:34:38,000 --> 00:34:41,760
memory and they can store numbers
861
00:34:39,520 --> 00:34:44,320
between these ranges depending if it's
862
00:34:41,760 --> 00:34:46,560
signed or unsigned oh and another way of
863
00:34:44,320 --> 00:34:48,800
writing these you don't necessarily need
864
00:34:46,560 --> 00:34:50,320
to declare these with the word and you
865
00:34:48,800 --> 00:34:52,480
could just say short and that would do
866
00:34:50,320 --> 00:34:55,119
the same thing people usually just call
867
00:34:52,480 --> 00:34:56,879
them shorts instead of short ants so
868
00:34:55,119 --> 00:34:58,480
those are what shorts are now with
869
00:34:56,879 --> 00:35:00,480
integers we kind of discussed this in
870
00:34:58,480 --> 00:35:03,760
the last video just briefly integers
871
00:35:00,480 --> 00:35:06,880
store a whole number between just under
872
00:35:03,760 --> 00:35:09,280
negative 2 billion to just over positive
873
00:35:06,880 --> 00:35:12,000
2 billion because they use 4 bytes of
874
00:35:09,280 --> 00:35:15,119
memory and we use the percent d format
875
00:35:12,000 --> 00:35:17,680
specifier to display a signed integer if
876
00:35:15,119 --> 00:35:21,599
that integer is unsigned the range
877
00:35:17,680 --> 00:35:24,160
changes from 0 to just over positive 4
878
00:35:21,599 --> 00:35:27,280
billion however there is a different
879
00:35:24,160 --> 00:35:30,400
format specifier to display an unsigned
880
00:35:27,280 --> 00:35:32,960
integer you instead use percent u so
881
00:35:30,400 --> 00:35:36,400
then let's display these percent d for a
882
00:35:32,960 --> 00:35:38,560
signed integer and percent u for an
883
00:35:36,400 --> 00:35:41,119
unsigned integer
884
00:35:38,560 --> 00:35:45,200
and these are the maximum numbers and
885
00:35:41,119 --> 00:35:47,440
then if i was to exceed the range
886
00:35:45,200 --> 00:35:49,200
this again would cause an overflow and
887
00:35:47,440 --> 00:35:52,480
reset these numbers back to their
888
00:35:49,200 --> 00:35:54,880
minimum values so those are standard
889
00:35:52,480 --> 00:35:57,280
integers they use four bytes of memory
890
00:35:54,880 --> 00:35:59,359
so they can store numbers between these
891
00:35:57,280 --> 00:36:01,440
ranges depending if they're signed or
892
00:35:59,359 --> 00:36:03,280
unsigned all right the last data type
893
00:36:01,440 --> 00:36:06,160
we're going to talk about for this topic
894
00:36:03,280 --> 00:36:08,480
is a long long integer now the reason
895
00:36:06,160 --> 00:36:10,560
that we have long twice is that with
896
00:36:08,480 --> 00:36:12,800
standard integers these are already
897
00:36:10,560 --> 00:36:15,040
considered longs but we don't need to
898
00:36:12,800 --> 00:36:18,000
explicitly type long for standard
899
00:36:15,040 --> 00:36:21,520
integers so to represent a really large
900
00:36:18,000 --> 00:36:24,240
number we can use a long long integer
901
00:36:21,520 --> 00:36:27,200
and these use eight bytes of memory the
902
00:36:24,240 --> 00:36:29,760
effective range for a signed long long
903
00:36:27,200 --> 00:36:32,320
integer is just underneath nine
904
00:36:29,760 --> 00:36:35,200
quintillion to just over nine
905
00:36:32,320 --> 00:36:38,320
quintillion and the format specifier for
906
00:36:35,200 --> 00:36:40,880
a long long integer one that is signed
907
00:36:38,320 --> 00:36:43,440
is percent lld
908
00:36:40,880 --> 00:36:46,800
now if it's unsigned that changes the
909
00:36:43,440 --> 00:36:49,920
range between zero to just over positive
910
00:36:46,800 --> 00:36:52,880
18 quintillion and the format specifier
911
00:36:49,920 --> 00:36:53,680
is percent llu then let's display these
912
00:36:52,880 --> 00:36:56,560
so
913
00:36:53,680 --> 00:37:00,160
for a signed long long integer that is
914
00:36:56,560 --> 00:37:02,800
lld and if it's unsigned that is llu now
915
00:37:00,160 --> 00:37:05,599
we'll encounter a warning
916
00:37:02,800 --> 00:37:07,520
so this warning applies to our unsigned
917
00:37:05,599 --> 00:37:10,160
long long integer
918
00:37:07,520 --> 00:37:12,800
integer constant is so large that it is
919
00:37:10,160 --> 00:37:15,359
unsigned so one way in which we can
920
00:37:12,800 --> 00:37:18,240
prevent that warning is after our number
921
00:37:15,359 --> 00:37:20,560
within our unsigned long long integer
922
00:37:18,240 --> 00:37:22,400
add a u to the end of this so then we
923
00:37:20,560 --> 00:37:25,440
can display this number with no warning
924
00:37:22,400 --> 00:37:28,160
so since long long integers use so many
925
00:37:25,440 --> 00:37:30,320
bytes they can store a gigantic number
926
00:37:28,160 --> 00:37:32,560
we tend to not use long long integers
927
00:37:30,320 --> 00:37:34,640
very often because well we don't really
928
00:37:32,560 --> 00:37:36,720
have a need for this large of a number
929
00:37:34,640 --> 00:37:38,560
but in certain circumstances you might
930
00:37:36,720 --> 00:37:40,960
perhaps you're dealing with the speed of
931
00:37:38,560 --> 00:37:43,280
light or something you may need to use a
932
00:37:40,960 --> 00:37:45,599
long long integer but commonly we use
933
00:37:43,280 --> 00:37:47,760
standard integers a lot more well yeah
934
00:37:45,599 --> 00:37:49,920
everybody those are even more c data
935
00:37:47,760 --> 00:37:51,599
types we likely won't be using most of
936
00:37:49,920 --> 00:37:53,839
these but you should still be made aware
937
00:37:51,599 --> 00:37:56,640
of their existence i would say that
938
00:37:53,839 --> 00:37:57,599
we're going to focus on chars array of
939
00:37:56,640 --> 00:37:59,920
chars
940
00:37:57,599 --> 00:38:02,400
doubles booleans
941
00:37:59,920 --> 00:38:04,320
and integers so pay attention to those
942
00:38:02,400 --> 00:38:06,480
ones but you should still be made aware
943
00:38:04,320 --> 00:38:08,320
of the existence of other data types
944
00:38:06,480 --> 00:38:10,160
just in case you encounter them if you
945
00:38:08,320 --> 00:38:11,839
found this video helpful please smash
946
00:38:10,160 --> 00:38:13,760
that like button leave a random comment
947
00:38:11,839 --> 00:38:17,560
down below and subscribe if you'd like
948
00:38:13,760 --> 00:38:17,560
to become a fellow bro
949
00:38:18,400 --> 00:38:21,760
all right welcome back everybody in this
950
00:38:20,400 --> 00:38:23,599
video i'm going to show you while a few
951
00:38:21,760 --> 00:38:26,160
extra things that we can do with format
952
00:38:23,599 --> 00:38:28,880
specifiers using a format specifier
953
00:38:26,160 --> 00:38:30,800
within a printf statement we can define
954
00:38:28,880 --> 00:38:33,200
and format a type of data to be
955
00:38:30,800 --> 00:38:35,280
displayed to use a format specifier you
956
00:38:33,200 --> 00:38:37,200
use a percent sign then follow that
957
00:38:35,280 --> 00:38:39,040
percent sign with a certain character
958
00:38:37,200 --> 00:38:40,720
depending on the type of data you would
959
00:38:39,040 --> 00:38:42,800
like to display let's say that we have
960
00:38:40,720 --> 00:38:44,960
three variables maybe we have an online
961
00:38:42,800 --> 00:38:47,040
store or something like that so let's
962
00:38:44,960 --> 00:38:49,200
say that these are of the float data
963
00:38:47,040 --> 00:38:51,440
type and we'll have three items for sale
964
00:38:49,200 --> 00:38:53,839
item one and make up some price let's
965
00:38:51,440 --> 00:38:55,760
say that the first item is five dollars
966
00:38:53,839 --> 00:38:58,320
and seventy five cents
967
00:38:55,760 --> 00:39:00,560
and we have item two
968
00:38:58,320 --> 00:39:03,440
and this will be ten dollars even and
969
00:39:00,560 --> 00:39:04,880
then we have item three and this will be
970
00:39:03,440 --> 00:39:07,680
one hundred dollars and ninety nine
971
00:39:04,880 --> 00:39:09,920
cents so let's display item one item two
972
00:39:07,680 --> 00:39:12,800
in item three with a print f statement
973
00:39:09,920 --> 00:39:17,040
print f and within a string let's say
974
00:39:12,800 --> 00:39:19,200
item one colon space then i'll add a
975
00:39:17,040 --> 00:39:22,000
dollar sign wherever you would like to
976
00:39:19,200 --> 00:39:24,320
insert a value or variable you will use
977
00:39:22,000 --> 00:39:27,040
that format specifier the percent sign
978
00:39:24,320 --> 00:39:29,599
so after my dollar sign i'll add item
979
00:39:27,040 --> 00:39:31,280
one so that would be percent then for
980
00:39:29,599 --> 00:39:33,280
floats you need f and then after the
981
00:39:31,280 --> 00:39:35,280
string add comma
982
00:39:33,280 --> 00:39:37,119
item one then let's just make sure that
983
00:39:35,280 --> 00:39:38,880
this works so we're going to be
984
00:39:37,119 --> 00:39:40,960
formatting this output what if we don't
985
00:39:38,880 --> 00:39:43,839
want all the zeros we can set the
986
00:39:40,960 --> 00:39:46,560
decimal precision of a float or double
987
00:39:43,839 --> 00:39:48,000
after the format specifier add a dot and
988
00:39:46,560 --> 00:39:50,000
the amount of digits you would like to
989
00:39:48,000 --> 00:39:52,640
display if i would only like to display
990
00:39:50,000 --> 00:39:55,760
two digits after the percent i will add
991
00:39:52,640 --> 00:39:57,760
point two and this will only display two
992
00:39:55,760 --> 00:40:00,000
digits for the amount of cents that we
993
00:39:57,760 --> 00:40:03,119
owe okay now let's do the same thing for
994
00:40:00,000 --> 00:40:04,240
items two and three so item two
995
00:40:03,119 --> 00:40:05,599
item two
996
00:40:04,240 --> 00:40:07,040
item three
997
00:40:05,599 --> 00:40:08,800
item three
998
00:40:07,040 --> 00:40:12,000
okay then let's add a new line character
999
00:40:08,800 --> 00:40:15,200
because i forgot so new line
1000
00:40:12,000 --> 00:40:17,040
new line and new line
1001
00:40:15,200 --> 00:40:19,280
much better another thing that we can do
1002
00:40:17,040 --> 00:40:22,079
with format specifiers is that we can
1003
00:40:19,280 --> 00:40:24,240
set a minimum field width so just add a
1004
00:40:22,079 --> 00:40:26,240
number after the percent sign let's say
1005
00:40:24,240 --> 00:40:28,160
that i would like to allocate eight
1006
00:40:26,240 --> 00:40:29,119
spaces worth of room to display my
1007
00:40:28,160 --> 00:40:31,520
number
1008
00:40:29,119 --> 00:40:33,520
so after the percent sign but before the
1009
00:40:31,520 --> 00:40:36,079
decimal add how many spaces you would
1010
00:40:33,520 --> 00:40:37,680
like to set for the minimum field width
1011
00:40:36,079 --> 00:40:39,680
let's say eight
1012
00:40:37,680 --> 00:40:41,760
and let's see the new output okay there
1013
00:40:39,680 --> 00:40:43,440
we go however it's all right aligned if
1014
00:40:41,760 --> 00:40:45,839
you would like this left aligned you
1015
00:40:43,440 --> 00:40:47,760
would use a negative sign after the
1016
00:40:45,839 --> 00:40:49,040
percent to left align all this this
1017
00:40:47,760 --> 00:40:50,720
would be
1018
00:40:49,040 --> 00:40:53,599
negative whatever number you would like
1019
00:40:50,720 --> 00:40:55,440
to allocate so the number is left
1020
00:40:53,599 --> 00:40:57,760
justified but we still have all of this
1021
00:40:55,440 --> 00:41:00,560
room after so that's what a format
1022
00:40:57,760 --> 00:41:03,200
specifier is it defines and formats a
1023
00:41:00,560 --> 00:41:05,839
type of data to be displayed so place a
1024
00:41:03,200 --> 00:41:07,680
format specifier within a string use a
1025
00:41:05,839 --> 00:41:09,440
certain character for the type of dad
1026
00:41:07,680 --> 00:41:12,160
you would like to display you can set
1027
00:41:09,440 --> 00:41:14,240
decimal precision for floats and doubles
1028
00:41:12,160 --> 00:41:16,079
you can set a minimum field width to
1029
00:41:14,240 --> 00:41:18,160
display your output if you would like to
1030
00:41:16,079 --> 00:41:20,480
line things up like you can see here and
1031
00:41:18,160 --> 00:41:23,040
you can left or right align your output
1032
00:41:20,480 --> 00:41:24,560
so yeah those are format specifiers if
1033
00:41:23,040 --> 00:41:26,640
you found this video helpful please be
1034
00:41:24,560 --> 00:41:28,720
sure to smash that like button leave a
1035
00:41:26,640 --> 00:41:32,319
random comment down below and subscribe
1036
00:41:28,720 --> 00:41:32,319
if you'd like to become a fellow bro
1037
00:41:33,680 --> 00:41:38,480
hey everyone so i'm going to attempt to
1038
00:41:36,240 --> 00:41:41,440
explain constants in about a minute a
1039
00:41:38,480 --> 00:41:43,359
constant is a fixed value that cannot be
1040
00:41:41,440 --> 00:41:46,319
altered by the program during its
1041
00:41:43,359 --> 00:41:48,240
execution i have a variable pi pi equals
1042
00:41:46,319 --> 00:41:50,240
3.1415
1043
00:41:48,240 --> 00:41:52,240
but what if we accidentally or somebody
1044
00:41:50,240 --> 00:41:56,560
else changes the value of pi it's an
1045
00:41:52,240 --> 00:41:58,720
important number pi now equals 420 69.
1046
00:41:56,560 --> 00:42:01,119
since this value was changed this will
1047
00:41:58,720 --> 00:42:03,680
alter the result of our program to
1048
00:42:01,119 --> 00:42:06,240
prevent a variable or value from being
1049
00:42:03,680 --> 00:42:09,280
changed we can turn that variable into a
1050
00:42:06,240 --> 00:42:11,839
constant by preceding the data type with
1051
00:42:09,280 --> 00:42:13,680
this keyword const and a common naming
1052
00:42:11,839 --> 00:42:15,599
convention with constants is that you
1053
00:42:13,680 --> 00:42:17,680
make all the letters uppercase although
1054
00:42:15,599 --> 00:42:20,240
it's not necessary but it's considered
1055
00:42:17,680 --> 00:42:22,640
good practice so if i attempt to take
1056
00:42:20,240 --> 00:42:25,040
our constant and assign this a different
1057
00:42:22,640 --> 00:42:27,839
value we'll run into an error error
1058
00:42:25,040 --> 00:42:30,480
assignment of read-only variable pi that
1059
00:42:27,839 --> 00:42:32,480
is what a constant is it's a fixed value
1060
00:42:30,480 --> 00:42:34,400
that cannot be altered by the program
1061
00:42:32,480 --> 00:42:36,000
during its execution and it provides a
1062
00:42:34,400 --> 00:42:37,760
little bit of security so if you found
1063
00:42:36,000 --> 00:42:39,520
this video helpful please be sure to
1064
00:42:37,760 --> 00:42:41,359
smash that like button leave a random
1065
00:42:39,520 --> 00:42:45,480
comment down below and subscribe if
1066
00:42:41,359 --> 00:42:45,480
you'd like to become a fellow bro
1067
00:42:45,760 --> 00:42:50,160
well well well welcome back so we need
1068
00:42:48,240 --> 00:42:52,000
to talk about arithmetic operators
1069
00:42:50,160 --> 00:42:53,760
because well this wouldn't be a full
1070
00:42:52,000 --> 00:42:56,319
course without them so as you probably
1071
00:42:53,760 --> 00:42:58,800
know arithmetic operators are well
1072
00:42:56,319 --> 00:43:00,880
addition subtraction multiplication
1073
00:42:58,800 --> 00:43:03,280
division and there's a few others too
1074
00:43:00,880 --> 00:43:05,280
unique with programming such as modulus
1075
00:43:03,280 --> 00:43:08,319
increment and decrement so let's go over
1076
00:43:05,280 --> 00:43:10,160
a few let's say that we have int z and
1077
00:43:08,319 --> 00:43:12,640
if we need to add two numbers we could
1078
00:43:10,160 --> 00:43:15,200
say x plus y
1079
00:43:12,640 --> 00:43:18,079
equals z and then display the sum which
1080
00:43:15,200 --> 00:43:21,200
of course is seven so subtraction is
1081
00:43:18,079 --> 00:43:24,079
well minus five minus two is three
1082
00:43:21,200 --> 00:43:25,119
multiplication is an asterisk five times
1083
00:43:24,079 --> 00:43:27,599
two
1084
00:43:25,119 --> 00:43:30,640
equals 10. okay now pay attention to
1085
00:43:27,599 --> 00:43:33,040
this with division so 5 divided by 2
1086
00:43:30,640 --> 00:43:35,119
equals 2.5 right well that's where
1087
00:43:33,040 --> 00:43:37,359
you're wrong it's 2 because we're
1088
00:43:35,119 --> 00:43:39,280
storing the result within an integer and
1089
00:43:37,359 --> 00:43:42,160
with integers we can only store whole
1090
00:43:39,280 --> 00:43:44,160
numbers so we will lose that decimal
1091
00:43:42,160 --> 00:43:46,079
portion it will be truncated there's a
1092
00:43:44,160 --> 00:43:48,319
few things we'll need to change first
1093
00:43:46,079 --> 00:43:50,800
we'll need to store the result within a
1094
00:43:48,319 --> 00:43:53,440
float or a double and then let's display
1095
00:43:50,800 --> 00:43:54,960
this percent f for a float okay another
1096
00:43:53,440 --> 00:43:57,440
thing that we need to change too and
1097
00:43:54,960 --> 00:44:00,000
that involves integer division if we're
1098
00:43:57,440 --> 00:44:02,079
dividing by an integer we'll truncate
1099
00:44:00,000 --> 00:44:04,000
that decimal portion there's one of two
1100
00:44:02,079 --> 00:44:06,240
things we can do we can either change
1101
00:44:04,000 --> 00:44:08,960
our divisor to a float
1102
00:44:06,240 --> 00:44:12,160
or a double and that will solve that
1103
00:44:08,960 --> 00:44:14,640
problem 2.5 or if we would like to keep
1104
00:44:12,160 --> 00:44:16,800
this as an integer we can convert this
1105
00:44:14,640 --> 00:44:19,520
integer to a float or double by
1106
00:44:16,800 --> 00:44:20,480
preceding the divisor with
1107
00:44:19,520 --> 00:44:23,359
float
1108
00:44:20,480 --> 00:44:25,440
or double either one
1109
00:44:23,359 --> 00:44:28,640
so if i would like to keep y as an
1110
00:44:25,440 --> 00:44:32,560
integer and we divide x by y we will
1111
00:44:28,640 --> 00:44:35,280
convert 2 into a float so 2.0 and store
1112
00:44:32,560 --> 00:44:38,560
the result within float z and the result
1113
00:44:35,280 --> 00:44:40,240
is 2.5 so if you're performing division
1114
00:44:38,560 --> 00:44:42,560
with any integers you need to pay
1115
00:44:40,240 --> 00:44:45,760
attention to integer division and you
1116
00:44:42,560 --> 00:44:48,400
may need to cast your divisor as a float
1117
00:44:45,760 --> 00:44:50,400
or as a double now we have modulus
1118
00:44:48,400 --> 00:44:54,079
modulus gives you the remainder of any
1119
00:44:50,400 --> 00:44:57,280
division 5 does not divide by 2 evenly
1120
00:44:54,079 --> 00:44:59,280
int z equals x
1121
00:44:57,280 --> 00:45:01,520
modulus y
1122
00:44:59,280 --> 00:45:03,040
and if we display z
1123
00:45:01,520 --> 00:45:05,839
so make sure to change your format
1124
00:45:03,040 --> 00:45:08,480
specifier if you didn't the remainder of
1125
00:45:05,839 --> 00:45:11,280
five divided by two is one
1126
00:45:08,480 --> 00:45:13,280
if this was four four divides by two
1127
00:45:11,280 --> 00:45:15,520
evenly and the result is going to be
1128
00:45:13,280 --> 00:45:17,119
zero modulus gives you the remainder of
1129
00:45:15,520 --> 00:45:19,839
any division it's actually pretty
1130
00:45:17,119 --> 00:45:22,960
helpful to find if a number is even odd
1131
00:45:19,839 --> 00:45:24,800
all you do is say modulus 2 or some
1132
00:45:22,960 --> 00:45:27,119
variable containing the value of 2
1133
00:45:24,800 --> 00:45:30,000
either way and then we can increment or
1134
00:45:27,119 --> 00:45:32,079
decrement a number by 1. if i need to
1135
00:45:30,000 --> 00:45:34,480
increment x for some reason you'll see
1136
00:45:32,079 --> 00:45:35,760
this when we get to the video on loops i
1137
00:45:34,480 --> 00:45:37,040
could say x
1138
00:45:35,760 --> 00:45:39,760
plus plus
1139
00:45:37,040 --> 00:45:42,160
and then let's display x
1140
00:45:39,760 --> 00:45:43,760
x incremented by one
1141
00:45:42,160 --> 00:45:47,520
would be six
1142
00:45:43,760 --> 00:45:51,839
and then let's decrement y y minus minus
1143
00:45:47,520 --> 00:45:53,440
and y decremented by one equals one so
1144
00:45:51,839 --> 00:45:55,440
yeah everybody those are a few
1145
00:45:53,440 --> 00:45:57,200
arithmetic operators a lot of these are
1146
00:45:55,440 --> 00:45:59,520
fairly simple you know addition
1147
00:45:57,200 --> 00:46:01,760
subtraction multiplication and division
1148
00:45:59,520 --> 00:46:03,599
do pay attention to integer division
1149
00:46:01,760 --> 00:46:06,160
because that can throw off your program
1150
00:46:03,599 --> 00:46:07,760
then there's also modulus increment and
1151
00:46:06,160 --> 00:46:09,680
decrement if you found this video
1152
00:46:07,760 --> 00:46:11,359
helpful please remember to smash that
1153
00:46:09,680 --> 00:46:13,200
like button leave a random comment down
1154
00:46:11,359 --> 00:46:16,240
below and subscribe if you'd like to
1155
00:46:13,200 --> 00:46:16,240
become a fellow bro
1156
00:46:17,359 --> 00:46:21,680
hey welcome back everybody here's a
1157
00:46:19,680 --> 00:46:23,520
super quick video on augmented
1158
00:46:21,680 --> 00:46:25,760
assignment operators they're used to
1159
00:46:23,520 --> 00:46:28,000
replace a statement where an operator
1160
00:46:25,760 --> 00:46:29,760
takes a variable as one of its arguments
1161
00:46:28,000 --> 00:46:31,359
and then assigns the result back to the
1162
00:46:29,760 --> 00:46:33,760
same variable now what the heck does
1163
00:46:31,359 --> 00:46:36,480
that mean okay so let's say that we need
1164
00:46:33,760 --> 00:46:38,240
to increment the value of x by one
1165
00:46:36,480 --> 00:46:41,680
without using an increment operator we
1166
00:46:38,240 --> 00:46:43,839
could say x equals x plus one so writing
1167
00:46:41,680 --> 00:46:46,000
this out can be somewhat redundant there
1168
00:46:43,839 --> 00:46:47,839
is a shortcut by using an augmented
1169
00:46:46,000 --> 00:46:49,440
assignment operator whatever variable
1170
00:46:47,839 --> 00:46:51,760
you would like to perform an operation
1171
00:46:49,440 --> 00:46:54,400
on you will list that variable use an
1172
00:46:51,760 --> 00:46:56,960
arithmetic operator equals and then some
1173
00:46:54,400 --> 00:46:59,280
value so writing this would increment x
1174
00:46:56,960 --> 00:47:02,079
by one so let's go over a few examples
1175
00:46:59,280 --> 00:47:05,280
we have into x equals ten so i could
1176
00:47:02,079 --> 00:47:07,520
write x equals x plus two to increment x
1177
00:47:05,280 --> 00:47:10,800
by two right or i could say as a
1178
00:47:07,520 --> 00:47:12,800
shortcut x plus equals two and that will
1179
00:47:10,800 --> 00:47:14,240
do the same thing and that's twelve now
1180
00:47:12,800 --> 00:47:17,359
let's try minus
1181
00:47:14,240 --> 00:47:19,359
x equals x minus three using the
1182
00:47:17,359 --> 00:47:23,119
augmented assignment operator that would
1183
00:47:19,359 --> 00:47:26,000
be x minus equals three which is seven
1184
00:47:23,119 --> 00:47:30,000
what about x equals x times four well
1185
00:47:26,000 --> 00:47:31,440
that would be x times equals four x
1186
00:47:30,000 --> 00:47:33,599
times four
1187
00:47:31,440 --> 00:47:36,480
is forty and division
1188
00:47:33,599 --> 00:47:38,559
x equals x divided by five
1189
00:47:36,480 --> 00:47:42,319
the augmented assignment operator of
1190
00:47:38,559 --> 00:47:43,200
this equation is x divided by equals
1191
00:47:42,319 --> 00:47:46,559
five
1192
00:47:43,200 --> 00:47:50,319
which is two and lastly modulus so what
1193
00:47:46,559 --> 00:47:53,760
about x equals x modulus two
1194
00:47:50,319 --> 00:47:57,200
that would be x modulus equals two
1195
00:47:53,760 --> 00:47:59,280
x modulus two equals zero well yeah
1196
00:47:57,200 --> 00:48:01,599
everybody those are augmented assignment
1197
00:47:59,280 --> 00:48:03,280
operators they're basically a shortcut
1198
00:48:01,599 --> 00:48:05,280
they're used to replace a statement
1199
00:48:03,280 --> 00:48:07,359
where an operator takes a variable as
1200
00:48:05,280 --> 00:48:09,359
one of its arguments and then assigns
1201
00:48:07,359 --> 00:48:11,440
the result back to the same variable
1202
00:48:09,359 --> 00:48:13,359
it's a shortcut if this video helped you
1203
00:48:11,440 --> 00:48:15,040
out you can help me out by smashing that
1204
00:48:13,359 --> 00:48:16,880
like button leave a random comment down
1205
00:48:15,040 --> 00:48:20,599
below and subscribe if you'd like to
1206
00:48:16,880 --> 00:48:20,599
become a fellow bro
1207
00:48:20,960 --> 00:48:24,960
hey uh welcome back in this video i'm
1208
00:48:23,359 --> 00:48:27,680
going to show you all how we can accept
1209
00:48:24,960 --> 00:48:29,680
user input in c now if you're using vs
1210
00:48:27,680 --> 00:48:32,319
code we need to switch from using our
1211
00:48:29,680 --> 00:48:34,000
output tab to terminal and one way in
1212
00:48:32,319 --> 00:48:37,520
which we can make that change is by
1213
00:48:34,000 --> 00:48:39,599
going to file preferences settings
1214
00:48:37,520 --> 00:48:41,839
search for code runner
1215
00:48:39,599 --> 00:48:43,599
and then check this run in terminal so
1216
00:48:41,839 --> 00:48:46,079
then when we run our code it will now
1217
00:48:43,599 --> 00:48:48,559
display in terminal which accepts user
1218
00:48:46,079 --> 00:48:50,480
input output doesn't because well it's
1219
00:48:48,559 --> 00:48:52,400
only for output and you can ignore this
1220
00:48:50,480 --> 00:48:54,640
this is a powershell command that will
1221
00:48:52,400 --> 00:48:56,319
compile and run your c program but if
1222
00:48:54,640 --> 00:48:58,319
you prefer instead of powershell you can
1223
00:48:56,319 --> 00:49:00,720
use command prompt but you would need to
1224
00:48:58,319 --> 00:49:03,599
manually compile and run your code since
1225
00:49:00,720 --> 00:49:05,839
i'm using windows that would be a gcc
1226
00:49:03,599 --> 00:49:08,079
the name of your c program hello world
1227
00:49:05,839 --> 00:49:10,880
dot c that will compile your program
1228
00:49:08,079 --> 00:49:12,720
into an executable named a dot exe
1229
00:49:10,880 --> 00:49:14,559
then just type that to run it but i'll
1230
00:49:12,720 --> 00:49:16,800
stick with using powershell just to kind
1231
00:49:14,559 --> 00:49:18,960
of simplify things okay now how can we
1232
00:49:16,800 --> 00:49:21,599
accept user input let's declare a
1233
00:49:18,960 --> 00:49:23,280
variable age but not yet assign it next
1234
00:49:21,599 --> 00:49:27,119
we're going to create a prompt to ask
1235
00:49:23,280 --> 00:49:29,200
the user for their age and we'll ask how
1236
00:49:27,119 --> 00:49:32,000
old are you
1237
00:49:29,200 --> 00:49:34,319
to accept user input we can use the scan
1238
00:49:32,000 --> 00:49:36,880
f function it's kind of the inverse of
1239
00:49:34,319 --> 00:49:39,280
printf printf is used to display
1240
00:49:36,880 --> 00:49:41,200
something as output scanf is used to
1241
00:49:39,280 --> 00:49:44,000
read input what we're going to place
1242
00:49:41,200 --> 00:49:46,400
within our scanf function is the format
1243
00:49:44,000 --> 00:49:49,359
specifier of the variable we will insert
1244
00:49:46,400 --> 00:49:51,839
a value into if we need to accept a
1245
00:49:49,359 --> 00:49:54,480
number an integer as input the
1246
00:49:51,839 --> 00:49:56,720
corresponding format specifier is
1247
00:49:54,480 --> 00:49:58,400
percent d
1248
00:49:56,720 --> 00:50:01,200
then add a comma the name of the
1249
00:49:58,400 --> 00:50:04,240
variable but precede the variable name
1250
00:50:01,200 --> 00:50:06,400
with an ampersand that is the address of
1251
00:50:04,240 --> 00:50:08,800
operator let's print a message that
1252
00:50:06,400 --> 00:50:11,119
contains the value stored within our
1253
00:50:08,800 --> 00:50:12,480
variable edge u
1254
00:50:11,119 --> 00:50:14,240
are
1255
00:50:12,480 --> 00:50:16,880
percent d
1256
00:50:14,240 --> 00:50:19,280
years old so this is our format
1257
00:50:16,880 --> 00:50:21,280
specifier it functions as a placeholder
1258
00:50:19,280 --> 00:50:23,520
and i would like to display the value
1259
00:50:21,280 --> 00:50:24,640
contained within my edge variable so
1260
00:50:23,520 --> 00:50:26,079
things are getting a little bit
1261
00:50:24,640 --> 00:50:28,480
difficult to read i'm just going to
1262
00:50:26,079 --> 00:50:30,960
precede this with a new line
1263
00:50:28,480 --> 00:50:33,440
okay let's try that again how old are
1264
00:50:30,960 --> 00:50:35,920
you let's say that i'm 21 not anymore
1265
00:50:33,440 --> 00:50:39,040
but let's pretend that i still am
1266
00:50:35,920 --> 00:50:41,599
you are 21 years old that's basically
1267
00:50:39,040 --> 00:50:44,800
how to accept user input you use the
1268
00:50:41,599 --> 00:50:46,960
scanf function use the format specifier
1269
00:50:44,800 --> 00:50:49,760
of the variable list the variable
1270
00:50:46,960 --> 00:50:52,079
appreciated with the address of operator
1271
00:50:49,760 --> 00:50:54,480
let's try this again but instead accept
1272
00:50:52,079 --> 00:50:56,400
a string from a user like a name we need
1273
00:50:54,480 --> 00:50:58,960
to declare an array of characters and
1274
00:50:56,400 --> 00:51:01,839
set asides so let's create a character
1275
00:50:58,960 --> 00:51:03,760
array named name
1276
00:51:01,839 --> 00:51:05,680
now with character arrays we can't
1277
00:51:03,760 --> 00:51:07,599
change the size of the array after the
1278
00:51:05,680 --> 00:51:10,160
program is already running let's set a
1279
00:51:07,599 --> 00:51:12,559
max size of maybe 25 bytes for this
1280
00:51:10,160 --> 00:51:15,359
array if we go over this limit this will
1281
00:51:12,559 --> 00:51:17,520
cause a buffer overflow and let's ask a
1282
00:51:15,359 --> 00:51:18,559
user for their name so we'll create a
1283
00:51:17,520 --> 00:51:21,359
prompt
1284
00:51:18,559 --> 00:51:21,359
printf
1285
00:51:21,680 --> 00:51:27,520
what's your
1286
00:51:23,599 --> 00:51:29,839
name and i again will use scanf
1287
00:51:27,520 --> 00:51:32,640
list the appropriate format specifier
1288
00:51:29,839 --> 00:51:34,960
for character arrays which is percent s
1289
00:51:32,640 --> 00:51:36,960
then our variable name and use the
1290
00:51:34,960 --> 00:51:38,640
address of operator then at the end
1291
00:51:36,960 --> 00:51:41,200
we'll display our name
1292
00:51:38,640 --> 00:51:42,480
printf
1293
00:51:41,200 --> 00:51:44,319
hello
1294
00:51:42,480 --> 00:51:46,319
percent s
1295
00:51:44,319 --> 00:51:49,119
how are you
1296
00:51:46,319 --> 00:51:50,559
then comma name and before we run this
1297
00:51:49,119 --> 00:51:52,319
i'm just going to add some new line
1298
00:51:50,559 --> 00:51:54,400
characters just to make everything
1299
00:51:52,319 --> 00:51:57,440
easier to read
1300
00:51:54,400 --> 00:51:57,440
okay let's try it
1301
00:51:58,319 --> 00:52:02,160
what's your name i'll type in just my
1302
00:52:00,480 --> 00:52:03,440
first name not including any white
1303
00:52:02,160 --> 00:52:05,520
spaces
1304
00:52:03,440 --> 00:52:08,240
how old are you 21
1305
00:52:05,520 --> 00:52:10,960
hello bro how are you you are 21 years
1306
00:52:08,240 --> 00:52:13,280
old so now this time let's type in a
1307
00:52:10,960 --> 00:52:15,280
first and last name because this is
1308
00:52:13,280 --> 00:52:16,800
going to act a little bit bizarre what's
1309
00:52:15,280 --> 00:52:18,400
your name bro
1310
00:52:16,800 --> 00:52:20,559
code and this is separated with the
1311
00:52:18,400 --> 00:52:22,960
white space hit enter
1312
00:52:20,559 --> 00:52:25,839
hello bro it did not include my last
1313
00:52:22,960 --> 00:52:28,559
name how are you you are zero years old
1314
00:52:25,839 --> 00:52:31,520
so using the scan f function we will
1315
00:52:28,559 --> 00:52:33,760
read up to any white spaces so if your
1316
00:52:31,520 --> 00:52:35,760
user input is going to include a white
1317
00:52:33,760 --> 00:52:39,920
space we need to use a different
1318
00:52:35,760 --> 00:52:41,119
function and that is the f gets function
1319
00:52:39,920 --> 00:52:42,800
f gets
1320
00:52:41,119 --> 00:52:44,640
parentheses
1321
00:52:42,800 --> 00:52:47,040
and there are three things we will list
1322
00:52:44,640 --> 00:52:49,359
within the fgets function
1323
00:52:47,040 --> 00:52:51,200
the name of the variable and we do not
1324
00:52:49,359 --> 00:52:53,440
need the address of operator that
1325
00:52:51,200 --> 00:52:56,000
ampersand for this
1326
00:52:53,440 --> 00:52:58,319
then we need to set an input size i will
1327
00:52:56,000 --> 00:53:00,400
set this to 25 to match the size of our
1328
00:52:58,319 --> 00:53:03,520
array
1329
00:53:00,400 --> 00:53:06,319
and then std in which means standard
1330
00:53:03,520 --> 00:53:08,079
input so using this function we can read
1331
00:53:06,319 --> 00:53:10,240
any white spaces
1332
00:53:08,079 --> 00:53:12,079
what's your name i'll type in a first
1333
00:53:10,240 --> 00:53:14,400
name and a last name
1334
00:53:12,079 --> 00:53:16,400
how old are you 21
1335
00:53:14,400 --> 00:53:18,079
hello bro code and notice that our
1336
00:53:16,400 --> 00:53:20,319
output is actually being displayed on
1337
00:53:18,079 --> 00:53:23,440
the next line that's because when you
1338
00:53:20,319 --> 00:53:25,200
use the f gets function it will include
1339
00:53:23,440 --> 00:53:27,040
the new line character when you hit
1340
00:53:25,200 --> 00:53:28,800
enter if you need to get rid of that new
1341
00:53:27,040 --> 00:53:30,640
line character at the end so that it's
1342
00:53:28,800 --> 00:53:32,559
not included with your input here's what
1343
00:53:30,640 --> 00:53:34,640
we can do it's a little advanced but
1344
00:53:32,559 --> 00:53:36,559
we'll cover this in future videos we'll
1345
00:53:34,640 --> 00:53:38,480
include this import
1346
00:53:36,559 --> 00:53:41,040
include
1347
00:53:38,480 --> 00:53:42,960
string dot h
1348
00:53:41,040 --> 00:53:45,040
so using this import we can work with
1349
00:53:42,960 --> 00:53:46,559
strings all we're going to do is edit
1350
00:53:45,040 --> 00:53:48,240
our string and get rid of that newline
1351
00:53:46,559 --> 00:53:49,839
character at the end what i'm about to
1352
00:53:48,240 --> 00:53:51,520
show you will look a little bit advanced
1353
00:53:49,839 --> 00:53:54,079
but it's going to make more sense when
1354
00:53:51,520 --> 00:53:55,920
we get to the video on string functions
1355
00:53:54,079 --> 00:53:58,000
type the name of the variable followed
1356
00:53:55,920 --> 00:54:02,000
by a set of straight brackets
1357
00:53:58,000 --> 00:54:04,559
type str len this gets the length
1358
00:54:02,000 --> 00:54:07,599
subtract one and we will set the sequel
1359
00:54:04,559 --> 00:54:09,680
to backslash zero that will get rid of
1360
00:54:07,599 --> 00:54:11,599
the new line character so again this is
1361
00:54:09,680 --> 00:54:13,599
a little bit advanced for us but it'll
1362
00:54:11,599 --> 00:54:16,000
make more sense in future videos when we
1363
00:54:13,599 --> 00:54:18,400
get to the video on string functions and
1364
00:54:16,000 --> 00:54:19,920
let's try this one last time
1365
00:54:18,400 --> 00:54:22,079
what's your name
1366
00:54:19,920 --> 00:54:24,720
type in a first name and a last name hit
1367
00:54:22,079 --> 00:54:27,680
enter how old are you 21.
1368
00:54:24,720 --> 00:54:30,319
hello bro code how are you you are 21
1369
00:54:27,680 --> 00:54:32,720
years old if you need to accept a string
1370
00:54:30,319 --> 00:54:34,880
from a user that includes white spaces
1371
00:54:32,720 --> 00:54:37,680
like a first name and a last name you'll
1372
00:54:34,880 --> 00:54:39,440
want to use fgets in place of scanf
1373
00:54:37,680 --> 00:54:41,760
because scanf can't read those white
1374
00:54:39,440 --> 00:54:43,839
spaces it stops right there but it will
1375
00:54:41,760 --> 00:54:45,040
include that new line character when you
1376
00:54:43,839 --> 00:54:47,200
hit enter
1377
00:54:45,040 --> 00:54:48,480
so you can do some string formatting
1378
00:54:47,200 --> 00:54:50,400
just to get rid of that new line
1379
00:54:48,480 --> 00:54:52,160
character and like i said this statement
1380
00:54:50,400 --> 00:54:54,480
here will make more sense when we reach
1381
00:54:52,160 --> 00:54:56,319
the video on string functions but yeah
1382
00:54:54,480 --> 00:54:59,119
basically that's how you can accept user
1383
00:54:56,319 --> 00:55:00,799
input you can use the scanf function if
1384
00:54:59,119 --> 00:55:02,960
you need to accept a string of
1385
00:55:00,799 --> 00:55:05,520
characters that includes white spaces
1386
00:55:02,960 --> 00:55:08,400
you'll want to use fgets instead so yeah
1387
00:55:05,520 --> 00:55:09,920
that's how to accept user input and see
1388
00:55:08,400 --> 00:55:12,000
if you found this video helpful please
1389
00:55:09,920 --> 00:55:13,599
be sure to smash that like button leave
1390
00:55:12,000 --> 00:55:15,040
a random comment down below and
1391
00:55:13,599 --> 00:55:17,680
subscribe if you'd like to become a
1392
00:55:15,040 --> 00:55:17,680
fellow bro
1393
00:55:17,920 --> 00:55:22,319
hey what's going on everybody so in this
1394
00:55:20,480 --> 00:55:25,200
video i'm gonna show you some useful
1395
00:55:22,319 --> 00:55:27,520
math functions in c now if we include
1396
00:55:25,200 --> 00:55:29,359
this math header file this contains a
1397
00:55:27,520 --> 00:55:31,680
lot of the useful functions i'm about to
1398
00:55:29,359 --> 00:55:33,680
show you one useful function is the
1399
00:55:31,680 --> 00:55:35,520
square root function let's say that we
1400
00:55:33,680 --> 00:55:37,520
have a bunch of variables we declared
1401
00:55:35,520 --> 00:55:40,960
them but we have not yet assigned them i
1402
00:55:37,520 --> 00:55:43,760
will assign a the square root of nine so
1403
00:55:40,960 --> 00:55:46,960
after including this math header file i
1404
00:55:43,760 --> 00:55:49,680
have access to a square root function
1405
00:55:46,960 --> 00:55:51,680
so type sqrt then add a set of
1406
00:55:49,680 --> 00:55:53,920
parentheses and within the parentheses
1407
00:55:51,680 --> 00:55:55,680
we can find the square root of a number
1408
00:55:53,920 --> 00:55:57,760
let's find what the square root of nine
1409
00:55:55,680 --> 00:55:59,280
is and then display it with a printf
1410
00:55:57,760 --> 00:56:02,160
statement
1411
00:55:59,280 --> 00:56:04,400
so the square root of nine is three so
1412
00:56:02,160 --> 00:56:07,599
let's move on we can raise a base to a
1413
00:56:04,400 --> 00:56:10,079
given power by using the pow function
1414
00:56:07,599 --> 00:56:12,160
the first number is the base let's raise
1415
00:56:10,079 --> 00:56:13,599
two to the power of four and then
1416
00:56:12,160 --> 00:56:16,240
display it
1417
00:56:13,599 --> 00:56:18,160
two to the power of four is 16. we can
1418
00:56:16,240 --> 00:56:20,000
round a number and i will store this
1419
00:56:18,160 --> 00:56:21,400
within an integer
1420
00:56:20,000 --> 00:56:23,119
let's round
1421
00:56:21,400 --> 00:56:24,960
3.14
1422
00:56:23,119 --> 00:56:29,040
oh and then make sure you use the
1423
00:56:24,960 --> 00:56:33,040
appropriate format specifier for ins
1424
00:56:29,040 --> 00:56:35,440
3.14 rounded is 3.
1425
00:56:33,040 --> 00:56:39,520
now by using the seal short for ceiling
1426
00:56:35,440 --> 00:56:39,520
function we can always round a number up
1427
00:56:39,799 --> 00:56:46,079
3.14 rounded up is 4.
1428
00:56:43,599 --> 00:56:48,920
likewise there's a floor function where
1429
00:56:46,079 --> 00:56:52,240
we will always round down
1430
00:56:48,920 --> 00:56:54,079
3.99 rounded down
1431
00:56:52,240 --> 00:56:57,119
is
1432
00:56:54,079 --> 00:56:59,520
3. we can find the absolute value of a
1433
00:56:57,119 --> 00:57:00,640
number that's how far a number is away
1434
00:56:59,520 --> 00:57:02,559
from zero
1435
00:57:00,640 --> 00:57:05,359
so it will take any negative numbers and
1436
00:57:02,559 --> 00:57:08,400
make them positive the absolute value of
1437
00:57:05,359 --> 00:57:08,400
negative 100
1438
00:57:08,640 --> 00:57:14,000
and that would be positive 100 if you're
1439
00:57:11,200 --> 00:57:16,799
familiar with logarithms we can find the
1440
00:57:14,000 --> 00:57:18,160
logarithm of a number
1441
00:57:16,799 --> 00:57:19,200
log 3
1442
00:57:18,160 --> 00:57:21,760
is
1443
00:57:19,200 --> 00:57:23,520
1.098612
1444
00:57:21,760 --> 00:57:27,440
and if you know some trigonometry
1445
00:57:23,520 --> 00:57:29,200
there's various functions for sine
1446
00:57:27,440 --> 00:57:31,599
cosine
1447
00:57:29,200 --> 00:57:35,680
and tangent
1448
00:57:31,599 --> 00:57:35,680
so what's the tangent of 45
1449
00:57:35,839 --> 00:57:41,599
that is supposedly this number
1450
00:57:38,839 --> 00:57:43,760
1.619775 so yeah everybody those are a
1451
00:57:41,599 --> 00:57:46,240
few math functions that you might be
1452
00:57:43,760 --> 00:57:49,440
interested in to use these just include
1453
00:57:46,240 --> 00:57:50,880
this header file at the top math.h if
1454
00:57:49,440 --> 00:57:52,880
you found this video helpful please be
1455
00:57:50,880 --> 00:57:54,960
sure to smash that like button leave a
1456
00:57:52,880 --> 00:57:58,559
random comment down below and subscribe
1457
00:57:54,960 --> 00:57:58,559
if you'd like to become a fellow bro
1458
00:57:59,119 --> 00:58:02,960
hey everyone here's a quick program that
1459
00:58:01,440 --> 00:58:04,720
we can make to calculate the
1460
00:58:02,960 --> 00:58:06,400
circumference of a circle i thought this
1461
00:58:04,720 --> 00:58:08,480
would be good practice for us now that
1462
00:58:06,400 --> 00:58:10,160
we know how user input works let's
1463
00:58:08,480 --> 00:58:12,799
declare all of the variables that we'll
1464
00:58:10,160 --> 00:58:14,720
need i'm going to create a constant
1465
00:58:12,799 --> 00:58:16,960
variable named pi and it's going to be
1466
00:58:14,720 --> 00:58:19,920
of the double data type pi equals
1467
00:58:16,960 --> 00:58:21,119
3.14159
1468
00:58:19,920 --> 00:58:23,040
the reason that i'm making this a
1469
00:58:21,119 --> 00:58:25,200
constant is that i don't want anybody
1470
00:58:23,040 --> 00:58:28,160
else to be able to change the value of
1471
00:58:25,200 --> 00:58:29,920
pi and let's declare but not assign a
1472
00:58:28,160 --> 00:58:31,440
radius quite yet we'll have the user
1473
00:58:29,920 --> 00:58:34,079
type that in
1474
00:58:31,440 --> 00:58:36,400
and double circumference
1475
00:58:34,079 --> 00:58:38,559
this will be calculated and displayed so
1476
00:58:36,400 --> 00:58:41,520
we will need to prompt the user to enter
1477
00:58:38,559 --> 00:58:42,960
in a radius i'll proceed this with a new
1478
00:58:41,520 --> 00:58:44,079
line
1479
00:58:42,960 --> 00:58:45,359
enter
1480
00:58:44,079 --> 00:58:48,880
radius
1481
00:58:45,359 --> 00:58:51,520
of a circle and then i will use scanf to
1482
00:58:48,880 --> 00:58:54,799
accept some user input we need to list
1483
00:58:51,520 --> 00:58:57,920
the format specifier of doubles which is
1484
00:58:54,799 --> 00:59:00,079
lf comma address of operator
1485
00:58:57,920 --> 00:59:04,000
radius
1486
00:59:00,079 --> 00:59:07,839
then the formula for radius is 2 times
1487
00:59:04,000 --> 00:59:10,960
pi times radius and then we will display
1488
00:59:07,839 --> 00:59:12,640
our circumference using printf
1489
00:59:10,960 --> 00:59:14,720
circumference
1490
00:59:12,640 --> 00:59:16,880
then we need a format specifier we're
1491
00:59:14,720 --> 00:59:19,440
displaying a double so the format
1492
00:59:16,880 --> 00:59:21,680
specifier is lf
1493
00:59:19,440 --> 00:59:24,160
comma circumference
1494
00:59:21,680 --> 00:59:28,079
and let's run it
1495
00:59:24,160 --> 00:59:30,960
enter well the radius of a circle
1496
00:59:28,079 --> 00:59:33,680
uh let's say that our radius is 10 maybe
1497
00:59:30,960 --> 00:59:35,880
this is 10 i don't know meters
1498
00:59:33,680 --> 00:59:38,559
all right our circumference is
1499
00:59:35,880 --> 00:59:40,640
62.83 meters now why not take this a
1500
00:59:38,559 --> 00:59:42,480
step further let's also calculate the
1501
00:59:40,640 --> 00:59:43,839
area of the circle i wasn't planning on
1502
00:59:42,480 --> 00:59:46,240
taking it this far but hey let's
1503
00:59:43,839 --> 00:59:48,880
calculate that as well for practice so
1504
00:59:46,240 --> 00:59:50,160
let's declare a double variable named
1505
00:59:48,880 --> 00:59:52,720
area
1506
00:59:50,160 --> 00:59:55,680
and we will calculate what area is the
1507
00:59:52,720 --> 00:59:57,839
formula for the area of the circle is
1508
00:59:55,680 --> 01:00:02,400
pi times radius squared
1509
00:59:57,839 --> 01:00:06,079
pi times radius times radius
1510
01:00:02,400 --> 01:00:06,079
then let's display the area
1511
01:00:06,559 --> 01:00:10,960
so area
1512
01:00:08,319 --> 01:00:13,200
we're using the double format specifier
1513
01:00:10,960 --> 01:00:16,799
and area then let's add some new line
1514
01:00:13,200 --> 01:00:19,680
characters to separate everything
1515
01:00:16,799 --> 01:00:21,200
enter the radius of a circle let's say
1516
01:00:19,680 --> 01:00:23,240
10 meters
1517
01:00:21,200 --> 01:00:28,799
the circumference is
1518
01:00:23,240 --> 01:00:30,960
62.83 meters and the area is 314
1519
01:00:28,799 --> 01:00:32,960
meters all right everybody that is a
1520
01:00:30,960 --> 01:00:34,799
small program to calculate the
1521
01:00:32,960 --> 01:00:37,119
circumference of a circle and i guess
1522
01:00:34,799 --> 01:00:38,880
the area as well just because well why
1523
01:00:37,119 --> 01:00:40,880
not so yeah if you found this video
1524
01:00:38,880 --> 01:00:42,640
helpful please be sure to smash that
1525
01:00:40,880 --> 01:00:44,480
like button leave a random comment down
1526
01:00:42,640 --> 01:00:47,440
below and subscribe if you'd like to
1527
01:00:44,480 --> 01:00:47,440
become a fellow bro
1528
01:00:48,720 --> 01:00:52,079
alright welcome back everybody in this
1529
01:00:50,799 --> 01:00:54,400
video we're going to write a small
1530
01:00:52,079 --> 01:00:56,319
practice program to find the hypotenuse
1531
01:00:54,400 --> 01:00:58,720
of a right triangle we'll need the help
1532
01:00:56,319 --> 01:01:00,400
of our math.h header file because we'll
1533
01:00:58,720 --> 01:01:03,040
need access to some useful math
1534
01:01:00,400 --> 01:01:04,880
functions more specifically the square
1535
01:01:03,040 --> 01:01:06,640
root function let's declare all of the
1536
01:01:04,880 --> 01:01:09,280
variables that we'll need we'll need
1537
01:01:06,640 --> 01:01:10,720
sides a b and c and these will be of the
1538
01:01:09,280 --> 01:01:11,520
double data type
1539
01:01:10,720 --> 01:01:12,480
a
1540
01:01:11,520 --> 01:01:13,440
b
1541
01:01:12,480 --> 01:01:16,240
c
1542
01:01:13,440 --> 01:01:17,440
we'll ask the user to enter in sides a
1543
01:01:16,240 --> 01:01:19,200
and b
1544
01:01:17,440 --> 01:01:20,480
print f
1545
01:01:19,200 --> 01:01:21,680
enter
1546
01:01:20,480 --> 01:01:25,440
side
1547
01:01:21,680 --> 01:01:28,720
a and then we will use scanf
1548
01:01:25,440 --> 01:01:31,760
so the format specifier for a double
1549
01:01:28,720 --> 01:01:34,880
is l f then i would like to insert a
1550
01:01:31,760 --> 01:01:37,839
value within a so i need to use the
1551
01:01:34,880 --> 01:01:41,119
address of operator then our variable a
1552
01:01:37,839 --> 01:01:44,240
okay let's do the same thing for side b
1553
01:01:41,119 --> 01:01:47,440
enter side b and store the user input
1554
01:01:44,240 --> 01:01:49,760
within variable b the formula to find
1555
01:01:47,440 --> 01:01:52,000
the hypotenuse of a right triangle is
1556
01:01:49,760 --> 01:01:53,040
the square root of a squared plus b
1557
01:01:52,000 --> 01:01:54,640
squared
1558
01:01:53,040 --> 01:01:57,440
so we will set c
1559
01:01:54,640 --> 01:02:00,640
equal to the square root function
1560
01:01:57,440 --> 01:02:02,000
and within the parentheses we will say a
1561
01:02:00,640 --> 01:02:05,760
times a
1562
01:02:02,000 --> 01:02:09,119
plus b times b
1563
01:02:05,760 --> 01:02:09,119
and then we will display c
1564
01:02:10,720 --> 01:02:12,880
side
1565
01:02:11,839 --> 01:02:15,200
c
1566
01:02:12,880 --> 01:02:19,119
and the format specifier for a double is
1567
01:02:15,200 --> 01:02:21,200
l f and we are displaying c and well
1568
01:02:19,119 --> 01:02:24,480
let's try it
1569
01:02:21,200 --> 01:02:25,280
so side a let's say is three side b is
1570
01:02:24,480 --> 01:02:28,720
four
1571
01:02:25,280 --> 01:02:30,000
that means side c is five so yeah i
1572
01:02:28,720 --> 01:02:32,000
thought that would be a good practice
1573
01:02:30,000 --> 01:02:34,319
program for us to get used to accepting
1574
01:02:32,000 --> 01:02:36,240
user input if this video helped you out
1575
01:02:34,319 --> 01:02:38,079
help me out by smashing that like button
1576
01:02:36,240 --> 01:02:39,520
leave a random comment down below and
1577
01:02:38,079 --> 01:02:42,160
subscribe if you'd like to become a
1578
01:02:39,520 --> 01:02:42,160
fellow bro
1579
01:02:42,960 --> 01:02:47,520
hey let's talk about if statements if
1580
01:02:45,039 --> 01:02:49,680
statements are used to add some choices
1581
01:02:47,520 --> 01:02:52,079
to a program let's take the small
1582
01:02:49,680 --> 01:02:54,319
program for example we have a variable
1583
01:02:52,079 --> 01:02:56,319
edge and we'll ask a user to enter in
1584
01:02:54,319 --> 01:02:58,160
their edge what if i would like to check
1585
01:02:56,319 --> 01:03:00,319
their edge maybe they're signing up for
1586
01:02:58,160 --> 01:03:02,640
a credit card or something so to check
1587
01:03:00,319 --> 01:03:05,039
some value we can write an if statement
1588
01:03:02,640 --> 01:03:08,559
if parentheses then a set of curly
1589
01:03:05,039 --> 01:03:11,119
braces if some condition that we specify
1590
01:03:08,559 --> 01:03:13,520
is true we will execute some block of
1591
01:03:11,119 --> 01:03:15,680
code some subsection of code what sort
1592
01:03:13,520 --> 01:03:18,720
of condition should we write let's check
1593
01:03:15,680 --> 01:03:21,039
to see if age is greater than or equal
1594
01:03:18,720 --> 01:03:23,200
to 18. so there's different comparison
1595
01:03:21,039 --> 01:03:24,000
operators there's greater than or equal
1596
01:03:23,200 --> 01:03:26,240
to
1597
01:03:24,000 --> 01:03:27,440
greater than less than less than or
1598
01:03:26,240 --> 01:03:29,680
equal to
1599
01:03:27,440 --> 01:03:32,559
or you could check to see if two values
1600
01:03:29,680 --> 01:03:34,880
are equal by using double equal signs
1601
01:03:32,559 --> 01:03:37,039
this is the comparison operator if you
1602
01:03:34,880 --> 01:03:38,960
use just one this is the assignment
1603
01:03:37,039 --> 01:03:41,680
operator and this would be the same as
1604
01:03:38,960 --> 01:03:44,640
assigning age equal to 18 so if you need
1605
01:03:41,680 --> 01:03:46,960
to compare if two values are equal use
1606
01:03:44,640 --> 01:03:49,119
the comparison operator which is double
1607
01:03:46,960 --> 01:03:51,440
equal signs but what i would like to do
1608
01:03:49,119 --> 01:03:54,319
is check to see if age is greater than
1609
01:03:51,440 --> 01:03:56,960
or equal to 18. if this condition
1610
01:03:54,319 --> 01:03:58,640
evaluates to be true we will have our
1611
01:03:56,960 --> 01:04:01,200
program do something
1612
01:03:58,640 --> 01:04:03,039
so let's print a message
1613
01:04:01,200 --> 01:04:07,760
since we're signing up for a credit card
1614
01:04:03,039 --> 01:04:09,359
let's say you are now signed up
1615
01:04:07,760 --> 01:04:11,920
and let's run it
1616
01:04:09,359 --> 01:04:14,480
end to your age let's say that i'm 21 i
1617
01:04:11,920 --> 01:04:16,799
hit enter boom you are now signed up but
1618
01:04:14,480 --> 01:04:18,559
what if this condition is false let's
1619
01:04:16,799 --> 01:04:20,400
say that i'm 12 years old and i'm
1620
01:04:18,559 --> 01:04:22,799
attempting to sign up for a credit card
1621
01:04:20,400 --> 01:04:25,520
well we skip this if statement if this
1622
01:04:22,799 --> 01:04:28,160
condition evaluates to be false we will
1623
01:04:25,520 --> 01:04:30,799
skip this block of code and continue on
1624
01:04:28,160 --> 01:04:33,119
with the rest of the program ignoring it
1625
01:04:30,799 --> 01:04:35,520
or we could do something else by using
1626
01:04:33,119 --> 01:04:38,480
an else statement if this condition is
1627
01:04:35,520 --> 01:04:41,039
false we will skip this subset of code
1628
01:04:38,480 --> 01:04:43,680
and instead perform this let's print a
1629
01:04:41,039 --> 01:04:48,160
different message instead
1630
01:04:43,680 --> 01:04:50,799
you are too young to sign up
1631
01:04:48,160 --> 01:04:53,280
and let's try that again
1632
01:04:50,799 --> 01:04:55,599
and to your edge i am 12.
1633
01:04:53,280 --> 01:04:57,680
you are too young to sign up you can
1634
01:04:55,599 --> 01:04:59,520
check more than one condition before
1635
01:04:57,680 --> 01:05:02,079
reaching your else statement by using
1636
01:04:59,520 --> 01:05:04,079
else if blocks and that is by using else
1637
01:05:02,079 --> 01:05:06,000
if statements we can check another
1638
01:05:04,079 --> 01:05:07,119
statement before reaching our else
1639
01:05:06,000 --> 01:05:10,079
statement
1640
01:05:07,119 --> 01:05:12,640
so let's check to see if age is less
1641
01:05:10,079 --> 01:05:14,240
than zero so obviously somebody's
1642
01:05:12,640 --> 01:05:16,640
messing with this program then because
1643
01:05:14,240 --> 01:05:19,200
you can't be under zero years old right
1644
01:05:16,640 --> 01:05:20,559
you haven't been
1645
01:05:19,200 --> 01:05:22,640
born yet
1646
01:05:20,559 --> 01:05:24,640
so after running this program if i say
1647
01:05:22,640 --> 01:05:26,160
that i'm negative
1648
01:05:24,640 --> 01:05:28,240
six
1649
01:05:26,160 --> 01:05:30,480
you haven't been born yet so we will
1650
01:05:28,240 --> 01:05:32,880
check our if statement first if this
1651
01:05:30,480 --> 01:05:35,039
condition is false we will move down to
1652
01:05:32,880 --> 01:05:37,280
the next block and then check this else
1653
01:05:35,039 --> 01:05:39,599
if condition if all above statements
1654
01:05:37,280 --> 01:05:41,760
evaluate to be false we will execute
1655
01:05:39,599 --> 01:05:43,920
this else block as a last resort so to
1656
01:05:41,760 --> 01:05:45,280
say and with these else if blocks you
1657
01:05:43,920 --> 01:05:46,640
can add more than one let's check
1658
01:05:45,280 --> 01:05:49,119
something else
1659
01:05:46,640 --> 01:05:50,799
just to demonstrate
1660
01:05:49,119 --> 01:05:54,799
else if
1661
01:05:50,799 --> 01:05:57,599
what about age is equal to zero
1662
01:05:54,799 --> 01:06:00,880
and we will print
1663
01:05:57,599 --> 01:06:05,559
you can't sign up
1664
01:06:00,880 --> 01:06:05,559
you were just born
1665
01:06:05,680 --> 01:06:10,799
and to your age i am zero years old
1666
01:06:08,559 --> 01:06:12,799
you can't sign up you were just born if
1667
01:06:10,799 --> 01:06:14,160
one of these conditions evaluates to be
1668
01:06:12,799 --> 01:06:16,319
true we will skip all of these
1669
01:06:14,160 --> 01:06:18,240
statements that come after then with our
1670
01:06:16,319 --> 01:06:20,640
else block if all above statements
1671
01:06:18,240 --> 01:06:22,640
evaluate to be false we definitely
1672
01:06:20,640 --> 01:06:25,039
execute whatever's within here
1673
01:06:22,640 --> 01:06:27,119
so yeah those are if statements they add
1674
01:06:25,039 --> 01:06:29,599
some choice to a program you can check
1675
01:06:27,119 --> 01:06:31,280
to see if some condition is true if not
1676
01:06:29,599 --> 01:06:33,359
you can check something else using else
1677
01:06:31,280 --> 01:06:34,880
if statements you can perform whatever
1678
01:06:33,359 --> 01:06:37,359
is within an else block and that's
1679
01:06:34,880 --> 01:06:39,359
optional so yeah those are if statements
1680
01:06:37,359 --> 01:06:41,039
and see if you found this video helpful
1681
01:06:39,359 --> 01:06:42,799
please be sure to help me out by
1682
01:06:41,039 --> 01:06:44,559
smashing that like button leave a random
1683
01:06:42,799 --> 01:06:47,839
comment down below and subscribe if
1684
01:06:44,559 --> 01:06:47,839
you'd like to become a fellow bro
1685
01:06:48,400 --> 01:06:54,000
all right welcome back people switches a
1686
01:06:51,280 --> 01:06:56,400
switch is a more efficient alternative
1687
01:06:54,000 --> 01:06:58,960
to using many else if statements it
1688
01:06:56,400 --> 01:07:01,520
allows a value to be tested for equality
1689
01:06:58,960 --> 01:07:03,440
against many cases here's an example of
1690
01:07:01,520 --> 01:07:05,760
where a switch would be useful i have a
1691
01:07:03,440 --> 01:07:08,240
small program we will tell a user to
1692
01:07:05,760 --> 01:07:10,079
enter in a letter grade and depending on
1693
01:07:08,240 --> 01:07:11,039
their grade we will print a custom
1694
01:07:10,079 --> 01:07:13,760
message
1695
01:07:11,039 --> 01:07:16,240
if grade equals a will print perfect
1696
01:07:13,760 --> 01:07:18,799
else if grade equals b will print a
1697
01:07:16,240 --> 01:07:21,119
different message so on and so forth so
1698
01:07:18,799 --> 01:07:23,280
it's considered bad practice to use a
1699
01:07:21,119 --> 01:07:25,920
lot of else if statements a better
1700
01:07:23,280 --> 01:07:29,200
alternative is to use a switch here's
1701
01:07:25,920 --> 01:07:30,480
how we can create one type switch
1702
01:07:29,200 --> 01:07:33,119
parentheses
1703
01:07:30,480 --> 01:07:35,440
curly braces whatever value you would
1704
01:07:33,119 --> 01:07:37,839
like to examine for equality placed
1705
01:07:35,440 --> 01:07:40,079
within the parentheses i would like to
1706
01:07:37,839 --> 01:07:42,079
examine my grade and now we need to
1707
01:07:40,079 --> 01:07:42,960
write some cases
1708
01:07:42,079 --> 01:07:44,880
case
1709
01:07:42,960 --> 01:07:46,559
and then some value you would like to
1710
01:07:44,880 --> 01:07:48,640
test for equality
1711
01:07:46,559 --> 01:07:50,400
so i am comparing characters i will
1712
01:07:48,640 --> 01:07:51,520
write the character a
1713
01:07:50,400 --> 01:07:53,520
colon
1714
01:07:51,520 --> 01:07:55,599
then if these values match we will
1715
01:07:53,520 --> 01:07:57,680
execute some subset of code kind of like
1716
01:07:55,599 --> 01:08:00,319
an if statement
1717
01:07:57,680 --> 01:08:00,319
let's print
1718
01:08:00,799 --> 01:08:03,280
perfect
1719
01:08:03,520 --> 01:08:10,280
and then add a break afterwards
1720
01:08:06,319 --> 01:08:10,280
then you can add another case
1721
01:08:10,480 --> 01:08:16,559
so case b
1722
01:08:13,359 --> 01:08:16,559
you did good
1723
01:08:17,440 --> 01:08:20,960
casey
1724
01:08:19,600 --> 01:08:23,279
you did
1725
01:08:20,960 --> 01:08:23,279
okay
1726
01:08:23,440 --> 01:08:29,120
case d
1727
01:08:25,520 --> 01:08:29,120
at least it's not an f
1728
01:08:31,440 --> 01:08:36,159
case f
1729
01:08:33,679 --> 01:08:38,080
you failed
1730
01:08:36,159 --> 01:08:40,400
now you can add a default case this
1731
01:08:38,080 --> 01:08:42,799
functions like an else statement if no
1732
01:08:40,400 --> 01:08:45,920
other cases match we will execute
1733
01:08:42,799 --> 01:08:48,159
whatever's within our default case
1734
01:08:45,920 --> 01:08:49,440
so that means there are no matching
1735
01:08:48,159 --> 01:08:50,799
letter grades
1736
01:08:49,440 --> 01:08:55,359
so let's print
1737
01:08:50,799 --> 01:08:57,679
please enter only valid grades
1738
01:08:55,359 --> 01:09:00,560
and let's test it
1739
01:08:57,679 --> 01:09:02,319
enter a letter grade a
1740
01:09:00,560 --> 01:09:04,560
this will print perfect whatever's
1741
01:09:02,319 --> 01:09:06,799
within our matching case
1742
01:09:04,560 --> 01:09:09,920
let's try it again
1743
01:09:06,799 --> 01:09:09,920
b you did good
1744
01:09:10,319 --> 01:09:13,440
see you did okay
1745
01:09:14,239 --> 01:09:18,080
d at least it's not enough
1746
01:09:19,520 --> 01:09:23,839
f you failed
1747
01:09:21,679 --> 01:09:26,239
and if there are no matching cases we
1748
01:09:23,839 --> 01:09:28,719
will execute our default case uh how
1749
01:09:26,239 --> 01:09:31,279
about w for win
1750
01:09:28,719 --> 01:09:33,199
please enter only valid grades the
1751
01:09:31,279 --> 01:09:35,040
reason that we add breaks after each
1752
01:09:33,199 --> 01:09:37,120
case is so we can break out of our
1753
01:09:35,040 --> 01:09:38,799
switch and exit let me show you what
1754
01:09:37,120 --> 01:09:41,199
this looks like if we do not have any
1755
01:09:38,799 --> 01:09:41,199
breaks
1756
01:09:41,920 --> 01:09:45,199
now let's say that we have a c letter
1757
01:09:44,080 --> 01:09:47,440
grade
1758
01:09:45,199 --> 01:09:49,679
you did okay at least it's not enough
1759
01:09:47,440 --> 01:09:50,719
you failed please enter only valid
1760
01:09:49,679 --> 01:09:52,799
grades
1761
01:09:50,719 --> 01:09:54,800
so the reason that we have breaks is to
1762
01:09:52,799 --> 01:09:57,120
exit out of our switch if we have a
1763
01:09:54,800 --> 01:09:59,280
matching case and there are no breaks we
1764
01:09:57,120 --> 01:10:01,440
will continue executing each case that
1765
01:09:59,280 --> 01:10:03,440
comes after so it is important to have
1766
01:10:01,440 --> 01:10:05,440
those breaks if you want to exit out of
1767
01:10:03,440 --> 01:10:07,440
your switch well yeah that's a switch
1768
01:10:05,440 --> 01:10:09,840
everybody it's a more efficient
1769
01:10:07,440 --> 01:10:12,000
alternative to using many else if
1770
01:10:09,840 --> 01:10:13,840
statements using a few elsif statements
1771
01:10:12,000 --> 01:10:16,400
is okay but it's considered poor
1772
01:10:13,840 --> 01:10:18,719
practice to use a lot of them so yeah
1773
01:10:16,400 --> 01:10:20,640
those are switches if this video helped
1774
01:10:18,719 --> 01:10:22,159
you out you can help me out by smashing
1775
01:10:20,640 --> 01:10:24,000
that like button leave a random comment
1776
01:10:22,159 --> 01:10:25,679
down below and subscribe if you'd like
1777
01:10:24,000 --> 01:10:27,840
to become a fellow bro
1778
01:10:25,679 --> 01:10:29,600
hey uh everybody it's bro hope you're
1779
01:10:27,840 --> 01:10:31,679
doing well and in this video we're going
1780
01:10:29,600 --> 01:10:33,360
to create a small program where the user
1781
01:10:31,679 --> 01:10:35,199
will type in a temperature and we can
1782
01:10:33,360 --> 01:10:38,320
convert that temperature from fahrenheit
1783
01:10:35,199 --> 01:10:42,320
to celsius or celsius to fahrenheit so
1784
01:10:38,320 --> 01:10:42,320
sit back relax and enjoy the show
1785
01:10:42,400 --> 01:10:46,719
now before we begin this video make sure
1786
01:10:44,480 --> 01:10:48,400
that you include these two imports at
1787
01:10:46,719 --> 01:10:50,320
the top we'll be working with string
1788
01:10:48,400 --> 01:10:53,920
functions and many of them can be found
1789
01:10:50,320 --> 01:10:55,440
within this import see type dot h so
1790
01:10:53,920 --> 01:10:56,640
let's declare the variables that we'll
1791
01:10:55,440 --> 01:11:00,239
need
1792
01:10:56,640 --> 01:11:03,920
char unit unit will be either c for
1793
01:11:00,239 --> 01:11:06,159
celsius or f for fahrenheit and float
1794
01:11:03,920 --> 01:11:08,239
temp short for temperature
1795
01:11:06,159 --> 01:11:10,840
let's prompt the user to enter in some
1796
01:11:08,239 --> 01:11:14,080
user input
1797
01:11:10,840 --> 01:11:16,640
printf is the
1798
01:11:14,080 --> 01:11:18,000
temperature
1799
01:11:16,640 --> 01:11:19,520
in
1800
01:11:18,000 --> 01:11:21,120
f
1801
01:11:19,520 --> 01:11:23,360
or
1802
01:11:21,120 --> 01:11:23,360
c
1803
01:11:23,920 --> 01:11:29,840
and then we will accept some user input
1804
01:11:26,880 --> 01:11:32,159
so we will be accepting a character so
1805
01:11:29,840 --> 01:11:33,360
use the appropriate format specifier for
1806
01:11:32,159 --> 01:11:35,760
characters
1807
01:11:33,360 --> 01:11:37,679
and we will use the address of operator
1808
01:11:35,760 --> 01:11:38,880
which is an ampersand
1809
01:11:37,679 --> 01:11:40,880
unit
1810
01:11:38,880 --> 01:11:42,320
and then let's use an if statement to
1811
01:11:40,880 --> 01:11:43,520
check to see
1812
01:11:42,320 --> 01:11:47,840
if unit
1813
01:11:43,520 --> 01:11:47,840
is equal to the character c
1814
01:11:48,000 --> 01:11:50,880
else if
1815
01:11:51,920 --> 01:11:57,280
unit is equal to f
1816
01:11:55,120 --> 01:12:00,080
we will use the formula to convert
1817
01:11:57,280 --> 01:12:02,400
fahrenheit to celsius if it's c celsius
1818
01:12:00,080 --> 01:12:04,480
to fahrenheit and we should probably add
1819
01:12:02,400 --> 01:12:06,840
an else statement as well
1820
01:12:04,480 --> 01:12:09,840
okay let's actually test these right
1821
01:12:06,840 --> 01:12:11,679
now okay this will only be temporary i'm
1822
01:12:09,840 --> 01:12:15,360
going to print a message
1823
01:12:11,679 --> 01:12:18,480
the temp is currently
1824
01:12:15,360 --> 01:12:18,480
in celsius
1825
01:12:19,120 --> 01:12:23,199
and with fahrenheit the temperature is
1826
01:12:20,880 --> 01:12:26,159
currently in fahrenheit
1827
01:12:23,199 --> 01:12:28,560
so if the user did not type in c or f
1828
01:12:26,159 --> 01:12:30,640
well then what the heck did they type in
1829
01:12:28,560 --> 01:12:32,560
so within our else statement let's yell
1830
01:12:30,640 --> 01:12:36,480
at the user let's say that whatever they
1831
01:12:32,560 --> 01:12:39,120
entered in is not valid input
1832
01:12:36,480 --> 01:12:44,960
so format specifier c
1833
01:12:39,120 --> 01:12:46,400
is not a valid unit of measurement
1834
01:12:44,960 --> 01:12:49,360
and then we will display whatever the
1835
01:12:46,400 --> 01:12:52,000
user typed into our unit variable
1836
01:12:49,360 --> 01:12:53,679
so let's test this is the temperature in
1837
01:12:52,000 --> 01:12:54,880
f or c
1838
01:12:53,679 --> 01:12:57,520
so f
1839
01:12:54,880 --> 01:12:59,199
the temp is currently in f
1840
01:12:57,520 --> 01:13:02,480
let's try it again
1841
01:12:59,199 --> 01:13:04,880
is the temperature in f or c see
1842
01:13:02,480 --> 01:13:07,600
the temp is currently in c
1843
01:13:04,880 --> 01:13:09,760
okay this time we will not type in f4c
1844
01:13:07,600 --> 01:13:11,520
how about the word pizza this only
1845
01:13:09,760 --> 01:13:14,320
accepts the first character
1846
01:13:11,520 --> 01:13:16,320
p is not a valid unit of measurement
1847
01:13:14,320 --> 01:13:19,760
here's one situation that we may run
1848
01:13:16,320 --> 01:13:22,960
into c programs are case sensitive if i
1849
01:13:19,760 --> 01:13:24,960
type in lowercase f or lowercase c well
1850
01:13:22,960 --> 01:13:27,120
technically neither of these conditions
1851
01:13:24,960 --> 01:13:28,640
would be true for example if i type
1852
01:13:27,120 --> 01:13:31,440
lowercase t
1853
01:13:28,640 --> 01:13:33,679
c is not a valid unit of measurement to
1854
01:13:31,440 --> 01:13:36,960
avoid that problem i can take my user
1855
01:13:33,679 --> 01:13:39,280
input and use the two upper function to
1856
01:13:36,960 --> 01:13:41,280
make it uppercase or you could set these
1857
01:13:39,280 --> 01:13:43,840
conditions to check for lowercase
1858
01:13:41,280 --> 01:13:45,040
characters instead and use the two lower
1859
01:13:43,840 --> 01:13:47,040
function
1860
01:13:45,040 --> 01:13:50,640
so let's take our unit variable and i'm
1861
01:13:47,040 --> 01:13:53,520
going to reassign it after using the to
1862
01:13:50,640 --> 01:13:56,159
upper function and then pass in our unit
1863
01:13:53,520 --> 01:13:59,280
to make it uppercase and now if i type
1864
01:13:56,159 --> 01:14:02,320
in lowercase c or lowercase f
1865
01:13:59,280 --> 01:14:04,640
this user input will be made uppercase
1866
01:14:02,320 --> 01:14:06,480
the temp is currently in c
1867
01:14:04,640 --> 01:14:08,640
so this is optional but i thought it'd
1868
01:14:06,480 --> 01:14:11,440
be a good thing to add to this program
1869
01:14:08,640 --> 01:14:13,679
now what we'll work on next depending on
1870
01:14:11,440 --> 01:14:16,239
the unit there's going to be a different
1871
01:14:13,679 --> 01:14:18,400
formula a different calculation
1872
01:14:16,239 --> 01:14:20,880
now we will need the user to enter in
1873
01:14:18,400 --> 01:14:23,280
the current temperature so let's begin
1874
01:14:20,880 --> 01:14:26,159
with our if statement if unit is equal
1875
01:14:23,280 --> 01:14:28,800
to c celsius
1876
01:14:26,159 --> 01:14:32,000
enter the temp
1877
01:14:28,800 --> 01:14:32,000
in celsius
1878
01:14:32,640 --> 01:14:35,760
this time we are accepting a floating
1879
01:14:34,640 --> 01:14:39,440
point number
1880
01:14:35,760 --> 01:14:40,840
we will use scanf the format specifier
1881
01:14:39,440 --> 01:14:44,080
for floating point
1882
01:14:40,840 --> 01:14:45,199
numbers address of operator
1883
01:14:44,080 --> 01:14:47,360
temp
1884
01:14:45,199 --> 01:14:49,280
and then we need to calculate the new
1885
01:14:47,360 --> 01:14:51,520
temperature after it's converted from
1886
01:14:49,280 --> 01:14:54,640
celsius to fahrenheit and we will
1887
01:14:51,520 --> 01:14:57,600
reassign it into the same variable temp
1888
01:14:54,640 --> 01:14:58,480
temp equals and here's the formula
1889
01:14:57,600 --> 01:14:59,600
temp
1890
01:14:58,480 --> 01:15:02,480
times
1891
01:14:59,600 --> 01:15:05,679
9 divided by 5
1892
01:15:02,480 --> 01:15:05,679
plus 32
1893
01:15:05,760 --> 01:15:09,120
and then let's print the temperature
1894
01:15:09,520 --> 01:15:13,360
printf
1895
01:15:11,920 --> 01:15:15,120
the
1896
01:15:13,360 --> 01:15:16,719
temp
1897
01:15:15,120 --> 01:15:18,800
in
1898
01:15:16,719 --> 01:15:21,120
fahrenheit
1899
01:15:18,800 --> 01:15:23,920
is
1900
01:15:21,120 --> 01:15:26,320
and i'm going to use a format specifier
1901
01:15:23,920 --> 01:15:29,199
percent f but i would only like to
1902
01:15:26,320 --> 01:15:31,679
display one digit after the decimal so i
1903
01:15:29,199 --> 01:15:34,400
will add dot one
1904
01:15:31,679 --> 01:15:37,120
and then we will insert our temperature
1905
01:15:34,400 --> 01:15:39,600
temp okay let's try this enter the
1906
01:15:37,120 --> 01:15:42,640
temperature in f or c currently we only
1907
01:15:39,600 --> 01:15:44,480
have the celsius portion set up
1908
01:15:42,640 --> 01:15:47,040
so see
1909
01:15:44,480 --> 01:15:50,719
enter the temp in celsius
1910
01:15:47,040 --> 01:15:54,640
how about zero degrees celsius
1911
01:15:50,719 --> 01:15:56,239
the temp in fahrenheit is 32.0 okay so
1912
01:15:54,640 --> 01:15:58,640
we know that it's working
1913
01:15:56,239 --> 01:16:01,679
let's fill out our else if statement
1914
01:15:58,640 --> 01:16:05,040
else if unit is equal to f
1915
01:16:01,679 --> 01:16:07,280
and let's copy some of this
1916
01:16:05,040 --> 01:16:08,719
enter the temp in
1917
01:16:07,280 --> 01:16:10,640
fahrenheit
1918
01:16:08,719 --> 01:16:12,239
we will reassign our temperature
1919
01:16:10,640 --> 01:16:14,080
variable
1920
01:16:12,239 --> 01:16:16,080
temp equals
1921
01:16:14,080 --> 01:16:19,679
and here's the formula
1922
01:16:16,080 --> 01:16:21,600
temp minus 32
1923
01:16:19,679 --> 01:16:25,920
times 5
1924
01:16:21,600 --> 01:16:25,920
and we will divide all of this by 9.
1925
01:16:26,190 --> 01:16:30,320
[Music]
1926
01:16:28,239 --> 01:16:32,880
then let's display the temperature in
1927
01:16:30,320 --> 01:16:32,880
celsius
1928
01:16:33,360 --> 01:16:37,520
the temp
1929
01:16:35,040 --> 01:16:40,320
in celsius is
1930
01:16:37,520 --> 01:16:43,440
our format specifier then the temp
1931
01:16:40,320 --> 01:16:46,080
variable is the temperature in f or c
1932
01:16:43,440 --> 01:16:49,440
this time it is in fahrenheit
1933
01:16:46,080 --> 01:16:51,679
enter the temp in fahrenheit so 32
1934
01:16:49,440 --> 01:16:54,000
degrees in fahrenheit should translate
1935
01:16:51,679 --> 01:16:55,440
to zero degrees celsius
1936
01:16:54,000 --> 01:16:57,120
which it is
1937
01:16:55,440 --> 01:16:59,360
so yeah everybody i thought that would
1938
01:16:57,120 --> 01:17:01,600
be a useful practice program for us to
1939
01:16:59,360 --> 01:17:03,280
get used to accepting user input
1940
01:17:01,600 --> 01:17:04,880
i'll post all of this code from this
1941
01:17:03,280 --> 01:17:06,880
program in the comments section down
1942
01:17:04,880 --> 01:17:08,560
below if you would like a copy
1943
01:17:06,880 --> 01:17:10,320
so if you found this video helpful you
1944
01:17:08,560 --> 01:17:12,159
can help me out by smashing that like
1945
01:17:10,320 --> 01:17:13,840
button leave random comments down below
1946
01:17:12,159 --> 01:17:17,159
and subscribe if you'd like to become a
1947
01:17:13,840 --> 01:17:17,159
fellow bro
1948
01:17:17,440 --> 01:17:21,440
hey yeah what's going on everybody i
1949
01:17:19,440 --> 01:17:23,920
thought in this video we could create a
1950
01:17:21,440 --> 01:17:26,239
simple calculator program in c for
1951
01:17:23,920 --> 01:17:28,159
practice let's begin by declaring all of
1952
01:17:26,239 --> 01:17:30,159
the different variables that we'll need
1953
01:17:28,159 --> 01:17:32,400
we'll need a character to store an
1954
01:17:30,159 --> 01:17:35,199
operator
1955
01:17:32,400 --> 01:17:36,640
so are we going to add subtract multiply
1956
01:17:35,199 --> 01:17:39,840
or divide
1957
01:17:36,640 --> 01:17:42,400
we'll need double num1
1958
01:17:39,840 --> 01:17:46,480
double num2
1959
01:17:42,400 --> 01:17:46,480
and double result
1960
01:17:47,120 --> 01:17:52,159
let's ask the user what type of
1961
01:17:48,800 --> 01:17:52,159
operation they would like to use
1962
01:17:52,400 --> 01:17:56,159
enter an operator
1963
01:17:56,560 --> 01:18:02,320
so we can use addition subtraction
1964
01:17:59,520 --> 01:18:05,280
multiplication or division
1965
01:18:02,320 --> 01:18:07,520
and we will use scanf to accept some
1966
01:18:05,280 --> 01:18:10,800
user input if we're accepting a
1967
01:18:07,520 --> 01:18:13,440
character the format specifier is c
1968
01:18:10,800 --> 01:18:15,199
and we will use the address of operator
1969
01:18:13,440 --> 01:18:18,159
the name of our variable we would like
1970
01:18:15,199 --> 01:18:21,360
to store some user input into
1971
01:18:18,159 --> 01:18:22,400
then let's accept num1
1972
01:18:21,360 --> 01:18:25,440
enter
1973
01:18:22,400 --> 01:18:25,440
number one
1974
01:18:25,520 --> 01:18:30,320
then scanf
1975
01:18:27,120 --> 01:18:33,199
the format specifier for a double
1976
01:18:30,320 --> 01:18:33,199
is lf
1977
01:18:33,679 --> 01:18:38,840
num1
1978
01:18:35,440 --> 01:18:43,600
okay do the same thing with number
1979
01:18:38,840 --> 01:18:46,239
two replace one with two
1980
01:18:43,600 --> 01:18:48,000
then to examine our operator let's use a
1981
01:18:46,239 --> 01:18:49,440
switch
1982
01:18:48,000 --> 01:18:52,239
switch
1983
01:18:49,440 --> 01:18:54,719
and we will examine our operator for any
1984
01:18:52,239 --> 01:18:56,800
matching cases let's add a default case
1985
01:18:54,719 --> 01:18:59,199
because i might forget to add this later
1986
01:18:56,800 --> 01:19:01,360
so if a user does not enter in anything
1987
01:18:59,199 --> 01:19:02,640
that has a matching case one of these
1988
01:19:01,360 --> 01:19:04,719
four symbols
1989
01:19:02,640 --> 01:19:10,719
let's print a message
1990
01:19:04,719 --> 01:19:10,719
let's say that our operator is not valid
1991
01:19:11,199 --> 01:19:17,600
our first case will be edition so case
1992
01:19:15,120 --> 01:19:18,719
addition
1993
01:19:17,600 --> 01:19:23,280
result
1994
01:19:18,719 --> 01:19:25,840
equals num1 plus num2 and let's display
1995
01:19:23,280 --> 01:19:25,840
our result
1996
01:19:26,640 --> 01:19:29,199
result
1997
01:19:31,040 --> 01:19:36,520
and the format specifier for a double is
1998
01:19:33,520 --> 01:19:36,520
lf
1999
01:19:36,560 --> 01:19:41,199
and then at the end of your case you
2000
01:19:38,400 --> 01:19:42,880
should break to break out of the switch
2001
01:19:41,199 --> 01:19:44,000
then we can copy this
2002
01:19:42,880 --> 01:19:47,440
paste it
2003
01:19:44,000 --> 01:19:48,800
and then change any plus to minus
2004
01:19:47,440 --> 01:19:51,800
then do the same thing with
2005
01:19:48,800 --> 01:19:51,800
multiplication
2006
01:19:52,320 --> 01:19:55,600
and lastly division
2007
01:19:55,920 --> 01:20:01,679
and that is it so let's try this
2008
01:19:58,800 --> 01:20:05,000
enter an operator i would like to add
2009
01:20:01,679 --> 01:20:06,880
enter number one 4.20 plus
2010
01:20:05,000 --> 01:20:08,040
3.14
2011
01:20:06,880 --> 01:20:10,719
that is
2012
01:20:08,040 --> 01:20:12,320
7.34 and you can limit the amount of
2013
01:20:10,719 --> 01:20:14,159
digits after the decimal that is
2014
01:20:12,320 --> 01:20:16,480
displayed if you would like to adjust
2015
01:20:14,159 --> 01:20:19,040
that with the format specifier with
2016
01:20:16,480 --> 01:20:20,880
result type dot than the amount of
2017
01:20:19,040 --> 01:20:22,320
digits you would like to display so i'm
2018
01:20:20,880 --> 01:20:24,800
just going to go ahead and change that
2019
01:20:22,320 --> 01:20:24,800
real quick
2020
01:20:25,199 --> 01:20:30,800
okay so by adding 0.2 to our format
2021
01:20:28,000 --> 01:20:32,639
specifier this will only display it two
2022
01:20:30,800 --> 01:20:34,880
digits after the decimal but you can
2023
01:20:32,639 --> 01:20:39,239
keep it the original way if you'd like
2024
01:20:34,880 --> 01:20:43,280
okay let's subtract so minus
2025
01:20:39,239 --> 01:20:44,840
4.20 minus 3.14
2026
01:20:43,280 --> 01:20:48,320
is
2027
01:20:44,840 --> 01:20:52,080
1.06 okay multiplication
2028
01:20:48,320 --> 01:20:55,040
3.14 times 4.20
2029
01:20:52,080 --> 01:20:57,199
is 13.19
2030
01:20:55,040 --> 01:20:58,719
and lastly division
2031
01:20:57,199 --> 01:21:01,440
3.14
2032
01:20:58,719 --> 01:21:02,199
divided by 4.20
2033
01:21:01,440 --> 01:21:03,760
is
2034
01:21:02,199 --> 01:21:06,080
0.75
2035
01:21:03,760 --> 01:21:08,159
and we do have a default case if there
2036
01:21:06,080 --> 01:21:10,080
are no matching cases
2037
01:21:08,159 --> 01:21:12,719
let's type in a character besides one of
2038
01:21:10,080 --> 01:21:14,719
these four symbols how about
2039
01:21:12,719 --> 01:21:16,560
i don't know a dollar sign
2040
01:21:14,719 --> 01:21:19,679
unfortunately we still need to enter
2041
01:21:16,560 --> 01:21:22,159
into numbers and then this states our
2042
01:21:19,679 --> 01:21:25,199
operator is not valid
2043
01:21:22,159 --> 01:21:27,360
so yeah everybody that is a very simple
2044
01:21:25,199 --> 01:21:29,120
calculator program in c
2045
01:21:27,360 --> 01:21:31,360
if you would like a copy of all this
2046
01:21:29,120 --> 01:21:33,840
code i'll post a copy in the comment
2047
01:21:31,360 --> 01:21:36,400
section down below so this calculator
2048
01:21:33,840 --> 01:21:38,960
can add subtract multiply and divide
2049
01:21:36,400 --> 01:21:41,199
there are four cases but you can expand
2050
01:21:38,960 --> 01:21:43,600
upon this if you'd like so yeah
2051
01:21:41,199 --> 01:21:47,360
everybody that was a very simple
2052
01:21:43,600 --> 01:21:47,360
calculator program in c
2053
01:21:48,880 --> 01:21:53,920
hey yeah what's going on people logical
2054
01:21:51,440 --> 01:21:56,320
operators there are three logical
2055
01:21:53,920 --> 01:21:58,000
operators we will discuss in the series
2056
01:21:56,320 --> 01:22:00,480
and or
2057
01:21:58,000 --> 01:22:03,679
and not but in this video we're going to
2058
01:22:00,480 --> 01:22:06,080
focus on and first the and logical
2059
01:22:03,679 --> 01:22:09,280
operator which is represented by two
2060
01:22:06,080 --> 01:22:11,440
ampersands checks to see if two or more
2061
01:22:09,280 --> 01:22:12,880
conditions are true here's an example
2062
01:22:11,440 --> 01:22:15,040
we're going to create a program that
2063
01:22:12,880 --> 01:22:17,360
will check to see if a given temperature
2064
01:22:15,040 --> 01:22:19,920
falls within a range so let's say we
2065
01:22:17,360 --> 01:22:21,840
have a floating point number named temp
2066
01:22:19,920 --> 01:22:24,159
temp short for temperature and this will
2067
01:22:21,840 --> 01:22:26,400
equal some number in celsius let's say
2068
01:22:24,159 --> 01:22:29,280
25 to begin with so using an if
2069
01:22:26,400 --> 01:22:32,639
statement let's check to see if temp
2070
01:22:29,280 --> 01:22:34,400
is greater than or equal to zero
2071
01:22:32,639 --> 01:22:36,880
if that is true then we will print
2072
01:22:34,400 --> 01:22:39,600
something
2073
01:22:36,880 --> 01:22:41,600
the weather is good
2074
01:22:39,600 --> 01:22:43,520
else
2075
01:22:41,600 --> 01:22:47,440
the weather is
2076
01:22:43,520 --> 01:22:49,440
bad okay so temp is 25 therefore the
2077
01:22:47,440 --> 01:22:50,960
weather is good
2078
01:22:49,440 --> 01:22:53,520
okay what if our temperature is
2079
01:22:50,960 --> 01:22:56,320
something extreme like 1000 degrees
2080
01:22:53,520 --> 01:22:58,639
celsius so technically this condition
2081
01:22:56,320 --> 01:23:00,159
would still be true the weather is
2082
01:22:58,639 --> 01:23:02,639
actually not good it's fairly
2083
01:23:00,159 --> 01:23:05,199
catastrophic so let's check to see if
2084
01:23:02,639 --> 01:23:07,920
another condition is also true by using
2085
01:23:05,199 --> 01:23:10,480
the and logical operator we're checking
2086
01:23:07,920 --> 01:23:13,679
to see if temp is greater than or equal
2087
01:23:10,480 --> 01:23:14,639
to 0 and some other condition such as
2088
01:23:13,679 --> 01:23:18,000
temp
2089
01:23:14,639 --> 01:23:21,600
is less than or equal to 30.
2090
01:23:18,000 --> 01:23:24,480
so if temp is at 1000
2091
01:23:21,600 --> 01:23:27,520
then the weather is bad this condition
2092
01:23:24,480 --> 01:23:30,239
is true but this one is false using the
2093
01:23:27,520 --> 01:23:32,400
and logical operator both conditions
2094
01:23:30,239 --> 01:23:35,760
must be true in order to execute this
2095
01:23:32,400 --> 01:23:38,480
statement now if our temperature was 25
2096
01:23:35,760 --> 01:23:41,280
degrees well then both conditions are
2097
01:23:38,480 --> 01:23:42,960
true and we will execute this statement
2098
01:23:41,280 --> 01:23:44,800
the weather is good
2099
01:23:42,960 --> 01:23:46,960
now you can add more than one condition
2100
01:23:44,800 --> 01:23:48,800
let's throw in another variable let's
2101
01:23:46,960 --> 01:23:51,199
say that we have a boolean variable
2102
01:23:48,800 --> 01:23:53,199
named sunny let's say that it's cloudy
2103
01:23:51,199 --> 01:23:55,360
outside now if we're working with
2104
01:23:53,199 --> 01:23:57,600
booleans include this header file at the
2105
01:23:55,360 --> 01:24:00,080
top stdbool.h
2106
01:23:57,600 --> 01:24:02,960
then let's add another condition
2107
01:24:00,080 --> 01:24:06,000
and let's check to see if sunny
2108
01:24:02,960 --> 01:24:08,560
is equal to true if you're checking the
2109
01:24:06,000 --> 01:24:11,199
value of a boolean variable you don't
2110
01:24:08,560 --> 01:24:12,400
necessarily have to type out equals true
2111
01:24:11,199 --> 01:24:14,719
you can say
2112
01:24:12,400 --> 01:24:17,280
sunny is equal to one or you can just
2113
01:24:14,719 --> 01:24:19,679
say sunny because this would contain
2114
01:24:17,280 --> 01:24:22,159
true or false so this time we are
2115
01:24:19,679 --> 01:24:23,840
checking to see if temp is greater than
2116
01:24:22,159 --> 01:24:27,760
or equal to zero
2117
01:24:23,840 --> 01:24:30,880
and temp is less than or equal to 30 and
2118
01:24:27,760 --> 01:24:33,600
sunny is true the temp is 25 but sunny
2119
01:24:30,880 --> 01:24:34,800
equals false therefore we do not execute
2120
01:24:33,600 --> 01:24:37,360
this statement
2121
01:24:34,800 --> 01:24:39,600
these first two conditions are true but
2122
01:24:37,360 --> 01:24:42,639
this one is false and using the and
2123
01:24:39,600 --> 01:24:43,600
logical operator all conditions must be
2124
01:24:42,639 --> 01:24:46,000
true
2125
01:24:43,600 --> 01:24:48,639
now if i set this to be true
2126
01:24:46,000 --> 01:24:50,800
well then all three of these conditions
2127
01:24:48,639 --> 01:24:53,360
are true and we will execute this
2128
01:24:50,800 --> 01:24:56,159
statement the weather is good so yeah
2129
01:24:53,360 --> 01:24:58,880
that is the and logical operator it is
2130
01:24:56,159 --> 01:25:01,440
represented by two ampersands it checks
2131
01:24:58,880 --> 01:25:04,080
to see if two or more conditions are
2132
01:25:01,440 --> 01:25:06,159
true and one example we used is to check
2133
01:25:04,080 --> 01:25:08,800
to see if our temperature falls within a
2134
01:25:06,159 --> 01:25:10,800
certain range so yeah that is the and
2135
01:25:08,800 --> 01:25:13,920
logical operator in the next video we
2136
01:25:10,800 --> 01:25:15,440
will discuss the or logical operator so
2137
01:25:13,920 --> 01:25:17,360
if this video helped you out you can
2138
01:25:15,440 --> 01:25:18,960
help me out by smashing that like button
2139
01:25:17,360 --> 01:25:20,320
leave a random comment down below and
2140
01:25:18,960 --> 01:25:22,960
subscribe if you'd like to become a
2141
01:25:20,320 --> 01:25:22,960
fellow bro
2142
01:25:24,159 --> 01:25:30,159
hey again it's me so we're gonna talk
2143
01:25:26,960 --> 01:25:32,719
about the or logical operator the or
2144
01:25:30,159 --> 01:25:35,679
logical operator which is represented by
2145
01:25:32,719 --> 01:25:38,159
two vertical bars checks if at least one
2146
01:25:35,679 --> 01:25:40,000
condition is true let's take the small
2147
01:25:38,159 --> 01:25:42,719
program for example it's kind of similar
2148
01:25:40,000 --> 01:25:44,560
to the last video so we have a variable
2149
01:25:42,719 --> 01:25:47,440
named temperature for temperature we
2150
01:25:44,560 --> 01:25:49,920
will first check if temp is less than or
2151
01:25:47,440 --> 01:25:51,920
equal to zero if that's true we will
2152
01:25:49,920 --> 01:25:54,480
print the weather is bad
2153
01:25:51,920 --> 01:25:57,520
else if temp is greater than or equal to
2154
01:25:54,480 --> 01:25:59,600
30 the weather is bad else the weather
2155
01:25:57,520 --> 01:26:02,320
is good so you know this program does
2156
01:25:59,600 --> 01:26:04,320
work the weather is good but another way
2157
01:26:02,320 --> 01:26:06,560
of writing this is that we can use the
2158
01:26:04,320 --> 01:26:09,920
or logical operator and we can check to
2159
01:26:06,560 --> 01:26:12,719
see if at least one of two or more
2160
01:26:09,920 --> 01:26:13,760
conditions is true so let's take this
2161
01:26:12,719 --> 01:26:16,239
condition
2162
01:26:13,760 --> 01:26:18,719
get rid of this else if statement
2163
01:26:16,239 --> 01:26:20,880
so we will check to see if temp is less
2164
01:26:18,719 --> 01:26:24,719
than or equal to zero
2165
01:26:20,880 --> 01:26:27,920
or temp is greater than or equal to 30.
2166
01:26:24,719 --> 01:26:31,199
so if our temperature is negative 1000
2167
01:26:27,920 --> 01:26:34,000
degrees celsius then the weather is bad
2168
01:26:31,199 --> 01:26:35,840
this is true but this is false and using
2169
01:26:34,000 --> 01:26:38,159
the or logical
2170
01:26:35,840 --> 01:26:40,159
only one of these conditions needs to be
2171
01:26:38,159 --> 01:26:42,320
true if both are true that's fine as
2172
01:26:40,159 --> 01:26:44,800
well or our temperature could be
2173
01:26:42,320 --> 01:26:47,440
positive 1000 degrees celsius the
2174
01:26:44,800 --> 01:26:49,280
weather is also bad this is false and
2175
01:26:47,440 --> 01:26:52,000
this is true and only one of these
2176
01:26:49,280 --> 01:26:55,120
conditions needs to be true but if our
2177
01:26:52,000 --> 01:26:57,600
temperature is 25 well neither of these
2178
01:26:55,120 --> 01:26:59,840
conditions are true at least one
2179
01:26:57,600 --> 01:27:02,320
condition needs to be true in order to
2180
01:26:59,840 --> 01:27:05,120
execute the statement so yeah that is
2181
01:27:02,320 --> 01:27:08,159
the or logical operator it checks to see
2182
01:27:05,120 --> 01:27:10,159
if at least one condition is true and
2183
01:27:08,159 --> 01:27:12,400
you can check more than one condition by
2184
01:27:10,159 --> 01:27:14,480
adding another set of vertical bars for
2185
01:27:12,400 --> 01:27:16,239
the ore operator so if you found this
2186
01:27:14,480 --> 01:27:17,840
video helpful please be sure to help me
2187
01:27:16,239 --> 01:27:19,360
out by smashing that like button leave
2188
01:27:17,840 --> 01:27:20,639
her in a comment down below and
2189
01:27:19,360 --> 01:27:23,280
subscribe if you'd like to become a
2190
01:27:20,639 --> 01:27:23,280
fellow bro
2191
01:27:24,239 --> 01:27:28,639
hey yeah let's discuss the notch logical
2192
01:27:26,880 --> 01:27:30,800
operator which is represented by an
2193
01:27:28,639 --> 01:27:32,719
exclamation point its purpose is to
2194
01:27:30,800 --> 01:27:34,480
reverse the state of a condition let's
2195
01:27:32,719 --> 01:27:36,960
write a small program let's say that we
2196
01:27:34,480 --> 01:27:39,199
have a boolean variable named sunny and
2197
01:27:36,960 --> 01:27:41,040
this will be either true or false let's
2198
01:27:39,199 --> 01:27:42,800
say it's true to begin with so to work
2199
01:27:41,040 --> 01:27:45,040
with boolean variables include this
2200
01:27:42,800 --> 01:27:46,719
header file at the top and we will write
2201
01:27:45,040 --> 01:27:48,800
an if-else statement to check to see if
2202
01:27:46,719 --> 01:27:50,239
it's sunny outside and print a custom
2203
01:27:48,800 --> 01:27:52,239
message
2204
01:27:50,239 --> 01:27:53,840
so if sunny
2205
01:27:52,239 --> 01:27:56,719
is equal to true
2206
01:27:53,840 --> 01:27:56,719
then we will print
2207
01:27:57,840 --> 01:28:02,719
it's sunny outside else it must be
2208
01:28:00,800 --> 01:28:05,280
cloudy outside
2209
01:28:02,719 --> 01:28:06,880
so this does work it's sunny outside
2210
01:28:05,280 --> 01:28:09,440
another way of writing this is that we
2211
01:28:06,880 --> 01:28:11,760
could say sunny is equal to one because
2212
01:28:09,440 --> 01:28:14,400
true represents one false represents
2213
01:28:11,760 --> 01:28:17,840
zero this would also work or you could
2214
01:28:14,400 --> 01:28:20,320
just say sunny this would work as well
2215
01:28:17,840 --> 01:28:22,560
so by using the not logical operator
2216
01:28:20,320 --> 01:28:24,880
this will reverse the state of condition
2217
01:28:22,560 --> 01:28:27,600
by using the not logical operator we're
2218
01:28:24,880 --> 01:28:29,679
checking to see if something is not true
2219
01:28:27,600 --> 01:28:32,400
so if i try this again
2220
01:28:29,679 --> 01:28:34,239
this program states it's cloudy outside
2221
01:28:32,400 --> 01:28:37,600
so i think it would be better if we were
2222
01:28:34,239 --> 01:28:40,000
to switch these print statements around
2223
01:28:37,600 --> 01:28:42,159
now what we're checking is if it's not
2224
01:28:40,000 --> 01:28:44,480
sunny if it's not sunny then we will
2225
01:28:42,159 --> 01:28:46,800
print it's cloudy outside
2226
01:28:44,480 --> 01:28:49,120
else it's sunny outside
2227
01:28:46,800 --> 01:28:51,120
it is sunny outside and if i change this
2228
01:28:49,120 --> 01:28:52,159
to be false
2229
01:28:51,120 --> 01:28:54,639
well then
2230
01:28:52,159 --> 01:28:56,480
it's cloudy outside so yeah that's the
2231
01:28:54,639 --> 01:28:58,639
not logical operator you can just
2232
01:28:56,480 --> 01:29:01,520
precede a condition or a boolean
2233
01:28:58,639 --> 01:29:03,360
variable with the not logical operator
2234
01:29:01,520 --> 01:29:05,600
and that gives you a few more options in
2235
01:29:03,360 --> 01:29:08,480
the way in which you can write a program
2236
01:29:05,600 --> 01:29:10,159
so yeah that is the not logical operator
2237
01:29:08,480 --> 01:29:12,080
if you found this video helpful you can
2238
01:29:10,159 --> 01:29:13,679
help me out by smashing that like button
2239
01:29:12,080 --> 01:29:15,040
leave random comments down below and
2240
01:29:13,679 --> 01:29:17,679
subscribe if you'd like to become a
2241
01:29:15,040 --> 01:29:17,679
fellow bro
2242
01:29:18,639 --> 01:29:23,199
hey yeah what's going on let's talk
2243
01:29:20,400 --> 01:29:25,199
about functions a function is a small
2244
01:29:23,199 --> 01:29:27,679
section of code that is executed
2245
01:29:25,199 --> 01:29:30,400
whenever we call it also known as
2246
01:29:27,679 --> 01:29:33,520
invoking a function when we compile and
2247
01:29:30,400 --> 01:29:36,239
run a c program we begin with invoking
2248
01:29:33,520 --> 01:29:38,560
the main function so here's our task we
2249
01:29:36,239 --> 01:29:41,280
need to sing three verses of happy
2250
01:29:38,560 --> 01:29:43,280
birthday for some reason if we are only
2251
01:29:41,280 --> 01:29:45,840
using the main function we could write
2252
01:29:43,280 --> 01:29:45,840
it like this
2253
01:29:49,760 --> 01:29:54,400
this will be the first verse of happy
2254
01:29:51,920 --> 01:29:57,360
birthday and if i need to sing three
2255
01:29:54,400 --> 01:29:58,800
verses i could just copy and paste this
2256
01:29:57,360 --> 01:30:00,960
one section
2257
01:29:58,800 --> 01:30:02,400
and then paste it two additional times
2258
01:30:00,960 --> 01:30:04,080
then i'm just going to add a new line
2259
01:30:02,400 --> 01:30:05,920
character at the end
2260
01:30:04,080 --> 01:30:08,400
technically this would work but it's
2261
01:30:05,920 --> 01:30:10,159
considered poor practice to repeat code
2262
01:30:08,400 --> 01:30:11,840
more than once if you don't have to
2263
01:30:10,159 --> 01:30:14,400
wouldn't it be better if we write this
2264
01:30:11,840 --> 01:30:16,560
code once and then simply reuse it well
2265
01:30:14,400 --> 01:30:18,560
we can by using a function so let's
2266
01:30:16,560 --> 01:30:20,960
delete two of these verses
2267
01:30:18,560 --> 01:30:22,880
and we will create a new function so
2268
01:30:20,960 --> 01:30:25,199
outside of the main function let's
2269
01:30:22,880 --> 01:30:27,600
declare a new function so for now we're
2270
01:30:25,199 --> 01:30:29,199
going to type void and then a unique
2271
01:30:27,600 --> 01:30:31,600
name for this function let's say that
2272
01:30:29,199 --> 01:30:33,760
it's the birthday function followed by a
2273
01:30:31,600 --> 01:30:36,320
set of parentheses and then a set of
2274
01:30:33,760 --> 01:30:38,560
curly braces so i'm going to take any
2275
01:30:36,320 --> 01:30:41,199
code i would like to repeat and place it
2276
01:30:38,560 --> 01:30:43,520
within this new birthday function and if
2277
01:30:41,199 --> 01:30:45,120
i need to invoke this function all i
2278
01:30:43,520 --> 01:30:47,120
have to do is type the name of the
2279
01:30:45,120 --> 01:30:48,880
function followed by a set of
2280
01:30:47,120 --> 01:30:51,120
parentheses i like to think of the
2281
01:30:48,880 --> 01:30:53,040
parentheses as a pair of telephones
2282
01:30:51,120 --> 01:30:54,639
talking to each other that's one trick i
2283
01:30:53,040 --> 01:30:56,800
used to remember that's how to call a
2284
01:30:54,639 --> 01:30:58,800
function so follow the function name
2285
01:30:56,800 --> 01:31:00,800
with the set of parentheses and when i
2286
01:30:58,800 --> 01:31:03,360
invoke this function once we will
2287
01:31:00,800 --> 01:31:05,920
execute any code within this function
2288
01:31:03,360 --> 01:31:07,679
once if i need to repeat this code three
2289
01:31:05,920 --> 01:31:10,400
times i will just have to call this
2290
01:31:07,679 --> 01:31:12,400
function three times
2291
01:31:10,400 --> 01:31:14,159
we are calling our birthday function
2292
01:31:12,400 --> 01:31:16,000
three times
2293
01:31:14,159 --> 01:31:17,840
and there we go we have invoked this
2294
01:31:16,000 --> 01:31:19,760
birthday function three times and we
2295
01:31:17,840 --> 01:31:21,760
have accomplished our goal of singing
2296
01:31:19,760 --> 01:31:23,920
three verses of happy birthday so
2297
01:31:21,760 --> 01:31:26,320
basically that's all what a function is
2298
01:31:23,920 --> 01:31:28,400
it's some subsection of code that is
2299
01:31:26,320 --> 01:31:30,639
executed whenever you invoke it also
2300
01:31:28,400 --> 01:31:32,560
known as calling a function a benefit to
2301
01:31:30,639 --> 01:31:34,320
using functions is that if you think
2302
01:31:32,560 --> 01:31:36,480
you're going to use some code more than
2303
01:31:34,320 --> 01:31:38,000
once you can stick it within a function
2304
01:31:36,480 --> 01:31:39,760
and then to repeat that code you just
2305
01:31:38,000 --> 01:31:42,000
call the function as many times as you
2306
01:31:39,760 --> 01:31:43,760
need so yeah those are functions if you
2307
01:31:42,000 --> 01:31:45,440
would like a copy of this code i will
2308
01:31:43,760 --> 01:31:47,280
post this to the comments section down
2309
01:31:45,440 --> 01:31:49,920
below and in the next video we will
2310
01:31:47,280 --> 01:31:54,159
discuss arguments and parameters so yeah
2311
01:31:49,920 --> 01:31:54,159
that's the basics of functions in c
2312
01:31:55,360 --> 01:32:00,159
hey yeah what's going on people so let's
2313
01:31:57,520 --> 01:32:02,000
talk about arguments and parameters i
2314
01:32:00,159 --> 01:32:04,080
have a function named birthday which we
2315
01:32:02,000 --> 01:32:06,400
discussed in the previous video this
2316
01:32:04,080 --> 01:32:10,080
will print two lines of text happy
2317
01:32:06,400 --> 01:32:12,560
birthday dear x you are y years old what
2318
01:32:10,080 --> 01:32:16,159
i would like to do is replace x with a
2319
01:32:12,560 --> 01:32:17,840
user's name and y with a user's age so
2320
01:32:16,159 --> 01:32:20,320
here's how we can write a program like
2321
01:32:17,840 --> 01:32:22,880
that so let's begin by within the main
2322
01:32:20,320 --> 01:32:25,360
function declaring two variables a
2323
01:32:22,880 --> 01:32:27,440
character array named name
2324
01:32:25,360 --> 01:32:29,840
and assign this your first name and an
2325
01:32:27,440 --> 01:32:32,560
age and this will be of the int data
2326
01:32:29,840 --> 01:32:34,400
type and let's say that this will be age
2327
01:32:32,560 --> 01:32:36,800
and make up some age i like to think
2328
01:32:34,400 --> 01:32:39,440
that i'm still 21 years old okay so if i
2329
01:32:36,800 --> 01:32:41,520
try and display a name and age you think
2330
01:32:39,440 --> 01:32:44,800
this would work right so i'm going to
2331
01:32:41,520 --> 01:32:47,440
use a format specifier then s to display
2332
01:32:44,800 --> 01:32:50,239
a string and then i will display our
2333
01:32:47,440 --> 01:32:52,960
name variable and with y let's display
2334
01:32:50,239 --> 01:32:55,199
our age so let's use d for our format
2335
01:32:52,960 --> 01:32:57,520
specifier to display an integer and we
2336
01:32:55,199 --> 01:32:58,800
will display age now here's the problem
2337
01:32:57,520 --> 01:33:01,760
you can see that we have some red
2338
01:32:58,800 --> 01:33:04,480
underlines name is undeclared as well as
2339
01:33:01,760 --> 01:33:07,120
age here's the deal functions can't see
2340
01:33:04,480 --> 01:33:10,159
inside of other functions our birthday
2341
01:33:07,120 --> 01:33:12,639
function is unaware of our name and age
2342
01:33:10,159 --> 01:33:14,960
variables one way in which we can make
2343
01:33:12,639 --> 01:33:17,440
our birthday function aware of these is
2344
01:33:14,960 --> 01:33:19,440
to pass them as arguments when we invoke
2345
01:33:17,440 --> 01:33:21,679
the birthday function so based on the
2346
01:33:19,440 --> 01:33:23,760
last topic on functions i mentioned that
2347
01:33:21,679 --> 01:33:26,000
when we call a function we type the
2348
01:33:23,760 --> 01:33:28,000
function's name followed by a set of
2349
01:33:26,000 --> 01:33:30,400
parentheses i like to imagine the set of
2350
01:33:28,000 --> 01:33:32,239
parentheses as a pair of telephones and
2351
01:33:30,400 --> 01:33:34,480
these two functions are talking to each
2352
01:33:32,239 --> 01:33:36,880
other our main function needs to make
2353
01:33:34,480 --> 01:33:39,360
our birthday function aware of our name
2354
01:33:36,880 --> 01:33:42,320
and age variables so what we can do is
2355
01:33:39,360 --> 01:33:44,800
pass these variables as arguments so
2356
01:33:42,320 --> 01:33:47,600
between the parentheses we can add our
2357
01:33:44,800 --> 01:33:51,120
variables separated with a comma so
2358
01:33:47,600 --> 01:33:53,440
let's pass name and age so anything that
2359
01:33:51,120 --> 01:33:56,480
you're passing to a function are known
2360
01:33:53,440 --> 01:33:58,800
as arguments but we need a matching set
2361
01:33:56,480 --> 01:34:01,120
of parameters within the function
2362
01:33:58,800 --> 01:34:04,080
declaration so between this set of
2363
01:34:01,120 --> 01:34:06,639
parentheses we will list our name and
2364
01:34:04,080 --> 01:34:08,719
age variables but precede each with the
2365
01:34:06,639 --> 01:34:12,000
data type that we're working with so we
2366
01:34:08,719 --> 01:34:14,800
have name and age so age
2367
01:34:12,000 --> 01:34:18,159
is an integer so precede the variable
2368
01:34:14,800 --> 01:34:20,480
name with int and name is a character
2369
01:34:18,159 --> 01:34:22,000
array so we're going to precede name
2370
01:34:20,480 --> 01:34:24,000
with char
2371
01:34:22,000 --> 01:34:26,880
and then add a set of straight brackets
2372
01:34:24,000 --> 01:34:28,880
after so these are parameters parameters
2373
01:34:26,880 --> 01:34:32,239
are what this function needs in order to
2374
01:34:28,880 --> 01:34:35,199
be executed so this does work happy
2375
01:34:32,239 --> 01:34:37,520
birthday dear bro you are 21 years old
2376
01:34:35,199 --> 01:34:39,760
so with parameter setup you can only
2377
01:34:37,520 --> 01:34:42,639
call this function if you pass a
2378
01:34:39,760 --> 01:34:44,639
character array as well as an integer if
2379
01:34:42,639 --> 01:34:47,600
i were to remove these arguments we can
2380
01:34:44,639 --> 01:34:50,159
no longer call this birthday function
2381
01:34:47,600 --> 01:34:52,400
error too few arguments to function
2382
01:34:50,159 --> 01:34:54,880
birthday so when you call a function you
2383
01:34:52,400 --> 01:34:57,199
can pass some arguments but you need a
2384
01:34:54,880 --> 01:34:59,119
matching set of parameters and the names
2385
01:34:57,199 --> 01:35:01,119
of these parameters don't necessarily
2386
01:34:59,119 --> 01:35:03,520
have to be the same what's important is
2387
01:35:01,119 --> 01:35:07,119
the order of the arguments and the data
2388
01:35:03,520 --> 01:35:10,239
type so let's say name is x and age is y
2389
01:35:07,119 --> 01:35:12,239
so let's replace that here as well
2390
01:35:10,239 --> 01:35:14,960
and that would work too so yeah those
2391
01:35:12,239 --> 01:35:16,880
are arguments and parameters arguments
2392
01:35:14,960 --> 01:35:19,360
are what you're sending a function
2393
01:35:16,880 --> 01:35:21,360
parameters are what a function expects
2394
01:35:19,360 --> 01:35:24,159
when it's invoked and the benefits of
2395
01:35:21,360 --> 01:35:26,320
arguments and parameters is that these
2396
01:35:24,159 --> 01:35:28,000
functions can talk to each other because
2397
01:35:26,320 --> 01:35:30,080
functions can't see inside of one
2398
01:35:28,000 --> 01:35:32,159
another so if you have some variables
2399
01:35:30,080 --> 01:35:34,400
within a function you can pass them to
2400
01:35:32,159 --> 01:35:36,719
another function as arguments so yeah
2401
01:35:34,400 --> 01:35:38,400
those are arguments and parameters if
2402
01:35:36,719 --> 01:35:40,159
you would like a copy of this code i
2403
01:35:38,400 --> 01:35:42,560
will post this in the comments section
2404
01:35:40,159 --> 01:35:46,719
down below and well yeah those are
2405
01:35:42,560 --> 01:35:46,719
arguments and parameters in c
2406
01:35:48,159 --> 01:35:53,360
hey what's going on people so the return
2407
01:35:50,639 --> 01:35:56,080
statement the return statement returns a
2408
01:35:53,360 --> 01:35:58,159
value back to a calling function here's
2409
01:35:56,080 --> 01:36:00,400
an example i have a function named
2410
01:35:58,159 --> 01:36:03,199
square square is going to accept one
2411
01:36:00,400 --> 01:36:05,360
argument a double it's going to square
2412
01:36:03,199 --> 01:36:07,360
that double and return the result back
2413
01:36:05,360 --> 01:36:10,159
to the calling function so here's an
2414
01:36:07,360 --> 01:36:13,119
example let's say we have double x and
2415
01:36:10,159 --> 01:36:15,280
i'm going to call the square function
2416
01:36:13,119 --> 01:36:17,840
and then pass in some value
2417
01:36:15,280 --> 01:36:20,159
let's say 3.14
2418
01:36:17,840 --> 01:36:22,560
so when we call a function we can also
2419
01:36:20,159 --> 01:36:25,040
return a value back to the spot in which
2420
01:36:22,560 --> 01:36:28,800
we call a function so within our square
2421
01:36:25,040 --> 01:36:32,480
function let's set double result equal
2422
01:36:28,800 --> 01:36:34,480
to x times x effectively squaring it so
2423
01:36:32,480 --> 01:36:37,440
to return a value back to the calling
2424
01:36:34,480 --> 01:36:39,520
function we will use this return keyword
2425
01:36:37,440 --> 01:36:42,400
followed by some value or variable i
2426
01:36:39,520 --> 01:36:44,639
would like to return result however we
2427
01:36:42,400 --> 01:36:46,320
need to list the data type of what we're
2428
01:36:44,639 --> 01:36:49,280
returning within the function
2429
01:36:46,320 --> 01:36:51,600
declaration right now it's void so if
2430
01:36:49,280 --> 01:36:54,080
we're returning a double we will list
2431
01:36:51,600 --> 01:36:55,920
double here if this was an integer we
2432
01:36:54,080 --> 01:36:58,239
would return int
2433
01:36:55,920 --> 01:37:00,800
if it was a character we would return
2434
01:36:58,239 --> 01:37:03,119
char you kind of get the idea so result
2435
01:37:00,800 --> 01:37:05,920
is a double so we need to change this
2436
01:37:03,119 --> 01:37:08,320
from void to double and we will return
2437
01:37:05,920 --> 01:37:11,199
this result back to the calling function
2438
01:37:08,320 --> 01:37:14,960
so we're effectively stating that double
2439
01:37:11,199 --> 01:37:16,880
x equals 3.14 squared and then let's
2440
01:37:14,960 --> 01:37:18,560
print the result
2441
01:37:16,880 --> 01:37:21,280
so printf
2442
01:37:18,560 --> 01:37:23,520
the format specifier for a double is lf
2443
01:37:21,280 --> 01:37:27,840
and we will print x
2444
01:37:23,520 --> 01:37:29,679
and 3.14 squared is 9.859
2445
01:37:27,840 --> 01:37:32,320
now you could shorten this too we could
2446
01:37:29,679 --> 01:37:35,119
just say return x times x
2447
01:37:32,320 --> 01:37:38,400
instead of declaring a local variable
2448
01:37:35,119 --> 01:37:40,400
return x times x
2449
01:37:38,400 --> 01:37:42,400
and that would work the same so yeah
2450
01:37:40,400 --> 01:37:44,159
that's basically the return statement
2451
01:37:42,400 --> 01:37:47,199
you can place it within a function and
2452
01:37:44,159 --> 01:37:48,400
return a value or variable back to the
2453
01:37:47,199 --> 01:37:50,320
calling function
2454
01:37:48,400 --> 01:37:52,719
and the spot in which you invoked that
2455
01:37:50,320 --> 01:37:55,360
function but if you're going to return a
2456
01:37:52,719 --> 01:37:57,760
value or variable within the function
2457
01:37:55,360 --> 01:38:00,080
declaration you need to change void to
2458
01:37:57,760 --> 01:38:02,239
the data type of what you're returning
2459
01:38:00,080 --> 01:38:03,679
so yeah that is the return statement and
2460
01:38:02,239 --> 01:38:06,560
you may have noticed within our main
2461
01:38:03,679 --> 01:38:08,880
function we're returning zero zero is
2462
01:38:06,560 --> 01:38:11,760
the exit status if this program runs
2463
01:38:08,880 --> 01:38:14,239
successfully so instead of void with our
2464
01:38:11,760 --> 01:38:16,960
main function declaration we have int
2465
01:38:14,239 --> 01:38:18,960
because zero is technically an integer
2466
01:38:16,960 --> 01:38:20,560
so yeah that's the return statement if
2467
01:38:18,960 --> 01:38:22,239
you would like a copy of this code i
2468
01:38:20,560 --> 01:38:24,880
will post this to the comments section
2469
01:38:22,239 --> 01:38:28,000
down below well yeah that's the return
2470
01:38:24,880 --> 01:38:28,000
statement in c
2471
01:38:29,600 --> 01:38:35,280
hey sup people the trinary operator also
2472
01:38:32,719 --> 01:38:37,760
known as the conditional operator it's a
2473
01:38:35,280 --> 01:38:40,480
shortcut to using an if-else statement
2474
01:38:37,760 --> 01:38:42,639
when assigning or returning a value
2475
01:38:40,480 --> 01:38:45,199
here's the formula we write a condition
2476
01:38:42,639 --> 01:38:48,400
followed by a question mark if this
2477
01:38:45,199 --> 01:38:51,600
condition is true we return some value
2478
01:38:48,400 --> 01:38:53,520
if true or if that condition is false we
2479
01:38:51,600 --> 01:38:55,360
return some other value here's an
2480
01:38:53,520 --> 01:38:57,199
example first we'll use an if-else
2481
01:38:55,360 --> 01:38:59,520
statement and then later we'll switch to
2482
01:38:57,199 --> 01:39:02,000
the ternary operator so let's create a
2483
01:38:59,520 --> 01:39:04,159
function that will find the maximum of
2484
01:39:02,000 --> 01:39:06,159
two integers and i'm going to assign
2485
01:39:04,159 --> 01:39:09,119
that to int max
2486
01:39:06,159 --> 01:39:10,800
and we will invoke a function find max
2487
01:39:09,119 --> 01:39:13,040
but we'll still need to declare it so
2488
01:39:10,800 --> 01:39:14,239
let's pass in two integers maybe three
2489
01:39:13,040 --> 01:39:16,000
and four
2490
01:39:14,239 --> 01:39:18,239
and then i'm going to display whatever
2491
01:39:16,000 --> 01:39:20,800
max is
2492
01:39:18,239 --> 01:39:22,880
okay so let's declare this function so
2493
01:39:20,800 --> 01:39:25,199
we're returning an integer
2494
01:39:22,880 --> 01:39:29,119
the name is find max and let's set up
2495
01:39:25,199 --> 01:39:31,119
some parameters into x and int y so if
2496
01:39:29,119 --> 01:39:33,679
we're using an if else statement if we
2497
01:39:31,119 --> 01:39:35,199
need to return x if it's larger we can
2498
01:39:33,679 --> 01:39:38,480
check to see if
2499
01:39:35,199 --> 01:39:41,440
x is larger than y
2500
01:39:38,480 --> 01:39:43,760
if so then return x
2501
01:39:41,440 --> 01:39:43,760
else
2502
01:39:43,840 --> 01:39:47,840
return
2503
01:39:44,960 --> 01:39:50,480
y so this does work
2504
01:39:47,840 --> 01:39:51,760
so the max between three and four
2505
01:39:50,480 --> 01:39:54,400
is four
2506
01:39:51,760 --> 01:39:57,360
so if i switch three to five
2507
01:39:54,400 --> 01:39:59,360
well then the max is five so a shortcut
2508
01:39:57,360 --> 01:40:01,520
to writing an if else statement like
2509
01:39:59,360 --> 01:40:04,239
this if we're returning or assigning a
2510
01:40:01,520 --> 01:40:07,119
value is that we could instead use the
2511
01:40:04,239 --> 01:40:08,800
tenary operator so let's return and then
2512
01:40:07,119 --> 01:40:11,280
we have a condition
2513
01:40:08,800 --> 01:40:13,920
so this is our condition
2514
01:40:11,280 --> 01:40:16,159
is x greater than y then we add a
2515
01:40:13,920 --> 01:40:19,119
question mark
2516
01:40:16,159 --> 01:40:23,600
then the value we're returning if true
2517
01:40:19,119 --> 01:40:26,560
so if x is greater than y let's return x
2518
01:40:23,600 --> 01:40:28,719
colon then our value if this condition
2519
01:40:26,560 --> 01:40:31,040
is false and here within our else
2520
01:40:28,719 --> 01:40:33,199
statement we're returning y
2521
01:40:31,040 --> 01:40:35,280
and we no longer need this if else
2522
01:40:33,199 --> 01:40:37,440
statement and that's all there is to it
2523
01:40:35,280 --> 01:40:39,679
so we cut down on all that code and now
2524
01:40:37,440 --> 01:40:41,679
just have one line of code so it's kind
2525
01:40:39,679 --> 01:40:44,239
of like a shortcut and this works much
2526
01:40:41,679 --> 01:40:46,800
the same so the maximum between five and
2527
01:40:44,239 --> 01:40:49,760
four is five and if i change five back
2528
01:40:46,800 --> 01:40:51,760
to three well then the max is for so
2529
01:40:49,760 --> 01:40:53,920
yeah that's the ternary operator it's a
2530
01:40:51,760 --> 01:40:56,560
shortcut to using an if else statement
2531
01:40:53,920 --> 01:40:58,800
when assigning or returning a value you
2532
01:40:56,560 --> 01:41:01,280
write some condition add a question mark
2533
01:40:58,800 --> 01:41:04,320
like you're asking a question then list
2534
01:41:01,280 --> 01:41:06,800
some value to return if this condition
2535
01:41:04,320 --> 01:41:09,199
is true and then some other value if
2536
01:41:06,800 --> 01:41:11,199
this condition is false so you just
2537
01:41:09,199 --> 01:41:12,960
follow this formula so yeah that's the
2538
01:41:11,199 --> 01:41:14,719
trenary operator if you would like a
2539
01:41:12,960 --> 01:41:16,719
copy of this code i'll post this to the
2540
01:41:14,719 --> 01:41:20,960
comments section down below and well
2541
01:41:16,719 --> 01:41:20,960
yeah that's the canary operator nc
2542
01:41:22,480 --> 01:41:27,679
hey everybody so let's discuss function
2543
01:41:24,800 --> 01:41:30,480
prototypes a function prototype is a
2544
01:41:27,679 --> 01:41:32,719
functional declaration without a body
2545
01:41:30,480 --> 01:41:35,040
before we declare the main function it
2546
01:41:32,719 --> 01:41:37,679
ensures that calls to a function are
2547
01:41:35,040 --> 01:41:39,840
made with the correct number and type of
2548
01:41:37,679 --> 01:41:41,760
arguments it has some benefits which
2549
01:41:39,840 --> 01:41:43,840
we'll discuss later basically this is
2550
01:41:41,760 --> 01:41:46,560
another way in which we can declare and
2551
01:41:43,840 --> 01:41:48,639
define functions in the last few topics
2552
01:41:46,560 --> 01:41:51,119
when we were declaring functions we were
2553
01:41:48,639 --> 01:41:53,119
doing so before the main function so
2554
01:41:51,119 --> 01:41:55,040
let's say void and we have a function
2555
01:41:53,119 --> 01:41:57,119
named hello and there will be two
2556
01:41:55,040 --> 01:41:59,920
arguments and ray of characters named
2557
01:41:57,119 --> 01:42:01,840
name and int age and what i would like
2558
01:41:59,920 --> 01:42:05,600
this function to do is print a message
2559
01:42:01,840 --> 01:42:08,560
using our name and age
2560
01:42:05,600 --> 01:42:08,560
so hello name
2561
01:42:12,159 --> 01:42:17,520
you are age years old so what we're
2562
01:42:15,040 --> 01:42:20,400
going to do is now move this function
2563
01:42:17,520 --> 01:42:22,960
from before the main function to after
2564
01:42:20,400 --> 01:42:26,320
the main function and let's declare some
2565
01:42:22,960 --> 01:42:29,280
name and age variables so char name
2566
01:42:26,320 --> 01:42:31,440
use your first name and int age make up
2567
01:42:29,280 --> 01:42:32,800
some age now i'm going to invoke my
2568
01:42:31,440 --> 01:42:35,040
hello function
2569
01:42:32,800 --> 01:42:37,040
but pass in an incorrect number of
2570
01:42:35,040 --> 01:42:39,360
arguments now here's one of the benefits
2571
01:42:37,040 --> 01:42:41,440
of a function of prototype it's going to
2572
01:42:39,360 --> 01:42:43,840
ensure that we have the correct number
2573
01:42:41,440 --> 01:42:46,000
of arguments when we invoke a function
2574
01:42:43,840 --> 01:42:48,320
so the correct arguments for our hello
2575
01:42:46,000 --> 01:42:50,159
function are an array of characters and
2576
01:42:48,320 --> 01:42:52,000
an integer what if i'm missing one of
2577
01:42:50,159 --> 01:42:53,360
these arguments let's say we only pass
2578
01:42:52,000 --> 01:42:55,360
in a name
2579
01:42:53,360 --> 01:42:57,840
and then invoke this function
2580
01:42:55,360 --> 01:42:59,840
so this is going to result in unexpected
2581
01:42:57,840 --> 01:43:01,040
behavior
2582
01:42:59,840 --> 01:43:03,440
and we do have a warning here
2583
01:43:01,040 --> 01:43:05,760
conflicting types for hello but this
2584
01:43:03,440 --> 01:43:09,040
program will still compile and execute
2585
01:43:05,760 --> 01:43:10,719
so here's the result hello bro you are
2586
01:43:09,040 --> 01:43:13,040
million five hundred twelve thousand
2587
01:43:10,719 --> 01:43:14,159
three hundred and fifty two years old
2588
01:43:13,040 --> 01:43:16,000
and if i run this again this will
2589
01:43:14,159 --> 01:43:18,320
probably be a different number so this
2590
01:43:16,000 --> 01:43:20,560
will result in unexpected behavior we're
2591
01:43:18,320 --> 01:43:22,719
not ensuring that calls to a function
2592
01:43:20,560 --> 01:43:24,639
are made with the correct arguments one
2593
01:43:22,719 --> 01:43:26,960
way in which we can do that is to add a
2594
01:43:24,639 --> 01:43:30,000
function prototype before the main
2595
01:43:26,960 --> 01:43:32,639
function and to add a function prototype
2596
01:43:30,000 --> 01:43:34,880
to a given function we will declare that
2597
01:43:32,639 --> 01:43:36,880
function but not give it a body before
2598
01:43:34,880 --> 01:43:38,880
the main function and then we will later
2599
01:43:36,880 --> 01:43:40,560
add a body and define what this function
2600
01:43:38,880 --> 01:43:43,040
is going to do so before the main
2601
01:43:40,560 --> 01:43:44,320
function we will type the return type of
2602
01:43:43,040 --> 01:43:45,840
this function
2603
01:43:44,320 --> 01:43:47,360
void
2604
01:43:45,840 --> 01:43:50,080
the name of the function
2605
01:43:47,360 --> 01:43:52,400
hello and then add the data type and the
2606
01:43:50,080 --> 01:43:53,920
order of arguments so we have a
2607
01:43:52,400 --> 01:43:55,840
character array
2608
01:43:53,920 --> 01:43:59,199
and an integer then end it with a
2609
01:43:55,840 --> 01:44:00,639
semicolon so this is a function
2610
01:43:59,199 --> 01:44:02,880
prototype
2611
01:44:00,639 --> 01:44:05,119
it ensures that calls to a function are
2612
01:44:02,880 --> 01:44:07,840
made with the correct arguments so now
2613
01:44:05,119 --> 01:44:10,080
if i attempt to call the hello function
2614
01:44:07,840 --> 01:44:12,400
with only one argument when two are
2615
01:44:10,080 --> 01:44:14,159
required instead of a warning we'll
2616
01:44:12,400 --> 01:44:16,719
receive an error and it states that
2617
01:44:14,159 --> 01:44:19,360
there are too few arguments to function
2618
01:44:16,719 --> 01:44:21,280
hello so this program will not compile
2619
01:44:19,360 --> 01:44:23,760
and run successfully which is what we
2620
01:44:21,280 --> 01:44:26,000
want because we made a mistake and if i
2621
01:44:23,760 --> 01:44:27,920
were to add the correct arguments now
2622
01:44:26,000 --> 01:44:29,840
well then this program is going to
2623
01:44:27,920 --> 01:44:31,840
compile and run fine
2624
01:44:29,840 --> 01:44:35,360
so here's some important notes regarding
2625
01:44:31,840 --> 01:44:37,840
function prototypes many c compilers do
2626
01:44:35,360 --> 01:44:39,840
not check for parameter matching that's
2627
01:44:37,840 --> 01:44:42,159
why this program was still able to
2628
01:44:39,840 --> 01:44:44,400
compile and run without a function
2629
01:44:42,159 --> 01:44:46,560
prototype even though we were missing
2630
01:44:44,400 --> 01:44:49,520
arguments and that can result in
2631
01:44:46,560 --> 01:44:51,679
unexpected and unwanted behavior such as
2632
01:44:49,520 --> 01:44:54,800
saying that i was over a million years
2633
01:44:51,679 --> 01:44:57,440
old so a function of prototype causes
2634
01:44:54,800 --> 01:44:59,280
the compiler to flag an error if
2635
01:44:57,440 --> 01:45:01,360
arguments are missing
2636
01:44:59,280 --> 01:45:03,840
you can write the functions before the
2637
01:45:01,360 --> 01:45:06,560
main function that's okay too but a few
2638
01:45:03,840 --> 01:45:08,320
advantages of using a function prototype
2639
01:45:06,560 --> 01:45:10,800
is that it's easier to navigate a
2640
01:45:08,320 --> 01:45:12,880
program when the main function is at the
2641
01:45:10,800 --> 01:45:15,119
top of your file it also helps with
2642
01:45:12,880 --> 01:45:16,960
debugging and it's commonly used in
2643
01:45:15,119 --> 01:45:19,520
header files which is something we need
2644
01:45:16,960 --> 01:45:21,760
to discuss in a future topic so yeah
2645
01:45:19,520 --> 01:45:24,320
that's basically a function prototype
2646
01:45:21,760 --> 01:45:26,480
we're going to define any functions
2647
01:45:24,320 --> 01:45:28,800
after the main function now and before
2648
01:45:26,480 --> 01:45:30,719
the main function we will declare a
2649
01:45:28,800 --> 01:45:32,719
function prototype one function
2650
01:45:30,719 --> 01:45:34,639
prototype for each function we have
2651
01:45:32,719 --> 01:45:36,880
besides the main function and this
2652
01:45:34,639 --> 01:45:39,520
ensures that we have the correct number
2653
01:45:36,880 --> 01:45:42,000
and type of arguments which will prevent
2654
01:45:39,520 --> 01:45:44,080
any unexpected behavior it's basically
2655
01:45:42,000 --> 01:45:46,400
just another way to declare functions
2656
01:45:44,080 --> 01:45:48,800
that you may run into in the future so
2657
01:45:46,400 --> 01:45:50,239
yeah those are function prototypes if
2658
01:45:48,800 --> 01:45:51,920
you would like a copy of this code i
2659
01:45:50,239 --> 01:45:54,320
will post this to the comment section
2660
01:45:51,920 --> 01:45:59,000
down below and well yeah those are
2661
01:45:54,320 --> 01:45:59,000
function of prototypes in c
2662
01:45:59,360 --> 01:46:03,119
hey everybody so now that we know how
2663
01:46:01,520 --> 01:46:05,280
functions work i thought this would be a
2664
01:46:03,119 --> 01:46:07,520
good opportunity for us to learn about
2665
01:46:05,280 --> 01:46:09,040
some useful string functions so there's
2666
01:46:07,520 --> 01:46:11,360
a lot of string functions that are
2667
01:46:09,040 --> 01:46:14,080
already written for us just include this
2668
01:46:11,360 --> 01:46:17,199
header file at the top include string
2669
01:46:14,080 --> 01:46:19,119
dot h so create two strings one named
2670
01:46:17,199 --> 01:46:21,600
string one and the other named string
2671
01:46:19,119 --> 01:46:23,520
two string one will be your first name
2672
01:46:21,600 --> 01:46:24,960
string two will be your last name and
2673
01:46:23,520 --> 01:46:27,440
then at the end of this program i am
2674
01:46:24,960 --> 01:46:29,840
just printing string one using a printf
2675
01:46:27,440 --> 01:46:33,040
statement so let's begin the first
2676
01:46:29,840 --> 01:46:34,960
useful string function is string lower
2677
01:46:33,040 --> 01:46:37,280
so whatever string you pass in as an
2678
01:46:34,960 --> 01:46:40,159
argument this function will convert a
2679
01:46:37,280 --> 01:46:42,960
string to lowercase so the first letter
2680
01:46:40,159 --> 01:46:45,280
in my name is uppercase it's capitalized
2681
01:46:42,960 --> 01:46:46,400
so after passing in string1 into this
2682
01:46:45,280 --> 01:46:49,199
function
2683
01:46:46,400 --> 01:46:52,080
my name is all now lowercase
2684
01:46:49,199 --> 01:46:54,639
otherwise there's string upper that will
2685
01:46:52,080 --> 01:46:56,880
convert a string to uppercase
2686
01:46:54,639 --> 01:47:00,800
and my name is uppercase
2687
01:46:56,880 --> 01:47:03,760
stringcat will concatenate as in append
2688
01:47:00,800 --> 01:47:05,920
string2 to the end of string1 so we will
2689
01:47:03,760 --> 01:47:08,639
append the second argument to the first
2690
01:47:05,920 --> 01:47:11,520
argument so if i pass in string one then
2691
01:47:08,639 --> 01:47:13,520
string two this will append my last name
2692
01:47:11,520 --> 01:47:16,400
to my first name and these strings are
2693
01:47:13,520 --> 01:47:18,719
now combined or i could append a given
2694
01:47:16,400 --> 01:47:20,880
amount of characters from string two to
2695
01:47:18,719 --> 01:47:23,440
string one so the third argument is a
2696
01:47:20,880 --> 01:47:25,920
number if i append one character from
2697
01:47:23,440 --> 01:47:28,639
string two to string one then this will
2698
01:47:25,920 --> 01:47:31,119
take the c from my last name and append
2699
01:47:28,639 --> 01:47:34,000
it to the end of my first name so that
2700
01:47:31,119 --> 01:47:36,000
is the string and cat function
2701
01:47:34,000 --> 01:47:37,840
there's also string copy and this one is
2702
01:47:36,000 --> 01:47:40,239
actually fairly common
2703
01:47:37,840 --> 01:47:43,199
so this will copy string two to string
2704
01:47:40,239 --> 01:47:45,679
one if i print string one well it now
2705
01:47:43,199 --> 01:47:47,840
displays code instead of bro this will
2706
01:47:45,679 --> 01:47:49,840
remove the contents from string one and
2707
01:47:47,840 --> 01:47:52,080
replace them with string two or you
2708
01:47:49,840 --> 01:47:54,320
could copy a given amount of characters
2709
01:47:52,080 --> 01:47:55,760
from string two to string one uh let's
2710
01:47:54,320 --> 01:47:58,159
say that i'm going to copy the first
2711
01:47:55,760 --> 01:48:00,719
character over so i'm copying the first
2712
01:47:58,159 --> 01:48:02,080
character from my last name to my first
2713
01:48:00,719 --> 01:48:04,880
name
2714
01:48:02,080 --> 01:48:06,800
and my first name is now crow or i could
2715
01:48:04,880 --> 01:48:08,800
set this to two
2716
01:48:06,800 --> 01:48:10,480
and this will copy the first two letters
2717
01:48:08,800 --> 01:48:12,880
over
2718
01:48:10,480 --> 01:48:16,000
coo all right here's a few more so
2719
01:48:12,880 --> 01:48:18,800
string set will set all characters of a
2720
01:48:16,000 --> 01:48:21,360
string to a given character i'm going to
2721
01:48:18,800 --> 01:48:24,159
set all of the characters from string 1
2722
01:48:21,360 --> 01:48:26,960
to a question mark all of the characters
2723
01:48:24,159 --> 01:48:29,760
within my name are all question marks
2724
01:48:26,960 --> 01:48:31,760
and string and set will set the first
2725
01:48:29,760 --> 01:48:34,239
and characters of a string to a given
2726
01:48:31,760 --> 01:48:37,119
character so i'm going to set the first
2727
01:48:34,239 --> 01:48:39,920
character of my first name to an x
2728
01:48:37,119 --> 01:48:42,080
so my name is now x bro and string
2729
01:48:39,920 --> 01:48:44,480
reverse will reverse a string so if i
2730
01:48:42,080 --> 01:48:47,360
reverse string one
2731
01:48:44,480 --> 01:48:50,400
my first name is now orb so this next
2732
01:48:47,360 --> 01:48:53,119
set of functions will return an integer
2733
01:48:50,400 --> 01:48:55,360
so i'm going to declare a variable int
2734
01:48:53,119 --> 01:48:58,400
result and this function is really
2735
01:48:55,360 --> 01:49:00,400
common string length this returns the
2736
01:48:58,400 --> 01:49:03,119
string length as an integer
2737
01:49:00,400 --> 01:49:05,199
if i pass in string one this string has
2738
01:49:03,119 --> 01:49:08,159
a length of three characters and then
2739
01:49:05,199 --> 01:49:09,520
i'm going to print my result and this is
2740
01:49:08,159 --> 01:49:12,159
an integer
2741
01:49:09,520 --> 01:49:15,040
so the length of my first name
2742
01:49:12,159 --> 01:49:16,480
is three characters this is a fairly
2743
01:49:15,040 --> 01:49:19,520
common function
2744
01:49:16,480 --> 01:49:22,159
the string compare function will compare
2745
01:49:19,520 --> 01:49:24,800
all of the characters within two strings
2746
01:49:22,159 --> 01:49:27,040
if they're the same they will return
2747
01:49:24,800 --> 01:49:29,679
zero if they're different they will
2748
01:49:27,040 --> 01:49:31,119
return a number beside zero so here's an
2749
01:49:29,679 --> 01:49:34,719
if statement
2750
01:49:31,119 --> 01:49:36,960
if result is equal to zero we will print
2751
01:49:34,719 --> 01:49:38,880
these strings are the same else these
2752
01:49:36,960 --> 01:49:41,440
strings are not the same
2753
01:49:38,880 --> 01:49:43,599
so my first name is not equal to my last
2754
01:49:41,440 --> 01:49:46,000
name these characters are different so
2755
01:49:43,599 --> 01:49:47,840
this code will print
2756
01:49:46,000 --> 01:49:50,080
these strings are not the same and let
2757
01:49:47,840 --> 01:49:54,000
me get rid of that printf statement
2758
01:49:50,080 --> 01:49:57,199
okay now if i make the strings the same
2759
01:49:54,000 --> 01:49:59,440
then result is going to be zero
2760
01:49:57,199 --> 01:50:02,400
these strings are the same and you can
2761
01:49:59,440 --> 01:50:04,639
compare a given amount of characters too
2762
01:50:02,400 --> 01:50:06,560
so currently i have these strings set to
2763
01:50:04,639 --> 01:50:08,719
be the same characters so this will
2764
01:50:06,560 --> 01:50:10,560
compare only the first character
2765
01:50:08,719 --> 01:50:12,480
these strings are the same even if i
2766
01:50:10,560 --> 01:50:14,719
change the other characters the first
2767
01:50:12,480 --> 01:50:17,599
characters will still be the same
2768
01:50:14,719 --> 01:50:19,679
so this function will return zero there
2769
01:50:17,599 --> 01:50:22,800
are two additional versions of string
2770
01:50:19,679 --> 01:50:27,440
compare and string and compare they are
2771
01:50:22,800 --> 01:50:29,840
string compare i and string n i compare
2772
01:50:27,440 --> 01:50:32,880
so these will do the exact same things
2773
01:50:29,840 --> 01:50:34,960
except they ignore case sensitivity so
2774
01:50:32,880 --> 01:50:36,880
yeah everybody those are just a few
2775
01:50:34,960 --> 01:50:39,520
useful string functions there's still a
2776
01:50:36,880 --> 01:50:41,199
lot more located within this header file
2777
01:50:39,520 --> 01:50:43,199
but we would need to understand how
2778
01:50:41,199 --> 01:50:45,119
things like pointers work which we have
2779
01:50:43,199 --> 01:50:47,360
not discussed yet but i thought i would
2780
01:50:45,119 --> 01:50:49,520
at least cover some of the basics so
2781
01:50:47,360 --> 01:50:51,440
yeah those are a few useful string
2782
01:50:49,520 --> 01:50:53,119
functions in c if you would like a copy
2783
01:50:51,440 --> 01:50:55,760
of all these notes i'll post them to the
2784
01:50:53,119 --> 01:50:58,239
comment section down below and well yeah
2785
01:50:55,760 --> 01:51:00,639
those are a few useful string functions
2786
01:50:58,239 --> 01:51:00,639
in c
2787
01:51:02,239 --> 01:51:08,400
hey yeah welcome back so we have to talk
2788
01:51:05,119 --> 01:51:10,719
about for loops a for loop repeats a
2789
01:51:08,400 --> 01:51:13,360
section of code a limited amount of
2790
01:51:10,719 --> 01:51:15,599
times here's an example let's say that
2791
01:51:13,360 --> 01:51:17,599
we would like to count the numbers 1
2792
01:51:15,599 --> 01:51:19,679
through 10. so we can actually write a
2793
01:51:17,599 --> 01:51:21,760
for loop to do that for us and here's
2794
01:51:19,679 --> 01:51:23,920
how to create one type four
2795
01:51:21,760 --> 01:51:26,560
a set of parentheses then a set of curly
2796
01:51:23,920 --> 01:51:29,280
braces anything within this set of curly
2797
01:51:26,560 --> 01:51:31,920
braces we can repeat a limited amount of
2798
01:51:29,280 --> 01:51:33,679
times but before we do so there's a few
2799
01:51:31,920 --> 01:51:35,920
statements we have to add to our for
2800
01:51:33,679 --> 01:51:38,639
loop there are three the first is that
2801
01:51:35,920 --> 01:51:40,320
we need some sort of counter or index
2802
01:51:38,639 --> 01:51:43,119
and we can actually declare that within
2803
01:51:40,320 --> 01:51:44,159
the for loop so we will declare an index
2804
01:51:43,119 --> 01:51:46,480
int
2805
01:51:44,159 --> 01:51:48,800
index and set the sql to some number
2806
01:51:46,480 --> 01:51:50,800
let's say one so we will start at one
2807
01:51:48,800 --> 01:51:53,440
and count to ten and then finish the
2808
01:51:50,800 --> 01:51:55,520
statement with a semicolon now a common
2809
01:51:53,440 --> 01:51:57,920
convention is that a lot of people will
2810
01:51:55,520 --> 01:51:59,520
shorten index to i because it's easier
2811
01:51:57,920 --> 01:52:01,599
to work with so this is the first
2812
01:51:59,520 --> 01:52:04,159
statement there's two more and the
2813
01:52:01,599 --> 01:52:06,080
second statement is a condition how long
2814
01:52:04,159 --> 01:52:08,159
should we continue repeating this code
2815
01:52:06,080 --> 01:52:12,239
so let's continue this for loop as long
2816
01:52:08,159 --> 01:52:15,360
as i our index is less than or equal to
2817
01:52:12,239 --> 01:52:17,520
10 and then add a semicolon at the end
2818
01:52:15,360 --> 01:52:19,840
so that is the second statement and the
2819
01:52:17,520 --> 01:52:22,000
third statement is that we can increment
2820
01:52:19,840 --> 01:52:25,119
or decrement our counter
2821
01:52:22,000 --> 01:52:27,599
so let's take i and increment it by one
2822
01:52:25,119 --> 01:52:32,400
after each iteration by using i plus
2823
01:52:27,599 --> 01:52:35,199
plus so let's just test this so printf
2824
01:52:32,400 --> 01:52:37,679
i'll just print the word test
2825
01:52:35,199 --> 01:52:38,560
so this should print the word test ten
2826
01:52:37,679 --> 01:52:39,840
times
2827
01:52:38,560 --> 01:52:43,480
uh then we should add a new line
2828
01:52:39,840 --> 01:52:43,480
character at the end
2829
01:52:44,000 --> 01:52:48,880
that's much better okay now let's
2830
01:52:46,000 --> 01:52:51,440
replace test with a format specifier
2831
01:52:48,880 --> 01:52:54,960
we're displaying an integer
2832
01:52:51,440 --> 01:52:57,440
and let's display i our index and this
2833
01:52:54,960 --> 01:53:00,400
should count from one to ten
2834
01:52:57,440 --> 01:53:02,000
yep just like that one through ten so
2835
01:53:00,400 --> 01:53:04,560
with this third statement we can
2836
01:53:02,000 --> 01:53:06,719
increment or decrement by one or a
2837
01:53:04,560 --> 01:53:09,840
greater number so this time let's count
2838
01:53:06,719 --> 01:53:13,199
by two so we can set this to i plus
2839
01:53:09,840 --> 01:53:15,920
equals two and we will now count up by
2840
01:53:13,199 --> 01:53:17,040
two starting from one one three five
2841
01:53:15,920 --> 01:53:18,560
seven nine
2842
01:53:17,040 --> 01:53:20,560
or how about three
2843
01:53:18,560 --> 01:53:23,199
one four seven ten you can also
2844
01:53:20,560 --> 01:53:26,800
decrement this time let's count from ten
2845
01:53:23,199 --> 01:53:29,440
to one so let's set i to equal 10
2846
01:53:26,800 --> 01:53:32,639
and we will continue this as long as i
2847
01:53:29,440 --> 01:53:36,159
is greater than or equal to 1
2848
01:53:32,639 --> 01:53:39,119
and we will decrement i minus minus
2849
01:53:36,159 --> 01:53:42,080
so this will count from 10 to 1 and then
2850
01:53:39,119 --> 01:53:45,119
stop or we could count down by two
2851
01:53:42,080 --> 01:53:47,760
i minus equals two
2852
01:53:45,119 --> 01:53:50,880
then we have ten eight six four two
2853
01:53:47,760 --> 01:53:53,199
or even three or a greater number
2854
01:53:50,880 --> 01:53:55,520
so yeah that's a for loop it repeats a
2855
01:53:53,199 --> 01:53:57,520
section of code a limited amount of
2856
01:53:55,520 --> 01:53:59,920
times there's up to three statements
2857
01:53:57,520 --> 01:54:02,639
that we can add we can declare an index
2858
01:53:59,920 --> 01:54:05,280
a counter so to say some condition that
2859
01:54:02,639 --> 01:54:07,760
we check after each iteration and then
2860
01:54:05,280 --> 01:54:09,840
some way to increment or decrement our
2861
01:54:07,760 --> 01:54:11,840
index and then place any code you would
2862
01:54:09,840 --> 01:54:14,400
like to repeat within a set of curly
2863
01:54:11,840 --> 01:54:16,000
braces so yeah that's a for loop if you
2864
01:54:14,400 --> 01:54:17,599
would like a copy of this code i will
2865
01:54:16,000 --> 01:54:20,239
post this to the comment section down
2866
01:54:17,599 --> 01:54:23,480
below and well yeah those are for loops
2867
01:54:20,239 --> 01:54:23,480
in c
2868
01:54:24,400 --> 01:54:28,400
all right we're back at it again so
2869
01:54:26,400 --> 01:54:30,960
let's talk about while loops a while
2870
01:54:28,400 --> 01:54:33,119
loop will repeat a section of code
2871
01:54:30,960 --> 01:54:36,159
possibly unlimited times we will
2872
01:54:33,119 --> 01:54:38,480
continue some section of code while some
2873
01:54:36,159 --> 01:54:40,320
condition remains true and it's possible
2874
01:54:38,480 --> 01:54:42,080
that a while loop might not execute at
2875
01:54:40,320 --> 01:54:43,760
all depending on what our condition is
2876
01:54:42,080 --> 01:54:46,000
so here's an example let's create a
2877
01:54:43,760 --> 01:54:48,719
program that will ask a user for their
2878
01:54:46,000 --> 01:54:50,239
name if they attempt to skip that prompt
2879
01:54:48,719 --> 01:54:52,159
then we will keep on asking them for
2880
01:54:50,239 --> 01:54:53,760
their name indefinitely so here's how we
2881
01:54:52,159 --> 01:54:55,840
can write a program like that using a
2882
01:54:53,760 --> 01:54:58,080
while loop we'll need to begin with a
2883
01:54:55,840 --> 01:55:00,639
character array let's say name and this
2884
01:54:58,080 --> 01:55:04,599
will be 25 bytes and we will create a
2885
01:55:00,639 --> 01:55:04,599
prompt using printf
2886
01:55:04,960 --> 01:55:09,599
what's your name we can use scanf for
2887
01:55:07,440 --> 01:55:11,840
user input but with names that may
2888
01:55:09,599 --> 01:55:13,679
contain spaces i'm going to use fgets
2889
01:55:11,840 --> 01:55:15,920
instead
2890
01:55:13,679 --> 01:55:18,239
refer to the video on user input to
2891
01:55:15,920 --> 01:55:21,280
learn more about fgets so i would like
2892
01:55:18,239 --> 01:55:23,920
to assign my variable name
2893
01:55:21,280 --> 01:55:25,280
set the size of the user input 25 to
2894
01:55:23,920 --> 01:55:29,040
match what we have
2895
01:55:25,280 --> 01:55:30,960
then type stdn for standard input then
2896
01:55:29,040 --> 01:55:33,040
we just need to get rid of the new line
2897
01:55:30,960 --> 01:55:35,040
character at the end of our user input
2898
01:55:33,040 --> 01:55:37,760
so we can write something like this
2899
01:55:35,040 --> 01:55:40,159
set our character array of name and
2900
01:55:37,760 --> 01:55:42,880
within the straight brackets we will use
2901
01:55:40,159 --> 01:55:44,840
the string length function
2902
01:55:42,880 --> 01:55:46,400
pass in name
2903
01:55:44,840 --> 01:55:49,119
-1
2904
01:55:46,400 --> 01:55:51,520
and we will set this equal to a null
2905
01:55:49,119 --> 01:55:54,880
character and then at the end we will
2906
01:55:51,520 --> 01:55:57,520
display the user's name printf
2907
01:55:54,880 --> 01:56:00,560
let's say hello and then use a format
2908
01:55:57,520 --> 01:56:02,880
specifier for strings which is s and we
2909
01:56:00,560 --> 01:56:04,159
will display a user's name
2910
01:56:02,880 --> 01:56:05,199
okay this is what this looks like
2911
01:56:04,159 --> 01:56:08,159
currently
2912
01:56:05,199 --> 01:56:09,119
what's your name i'll type my name hit
2913
01:56:08,159 --> 01:56:11,840
enter
2914
01:56:09,119 --> 01:56:14,159
and it says hello bro now what if i
2915
01:56:11,840 --> 01:56:15,280
don't type in anything like i just hit
2916
01:56:14,159 --> 01:56:17,360
enter
2917
01:56:15,280 --> 01:56:20,000
what's your name i'm going to hit enter
2918
01:56:17,360 --> 01:56:22,800
hello and then there is no name here how
2919
01:56:20,000 --> 01:56:24,719
can i force a user to type in something
2920
01:56:22,800 --> 01:56:26,800
well i could use a while loop and that
2921
01:56:24,719 --> 01:56:29,119
will prompt a user to enter their name
2922
01:56:26,800 --> 01:56:32,400
indefinitely so here's how to create a
2923
01:56:29,119 --> 01:56:34,719
while loop type while parentheses
2924
01:56:32,400 --> 01:56:36,320
curly braces and we need a condition
2925
01:56:34,719 --> 01:56:38,480
what are we going to check we will
2926
01:56:36,320 --> 01:56:40,239
continue this while loop while some
2927
01:56:38,480 --> 01:56:42,080
condition remains true
2928
01:56:40,239 --> 01:56:43,360
our condition to check to see if we have
2929
01:56:42,080 --> 01:56:46,960
an empty string
2930
01:56:43,360 --> 01:56:49,520
we could use the string length function
2931
01:56:46,960 --> 01:56:51,599
pass in name and check to see if this is
2932
01:56:49,520 --> 01:56:53,520
equal to zero
2933
01:56:51,599 --> 01:56:55,040
if it is zero that means that somebody
2934
01:56:53,520 --> 01:56:57,920
just hit enter without typing in
2935
01:56:55,040 --> 01:57:00,159
anything so let's print a message to
2936
01:56:57,920 --> 01:57:01,199
yell at them
2937
01:57:00,159 --> 01:57:04,400
you
2938
01:57:01,199 --> 01:57:06,400
did not enter your name
2939
01:57:04,400 --> 01:57:08,800
and then let's copy this section of code
2940
01:57:06,400 --> 01:57:10,480
and paste it to reprompt the user to
2941
01:57:08,800 --> 01:57:13,199
type in their name
2942
01:57:10,480 --> 01:57:15,440
and that's all there is to it so while
2943
01:57:13,199 --> 01:57:18,159
this condition remains true we will
2944
01:57:15,440 --> 01:57:19,679
execute this code forever so let's try
2945
01:57:18,159 --> 01:57:21,520
it again
2946
01:57:19,679 --> 01:57:23,599
okay what's your name i'm going to hit
2947
01:57:21,520 --> 01:57:26,960
enter you did not enter your name what's
2948
01:57:23,599 --> 01:57:28,960
your name no no no no
2949
01:57:26,960 --> 01:57:31,199
okay i give up
2950
01:57:28,960 --> 01:57:33,280
hello whatever your name is so yeah
2951
01:57:31,199 --> 01:57:36,080
that's basically a while loop it repeats
2952
01:57:33,280 --> 01:57:38,560
a section of code possibly unlimited
2953
01:57:36,080 --> 01:57:40,800
times there's a lot of overlap where you
2954
01:57:38,560 --> 01:57:43,760
could use either a for loop or a while
2955
01:57:40,800 --> 01:57:46,080
loop and we execute some body of code
2956
01:57:43,760 --> 01:57:48,480
while some condition remains true
2957
01:57:46,080 --> 01:57:51,280
however a while loop might not execute
2958
01:57:48,480 --> 01:57:53,360
at all if this condition is false to
2959
01:57:51,280 --> 01:57:56,000
begin with so if the user actually does
2960
01:57:53,360 --> 01:57:58,639
type in something well this condition is
2961
01:57:56,000 --> 01:58:00,560
false and we do not execute this body of
2962
01:57:58,639 --> 01:58:02,480
code so yeah that's a while loop it
2963
01:58:00,560 --> 01:58:04,960
repeats a section of code possibly
2964
01:58:02,480 --> 01:58:06,560
unlimited times if you would like a copy
2965
01:58:04,960 --> 01:58:09,040
of this code i will post this to the
2966
01:58:06,560 --> 01:58:12,800
comment section down below and well yeah
2967
01:58:09,040 --> 01:58:12,800
those are while loops in c
2968
01:58:14,719 --> 01:58:19,360
hey everybody so let's talk about do
2969
01:58:17,119 --> 01:58:22,080
while loops a do while loop is a
2970
01:58:19,360 --> 01:58:24,719
variation of a while loop so a while
2971
01:58:22,080 --> 01:58:27,679
loop first checks a condition
2972
01:58:24,719 --> 01:58:30,239
then it executes a block of code if that
2973
01:58:27,679 --> 01:58:32,239
condition is true so we may not even
2974
01:58:30,239 --> 01:58:34,719
execute that block of code at all if
2975
01:58:32,239 --> 01:58:37,920
that condition is false from the get go
2976
01:58:34,719 --> 01:58:40,400
a do while loop always executes a block
2977
01:58:37,920 --> 01:58:42,560
of code once and then we checking
2978
01:58:40,400 --> 01:58:45,360
condition at the end if that condition
2979
01:58:42,560 --> 01:58:46,400
is true we will continue another loop so
2980
01:58:45,360 --> 01:58:47,920
here's what i'm thinking for a
2981
01:58:46,400 --> 01:58:50,480
demonstration we'll create a small
2982
01:58:47,920 --> 01:58:52,480
program where we will have a user type
2983
01:58:50,480 --> 01:58:54,560
in as many numbers as they want as long
2984
01:58:52,480 --> 01:58:56,560
as it's above zero and then we will find
2985
01:58:54,560 --> 01:58:58,960
a sum so if we're writing a program like
2986
01:58:56,560 --> 01:59:01,119
that we can first declare two variables
2987
01:58:58,960 --> 01:59:03,040
int number i'll go ahead and assign
2988
01:59:01,119 --> 01:59:06,080
these some values right away so we'll
2989
01:59:03,040 --> 01:59:08,000
set number to zero and int sum equal to
2990
01:59:06,080 --> 01:59:10,080
zero as well to keep track of the sum
2991
01:59:08,000 --> 01:59:10,880
we'll first begin by creating a while
2992
01:59:10,080 --> 01:59:13,679
loop
2993
01:59:10,880 --> 01:59:16,400
so we will use a while loop
2994
01:59:13,679 --> 01:59:18,560
and our condition is if number
2995
01:59:16,400 --> 01:59:20,719
is greater than zero
2996
01:59:18,560 --> 01:59:22,400
then we will continue to ask the user to
2997
01:59:20,719 --> 01:59:24,880
type in some numbers
2998
01:59:22,400 --> 01:59:28,400
if that number is zero or less then we
2999
01:59:24,880 --> 01:59:30,480
stop so let's ask a user for a number
3000
01:59:28,400 --> 01:59:34,000
enter a number
3001
01:59:30,480 --> 01:59:36,320
above zero and then using scanf we will
3002
01:59:34,000 --> 01:59:39,040
accept some user input
3003
01:59:36,320 --> 01:59:40,639
and store this within number and let's
3004
01:59:39,040 --> 01:59:41,599
check to see if
3005
01:59:40,639 --> 01:59:44,639
number
3006
01:59:41,599 --> 01:59:47,040
is greater than zero
3007
01:59:44,639 --> 01:59:48,000
if it is we will take sum set this equal
3008
01:59:47,040 --> 01:59:50,239
to sum
3009
01:59:48,000 --> 01:59:53,360
plus number but you can just shorten the
3010
01:59:50,239 --> 01:59:56,400
statement to plus equals number that's
3011
01:59:53,360 --> 01:59:58,719
fine too and then at the end we will
3012
01:59:56,400 --> 01:59:59,520
print whatever the sum is
3013
01:59:58,719 --> 02:00:01,520
sum
3014
01:59:59,520 --> 02:00:04,000
we're displaying an integer
3015
02:00:01,520 --> 02:00:06,800
and we are displaying some so with our
3016
02:00:04,000 --> 02:00:08,560
number i set this to zero and since our
3017
02:00:06,800 --> 02:00:10,400
while loop is checking the condition
3018
02:00:08,560 --> 02:00:13,360
first we're not actually going to
3019
02:00:10,400 --> 02:00:15,679
execute this body of code
3020
02:00:13,360 --> 02:00:17,119
so our program skipped this while loop
3021
02:00:15,679 --> 02:00:19,440
and went straight to the printf
3022
02:00:17,119 --> 02:00:21,920
statement so a while loop checks a
3023
02:00:19,440 --> 02:00:24,560
condition first a do while loop will
3024
02:00:21,920 --> 02:00:26,320
check a condition last so to change this
3025
02:00:24,560 --> 02:00:27,440
to a do while loop we will take our
3026
02:00:26,320 --> 02:00:29,199
condition
3027
02:00:27,440 --> 02:00:31,679
and move it to the end
3028
02:00:29,199 --> 02:00:34,000
just after the last curly brace and add
3029
02:00:31,679 --> 02:00:36,800
a semicolon and then before the first
3030
02:00:34,000 --> 02:00:39,280
curly brace we will add the word do so
3031
02:00:36,800 --> 02:00:40,800
we will do this once and then check the
3032
02:00:39,280 --> 02:00:43,520
condition to see if we would like to
3033
02:00:40,800 --> 02:00:46,000
continue so now if i run this
3034
02:00:43,520 --> 02:00:48,159
we get that prompt enter a number above
3035
02:00:46,000 --> 02:00:52,239
zero and i can type in as many numbers
3036
02:00:48,159 --> 02:00:54,400
as i want so one two three four five so
3037
02:00:52,239 --> 02:00:57,119
we check the condition at the end
3038
02:00:54,400 --> 02:00:59,119
so our condition is that if our number
3039
02:00:57,119 --> 02:01:01,840
is greater than zero we will continue
3040
02:00:59,119 --> 02:01:03,360
this loop so if i type in negative one
3041
02:01:01,840 --> 02:01:06,320
we will exit
3042
02:01:03,360 --> 02:01:08,480
and our sum is 15. so that's the major
3043
02:01:06,320 --> 02:01:10,400
difference between a while loop and a do
3044
02:01:08,480 --> 02:01:13,119
while loop a while loop checks a
3045
02:01:10,400 --> 02:01:15,599
condition first then executes a block of
3046
02:01:13,119 --> 02:01:17,920
code if that condition is true a do
3047
02:01:15,599 --> 02:01:19,119
while loop always executes a block of
3048
02:01:17,920 --> 02:01:20,960
code once
3049
02:01:19,119 --> 02:01:23,520
then checks a condition if that
3050
02:01:20,960 --> 02:01:25,920
condition is true then we continue again
3051
02:01:23,520 --> 02:01:28,000
so yeah that's a do while loop it's a
3052
02:01:25,920 --> 02:01:29,760
variation of a while loop and if you
3053
02:01:28,000 --> 02:01:31,520
would like a copy of this code i'll post
3054
02:01:29,760 --> 02:01:37,159
this to the comments section down below
3055
02:01:31,520 --> 02:01:37,159
and well that's the do while loop in c
3056
02:01:37,840 --> 02:01:43,840
hey welcome back so nested loops a
3057
02:01:40,960 --> 02:01:45,520
nested loop is a loop inside of another
3058
02:01:43,840 --> 02:01:47,599
loop when you'll encounter them it's
3059
02:01:45,520 --> 02:01:49,440
really situational so i don't really
3060
02:01:47,599 --> 02:01:51,920
have one good example but what i'm
3061
02:01:49,440 --> 02:01:54,080
thinking we'll do is use a nested loop
3062
02:01:51,920 --> 02:01:56,159
for an exercise what i'm thinking is
3063
02:01:54,080 --> 02:01:58,800
that we'll let a user type in a number
3064
02:01:56,159 --> 02:02:01,360
of rows and columns and a symbol and
3065
02:01:58,800 --> 02:02:03,199
print a rectangle of that given symbol
3066
02:02:01,360 --> 02:02:05,520
but the user is going to specify a
3067
02:02:03,199 --> 02:02:07,199
number of rows and columns so let's
3068
02:02:05,520 --> 02:02:09,760
begin by declaring all of the different
3069
02:02:07,199 --> 02:02:12,000
variables that we'll need intros and
3070
02:02:09,760 --> 02:02:14,159
columns and char
3071
02:02:12,000 --> 02:02:18,000
symbol we'll let the user type in a
3072
02:02:14,159 --> 02:02:20,960
number of rows and columns
3073
02:02:18,000 --> 02:02:23,920
enter number of rows
3074
02:02:20,960 --> 02:02:27,280
then use scanf to accept some user input
3075
02:02:23,920 --> 02:02:30,840
we are accepting an integer so use d for
3076
02:02:27,280 --> 02:02:35,360
the format specifier address of operator
3077
02:02:30,840 --> 02:02:35,360
rows then do the same thing with columns
3078
02:02:36,239 --> 02:02:42,320
enter number of columns
3079
02:02:40,159 --> 02:02:44,880
let's create our nested loops you can
3080
02:02:42,320 --> 02:02:46,880
use either for loops or while loops it's
3081
02:02:44,880 --> 02:02:49,199
just the concept of one loop inside of
3082
02:02:46,880 --> 02:02:51,920
another so let's use for loops for our
3083
02:02:49,199 --> 02:02:53,920
outer loop and inner loop
3084
02:02:51,920 --> 02:02:56,239
the outer loop is in charge of keeping
3085
02:02:53,920 --> 02:02:57,599
track of the rows the inner loop will be
3086
02:02:56,239 --> 02:03:00,560
in charge of keeping track of the
3087
02:02:57,599 --> 02:03:03,199
columns so i need this outer for loop to
3088
02:03:00,560 --> 02:03:05,360
iterate once for every row that we have
3089
02:03:03,199 --> 02:03:08,639
so we could write something like this
3090
02:03:05,360 --> 02:03:11,760
int i set this equal to one i need to
3091
02:03:08,639 --> 02:03:15,040
continue this for loop as long as i is
3092
02:03:11,760 --> 02:03:17,520
less than or equal to rows
3093
02:03:15,040 --> 02:03:20,080
and then increment i by one after each
3094
02:03:17,520 --> 02:03:22,400
iteration so now let's create a nested
3095
02:03:20,080 --> 02:03:24,159
for loop we will declare a loop inside
3096
02:03:22,400 --> 02:03:26,239
of another loop
3097
02:03:24,159 --> 02:03:28,800
and this inner for loop is in charge of
3098
02:03:26,239 --> 02:03:30,800
the columns we should probably not reuse
3099
02:03:28,800 --> 02:03:33,360
our index of i so let's create a new
3100
02:03:30,800 --> 02:03:36,320
index and a common naming convention for
3101
02:03:33,360 --> 02:03:38,400
an inner for loop is to use j because j
3102
02:03:36,320 --> 02:03:41,840
comes after i in the alphabet
3103
02:03:38,400 --> 02:03:44,960
so i will set int j equal to one we will
3104
02:03:41,840 --> 02:03:47,920
continue this as long as j is less than
3105
02:03:44,960 --> 02:03:50,800
or equal to columns
3106
02:03:47,920 --> 02:03:52,880
then increment j by one for the time
3107
02:03:50,800 --> 02:03:55,840
being until we let a user type in a
3108
02:03:52,880 --> 02:03:59,040
symbol let's just print our index so
3109
02:03:55,840 --> 02:03:59,040
let's use printf
3110
02:03:59,360 --> 02:04:04,800
we're displaying an integer
3111
02:04:01,920 --> 02:04:06,639
and let's display j
3112
02:04:04,800 --> 02:04:08,880
and let's take a look at this
3113
02:04:06,639 --> 02:04:11,119
okay enter number of rows how about 3
3114
02:04:08,880 --> 02:04:13,840
rows and five columns
3115
02:04:11,119 --> 02:04:16,960
so here's our output we have the numbers
3116
02:04:13,840 --> 02:04:19,440
one through five three times
3117
02:04:16,960 --> 02:04:21,360
so to make this more of a rectangle a
3118
02:04:19,440 --> 02:04:24,719
grid i'm going to add a new line
3119
02:04:21,360 --> 02:04:27,040
character whenever we finish a row
3120
02:04:24,719 --> 02:04:31,040
so printf
3121
02:04:27,040 --> 02:04:33,840
newline character let's try that again
3122
02:04:31,040 --> 02:04:35,280
enter number of rows three number of
3123
02:04:33,840 --> 02:04:38,480
columns five
3124
02:04:35,280 --> 02:04:40,400
so we have three rows and five columns
3125
02:04:38,480 --> 02:04:42,960
basically speaking to complete one
3126
02:04:40,400 --> 02:04:45,360
iteration of our outer loop we have to
3127
02:04:42,960 --> 02:04:47,440
escape our inner loop first
3128
02:04:45,360 --> 02:04:49,679
once this condition is no longer true
3129
02:04:47,440 --> 02:04:51,920
then we will escape the inner loop and
3130
02:04:49,679 --> 02:04:54,159
complete one iteration of the outer for
3131
02:04:51,920 --> 02:04:56,239
loop but then once we begin in the next
3132
02:04:54,159 --> 02:04:58,560
iteration of our for loop we're stuck
3133
02:04:56,239 --> 02:05:00,800
back within our inner for loop again so
3134
02:04:58,560 --> 02:05:03,280
that's kind of the concept now this time
3135
02:05:00,800 --> 02:05:07,400
let's let a user type in a symbol and we
3136
02:05:03,280 --> 02:05:07,400
will create a sort of rectangle
3137
02:05:07,440 --> 02:05:14,639
enter a symbol to use then scanf we are
3138
02:05:12,079 --> 02:05:17,599
accepting a character so use the c
3139
02:05:14,639 --> 02:05:20,159
format specifier address of operator
3140
02:05:17,599 --> 02:05:22,880
our symbol variable
3141
02:05:20,159 --> 02:05:24,480
now we're going to replace j with our
3142
02:05:22,880 --> 02:05:26,880
symbol
3143
02:05:24,480 --> 02:05:27,840
and the format specifier for a character
3144
02:05:26,880 --> 02:05:30,960
is c
3145
02:05:27,840 --> 02:05:30,960
then let's try this again
3146
02:05:31,119 --> 02:05:37,199
so how about three rows and six columns
3147
02:05:35,280 --> 02:05:38,800
okay here's the issue so we have all of
3148
02:05:37,199 --> 02:05:40,639
this empty space
3149
02:05:38,800 --> 02:05:43,599
now when we entered our number of
3150
02:05:40,639 --> 02:05:45,599
columns after hitting enter we have the
3151
02:05:43,599 --> 02:05:48,480
new line character within our input
3152
02:05:45,599 --> 02:05:50,480
buffer so our next scana function
3153
02:05:48,480 --> 02:05:52,960
actually picked that up so what we need
3154
02:05:50,480 --> 02:05:54,239
to do is clear our buffer and one simple
3155
02:05:52,960 --> 02:05:56,560
way of doing that there's a couple
3156
02:05:54,239 --> 02:05:58,560
different ways is that we can just use
3157
02:05:56,560 --> 02:06:00,880
scanf again
3158
02:05:58,560 --> 02:06:02,800
and we will read a character and that's
3159
02:06:00,880 --> 02:06:04,800
one way to do it basically with this
3160
02:06:02,800 --> 02:06:06,960
line we're just getting rid of the new
3161
02:06:04,800 --> 02:06:08,800
line character after the last time we
3162
02:06:06,960 --> 02:06:10,880
use scanf because that's still within
3163
02:06:08,800 --> 02:06:13,679
our buffer okay let's try this one last
3164
02:06:10,880 --> 02:06:16,719
time what about four rows
3165
02:06:13,679 --> 02:06:18,560
and five columns enter a symbol to use
3166
02:06:16,719 --> 02:06:21,040
uh how about the dollar sign
3167
02:06:18,560 --> 02:06:24,400
there we go here's our rectangle it has
3168
02:06:21,040 --> 02:06:26,719
five columns and four rows so yeah
3169
02:06:24,400 --> 02:06:28,800
that's basically a nested loop it's a
3170
02:06:26,719 --> 02:06:30,239
loop inside of another loop and when
3171
02:06:28,800 --> 02:06:32,400
you'll encounter them it's really
3172
02:06:30,239 --> 02:06:34,560
situational i thought this would be good
3173
02:06:32,400 --> 02:06:37,360
practice to understand how they work to
3174
02:06:34,560 --> 02:06:40,079
complete one iteration of the outer loop
3175
02:06:37,360 --> 02:06:41,920
you have to first escape the inner loop
3176
02:06:40,079 --> 02:06:44,079
and that may involve completing all
3177
02:06:41,920 --> 02:06:45,840
iterations of the inner loop so yeah
3178
02:06:44,079 --> 02:06:47,679
those are nested loops if you would like
3179
02:06:45,840 --> 02:06:49,840
a copy of this code i will post this to
3180
02:06:47,679 --> 02:06:54,000
the comment section down below and well
3181
02:06:49,840 --> 02:06:54,000
yeah those are nested loops in c
3182
02:06:55,679 --> 02:06:59,920
hey people i have a super quick video
3183
02:06:58,000 --> 02:07:02,400
for you today on the differences between
3184
02:06:59,920 --> 02:07:04,480
the continue and break statements so
3185
02:07:02,400 --> 02:07:06,960
when using either statements within the
3186
02:07:04,480 --> 02:07:09,280
context of a loop a continue statement
3187
02:07:06,960 --> 02:07:11,679
will skip the rest of a section of code
3188
02:07:09,280 --> 02:07:13,920
and force the next iteration of a loop a
3189
02:07:11,679 --> 02:07:15,920
break statement will exit out of a loop
3190
02:07:13,920 --> 02:07:18,000
entirely another place where you see
3191
02:07:15,920 --> 02:07:20,239
break statements is when used within a
3192
02:07:18,000 --> 02:07:21,840
switch after each case so here's an
3193
02:07:20,239 --> 02:07:24,320
example let's say that we would like to
3194
02:07:21,840 --> 02:07:26,320
count the numbers 1 through 20 but i
3195
02:07:24,320 --> 02:07:28,880
would like to skip the number 13 because
3196
02:07:26,320 --> 02:07:30,639
13 is considered an unlucky number so if
3197
02:07:28,880 --> 02:07:32,239
i were to write a program like that that
3198
02:07:30,639 --> 02:07:35,199
would look something like this
3199
02:07:32,239 --> 02:07:38,239
so let's use a for loop and i will set
3200
02:07:35,199 --> 02:07:41,280
an index of i equal to one continue this
3201
02:07:38,239 --> 02:07:43,599
as long as i is less than or equal to 20
3202
02:07:41,280 --> 02:07:46,159
and then increment i by one then let's
3203
02:07:43,599 --> 02:07:47,760
print our index so printf we're
3204
02:07:46,159 --> 02:07:49,920
displaying an integer
3205
02:07:47,760 --> 02:07:51,840
and let's display i i'm just going to
3206
02:07:49,920 --> 02:07:54,159
add a new line character after each
3207
02:07:51,840 --> 02:07:56,079
iteration when we use a printf statement
3208
02:07:54,159 --> 02:07:59,199
okay so this is what this looks like we
3209
02:07:56,079 --> 02:08:01,760
have the numbers 1 through 20. so if i
3210
02:07:59,199 --> 02:08:03,760
would like to skip the number 13 i can
3211
02:08:01,760 --> 02:08:06,880
use a continue statement but we need to
3212
02:08:03,760 --> 02:08:09,679
check to see if i is equal to 13 we can
3213
02:08:06,880 --> 02:08:10,480
use an if statement
3214
02:08:09,679 --> 02:08:13,040
so
3215
02:08:10,480 --> 02:08:16,239
if i is equal to 13
3216
02:08:13,040 --> 02:08:17,920
then we will continue
3217
02:08:16,239 --> 02:08:21,280
and let's take a look so we have the
3218
02:08:17,920 --> 02:08:24,320
numbers 1 through 20 but we are missing
3219
02:08:21,280 --> 02:08:26,960
13 so a continue statement will skip the
3220
02:08:24,320 --> 02:08:29,679
rest of a section of code and force the
3221
02:08:26,960 --> 02:08:31,920
next iteration of a loop now if this was
3222
02:08:29,679 --> 02:08:36,560
a break statement this will break out of
3223
02:08:31,920 --> 02:08:38,400
our loop entirely so once we reach 13
3224
02:08:36,560 --> 02:08:40,560
then we will exit out of this loop
3225
02:08:38,400 --> 02:08:43,840
entirely so here we only have the
3226
02:08:40,560 --> 02:08:46,000
numbers 1 through 12 and then we break
3227
02:08:43,840 --> 02:08:47,920
we exit out of the loop so that's a
3228
02:08:46,000 --> 02:08:50,639
quick demonstration of the differences
3229
02:08:47,920 --> 02:08:52,320
between continue and break the continue
3230
02:08:50,639 --> 02:08:54,400
statement will skip the rest of a
3231
02:08:52,320 --> 02:08:56,800
section of code and force the next
3232
02:08:54,400 --> 02:08:59,360
iteration of a loop a break statement
3233
02:08:56,800 --> 02:09:01,119
exits out of a loop entirely so yeah
3234
02:08:59,360 --> 02:09:03,280
that's a super quick video on the
3235
02:09:01,119 --> 02:09:04,960
differences between the continue and
3236
02:09:03,280 --> 02:09:06,719
break statements if you would like a
3237
02:09:04,960 --> 02:09:08,560
copy of this code i'll post this to the
3238
02:09:06,719 --> 02:09:10,159
comments section down below and well
3239
02:09:08,560 --> 02:09:11,760
yeah those are the major differences
3240
02:09:10,159 --> 02:09:14,880
between the continue and break
3241
02:09:11,760 --> 02:09:14,880
statements in c
3242
02:09:16,000 --> 02:09:21,440
hey everybody let's talk about arrays an
3243
02:09:18,400 --> 02:09:24,079
array is a data structure that can store
3244
02:09:21,440 --> 02:09:26,560
many values of the same data type here's
3245
02:09:24,079 --> 02:09:28,639
an example let's say i have a variable
3246
02:09:26,560 --> 02:09:31,199
named price and i have the price of
3247
02:09:28,639 --> 02:09:33,360
maybe one item in a fictional store of
3248
02:09:31,199 --> 02:09:35,840
ours so by using an array we can
3249
02:09:33,360 --> 02:09:38,079
actually store more than one value but
3250
02:09:35,840 --> 02:09:40,480
it has to be of the same data type so i
3251
02:09:38,079 --> 02:09:43,199
can turn this variable price into an
3252
02:09:40,480 --> 02:09:45,520
array and i can do that by following the
3253
02:09:43,199 --> 02:09:47,920
name of the variable and add a set of
3254
02:09:45,520 --> 02:09:50,239
straight brackets and then any values i
3255
02:09:47,920 --> 02:09:52,800
would like to add to this array i will
3256
02:09:50,239 --> 02:09:54,800
surround with a set of curly braces and
3257
02:09:52,800 --> 02:09:57,119
there we go we have turned our variable
3258
02:09:54,800 --> 02:09:59,360
price into an array but i'm going to
3259
02:09:57,119 --> 02:10:01,119
change the name to prices because i
3260
02:09:59,360 --> 02:10:03,440
think it would be more descriptive
3261
02:10:01,119 --> 02:10:06,159
because we can store more than one value
3262
02:10:03,440 --> 02:10:08,480
now so we can add multiple values each
3263
02:10:06,159 --> 02:10:11,440
separated with a comma so let's say we
3264
02:10:08,480 --> 02:10:13,040
have another price that is 10
3265
02:10:11,440 --> 02:10:16,560
15
3266
02:10:13,040 --> 02:10:18,960
25 and maybe 20. now you may have
3267
02:10:16,560 --> 02:10:20,480
noticed some parallels when we create a
3268
02:10:18,960 --> 02:10:22,960
string it's really an array of
3269
02:10:20,480 --> 02:10:25,040
characters so if i need a name we would
3270
02:10:22,960 --> 02:10:27,360
say the data type is char
3271
02:10:25,040 --> 02:10:29,840
and we will create a name array followed
3272
02:10:27,360 --> 02:10:32,079
by a set of square brackets and i will
3273
02:10:29,840 --> 02:10:34,639
set this equal to some amount of
3274
02:10:32,079 --> 02:10:37,119
characters a string is really just an
3275
02:10:34,639 --> 02:10:39,760
array of individual characters and with
3276
02:10:37,119 --> 02:10:42,560
my name here i have three individual
3277
02:10:39,760 --> 02:10:45,119
elements three individual values now if
3278
02:10:42,560 --> 02:10:47,599
i need to access one of these values i
3279
02:10:45,119 --> 02:10:50,000
need to use an index number so let's
3280
02:10:47,599 --> 02:10:51,679
print one of these numbers
3281
02:10:50,000 --> 02:10:53,840
so printf
3282
02:10:51,679 --> 02:10:56,239
i'm going to use the format specifier
3283
02:10:53,840 --> 02:10:57,360
for a double and i'm going to list my
3284
02:10:56,239 --> 02:10:59,920
array name
3285
02:10:57,360 --> 02:11:02,480
prices then follow this with a set of
3286
02:10:59,920 --> 02:11:05,119
straight brackets then i need an index
3287
02:11:02,480 --> 02:11:07,760
number so each spot within an array is
3288
02:11:05,119 --> 02:11:10,480
known as an element so we have five
3289
02:11:07,760 --> 02:11:12,320
elements within our array and to access
3290
02:11:10,480 --> 02:11:14,800
one of these elements i need to list an
3291
02:11:12,320 --> 02:11:17,119
index number the first element has an
3292
02:11:14,800 --> 02:11:19,679
index number of zero because computers
3293
02:11:17,119 --> 02:11:22,159
always start with zero so if i display
3294
02:11:19,679 --> 02:11:24,000
prices at index zero
3295
02:11:22,159 --> 02:11:25,679
this contains the number five i'm
3296
02:11:24,000 --> 02:11:28,159
actually going to format this let's
3297
02:11:25,679 --> 02:11:29,760
display it two digits after the decimal
3298
02:11:28,159 --> 02:11:31,360
and i'll precede this number with a
3299
02:11:29,760 --> 02:11:33,360
dollar sign
3300
02:11:31,360 --> 02:11:36,159
much better all right so then if i
3301
02:11:33,360 --> 02:11:38,239
display prices at index one that is
3302
02:11:36,159 --> 02:11:39,840
technically our second number of ten
3303
02:11:38,239 --> 02:11:41,920
dollars
3304
02:11:39,840 --> 02:11:44,639
so prices at index two
3305
02:11:41,920 --> 02:11:46,719
is fifteen three
3306
02:11:44,639 --> 02:11:49,280
is twenty five
3307
02:11:46,719 --> 02:11:52,239
and four which is our last element
3308
02:11:49,280 --> 02:11:54,800
is twenty so arrays they have a fixed
3309
02:11:52,239 --> 02:11:56,960
size we can't change the size after we
3310
02:11:54,800 --> 02:11:58,800
compile and run our program another
3311
02:11:56,960 --> 02:12:00,400
thing that you can do too if you do not
3312
02:11:58,800 --> 02:12:02,239
know what values you would like to
3313
02:12:00,400 --> 02:12:04,000
initialize your array with you can
3314
02:12:02,239 --> 02:12:06,719
always just set a size let's say i would
3315
02:12:04,000 --> 02:12:09,040
like a size of five elements but then
3316
02:12:06,719 --> 02:12:11,040
i'll assign some values later
3317
02:12:09,040 --> 02:12:12,159
so later on in our program we can say
3318
02:12:11,040 --> 02:12:14,960
prices
3319
02:12:12,159 --> 02:12:17,920
at whatever index let's say index zero
3320
02:12:14,960 --> 02:12:20,880
i'm going to assign with five and i'll
3321
02:12:17,920 --> 02:12:25,599
do the same thing for the other elements
3322
02:12:20,880 --> 02:12:30,159
so prices at index one is maybe ten
3323
02:12:25,599 --> 02:12:30,159
prices at index two is fifteen
3324
02:12:30,639 --> 02:12:37,440
three will be twenty five
3325
02:12:34,800 --> 02:12:38,960
and 4 will be 20.
3326
02:12:37,440 --> 02:12:41,440
that's another way in which you can
3327
02:12:38,960 --> 02:12:44,480
initialize an array you can always set a
3328
02:12:41,440 --> 02:12:46,639
size and then add elements later another
3329
02:12:44,480 --> 02:12:48,960
option is that you could combine the two
3330
02:12:46,639 --> 02:12:51,599
we could set a size right away so maybe
3331
02:12:48,960 --> 02:12:53,760
prices will have a size of 10 and we can
3332
02:12:51,599 --> 02:12:56,239
right off the bat assign some values
3333
02:12:53,760 --> 02:12:58,079
right away so we have five elements that
3334
02:12:56,239 --> 02:13:00,159
are currently filled but then the other
3335
02:12:58,079 --> 02:13:01,280
five are empty so there's a few
3336
02:13:00,159 --> 02:13:03,360
different ways in which you can
3337
02:13:01,280 --> 02:13:05,679
initialize an array so yeah that's
3338
02:13:03,360 --> 02:13:08,239
basically an array it's a data structure
3339
02:13:05,679 --> 02:13:10,480
that can store many values of the same
3340
02:13:08,239 --> 02:13:12,560
data type initializing an array is much
3341
02:13:10,480 --> 02:13:14,800
like creating a variable except you will
3342
02:13:12,560 --> 02:13:16,320
add a set of straight brackets after and
3343
02:13:14,800 --> 02:13:18,480
then you can either add some values
3344
02:13:16,320 --> 02:13:21,040
right away or later on in your program
3345
02:13:18,480 --> 02:13:22,800
but you need to list an index number of
3346
02:13:21,040 --> 02:13:25,360
where you're placing a value within the
3347
02:13:22,800 --> 02:13:27,280
array and then to access a value within
3348
02:13:25,360 --> 02:13:29,199
your array you type the array name
3349
02:13:27,280 --> 02:13:31,520
followed by an index number kind of like
3350
02:13:29,199 --> 02:13:33,199
a parking spot number so yeah those are
3351
02:13:31,520 --> 02:13:34,880
arrays in the next video i'm going to
3352
02:13:33,199 --> 02:13:37,520
show you how we can iterate over the
3353
02:13:34,880 --> 02:13:39,040
elements in an array using a for loop if
3354
02:13:37,520 --> 02:13:40,560
you would like a copy of this code i
3355
02:13:39,040 --> 02:13:42,560
will post this to the comments section
3356
02:13:40,560 --> 02:13:46,480
down below and well yeah that's an
3357
02:13:42,560 --> 02:13:46,480
introduction to arrays in c
3358
02:13:48,560 --> 02:13:52,480
hey welcome back everybody in this video
3359
02:13:50,960 --> 02:13:54,320
i'm going to show you how we can loop
3360
02:13:52,480 --> 02:13:57,040
through and print the elements of an
3361
02:13:54,320 --> 02:13:59,199
array so i have an array of doubles
3362
02:13:57,040 --> 02:14:01,840
named prices and i just made up some
3363
02:13:59,199 --> 02:14:04,719
fictional prices to display one of these
3364
02:14:01,840 --> 02:14:06,480
elements i can use a printf statement i
3365
02:14:04,719 --> 02:14:08,560
will use the appropriate format
3366
02:14:06,480 --> 02:14:10,719
specifier for the elements displayed
3367
02:14:08,560 --> 02:14:12,960
within this array since this array
3368
02:14:10,719 --> 02:14:15,599
contains doubles i will use the format
3369
02:14:12,960 --> 02:14:17,119
specifier for a double then to access
3370
02:14:15,599 --> 02:14:19,199
one of these elements i will type the
3371
02:14:17,119 --> 02:14:21,840
name of my array followed by a set of
3372
02:14:19,199 --> 02:14:24,400
straight brackets then an index number
3373
02:14:21,840 --> 02:14:26,719
so the first element in an array is zero
3374
02:14:24,400 --> 02:14:28,239
because computers always start with zero
3375
02:14:26,719 --> 02:14:30,560
then if i need to display the other
3376
02:14:28,239 --> 02:14:34,880
elements well i would have to manually
3377
02:14:30,560 --> 02:14:37,679
type prices at index zero then one two
3378
02:14:34,880 --> 02:14:39,360
three four so on and so forth so this
3379
02:14:37,679 --> 02:14:41,199
does work if you have a couple elements
3380
02:14:39,360 --> 02:14:43,280
but what if you have hundreds this
3381
02:14:41,199 --> 02:14:45,520
method is not going to be practical a
3382
02:14:43,280 --> 02:14:47,760
better solution is that we could make a
3383
02:14:45,520 --> 02:14:49,920
for loop and this for loop will iterate
3384
02:14:47,760 --> 02:14:52,639
once and display each element of this
3385
02:14:49,920 --> 02:14:55,199
array so let's create a for loop for
3386
02:14:52,639 --> 02:14:57,840
parentheses curly braces and we'll need
3387
02:14:55,199 --> 02:14:58,880
an index int i i'll set this equal to
3388
02:14:57,840 --> 02:15:00,639
zero
3389
02:14:58,880 --> 02:15:03,840
then we'll need a condition for the time
3390
02:15:00,639 --> 02:15:05,440
being let's set i less than five but
3391
02:15:03,840 --> 02:15:06,960
later on we're going to replace five
3392
02:15:05,440 --> 02:15:09,199
with something else and i'll explain
3393
02:15:06,960 --> 02:15:12,159
that later and then we will increment i
3394
02:15:09,199 --> 02:15:14,880
by one so then during each iteration
3395
02:15:12,159 --> 02:15:17,360
let's use this print statement again
3396
02:15:14,880 --> 02:15:19,760
and make one change instead of setting
3397
02:15:17,360 --> 02:15:21,679
an index number let's use our index
3398
02:15:19,760 --> 02:15:23,760
instead then i'm just going to add a new
3399
02:15:21,679 --> 02:15:24,880
line character after each printf
3400
02:15:23,760 --> 02:15:26,719
statement
3401
02:15:24,880 --> 02:15:28,719
so here we go this for loop should
3402
02:15:26,719 --> 02:15:30,719
iterate five times
3403
02:15:28,719 --> 02:15:32,000
and it will display the elements within
3404
02:15:30,719 --> 02:15:35,599
our array
3405
02:15:32,000 --> 02:15:37,280
so we have 5 10 15 25 20. maybe i'll add
3406
02:15:35,599 --> 02:15:40,000
a couple extra things to this format
3407
02:15:37,280 --> 02:15:41,360
specifier let's display two digits and a
3408
02:15:40,000 --> 02:15:43,360
dollar sign
3409
02:15:41,360 --> 02:15:45,599
that's much better so this kind of
3410
02:15:43,360 --> 02:15:46,960
resembles maybe a receipt of some sort
3411
02:15:45,599 --> 02:15:49,920
like somebody just bought a bunch of
3412
02:15:46,960 --> 02:15:52,000
items okay now here's one situation what
3413
02:15:49,920 --> 02:15:54,719
if we add or remove elements from this
3414
02:15:52,000 --> 02:15:57,520
array let's say that a user purchases
3415
02:15:54,719 --> 02:15:59,599
one more item for thirty dollars now the
3416
02:15:57,520 --> 02:16:02,239
way that this for loop is written it's
3417
02:15:59,599 --> 02:16:05,199
not going to display this last element
3418
02:16:02,239 --> 02:16:07,599
because we set this for loop to iterate
3419
02:16:05,199 --> 02:16:10,000
as long as i is less than or equal to
3420
02:16:07,599 --> 02:16:12,560
five it's not going to display this last
3421
02:16:10,000 --> 02:16:14,320
element so we would need some way to
3422
02:16:12,560 --> 02:16:16,320
update this condition
3423
02:16:14,320 --> 02:16:18,960
a better solution instead of using a
3424
02:16:16,320 --> 02:16:21,199
number here is that we could calculate
3425
02:16:18,960 --> 02:16:23,920
the amount of elements within our array
3426
02:16:21,199 --> 02:16:26,000
and loop that many times and one way in
3427
02:16:23,920 --> 02:16:28,800
which we could do that is to use the
3428
02:16:26,000 --> 02:16:30,960
size of operator so let me demonstrate
3429
02:16:28,800 --> 02:16:34,240
the size of operator real quick it will
3430
02:16:30,960 --> 02:16:35,920
return the size of an operand in bytes
3431
02:16:34,240 --> 02:16:37,679
so let's print this
3432
02:16:35,920 --> 02:16:41,040
so this will be an integer
3433
02:16:37,679 --> 02:16:44,160
and we will use the size of operator and
3434
02:16:41,040 --> 02:16:46,399
pass in our array of prices so this size
3435
02:16:44,160 --> 02:16:50,639
is going to be in bytes
3436
02:16:46,399 --> 02:16:53,599
so the size of our ray in bytes is 48
3437
02:16:50,639 --> 02:16:56,800
bytes we have six elements made up of
3438
02:16:53,599 --> 02:17:00,000
doubles each double uses up eight bytes
3439
02:16:56,800 --> 02:17:04,399
so six times eight is 48 so now let's
3440
02:17:00,000 --> 02:17:05,679
use the size of operator instead size of
3441
02:17:04,399 --> 02:17:07,599
prices
3442
02:17:05,679 --> 02:17:09,840
but we are going to divide the size of
3443
02:17:07,599 --> 02:17:11,280
our array by the size of one of these
3444
02:17:09,840 --> 02:17:13,040
elements they're all going to be
3445
02:17:11,280 --> 02:17:16,080
consistent because they all have the
3446
02:17:13,040 --> 02:17:18,319
same data type so we will take size of
3447
02:17:16,080 --> 02:17:20,559
prices divided by
3448
02:17:18,319 --> 02:17:24,000
the size of one of these elements let's
3449
02:17:20,559 --> 02:17:26,800
say prices at index zero
3450
02:17:24,000 --> 02:17:29,599
so 48 divided by
3451
02:17:26,800 --> 02:17:32,240
eight equals six we will iterate through
3452
02:17:29,599 --> 02:17:37,120
this for loop six times and here's our
3453
02:17:32,240 --> 02:17:39,599
array we have 5 10 15 25 20 30 and we
3454
02:17:37,120 --> 02:17:41,519
can add or remove elements freely and
3455
02:17:39,599 --> 02:17:43,280
there's no need to update our code
3456
02:17:41,519 --> 02:17:44,479
because we may not remember to do that
3457
02:17:43,280 --> 02:17:46,719
later on
3458
02:17:44,479 --> 02:17:48,880
so yeah that's one way to loop through
3459
02:17:46,719 --> 02:17:51,519
and print the elements of an array you
3460
02:17:48,880 --> 02:17:53,679
can use a for loop then use a printf
3461
02:17:51,519 --> 02:17:55,920
statement and for the index of your
3462
02:17:53,679 --> 02:17:57,840
array you can use the index of your for
3463
02:17:55,920 --> 02:18:00,080
loop then if you would like your for
3464
02:17:57,840 --> 02:18:02,160
loop to calculate how many times it's
3465
02:18:00,080 --> 02:18:05,120
going to iterate through this array you
3466
02:18:02,160 --> 02:18:08,639
can use the size of operator pass in
3467
02:18:05,120 --> 02:18:10,639
prices divided by the size of one of the
3468
02:18:08,639 --> 02:18:12,960
elements and that will calculate how
3469
02:18:10,639 --> 02:18:14,960
many elements are within your array
3470
02:18:12,960 --> 02:18:16,880
so yeah that's one way to loop through
3471
02:18:14,960 --> 02:18:18,719
the elements of an array if you would
3472
02:18:16,880 --> 02:18:20,639
like a copy of this code i'll post this
3473
02:18:18,719 --> 02:18:22,880
to the comment section down below and
3474
02:18:20,639 --> 02:18:27,040
well yeah that's how to loop through and
3475
02:18:22,880 --> 02:18:27,040
print the elements of an array in c
3476
02:18:28,160 --> 02:18:33,599
hey everyone two dimensional arrays it's
3477
02:18:30,559 --> 02:18:36,399
an array where each element is an entire
3478
02:18:33,599 --> 02:18:39,359
array it's useful if you need a matrix
3479
02:18:36,399 --> 02:18:41,760
grid or table of data let's begin by
3480
02:18:39,359 --> 02:18:44,639
creating a simple one-dimensional array
3481
02:18:41,760 --> 02:18:45,679
of maybe some numbers so the data type
3482
02:18:44,639 --> 02:18:48,160
is int
3483
02:18:45,679 --> 02:18:50,479
the array name will be numbers and let's
3484
02:18:48,160 --> 02:18:53,599
initialize this array with a few numbers
3485
02:18:50,479 --> 02:18:56,080
1 two and three something simple so if i
3486
02:18:53,599 --> 02:18:58,399
would like to store a grid or matrix of
3487
02:18:56,080 --> 02:19:01,359
data these elements will be the first
3488
02:18:58,399 --> 02:19:03,920
row and i can add a second row
3489
02:19:01,359 --> 02:19:06,160
so separate each row with a comma then
3490
02:19:03,920 --> 02:19:08,960
another set of curly braces and then you
3491
02:19:06,160 --> 02:19:11,679
can add more values so let's say four
3492
02:19:08,960 --> 02:19:13,840
five and six and we'll stop here with
3493
02:19:11,679 --> 02:19:16,160
these separate arrays we will surround
3494
02:19:13,840 --> 02:19:18,719
with a set of curly braces
3495
02:19:16,160 --> 02:19:20,639
and preceding the first set of straight
3496
02:19:18,719 --> 02:19:23,280
brackets we will add a second set of
3497
02:19:20,639 --> 02:19:25,359
straight brackets so this is now a two
3498
02:19:23,280 --> 02:19:27,920
dimensional array however with a two
3499
02:19:25,359 --> 02:19:30,719
dimensional array we have to specify a
3500
02:19:27,920 --> 02:19:32,800
maximum size of elements within each of
3501
02:19:30,719 --> 02:19:35,840
these arrays let's say that each of
3502
02:19:32,800 --> 02:19:38,399
these arrays will have a maximum size of
3503
02:19:35,840 --> 02:19:40,479
three elements a piece so within the
3504
02:19:38,399 --> 02:19:42,880
second set of straight brackets i will
3505
02:19:40,479 --> 02:19:46,240
list three and you can although it's not
3506
02:19:42,880 --> 02:19:48,880
necessary set a maximum amount of arrays
3507
02:19:46,240 --> 02:19:51,120
within your two-dimensional array
3508
02:19:48,880 --> 02:19:52,640
so let's say two because we have two
3509
02:19:51,120 --> 02:19:54,640
separate arrays within our
3510
02:19:52,640 --> 02:19:56,800
two-dimensional array now to better
3511
02:19:54,640 --> 02:19:58,880
visualize how this is more or less a
3512
02:19:56,800 --> 02:20:01,880
table of data i'm going to rearrange
3513
02:19:58,880 --> 02:20:01,880
these
3514
02:20:02,399 --> 02:20:06,560
so this may be a better visualization
3515
02:20:04,800 --> 02:20:08,880
this first set of straight brackets is
3516
02:20:06,560 --> 02:20:10,560
for the number of rows and the second
3517
02:20:08,880 --> 02:20:13,439
set of straight brackets is for the
3518
02:20:10,560 --> 02:20:15,040
number of columns so we have two rows
3519
02:20:13,439 --> 02:20:17,040
and three columns within our
3520
02:20:15,040 --> 02:20:19,120
two-dimensional array now you can
3521
02:20:17,040 --> 02:20:21,040
declare a two-dimensional array but not
3522
02:20:19,120 --> 02:20:23,600
assigned values quite yet but you'll
3523
02:20:21,040 --> 02:20:25,200
need to set a maximum size so right now
3524
02:20:23,600 --> 02:20:26,960
i'm just going to turn this into one
3525
02:20:25,200 --> 02:20:29,040
giant comment
3526
02:20:26,960 --> 02:20:33,040
and let's declare a two-dimensional
3527
02:20:29,040 --> 02:20:35,280
array with two rows and three columns
3528
02:20:33,040 --> 02:20:37,439
and here's how to assign some values we
3529
02:20:35,280 --> 02:20:40,000
will type the name of the array followed
3530
02:20:37,439 --> 02:20:42,560
by two sets of straight brackets we need
3531
02:20:40,000 --> 02:20:44,560
a row number and a column number so the
3532
02:20:42,560 --> 02:20:47,600
first column within the first row is
3533
02:20:44,560 --> 02:20:49,200
going to be zero zero because computers
3534
02:20:47,600 --> 02:20:51,920
always start with zero
3535
02:20:49,200 --> 02:20:54,640
and let's assign this a value of one the
3536
02:20:51,920 --> 02:20:56,160
second column within the first row
3537
02:20:54,640 --> 02:20:59,520
would be
3538
02:20:56,160 --> 02:21:02,240
zero one and let's assign two there
3539
02:20:59,520 --> 02:21:04,640
then zero two and that will be number
3540
02:21:02,240 --> 02:21:08,640
three so the first column within the
3541
02:21:04,640 --> 02:21:10,720
second row would be numbers one zero and
3542
02:21:08,640 --> 02:21:13,120
this will be four then we will follow
3543
02:21:10,720 --> 02:21:16,720
the same pattern so where five was that
3544
02:21:13,120 --> 02:21:16,720
would be one one
3545
02:21:16,880 --> 02:21:22,479
and six is one two
3546
02:21:20,399 --> 02:21:24,960
so this is another way to initialize an
3547
02:21:22,479 --> 02:21:27,040
array you can set a maximum size and
3548
02:21:24,960 --> 02:21:28,640
then assign some values later or you
3549
02:21:27,040 --> 02:21:30,080
could assign all of the values right
3550
02:21:28,640 --> 02:21:32,160
from the beginning if you know what they
3551
02:21:30,080 --> 02:21:34,399
are okay now how can we display the
3552
02:21:32,160 --> 02:21:36,960
elements of a two-dimensional array
3553
02:21:34,399 --> 02:21:39,040
we'll have to use nested loops
3554
02:21:36,960 --> 02:21:41,040
so let's create a for loop and i will
3555
02:21:39,040 --> 02:21:43,359
declare an index of i
3556
02:21:41,040 --> 02:21:45,280
set this equal to zero and for the time
3557
02:21:43,359 --> 02:21:47,840
being i'm going to say i
3558
02:21:45,280 --> 02:21:50,240
is less than the number of rows that we
3559
02:21:47,840 --> 02:21:51,920
have so right now we have two rows but
3560
02:21:50,240 --> 02:21:53,600
we're going to change this value later
3561
02:21:51,920 --> 02:21:55,359
to something that's more flexible that
3562
02:21:53,600 --> 02:21:57,840
will calculate the amount of rows that
3563
02:21:55,359 --> 02:22:00,240
we have and then i will increment i by
3564
02:21:57,840 --> 02:22:02,000
one now let's create a nested loop
3565
02:22:00,240 --> 02:22:03,680
that's in charge of keeping track of the
3566
02:22:02,000 --> 02:22:06,560
columns
3567
02:22:03,680 --> 02:22:08,880
and let's use an index of j because we
3568
02:22:06,560 --> 02:22:12,080
do not want to reuse i
3569
02:22:08,880 --> 02:22:14,000
so set j equal to zero we will continue
3570
02:22:12,080 --> 02:22:16,399
this as long as j
3571
02:22:14,000 --> 02:22:19,520
is less than however many columns we
3572
02:22:16,399 --> 02:22:22,080
have three a maximum of three and then
3573
02:22:19,520 --> 02:22:24,080
increment j by one so during each
3574
02:22:22,080 --> 02:22:26,399
iteration of the inner for loop let's
3575
02:22:24,080 --> 02:22:29,200
display one of these elements so we will
3576
02:22:26,399 --> 02:22:30,800
use the format specifier for an integer
3577
02:22:29,200 --> 02:22:33,840
d is fine
3578
02:22:30,800 --> 02:22:36,880
followed by our array numbers and then
3579
02:22:33,840 --> 02:22:38,160
we have two indices so the row is going
3580
02:22:36,880 --> 02:22:40,160
to be i
3581
02:22:38,160 --> 02:22:42,720
and this will begin at zero
3582
02:22:40,160 --> 02:22:46,080
and the columns is j this will also
3583
02:22:42,720 --> 02:22:48,640
begin at zero so after each iteration of
3584
02:22:46,080 --> 02:22:50,640
the inner for loop we will increase j
3585
02:22:48,640 --> 02:22:53,439
when we finish the inner for loop we
3586
02:22:50,640 --> 02:22:55,680
will increment i by one so by using
3587
02:22:53,439 --> 02:22:57,600
nested for loops we can iterate over all
3588
02:22:55,680 --> 02:22:59,840
of the elements of this array so i'm
3589
02:22:57,600 --> 02:23:02,000
just going to add a space after our
3590
02:22:59,840 --> 02:23:04,399
number and we should probably add a new
3591
02:23:02,000 --> 02:23:07,280
line after each row so i'll add a new
3592
02:23:04,399 --> 02:23:10,319
line let's test this
3593
02:23:07,280 --> 02:23:12,399
here we go we have our table of two rows
3594
02:23:10,319 --> 02:23:14,640
and three columns now there's one
3595
02:23:12,399 --> 02:23:17,040
situation that we may run into what if
3596
02:23:14,640 --> 02:23:19,520
we change the amount of rows and columns
3597
02:23:17,040 --> 02:23:21,840
that we have so let's say that we add
3598
02:23:19,520 --> 02:23:25,920
one more row our 2d array of numbers
3599
02:23:21,840 --> 02:23:28,880
will have three rows and three columns
3600
02:23:25,920 --> 02:23:32,720
row two column zero equals seven
3601
02:23:28,880 --> 02:23:35,920
row two column one will equal eight and
3602
02:23:32,720 --> 02:23:37,840
row two column two will equal nine so if
3603
02:23:35,920 --> 02:23:40,000
i were to run this again well this last
3604
02:23:37,840 --> 02:23:42,160
row is not going to be displayed so it
3605
02:23:40,000 --> 02:23:44,160
would be better if we can calculate how
3606
02:23:42,160 --> 02:23:46,319
many rows and columns are within our
3607
02:23:44,160 --> 02:23:48,560
two-dimensional array and here's one way
3608
02:23:46,319 --> 02:23:50,720
to do so i'm going to declare two new
3609
02:23:48,560 --> 02:23:53,200
variables int rows
3610
02:23:50,720 --> 02:23:55,840
and into columns and i will set the
3611
02:23:53,200 --> 02:23:59,040
condition of the outer for loop to be as
3612
02:23:55,840 --> 02:24:01,760
long as i is less than rows and the
3613
02:23:59,040 --> 02:24:02,880
inner for loop will be j is less than
3614
02:24:01,760 --> 02:24:04,479
columns
3615
02:24:02,880 --> 02:24:06,479
now we just need to calculate what these
3616
02:24:04,479 --> 02:24:09,600
numbers are going to be to calculate
3617
02:24:06,479 --> 02:24:12,000
rows we can use the size of operator and
3618
02:24:09,600 --> 02:24:13,920
then pass in the entire size of our
3619
02:24:12,000 --> 02:24:16,640
two-dimensional rib and we're going to
3620
02:24:13,920 --> 02:24:18,560
divide this by the size of one of our
3621
02:24:16,640 --> 02:24:21,520
rows they're all going to have the same
3622
02:24:18,560 --> 02:24:23,760
size so we can pass in our rain numbers
3623
02:24:21,520 --> 02:24:25,200
and then specify one of the rows let's
3624
02:24:23,760 --> 02:24:27,200
say the first row they're all going to
3625
02:24:25,200 --> 02:24:28,880
be the same okay so that's how we can
3626
02:24:27,200 --> 02:24:30,800
calculate the number of rows that we
3627
02:24:28,880 --> 02:24:32,880
have now to find the number of columns
3628
02:24:30,800 --> 02:24:34,479
we can copy what we have here and then
3629
02:24:32,880 --> 02:24:36,080
just make a few changes
3630
02:24:34,479 --> 02:24:39,040
so we will say
3631
02:24:36,080 --> 02:24:41,040
the size of the first row row 0
3632
02:24:39,040 --> 02:24:44,720
divided by the size of one of the
3633
02:24:41,040 --> 02:24:47,520
elements found within the first row
3634
02:24:44,720 --> 02:24:49,120
so we can say zero zero and let's print
3635
02:24:47,520 --> 02:24:52,080
the amount of rows and columns that we
3636
02:24:49,120 --> 02:24:52,080
have just to test it
3637
02:24:54,240 --> 02:24:59,600
so we have rows
3638
02:24:56,720 --> 02:24:59,600
and columns
3639
02:24:59,840 --> 02:25:04,000
i'm just going to add a new line
3640
02:25:01,120 --> 02:25:04,000
character real quick
3641
02:25:05,359 --> 02:25:10,640
okay so we have three rows three columns
3642
02:25:08,240 --> 02:25:12,720
and here's our table three rows with
3643
02:25:10,640 --> 02:25:14,640
three columns a piece so yeah that's
3644
02:25:12,720 --> 02:25:17,439
basically a two-dimensional array it's
3645
02:25:14,640 --> 02:25:19,920
an array of arrays where each element is
3646
02:25:17,439 --> 02:25:23,040
an entire array it's useful if you need
3647
02:25:19,920 --> 02:25:25,920
a matrix grid or table of data and in
3648
02:25:23,040 --> 02:25:27,760
this example we made a table of integers
3649
02:25:25,920 --> 02:25:29,280
just the numbers one through nine so
3650
02:25:27,760 --> 02:25:31,120
there's a couple different ways in which
3651
02:25:29,280 --> 02:25:33,120
you can initialize a two-dimensional
3652
02:25:31,120 --> 02:25:35,840
array but you'll need two sets of
3653
02:25:33,120 --> 02:25:38,000
straight brackets it's optional to set a
3654
02:25:35,840 --> 02:25:40,479
maximum number of rows but it is
3655
02:25:38,000 --> 02:25:42,640
necessary to set a maximum number of
3656
02:25:40,479 --> 02:25:44,800
elements within each row and then to
3657
02:25:42,640 --> 02:25:47,120
access one of the elements you use two
3658
02:25:44,800 --> 02:25:49,280
indices one for the row one for the
3659
02:25:47,120 --> 02:25:50,880
column so yeah those are two dimensional
3660
02:25:49,280 --> 02:25:52,479
arrays if you would like a copy of this
3661
02:25:50,880 --> 02:25:54,800
code i'll post this to the comment
3662
02:25:52,479 --> 02:25:58,720
section down below and well yeah those
3663
02:25:54,800 --> 02:25:58,720
are two dimensional arrays in c
3664
02:26:00,560 --> 02:26:04,560
hey everyone in this video i'm going to
3665
02:26:02,640 --> 02:26:07,280
show you how we can create an array of
3666
02:26:04,560 --> 02:26:08,800
strings let's say we will have an array
3667
02:26:07,280 --> 02:26:11,120
named cars
3668
02:26:08,800 --> 02:26:13,439
so we will need a two-dimensional array
3669
02:26:11,120 --> 02:26:15,359
so we need two sets of straight brackets
3670
02:26:13,439 --> 02:26:18,240
and within the second set of straight
3671
02:26:15,359 --> 02:26:20,080
brackets we will specify a maximum size
3672
02:26:18,240 --> 02:26:22,800
for each of these elements let's say
3673
02:26:20,080 --> 02:26:25,120
maybe 10 characters so a string is
3674
02:26:22,800 --> 02:26:27,439
already an array of characters let's add
3675
02:26:25,120 --> 02:26:29,520
our first string maybe we have a mustang
3676
02:26:27,439 --> 02:26:31,520
so to add a second element we would
3677
02:26:29,520 --> 02:26:33,439
separate the next one with a comma then
3678
02:26:31,520 --> 02:26:35,280
we can add another string
3679
02:26:33,439 --> 02:26:38,560
let's say we have a corvette and then
3680
02:26:35,280 --> 02:26:40,319
one more how about a maybe camaro okay
3681
02:26:38,560 --> 02:26:42,240
so with all of these different strings
3682
02:26:40,319 --> 02:26:44,640
i'm going to surround with a set of
3683
02:26:42,240 --> 02:26:47,680
curly braces and here we go we have an
3684
02:26:44,640 --> 02:26:50,399
array of strings it's basically a 2d
3685
02:26:47,680 --> 02:26:52,640
array of individual characters except
3686
02:26:50,399 --> 02:26:54,319
each element is a string now one
3687
02:26:52,640 --> 02:26:56,960
important difference with an array of
3688
02:26:54,319 --> 02:26:58,319
strings is that we can't directly change
3689
02:26:56,960 --> 02:27:01,359
one of the values
3690
02:26:58,319 --> 02:27:02,880
let's say car's at index zero equals a
3691
02:27:01,359 --> 02:27:06,080
tesla
3692
02:27:02,880 --> 02:27:08,479
and then i'm going to try and run this
3693
02:27:06,080 --> 02:27:10,880
so we can't directly assign a new value
3694
02:27:08,479 --> 02:27:13,520
one way in which we can do so is to use
3695
02:27:10,880 --> 02:27:15,359
the string copy function so if you're
3696
02:27:13,520 --> 02:27:17,760
going to use the string copy function
3697
02:27:15,359 --> 02:27:20,720
include this import
3698
02:27:17,760 --> 02:27:23,760
string dot h okay so to update one of
3699
02:27:20,720 --> 02:27:25,600
the values i will use string
3700
02:27:23,760 --> 02:27:27,680
copy
3701
02:27:25,600 --> 02:27:29,359
pass in my array
3702
02:27:27,680 --> 02:27:32,160
and an index number
3703
02:27:29,359 --> 02:27:35,359
followed by a string so let's say a
3704
02:27:32,160 --> 02:27:37,280
tesla and this would work now to display
3705
02:27:35,359 --> 02:27:38,640
an array of strings we can use a for
3706
02:27:37,280 --> 02:27:40,960
loop
3707
02:27:38,640 --> 02:27:43,200
and there's no need for a nested loop so
3708
02:27:40,960 --> 02:27:45,600
we will create an index i set this equal
3709
02:27:43,200 --> 02:27:48,160
to zero i would like to continue this as
3710
02:27:45,600 --> 02:27:50,000
long as i is less than and we will
3711
02:27:48,160 --> 02:27:52,640
calculate how many elements are within
3712
02:27:50,000 --> 02:27:55,680
our array so we will use the size of
3713
02:27:52,640 --> 02:27:57,359
operator pass in our array divided by
3714
02:27:55,680 --> 02:27:59,040
one of the elements
3715
02:27:57,359 --> 02:28:02,240
size of
3716
02:27:59,040 --> 02:28:04,640
cars and maybe the first element of zero
3717
02:28:02,240 --> 02:28:07,120
and then increment i by one during each
3718
02:28:04,640 --> 02:28:08,720
iteration so then during each iteration
3719
02:28:07,120 --> 02:28:10,880
let's display one of these elements
3720
02:28:08,720 --> 02:28:12,399
using a printf statement
3721
02:28:10,880 --> 02:28:14,720
let's display a string
3722
02:28:12,399 --> 02:28:15,680
i'll add a new line after each printf
3723
02:28:14,720 --> 02:28:18,240
statement
3724
02:28:15,680 --> 02:28:19,520
and let's display our array cards at
3725
02:28:18,240 --> 02:28:22,160
index of i
3726
02:28:19,520 --> 02:28:24,720
so this should display tesla corvette
3727
02:28:22,160 --> 02:28:27,760
and camaro in that order
3728
02:28:24,720 --> 02:28:29,920
tesla corvette and a camaro so yeah
3729
02:28:27,760 --> 02:28:32,240
that's an array of strings it's really a
3730
02:28:29,920 --> 02:28:34,240
two-dimensional array of characters but
3731
02:28:32,240 --> 02:28:36,479
it involves less steps because each of
3732
02:28:34,240 --> 02:28:38,319
these strings is already an array and if
3733
02:28:36,479 --> 02:28:40,560
you need to update or edit one of the
3734
02:28:38,319 --> 02:28:42,640
values you can use the string copy
3735
02:28:40,560 --> 02:28:44,399
function so yeah that's how to work with
3736
02:28:42,640 --> 02:28:46,160
an array of strings if you would like a
3737
02:28:44,399 --> 02:28:48,160
copy of this code i'll post this to the
3738
02:28:46,160 --> 02:28:50,000
comments section down below and well
3739
02:28:48,160 --> 02:28:53,880
yeah that's how to work with an array of
3740
02:28:50,000 --> 02:28:53,880
strings in c
3741
02:28:54,399 --> 02:28:57,760
all right what's going on people so in
3742
02:28:56,319 --> 02:29:00,399
this video i'm going to show you how we
3743
02:28:57,760 --> 02:29:01,760
can swap the values of two variables now
3744
02:29:00,399 --> 02:29:04,000
you may be wondering why the heck do we
3745
02:29:01,760 --> 02:29:06,319
need to know this so swapping variables
3746
02:29:04,000 --> 02:29:08,479
is actually very common within sorting
3747
02:29:06,319 --> 02:29:10,560
algorithms and when we reach the next
3748
02:29:08,479 --> 02:29:12,560
topic on sorting arrays we'll need to
3749
02:29:10,560 --> 02:29:15,520
learn how to do this so let's begin i
3750
02:29:12,560 --> 02:29:18,399
have two variables x and y x contains
3751
02:29:15,520 --> 02:29:20,240
the letter x y contains the letter y and
3752
02:29:18,399 --> 02:29:24,319
let's print these values i'll use a
3753
02:29:20,240 --> 02:29:24,319
printf statement let's display x
3754
02:29:25,520 --> 02:29:28,080
and y
3755
02:29:30,000 --> 02:29:36,399
x equals x y equals y so how can we swap
3756
02:29:33,600 --> 02:29:39,280
these let's try x equals y and see what
3757
02:29:36,399 --> 02:29:42,800
happens within both x and y the value is
3758
02:29:39,280 --> 02:29:44,640
y what about y equals x
3759
02:29:42,800 --> 02:29:46,800
well then both variables contain the
3760
02:29:44,640 --> 02:29:48,640
character x so what can we do it seems
3761
02:29:46,800 --> 02:29:51,520
like we're stuck what we could do is
3762
02:29:48,640 --> 02:29:53,520
that we could introduce a third variable
3763
02:29:51,520 --> 02:29:56,160
let's say char and let's name this temp
3764
02:29:53,520 --> 02:29:58,560
because it's a temporary variable so we
3765
02:29:56,160 --> 02:30:01,280
have a third variable to work with as
3766
02:29:58,560 --> 02:30:02,800
temporary storage for some value so
3767
02:30:01,280 --> 02:30:05,520
let's assign
3768
02:30:02,800 --> 02:30:07,280
temp is equal to x
3769
02:30:05,520 --> 02:30:09,359
and then we will assign
3770
02:30:07,280 --> 02:30:12,640
x equals y
3771
02:30:09,359 --> 02:30:15,359
and then lastly y equals temp
3772
02:30:12,640 --> 02:30:18,880
and this should work let's try it
3773
02:30:15,359 --> 02:30:20,319
yep x equals y and y equals x
3774
02:30:18,880 --> 02:30:22,399
now this is a little bit different if
3775
02:30:20,319 --> 02:30:25,920
you're working with strings so let's say
3776
02:30:22,399 --> 02:30:25,920
these are character arrays
3777
02:30:26,160 --> 02:30:30,840
and we'll pretend that these are maybe
3778
02:30:27,920 --> 02:30:32,399
glasses holding some fluid x contains
3779
02:30:30,840 --> 02:30:35,920
water
3780
02:30:32,399 --> 02:30:37,920
and y contains maybe lemonade
3781
02:30:35,920 --> 02:30:40,240
and let's make temp a character array as
3782
02:30:37,920 --> 02:30:42,640
well but we need to declare a size let's
3783
02:30:40,240 --> 02:30:44,399
say 15. okay so this isn't gonna work
3784
02:30:42,640 --> 02:30:47,760
we're already receiving problems
3785
02:30:44,399 --> 02:30:49,680
expression must be a modifiable l value
3786
02:30:47,760 --> 02:30:52,640
so when working with arrays it's not
3787
02:30:49,680 --> 02:30:55,680
enough to simply assign values
3788
02:30:52,640 --> 02:30:58,160
we could use the string copy function so
3789
02:30:55,680 --> 02:31:00,960
let's get rid of these and we will use
3790
02:30:58,160 --> 02:31:03,520
string copy oh also make sure that you
3791
02:31:00,960 --> 02:31:05,520
include this header file okay so we will
3792
02:31:03,520 --> 02:31:07,200
copy the contents
3793
02:31:05,520 --> 02:31:09,120
of x
3794
02:31:07,200 --> 02:31:11,200
over to temp
3795
02:31:09,120 --> 02:31:12,840
and it's the same procedure as before
3796
02:31:11,200 --> 02:31:15,280
string
3797
02:31:12,840 --> 02:31:16,960
copy y
3798
02:31:15,280 --> 02:31:18,560
over to x
3799
02:31:16,960 --> 02:31:20,960
and string
3800
02:31:18,560 --> 02:31:20,960
copy
3801
02:31:21,600 --> 02:31:24,880
temp over to y
3802
02:31:23,680 --> 02:31:27,680
and then make sure you change these
3803
02:31:24,880 --> 02:31:29,840
format specifiers to strings
3804
02:31:27,680 --> 02:31:32,080
x did contain water now it contains
3805
02:31:29,840 --> 02:31:33,600
lemonade y did contain lemonade and now
3806
02:31:32,080 --> 02:31:35,280
it contains water
3807
02:31:33,600 --> 02:31:37,920
when swapping variables i like to
3808
02:31:35,280 --> 02:31:40,160
imagine my variables as glasses and they
3809
02:31:37,920 --> 02:31:42,399
contain some fluid if we pour the
3810
02:31:40,160 --> 02:31:44,319
contents of one glass into the other
3811
02:31:42,399 --> 02:31:46,720
well then the contents will be pushed
3812
02:31:44,319 --> 02:31:48,960
out and overflow so it would be wise of
3813
02:31:46,720 --> 02:31:51,439
us to introduce a third glass that's
3814
02:31:48,960 --> 02:31:53,200
empty to hold one of these fluids so
3815
02:31:51,439 --> 02:31:55,680
then we can pour the contents of one
3816
02:31:53,200 --> 02:31:57,680
variable into the other now here's one
3817
02:31:55,680 --> 02:32:00,479
issue that you may run into when using
3818
02:31:57,680 --> 02:32:03,040
the string copy function if the length
3819
02:32:00,479 --> 02:32:05,040
of the second argument is less than the
3820
02:32:03,040 --> 02:32:07,280
first argument this can lead to
3821
02:32:05,040 --> 02:32:09,680
unexpected behavior i'm going to replace
3822
02:32:07,280 --> 02:32:10,640
lemonade with soda and let's see what
3823
02:32:09,680 --> 02:32:12,560
happens
3824
02:32:10,640 --> 02:32:15,040
so x doesn't contain anything but y
3825
02:32:12,560 --> 02:32:17,040
contains water one solution is that we
3826
02:32:15,040 --> 02:32:17,920
can make these character arrays the same
3827
02:32:17,040 --> 02:32:20,479
size
3828
02:32:17,920 --> 02:32:22,000
let's say that they're both 15
3829
02:32:20,479 --> 02:32:23,200
and that should solve that issue so
3830
02:32:22,000 --> 02:32:25,040
that's something you may need to take
3831
02:32:23,200 --> 02:32:27,040
into consideration when using the string
3832
02:32:25,040 --> 02:32:29,600
copy function if you're going to swap
3833
02:32:27,040 --> 02:32:32,319
strings so yeah everybody that's how to
3834
02:32:29,600 --> 02:32:34,080
swap the values of two variables if you
3835
02:32:32,319 --> 02:32:35,760
would like a copy of this code i'll post
3836
02:32:34,080 --> 02:32:37,520
this to the comment section down below
3837
02:32:35,760 --> 02:32:39,200
and in the next video i'm gonna show you
3838
02:32:37,520 --> 02:32:41,520
how we can sort the elements within an
3839
02:32:39,200 --> 02:32:45,840
array and well yeah that's how to swap
3840
02:32:41,520 --> 02:32:45,840
the values of two variables in c
3841
02:32:47,280 --> 02:32:50,800
hey uh what's going on people so in this
3842
02:32:49,439 --> 02:32:53,280
video we're going to write a small
3843
02:32:50,800 --> 02:32:55,280
program to sort the elements of an array
3844
02:32:53,280 --> 02:32:56,720
let's begin with an array of integers
3845
02:32:55,280 --> 02:32:59,200
and then later we'll create an array of
3846
02:32:56,720 --> 02:33:01,680
characters the data type will be int and
3847
02:32:59,200 --> 02:33:03,520
let's name this array
3848
02:33:01,680 --> 02:33:05,040
and assign some values make sure that
3849
02:33:03,520 --> 02:33:07,840
they're not in order just make up some
3850
02:33:05,040 --> 02:33:07,840
numbers that's fine
3851
02:33:08,160 --> 02:33:11,760
looks good to me now let's calculate the
3852
02:33:10,240 --> 02:33:13,359
size of our array because we'll need to
3853
02:33:11,760 --> 02:33:16,880
know how many times we're going to
3854
02:33:13,359 --> 02:33:19,439
iterate through our array so int size i
3855
02:33:16,880 --> 02:33:22,160
will set this equal to the size of
3856
02:33:19,439 --> 02:33:24,720
operator pass in our array this will
3857
02:33:22,160 --> 02:33:26,560
calculate the size of our array in bytes
3858
02:33:24,720 --> 02:33:29,359
and to find the number of elements we
3859
02:33:26,560 --> 02:33:32,800
can divide this by the size of just one
3860
02:33:29,359 --> 02:33:36,000
element so size of array divided by the
3861
02:33:32,800 --> 02:33:38,160
size of array at index of zero
3862
02:33:36,000 --> 02:33:40,319
size will equal the amount of elements
3863
02:33:38,160 --> 02:33:42,640
within our array and just to keep this
3864
02:33:40,319 --> 02:33:44,800
code more organized let's declare a
3865
02:33:42,640 --> 02:33:46,560
function to actually sort all of this
3866
02:33:44,800 --> 02:33:48,399
for us so we don't have to do that
3867
02:33:46,560 --> 02:33:51,040
within the main function
3868
02:33:48,399 --> 02:33:52,880
so let's declare a sort function the
3869
02:33:51,040 --> 02:33:56,160
return type is void we're not returning
3870
02:33:52,880 --> 02:33:58,240
anything and let's name this sort
3871
02:33:56,160 --> 02:34:01,760
and there will be two parameters
3872
02:33:58,240 --> 02:34:01,760
an array of integers
3873
02:34:01,840 --> 02:34:06,880
and integer size
3874
02:34:05,040 --> 02:34:08,720
and let's invoke this function we're
3875
02:34:06,880 --> 02:34:10,240
invoking sort and then we need to pass
3876
02:34:08,720 --> 02:34:11,840
in two arguments
3877
02:34:10,240 --> 02:34:13,600
our array
3878
02:34:11,840 --> 02:34:15,359
and a size
3879
02:34:13,600 --> 02:34:18,640
within the sort function we'll need
3880
02:34:15,359 --> 02:34:21,520
nested loops we can use four loops
3881
02:34:18,640 --> 02:34:24,000
so the outer for loop will be int i set
3882
02:34:21,520 --> 02:34:27,200
this equal to zero and we will continue
3883
02:34:24,000 --> 02:34:30,240
this for loop as long as i is less than
3884
02:34:27,200 --> 02:34:32,800
the size of our array minus one and then
3885
02:34:30,240 --> 02:34:34,960
increment i by one and then we need an
3886
02:34:32,800 --> 02:34:38,319
inner for loop so let's copy our outer
3887
02:34:34,960 --> 02:34:40,640
for loop paste it and make a few changes
3888
02:34:38,319 --> 02:34:41,439
so the index for the inner loop will be
3889
02:34:40,640 --> 02:34:44,080
j
3890
02:34:41,439 --> 02:34:46,399
and we'll continue this as long as j is
3891
02:34:44,080 --> 02:34:49,120
less than the size of our array minus
3892
02:34:46,399 --> 02:34:50,640
one then j plus plus
3893
02:34:49,120 --> 02:34:52,960
here's what we're doing within our array
3894
02:34:50,640 --> 02:34:54,640
so we will begin at our first index
3895
02:34:52,960 --> 02:34:56,640
we're checking to see if the element on
3896
02:34:54,640 --> 02:34:58,640
the left is greater than the element on
3897
02:34:56,640 --> 02:35:00,479
the right if so we're going to swap
3898
02:34:58,640 --> 02:35:02,800
these two values and we'll need the help
3899
02:35:00,479 --> 02:35:05,200
of a temporary variable much like what
3900
02:35:02,800 --> 02:35:06,720
we learned in the last topic so let's
3901
02:35:05,200 --> 02:35:08,560
check to see if the element on the left
3902
02:35:06,720 --> 02:35:10,800
is greater than the element on the right
3903
02:35:08,560 --> 02:35:13,840
if it isn't we do nothing
3904
02:35:10,800 --> 02:35:15,840
so we need an if statement
3905
02:35:13,840 --> 02:35:18,479
if array
3906
02:35:15,840 --> 02:35:23,120
at index of j
3907
02:35:18,479 --> 02:35:24,000
is greater than array at index of j plus
3908
02:35:23,120 --> 02:35:25,680
one
3909
02:35:24,000 --> 02:35:27,520
we're adding plus one because we're
3910
02:35:25,680 --> 02:35:29,920
checking the element directly next to
3911
02:35:27,520 --> 02:35:31,920
the one that we're currently examining
3912
02:35:29,920 --> 02:35:33,600
so if the number on the left is greater
3913
02:35:31,920 --> 02:35:36,319
than the number on the right we will
3914
02:35:33,600 --> 02:35:39,359
perform a basic variable swap
3915
02:35:36,319 --> 02:35:41,680
so let's declare a variable in temp to
3916
02:35:39,359 --> 02:35:45,520
temporarily store some value and i will
3917
02:35:41,680 --> 02:35:47,520
set the sequel to array at index of j
3918
02:35:45,520 --> 02:35:50,160
now i need to move the element on the
3919
02:35:47,520 --> 02:35:51,120
right over to the element on the left
3920
02:35:50,160 --> 02:35:53,120
array
3921
02:35:51,120 --> 02:35:56,560
at index of j
3922
02:35:53,120 --> 02:35:58,800
equals array at index of j
3923
02:35:56,560 --> 02:36:00,720
plus one and then lastly whatever's
3924
02:35:58,800 --> 02:36:04,160
within temp i'm moving that to the
3925
02:36:00,720 --> 02:36:06,840
element on the right so array at index
3926
02:36:04,160 --> 02:36:10,160
of j plus one
3927
02:36:06,840 --> 02:36:12,319
equals temp and that's it so if you
3928
02:36:10,160 --> 02:36:13,280
would like to optimize this you can set
3929
02:36:12,319 --> 02:36:14,640
size
3930
02:36:13,280 --> 02:36:17,120
minus i
3931
02:36:14,640 --> 02:36:19,280
minus one basically this is a bubble
3932
02:36:17,120 --> 02:36:20,960
sort so depending on the size of this
3933
02:36:19,280 --> 02:36:23,200
number it's going to find a final
3934
02:36:20,960 --> 02:36:25,040
resting place since 9 is going to be the
3935
02:36:23,200 --> 02:36:27,359
largest it's going to be pushed all the
3936
02:36:25,040 --> 02:36:30,160
way to the right because we will examine
3937
02:36:27,359 --> 02:36:32,479
9 against each of these numbers so once
3938
02:36:30,160 --> 02:36:34,319
9 is pushed all the way to the right all
3939
02:36:32,479 --> 02:36:36,160
these other numbers that are less than 9
3940
02:36:34,319 --> 02:36:38,160
will be on the left so it's already
3941
02:36:36,160 --> 02:36:40,000
partially organized and now we just need
3942
02:36:38,160 --> 02:36:43,280
some way to display the elements of our
3943
02:36:40,000 --> 02:36:47,200
array so let's declare a print function
3944
02:36:43,280 --> 02:36:49,359
void and let's name this print array
3945
02:36:47,200 --> 02:36:52,560
and these will have the same parameters
3946
02:36:49,359 --> 02:36:55,280
an array of integers and into size
3947
02:36:52,560 --> 02:36:56,640
and this is a basic for loop
3948
02:36:55,280 --> 02:36:59,040
so for
3949
02:36:56,640 --> 02:37:01,439
and i set this equal to zero
3950
02:36:59,040 --> 02:37:04,399
we will continue this loop as long as i
3951
02:37:01,439 --> 02:37:06,479
is less than the size of our array and
3952
02:37:04,399 --> 02:37:09,200
increment i by one
3953
02:37:06,479 --> 02:37:10,640
and i would like to display each integer
3954
02:37:09,200 --> 02:37:13,600
and maybe i'll separate these with the
3955
02:37:10,640 --> 02:37:16,720
space and we are displaying our array at
3956
02:37:13,600 --> 02:37:19,840
index of i and all we have to do is
3957
02:37:16,720 --> 02:37:23,040
invoke the print array function
3958
02:37:19,840 --> 02:37:24,880
and then pass in our array and our size
3959
02:37:23,040 --> 02:37:26,319
and let's take a look fingers are
3960
02:37:24,880 --> 02:37:28,800
crossed
3961
02:37:26,319 --> 02:37:31,040
oh yeah there we go our array is now
3962
02:37:28,800 --> 02:37:33,120
sorted now if you would like this in
3963
02:37:31,040 --> 02:37:35,359
descending order all we have to do is
3964
02:37:33,120 --> 02:37:38,000
switch this if statement around from
3965
02:37:35,359 --> 02:37:40,880
greater than to less than
3966
02:37:38,000 --> 02:37:42,800
and our array is sorted in reverse order
3967
02:37:40,880 --> 02:37:44,720
and we could sort an array of other data
3968
02:37:42,800 --> 02:37:47,040
types too this time let's sort an array
3969
02:37:44,720 --> 02:37:50,080
of characters so let's declare an array
3970
02:37:47,040 --> 02:37:50,080
of characters this time
3971
02:37:50,720 --> 02:37:54,000
and make up some characters
3972
02:37:57,280 --> 02:38:02,000
so then to sort an array of characters
3973
02:37:59,200 --> 02:38:04,960
let's change the data type of our array
3974
02:38:02,000 --> 02:38:04,960
to characters
3975
02:38:06,319 --> 02:38:10,240
and then when we display each character
3976
02:38:08,319 --> 02:38:13,280
make sure that we're using the c format
3977
02:38:10,240 --> 02:38:16,720
specifier because we were using d before
3978
02:38:13,280 --> 02:38:16,720
and this should now be sorted
3979
02:38:17,439 --> 02:38:21,439
yep so that's currently in reverse order
3980
02:38:19,439 --> 02:38:23,600
so then to switch that around just
3981
02:38:21,439 --> 02:38:25,840
change the sign
3982
02:38:23,600 --> 02:38:28,319
all right there you go so yeah everybody
3983
02:38:25,840 --> 02:38:31,040
that is a simple program to sort the
3984
02:38:28,319 --> 02:38:32,800
elements of an array this is a basic
3985
02:38:31,040 --> 02:38:34,560
bubble sort and if you're interested in
3986
02:38:32,800 --> 02:38:36,800
learning more about sorting algorithms i
3987
02:38:34,560 --> 02:38:38,960
do have a playlist on data structures
3988
02:38:36,800 --> 02:38:40,479
and algorithms if you would like a copy
3989
02:38:38,960 --> 02:38:42,479
of this code i'll post this in the
3990
02:38:40,479 --> 02:38:45,280
comments section down below and well
3991
02:38:42,479 --> 02:38:47,680
yeah that's how to sort a simple array
3992
02:38:45,280 --> 02:38:47,680
in c
3993
02:38:49,280 --> 02:38:54,479
okay everybody so let's talk about
3994
02:38:51,680 --> 02:38:57,040
structs a struct is short for structure
3995
02:38:54,479 --> 02:38:59,439
think of it as a collection of related
3996
02:38:57,040 --> 02:39:01,680
members kind of like variables they can
3997
02:38:59,439 --> 02:39:04,640
be of different data types unlike with
3998
02:39:01,680 --> 02:39:06,800
arrays structs are listed under one name
3999
02:39:04,640 --> 02:39:08,880
in a block of memory in order to refer
4000
02:39:06,800 --> 02:39:10,800
to that struct we can refer to a
4001
02:39:08,880 --> 02:39:12,720
variable name and then we have access to
4002
02:39:10,800 --> 02:39:16,000
that block of memory that contains
4003
02:39:12,720 --> 02:39:18,399
related members and lastly structs are
4004
02:39:16,000 --> 02:39:20,399
very similar to classes in other
4005
02:39:18,399 --> 02:39:22,399
languages if you're familiar with more
4006
02:39:20,399 --> 02:39:24,800
than just c you'll probably know what a
4007
02:39:22,399 --> 02:39:27,120
class is but there are no methods within
4008
02:39:24,800 --> 02:39:29,280
a struct it's only members so we can
4009
02:39:27,120 --> 02:39:31,600
actually use structs to mimic real world
4010
02:39:29,280 --> 02:39:34,160
objects let's say that maybe we're
4011
02:39:31,600 --> 02:39:37,120
playing a game and we need two players
4012
02:39:34,160 --> 02:39:39,359
each player will have a name and a score
4013
02:39:37,120 --> 02:39:41,600
these will be the members so to create a
4014
02:39:39,359 --> 02:39:44,319
struct outside of our main function we
4015
02:39:41,600 --> 02:39:46,880
will type the keyword struct followed by
4016
02:39:44,319 --> 02:39:49,200
a tag name let's say we are working with
4017
02:39:46,880 --> 02:39:52,240
players then we need a set of curly
4018
02:39:49,200 --> 02:39:53,840
braces then end it with a semicolon so
4019
02:39:52,240 --> 02:39:55,840
any members think of these like
4020
02:39:53,840 --> 02:39:58,720
variables so let's say we have a
4021
02:39:55,840 --> 02:40:00,880
character array named name and i'll give
4022
02:39:58,720 --> 02:40:02,800
this a size of maybe 12.
4023
02:40:00,880 --> 02:40:05,040
so we will declare these but not assign
4024
02:40:02,800 --> 02:40:08,479
them we'll do that later and we have an
4025
02:40:05,040 --> 02:40:11,600
int named score so each player is going
4026
02:40:08,479 --> 02:40:13,279
to have a name and a score kind of like
4027
02:40:11,600 --> 02:40:14,960
we're playing an arcade game now to
4028
02:40:13,279 --> 02:40:17,279
assign some of these values we're going
4029
02:40:14,960 --> 02:40:19,359
to create a type of variable and a
4030
02:40:17,279 --> 02:40:22,319
player is going to be kind of the data
4031
02:40:19,359 --> 02:40:23,760
type so we will type struct
4032
02:40:22,319 --> 02:40:26,160
then the tag name
4033
02:40:23,760 --> 02:40:28,000
which was player
4034
02:40:26,160 --> 02:40:30,479
and then a variable name let's say we
4035
02:40:28,000 --> 02:40:32,960
have player 1 and player 2. so with
4036
02:40:30,479 --> 02:40:35,920
these structs you can reuse them so we
4037
02:40:32,960 --> 02:40:38,800
have struct player player 1 and struct
4038
02:40:35,920 --> 02:40:40,800
player player 2. so we can assign values
4039
02:40:38,800 --> 02:40:42,479
to these members however if we have an
4040
02:40:40,800 --> 02:40:44,399
array of characters we're going to use
4041
02:40:42,479 --> 02:40:48,240
the string copy function
4042
02:40:44,399 --> 02:40:50,160
so let's use the string copy function
4043
02:40:48,240 --> 02:40:52,560
then to access one of the members you
4044
02:40:50,160 --> 02:40:54,560
will type the name of that struct so
4045
02:40:52,560 --> 02:40:58,479
let's say player one and then follow
4046
02:40:54,560 --> 02:41:01,120
this with a dot the dot is a member axis
4047
02:40:58,479 --> 02:41:04,399
operator so after typing the dot we have
4048
02:41:01,120 --> 02:41:06,560
access to a name and score member so
4049
02:41:04,399 --> 02:41:08,319
let's set the player's name equal to
4050
02:41:06,560 --> 02:41:09,600
whatever your first name is now if
4051
02:41:08,319 --> 02:41:11,359
you're not working with an array of
4052
02:41:09,600 --> 02:41:14,080
characters such as an integer you can
4053
02:41:11,359 --> 02:41:17,760
just access these directly so let's set
4054
02:41:14,080 --> 02:41:19,920
player one's score two equal maybe four
4055
02:41:17,760 --> 02:41:21,920
points and we also have player two but
4056
02:41:19,920 --> 02:41:22,960
we have not assigned values to its
4057
02:41:21,920 --> 02:41:25,840
members
4058
02:41:22,960 --> 02:41:27,680
so this time let's assign player two's
4059
02:41:25,840 --> 02:41:29,359
name and score
4060
02:41:27,680 --> 02:41:32,399
so we can just copy what we have but
4061
02:41:29,359 --> 02:41:36,080
change player 1 to player 2. player 2
4062
02:41:32,399 --> 02:41:38,399
let's say is bra and player 2's score is
4063
02:41:36,080 --> 02:41:40,319
5. now if i would like to display these
4064
02:41:38,399 --> 02:41:43,359
members i can do so with a print
4065
02:41:40,319 --> 02:41:45,920
statement so printf let's display our
4066
02:41:43,359 --> 02:41:47,279
name first so i will use the s format
4067
02:41:45,920 --> 02:41:49,920
specifier
4068
02:41:47,279 --> 02:41:52,240
player one dot name and then i'll add a
4069
02:41:49,920 --> 02:41:54,319
new line then to access player one's
4070
02:41:52,240 --> 02:41:57,520
score well that's an integer so i will
4071
02:41:54,319 --> 02:42:00,080
use the d format specifier player one
4072
02:41:57,520 --> 02:42:02,000
dot score and then let's do the same
4073
02:42:00,080 --> 02:42:05,439
thing with player two
4074
02:42:02,000 --> 02:42:08,000
so player two dot name and player two
4075
02:42:05,439 --> 02:42:11,200
dot score so we have our player one
4076
02:42:08,000 --> 02:42:13,680
strucks name member as bro and score
4077
02:42:11,200 --> 02:42:17,359
member set to four and then our player
4078
02:42:13,680 --> 02:42:20,080
two struct has a name member of bra
4079
02:42:17,359 --> 02:42:22,080
and a score member of five so yeah
4080
02:42:20,080 --> 02:42:24,720
that's basically a struct it's a
4081
02:42:22,080 --> 02:42:27,200
collection of related members think of
4082
02:42:24,720 --> 02:42:29,680
these kind of like variables they can be
4083
02:42:27,200 --> 02:42:31,760
of different data types unlike arrays
4084
02:42:29,680 --> 02:42:33,840
and they are listed under one name in a
4085
02:42:31,760 --> 02:42:36,319
block of memory so in this example we
4086
02:42:33,840 --> 02:42:38,800
have player 1 and player 2. player 1 and
4087
02:42:36,319 --> 02:42:41,359
player 2 refer to different blocks of
4088
02:42:38,800 --> 02:42:43,680
memory which contain their own unique
4089
02:42:41,359 --> 02:42:45,760
members if you're familiar with other
4090
02:42:43,680 --> 02:42:48,160
programming languages they're very
4091
02:42:45,760 --> 02:42:50,560
similar to classes but they cannot
4092
02:42:48,160 --> 02:42:52,240
contain any methods so yeah those are
4093
02:42:50,560 --> 02:42:53,760
structs if you would like a copy of this
4094
02:42:52,240 --> 02:42:56,080
code i'll post this to the comment
4095
02:42:53,760 --> 02:42:59,359
section down below and well yeah those
4096
02:42:56,080 --> 02:42:59,359
are structs in c
4097
02:43:01,120 --> 02:43:07,279
all right typedef typedef is a reserved
4098
02:43:04,560 --> 02:43:09,920
keyword that gives an existing data type
4099
02:43:07,279 --> 02:43:13,439
a nickname here's an example let's
4100
02:43:09,920 --> 02:43:15,680
create a character array of 25 bytes and
4101
02:43:13,439 --> 02:43:16,720
we'll say that this is user one so
4102
02:43:15,680 --> 02:43:20,640
character
4103
02:43:16,720 --> 02:43:23,120
user one and the size is 25 bytes
4104
02:43:20,640 --> 02:43:25,279
and then make up some username
4105
02:43:23,120 --> 02:43:27,520
okay so writing some of the syntax can
4106
02:43:25,279 --> 02:43:29,359
be somewhat tedious what a lot of c
4107
02:43:27,520 --> 02:43:32,319
programmers will do is that they will
4108
02:43:29,359 --> 02:43:35,359
use this type def keyword to give some
4109
02:43:32,319 --> 02:43:38,160
data type a nickname let's use this type
4110
02:43:35,359 --> 02:43:40,720
def keyword to give a character array of
4111
02:43:38,160 --> 02:43:42,560
25 bytes some nickname so i'll do this
4112
02:43:40,720 --> 02:43:44,960
outside of the main function we will
4113
02:43:42,560 --> 02:43:46,960
type type def
4114
02:43:44,960 --> 02:43:50,000
then our data type
4115
02:43:46,960 --> 02:43:51,920
so this is a character array of 25 bytes
4116
02:43:50,000 --> 02:43:54,479
and we need some sort of nickname so
4117
02:43:51,920 --> 02:43:58,080
let's say that a character array of 25
4118
02:43:54,479 --> 02:44:00,319
bytes will be known as a user
4119
02:43:58,080 --> 02:44:02,560
now i don't need all of the syntax now i
4120
02:44:00,319 --> 02:44:04,800
can type my data type
4121
02:44:02,560 --> 02:44:07,520
user and then some variable name let's
4122
02:44:04,800 --> 02:44:10,640
say user one from this point forward if
4123
02:44:07,520 --> 02:44:13,600
i need a character array of 25 bytes i
4124
02:44:10,640 --> 02:44:15,359
can call that a user that's my nickname
4125
02:44:13,600 --> 02:44:17,600
now one place where you see this used a
4126
02:44:15,359 --> 02:44:20,160
lot is with structs this time let's
4127
02:44:17,600 --> 02:44:21,760
create a struct at first we won't use
4128
02:44:20,160 --> 02:44:24,080
the typedef keyword and then i'll show
4129
02:44:21,760 --> 02:44:25,600
you the benefits later so let's say this
4130
02:44:24,080 --> 02:44:27,760
is a struct
4131
02:44:25,600 --> 02:44:30,479
and the tag name is user
4132
02:44:27,760 --> 02:44:35,760
users will have let's say three members
4133
02:44:30,479 --> 02:44:39,200
a character array named name of 25 bytes
4134
02:44:35,760 --> 02:44:39,200
a password of 12 bytes
4135
02:44:40,160 --> 02:44:45,600
and int id like an id number
4136
02:44:43,680 --> 02:44:47,279
so to create a struct we would have to
4137
02:44:45,600 --> 02:44:48,720
type struct
4138
02:44:47,279 --> 02:44:49,840
then the tag name
4139
02:44:48,720 --> 02:44:51,520
user
4140
02:44:49,840 --> 02:44:53,680
and a variable name
4141
02:44:51,520 --> 02:44:58,640
and let's assign some of these values
4142
02:44:53,680 --> 02:44:58,640
i'll assign a name password and id
4143
02:44:59,359 --> 02:45:04,000
looks good to me okay then if i need to
4144
02:45:01,439 --> 02:45:06,800
create user two
4145
02:45:04,000 --> 02:45:08,720
i would type again struct user some
4146
02:45:06,800 --> 02:45:11,200
unique variable name and make up some
4147
02:45:08,720 --> 02:45:11,200
values
4148
02:45:11,359 --> 02:45:16,800
okay so with a strut if i would like to
4149
02:45:13,840 --> 02:45:20,479
use the typedef keyword i will precede
4150
02:45:16,800 --> 02:45:22,640
the struct keyword with typedef
4151
02:45:20,479 --> 02:45:25,600
and i don't necessarily need this tag
4152
02:45:22,640 --> 02:45:27,920
name and after the last curly brace here
4153
02:45:25,600 --> 02:45:30,479
i will add my nickname i'll call this a
4154
02:45:27,920 --> 02:45:32,560
user if we need to create a user struct
4155
02:45:30,479 --> 02:45:35,359
we no longer need to use the struct
4156
02:45:32,560 --> 02:45:37,120
keyword we can remove this
4157
02:45:35,359 --> 02:45:40,439
and this should work so let's print some
4158
02:45:37,120 --> 02:45:40,439
of these members
4159
02:45:54,640 --> 02:46:00,560
and here are the two users that we
4160
02:45:57,040 --> 02:46:02,640
created yeah so in conclusion typedef is
4161
02:46:00,560 --> 02:46:05,279
a reserved keyword that gives an
4162
02:46:02,640 --> 02:46:07,920
existing data type a nickname and it's
4163
02:46:05,279 --> 02:46:09,920
mostly used for convenience if we use
4164
02:46:07,920 --> 02:46:12,479
the typed up keyword when declaring a
4165
02:46:09,920 --> 02:46:14,880
struct we no longer need to use that
4166
02:46:12,479 --> 02:46:17,520
struct keyword to create a struct we can
4167
02:46:14,880 --> 02:46:19,040
just use this nickname as the data type
4168
02:46:17,520 --> 02:46:21,359
and then come up with some unique
4169
02:46:19,040 --> 02:46:23,120
variable name so yeah that is the
4170
02:46:21,359 --> 02:46:24,560
typedef keyword if you found this video
4171
02:46:23,120 --> 02:46:26,560
helpful please be sure to give this
4172
02:46:24,560 --> 02:46:28,319
video a thumbs up leave a random comment
4173
02:46:26,560 --> 02:46:31,439
down below and subscribe if you'd like
4174
02:46:28,319 --> 02:46:31,439
to become a fellow bro
4175
02:46:32,880 --> 02:46:36,560
hey everybody so in this video i'm going
4176
02:46:34,720 --> 02:46:38,880
to show you how we can create an array
4177
02:46:36,560 --> 02:46:40,720
of structs let's say we're working with
4178
02:46:38,880 --> 02:46:43,840
students and we need to print each
4179
02:46:40,720 --> 02:46:46,000
student's name and gpa so let's create a
4180
02:46:43,840 --> 02:46:48,479
struct outside of our main function type
4181
02:46:46,000 --> 02:46:51,120
struct and then a tag name for the
4182
02:46:48,479 --> 02:46:53,600
struct let's say student so each student
4183
02:46:51,120 --> 02:46:56,240
will have a name and a gpa
4184
02:46:53,600 --> 02:46:58,720
so let's create a character array named
4185
02:46:56,240 --> 02:47:00,080
name i'll set a maximum size of maybe 12
4186
02:46:58,720 --> 02:47:03,120
characters
4187
02:47:00,080 --> 02:47:06,399
and a gpa that could be a float
4188
02:47:03,120 --> 02:47:09,040
so float gpa now let's initialize some
4189
02:47:06,399 --> 02:47:11,520
structs we type struct followed by the
4190
02:47:09,040 --> 02:47:13,760
tag name and then some variable name to
4191
02:47:11,520 --> 02:47:16,000
identify each struct so let's say we
4192
02:47:13,760 --> 02:47:17,680
will have four students overall and i'll
4193
02:47:16,000 --> 02:47:20,160
go ahead and assign some values right
4194
02:47:17,680 --> 02:47:22,800
away when we initialize the struct so we
4195
02:47:20,160 --> 02:47:25,040
need a name and a gpa so the first
4196
02:47:22,800 --> 02:47:28,960
student let's say is spongebob
4197
02:47:25,040 --> 02:47:31,040
and spongebob has a gpa of 3.0
4198
02:47:28,960 --> 02:47:34,560
so we can copy this and create a few
4199
02:47:31,040 --> 02:47:37,120
more students so student one student two
4200
02:47:34,560 --> 02:47:39,040
student three and student four
4201
02:47:37,120 --> 02:47:42,800
next we have patrick
4202
02:47:39,040 --> 02:47:45,600
patrick has a 2.5
4203
02:47:42,800 --> 02:47:49,439
then sandy sandy is smart so she has a
4204
02:47:45,600 --> 02:47:52,560
4.0 and squidward
4205
02:47:49,439 --> 02:47:55,040
squidward has a 2.0 so we have four
4206
02:47:52,560 --> 02:47:56,960
students now we will create an array so
4207
02:47:55,040 --> 02:47:59,760
the data type of our array what it's
4208
02:47:56,960 --> 02:48:01,439
containing will be struxx it will be
4209
02:47:59,760 --> 02:48:03,200
struct student it's kind of like the
4210
02:48:01,439 --> 02:48:04,720
data type so with an array you have to
4211
02:48:03,200 --> 02:48:07,600
declare what you're going to be putting
4212
02:48:04,720 --> 02:48:09,920
within the array struct students and
4213
02:48:07,600 --> 02:48:12,640
then we need an array name so this will
4214
02:48:09,920 --> 02:48:14,399
be students and then specify a maximum
4215
02:48:12,640 --> 02:48:16,399
size if you need one but i'm just going
4216
02:48:14,399 --> 02:48:19,520
to initialize this array with some
4217
02:48:16,399 --> 02:48:22,160
students so let's add student 1
4218
02:48:19,520 --> 02:48:23,600
student 2
4219
02:48:22,160 --> 02:48:26,720
student 3
4220
02:48:23,600 --> 02:48:28,560
and student 4 and there we go we have an
4221
02:48:26,720 --> 02:48:30,240
array of structs
4222
02:48:28,560 --> 02:48:32,640
now if i would like to display each
4223
02:48:30,240 --> 02:48:34,880
student's name i can use a for loop to
4224
02:48:32,640 --> 02:48:36,240
loop through our array
4225
02:48:34,880 --> 02:48:38,800
so for
4226
02:48:36,240 --> 02:48:41,359
curly braces we will create an index
4227
02:48:38,800 --> 02:48:43,760
into i set this equal to zero
4228
02:48:41,359 --> 02:48:45,520
we will loop through this as long as i
4229
02:48:43,760 --> 02:48:47,359
is less than and then we need to
4230
02:48:45,520 --> 02:48:49,840
calculate how many elements are within
4231
02:48:47,359 --> 02:48:51,920
our array so we have four elements but
4232
02:48:49,840 --> 02:48:54,479
we can do that by using the size of
4233
02:48:51,920 --> 02:48:56,720
operator passing our array and then
4234
02:48:54,479 --> 02:48:58,399
divide this by the size of one of these
4235
02:48:56,720 --> 02:49:00,560
elements
4236
02:48:58,399 --> 02:49:02,560
size of students and then pick maybe the
4237
02:49:00,560 --> 02:49:05,359
first element of zero after each
4238
02:49:02,560 --> 02:49:07,200
iteration we will increment i by one so
4239
02:49:05,359 --> 02:49:09,680
then within our for loop let's print
4240
02:49:07,200 --> 02:49:12,479
each student's name
4241
02:49:09,680 --> 02:49:15,359
so let's use the s format specifier to
4242
02:49:12,479 --> 02:49:19,359
display a string and we will display our
4243
02:49:15,359 --> 02:49:20,240
array at index of i whatever our counter
4244
02:49:19,359 --> 02:49:23,359
is
4245
02:49:20,240 --> 02:49:26,479
and we will use the member axis operator
4246
02:49:23,359 --> 02:49:29,520
of dot so follow students at index of i
4247
02:49:26,479 --> 02:49:31,920
with a dot then we have access to a name
4248
02:49:29,520 --> 02:49:33,760
and a gpa so let's display each
4249
02:49:31,920 --> 02:49:36,399
student's name and then i'll add a new
4250
02:49:33,760 --> 02:49:38,720
line after each printf statement
4251
02:49:36,399 --> 02:49:41,200
so there we go we have spongebob patrick
4252
02:49:38,720 --> 02:49:43,920
sandy and squidward now let's display
4253
02:49:41,200 --> 02:49:44,720
each student's gpa so let's copy this
4254
02:49:43,920 --> 02:49:47,120
line
4255
02:49:44,720 --> 02:49:49,840
we are displaying a float so the format
4256
02:49:47,120 --> 02:49:52,000
specifier for a float is f
4257
02:49:49,840 --> 02:49:52,840
students at index of i
4258
02:49:52,000 --> 02:49:55,600
dot
4259
02:49:52,840 --> 02:49:57,439
gpa then i'll get rid of that new line
4260
02:49:55,600 --> 02:49:58,800
character and let's take a look to see
4261
02:49:57,439 --> 02:50:00,880
what we have
4262
02:49:58,800 --> 02:50:03,520
okay we can see each student's name and
4263
02:50:00,880 --> 02:50:05,840
gpa but we may want to format this
4264
02:50:03,520 --> 02:50:08,560
so after each student's name i'll add a
4265
02:50:05,840 --> 02:50:10,479
tab and i'll allocate maybe 12
4266
02:50:08,560 --> 02:50:13,120
characters to display each student's
4267
02:50:10,479 --> 02:50:15,600
name and then left justify it and then
4268
02:50:13,120 --> 02:50:17,840
with our gpa let's display two digits
4269
02:50:15,600 --> 02:50:19,359
after the decimal so i will add
4270
02:50:17,840 --> 02:50:20,960
dot 2
4271
02:50:19,359 --> 02:50:22,880
so this should look better
4272
02:50:20,960 --> 02:50:25,920
yeah that's much more organized so we
4273
02:50:22,880 --> 02:50:29,680
have spongebob with the gpa of 3.0
4274
02:50:25,920 --> 02:50:32,399
patrick with 2.5 sandy with 4.0 and
4275
02:50:29,680 --> 02:50:34,240
squidward with a 2.0 so yeah that's how
4276
02:50:32,399 --> 02:50:35,840
to create an array of structs if you
4277
02:50:34,240 --> 02:50:37,600
would like a copy of this code i'll post
4278
02:50:35,840 --> 02:50:39,439
this to the comment section down below
4279
02:50:37,600 --> 02:50:43,120
and well yeah that's how to create an
4280
02:50:39,439 --> 02:50:43,120
array of structs in c
4281
02:50:44,560 --> 02:50:50,000
well well well welcome back so enums
4282
02:50:47,359 --> 02:50:53,279
short for enumerations they are a
4283
02:50:50,000 --> 02:50:56,080
user-defined type of named integer
4284
02:50:53,279 --> 02:50:58,800
identifiers a benefit is that they help
4285
02:50:56,080 --> 02:51:00,720
make a program more readable either for
4286
02:50:58,800 --> 02:51:02,720
us or for somebody else that's looking
4287
02:51:00,720 --> 02:51:04,399
over our code so here's how to create
4288
02:51:02,720 --> 02:51:06,319
some enums you can either declare these
4289
02:51:04,399 --> 02:51:08,319
within the main function or outside of
4290
02:51:06,319 --> 02:51:10,080
the main function let's do so outside of
4291
02:51:08,319 --> 02:51:13,120
the main function so to create some
4292
02:51:10,080 --> 02:51:14,960
enums type the keyword enum followed by
4293
02:51:13,120 --> 02:51:17,359
a unique identifier let's say we're
4294
02:51:14,960 --> 02:51:18,880
working with days of the week so let's
4295
02:51:17,359 --> 02:51:21,200
say day
4296
02:51:18,880 --> 02:51:24,080
curly braces and then end this with a
4297
02:51:21,200 --> 02:51:26,240
semicolon so enums are constants and
4298
02:51:24,080 --> 02:51:28,160
let's declare some constant names let's
4299
02:51:26,240 --> 02:51:30,319
say the seven days of the week so
4300
02:51:28,160 --> 02:51:31,600
separate each constant with a comma and
4301
02:51:30,319 --> 02:51:33,840
we will just go through the days of the
4302
02:51:31,600 --> 02:51:33,840
week
4303
02:51:36,240 --> 02:51:40,080
here are the enumerations that we
4304
02:51:37,920 --> 02:51:42,399
declared the days sunday through
4305
02:51:40,080 --> 02:51:44,479
saturday the days of the week now each
4306
02:51:42,399 --> 02:51:46,640
of these constants has an associated
4307
02:51:44,479 --> 02:51:50,240
integer so beginning with your first
4308
02:51:46,640 --> 02:51:52,240
constant this will have a value of zero
4309
02:51:50,240 --> 02:51:54,960
then the next will have one
4310
02:51:52,240 --> 02:51:57,040
then two then three then you continue on
4311
02:51:54,960 --> 02:51:59,279
in that pattern but you can give these a
4312
02:51:57,040 --> 02:52:00,720
unique value let's change it sunday to
4313
02:51:59,279 --> 02:52:03,359
one because it's the first day of the
4314
02:52:00,720 --> 02:52:06,560
week then monday will be two and then we
4315
02:52:03,359 --> 02:52:08,319
will just continue with these
4316
02:52:06,560 --> 02:52:12,160
now to use one of these enums we would
4317
02:52:08,319 --> 02:52:14,319
type enum then our identifier name day
4318
02:52:12,160 --> 02:52:16,479
it's kind of like a data type and then
4319
02:52:14,319 --> 02:52:18,000
some variable name let's say today and
4320
02:52:16,479 --> 02:52:19,520
then you can set the sequel to one of
4321
02:52:18,000 --> 02:52:21,279
these constant names let's say that
4322
02:52:19,520 --> 02:52:22,720
today is sunday
4323
02:52:21,279 --> 02:52:25,520
so an important note with these
4324
02:52:22,720 --> 02:52:27,680
enumerations they're treated as integers
4325
02:52:25,520 --> 02:52:29,600
and not strings if i need to work with
4326
02:52:27,680 --> 02:52:31,760
one of these enums we're going to treat
4327
02:52:29,600 --> 02:52:34,080
them as if they were an integer so just
4328
02:52:31,760 --> 02:52:36,640
for an example i'm going to print the
4329
02:52:34,080 --> 02:52:38,560
value contained within today so this is
4330
02:52:36,640 --> 02:52:41,040
going to be an integer so we will use
4331
02:52:38,560 --> 02:52:42,479
the d format specifier and we will
4332
02:52:41,040 --> 02:52:45,680
display today
4333
02:52:42,479 --> 02:52:48,479
so since today is equal to sunday this
4334
02:52:45,680 --> 02:52:50,080
has an associated integer value of one
4335
02:52:48,479 --> 02:52:52,399
and if i display that
4336
02:52:50,080 --> 02:52:54,880
this will print the number one if i
4337
02:52:52,399 --> 02:52:57,680
change this to saturday well then it
4338
02:52:54,880 --> 02:53:00,240
will print the number seven
4339
02:52:57,680 --> 02:53:02,960
so an important note is that enums are
4340
02:53:00,240 --> 02:53:05,040
not strings but they can be treated as
4341
02:53:02,960 --> 02:53:07,359
integers now how does this make a
4342
02:53:05,040 --> 02:53:08,960
program more readable so let's write an
4343
02:53:07,359 --> 02:53:11,200
if-else statement that's going to check
4344
02:53:08,960 --> 02:53:12,640
what today is
4345
02:53:11,200 --> 02:53:16,479
first we'll begin with using the
4346
02:53:12,640 --> 02:53:18,240
associated integer value so if today
4347
02:53:16,479 --> 02:53:19,120
is equal to 1
4348
02:53:18,240 --> 02:53:20,160
or
4349
02:53:19,120 --> 02:53:22,240
today
4350
02:53:20,160 --> 02:53:24,080
is equal to 7
4351
02:53:22,240 --> 02:53:26,800
then that means it's either sunday or
4352
02:53:24,080 --> 02:53:30,160
saturday and let's print a message
4353
02:53:26,800 --> 02:53:31,600
let's say it's the weekend
4354
02:53:30,160 --> 02:53:35,200
party time
4355
02:53:31,600 --> 02:53:38,560
else we will print something else
4356
02:53:35,200 --> 02:53:41,120
i have to work today
4357
02:53:38,560 --> 02:53:43,680
so today is set to saturday that means
4358
02:53:41,120 --> 02:53:44,880
that saturday has an associated integer
4359
02:53:43,680 --> 02:53:48,800
of seven
4360
02:53:44,880 --> 02:53:50,640
so if today is equal to one or seven
4361
02:53:48,800 --> 02:53:54,000
well then we will execute this if
4362
02:53:50,640 --> 02:53:55,279
statement it's the weekend party time i
4363
02:53:54,000 --> 02:53:57,040
think i'm just going to add a new line
4364
02:53:55,279 --> 02:53:58,640
real quick
4365
02:53:57,040 --> 02:54:00,880
now if i set this to a different day of
4366
02:53:58,640 --> 02:54:03,359
the week let's say monday
4367
02:54:00,880 --> 02:54:05,279
well then i have to work today so the
4368
02:54:03,359 --> 02:54:07,600
way that this is written now
4369
02:54:05,279 --> 02:54:10,240
it's not too readable so instead of
4370
02:54:07,600 --> 02:54:12,000
using an integer value let's use the
4371
02:54:10,240 --> 02:54:14,560
associated name with each of these
4372
02:54:12,000 --> 02:54:16,640
constants so let's replace
4373
02:54:14,560 --> 02:54:19,600
one with sunday
4374
02:54:16,640 --> 02:54:21,359
and seven with saturday and i'll change
4375
02:54:19,600 --> 02:54:23,439
this to maybe sunday
4376
02:54:21,359 --> 02:54:25,439
so this is a lot more readable
4377
02:54:23,439 --> 02:54:27,600
especially if somebody else is reviewing
4378
02:54:25,439 --> 02:54:30,399
your code they may not understand why
4379
02:54:27,600 --> 02:54:33,200
today is equal to one or today is equal
4380
02:54:30,399 --> 02:54:37,120
to seven but this makes a lot more sense
4381
02:54:33,200 --> 02:54:40,240
oh so if today is equal to sunday or if
4382
02:54:37,120 --> 02:54:43,200
today is equal to saturday then we print
4383
02:54:40,240 --> 02:54:45,840
it's the weekend party time so that's a
4384
02:54:43,200 --> 02:54:48,720
huge benefit with enums is that they
4385
02:54:45,840 --> 02:54:51,120
help make a program more readable so to
4386
02:54:48,720 --> 02:54:53,840
declare enums you type enum then an
4387
02:54:51,120 --> 02:54:56,080
identifier and then you can list as many
4388
02:54:53,840 --> 02:54:58,479
constants as you would like and you can
4389
02:54:56,080 --> 02:55:00,960
give them an associated integer value
4390
02:54:58,479 --> 02:55:02,720
too so yeah those are enums if you'd
4391
02:55:00,960 --> 02:55:04,479
like a copy of this code i'll post this
4392
02:55:02,720 --> 02:55:09,040
to the comments section down below and
4393
02:55:04,479 --> 02:55:09,040
well yeah those are enums in c
4394
02:55:10,560 --> 02:55:14,080
hey uh what's going on everybody so in
4395
02:55:12,640 --> 02:55:16,479
this video i'm going to show you how we
4396
02:55:14,080 --> 02:55:18,319
can generate some random numbers in c
4397
02:55:16,479 --> 02:55:20,720
just as a disclaimer these are
4398
02:55:18,319 --> 02:55:22,800
pseudorandom numbers they are a set of
4399
02:55:20,720 --> 02:55:25,200
values or elements that are
4400
02:55:22,800 --> 02:55:27,279
statistically random so don't use these
4401
02:55:25,200 --> 02:55:29,279
for any sort of cryptographic security
4402
02:55:27,279 --> 02:55:31,040
now before we begin include these two
4403
02:55:29,279 --> 02:55:33,680
header files at the top
4404
02:55:31,040 --> 02:55:35,359
stdlib and time the first thing that
4405
02:55:33,680 --> 02:55:37,760
we're going to do is use the current
4406
02:55:35,359 --> 02:55:40,960
time to generate a seed which we need
4407
02:55:37,760 --> 02:55:44,880
for random numbers so we can use the s
4408
02:55:40,960 --> 02:55:46,640
rand function s for seed rand for random
4409
02:55:44,880 --> 02:55:47,520
and then we will pass in the current
4410
02:55:46,640 --> 02:55:50,800
time
4411
02:55:47,520 --> 02:55:53,439
time function pass and zero so we will
4412
02:55:50,800 --> 02:55:55,200
use the current time as a seed for
4413
02:55:53,439 --> 02:55:56,880
random numbers and then the function
4414
02:55:55,200 --> 02:55:59,040
that you need to generate a random
4415
02:55:56,880 --> 02:56:01,439
number using this seed is the rand
4416
02:55:59,040 --> 02:56:04,160
function so let's store our number
4417
02:56:01,439 --> 02:56:06,560
within a variable maybe number one int
4418
02:56:04,160 --> 02:56:08,640
number one and i will set the sequel to
4419
02:56:06,560 --> 02:56:10,640
then invoke the rand function now the
4420
02:56:08,640 --> 02:56:14,800
rand function will give you a random
4421
02:56:10,640 --> 02:56:16,720
number between 0 and 32 767.
4422
02:56:14,800 --> 02:56:18,880
so i'm guessing we probably don't need a
4423
02:56:16,720 --> 02:56:20,960
number that large so depending on the
4424
02:56:18,880 --> 02:56:23,200
range of numbers that we need we can use
4425
02:56:20,960 --> 02:56:25,279
modulus and then the maximum number that
4426
02:56:23,200 --> 02:56:28,479
you would like so if i'm rolling a six
4427
02:56:25,279 --> 02:56:30,240
sided dice i can say modulus six so the
4428
02:56:28,479 --> 02:56:32,240
modulus operator gives you the remainder
4429
02:56:30,240 --> 02:56:34,399
of any division so technically this
4430
02:56:32,240 --> 02:56:36,240
gives us a random number between zero
4431
02:56:34,399 --> 02:56:38,399
and five because computers always start
4432
02:56:36,240 --> 02:56:40,640
with zero but you can add an offset so
4433
02:56:38,399 --> 02:56:42,240
if i need the numbers one through six i
4434
02:56:40,640 --> 02:56:43,840
can just add plus one to the end and
4435
02:56:42,240 --> 02:56:45,600
then maybe i'll surround this section
4436
02:56:43,840 --> 02:56:47,760
with a set of parentheses just to make
4437
02:56:45,600 --> 02:56:50,720
this more readable so generate a random
4438
02:56:47,760 --> 02:56:52,560
number between zero and five add one
4439
02:56:50,720 --> 02:56:54,800
that gives us a random number between
4440
02:56:52,560 --> 02:56:57,520
one and six and let's just test that
4441
02:56:54,800 --> 02:56:59,920
theory so i'm going to use printf and we
4442
02:56:57,520 --> 02:57:02,319
are displaying an integer
4443
02:56:59,920 --> 02:57:04,560
and let's display number one so we
4444
02:57:02,319 --> 02:57:05,760
should have a random number between one
4445
02:57:04,560 --> 02:57:08,160
and six
4446
02:57:05,760 --> 02:57:09,520
and our random number is two and if i
4447
02:57:08,160 --> 02:57:12,240
run this again this will be a different
4448
02:57:09,520 --> 02:57:13,920
number probably yup six okay now if i
4449
02:57:12,240 --> 02:57:16,399
need to generate a couple different
4450
02:57:13,920 --> 02:57:18,160
random numbers i can just use this rand
4451
02:57:16,399 --> 02:57:19,840
function again so let's say we are
4452
02:57:18,160 --> 02:57:22,399
rolling three dice
4453
02:57:19,840 --> 02:57:24,240
number one number two and number three
4454
02:57:22,399 --> 02:57:26,640
and then i'll print these as well
4455
02:57:24,240 --> 02:57:29,120
so number one number two and number
4456
02:57:26,640 --> 02:57:31,359
three and i'll add a new line character
4457
02:57:29,120 --> 02:57:34,000
after each of these print statements
4458
02:57:31,359 --> 02:57:36,640
okay so this will give us three random
4459
02:57:34,000 --> 02:57:37,920
numbers between one and six one three
4460
02:57:36,640 --> 02:57:39,600
one
4461
02:57:37,920 --> 02:57:40,800
three five six
4462
02:57:39,600 --> 02:57:42,479
now it is important that we're
4463
02:57:40,800 --> 02:57:44,880
generating a random seed let me show you
4464
02:57:42,479 --> 02:57:46,560
what happens when i take this out
4465
02:57:44,880 --> 02:57:49,279
so this will give us the same numbers
4466
02:57:46,560 --> 02:57:50,800
over and over again six six five
4467
02:57:49,279 --> 02:57:52,960
six six five
4468
02:57:50,800 --> 02:57:55,120
and six six five so that's why it's
4469
02:57:52,960 --> 02:57:57,200
important that we use a seed for random
4470
02:57:55,120 --> 02:57:58,960
numbers now another thing too if you
4471
02:57:57,200 --> 02:58:01,040
need a larger number you can just change
4472
02:57:58,960 --> 02:58:02,960
this number for example i play a lot of
4473
02:58:01,040 --> 02:58:05,439
dungeons and dragons so i may need to
4474
02:58:02,960 --> 02:58:07,920
roll a 20-sided dice so i can just set
4475
02:58:05,439 --> 02:58:09,600
this to 20.
4476
02:58:07,920 --> 02:58:12,800
so 288
4477
02:58:09,600 --> 02:58:14,080
and let's try this again 12-5-4 so yeah
4478
02:58:12,800 --> 02:58:16,000
that's how to generate some random
4479
02:58:14,080 --> 02:58:17,680
numbers in c if you would like a copy of
4480
02:58:16,000 --> 02:58:19,920
this code i'll post this to the comment
4481
02:58:17,680 --> 02:58:23,359
section down below and well yeah that's
4482
02:58:19,920 --> 02:58:25,439
how to generate some random numbers in c
4483
02:58:23,359 --> 02:58:27,120
hey y'all what's going on everybody it's
4484
02:58:25,439 --> 02:58:28,720
you bro hope you're doing well and in
4485
02:58:27,120 --> 02:58:31,920
this video we're going to create a
4486
02:58:28,720 --> 02:58:35,520
number guessing game in c so sit back
4487
02:58:31,920 --> 02:58:35,520
relax and enjoy the show
4488
02:58:35,760 --> 02:58:40,399
if you find this video helpful please
4489
02:58:37,439 --> 02:58:42,319
remember to like comment and subscribe
4490
02:58:40,399 --> 02:58:44,880
your support will help keep this channel
4491
02:58:42,319 --> 02:58:46,880
running hey everybody so now that we
4492
02:58:44,880 --> 02:58:48,640
know how to generate some pseudo-random
4493
02:58:46,880 --> 02:58:50,880
numbers in c i thought this would be a
4494
02:58:48,640 --> 02:58:52,560
good opportunity for us to create a
4495
02:58:50,880 --> 02:58:54,399
number guessing game if we're working
4496
02:58:52,560 --> 02:58:56,160
with pseudorandom numbers be sure to
4497
02:58:54,399 --> 02:58:58,080
include at least these three header
4498
02:58:56,160 --> 02:59:02,319
files at the top of your program so
4499
02:58:58,080 --> 02:59:04,800
let's generate two constants min and max
4500
02:59:02,319 --> 02:59:06,880
const int and a naming convention for
4501
02:59:04,800 --> 02:59:08,160
constants is to make all of the letters
4502
02:59:06,880 --> 02:59:10,880
uppercase
4503
02:59:08,160 --> 02:59:12,880
so minimum will be the minimum number
4504
02:59:10,880 --> 02:59:14,800
that we will generate when we generate a
4505
02:59:12,880 --> 02:59:17,680
random number so let's pick a number
4506
02:59:14,800 --> 02:59:18,880
between maybe one and one hundred so min
4507
02:59:17,680 --> 02:59:21,840
will be one
4508
02:59:18,880 --> 02:59:23,359
and max will be 100 but feel free to
4509
02:59:21,840 --> 02:59:26,399
take the liberty to choose some other
4510
02:59:23,359 --> 02:59:28,399
numbers and we'll need int guess to
4511
02:59:26,399 --> 02:59:31,600
store a user's guess
4512
02:59:28,399 --> 02:59:33,040
int guesses to keep track of the guesses
4513
02:59:31,600 --> 02:59:35,279
and int
4514
02:59:33,040 --> 02:59:37,520
answer okay now if we're working with
4515
02:59:35,279 --> 02:59:39,520
random numbers we'll want to create a
4516
02:59:37,520 --> 02:59:42,160
seed and to do that we can actually use
4517
02:59:39,520 --> 02:59:46,399
the current time so use the s rand
4518
02:59:42,160 --> 02:59:49,279
function and pass in time pass in zero
4519
02:59:46,399 --> 02:59:52,560
so this uses the current
4520
02:59:49,279 --> 02:59:54,080
time as a seed to generate some random
4521
02:59:52,560 --> 02:59:56,399
numbers
4522
02:59:54,080 --> 02:59:59,520
and if we need a random number between
4523
02:59:56,399 --> 03:00:00,720
these two constants min and max this is
4524
02:59:59,520 --> 03:00:03,600
what we can do
4525
03:00:00,720 --> 03:00:04,560
so we will assign answer set this equal
4526
03:00:03,600 --> 03:00:06,399
to
4527
03:00:04,560 --> 03:00:09,120
call the rand function
4528
03:00:06,399 --> 03:00:11,920
and use the modulus operator
4529
03:00:09,120 --> 03:00:14,399
and set this to max and then we will add
4530
03:00:11,920 --> 03:00:16,560
min as an offset
4531
03:00:14,399 --> 03:00:20,399
so this will generate a random number
4532
03:00:16,560 --> 03:00:22,000
between these two constants 1 and 100 or
4533
03:00:20,399 --> 03:00:24,560
some other numbers if you have something
4534
03:00:22,000 --> 03:00:27,439
different so this line of code will
4535
03:00:24,560 --> 03:00:28,960
generate a random number
4536
03:00:27,439 --> 03:00:29,920
between
4537
03:00:28,960 --> 03:00:31,439
min
4538
03:00:29,920 --> 03:00:33,359
and max
4539
03:00:31,439 --> 03:00:34,720
now before we actually move on let's
4540
03:00:33,359 --> 03:00:36,399
test this just to be sure that
4541
03:00:34,720 --> 03:00:39,040
everything's working
4542
03:00:36,399 --> 03:00:40,720
so let's print whatever the answer is so
4543
03:00:39,040 --> 03:00:42,160
we're displaying an integer we are
4544
03:00:40,720 --> 03:00:43,200
displaying
4545
03:00:42,160 --> 03:00:46,240
answer
4546
03:00:43,200 --> 03:00:48,800
and let's see what this is
4547
03:00:46,240 --> 03:00:52,000
so my answer this round is 73 if i run
4548
03:00:48,800 --> 03:00:54,160
this again it is 93. now if i change
4549
03:00:52,000 --> 03:00:56,240
these values we will adjust the range in
4550
03:00:54,160 --> 03:01:00,000
which some random numbers will generate
4551
03:00:56,240 --> 03:01:02,640
so if i set min to 100 and max to 1000
4552
03:01:00,000 --> 03:01:06,080
that should change the range of numbers
4553
03:01:02,640 --> 03:01:08,399
and this time i have 334 but i'll change
4554
03:01:06,080 --> 03:01:10,319
this back to 1 and 100
4555
03:01:08,399 --> 03:01:12,319
okay so we know that we're generating a
4556
03:01:10,319 --> 03:01:14,880
random number so let's move on
4557
03:01:12,319 --> 03:01:16,880
let's create a do while loop so it's
4558
03:01:14,880 --> 03:01:18,720
going to take at least one guess in
4559
03:01:16,880 --> 03:01:22,960
order for somebody to guess the correct
4560
03:01:18,720 --> 03:01:25,520
answer so do curly braces we'll add
4561
03:01:22,960 --> 03:01:27,920
while and check a condition at the end
4562
03:01:25,520 --> 03:01:30,000
and the condition is guess
4563
03:01:27,920 --> 03:01:32,720
does not equal
4564
03:01:30,000 --> 03:01:35,120
answer so let's create a prompt and ask
4565
03:01:32,720 --> 03:01:37,840
the user to enter a guess
4566
03:01:35,120 --> 03:01:40,319
enter a guess
4567
03:01:37,840 --> 03:01:43,600
and we will use scanf to accept some
4568
03:01:40,319 --> 03:01:45,760
user input so this is an integer
4569
03:01:43,600 --> 03:01:49,279
and we are storing this within
4570
03:01:45,760 --> 03:01:51,120
guess so address of operator guess
4571
03:01:49,279 --> 03:01:52,880
now that we have our user's guess let's
4572
03:01:51,120 --> 03:01:54,720
compare it to our answer
4573
03:01:52,880 --> 03:01:56,880
so we'll use an if statement and check
4574
03:01:54,720 --> 03:02:00,319
to see if guess
4575
03:01:56,880 --> 03:02:02,080
is greater than answer so that means a
4576
03:02:00,319 --> 03:02:03,279
user guessed a number that was a little
4577
03:02:02,080 --> 03:02:05,760
too high
4578
03:02:03,279 --> 03:02:07,359
so let's print a message to let the user
4579
03:02:05,760 --> 03:02:08,080
know
4580
03:02:07,359 --> 03:02:10,240
to
4581
03:02:08,080 --> 03:02:10,240
hi
4582
03:02:10,479 --> 03:02:14,479
and then we can use an else if statement
4583
03:02:12,720 --> 03:02:15,439
else if
4584
03:02:14,479 --> 03:02:17,600
guess
4585
03:02:15,439 --> 03:02:20,800
is less than answer well then their
4586
03:02:17,600 --> 03:02:20,800
guess was too low
4587
03:02:21,520 --> 03:02:26,880
print too low
4588
03:02:24,479 --> 03:02:29,120
so if our guess is not greater than the
4589
03:02:26,880 --> 03:02:31,040
answer or less than the answer that
4590
03:02:29,120 --> 03:02:35,279
means they must have got it right
4591
03:02:31,040 --> 03:02:35,279
so within an else block let's print
4592
03:02:35,760 --> 03:02:40,160
correct
4593
03:02:37,600 --> 03:02:43,359
so then after our if else statements
4594
03:02:40,160 --> 03:02:44,560
let's increase guesses by one guesses
4595
03:02:43,359 --> 03:02:46,720
plus plus
4596
03:02:44,560 --> 03:02:48,560
the very best possible score a user can
4597
03:02:46,720 --> 03:02:51,359
get is one if they guess it on the first
4598
03:02:48,560 --> 03:02:54,160
try so before we finish a while loop we
4599
03:02:51,359 --> 03:02:56,399
will increment guesses by one
4600
03:02:54,160 --> 03:02:59,200
so then once we escape our while loop
4601
03:02:56,399 --> 03:03:00,800
let's print the user's final score
4602
03:02:59,200 --> 03:03:03,359
so we will print
4603
03:03:00,800 --> 03:03:05,920
the answer
4604
03:03:03,359 --> 03:03:07,840
this is an integer
4605
03:03:05,920 --> 03:03:10,319
and we are displaying
4606
03:03:07,840 --> 03:03:10,319
answer
4607
03:03:11,680 --> 03:03:15,359
and we will display the guesses it took
4608
03:03:14,479 --> 03:03:16,640
so
4609
03:03:15,359 --> 03:03:18,240
guesses
4610
03:03:16,640 --> 03:03:19,760
and display the value found within
4611
03:03:18,240 --> 03:03:21,359
guesses
4612
03:03:19,760 --> 03:03:24,160
and this part isn't necessary but i like
4613
03:03:21,359 --> 03:03:26,080
to add some text decorations
4614
03:03:24,160 --> 03:03:29,040
so maybe i'll add a bunch of asterisks
4615
03:03:26,080 --> 03:03:29,040
to make it look nice
4616
03:03:29,680 --> 03:03:32,319
let's run it
4617
03:03:32,479 --> 03:03:37,040
enter a guess i'll guess something right
4618
03:03:34,080 --> 03:03:40,960
in the middle between so maybe 50.
4619
03:03:37,040 --> 03:03:42,800
so that guess was too low so maybe 75
4620
03:03:40,960 --> 03:03:48,080
okay that was too high so our numbers
4621
03:03:42,800 --> 03:03:51,760
between 50 and 75 how about 62
4622
03:03:48,080 --> 03:03:53,439
that's too low so it's between 62 and 75
4623
03:03:51,760 --> 03:03:56,080
how about 69
4624
03:03:53,439 --> 03:03:57,279
that's still too low so between 69 and
4625
03:03:56,080 --> 03:03:59,120
75
4626
03:03:57,279 --> 03:04:01,439
maybe 72
4627
03:03:59,120 --> 03:04:03,120
that's still too low so it's between 72
4628
03:04:01,439 --> 03:04:04,399
and 75
4629
03:04:03,120 --> 03:04:06,560
73
4630
03:04:04,399 --> 03:04:08,399
all right that was the right answer i'm
4631
03:04:06,560 --> 03:04:11,439
just gonna fix one thing real quick
4632
03:04:08,399 --> 03:04:13,760
uh let's add a new line here okay let's
4633
03:04:11,439 --> 03:04:17,439
try this again okay until i guess i'll
4634
03:04:13,760 --> 03:04:19,760
try 50 again that's too high maybe 25
4635
03:04:17,439 --> 03:04:20,960
that's still too high what about 12
4636
03:04:19,760 --> 03:04:22,479
2 low
4637
03:04:20,960 --> 03:04:24,720
maybe 18
4638
03:04:22,479 --> 03:04:28,720
2 low 21
4639
03:04:24,720 --> 03:04:31,279
that's still too low 23 too low 24
4640
03:04:28,720 --> 03:04:33,120
and 24 was the correct answer all right
4641
03:04:31,279 --> 03:04:34,880
everybody so i thought that would be a
4642
03:04:33,120 --> 03:04:36,960
fun guessing game we could create for
4643
03:04:34,880 --> 03:04:38,479
practice if you would like a copy of all
4644
03:04:36,960 --> 03:04:41,200
this i'll post this to the comment
4645
03:04:38,479 --> 03:04:45,600
section down below and well yeah that is
4646
03:04:41,200 --> 03:04:45,600
a simple number guessing game in c
4647
03:04:47,359 --> 03:04:51,279
all right people so now that we know how
4648
03:04:49,520 --> 03:04:53,040
to create an array of strings i thought
4649
03:04:51,279 --> 03:04:55,680
this would be a good opportunity for us
4650
03:04:53,040 --> 03:04:57,600
to create a quiz game so before we begin
4651
03:04:55,680 --> 03:04:59,840
make sure to include these two header
4652
03:04:57,600 --> 03:05:02,000
files at the top of your program so
4653
03:04:59,840 --> 03:05:04,160
let's begin with a 2d array of
4654
03:05:02,000 --> 03:05:06,479
characters and these will store our
4655
03:05:04,160 --> 03:05:08,720
questions
4656
03:05:06,479 --> 03:05:11,520
and i'm going to set a maximum size for
4657
03:05:08,720 --> 03:05:13,279
each question to 100 bytes
4658
03:05:11,520 --> 03:05:15,520
and we can go ahead and initialize this
4659
03:05:13,279 --> 03:05:17,680
array with some questions so remember
4660
03:05:15,520 --> 03:05:18,479
with strings each string is its own
4661
03:05:17,680 --> 03:05:20,800
array
4662
03:05:18,479 --> 03:05:22,560
so let's ask maybe three questions but
4663
03:05:20,800 --> 03:05:23,840
feel free to pick any amount that you
4664
03:05:22,560 --> 03:05:25,200
like
4665
03:05:23,840 --> 03:05:27,520
and come up with any questions you would
4666
03:05:25,200 --> 03:05:29,760
like as well or you can copy me i don't
4667
03:05:27,520 --> 03:05:29,760
care
4668
03:05:31,200 --> 03:05:37,120
what year did the c language debut then
4669
03:05:34,080 --> 03:05:38,479
separate each string with a comma
4670
03:05:37,120 --> 03:05:40,479
then just to make this more readable i'm
4671
03:05:38,479 --> 03:05:43,920
going to go down to the next line
4672
03:05:40,479 --> 03:05:43,920
so this is question two now
4673
03:05:44,479 --> 03:05:50,399
who is credited with creating c
4674
03:05:47,279 --> 03:05:50,399
and one more question
4675
03:05:52,240 --> 03:05:56,960
what is the predecessor of c so now we
4676
03:05:55,200 --> 03:05:59,760
have some questions we'll need some
4677
03:05:56,960 --> 03:06:02,000
options this could be a separate 2d
4678
03:05:59,760 --> 03:06:03,120
character array
4679
03:06:02,000 --> 03:06:04,640
char
4680
03:06:03,120 --> 03:06:06,800
options
4681
03:06:04,640 --> 03:06:09,200
and i'll copy the dimensions from
4682
03:06:06,800 --> 03:06:11,520
questions
4683
03:06:09,200 --> 03:06:15,000
and we'll create four sets of options
4684
03:06:11,520 --> 03:06:15,000
for each question
4685
03:06:18,640 --> 03:06:23,040
this is the first set of options for the
4686
03:06:20,880 --> 03:06:24,800
first question and then let's add a
4687
03:06:23,040 --> 03:06:27,800
second set of options for the second
4688
03:06:24,800 --> 03:06:27,800
question
4689
03:06:36,640 --> 03:06:40,279
and a third set
4690
03:06:45,439 --> 03:06:49,439
now we'll create an answer key this will
4691
03:06:47,120 --> 03:06:51,120
be a one-dimensional array
4692
03:06:49,439 --> 03:06:54,720
so char
4693
03:06:51,120 --> 03:06:56,960
answers and the size will be three
4694
03:06:54,720 --> 03:06:59,840
and i will set this equal to
4695
03:06:56,960 --> 03:07:02,240
my answer key so the correct answers for
4696
03:06:59,840 --> 03:07:03,279
my quiz are b
4697
03:07:02,240 --> 03:07:04,960
a
4698
03:07:03,279 --> 03:07:06,960
b
4699
03:07:04,960 --> 03:07:09,680
so these will be individual characters
4700
03:07:06,960 --> 03:07:10,560
all separated with a comma b
4701
03:07:09,680 --> 03:07:11,520
a
4702
03:07:10,560 --> 03:07:13,120
b
4703
03:07:11,520 --> 03:07:14,720
so let's calculate the number of
4704
03:07:13,120 --> 03:07:16,880
questions that we have
4705
03:07:14,720 --> 03:07:18,080
so i will declare int
4706
03:07:16,880 --> 03:07:19,359
number
4707
03:07:18,080 --> 03:07:20,800
of
4708
03:07:19,359 --> 03:07:22,720
questions
4709
03:07:20,800 --> 03:07:24,560
and set this equal to
4710
03:07:22,720 --> 03:07:26,800
sizeof operator
4711
03:07:24,560 --> 03:07:28,080
pass in our two-dimensional array of
4712
03:07:26,800 --> 03:07:30,240
questions
4713
03:07:28,080 --> 03:07:32,319
and divide this by one of the elements
4714
03:07:30,240 --> 03:07:35,279
because they all have the same size
4715
03:07:32,319 --> 03:07:36,880
questions at index zero
4716
03:07:35,279 --> 03:07:40,160
and let's declare
4717
03:07:36,880 --> 03:07:42,399
char guess to store our guess
4718
03:07:40,160 --> 03:07:43,840
and int score to keep track of the
4719
03:07:42,399 --> 03:07:46,800
player's score
4720
03:07:43,840 --> 03:07:50,160
so now let's begin this i will print
4721
03:07:46,800 --> 03:07:50,160
the title quiz game
4722
03:07:50,720 --> 03:07:54,960
then add a new line
4723
03:07:52,880 --> 03:07:59,040
okay now we need to loop through all of
4724
03:07:54,960 --> 03:08:02,160
the questions so let's use a for loop
4725
03:07:59,040 --> 03:08:03,600
so we will have an index of i set to
4726
03:08:02,160 --> 03:08:05,840
zero
4727
03:08:03,600 --> 03:08:08,560
and i would like to loop through this as
4728
03:08:05,840 --> 03:08:10,240
long as i is less than the number of
4729
03:08:08,560 --> 03:08:12,560
questions that we have
4730
03:08:10,240 --> 03:08:14,560
then increment i by one
4731
03:08:12,560 --> 03:08:16,960
and to test this let's print our
4732
03:08:14,560 --> 03:08:18,880
questions
4733
03:08:16,960 --> 03:08:21,359
so printf
4734
03:08:18,880 --> 03:08:24,000
we're displaying a string so use the s
4735
03:08:21,359 --> 03:08:27,760
format specifier
4736
03:08:24,000 --> 03:08:29,600
questions add index of i
4737
03:08:27,760 --> 03:08:32,160
and then i'll add a new line character
4738
03:08:29,600 --> 03:08:34,240
after each printf statement okay let's
4739
03:08:32,160 --> 03:08:36,319
at least test this so we should cycle
4740
03:08:34,240 --> 03:08:38,479
through our questions
4741
03:08:36,319 --> 03:08:40,800
okay here are my questions what year did
4742
03:08:38,479 --> 03:08:42,319
the c language debut who is credited
4743
03:08:40,800 --> 03:08:44,560
with creating c
4744
03:08:42,319 --> 03:08:46,160
what is the predecessor of c
4745
03:08:44,560 --> 03:08:49,120
and this part isn't necessary but i'm
4746
03:08:46,160 --> 03:08:51,680
going to add some text decorations
4747
03:08:49,120 --> 03:08:53,520
i'll just display a bunch of asterisks
4748
03:08:51,680 --> 03:08:55,120
i'll do that before and after the
4749
03:08:53,520 --> 03:08:57,520
question
4750
03:08:55,120 --> 03:09:00,080
and let's try that again oh then add a
4751
03:08:57,520 --> 03:09:01,680
new line character
4752
03:09:00,080 --> 03:09:04,160
that's a little bit better
4753
03:09:01,680 --> 03:09:06,160
after our question let's display the
4754
03:09:04,160 --> 03:09:09,279
possible options
4755
03:09:06,160 --> 03:09:11,520
so we will need a nested for loop
4756
03:09:09,279 --> 03:09:12,800
so four
4757
03:09:11,520 --> 03:09:13,840
now this part's gonna be a little bit
4758
03:09:12,800 --> 03:09:16,800
tricky
4759
03:09:13,840 --> 03:09:19,120
we need to begin our for loop at every
4760
03:09:16,800 --> 03:09:22,239
fourth string
4761
03:09:19,120 --> 03:09:24,080
within our for loop let's set int j
4762
03:09:22,239 --> 03:09:26,560
equal to
4763
03:09:24,080 --> 03:09:29,200
i times four
4764
03:09:26,560 --> 03:09:32,479
so during the first iteration i will be
4765
03:09:29,200 --> 03:09:34,640
zero so zero times four is zero but
4766
03:09:32,479 --> 03:09:38,479
during the next iteration of the outer
4767
03:09:34,640 --> 03:09:40,720
for loop i is going to equal one so one
4768
03:09:38,479 --> 03:09:43,279
times four is four
4769
03:09:40,720 --> 03:09:46,800
so that way we will begin the inner for
4770
03:09:43,279 --> 03:09:49,040
loop with every fourth string within our
4771
03:09:46,800 --> 03:09:51,680
2d array of options
4772
03:09:49,040 --> 03:09:54,479
and the condition is going to be
4773
03:09:51,680 --> 03:09:55,359
j is less than
4774
03:09:54,479 --> 03:09:56,560
i
4775
03:09:55,359 --> 03:09:58,399
times 4
4776
03:09:56,560 --> 03:09:59,760
and then we would like to display
4777
03:09:58,399 --> 03:10:02,319
four strings
4778
03:09:59,760 --> 03:10:05,600
this for loop will cycle four times
4779
03:10:02,319 --> 03:10:08,800
beginning at every fourth string
4780
03:10:05,600 --> 03:10:12,080
and then we need to increment j by one
4781
03:10:08,800 --> 03:10:15,040
now let's print every option
4782
03:10:12,080 --> 03:10:17,040
so this is a string i'll add a new line
4783
03:10:15,040 --> 03:10:20,160
and we are printing
4784
03:10:17,040 --> 03:10:21,920
options at index of j
4785
03:10:20,160 --> 03:10:22,880
and let's test this just to be sure that
4786
03:10:21,920 --> 03:10:25,600
it works
4787
03:10:22,880 --> 03:10:28,560
okay so we have our question followed by
4788
03:10:25,600 --> 03:10:30,720
four possible options
4789
03:10:28,560 --> 03:10:32,720
now let's accept some user input after
4790
03:10:30,720 --> 03:10:34,319
the nested for loop currently our
4791
03:10:32,720 --> 03:10:36,160
program isn't going to wait around for
4792
03:10:34,319 --> 03:10:38,319
any user input that's why i displayed
4793
03:10:36,160 --> 03:10:40,720
all the questions and options okay we
4794
03:10:38,319 --> 03:10:42,160
will create a prompt to have a user type
4795
03:10:40,720 --> 03:10:45,359
in a guess
4796
03:10:42,160 --> 03:10:47,040
guess and we will use scanf to accept
4797
03:10:45,359 --> 03:10:50,000
some user input
4798
03:10:47,040 --> 03:10:53,439
we are accepting a single character
4799
03:10:50,000 --> 03:10:54,800
and use the address of operator guess
4800
03:10:53,439 --> 03:10:56,239
now let me show you what happens when we
4801
03:10:54,800 --> 03:10:57,840
run this real quick
4802
03:10:56,239 --> 03:10:59,359
so i'm just going to type in anything
4803
03:10:57,840 --> 03:11:01,920
let's say a
4804
03:10:59,359 --> 03:11:04,319
okay so we skipped question two
4805
03:11:01,920 --> 03:11:06,479
that's because after using scanf when we
4806
03:11:04,319 --> 03:11:08,560
accept a character we have that new line
4807
03:11:06,479 --> 03:11:10,000
character within the input buffer so we
4808
03:11:08,560 --> 03:11:12,000
need to clear that
4809
03:11:10,000 --> 03:11:15,359
so one way in which we can clear that is
4810
03:11:12,000 --> 03:11:15,359
to use scanf again
4811
03:11:15,600 --> 03:11:21,040
use the c format specifier and that's it
4812
03:11:18,560 --> 03:11:23,359
so this will clear the new line
4813
03:11:21,040 --> 03:11:24,479
character from input
4814
03:11:23,359 --> 03:11:26,239
buffer
4815
03:11:24,479 --> 03:11:27,600
so this should no longer just skip
4816
03:11:26,239 --> 03:11:28,560
question two
4817
03:11:27,600 --> 03:11:30,000
okay
4818
03:11:28,560 --> 03:11:30,880
let's say a
4819
03:11:30,000 --> 03:11:32,239
b
4820
03:11:30,880 --> 03:11:34,239
c
4821
03:11:32,239 --> 03:11:35,439
okay so we know we can accept some user
4822
03:11:34,239 --> 03:11:37,120
input now
4823
03:11:35,439 --> 03:11:39,520
now here's one thing what if somebody
4824
03:11:37,120 --> 03:11:40,800
types in a lowercase character i think
4825
03:11:39,520 --> 03:11:43,040
that should still count even though
4826
03:11:40,800 --> 03:11:46,319
we're looking for an uppercase character
4827
03:11:43,040 --> 03:11:48,319
so let's take our guess and use the two
4828
03:11:46,319 --> 03:11:51,359
upper function to make it uppercase
4829
03:11:48,319 --> 03:11:53,279
guess equals to upper
4830
03:11:51,359 --> 03:11:54,960
then pass in guess
4831
03:11:53,279 --> 03:11:57,279
so basically we're passing in our guess
4832
03:11:54,960 --> 03:11:58,720
making it uppercase and reassigning it
4833
03:11:57,279 --> 03:12:00,880
to our guess
4834
03:11:58,720 --> 03:12:03,200
and let's write an if else statement to
4835
03:12:00,880 --> 03:12:06,319
check our guess if it's equal to our
4836
03:12:03,200 --> 03:12:08,880
answer so if else
4837
03:12:06,319 --> 03:12:09,760
our condition within our if statement if
4838
03:12:08,880 --> 03:12:13,120
guess
4839
03:12:09,760 --> 03:12:15,279
is equal to answer at index of i
4840
03:12:13,120 --> 03:12:17,520
remember that we're still within the for
4841
03:12:15,279 --> 03:12:20,479
loop the outer for loop so we have that
4842
03:12:17,520 --> 03:12:23,359
index oh that's answers plural
4843
03:12:20,479 --> 03:12:25,920
if a user's guess is equal to the answer
4844
03:12:23,359 --> 03:12:29,680
then let's print correct
4845
03:12:25,920 --> 03:12:32,319
and then give them one point so printf
4846
03:12:29,680 --> 03:12:35,120
correct
4847
03:12:32,319 --> 03:12:37,279
and then increment our score score plus
4848
03:12:35,120 --> 03:12:40,160
plus
4849
03:12:37,279 --> 03:12:41,439
else we will print wrong and you can
4850
03:12:40,160 --> 03:12:43,600
give the correct answer if you would
4851
03:12:41,439 --> 03:12:45,840
like just do that within here now once
4852
03:12:43,600 --> 03:12:48,319
we cycle through all of the questions we
4853
03:12:45,840 --> 03:12:50,239
can display a user score so be sure to
4854
03:12:48,319 --> 03:12:52,560
do that outside of the for loop the
4855
03:12:50,239 --> 03:12:53,840
outer for loop that is so let's use
4856
03:12:52,560 --> 03:12:55,200
printf
4857
03:12:53,840 --> 03:12:57,680
and display
4858
03:12:55,200 --> 03:13:00,960
final score
4859
03:12:57,680 --> 03:13:03,279
so i'm going to display two values
4860
03:13:00,960 --> 03:13:06,080
so the first will be the player score
4861
03:13:03,279 --> 03:13:08,720
divided by the number of questions
4862
03:13:06,080 --> 03:13:11,920
so we have two integers to work with and
4863
03:13:08,720 --> 03:13:14,399
we are displaying score and number of
4864
03:13:11,920 --> 03:13:16,000
questions and then if you want you can
4865
03:13:14,399 --> 03:13:17,520
add some text decoration to make it look
4866
03:13:16,000 --> 03:13:19,120
cool
4867
03:13:17,520 --> 03:13:21,439
so i'm going to run through this quiz
4868
03:13:19,120 --> 03:13:22,479
once and intentionally get one question
4869
03:13:21,439 --> 03:13:24,560
wrong
4870
03:13:22,479 --> 03:13:28,319
so what year did the c language debut
4871
03:13:24,560 --> 03:13:30,319
that would be 1972 that's b who is
4872
03:13:28,319 --> 03:13:32,560
credited with creating c
4873
03:13:30,319 --> 03:13:34,560
uh let's say doc brown
4874
03:13:32,560 --> 03:13:36,880
so d
4875
03:13:34,560 --> 03:13:39,520
and what is the predecessor of c that
4876
03:13:36,880 --> 03:13:41,920
would be b
4877
03:13:39,520 --> 03:13:42,800
okay and my final score is two out of
4878
03:13:41,920 --> 03:13:44,720
three
4879
03:13:42,800 --> 03:13:46,720
so let's try that again and this time i
4880
03:13:44,720 --> 03:13:49,520
will try and get one hundred percent so
4881
03:13:46,720 --> 03:13:51,520
the correct answer is b
4882
03:13:49,520 --> 03:13:53,680
a dennis ritchie
4883
03:13:51,520 --> 03:13:56,479
and b
4884
03:13:53,680 --> 03:13:58,720
and my final score is three out of three
4885
03:13:56,479 --> 03:14:00,880
so yeah i thought that would be a simple
4886
03:13:58,720 --> 03:14:02,319
quiz game in c if you would like a copy
4887
03:14:00,880 --> 03:14:05,040
of this code i'll post this to the
4888
03:14:02,319 --> 03:14:10,200
comment section down below and well yeah
4889
03:14:05,040 --> 03:14:10,200
that's a simple quiz game in c
4890
03:14:11,279 --> 03:14:16,399
hey everybody let's talk about bitwise
4891
03:14:13,680 --> 03:14:18,960
operators these are special operators
4892
03:14:16,399 --> 03:14:20,960
used in a bit level programming
4893
03:14:18,960 --> 03:14:23,200
knowing binary is important for this
4894
03:14:20,960 --> 03:14:25,040
topic so if you don't know how binary
4895
03:14:23,200 --> 03:14:27,040
works i would recommend skipping to the
4896
03:14:25,040 --> 03:14:29,120
next topic but if you're comfortable
4897
03:14:27,040 --> 03:14:31,439
with binary well this video is for you
4898
03:14:29,120 --> 03:14:34,560
so let's begin i'm going to discuss five
4899
03:14:31,439 --> 03:14:37,359
different bitwise operators and or
4900
03:14:34,560 --> 03:14:40,960
exclusive or left shift and right shift
4901
03:14:37,359 --> 03:14:43,840
let's create three variables x y and z
4902
03:14:40,960 --> 03:14:46,160
x will equal six
4903
03:14:43,840 --> 03:14:48,560
y will equal twelve
4904
03:14:46,160 --> 03:14:50,479
and z will store our result and we will
4905
03:14:48,560 --> 03:14:52,319
set that to be zero
4906
03:14:50,479 --> 03:14:55,279
so i'm going to give you the binary
4907
03:14:52,319 --> 03:14:58,000
representation of each of these numbers
4908
03:14:55,279 --> 03:15:00,880
so six is
4909
03:14:58,000 --> 03:15:03,040
this number so we have eight bits that's
4910
03:15:00,880 --> 03:15:06,640
a byte and this is the binary
4911
03:15:03,040 --> 03:15:09,840
representation of the number six
4912
03:15:06,640 --> 03:15:12,720
12 would be this binary number
4913
03:15:09,840 --> 03:15:15,760
and zero zero is easy
4914
03:15:12,720 --> 03:15:17,840
that would be all zeros so let's use the
4915
03:15:15,760 --> 03:15:21,359
and bitwise operator so what we're going
4916
03:15:17,840 --> 03:15:23,520
to do is set z equal to x
4917
03:15:21,359 --> 03:15:25,279
and y
4918
03:15:23,520 --> 03:15:28,800
so imagine this imagine that we're
4919
03:15:25,279 --> 03:15:31,120
aligning all of these bits in columns
4920
03:15:28,800 --> 03:15:33,840
using the and bitwise operator we're
4921
03:15:31,120 --> 03:15:36,000
going to look at our operands x and y if
4922
03:15:33,840 --> 03:15:37,760
both of these are a one we will assign
4923
03:15:36,000 --> 03:15:39,520
one to our result
4924
03:15:37,760 --> 03:15:41,760
and let's begin with the first column on
4925
03:15:39,520 --> 03:15:44,000
the right both of these are zeros so our
4926
03:15:41,760 --> 03:15:46,800
result will stay at zero and with the
4927
03:15:44,000 --> 03:15:49,279
next column we have one and zero and
4928
03:15:46,800 --> 03:15:51,600
using the and bitwise operator both of
4929
03:15:49,279 --> 03:15:53,760
these need to be 1 in order to assign
4930
03:15:51,600 --> 03:15:56,080
one to our result so we will skip this
4931
03:15:53,760 --> 03:15:59,200
column now with our third column these
4932
03:15:56,080 --> 03:16:02,000
are both 1 so we will assign 1 to our
4933
03:15:59,200 --> 03:16:05,120
result the next column is zero and one
4934
03:16:02,000 --> 03:16:07,840
we skip it and the rest are just zero so
4935
03:16:05,120 --> 03:16:10,479
this binary number is the binary
4936
03:16:07,840 --> 03:16:14,000
representation of the number four
4937
03:16:10,479 --> 03:16:17,600
so let's test that z equals x and y and
4938
03:16:14,000 --> 03:16:20,960
let's print z
4939
03:16:17,600 --> 03:16:22,800
and equals we're displaying an integer
4940
03:16:20,960 --> 03:16:25,200
i'll add a new line
4941
03:16:22,800 --> 03:16:27,439
and we are displaying z
4942
03:16:25,200 --> 03:16:30,800
and let's see if this is for
4943
03:16:27,439 --> 03:16:33,279
yep our result z is equal to four now
4944
03:16:30,800 --> 03:16:35,040
let's move on to or so let's set this
4945
03:16:33,279 --> 03:16:36,720
back to zero
4946
03:16:35,040 --> 03:16:38,560
and we will set z
4947
03:16:36,720 --> 03:16:42,160
equal to x
4948
03:16:38,560 --> 03:16:44,640
or y or is represented by a single
4949
03:16:42,160 --> 03:16:46,640
vertical bar
4950
03:16:44,640 --> 03:16:48,880
and we will print
4951
03:16:46,640 --> 03:16:51,200
or let's try and guess what z is going
4952
03:16:48,880 --> 03:16:53,920
to be before we actually display it
4953
03:16:51,200 --> 03:16:56,160
with the or bitwise operator only one of
4954
03:16:53,920 --> 03:16:58,479
these bits needs to be one in order for
4955
03:16:56,160 --> 03:17:00,720
us to assign one to the result
4956
03:16:58,479 --> 03:17:02,239
so since these are both zero this will
4957
03:17:00,720 --> 03:17:04,720
stay at zero
4958
03:17:02,239 --> 03:17:07,040
this bit is one this one's zero so this
4959
03:17:04,720 --> 03:17:08,880
would be one and for the next column of
4960
03:17:07,040 --> 03:17:09,840
bits these are both one so that counts
4961
03:17:08,880 --> 03:17:12,239
as well
4962
03:17:09,840 --> 03:17:14,800
and we have a one in here so that is one
4963
03:17:12,239 --> 03:17:17,760
and the rest are just zero so this is
4964
03:17:14,800 --> 03:17:20,239
the binary representation of the number
4965
03:17:17,760 --> 03:17:22,000
fourteen and let's see if z is equal to
4966
03:17:20,239 --> 03:17:23,840
fourteen
4967
03:17:22,000 --> 03:17:26,800
yep we got that right
4968
03:17:23,840 --> 03:17:28,720
so let's set this back to zero
4969
03:17:26,800 --> 03:17:31,680
now with exclusive ore that is
4970
03:17:28,720 --> 03:17:34,880
represented by a carrot with exclusive
4971
03:17:31,680 --> 03:17:37,439
ore only one of these bits can be won if
4972
03:17:34,880 --> 03:17:39,600
neither or both of these bits are one we
4973
03:17:37,439 --> 03:17:42,319
don't assign anything so both of these
4974
03:17:39,600 --> 03:17:44,479
bits are zero they will stay a zero
4975
03:17:42,319 --> 03:17:46,880
we have one of these bits as one and not
4976
03:17:44,479 --> 03:17:48,800
the other so that counts as one now with
4977
03:17:46,880 --> 03:17:51,520
this next column both of these are one
4978
03:17:48,800 --> 03:17:54,080
and using exclusive ore only one of
4979
03:17:51,520 --> 03:17:56,160
these can be one so since these are both
4980
03:17:54,080 --> 03:17:58,080
ones we do not assign anything so we
4981
03:17:56,160 --> 03:18:00,560
keep that as zero and with the next
4982
03:17:58,080 --> 03:18:02,319
column only one of these is one so we
4983
03:18:00,560 --> 03:18:05,040
will set this to be one within the
4984
03:18:02,319 --> 03:18:08,080
result and this is the binary
4985
03:18:05,040 --> 03:18:09,520
representation of ten and let's test
4986
03:18:08,080 --> 03:18:10,399
that
4987
03:18:09,520 --> 03:18:12,479
z
4988
03:18:10,399 --> 03:18:15,920
equals x
4989
03:18:12,479 --> 03:18:19,840
exclusive or y
4990
03:18:15,920 --> 03:18:22,880
and we will print exclusive or equals z
4991
03:18:19,840 --> 03:18:26,479
and let's see if that's ten
4992
03:18:22,880 --> 03:18:29,040
yep z is equal to ten now here's left
4993
03:18:26,479 --> 03:18:31,920
shift we will shift these bits so many
4994
03:18:29,040 --> 03:18:33,600
spaces to the left using left shift so
4995
03:18:31,920 --> 03:18:37,920
we will set z
4996
03:18:33,600 --> 03:18:40,800
equal to x and then left shift which is
4997
03:18:37,920 --> 03:18:43,279
represented by two left angle brackets a
4998
03:18:40,800 --> 03:18:45,920
number of spaces let's begin with one
4999
03:18:43,279 --> 03:18:48,160
so we have x equal to six
5000
03:18:45,920 --> 03:18:50,720
what we're doing is shifting these bits
5001
03:18:48,160 --> 03:18:52,800
one spot to the left
5002
03:18:50,720 --> 03:18:55,600
and then adding zero at the end
5003
03:18:52,800 --> 03:18:57,840
and this is the binary representation of
5004
03:18:55,600 --> 03:18:59,840
the number 12. actually it's the same as
5005
03:18:57,840 --> 03:19:00,800
y currently these binary numbers are the
5006
03:18:59,840 --> 03:19:04,520
same
5007
03:19:00,800 --> 03:19:04,520
and let's print that
5008
03:19:04,640 --> 03:19:07,840
so
5009
03:19:05,600 --> 03:19:10,800
shift left
5010
03:19:07,840 --> 03:19:11,600
and z should equal 12
5011
03:19:10,800 --> 03:19:14,160
yep
5012
03:19:11,600 --> 03:19:16,239
and if i shift left twice
5013
03:19:14,160 --> 03:19:22,000
well then we are just moving these bits
5014
03:19:16,239 --> 03:19:22,000
one more space and now this is 24.
5015
03:19:22,560 --> 03:19:25,279
there you go
5016
03:19:23,600 --> 03:19:27,680
so you probably noticed that there's a
5017
03:19:25,279 --> 03:19:29,439
pattern every time we shift a binary
5018
03:19:27,680 --> 03:19:30,479
number to the left it effectively
5019
03:19:29,439 --> 03:19:32,800
doubles
5020
03:19:30,479 --> 03:19:35,439
now let's right shift
5021
03:19:32,800 --> 03:19:37,120
so let's copy this
5022
03:19:35,439 --> 03:19:40,479
paste it
5023
03:19:37,120 --> 03:19:43,359
and we will use two right angle brackets
5024
03:19:40,479 --> 03:19:46,640
and this is shift right
5025
03:19:43,359 --> 03:19:48,640
and let's set this to 6.
5026
03:19:46,640 --> 03:19:51,279
if we're right shifting we shift these
5027
03:19:48,640 --> 03:19:53,920
bits one spot to the right and this is
5028
03:19:51,279 --> 03:19:57,120
the binary representation of the number
5029
03:19:53,920 --> 03:20:00,479
three and if i print this
5030
03:19:57,120 --> 03:20:01,920
after shifting right once our result z
5031
03:20:00,479 --> 03:20:03,439
is three
5032
03:20:01,920 --> 03:20:05,600
every time we shift right we're
5033
03:20:03,439 --> 03:20:07,760
effectively cutting our number in half
5034
03:20:05,600 --> 03:20:09,840
now if i shift right again
5035
03:20:07,760 --> 03:20:11,840
we will shift these bits one more spot
5036
03:20:09,840 --> 03:20:14,239
to the right and we are going to
5037
03:20:11,840 --> 03:20:16,720
truncate this one at the end and this
5038
03:20:14,239 --> 03:20:18,960
will give us one that is the binary
5039
03:20:16,720 --> 03:20:21,920
representation of the number one
5040
03:20:18,960 --> 03:20:24,800
so technically half of three is 1.5 but
5041
03:20:21,920 --> 03:20:27,359
we can't store that 0.5 portion so it
5042
03:20:24,800 --> 03:20:30,319
just gets truncated all right everybody
5043
03:20:27,359 --> 03:20:33,279
those are just a few bitwise operators
5044
03:20:30,319 --> 03:20:35,600
they are special operators used in bit
5045
03:20:33,279 --> 03:20:37,359
level programming there's five that we
5046
03:20:35,600 --> 03:20:39,840
discussed today but there is another
5047
03:20:37,359 --> 03:20:42,239
known as the complement operator but
5048
03:20:39,840 --> 03:20:44,800
that's a little bit more complex and i
5049
03:20:42,239 --> 03:20:47,359
might save that for another video so
5050
03:20:44,800 --> 03:20:49,200
yeah those are bitwise operators if you
5051
03:20:47,359 --> 03:20:51,040
would like a copy of this code i'll post
5052
03:20:49,200 --> 03:20:53,279
this to the comments section down below
5053
03:20:51,040 --> 03:20:57,319
and well yeah those are some bitwise
5054
03:20:53,279 --> 03:20:57,319
operators in c
5055
03:20:58,000 --> 03:21:03,439
all right everybody so let's talk about
5056
03:21:00,319 --> 03:21:06,800
memory memory is an array of bytes
5057
03:21:03,439 --> 03:21:10,080
within ram and a memory block is a
5058
03:21:06,800 --> 03:21:13,120
single unit in bytes within memory
5059
03:21:10,080 --> 03:21:16,560
they're used to hold some value and a
5060
03:21:13,120 --> 03:21:19,680
memory address is the address of where a
5061
03:21:16,560 --> 03:21:22,319
memory block is located within ram i
5062
03:21:19,680 --> 03:21:24,560
like to think of memory as a street with
5063
03:21:22,319 --> 03:21:27,760
a bunch of different houses and each
5064
03:21:24,560 --> 03:21:29,920
house is a memory block which can hold
5065
03:21:27,760 --> 03:21:32,880
some value kind of like they're storing
5066
03:21:29,920 --> 03:21:35,359
a person and a memory address is the
5067
03:21:32,880 --> 03:21:37,840
house address on that street where that
5068
03:21:35,359 --> 03:21:40,160
memory block is located so this would be
5069
03:21:37,840 --> 03:21:42,479
a street address like one two three fake
5070
03:21:40,160 --> 03:21:45,279
street so when we declare variable let's
5071
03:21:42,479 --> 03:21:47,760
say we have char a when we declare a
5072
03:21:45,279 --> 03:21:51,040
variable we are setting some amount of
5073
03:21:47,760 --> 03:21:53,359
memory blocks aside to store some value
5074
03:21:51,040 --> 03:21:56,640
so the memory block that this variable
5075
03:21:53,359 --> 03:21:59,279
is using up has some memory address and
5076
03:21:56,640 --> 03:22:01,840
if i assign this variable a value let's
5077
03:21:59,279 --> 03:22:04,239
say maybe x well then if i was to go
5078
03:22:01,840 --> 03:22:07,200
into my computer's memory and look for
5079
03:22:04,239 --> 03:22:09,520
this address i would find this character
5080
03:22:07,200 --> 03:22:11,120
so imagine we're walking down the street
5081
03:22:09,520 --> 03:22:12,880
and we are looking for somebody's
5082
03:22:11,120 --> 03:22:15,600
address so we're going house to house
5083
03:22:12,880 --> 03:22:17,359
and we find a matching address if i were
5084
03:22:15,600 --> 03:22:19,359
to open their front door i should
5085
03:22:17,359 --> 03:22:22,080
probably knock first at least then i
5086
03:22:19,359 --> 03:22:24,000
will find the character x so that's kind
5087
03:22:22,080 --> 03:22:26,239
of an analogy i like to use when
5088
03:22:24,000 --> 03:22:29,439
thinking about memory so let's declare a
5089
03:22:26,239 --> 03:22:32,399
couple other characters we have char b
5090
03:22:29,439 --> 03:22:34,880
i'll give this a value of y
5091
03:22:32,399 --> 03:22:38,000
and char c
5092
03:22:34,880 --> 03:22:40,160
and i'll give this a character of z okay
5093
03:22:38,000 --> 03:22:42,560
so let's find the size of each of these
5094
03:22:40,160 --> 03:22:44,800
characters so i'm going to use a printf
5095
03:22:42,560 --> 03:22:46,000
statement i'm going to display an
5096
03:22:44,800 --> 03:22:48,319
integer
5097
03:22:46,000 --> 03:22:49,920
and the size of each of these characters
5098
03:22:48,319 --> 03:22:53,200
is within bytes
5099
03:22:49,920 --> 03:22:55,760
so i'm going to use the size of operator
5100
03:22:53,200 --> 03:22:57,680
and pass in one of my characters so i'm
5101
03:22:55,760 --> 03:23:02,239
going to pass in a
5102
03:22:57,680 --> 03:23:06,000
and i'll do the same thing with b and c
5103
03:23:02,239 --> 03:23:08,160
so the size of each of these characters
5104
03:23:06,000 --> 03:23:11,359
is one bite and i think i'm just going
5105
03:23:08,160 --> 03:23:11,359
to add a new line here
5106
03:23:11,920 --> 03:23:16,720
much better okay now we can actually
5107
03:23:14,479 --> 03:23:19,920
access the address of each of these
5108
03:23:16,720 --> 03:23:23,200
variables so each of these variables is
5109
03:23:19,920 --> 03:23:25,840
using one block of memory a single block
5110
03:23:23,200 --> 03:23:28,000
of memory is a byte
5111
03:23:25,840 --> 03:23:30,880
so within a printf statement i would
5112
03:23:28,000 --> 03:23:32,960
like to display an address so the format
5113
03:23:30,880 --> 03:23:36,080
specifier is p
5114
03:23:32,960 --> 03:23:38,560
and i will use the address of operator
5115
03:23:36,080 --> 03:23:40,880
then the name of one of my variables
5116
03:23:38,560 --> 03:23:42,239
so i'll display the address of variables
5117
03:23:40,880 --> 03:23:44,160
a b
5118
03:23:42,239 --> 03:23:47,760
and c
5119
03:23:44,160 --> 03:23:50,160
and then display each on a new line
5120
03:23:47,760 --> 03:23:52,960
so here are the memory addresses for
5121
03:23:50,160 --> 03:23:55,200
these three variables these addresses
5122
03:23:52,960 --> 03:23:57,840
are in hexadecimal these are the street
5123
03:23:55,200 --> 03:23:59,840
addresses of each of our memory blocks
5124
03:23:57,840 --> 03:24:02,080
on our fictional street
5125
03:23:59,840 --> 03:24:04,640
hey uh so this is bro from the future
5126
03:24:02,080 --> 03:24:07,760
just a quick crash course on hexadecimal
5127
03:24:04,640 --> 03:24:10,840
values with standard decimal values they
5128
03:24:07,760 --> 03:24:13,680
use the numbers 0 through 9 but with
5129
03:24:10,840 --> 03:24:17,520
hexadecimal you use the numbers 0
5130
03:24:13,680 --> 03:24:20,800
through 9 as well as a through f so that
5131
03:24:17,520 --> 03:24:23,120
gives you a total of 16 different values
5132
03:24:20,800 --> 03:24:25,279
for each digit so that's why there's a
5133
03:24:23,120 --> 03:24:28,080
mix of numbers and letters
5134
03:24:25,279 --> 03:24:30,399
so each of these variables uses one
5135
03:24:28,080 --> 03:24:32,720
memory block they use one byte of memory
5136
03:24:30,399 --> 03:24:35,120
so beginning with variable a
5137
03:24:32,720 --> 03:24:37,040
this has this street address but if we
5138
03:24:35,120 --> 03:24:40,479
go down to the next memory block you can
5139
03:24:37,040 --> 03:24:43,520
see that there is just one change f goes
5140
03:24:40,479 --> 03:24:47,120
down to e and then with variable c
5141
03:24:43,520 --> 03:24:49,439
this address goes down by one so f e d
5142
03:24:47,120 --> 03:24:51,359
these are contiguous memory blocks
5143
03:24:49,439 --> 03:24:53,279
they're all next to each other but now
5144
03:24:51,359 --> 03:24:55,520
if we switch to a different data type
5145
03:24:53,279 --> 03:24:57,120
like a short we haven't used these in a
5146
03:24:55,520 --> 03:25:00,160
long time
5147
03:24:57,120 --> 03:25:02,800
well shorts use two bytes of memory
5148
03:25:00,160 --> 03:25:07,279
as we can see here so these hexadecimal
5149
03:25:02,800 --> 03:25:08,399
addresses are now going down by two e d
5150
03:25:07,279 --> 03:25:09,120
c
5151
03:25:08,399 --> 03:25:12,000
b
5152
03:25:09,120 --> 03:25:15,040
a so each of these variables are using
5153
03:25:12,000 --> 03:25:17,920
two memory blocks we need an even larger
5154
03:25:15,040 --> 03:25:20,319
house even more memory blocks and they
5155
03:25:17,920 --> 03:25:22,800
use up two memory addresses
5156
03:25:20,319 --> 03:25:24,479
and then if i change these two integers
5157
03:25:22,800 --> 03:25:25,840
well they're going to use up even more
5158
03:25:24,479 --> 03:25:28,800
memory
5159
03:25:25,840 --> 03:25:31,200
so integers use four bytes memory
5160
03:25:28,800 --> 03:25:34,239
and these are the street addresses of
5161
03:25:31,200 --> 03:25:37,840
these three variables and doubles use
5162
03:25:34,239 --> 03:25:37,840
double the size of an integer
5163
03:25:38,399 --> 03:25:43,279
doubles use eight bytes of memory and
5164
03:25:40,960 --> 03:25:46,239
these street addresses are decrementing
5165
03:25:43,279 --> 03:25:48,880
by eight if this was a fictional street
5166
03:25:46,239 --> 03:25:51,200
with houses well doubles use up eight
5167
03:25:48,880 --> 03:25:53,439
bytes memory so think of these like
5168
03:25:51,200 --> 03:25:55,359
mansions they're using up a lot of space
5169
03:25:53,439 --> 03:25:56,560
now what happens with an array i'm going
5170
03:25:55,359 --> 03:25:59,120
to declare
5171
03:25:56,560 --> 03:26:01,040
char a
5172
03:25:59,120 --> 03:26:03,359
and declare an array this will be an
5173
03:26:01,040 --> 03:26:05,920
array of characters to begin with so
5174
03:26:03,359 --> 03:26:08,479
let's say character array b and i'll
5175
03:26:05,920 --> 03:26:10,640
give this a size of one
5176
03:26:08,479 --> 03:26:12,319
so i'm going to print the size of a and
5177
03:26:10,640 --> 03:26:14,640
the size of b
5178
03:26:12,319 --> 03:26:17,760
as well as their memory addresses
5179
03:26:14,640 --> 03:26:19,520
our single character a uses one byte and
5180
03:26:17,760 --> 03:26:22,080
this is the street address of this
5181
03:26:19,520 --> 03:26:24,160
variable and our array b
5182
03:26:22,080 --> 03:26:26,399
also uses up one byte
5183
03:26:24,160 --> 03:26:29,040
and this is the street address of where
5184
03:26:26,399 --> 03:26:32,080
this array begins if i set the size of
5185
03:26:29,040 --> 03:26:34,399
this array to 2 well then my array is
5186
03:26:32,080 --> 03:26:36,960
going to use 2 bytes
5187
03:26:34,399 --> 03:26:38,880
and now it has a different address to
5188
03:26:36,960 --> 03:26:41,920
accommodate the increased size of the
5189
03:26:38,880 --> 03:26:44,479
array and if i change this to 3 it now
5190
03:26:41,920 --> 03:26:46,880
uses 3 bytes of memory and it has a new
5191
03:26:44,479 --> 03:26:48,800
address and if i change the data type of
5192
03:26:46,880 --> 03:26:52,000
the array we'll have to take that into
5193
03:26:48,800 --> 03:26:54,640
consideration so a short uses two bytes
5194
03:26:52,000 --> 03:26:57,439
memory and we need three elements so
5195
03:26:54,640 --> 03:27:00,319
that's a total of six two times three is
5196
03:26:57,439 --> 03:27:03,680
six and if i change this to int and int
5197
03:27:00,319 --> 03:27:06,080
uses four bytes so four times three is
5198
03:27:03,680 --> 03:27:08,560
twelve and a double
5199
03:27:06,080 --> 03:27:10,800
is eight so eight times three is twenty
5200
03:27:08,560 --> 03:27:14,000
four so yeah that's kind of the basics
5201
03:27:10,800 --> 03:27:16,720
of memory so memory is an array of bites
5202
03:27:14,000 --> 03:27:19,520
within ram think of it like a street and
5203
03:27:16,720 --> 03:27:22,239
a street contains houses each house is a
5204
03:27:19,520 --> 03:27:24,720
memory block that can hold some value
5205
03:27:22,239 --> 03:27:26,720
think of each value as a person living
5206
03:27:24,720 --> 03:27:29,200
in that house and in order to find a
5207
03:27:26,720 --> 03:27:31,600
person we have to know their address
5208
03:27:29,200 --> 03:27:33,680
like a street address c refers to these
5209
03:27:31,600 --> 03:27:35,760
variables by their street address but
5210
03:27:33,680 --> 03:27:38,000
that's difficult for us to understand so
5211
03:27:35,760 --> 03:27:40,080
we just refer to this memory address as
5212
03:27:38,000 --> 03:27:42,160
a variable name because that's easier
5213
03:27:40,080 --> 03:27:44,720
for humans to work with so yeah that's
5214
03:27:42,160 --> 03:27:46,160
some basics of memory and c and in the
5215
03:27:44,720 --> 03:27:48,560
next video we're going to discuss
5216
03:27:46,160 --> 03:27:50,239
pointers so if you would like a copy of
5217
03:27:48,560 --> 03:27:52,720
my notes here i'll post them to the
5218
03:27:50,239 --> 03:27:56,319
comment section down below and well yeah
5219
03:27:52,720 --> 03:27:58,319
that's the basics of memory in c
5220
03:27:56,319 --> 03:28:00,080
hey yeah it's you bro hope you're doing
5221
03:27:58,319 --> 03:28:03,760
well and in this video i'm going to
5222
03:28:00,080 --> 03:28:08,000
explain the basics of pointers in c so
5223
03:28:03,760 --> 03:28:08,000
sit back relax and enjoy the show
5224
03:28:08,239 --> 03:28:13,600
if you wouldn't mind please like comment
5225
03:28:10,800 --> 03:28:15,840
and subscribe one like equals one prayer
5226
03:28:13,600 --> 03:28:18,239
for the youtube algorithm
5227
03:28:15,840 --> 03:28:20,880
oh yeah we finally made it to pointers
5228
03:28:18,239 --> 03:28:24,000
we're only about 40 topics in a pointer
5229
03:28:20,880 --> 03:28:27,200
is a variable like reference that holds
5230
03:28:24,000 --> 03:28:30,399
a memory address as a value to another
5231
03:28:27,200 --> 03:28:32,960
variable array etc some tasks are
5232
03:28:30,399 --> 03:28:34,720
performed more easily with pointers and
5233
03:28:32,960 --> 03:28:36,720
here's a comprehensive list of a few of
5234
03:28:34,720 --> 03:28:38,800
the benefits in this video we're going
5235
03:28:36,720 --> 03:28:40,640
to more or less focus on building a
5236
03:28:38,800 --> 03:28:42,479
solid foundation with pointers and
5237
03:28:40,640 --> 03:28:44,960
understanding how they work so let's say
5238
03:28:42,479 --> 03:28:47,840
we have a variable int edge and i'll
5239
03:28:44,960 --> 03:28:50,720
give this some value a variable has a
5240
03:28:47,840 --> 03:28:52,880
value and an address and let's display
5241
03:28:50,720 --> 03:28:55,920
the value and the address of this
5242
03:28:52,880 --> 03:28:58,000
variable using two print statements
5243
03:28:55,920 --> 03:29:00,399
so first i'm going to display the
5244
03:28:58,000 --> 03:29:04,000
address of this variable
5245
03:29:00,399 --> 03:29:06,479
address of age then i will use the p
5246
03:29:04,000 --> 03:29:09,200
format specifier to display an address
5247
03:29:06,479 --> 03:29:12,080
in hexadecimal and we are displaying the
5248
03:29:09,200 --> 03:29:15,680
address of age and ampersand is the
5249
03:29:12,080 --> 03:29:17,840
address of operator address of age and i
5250
03:29:15,680 --> 03:29:18,960
would also like to display the value of
5251
03:29:17,840 --> 03:29:21,279
age
5252
03:29:18,960 --> 03:29:23,120
value of age
5253
03:29:21,279 --> 03:29:26,560
and this is an integer
5254
03:29:23,120 --> 03:29:30,080
and we will display age
5255
03:29:26,560 --> 03:29:32,640
so variables have a value and an address
5256
03:29:30,080 --> 03:29:35,279
this is the address of this variable and
5257
03:29:32,640 --> 03:29:36,720
the value at this address
5258
03:29:35,279 --> 03:29:38,720
as you know there's tons of different
5259
03:29:36,720 --> 03:29:40,720
things we can do with a value of a
5260
03:29:38,720 --> 03:29:42,960
variable but there are things that we
5261
03:29:40,720 --> 03:29:44,960
can do with an address as well so we can
5262
03:29:42,960 --> 03:29:47,760
actually store this address within a
5263
03:29:44,960 --> 03:29:50,000
separate variable a variable like
5264
03:29:47,760 --> 03:29:52,000
reference and that is called a pointer
5265
03:29:50,000 --> 03:29:53,840
so to create a pointer we will make sure
5266
03:29:52,000 --> 03:29:56,399
these are of the same data type as the
5267
03:29:53,840 --> 03:29:58,560
variable we're pointing to age is an
5268
03:29:56,399 --> 03:30:00,560
integer so we will declare this pointer
5269
03:29:58,560 --> 03:30:02,399
of the integer data type and the next
5270
03:30:00,560 --> 03:30:04,720
step to declare a pointer is that we
5271
03:30:02,399 --> 03:30:07,200
will use an asterisk this is the in
5272
03:30:04,720 --> 03:30:09,840
direction operator and a common naming
5273
03:30:07,200 --> 03:30:12,160
convention for pointers is that you type
5274
03:30:09,840 --> 03:30:14,239
lowercase p the name of the variable
5275
03:30:12,160 --> 03:30:16,880
you're going to point to but make the
5276
03:30:14,239 --> 03:30:18,720
first letter uppercase and i'm going to
5277
03:30:16,880 --> 03:30:21,279
set this equal to
5278
03:30:18,720 --> 03:30:24,640
the address of age
5279
03:30:21,279 --> 03:30:27,359
so the address of age and the value at
5280
03:30:24,640 --> 03:30:28,960
this variable are the same and let's
5281
03:30:27,359 --> 03:30:31,600
test that theory
5282
03:30:28,960 --> 03:30:37,200
so this time i'm going to display
5283
03:30:31,600 --> 03:30:38,960
the address of age and the value of ph
5284
03:30:37,200 --> 03:30:42,640
and this will display an address so
5285
03:30:38,960 --> 03:30:44,640
change the format specifier from d to p
5286
03:30:42,640 --> 03:30:47,920
and i'm going to turn this line into a
5287
03:30:44,640 --> 03:30:50,479
comment for now okay so these addresses
5288
03:30:47,920 --> 03:30:53,520
should both be the same the address of
5289
03:30:50,479 --> 03:30:54,640
age as well as the value stored within
5290
03:30:53,520 --> 03:30:56,960
ph
5291
03:30:54,640 --> 03:30:59,840
so our pointer has its own address but
5292
03:30:56,960 --> 03:31:02,000
the value stored within it is an address
5293
03:30:59,840 --> 03:31:03,840
and we can access the value at this
5294
03:31:02,000 --> 03:31:06,160
address by using the indirection
5295
03:31:03,840 --> 03:31:08,160
operator so this time i'm going to print
5296
03:31:06,160 --> 03:31:09,680
the value of age
5297
03:31:08,160 --> 03:31:10,720
and the value
5298
03:31:09,680 --> 03:31:12,960
at
5299
03:31:10,720 --> 03:31:14,239
stored
5300
03:31:12,960 --> 03:31:16,720
address
5301
03:31:14,239 --> 03:31:20,479
and to dereference a pointer you will
5302
03:31:16,720 --> 03:31:22,560
type the pointer name ph appreciated
5303
03:31:20,479 --> 03:31:24,560
with the indirection operator
5304
03:31:22,560 --> 03:31:27,120
as my own personal nickname for the
5305
03:31:24,560 --> 03:31:29,520
indirection operator i call it the value
5306
03:31:27,120 --> 03:31:31,279
at address operator that's not any
5307
03:31:29,520 --> 03:31:34,479
official name that's just how i think
5308
03:31:31,279 --> 03:31:37,120
about it so we're extracting a value at
5309
03:31:34,479 --> 03:31:40,560
the given address within this pointer so
5310
03:31:37,120 --> 03:31:43,279
if i display the value of age and the
5311
03:31:40,560 --> 03:31:45,520
value at the stored address using the
5312
03:31:43,279 --> 03:31:47,600
indirection operator well both of these
5313
03:31:45,520 --> 03:31:50,560
are going to be the same
5314
03:31:47,600 --> 03:31:53,120
so we have 21 stored within our edge
5315
03:31:50,560 --> 03:31:55,760
and after dereferencing this pointer
5316
03:31:53,120 --> 03:31:56,880
we're extracting the value at this given
5317
03:31:55,760 --> 03:31:59,200
address
5318
03:31:56,880 --> 03:32:01,600
so you use the indirection operator when
5319
03:31:59,200 --> 03:32:04,319
you declare a pointer as well as when
5320
03:32:01,600 --> 03:32:05,600
you want to access a value at the stored
5321
03:32:04,319 --> 03:32:07,520
address
5322
03:32:05,600 --> 03:32:08,479
so with the data types of the pointer
5323
03:32:07,520 --> 03:32:10,880
you'll want to make sure they're
5324
03:32:08,479 --> 03:32:13,520
consistent c is a strongly typed
5325
03:32:10,880 --> 03:32:15,279
language so if i change the data type of
5326
03:32:13,520 --> 03:32:18,000
my pointer to char
5327
03:32:15,279 --> 03:32:21,279
using my compiler i'll receive a warning
5328
03:32:18,000 --> 03:32:24,080
initialization of char from incompatible
5329
03:32:21,279 --> 03:32:26,319
pointer type int now the actual data
5330
03:32:24,080 --> 03:32:28,800
type of a pointer is the same they use
5331
03:32:26,319 --> 03:32:31,520
eight bytes to store an address so i'm
5332
03:32:28,800 --> 03:32:33,920
going to print the size of our variable
5333
03:32:31,520 --> 03:32:35,680
as well as our pointer
5334
03:32:33,920 --> 03:32:38,000
so i'll change this data type back to
5335
03:32:35,680 --> 03:32:39,920
what it was originally and this time i'm
5336
03:32:38,000 --> 03:32:41,760
going to print the size of our age
5337
03:32:39,920 --> 03:32:42,960
variable as well as the size of our
5338
03:32:41,760 --> 03:32:45,920
pointer
5339
03:32:42,960 --> 03:32:47,680
size of age
5340
03:32:45,920 --> 03:32:48,840
and we're displaying an integer and this
5341
03:32:47,680 --> 03:32:54,000
will be in
5342
03:32:48,840 --> 03:32:54,000
bytes size of age
5343
03:32:54,160 --> 03:32:58,319
size of pointer age
5344
03:32:58,560 --> 03:33:03,200
and then i'm going to display a new line
5345
03:33:00,560 --> 03:33:05,760
okay so the size of our age variable is
5346
03:33:03,200 --> 03:33:07,920
4 bytes it's an integer integers use 4
5347
03:33:05,760 --> 03:33:09,840
bytes so even though we declared our
5348
03:33:07,920 --> 03:33:12,239
pointer as an integer
5349
03:33:09,840 --> 03:33:14,640
the actual size of our pointer is going
5350
03:33:12,239 --> 03:33:16,640
to be 8 bytes that's enough to store a
5351
03:33:14,640 --> 03:33:19,279
hexadecimal address
5352
03:33:16,640 --> 03:33:21,359
so just as good practice since c is a
5353
03:33:19,279 --> 03:33:22,880
strongly typed language you'll want to
5354
03:33:21,359 --> 03:33:24,960
be sure that the data type of your
5355
03:33:22,880 --> 03:33:26,399
pointer is consistent with the variable
5356
03:33:24,960 --> 03:33:28,160
that it's pointing to
5357
03:33:26,399 --> 03:33:31,040
now here's one thing that we can do with
5358
03:33:28,160 --> 03:33:32,640
pointers we can pass a pointer as an
5359
03:33:31,040 --> 03:33:34,399
argument to a function
5360
03:33:32,640 --> 03:33:36,319
so outside of my main function i'm going
5361
03:33:34,399 --> 03:33:37,279
to declare a function
5362
03:33:36,319 --> 03:33:38,880
void
5363
03:33:37,279 --> 03:33:39,840
let's say print
5364
03:33:38,880 --> 03:33:41,920
age
5365
03:33:39,840 --> 03:33:44,960
first we'll do this with passing and
5366
03:33:41,920 --> 03:33:48,160
integer so int age
5367
03:33:44,960 --> 03:33:49,120
and i will display the value of age
5368
03:33:48,160 --> 03:33:51,200
you
5369
03:33:49,120 --> 03:33:53,200
are
5370
03:33:51,200 --> 03:33:54,720
age years
5371
03:33:53,200 --> 03:33:57,520
old
5372
03:33:54,720 --> 03:33:59,439
and i will display my age variable
5373
03:33:57,520 --> 03:34:01,840
then at the end we will pass in our
5374
03:33:59,439 --> 03:34:01,840
variable
5375
03:34:02,399 --> 03:34:06,640
print edge and i will pass in age
5376
03:34:05,279 --> 03:34:09,040
for now i'm just going to turn all of
5377
03:34:06,640 --> 03:34:11,600
these into comments
5378
03:34:09,040 --> 03:34:14,800
okay so you know this works
5379
03:34:11,600 --> 03:34:16,319
you are 21 years old you could also pass
5380
03:34:14,800 --> 03:34:18,880
in a pointer too
5381
03:34:16,319 --> 03:34:20,800
so i'm going to this time pass in
5382
03:34:18,880 --> 03:34:23,439
pointer age
5383
03:34:20,800 --> 03:34:26,080
and we need to change the parameter from
5384
03:34:23,439 --> 03:34:28,720
an integer to a pointer
5385
03:34:26,080 --> 03:34:30,399
so precede the parameter name with the
5386
03:34:28,720 --> 03:34:33,920
indirection operator
5387
03:34:30,399 --> 03:34:35,840
and i'll rename this parameter as ph
5388
03:34:33,920 --> 03:34:38,560
in order to access the value of the
5389
03:34:35,840 --> 03:34:41,279
address stored within my pointer i need
5390
03:34:38,560 --> 03:34:43,760
to de-reference
5391
03:34:41,279 --> 03:34:47,120
so i will use the indirection operator
5392
03:34:43,760 --> 03:34:48,399
then type my pointer name ph
5393
03:34:47,120 --> 03:34:49,840
and this will do the same thing as
5394
03:34:48,399 --> 03:34:52,399
before
5395
03:34:49,840 --> 03:34:54,800
so yeah those are pointers oh and before
5396
03:34:52,399 --> 03:34:56,800
i forget you can declare and initialize
5397
03:34:54,800 --> 03:34:58,479
a pointer in two steps
5398
03:34:56,800 --> 03:35:01,279
but it would be good practice if you're
5399
03:34:58,479 --> 03:35:02,239
declaring a pointer to assign a value of
5400
03:35:01,279 --> 03:35:03,840
null
5401
03:35:02,239 --> 03:35:05,680
since we already declared this pointer
5402
03:35:03,840 --> 03:35:08,080
we do not need to use this indirection
5403
03:35:05,680 --> 03:35:08,720
operator again when assigning a value so
5404
03:35:08,080 --> 03:35:12,880
p
5405
03:35:08,720 --> 03:35:15,120
h equals the address of age
5406
03:35:12,880 --> 03:35:17,520
so it's considered good practice to
5407
03:35:15,120 --> 03:35:20,720
assign null if you're declaring a
5408
03:35:17,520 --> 03:35:23,359
pointer and not yet assigning a value so
5409
03:35:20,720 --> 03:35:26,000
yeah that's basically a pointer it's a
5410
03:35:23,359 --> 03:35:29,040
variable like reference that has a
5411
03:35:26,000 --> 03:35:32,479
memory address as value to another
5412
03:35:29,040 --> 03:35:35,439
variable array etc some tasks are
5413
03:35:32,479 --> 03:35:37,520
performed more easily with pointers and
5414
03:35:35,439 --> 03:35:39,760
to declare a pointer you use the
5415
03:35:37,520 --> 03:35:42,560
indirection operator and if you need to
5416
03:35:39,760 --> 03:35:45,279
access a value stored within a pointer
5417
03:35:42,560 --> 03:35:48,080
you type the pointer name preceded with
5418
03:35:45,279 --> 03:35:50,560
the indirection operator so yeah that is
5419
03:35:48,080 --> 03:35:52,080
a quick intro to pointers if you found
5420
03:35:50,560 --> 03:35:54,000
this video helpful please give this
5421
03:35:52,080 --> 03:35:55,840
video a thumbs up leave a random comment
5422
03:35:54,000 --> 03:35:58,960
down below and subscribe if you'd like
5423
03:35:55,840 --> 03:35:58,960
to become a fellow bro
5424
03:36:01,520 --> 03:36:06,720
all right let's do this here's how we
5425
03:36:03,600 --> 03:36:09,760
can write to a file in c let's create a
5426
03:36:06,720 --> 03:36:13,040
file pointer the data type is file and
5427
03:36:09,760 --> 03:36:15,840
i'll name this pf it's a pointer to a
5428
03:36:13,040 --> 03:36:19,120
file and i will set this equal to then
5429
03:36:15,840 --> 03:36:21,600
invoke the f open function we will pass
5430
03:36:19,120 --> 03:36:24,479
in a name we would like to give our file
5431
03:36:21,600 --> 03:36:26,239
let's name this test.txt but if you
5432
03:36:24,479 --> 03:36:28,640
would like this to be a different file
5433
03:36:26,239 --> 03:36:30,560
extension such as html well you can
5434
03:36:28,640 --> 03:36:32,720
change it to that then uh we'll keep
5435
03:36:30,560 --> 03:36:36,000
this as a plain text file just to keep
5436
03:36:32,720 --> 03:36:38,640
it simple and to write to a file there
5437
03:36:36,000 --> 03:36:42,160
is a second argument a mode so this
5438
03:36:38,640 --> 03:36:45,600
could be w for right a for append r for
5439
03:36:42,160 --> 03:36:47,200
read but we are looking for w for now so
5440
03:36:45,600 --> 03:36:50,000
it is good practice at the end of your
5441
03:36:47,200 --> 03:36:53,760
program to close any files that are open
5442
03:36:50,000 --> 03:36:57,279
so we will use the f close function and
5443
03:36:53,760 --> 03:36:59,359
pass in our pointer p f make sure to not
5444
03:36:57,279 --> 03:37:04,080
dereference it now to write a line of
5445
03:36:59,359 --> 03:37:06,720
text to our file we can use f print f
5446
03:37:04,080 --> 03:37:08,720
pass in our pointer p f do not
5447
03:37:06,720 --> 03:37:13,200
dereference it and then some string of
5448
03:37:08,720 --> 03:37:15,760
text let's say spongebob squarepants
5449
03:37:13,200 --> 03:37:18,800
so this file will be created in the same
5450
03:37:15,760 --> 03:37:20,000
folder as my c program so after running
5451
03:37:18,800 --> 03:37:22,160
this
5452
03:37:20,000 --> 03:37:25,520
we have a new text file within the same
5453
03:37:22,160 --> 03:37:27,279
folder and it says spongebob squarepants
5454
03:37:25,520 --> 03:37:31,439
what if i run this again and write
5455
03:37:27,279 --> 03:37:33,920
something different like patrick starr
5456
03:37:31,439 --> 03:37:35,920
so if the mode is w
5457
03:37:33,920 --> 03:37:38,560
then we will actually overwrite any
5458
03:37:35,920 --> 03:37:41,359
existing data already within this file
5459
03:37:38,560 --> 03:37:44,399
if we need to append a file we can use
5460
03:37:41,359 --> 03:37:46,080
the a mode for append so this time let's
5461
03:37:44,399 --> 03:37:48,640
append some text to the end of our
5462
03:37:46,080 --> 03:37:48,640
document
5463
03:37:48,880 --> 03:37:53,520
spongebob squarepants
5464
03:37:51,920 --> 03:37:56,160
and you have to keep track of any new
5465
03:37:53,520 --> 03:37:58,319
lines that you add too so if i run this
5466
03:37:56,160 --> 03:38:00,640
again and this time i am appending some
5467
03:37:58,319 --> 03:38:02,880
text
5468
03:38:00,640 --> 03:38:05,279
well patrick starr is still there and we
5469
03:38:02,880 --> 03:38:06,800
appended spongebob squarepants to the
5470
03:38:05,279 --> 03:38:08,800
end of our file
5471
03:38:06,800 --> 03:38:11,120
now if you would like to delete a file
5472
03:38:08,800 --> 03:38:14,080
here's how i'm going to turn all of this
5473
03:38:11,120 --> 03:38:16,239
into one giant comment
5474
03:38:14,080 --> 03:38:18,239
let's use a little bit of file detection
5475
03:38:16,239 --> 03:38:19,520
let's check to see if our file does
5476
03:38:18,239 --> 03:38:21,840
exist
5477
03:38:19,520 --> 03:38:24,640
within our if statement let's invoke the
5478
03:38:21,840 --> 03:38:27,399
remove method and we will pass in the
5479
03:38:24,640 --> 03:38:30,319
name or location to our file
5480
03:38:27,399 --> 03:38:33,600
test.txt if we remove this file
5481
03:38:30,319 --> 03:38:36,399
successfully it will return zero so if
5482
03:38:33,600 --> 03:38:39,120
after invoking the remove function if
5483
03:38:36,399 --> 03:38:41,040
this returns zero that's its exit code
5484
03:38:39,120 --> 03:38:43,199
that means that file was deleted
5485
03:38:41,040 --> 03:38:45,040
successfully let's print a message to
5486
03:38:43,199 --> 03:38:45,920
let the user know
5487
03:38:45,040 --> 03:38:47,199
that
5488
03:38:45,920 --> 03:38:49,439
file
5489
03:38:47,199 --> 03:38:51,760
was deleted
5490
03:38:49,439 --> 03:38:54,319
successfully
5491
03:38:51,760 --> 03:38:56,479
else we will print a different message
5492
03:38:54,319 --> 03:38:59,040
printf
5493
03:38:56,479 --> 03:39:00,399
that file was not
5494
03:38:59,040 --> 03:39:02,640
deleted
5495
03:39:00,399 --> 03:39:06,080
okay let's try this so here's my file
5496
03:39:02,640 --> 03:39:08,640
within the same folder if i run this
5497
03:39:06,080 --> 03:39:10,479
that file was deleted successfully
5498
03:39:08,640 --> 03:39:12,160
and that file is no longer within the
5499
03:39:10,479 --> 03:39:13,439
same folder you can see here that this
5500
03:39:12,160 --> 03:39:15,279
says deleted
5501
03:39:13,439 --> 03:39:17,199
and if i run this again we will not be
5502
03:39:15,279 --> 03:39:18,239
able to locate that file because it's
5503
03:39:17,199 --> 03:39:20,319
not there
5504
03:39:18,239 --> 03:39:22,479
now you can write a file to a different
5505
03:39:20,319 --> 03:39:25,199
location let's say i would like to write
5506
03:39:22,479 --> 03:39:28,000
a file to my desktop i can set a
5507
03:39:25,199 --> 03:39:30,960
relative file path or an absolute file
5508
03:39:28,000 --> 03:39:32,960
path i'll use an absolute file path
5509
03:39:30,960 --> 03:39:35,120
i'm going to get the file location of my
5510
03:39:32,960 --> 03:39:36,640
desktop so i'm just going to click on
5511
03:39:35,120 --> 03:39:38,960
one of these folders
5512
03:39:36,640 --> 03:39:41,840
go to properties
5513
03:39:38,960 --> 03:39:44,160
and copy this location
5514
03:39:41,840 --> 03:39:46,880
so with my file name i'm going to
5515
03:39:44,160 --> 03:39:49,359
precede this with an absolute file path
5516
03:39:46,880 --> 03:39:51,040
then any backslashes might need to be
5517
03:39:49,359 --> 03:39:53,840
double backslashes
5518
03:39:51,040 --> 03:39:56,000
so this time if i write a file
5519
03:39:53,840 --> 03:39:58,160
then this file will appear at this
5520
03:39:56,000 --> 03:40:01,359
location on my desktop
5521
03:39:58,160 --> 03:40:04,720
and here's that file test.txt and it
5522
03:40:01,359 --> 03:40:07,120
says spongebob squarepants well alright
5523
03:40:04,720 --> 03:40:09,920
everybody that is how to write append
5524
03:40:07,120 --> 03:40:12,160
and delete files in c if you found this
5525
03:40:09,920 --> 03:40:14,160
video helpful be sure to help me out by
5526
03:40:12,160 --> 03:40:16,080
smashing that like button leave a random
5527
03:40:14,160 --> 03:40:19,840
comment down below and subscribe if
5528
03:40:16,080 --> 03:40:19,840
you'd like to become a fellow bro
5529
03:40:20,800 --> 03:40:24,239
alright people so in this video i'm
5530
03:40:22,720 --> 03:40:26,319
going to show you how we can read the
5531
03:40:24,239 --> 03:40:28,160
contents of a file someplace on your
5532
03:40:26,319 --> 03:40:30,479
computer you can either place this
5533
03:40:28,160 --> 03:40:32,319
within the same folder as your c program
5534
03:40:30,479 --> 03:40:34,880
or someplace else i think it would be
5535
03:40:32,319 --> 03:40:36,720
cool if we read a file from our desktops
5536
03:40:34,880 --> 03:40:39,199
so i'm going to create a new file and
5537
03:40:36,720 --> 03:40:40,960
this is going to be a plain text file
5538
03:40:39,199 --> 03:40:42,720
and i'll write a poem i'll name this
5539
03:40:40,960 --> 03:40:45,760
poem.txt
5540
03:40:42,720 --> 03:40:47,760
here's my beautiful poem
5541
03:40:45,760 --> 03:40:51,120
are red
5542
03:40:47,760 --> 03:40:55,600
violets are blue
5543
03:40:51,120 --> 03:40:56,720
booty booty booty booty
5544
03:40:55,600 --> 03:40:58,800
rockin
5545
03:40:56,720 --> 03:41:00,640
everywhere
5546
03:40:58,800 --> 03:41:02,640
okay i'm going to save
5547
03:41:00,640 --> 03:41:04,560
and then i'll need the file location of
5548
03:41:02,640 --> 03:41:07,199
where this is located i'm going to right
5549
03:41:04,560 --> 03:41:10,319
click go to properties and copy this
5550
03:41:07,199 --> 03:41:12,160
file path and save it for later now to
5551
03:41:10,319 --> 03:41:14,560
read a file on your computer we'll need
5552
03:41:12,160 --> 03:41:15,600
to create a pointer the data type is
5553
03:41:14,560 --> 03:41:17,040
file
5554
03:41:15,600 --> 03:41:20,319
and i'll name this
5555
03:41:17,040 --> 03:41:23,120
p f and i will set the sql to then
5556
03:41:20,319 --> 03:41:27,279
invoke the f open function
5557
03:41:23,120 --> 03:41:30,000
pass in your file mine is poem.txt
5558
03:41:27,279 --> 03:41:32,160
so if this file is not within the same
5559
03:41:30,000 --> 03:41:35,040
folder as your c program you'll need a
5560
03:41:32,160 --> 03:41:37,600
relative or absolute file path since
5561
03:41:35,040 --> 03:41:39,359
this file is on my desktop i could use
5562
03:41:37,600 --> 03:41:42,319
an absolute file path
5563
03:41:39,359 --> 03:41:44,960
so preceding my file name i will paste
5564
03:41:42,319 --> 03:41:46,800
that location and then each backslash
5565
03:41:44,960 --> 03:41:49,120
may need to be a double backslash
5566
03:41:46,800 --> 03:41:51,520
because that is an escape sequence for a
5567
03:41:49,120 --> 03:41:53,680
backslash character okay the second
5568
03:41:51,520 --> 03:41:57,199
argument is the mode this time we are
5569
03:41:53,680 --> 03:41:59,279
going to read a file so pass in r for
5570
03:41:57,199 --> 03:42:01,199
the second argument then it's good
5571
03:41:59,279 --> 03:42:03,680
practice at the end of your program to
5572
03:42:01,199 --> 03:42:06,479
close any open files
5573
03:42:03,680 --> 03:42:09,279
we will invoke the f close function pass
5574
03:42:06,479 --> 03:42:12,239
in our pointer do not dereference it and
5575
03:42:09,279 --> 03:42:14,960
we will need one more thing a buffer a
5576
03:42:12,239 --> 03:42:17,359
buffer is going to act as a container an
5577
03:42:14,960 --> 03:42:20,160
array of characters to hold one line of
5578
03:42:17,359 --> 03:42:22,160
our text document one line at a time so
5579
03:42:20,160 --> 03:42:25,600
let's declare an array of characters
5580
03:42:22,160 --> 03:42:28,479
named buffer and i'll set the size to
5581
03:42:25,600 --> 03:42:31,439
255. our buffer is going to hold one
5582
03:42:28,479 --> 03:42:34,160
line of our file one line at a time and
5583
03:42:31,439 --> 03:42:37,279
to read a single line from our file we
5584
03:42:34,160 --> 03:42:40,000
will use the f gets function and there
5585
03:42:37,279 --> 03:42:42,640
are three arguments our buffer this is
5586
03:42:40,000 --> 03:42:45,359
what we're inserting a line of text into
5587
03:42:42,640 --> 03:42:48,399
a maximum input size i'll set this to be
5588
03:42:45,359 --> 03:42:50,800
255 to match our buffer and then a
5589
03:42:48,399 --> 03:42:52,640
pointer to the file that we're opening
5590
03:42:50,800 --> 03:42:55,120
and again make sure to not dereference
5591
03:42:52,640 --> 03:42:57,920
it so this will read a single line of
5592
03:42:55,120 --> 03:43:00,399
text and then let's print our buffer so
5593
03:42:57,920 --> 03:43:02,160
we're printing a string and we are
5594
03:43:00,399 --> 03:43:03,359
displaying whatever is currently within
5595
03:43:02,160 --> 03:43:05,520
our buffer
5596
03:43:03,359 --> 03:43:07,520
so after running this this will read a
5597
03:43:05,520 --> 03:43:08,479
single line of text whatever's at the
5598
03:43:07,520 --> 03:43:10,239
top
5599
03:43:08,479 --> 03:43:11,840
roses are red
5600
03:43:10,239 --> 03:43:14,560
now if i need to read all of the
5601
03:43:11,840 --> 03:43:19,160
contents of my file i can place this
5602
03:43:14,560 --> 03:43:19,160
section of code within a while loop
5603
03:43:20,399 --> 03:43:25,600
so with our condition i'm going to move
5604
03:43:22,560 --> 03:43:27,840
the f gets function within the condition
5605
03:43:25,600 --> 03:43:30,479
if we reach the end of a file f gets
5606
03:43:27,840 --> 03:43:34,160
will return null so let's continue this
5607
03:43:30,479 --> 03:43:36,960
while loop while f gets does not return
5608
03:43:34,160 --> 03:43:39,359
no if we do not return null then we will
5609
03:43:36,960 --> 03:43:41,680
print whatever's within our buffer now
5610
03:43:39,359 --> 03:43:44,640
if i run this this will display all of
5611
03:43:41,680 --> 03:43:47,040
the lines from my file roses are red
5612
03:43:44,640 --> 03:43:49,199
violets are blue booty booty booty booty
5613
03:43:47,040 --> 03:43:50,399
rocking everywhere and if you would like
5614
03:43:49,199 --> 03:43:52,800
you can add a little bit of file
5615
03:43:50,399 --> 03:43:54,880
detection so before we reach our while
5616
03:43:52,800 --> 03:43:57,279
loop let's check to see if that file
5617
03:43:54,880 --> 03:43:59,600
even exists before we attempt to open
5618
03:43:57,279 --> 03:44:02,640
and read it so let's check to see if our
5619
03:43:59,600 --> 03:44:05,199
pointer equals no if it is null that
5620
03:44:02,640 --> 03:44:07,760
means we could not locate this file so
5621
03:44:05,199 --> 03:44:10,319
i'm going to print
5622
03:44:07,760 --> 03:44:11,680
unable to open
5623
03:44:10,319 --> 03:44:14,479
file
5624
03:44:11,680 --> 03:44:17,359
and we'll create an else statement
5625
03:44:14,479 --> 03:44:20,160
else let's read and print the contents
5626
03:44:17,359 --> 03:44:20,160
of this file
5627
03:44:20,560 --> 03:44:24,160
okay so let's say i get the file
5628
03:44:22,399 --> 03:44:27,279
extension wrong let's say i'm looking
5629
03:44:24,160 --> 03:44:28,560
for an html file named poem and i try
5630
03:44:27,279 --> 03:44:30,399
and read this
5631
03:44:28,560 --> 03:44:33,040
unable to open file
5632
03:44:30,399 --> 03:44:35,520
but if we are able to locate this file
5633
03:44:33,040 --> 03:44:37,600
we will open it and read the contents
5634
03:44:35,520 --> 03:44:39,680
and then close this file so yeah
5635
03:44:37,600 --> 03:44:42,399
everybody that is how to read the
5636
03:44:39,680 --> 03:44:44,080
contents of a file line by line if you
5637
03:44:42,399 --> 03:44:45,840
found this video helpful please be sure
5638
03:44:44,080 --> 03:44:47,680
to help me out by smashing that like
5639
03:44:45,840 --> 03:44:49,359
button leave a random comment down below
5640
03:44:47,680 --> 03:44:51,520
and subscribe if you'd like to become a
5641
03:44:49,359 --> 03:44:53,680
fellow bro
5642
03:44:51,520 --> 03:44:55,279
oh yeah what's going on everybody it's
5643
03:44:53,680 --> 03:44:56,880
you bro hope you're doing well and in
5644
03:44:55,279 --> 03:45:00,000
this video we're going to create a game
5645
03:44:56,880 --> 03:45:02,800
of tic-tac-toe so sit back relax and
5646
03:45:00,000 --> 03:45:02,800
enjoy the show
5647
03:45:03,199 --> 03:45:07,840
if you find this video helpful please
5648
03:45:04,880 --> 03:45:09,760
remember to like comment and subscribe
5649
03:45:07,840 --> 03:45:12,319
your support will help keep this channel
5650
03:45:09,760 --> 03:45:13,760
running all right let's begin so the
5651
03:45:12,319 --> 03:45:15,920
first thing that you're going to need is
5652
03:45:13,760 --> 03:45:18,560
to include these files at the top of
5653
03:45:15,920 --> 03:45:20,800
your c program and we will create seven
5654
03:45:18,560 --> 03:45:22,960
different function prototypes so the
5655
03:45:20,800 --> 03:45:25,439
return type of this first function is
5656
03:45:22,960 --> 03:45:28,399
void and the name of this function will
5657
03:45:25,439 --> 03:45:30,880
be reset board our board is going to be
5658
03:45:28,399 --> 03:45:33,760
a 2d character array and our second
5659
03:45:30,880 --> 03:45:36,479
function is void print board
5660
03:45:33,760 --> 03:45:38,640
which will print our 2d character array
5661
03:45:36,479 --> 03:45:41,920
then with this next function this has a
5662
03:45:38,640 --> 03:45:44,720
return type event and this will check
5663
03:45:41,920 --> 03:45:47,120
free spaces
5664
03:45:44,720 --> 03:45:49,439
if after invoking this function this
5665
03:45:47,120 --> 03:45:51,359
function returns zero that means the
5666
03:45:49,439 --> 03:45:54,640
game is over there's no more places to
5667
03:45:51,359 --> 03:45:57,680
move and we will need void player move
5668
03:45:54,640 --> 03:46:00,160
when it's the player's turn to move
5669
03:45:57,680 --> 03:46:02,160
void computer move when it's the
5670
03:46:00,160 --> 03:46:03,199
computer's turn to move
5671
03:46:02,160 --> 03:46:04,960
char
5672
03:46:03,199 --> 03:46:06,479
check winner
5673
03:46:04,960 --> 03:46:09,840
so we have a few different win
5674
03:46:06,479 --> 03:46:11,120
conditions to check and void print
5675
03:46:09,840 --> 03:46:13,760
winner
5676
03:46:11,120 --> 03:46:16,160
and there is one parameter a single
5677
03:46:13,760 --> 03:46:18,319
character soap char so these are
5678
03:46:16,160 --> 03:46:20,080
function prototypes we tend to declare
5679
03:46:18,319 --> 03:46:22,080
these before the main function because
5680
03:46:20,080 --> 03:46:23,840
they help with readability after the
5681
03:46:22,080 --> 03:46:25,760
main function we will give each of these
5682
03:46:23,840 --> 03:46:28,479
a body
5683
03:46:25,760 --> 03:46:31,920
so copy and paste those functions and
5684
03:46:28,479 --> 03:46:31,920
then give each a body
5685
03:46:35,199 --> 03:46:39,840
and with the parameter for print winner
5686
03:46:37,520 --> 03:46:42,000
the parameter is char
5687
03:46:39,840 --> 03:46:44,080
winner now let's head back to the top of
5688
03:46:42,000 --> 03:46:46,080
our program and there's a few global
5689
03:46:44,080 --> 03:46:47,920
variables that we're going to declare we
5690
03:46:46,080 --> 03:46:49,520
could declare these as local variables
5691
03:46:47,920 --> 03:46:50,960
within the main function but this
5692
03:46:49,520 --> 03:46:52,479
program is going to get really
5693
03:46:50,960 --> 03:46:54,560
complicated then if we have to pass
5694
03:46:52,479 --> 03:46:56,720
around pointers to a two-dimensional
5695
03:46:54,560 --> 03:46:58,880
array so just to keep this simple i'm
5696
03:46:56,720 --> 03:47:00,479
going to create some global variables
5697
03:46:58,880 --> 03:47:02,080
there are some downsides to global
5698
03:47:00,479 --> 03:47:04,720
variables so i tend to not to like to
5699
03:47:02,080 --> 03:47:06,880
use these so we have a 2d array of
5700
03:47:04,720 --> 03:47:09,520
characters named board it's our game
5701
03:47:06,880 --> 03:47:12,720
board and let's create two constants
5702
03:47:09,520 --> 03:47:14,640
constant char player a naming convention
5703
03:47:12,720 --> 03:47:16,239
for constants is to make all of the
5704
03:47:14,640 --> 03:47:18,319
letters uppercase
5705
03:47:16,239 --> 03:47:21,279
and i will set this to a character of my
5706
03:47:18,319 --> 03:47:23,040
choice i could be oh or i could be x or
5707
03:47:21,279 --> 03:47:25,439
something else maybe i could be a dollar
5708
03:47:23,040 --> 03:47:28,160
sign whatever uh i'll set myself to be
5709
03:47:25,439 --> 03:47:31,120
an x and then let's create a constant
5710
03:47:28,160 --> 03:47:34,479
for the computer constant char
5711
03:47:31,120 --> 03:47:37,040
computer and computers will be o so
5712
03:47:34,479 --> 03:47:39,680
within the main function we will declare
5713
03:47:37,040 --> 03:47:40,640
a local variable char
5714
03:47:39,680 --> 03:47:42,800
winner
5715
03:47:40,640 --> 03:47:45,520
and i'll go ahead and set this to an
5716
03:47:42,800 --> 03:47:47,680
empty space so if our winner is in empty
5717
03:47:45,520 --> 03:47:50,399
space that means there currently is no
5718
03:47:47,680 --> 03:47:53,920
winner if player wins then that would be
5719
03:47:50,399 --> 03:47:55,840
an x if computer wins that's a no within
5720
03:47:53,920 --> 03:47:58,000
our main function this is acting as a
5721
03:47:55,840 --> 03:48:01,680
driver for our code so the first thing
5722
03:47:58,000 --> 03:48:03,760
we'll do is reset our board
5723
03:48:01,680 --> 03:48:05,359
so we are going to initialize all of the
5724
03:48:03,760 --> 03:48:07,439
different characters within our
5725
03:48:05,359 --> 03:48:09,439
two-dimensional board so let's head to
5726
03:48:07,439 --> 03:48:12,239
this function
5727
03:48:09,439 --> 03:48:15,279
so with our reset board function we need
5728
03:48:12,239 --> 03:48:17,760
nested loops
5729
03:48:15,279 --> 03:48:20,080
the outer for loop is for the rows the
5730
03:48:17,760 --> 03:48:22,720
inner for loop will be for the columns
5731
03:48:20,080 --> 03:48:25,199
int i set this equal to zero and i need
5732
03:48:22,720 --> 03:48:27,279
to iterate this for loop three times one
5733
03:48:25,199 --> 03:48:31,279
for each row that we have
5734
03:48:27,279 --> 03:48:35,120
and then we need an inner for loop
5735
03:48:31,279 --> 03:48:38,319
so change the index to j
5736
03:48:35,120 --> 03:48:40,080
and for each index within our 2d array
5737
03:48:38,319 --> 03:48:42,880
at index of i
5738
03:48:40,080 --> 03:48:44,080
and j i will set the sequel to an empty
5739
03:48:42,880 --> 03:48:46,640
space
5740
03:48:44,080 --> 03:48:49,199
so when we call the reset board function
5741
03:48:46,640 --> 03:48:51,600
each element within this 2d array of
5742
03:48:49,199 --> 03:48:53,439
characters will be an empty space
5743
03:48:51,600 --> 03:48:55,600
effectively clearing it
5744
03:48:53,439 --> 03:48:57,600
so back within the main function after
5745
03:48:55,600 --> 03:48:59,920
resetting our board let's print our
5746
03:48:57,600 --> 03:48:59,920
board
5747
03:49:00,000 --> 03:49:05,199
and fill in this function
5748
03:49:03,120 --> 03:49:08,000
so you can get creative with this
5749
03:49:05,199 --> 03:49:11,520
here's one thing that i'll use
5750
03:49:08,000 --> 03:49:13,840
so i'm going to print a space
5751
03:49:11,520 --> 03:49:15,279
a character a space
5752
03:49:13,840 --> 03:49:16,560
a vertical bar
5753
03:49:15,279 --> 03:49:19,439
space
5754
03:49:16,560 --> 03:49:20,479
character space vertical bar
5755
03:49:19,439 --> 03:49:22,239
space
5756
03:49:20,479 --> 03:49:24,239
character
5757
03:49:22,239 --> 03:49:26,239
these three format specifiers are
5758
03:49:24,239 --> 03:49:29,439
placeholders the first character i'm
5759
03:49:26,239 --> 03:49:33,279
going to display is our board at index
5760
03:49:29,439 --> 03:49:38,319
of zero zero that is row zero column
5761
03:49:33,279 --> 03:49:41,600
zero followed by board index zero one
5762
03:49:38,319 --> 03:49:43,920
then board zero two
5763
03:49:41,600 --> 03:49:45,920
and with the next printf statement let's
5764
03:49:43,920 --> 03:49:47,199
display something like this
5765
03:49:45,920 --> 03:49:48,640
a new line
5766
03:49:47,199 --> 03:49:50,239
three dashes
5767
03:49:48,640 --> 03:49:53,040
a vertical bar
5768
03:49:50,239 --> 03:49:55,920
three dashes another vertical bar
5769
03:49:53,040 --> 03:49:58,080
three dashes then a new line
5770
03:49:55,920 --> 03:50:00,000
okay so let's copy these
5771
03:49:58,080 --> 03:50:02,640
paste it once
5772
03:50:00,000 --> 03:50:05,199
and we need to change these indices
5773
03:50:02,640 --> 03:50:09,120
so with this third printf statement
5774
03:50:05,199 --> 03:50:10,880
these indices will be one zero one one
5775
03:50:09,120 --> 03:50:15,120
one two
5776
03:50:10,880 --> 03:50:16,479
and then do this again so copy paste
5777
03:50:15,120 --> 03:50:17,920
two zero
5778
03:50:16,479 --> 03:50:19,600
two one
5779
03:50:17,920 --> 03:50:21,520
two two
5780
03:50:19,600 --> 03:50:23,920
then at the end i'll add a new line
5781
03:50:21,520 --> 03:50:25,600
printf
5782
03:50:23,920 --> 03:50:28,960
new line
5783
03:50:25,600 --> 03:50:28,960
oh let me fix one thing
5784
03:50:31,040 --> 03:50:34,800
okay so this is what it should be
5785
03:50:33,279 --> 03:50:36,239
all right now back within our main
5786
03:50:34,800 --> 03:50:38,319
function we're going to create a while
5787
03:50:36,239 --> 03:50:41,760
loop and we'll surround our print board
5788
03:50:38,319 --> 03:50:41,760
function within this while loop
5789
03:50:44,399 --> 03:50:50,640
so our condition is if winner is equal
5790
03:50:48,560 --> 03:50:53,680
to an empty space
5791
03:50:50,640 --> 03:50:57,439
that means there currently is no winner
5792
03:50:53,680 --> 03:50:58,640
and after invoking the check free spaces
5793
03:50:57,439 --> 03:51:02,560
function
5794
03:50:58,640 --> 03:51:04,960
the value returned does not equal zero
5795
03:51:02,560 --> 03:51:06,160
so let's fill in this check free spaces
5796
03:51:04,960 --> 03:51:08,399
function
5797
03:51:06,160 --> 03:51:11,439
within the check free spaces function
5798
03:51:08,399 --> 03:51:13,840
let's declare a local variable int free
5799
03:51:11,439 --> 03:51:18,560
spaces and i'll initially set this to
5800
03:51:13,840 --> 03:51:18,560
nine then we need a nested for loops
5801
03:51:20,239 --> 03:51:25,520
and i equals 0 we will continue this as
5802
03:51:23,040 --> 03:51:27,439
long as i is less than 3
5803
03:51:25,520 --> 03:51:30,399
i plus plus
5804
03:51:27,439 --> 03:51:34,160
then create an inner for loop
5805
03:51:30,399 --> 03:51:36,080
change the index from i to j
5806
03:51:34,160 --> 03:51:38,239
and within the inner forward loop we're
5807
03:51:36,080 --> 03:51:40,560
going to write an if statement
5808
03:51:38,239 --> 03:51:42,960
and we are checking to see if our 2d
5809
03:51:40,560 --> 03:51:46,880
array of characters bored
5810
03:51:42,960 --> 03:51:48,080
at index of i and j does not equal an
5811
03:51:46,880 --> 03:51:50,319
empty space
5812
03:51:48,080 --> 03:51:53,520
if whatever spot we're on is currently
5813
03:51:50,319 --> 03:51:56,960
occupied we will take our free spaces
5814
03:51:53,520 --> 03:51:59,359
local variable and decrement it by one
5815
03:51:56,960 --> 03:52:00,560
then outside of our for loops we will
5816
03:51:59,359 --> 03:52:03,359
return
5817
03:52:00,560 --> 03:52:06,000
free spaces
5818
03:52:03,359 --> 03:52:08,319
if we return zero that means the game is
5819
03:52:06,000 --> 03:52:09,279
over there's no more places a player can
5820
03:52:08,319 --> 03:52:11,520
move
5821
03:52:09,279 --> 03:52:13,520
so heading back to our main function
5822
03:52:11,520 --> 03:52:14,960
it will be let's say the player's turn
5823
03:52:13,520 --> 03:52:18,640
to move first
5824
03:52:14,960 --> 03:52:20,479
so invoke the player move function and
5825
03:52:18,640 --> 03:52:22,319
we'll need to fill this in
5826
03:52:20,479 --> 03:52:26,560
with the player move function we will
5827
03:52:22,319 --> 03:52:29,199
declare two local variables intex into y
5828
03:52:26,560 --> 03:52:31,199
and we will ask a user to enter in a row
5829
03:52:29,199 --> 03:52:34,000
number and a column number of where they
5830
03:52:31,199 --> 03:52:35,199
would like to move to so using a printf
5831
03:52:34,000 --> 03:52:36,800
statement
5832
03:52:35,199 --> 03:52:39,199
we will enter
5833
03:52:36,800 --> 03:52:41,840
row number
5834
03:52:39,199 --> 03:52:41,840
one through three
5835
03:52:42,000 --> 03:52:47,279
and then use the scan f function to
5836
03:52:44,880 --> 03:52:49,840
accept some user input so this will be
5837
03:52:47,279 --> 03:52:52,000
an integer and use the address of
5838
03:52:49,840 --> 03:52:54,640
operator x
5839
03:52:52,000 --> 03:52:56,319
so a user is going to enter in numbers
5840
03:52:54,640 --> 03:52:58,640
one through three for which row that
5841
03:52:56,319 --> 03:53:01,199
they need but with arrays they always
5842
03:52:58,640 --> 03:53:03,279
begin with zero but a user is not going
5843
03:53:01,199 --> 03:53:06,399
to know that so with whatever the user
5844
03:53:03,279 --> 03:53:08,960
types in we will decrement x by one
5845
03:53:06,399 --> 03:53:10,399
so this gives us rows zero through two
5846
03:53:08,960 --> 03:53:13,040
technically
5847
03:53:10,399 --> 03:53:17,120
then do the same thing with our columns
5848
03:53:13,040 --> 03:53:20,800
enter column number one through three
5849
03:53:17,120 --> 03:53:22,800
address of operator y then decrement y
5850
03:53:20,800 --> 03:53:25,760
we will check to see if the coordinates
5851
03:53:22,800 --> 03:53:28,000
that the user gave are occupied or not
5852
03:53:25,760 --> 03:53:30,560
so using an if statement
5853
03:53:28,000 --> 03:53:32,960
we will check to see if our board
5854
03:53:30,560 --> 03:53:34,319
at index of x
5855
03:53:32,960 --> 03:53:37,680
and y
5856
03:53:34,319 --> 03:53:40,000
does not equal an empty space that means
5857
03:53:37,680 --> 03:53:41,359
that this spot is currently occupied by
5858
03:53:40,000 --> 03:53:43,199
another character
5859
03:53:41,359 --> 03:53:45,040
so let's print something to let a user
5860
03:53:43,199 --> 03:53:48,160
know
5861
03:53:45,040 --> 03:53:51,279
invalid move
5862
03:53:48,160 --> 03:53:53,600
else that spot is open
5863
03:53:51,279 --> 03:53:56,960
so take our board
5864
03:53:53,600 --> 03:53:59,520
at index of x and y
5865
03:53:56,960 --> 03:54:01,520
set this equal to our player character
5866
03:53:59,520 --> 03:54:04,319
so we're going to take all of this code
5867
03:54:01,520 --> 03:54:05,920
and place it within a do while loop so
5868
03:54:04,319 --> 03:54:07,120
write do
5869
03:54:05,920 --> 03:54:08,479
while
5870
03:54:07,120 --> 03:54:11,040
place your code that you just wrote
5871
03:54:08,479 --> 03:54:11,040
within there
5872
03:54:13,120 --> 03:54:16,880
and our condition
5873
03:54:14,720 --> 03:54:18,800
is if our board
5874
03:54:16,880 --> 03:54:22,239
at index of x
5875
03:54:18,800 --> 03:54:24,080
and y does not equal an empty space
5876
03:54:22,239 --> 03:54:26,479
if the spot that the player would like
5877
03:54:24,080 --> 03:54:29,199
to place their character is currently
5878
03:54:26,479 --> 03:54:31,199
occupied then we will ask them again to
5879
03:54:29,199 --> 03:54:33,520
enter some coordinates uh then within
5880
03:54:31,199 --> 03:54:35,520
our else statement we need to break out
5881
03:54:33,520 --> 03:54:38,880
of this while loop okay so that is
5882
03:54:35,520 --> 03:54:40,880
everything for the player move function
5883
03:54:38,880 --> 03:54:41,840
so then heading back to the main
5884
03:54:40,880 --> 03:54:43,840
function
5885
03:54:41,840 --> 03:54:45,520
after the player moves we will check to
5886
03:54:43,840 --> 03:54:48,880
see if there's a winner
5887
03:54:45,520 --> 03:54:50,880
winner equals then invoke the check
5888
03:54:48,880 --> 03:54:53,920
winner function
5889
03:54:50,880 --> 03:54:56,720
and we will fill in this function next
5890
03:54:53,920 --> 03:54:58,239
so find the check winner function and we
5891
03:54:56,720 --> 03:54:59,439
need to check all of the different win
5892
03:54:58,239 --> 03:55:02,479
conditions
5893
03:54:59,439 --> 03:55:05,120
so first we will check each row using a
5894
03:55:02,479 --> 03:55:08,160
for loop so this section of code we will
5895
03:55:05,120 --> 03:55:11,120
check our rows we'll need a for loop to
5896
03:55:08,160 --> 03:55:12,560
iterate three times one for each row
5897
03:55:11,120 --> 03:55:15,359
int i
5898
03:55:12,560 --> 03:55:17,040
equals zero continue this as long as i
5899
03:55:15,359 --> 03:55:19,840
is less than three
5900
03:55:17,040 --> 03:55:21,920
increment i by one so this if statement
5901
03:55:19,840 --> 03:55:24,399
is going to get a little bit funky we
5902
03:55:21,920 --> 03:55:25,520
need to check each set of horizontal
5903
03:55:24,399 --> 03:55:30,640
elements
5904
03:55:25,520 --> 03:55:33,760
so let's begin with board at index of i
5905
03:55:30,640 --> 03:55:36,479
and zero so that would be the top left
5906
03:55:33,760 --> 03:55:38,720
element and we are checking to see if
5907
03:55:36,479 --> 03:55:40,160
the character here is equal to this
5908
03:55:38,720 --> 03:55:43,120
character
5909
03:55:40,160 --> 03:55:45,520
so board at index i
5910
03:55:43,120 --> 03:55:48,800
and one
5911
03:55:45,520 --> 03:55:52,880
and we're checking to see if board at
5912
03:55:48,800 --> 03:55:55,600
index of i and zero is equal to board at
5913
03:55:52,880 --> 03:55:58,000
index of i and two so here we're
5914
03:55:55,600 --> 03:56:00,720
checking to see if this element is equal
5915
03:55:58,000 --> 03:56:02,319
to this element and this element is
5916
03:56:00,720 --> 03:56:04,080
equal to this element
5917
03:56:02,319 --> 03:56:06,399
if they're all consistent we have a
5918
03:56:04,080 --> 03:56:08,239
winner so we will return whatever
5919
03:56:06,399 --> 03:56:11,199
character is within one of these
5920
03:56:08,239 --> 03:56:14,560
elements let's say this one so return
5921
03:56:11,199 --> 03:56:16,880
board at index of i index of zero return
5922
03:56:14,560 --> 03:56:18,640
whatever character is within here this
5923
03:56:16,880 --> 03:56:21,199
section of code will check all of the
5924
03:56:18,640 --> 03:56:25,040
win conditions for each row but now we
5925
03:56:21,199 --> 03:56:27,920
need columns so check columns
5926
03:56:25,040 --> 03:56:29,600
and we can copy most of this so copy it
5927
03:56:27,920 --> 03:56:31,120
and paste it
5928
03:56:29,600 --> 03:56:32,800
then we just need to change these
5929
03:56:31,120 --> 03:56:36,000
indices around
5930
03:56:32,800 --> 03:56:36,880
so if board at index 0
5931
03:56:36,000 --> 03:56:40,560
i
5932
03:56:36,880 --> 03:56:42,319
is equal to board at index of one
5933
03:56:40,560 --> 03:56:46,000
i
5934
03:56:42,319 --> 03:56:46,800
and board at index of zero
5935
03:56:46,000 --> 03:56:50,399
i
5936
03:56:46,800 --> 03:56:53,120
is equal to board at index of two
5937
03:56:50,399 --> 03:56:56,720
i then we will return whatever character
5938
03:56:53,120 --> 03:56:59,279
is within board index of zero index of
5939
03:56:56,720 --> 03:57:02,239
one so this section of code will check
5940
03:56:59,279 --> 03:57:05,359
for any column when conditions then next
5941
03:57:02,239 --> 03:57:08,720
we have diagonals there's only two
5942
03:57:05,359 --> 03:57:11,199
check diagonals
5943
03:57:08,720 --> 03:57:15,680
so let's copy this section of code
5944
03:57:11,199 --> 03:57:16,960
paste it so our indices are 0 0
5945
03:57:15,680 --> 03:57:17,760
is equal to
5946
03:57:16,960 --> 03:57:21,040
1
5947
03:57:17,760 --> 03:57:23,439
1. 1 1 is in the middle and board at
5948
03:57:21,040 --> 03:57:27,359
index of zero zero
5949
03:57:23,439 --> 03:57:29,920
is equal to board at index of two two
5950
03:57:27,359 --> 03:57:31,120
if so then return zero
5951
03:57:29,920 --> 03:57:33,760
zero
5952
03:57:31,120 --> 03:57:34,640
then we have one more diagonal so copy
5953
03:57:33,760 --> 03:57:36,160
this
5954
03:57:34,640 --> 03:57:39,840
paste it
5955
03:57:36,160 --> 03:57:40,640
and here are the elements if board at 0
5956
03:57:39,840 --> 03:57:42,640
2
5957
03:57:40,640 --> 03:57:46,880
is equal to board at
5958
03:57:42,640 --> 03:57:48,399
1 1 so 0 2 is the bottom left 1 1 is the
5959
03:57:46,880 --> 03:57:52,080
middle and
5960
03:57:48,399 --> 03:57:55,040
board at zero zero equals board at two
5961
03:57:52,080 --> 03:57:57,439
zero that is the top right corner right
5962
03:57:55,040 --> 03:58:00,960
here so if we have a diagonal we have a
5963
03:57:57,439 --> 03:58:03,439
winner so return board at zero
5964
03:58:00,960 --> 03:58:05,439
2 let's say if after checking all of
5965
03:58:03,439 --> 03:58:06,560
these different win conditions there is
5966
03:58:05,439 --> 03:58:09,120
no winner
5967
03:58:06,560 --> 03:58:11,600
then let's return an empty character
5968
03:58:09,120 --> 03:58:13,920
that means there currently is no winner
5969
03:58:11,600 --> 03:58:16,399
so then head back to the main function
5970
03:58:13,920 --> 03:58:17,920
after invoking the check winner function
5971
03:58:16,399 --> 03:58:19,760
let's write an if statement to see if
5972
03:58:17,920 --> 03:58:23,359
the game is over
5973
03:58:19,760 --> 03:58:26,239
so our condition is if winner does not
5974
03:58:23,359 --> 03:58:27,359
equal an empty space that means there is
5975
03:58:26,239 --> 03:58:30,560
a winner
5976
03:58:27,359 --> 03:58:34,080
or after invoking the check free spaces
5977
03:58:30,560 --> 03:58:36,239
function and the value returned is zero
5978
03:58:34,080 --> 03:58:38,239
then we will break out of this while
5979
03:58:36,239 --> 03:58:40,080
loop this is everything done for the
5980
03:58:38,239 --> 03:58:41,840
player now we need to create a section
5981
03:58:40,080 --> 03:58:42,800
of code within our while loop for the
5982
03:58:41,840 --> 03:58:45,920
computer
5983
03:58:42,800 --> 03:58:48,000
so copy this section of code and paste
5984
03:58:45,920 --> 03:58:50,399
it and this time it will be the
5985
03:58:48,000 --> 03:58:52,720
computer's move
5986
03:58:50,399 --> 03:58:54,960
invoke the computer move function and we
5987
03:58:52,720 --> 03:58:56,000
will need to fill in the computer move
5988
03:58:54,960 --> 03:58:58,239
function
5989
03:58:56,000 --> 03:59:00,000
within the computer move function the
5990
03:58:58,239 --> 03:59:02,239
computer's move will be randomly
5991
03:59:00,000 --> 03:59:05,760
generated and to generate some random
5992
03:59:02,239 --> 03:59:05,760
numbers we'll need a seed
5993
03:59:06,319 --> 03:59:10,000
so to create a seed to generate random
5994
03:59:08,399 --> 03:59:12,479
numbers
5995
03:59:10,000 --> 03:59:14,479
invoke the s rand function
5996
03:59:12,479 --> 03:59:17,520
pass in time
5997
03:59:14,479 --> 03:59:21,199
invoke it pass in zero
5998
03:59:17,520 --> 03:59:23,600
and we will declare intex and into y we
5999
03:59:21,199 --> 03:59:26,399
will generate two random numbers between
6000
03:59:23,600 --> 03:59:28,319
zero and two now before we generate some
6001
03:59:26,399 --> 03:59:30,880
random numbers let's check to see if we
6002
03:59:28,319 --> 03:59:33,840
even have any free spaces available
6003
03:59:30,880 --> 03:59:37,279
so our if statement is going to be check
6004
03:59:33,840 --> 03:59:39,600
free spaces invoke it then if this is
6005
03:59:37,279 --> 03:59:41,120
greater than zero then we will generate
6006
03:59:39,600 --> 03:59:44,960
some random numbers
6007
03:59:41,120 --> 03:59:47,120
and we'll do so within a do while loop
6008
03:59:44,960 --> 03:59:49,760
so within this do while loop we will
6009
03:59:47,120 --> 03:59:50,880
generate two random numbers between 0
6010
03:59:49,760 --> 03:59:52,399
and 2.
6011
03:59:50,880 --> 03:59:54,399
x equals
6012
03:59:52,399 --> 03:59:56,640
invoke the rand function
6013
03:59:54,399 --> 04:00:00,080
modulus three
6014
03:59:56,640 --> 04:00:02,960
then y equals invoke the rand function
6015
04:00:00,080 --> 04:00:05,279
modulus three with our condition let's
6016
04:00:02,960 --> 04:00:06,399
check to see if the spot generated is
6017
04:00:05,279 --> 04:00:09,120
even open
6018
04:00:06,399 --> 04:00:10,319
so board at index of x
6019
04:00:09,120 --> 04:00:13,279
and y
6020
04:00:10,319 --> 04:00:15,920
does not equal an empty space so we will
6021
04:00:13,279 --> 04:00:18,319
keep on generating random numbers until
6022
04:00:15,920 --> 04:00:21,199
there is an open space then escape this
6023
04:00:18,319 --> 04:00:25,760
while loop so if we find an open space
6024
04:00:21,199 --> 04:00:28,479
let's take our board at index of x and y
6025
04:00:25,760 --> 04:00:31,040
set the sequel to our computer player
6026
04:00:28,479 --> 04:00:33,040
this is all within an if statement
6027
04:00:31,040 --> 04:00:34,239
else if there are no more spaces
6028
04:00:33,040 --> 04:00:39,359
available
6029
04:00:34,239 --> 04:00:41,439
we will invoke the print winner function
6030
04:00:39,359 --> 04:00:43,279
and pass in an empty space
6031
04:00:41,439 --> 04:00:44,399
this means that there is no winner it's
6032
04:00:43,279 --> 04:00:46,239
a draw
6033
04:00:44,399 --> 04:00:50,080
now let's fill in this print winner
6034
04:00:46,239 --> 04:00:53,760
function this function is fairly easy
6035
04:00:50,080 --> 04:00:56,800
if winner is equal to player
6036
04:00:53,760 --> 04:00:56,800
then we will print
6037
04:00:56,960 --> 04:00:59,600
you win
6038
04:00:59,840 --> 04:01:06,000
else if
6039
04:01:01,760 --> 04:01:09,040
winner is equal to computer
6040
04:01:06,000 --> 04:01:09,040
then we will print
6041
04:01:09,680 --> 04:01:15,279
you lose
6042
04:01:12,479 --> 04:01:19,000
else if there is no winner then it's a
6043
04:01:15,279 --> 04:01:19,000
tie it's a draw
6044
04:01:20,560 --> 04:01:24,000
it's a tie
6045
04:01:22,319 --> 04:01:26,479
within the main function we just have a
6046
04:01:24,000 --> 04:01:29,840
few more things to add so outside of our
6047
04:01:26,479 --> 04:01:31,680
while loop we will print our board and
6048
04:01:29,840 --> 04:01:32,560
print the winner
6049
04:01:31,680 --> 04:01:34,000
print
6050
04:01:32,560 --> 04:01:35,520
board
6051
04:01:34,000 --> 04:01:37,040
and print
6052
04:01:35,520 --> 04:01:39,439
winner
6053
04:01:37,040 --> 04:01:40,960
there is one argument whoever the winner
6054
04:01:39,439 --> 04:01:43,040
is
6055
04:01:40,960 --> 04:01:45,359
so let's run this once
6056
04:01:43,040 --> 04:01:46,880
enter row numbers one through three uh
6057
04:01:45,359 --> 04:01:48,640
let's say one
6058
04:01:46,880 --> 04:01:50,720
and one
6059
04:01:48,640 --> 04:01:53,199
so i moved here and the computer moved
6060
04:01:50,720 --> 04:01:56,319
to row three column two
6061
04:01:53,199 --> 04:01:58,399
uh let's say row three column three
6062
04:01:56,319 --> 04:02:01,040
okay so i moved here and the computer is
6063
04:01:58,399 --> 04:02:03,600
up here so i moved to row three column
6064
04:02:01,040 --> 04:02:04,880
three and our computer is at row one
6065
04:02:03,600 --> 04:02:07,520
column three
6066
04:02:04,880 --> 04:02:08,399
uh then i'll move to the middle two
6067
04:02:07,520 --> 04:02:10,479
two
6068
04:02:08,399 --> 04:02:12,080
and it looks like i won this round so
6069
04:02:10,479 --> 04:02:15,680
let's try this again and we will
6070
04:02:12,080 --> 04:02:17,520
intentionally lose so one one
6071
04:02:15,680 --> 04:02:18,960
two one
6072
04:02:17,520 --> 04:02:20,960
one two
6073
04:02:18,960 --> 04:02:23,680
two two
6074
04:02:20,960 --> 04:02:27,800
and unfortunately it looks like i lost
6075
04:02:23,680 --> 04:02:27,800
this time let's attempt to tie
6076
04:02:30,080 --> 04:02:34,000
and it looks like it's a tie so it looks
6077
04:02:32,000 --> 04:02:35,520
like this game is working so what if we
6078
04:02:34,000 --> 04:02:37,439
would like to ask the user if they would
6079
04:02:35,520 --> 04:02:39,600
like to play again we can easily place
6080
04:02:37,439 --> 04:02:41,199
our code within a do while loop so
6081
04:02:39,600 --> 04:02:42,800
within our main function
6082
04:02:41,199 --> 04:02:44,239
let's create char
6083
04:02:42,800 --> 04:02:46,880
response
6084
04:02:44,239 --> 04:02:49,359
and we will create a do while loop
6085
04:02:46,880 --> 04:02:52,880
so take all of this code after the do
6086
04:02:49,359 --> 04:02:54,399
while loop beginning with reset board
6087
04:02:52,880 --> 04:02:55,760
copy it
6088
04:02:54,399 --> 04:02:57,439
delete it
6089
04:02:55,760 --> 04:02:59,680
and then paste it within the do while
6090
04:02:57,439 --> 04:02:59,680
loop
6091
04:03:01,279 --> 04:03:07,040
at the top of our do while loop let's
6092
04:03:03,199 --> 04:03:10,399
reset winner and response
6093
04:03:07,040 --> 04:03:13,920
winner equals an empty space
6094
04:03:10,399 --> 04:03:15,760
and response equals an empty space
6095
04:03:13,920 --> 04:03:17,760
then heading to the bottom of our do
6096
04:03:15,760 --> 04:03:19,120
while loop let's ask if the user would
6097
04:03:17,760 --> 04:03:21,520
like to play again
6098
04:03:19,120 --> 04:03:22,840
so after displaying the winner let's
6099
04:03:21,520 --> 04:03:24,720
create a prompt
6100
04:03:22,840 --> 04:03:30,080
printf
6101
04:03:24,720 --> 04:03:30,080
would you like to play again
6102
04:03:30,239 --> 04:03:33,760
y for yes and for no
6103
04:03:34,640 --> 04:03:39,439
and i'm just going to clear our buffer
6104
04:03:36,720 --> 04:03:41,040
by using scanf and accepting a character
6105
04:03:39,439 --> 04:03:43,279
just in case there is a new line
6106
04:03:41,040 --> 04:03:46,160
character within our buffer then we will
6107
04:03:43,279 --> 04:03:48,960
use the scanf function and accept a
6108
04:03:46,160 --> 04:03:51,520
character after clearing our buffer
6109
04:03:48,960 --> 04:03:52,720
and we will use the address of operator
6110
04:03:51,520 --> 04:03:54,720
response
6111
04:03:52,720 --> 04:03:56,560
now just in case somebody types in
6112
04:03:54,720 --> 04:03:59,040
lowercase y i would still like to count
6113
04:03:56,560 --> 04:04:00,399
that as yes so i'm going to take our
6114
04:03:59,040 --> 04:04:03,840
response
6115
04:04:00,399 --> 04:04:05,760
set this equal to invoke the to
6116
04:04:03,840 --> 04:04:08,640
upper function
6117
04:04:05,760 --> 04:04:10,720
pass in response
6118
04:04:08,640 --> 04:04:14,399
and with this condition we will continue
6119
04:04:10,720 --> 04:04:16,560
playing while our response is equal to
6120
04:04:14,399 --> 04:04:18,640
the character y
6121
04:04:16,560 --> 04:04:21,199
and at the end let's print
6122
04:04:18,640 --> 04:04:23,920
thanks for playing okay let's run this
6123
04:04:21,199 --> 04:04:23,920
one last time
6124
04:04:25,920 --> 04:04:28,399
all right i win would you like to play
6125
04:04:27,600 --> 04:04:30,720
again
6126
04:04:28,399 --> 04:04:34,600
i'll type in y for yes
6127
04:04:30,720 --> 04:04:34,600
and we have a new game
6128
04:04:35,439 --> 04:04:40,399
all right it looks like i win so this
6129
04:04:37,439 --> 04:04:41,840
time i will exit by typing n
6130
04:04:40,399 --> 04:04:44,319
thanks for playing
6131
04:04:41,840 --> 04:04:46,560
so yeah everybody that is a game of
6132
04:04:44,319 --> 04:04:48,399
tic-tac-toe if you would like a copy of
6133
04:04:46,560 --> 04:04:50,479
this code i'll post this to the comments
6134
04:04:48,399 --> 04:04:52,239
section down below hey if you found this
6135
04:04:50,479 --> 04:04:54,000
video helpful you can help me out by
6136
04:04:52,239 --> 04:04:55,840
smashing that like button leave a random
6137
04:04:54,000 --> 04:04:59,960
comment down below and subscribe if
6138
04:04:55,840 --> 04:04:59,960
you'd like to become a fellow bro447881
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.