Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:05,310 --> 00:00:06,720
Hello, and welcome to a new section.
2
00:00:07,160 --> 00:00:10,229
In this section, we're going to
talk all about advanced data types.
3
00:00:10,599 --> 00:00:13,120
We're going to start with the
define statement, which is a
4
00:00:13,120 --> 00:00:16,599
preprocessor directive that really
has to just do with constants.
5
00:00:17,930 --> 00:00:21,680
So the defined preprocessor
directive, like all other preprocessor
6
00:00:21,680 --> 00:00:25,360
directives, begins with the pound
symbol at the beginning of a line.
7
00:00:26,009 --> 00:00:29,700
It can be preceded by spaces
or tabs and it also allows for
8
00:00:29,700 --> 00:00:32,649
space between the pound and
the remainder of the directive.
9
00:00:33,589 --> 00:00:39,000
So we've seen this before in
previous preprocessor directives,
10
00:00:39,060 --> 00:00:41,950
right, specifically with the
include preprocessor directive.
11
00:00:42,390 --> 00:00:44,060
There's another one with define.
12
00:00:44,650 --> 00:00:48,379
So in c you can use this
directive to define a symbolic or
13
00:00:48,380 --> 00:00:50,359
manifest constants in a program.
14
00:00:50,839 --> 00:00:53,499
It's all about using
constants, so when you have
15
00:00:53,500 --> 00:00:54,749
data that doesn't change.
16
00:00:55,300 --> 00:00:58,750
The preprocessor directives
run until the first new line
17
00:00:58,760 --> 00:01:00,010
following the pound symbol.
18
00:01:00,740 --> 00:01:03,230
So a directive is limited
to one line in length.
19
00:01:03,800 --> 00:01:07,579
The combination the backslash
the new line is deleted before
20
00:01:07,580 --> 00:01:09,840
the preprocessor begins, right.
21
00:01:09,849 --> 00:01:12,990
So you can spread the directive over
several physical lines if you want to.
22
00:01:13,380 --> 00:01:15,770
These lines still constitute
a single logical line.
23
00:01:16,430 --> 00:01:19,480
So make sure you remember what
a preprocessor directive is.
24
00:01:19,890 --> 00:01:22,290
It's basically code that
runs before the compiler.
25
00:01:22,750 --> 00:01:25,880
So when it sees this preprocessor
directive for define, it's
26
00:01:25,880 --> 00:01:28,510
going to be running before
the compiler for consonants.
27
00:01:29,119 --> 00:01:30,980
It's a way of telling
the compiler something.
28
00:01:31,360 --> 00:01:33,060
In this case, that
something is a constant.
29
00:01:34,910 --> 00:01:37,310
Typically, defined statements
appear first in the program
30
00:01:37,310 --> 00:01:38,460
usually right at the top.
31
00:01:39,059 --> 00:01:41,650
It's not required to do that. You
can put them anywhere you want, but
32
00:01:41,650 --> 00:01:43,390
common convention is right at the top.
33
00:01:44,020 --> 00:01:46,839
Most programmers group their defined
statements at the beginning of the
34
00:01:46,850 --> 00:01:51,080
program or inside an include file,
a lot of times they're reused.
35
00:01:51,660 --> 00:01:54,699
They can be quickly referenced and
shared by more than one source file.
36
00:01:55,330 --> 00:01:58,949
By convention, the defined
names are defined using only
37
00:01:58,940 --> 00:02:00,550
uppercase letters and underscores.
38
00:02:00,929 --> 00:02:04,080
So again, you typically see
these common conventions.
39
00:02:05,760 --> 00:02:11,150
Here is an example of how you can use
the define statement specifically, for
40
00:02:11,150 --> 00:02:13,180
symbolic names to program constants.
41
00:02:13,460 --> 00:02:16,820
So you'd have something like
define with the pound symbol, yes,
42
00:02:16,900 --> 00:02:18,760
and again that's all uppercase.
43
00:02:19,120 --> 00:02:20,950
It's only one word, so you
don't need the underscore.
44
00:02:21,219 --> 00:02:24,239
And then right after,
you see the one, right.
45
00:02:24,240 --> 00:02:26,830
So this is going to define
the name yes and make it
46
00:02:26,860 --> 00:02:28,190
equivalent to the value one.
47
00:02:28,190 --> 00:02:31,540
So anytime in your program
where you see yes, it's going
48
00:02:31,540 --> 00:02:36,970
to interpolate that yes define
and put one inside of it, right.
49
00:02:36,970 --> 00:02:39,390
So it's sort of like just doing
the same as doing a search and
50
00:02:39,390 --> 00:02:41,349
replace with the text editor, right.
51
00:02:41,379 --> 00:02:44,650
Whenever you see the name yes,
it can be used subsequently
52
00:02:44,660 --> 00:02:46,190
anywhere you need to have constant.
53
00:02:46,800 --> 00:02:49,190
The preprocessor because it runs
before the compiler is going to
54
00:02:49,190 --> 00:02:53,230
replace all these occurrences of the
defined with its associated text.
55
00:02:53,980 --> 00:02:56,320
The defined statement
has a special syntax.
56
00:02:56,839 --> 00:02:58,010
There's no equal sign, right.
57
00:02:58,029 --> 00:03:00,270
So you should have noticed that
usually when you assign data to
58
00:03:00,270 --> 00:03:01,530
variables, there's an equal sign.
59
00:03:01,720 --> 00:03:04,440
But remember, this is
not a variable, right.
60
00:03:04,450 --> 00:03:07,510
So there's no equal sign when
you have to assign one to yes.
61
00:03:08,120 --> 00:03:10,329
There's also no semicolon, right.
62
00:03:10,340 --> 00:03:11,880
So notice those differences.
63
00:03:11,889 --> 00:03:13,220
It's special syntax.
64
00:03:15,839 --> 00:03:18,420
Whenever this name appears,
it's defined value of one is
65
00:03:18,430 --> 00:03:21,489
automatically substituted into
the program by the preprocessor.
66
00:03:22,220 --> 00:03:27,100
Another example here is if you have
game over equals yes since yes is
67
00:03:27,100 --> 00:03:30,290
a defined statement, game over is
actually going to be equal to one.
68
00:03:30,719 --> 00:03:33,489
It has the same effect of
assigning one to game over.
69
00:03:34,670 --> 00:03:36,860
But it is a little bit more
readable, and that's why one
70
00:03:36,860 --> 00:03:38,210
of the reasons you use defines.
71
00:03:39,040 --> 00:03:42,250
A defined name is not a variable and
that's why it doesn't use the equals
72
00:03:42,530 --> 00:03:44,080
operator and there's no semicolon.
73
00:03:44,450 --> 00:03:47,489
You can't assign a value to it
unless the result of substituting the
74
00:03:47,490 --> 00:03:50,030
defined value is in fact a variable.
75
00:03:50,660 --> 00:03:52,539
So let's look at some
defined expressions.
76
00:03:52,850 --> 00:03:57,109
Remember, expression are usually
things that return information.
77
00:03:57,560 --> 00:04:00,170
So definition for a name can
include more than a simple
78
00:04:00,170 --> 00:04:01,660
constant value, right.
79
00:04:01,680 --> 00:04:02,880
It can be an expression.
80
00:04:03,410 --> 00:04:07,739
And so for example, you could
have a define which is named 2
81
00:04:08,110 --> 00:04:11,970
pi as the product of 2 and 3.14.
82
00:04:12,400 --> 00:04:13,540
So you're going to multiply these.
83
00:04:13,830 --> 00:04:18,240
So all you would do is you would
say define 2 pi and then right after
84
00:04:18,240 --> 00:04:21,000
the space 2.0 times this number.
85
00:04:22,089 --> 00:04:24,460
And then you'd be able to
use this defined name Two PI
86
00:04:24,469 --> 00:04:29,259
anywhere in the program where
the expression is 2 times 3.14.
87
00:04:30,460 --> 00:04:32,050
So instead of having to use
that -- and again, it makes
88
00:04:32,050 --> 00:04:33,350
it more readable, Two PI.
89
00:04:33,800 --> 00:04:37,190
So you could replace the return
statement of a circumference function
90
00:04:37,550 --> 00:04:38,630
with the following statement.
91
00:04:38,889 --> 00:04:42,100
Return two pi times r instead
of having to hard code those
92
00:04:42,100 --> 00:04:45,139
constants, even though 2
pi represents a constant.
93
00:04:46,820 --> 00:04:47,799
You also have null.
94
00:04:47,809 --> 00:04:50,520
So the defined name null is
frequently used by programmers
95
00:04:50,520 --> 00:04:51,849
to represent the null pointer.
96
00:04:52,030 --> 00:04:54,470
We've used this in past programs.
97
00:04:54,810 --> 00:04:56,710
This is a defined underneath the hood.
98
00:04:57,240 --> 00:04:59,520
It's defined inside standard def.h.
99
00:05:00,460 --> 00:05:03,740
So all you see if you look at that
header file is define null and
100
00:05:03,740 --> 00:05:05,660
it's actually 0, represents 0.
101
00:05:06,730 --> 00:05:08,740
So it makes the program
more readable as opposed to
102
00:05:08,740 --> 00:05:10,030
putting something like 0.
103
00:05:10,350 --> 00:05:15,159
You can actually say why my pointer
is not equal to no as opposed to
104
00:05:15,160 --> 00:05:17,240
why my pointer is not equal to 0.
105
00:05:17,759 --> 00:05:18,559
So much more readable.
106
00:05:19,690 --> 00:05:22,410
So to set up a while loop that will
execute as long as the value of list
107
00:05:22,410 --> 00:05:25,690
pointer is not equal to the null
pointer, you could do that why
108
00:05:25,700 --> 00:05:26,840
this pointer is not equal to null.
109
00:05:27,630 --> 00:05:31,240
So what are the benefits of
using this defined preprocessor?
110
00:05:31,250 --> 00:05:34,099
Well, when you use this defined name
for a constant value, it helps to make
111
00:05:34,099 --> 00:05:35,909
the programs more readily extendable.
112
00:05:36,570 --> 00:05:38,520
It also makes them more
readable, as I mentioned.
113
00:05:39,000 --> 00:05:41,409
You know exactly what's going
on in the code if you see a name
114
00:05:41,410 --> 00:05:42,550
as opposed to some constant.
115
00:05:43,220 --> 00:05:46,540
So for example when you define an
array, you must specify the number
116
00:05:46,540 --> 00:05:49,870
of elements in the array subsequent
program statements will likely
117
00:05:49,870 --> 00:05:52,860
use the knowledge of the number
of elements contained inside the
118
00:05:52,860 --> 00:05:55,160
array to get the size and so forth.
119
00:05:55,179 --> 00:05:56,950
So you're going to see it quite a bit.
120
00:05:57,250 --> 00:06:00,870
So you could just say float
data values 1,000, 1000 is
121
00:06:00,870 --> 00:06:02,080
kind of like a magic number.
122
00:06:03,260 --> 00:06:04,140
It's not very readable.
123
00:06:04,440 --> 00:06:06,240
There's a good chance that when
you see the statements in the
124
00:06:06,240 --> 00:06:12,370
program the fact that you use
this 1000 is not very readable.
125
00:06:12,679 --> 00:06:15,880
You end up using it a lot
for iterating through some
126
00:06:15,880 --> 00:06:17,980
kind of size of the array,
so you might have a for loop.
127
00:06:18,820 --> 00:06:21,330
So you would use the value
as an upper bound for size.
128
00:06:21,990 --> 00:06:25,170
Suppose you had to increase the
size of the values from one 1000
129
00:06:25,200 --> 00:06:29,609
to 2000, you would then have to
change all the spots in the program
130
00:06:29,610 --> 00:06:31,060
that use that hard coded value.
131
00:06:31,770 --> 00:06:35,350
It'd be much nicer if you had
something at the top and you only
132
00:06:35,350 --> 00:06:36,509
had to change it in one spot.
133
00:06:37,640 --> 00:06:40,610
So what you could do is a better
way of dealing with this array
134
00:06:40,610 --> 00:06:44,640
bounds is to define a name and
use the preprocessor directive
135
00:06:44,880 --> 00:06:46,090
for the upper array bound.
136
00:06:46,469 --> 00:06:49,969
So something like maximum data
values and then setting that to 1000.
137
00:06:50,690 --> 00:06:53,919
Now anywhere in your program,
you can use maximum data values
138
00:06:54,689 --> 00:06:57,890
when you declare the size of
the array or inside your loop.
139
00:06:58,760 --> 00:07:01,600
So if you're going through
an array bound, you can just
140
00:07:01,600 --> 00:07:02,919
say maximum data values.
141
00:07:04,510 --> 00:07:08,169
And now if maximum data values
changes, all you have to do is change
142
00:07:08,170 --> 00:07:09,629
it at the top, the define statement.
143
00:07:10,689 --> 00:07:13,910
So if you want to change it
to 2000, you simply say define
144
00:07:13,910 --> 00:07:15,360
maximum data values 2000.
145
00:07:16,480 --> 00:07:19,000
So when the program is written
using this preprocessor director
146
00:07:19,000 --> 00:07:22,320
define, in all cases where the
size of the array was used, it's
147
00:07:22,320 --> 00:07:23,690
automatically going to be updated.
148
00:07:25,080 --> 00:07:27,810
So that define could be the only
statement in the program and you
149
00:07:27,810 --> 00:07:31,670
only have to update it once as
opposed to everywhere else using it.
150
00:07:32,500 --> 00:07:35,849
Another nice use or another
benefits of define is that the
151
00:07:35,850 --> 00:07:39,150
defined statement helps make
programs more portable from one
152
00:07:39,150 --> 00:07:40,690
computer system to another.
153
00:07:41,190 --> 00:07:43,479
So, it might be necessary to use
constant values that are related
154
00:07:43,500 --> 00:07:45,850
to the particular computer on
which the program is running.
155
00:07:46,449 --> 00:07:50,150
A lot of times we have data types
and data types are different sizes
156
00:07:50,150 --> 00:07:53,380
depending on if you're running
on a particular architecture.
157
00:07:53,940 --> 00:07:56,590
So it might have to also do
with particular memory address,
158
00:07:56,610 --> 00:07:59,689
file name or the number of bits
contained in a computer word.
159
00:08:00,429 --> 00:08:03,919
If you want to execute a program on
a different machine where an int for
160
00:08:03,920 --> 00:08:14,289
example is 64 bits versus 32, 4 bytes
versus 8 bytes, you can basically
161
00:08:14,290 --> 00:08:15,920
use the define to set this value.
162
00:08:16,429 --> 00:08:18,810
So in situations when the program
must be written to make use of
163
00:08:18,820 --> 00:08:23,250
machine dependent values, the
defined statement would be ideal.
164
00:08:23,699 --> 00:08:25,710
Because it can help isolate
the machine dependent values
165
00:08:25,710 --> 00:08:28,960
from the program as much as
possible, which makes it more
166
00:08:28,960 --> 00:08:30,680
portable to another machine.
167
00:08:31,960 --> 00:08:34,819
Now you may be asking yourself
when should we use define versus
168
00:08:34,820 --> 00:08:38,460
constant because c has a nice
little keyword called const.
169
00:08:39,370 --> 00:08:45,170
And so normally, if you define pi
equal to 3.14159,
170
00:08:45,360 --> 00:08:49,329
this is going to define pi as a symbol and the
preprocessor is going to go through
171
00:08:49,330 --> 00:08:54,790
all the text and replace it with that
value 3.14 anywhere it sees the names pi.
172
00:08:55,219 --> 00:08:58,890
Now you could have defined pi
as a variable, but to tell the
173
00:08:58,890 --> 00:09:01,960
compiler that its value is fixed
it must not be changed, so we
174
00:09:01,960 --> 00:09:03,170
could use something like const.
175
00:09:03,740 --> 00:09:05,810
So you can fix the value of
any variable when you declare
176
00:09:05,810 --> 00:09:07,960
it by prefixing the type name
with the keyword const, so we
177
00:09:07,960 --> 00:09:09,380
could have done const flow pi.
178
00:09:10,480 --> 00:09:15,030
This defines the value of pi as
fixed, has the same effect as
179
00:09:15,030 --> 00:09:16,510
using the preprocessor directive.
180
00:09:17,230 --> 00:09:19,570
So the advantage of defining pi
in this way is that you're now
181
00:09:19,570 --> 00:09:23,430
defining as a constant numerical
value with a specified type.
182
00:09:24,350 --> 00:09:28,150
When you use define, pi is
just a sequence of characters
183
00:09:28,150 --> 00:09:30,260
that replaces all the
occurrences of pi in your code.
184
00:09:30,900 --> 00:09:33,579
Constant is sometimes
preferred because it does
185
00:09:33,580 --> 00:09:34,890
do the data type checking.
186
00:09:36,240 --> 00:09:39,170
So you're going to see define
preprocessor all over the place
187
00:09:39,170 --> 00:09:42,120
again usually at the top of
the file to represent constant.
188
00:09:42,230 --> 00:09:44,860
Just remember, it has the pound
symbol so it's a preprocessor
189
00:09:44,860 --> 00:09:50,280
directive and it's just used to assign
constants to names, makes the program
190
00:09:50,280 --> 00:09:52,360
more readable and more portable.
191
00:09:52,730 --> 00:09:53,730
Thank you.
17363
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.