Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Hello, and welcome to a new section.
In this section, we're going to talk all about advanced data types.
We're going to start with the define statement, which is a
preprocessor directive that really has to just do with constants.
So the defined preprocessor directive, like all other preprocessor
directives, begins with the pound symbol at the beginning of a line.
It can be preceded by spaces or tabs and it also allows for
space between the pound and the remainder of the directive.
So we've seen this before in previous preprocessor directives,
right, specifically with the include preprocessor directive.
There's another one with define.
So in c you can use this directive to define a symbolic or
manifest constants in a program.
It's all about using constants, so when you have
data that doesn't change.
The preprocessor directives run until the first new line
following the pound symbol.
So a directive is limited to one line in length.
The combination the backslash the new line is deleted before
the preprocessor begins, right.
So you can spread the directive over several physical lines if you want to.
These lines still constitute a single logical line.
So make sure you remember what a preprocessor directive is.
It's basically code that runs before the compiler.
So when it sees this preprocessor directive for define, it's
going to be running before the compiler for consonants.
It's a way of telling the compiler something.
In this case, that something is a constant.
Typically, defined statements appear first in the program
usually right at the top.
It's not required to do that. You can put them anywhere you want, but
common convention is right at the top.
Most programmers group their defined statements at the beginning of the
program or inside an include file, a lot of times they're reused.
They can be quickly referenced and shared by more than one source file.
By convention, the defined names are defined using only
uppercase letters and underscores.
So again, you typically see these common conventions.
Here is an example of how you can use the define statement specifically, for
symbolic names to program constants.
So you'd have something like define with the pound symbol, yes,
and again that's all uppercase.
It's only one word, so you don't need the underscore.
And then right after, you see the one, right.
So this is going to define the name yes and make it
equivalent to the value one.
So anytime in your program where you see yes, it's going
to interpolate that yes define and put one inside of it, right.
So it's sort of like just doing the same as doing a search and
replace with the text editor, right.
Whenever you see the name yes, it can be used subsequently
anywhere you need to have constant.
The preprocessor because it runs before the compiler is going to
replace all these occurrences of the defined with its associated text.
The defined statement has a special syntax.
There's no equal sign, right.
So you should have noticed that usually when you assign data to
variables, there's an equal sign.
But remember, this is not a variable, right.
So there's no equal sign when you have to assign one to yes.
There's also no semicolon, right.
So notice those differences.
It's special syntax.
Whenever this name appears, it's defined value of one is
automatically substituted into the program by the preprocessor.
Another example here is if you have game over equals yes since yes is
a defined statement, game over is actually going to be equal to one.
It has the same effect of assigning one to game over.
But it is a little bit more readable, and that's why one
of the reasons you use defines.
A defined name is not a variable and that's why it doesn't use the equals
operator and there's no semicolon.
You can't assign a value to it unless the result of substituting the
defined value is in fact a variable.
So let's look at some defined expressions.
Remember, expression are usually things that return information.
So definition for a name can include more than a simple
constant value, right.
It can be an expression.
And so for example, you could have a define which is named 2
pi as the product of 2 and 3.14.
So you're going to multiply these.
So all you would do is you would say define 2 pi and then right after
the space 2.0 times this number.
And then you'd be able to use this defined name Two PI
anywhere in the program where the expression is 2 times 3.14.
So instead of having to use that -- and again, it makes
it more readable, Two PI.
So you could replace the return statement of a circumference function
with the following statement.
Return two pi times r instead of having to hard code those
constants, even though 2 pi represents a constant.
You also have null.
So the defined name null is frequently used by programmers
to represent the null pointer.
We've used this in past programs.
This is a defined underneath the hood.
It's defined inside standard def.h.
So all you see if you look at that header file is define null and
it's actually 0, represents 0.
So it makes the program more readable as opposed to
putting something like 0.
You can actually say why my pointer is not equal to no as opposed to
why my pointer is not equal to 0.
So much more readable.
So to set up a while loop that will execute as long as the value of list
pointer is not equal to the null pointer, you could do that why
this pointer is not equal to null.
So what are the benefits of using this defined preprocessor?
Well, when you use this defined name for a constant value, it helps to make
the programs more readily extendable.
It also makes them more readable, as I mentioned.
You know exactly what's going on in the code if you see a name
as opposed to some constant.
So for example when you define an array, you must specify the number
of elements in the array subsequent program statements will likely
use the knowledge of the number of elements contained inside the
array to get the size and so forth.
So you're going to see it quite a bit.
So you could just say float data values 1,000, 1000 is
kind of like a magic number.
It's not very readable.
There's a good chance that when you see the statements in the
program the fact that you use this 1000 is not very readable.
You end up using it a lot for iterating through some
kind of size of the array, so you might have a for loop.
So you would use the value as an upper bound for size.
Suppose you had to increase the size of the values from one 1000
to 2000, you would then have to change all the spots in the program
that use that hard coded value.
It'd be much nicer if you had something at the top and you only
had to change it in one spot.
So what you could do is a better way of dealing with this array
bounds is to define a name and use the preprocessor directive
for the upper array bound.
So something like maximum data values and then setting that to 1000.
Now anywhere in your program, you can use maximum data values
when you declare the size of the array or inside your loop.
So if you're going through an array bound, you can just
say maximum data values.
And now if maximum data values changes, all you have to do is change
it at the top, the define statement.
So if you want to change it to 2000, you simply say define
maximum data values 2000.
So when the program is written using this preprocessor director
define, in all cases where the size of the array was used, it's
automatically going to be updated.
So that define could be the only statement in the program and you
only have to update it once as opposed to everywhere else using it.
Another nice use or another benefits of define is that the
defined statement helps make programs more portable from one
computer system to another.
So, it might be necessary to use constant values that are related
to the particular computer on which the program is running.
A lot of times we have data types and data types are different sizes
depending on if you're running on a particular architecture.
So it might have to also do with particular memory address,
file name or the number of bits contained in a computer word.
If you want to execute a program on a different machine where an int for
example is 64 bits versus 32, 4 bytes versus 8 bytes, you can basically
use the define to set this value.
So in situations when the program must be written to make use of
machine dependent values, the defined statement would be ideal.
Because it can help isolate the machine dependent values
from the program as much as possible, which makes it more
portable to another machine.
Now you may be asking yourself when should we use define versus
constant because c has a nice little keyword called const.
And so normally, if you define pi equal to 3.14159,
this is going to define pi as a symbol and the preprocessor is going to go through
all the text and replace it with that value 3.14 anywhere it sees the names pi.
Now you could have defined pi as a variable, but to tell the
compiler that its value is fixed it must not be changed, so we
could use something like const.
So you can fix the value of any variable when you declare
it by prefixing the type name with the keyword const, so we
could have done const flow pi.
This defines the value of pi as fixed, has the same effect as
using the preprocessor directive.
So the advantage of defining pi in this way is that you're now
defining as a constant numerical value with a specified type.
When you use define, pi is just a sequence of characters
that replaces all the occurrences of pi in your code.
Constant is sometimes preferred because it does
do the data type checking.
So you're going to see define preprocessor all over the place
again usually at the top of the file to represent constant.
Just remember, it has the pound symbol so it's a preprocessor
directive and it's just used to assign constants to names, makes the program
more readable and more portable.
Thank you.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.