All language subtitles for 001 const.en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Hello, and welcome.

Welcome to a new section. This section is all about type qualifiers.

The first type of qualifier we're going to discuss in detail is the const type qualifier.

So type qualifiers are basically can be used in front of variables

to give the compiler more information about the intended use of the variable.

It helps with optimization. It's similar to the concept that we previously discussed,

which is storage classes but a little bit different because this has to do more with

optimization. So we're going to discuss a few of the type qualifiers important in c.

Both type qualifiers are const, which we'll do in this lecture and then volatile and restrict.

C90 added two new type qualifiers. Constancy and volatility.

So that's volatile and the constant keywords.

So C90, again, we don't talk about that standard a lot

because there's not much that went on in there.

But for this case, it did add two nice things.

So these properties are declared with the keywords const and volatile,

which create qualified types and that's again what this section is all about.

C99 added a third qualifier which is the restrict qualifier.

This is designated to facilitate compiler optimizations.

Type qualifiers are also item potent.

This was added in C99.

What this means is that you can use the same qualifier more than once in a declaration

and the superfluous ones are ignored.

Okay, and we'll see an example of that. So let's talk about const.

So the compiler allows you to associate the const qualifier

with variables whose values will not be changed by the program.

Okay, you can tell the compiler basically that this variable

is going to have the same value throughout the entire program's execution,

it's going to be constant.

If you try to assign a value to this variable

after initializing it, the compiler is going to give you an error.

It doesn't always require to do so depending on what compiler you're going to give,

but most of them will give you an error saying hey this is a constant variable

you cannot change the value after it's been initialized.

One of the motivations for the constant attribute

in the c language is that it allows the compiler to place your constant variables into read-only memory.

So it's a compiler optimization.

If you have read-only memory, you don't have to worry about writes happening to it.

So let's look at some examples of how you can create these constants and use them.

So to create a constant, here we have our codeblocks problem,

all you have to do is use that const keyword.

So we can say const and a very popular one

is pi because pi is a const. You specify the data type after the keyword const.

We're going to create a double.

We're going to say pi, and we're just going to say it's equal to

3.141592654 which is

pi in most things. This declares the const variable pi.

Again, it tells the compiler that this variable cannot be modified by the program.

So if you try to do something later in the program,

like change the value by saying pi equals pi divided by two,

you're not allowed to do that. So when you try to build it, the compiler is going to say

assignment of read-only variable. You're not allowed to do that.

The gcc compiler gives an error.

So you can use this const keyword to create an array also of data that the program cannot alter.

So say we wanted to create an array using this keyword. We could do something like

const int days

which is an array we could set the size to 12

and then we could initialize it

to the days of the month. We could say 31 28

31 30 dot dot dot.

And this will set the array to constant values.

You can now not change any elements in the array.

You want to be able to access the index and change the elements.

Now I also mentioned that these constant keywords and the type qualifiers in general

are item potent.

So that means you can add the type qualifier as many times as you want

and it doesn't affect it.

So I could say const const const

const as many times as I want and that's the exact same as saying

const double pi.

The compiler doesn't care.

So it makes it possible basically

to do different things like allowing typedef

to be specified. So we could do something like this. We could say typedef

const int zip.

And then we can say const zip q

equals eight.

And in theory that's actually doing const twice right because zip is

identified as a constant int.

It's another name for const int. So if we say const zip that's really saying constant const in.

And you'll notice if you try to compile this,

you're perfectly fine, nothing happens. You can say build

and it doesn't matter that those things are messed up. You have to spell typedef correctly.

But if you spell typedef correctly, you're good to go.

So that's what it means to be item potent.

We have some warnings because we're not actually using any of these variables.

And it will even give you some warnings for that potent stuff,

but it doesn't give you an error.

So I showed you how you can use this constant keyword on

simple variables and arrays.

You can also use the constant with a pointer.

With pointers, it's a bit more complicated because you have to distinguish between making the pointer itself constant

and making the value that is pointed to constant.

So for example, if we wanted to apply constant to a pointer

and we do something like this,

const float star pf,

so what this is doing is it's establishing that pf points to a value that must remain constant.

So the value of pf itself can be changed

it can be set to point at another constant value.

So remember the difference with pointers. Pointers have values which are addresses.

And then the address is pointing to a particular value.

What it's pointing to cannot change when you do the syntax.

You can't change the address, the value of the pointer itself, understand that distinct point.

Now if we declare a pointer like this.

If we say float star constant pt,

this is saying pt is a constant pointer.

So what this is saying is that the pointer pt itself cannot have its value changed,

so that means the address can't change. It must always point to the same address.

The pointed to value can change but not the address.

So it's really important where you place that asterisk.

If you place the asterisk before the constant, you can't change the address.

If you place the asterisk after the const keyword,

that means you can't change the value that the pointer is pointing to.

Now what if we do something like this to make it even more confusing. What if we say constant

float star constant pointer,

something like that.

That basically means that the pointer must always point to the same location

and that the value stored at the location must also not change.

So both are constant, and that's perfectly legal to do.

And then the last thing I wanted to mention about pointers and constants

is you can actually do something like this.

And this is the same as const float

star pfc.

So it doesn't matter the order

of the constant. It's either constant float or float constant.

They're both equivalent. Placing the constant after the type name and before the star

means the pointer cannot be used to change the pointer to value.

A constant anywhere to the left of the star makes the data constant.

So a constant to the right of star makes the pointer itself constant.

Understand those distinctions.

Where constants placed in relation to the star.

Now what about if we want to use the constant keyword with parameter

declarations and this is very, very common.

So we can use constant keyword

in declaring pointers that serve as formal function parameters.

The formal function parameters are the ones in the function prototype.

So for example say you have a function called display that displays the contents of an array.

In order to use it, you would pass the name of the array as an actual argument,

but the name of the array is an address

and that would enable the function altered data in the calling function,

you would be able to alter that array. So if you didn't want to alter that array,

what you would do is for that display function, you would say void display

and then you would say const int array

and then maybe another parameter is the limit.

You don't want the array to change.

So this parameter declaration constant array

right here is the same as const int star array.

So the declaration is basically saying that the data to which the array points

cannot be changed.

And this is the practice that ANSI c library follows,

so ANSI is the standard. So if a pointer is used only

to give a function access to values,

the pointer is declared as a pointer to a constant qualified type.

If the pointer is used to alter data in the calling function,

the constant keyword is not used.

So for example if you have something like this char,

it returns a char strcat, this is part of the ANSI library,

it may say something like restrict.

This is the actual definition. And then it has a constant for the second one,

constant charstar restrict

s2 and we haven't talked about restrict yet but we will.

That's the actual function prototype for strcat.

Remember, this function will add a copy of the second string to the end of the first string,

s2 to s1.

It will modify the first string and that's why it's not a constant.

But the second string is just read only.

The second string doesn't change s2.

And so that's why the declaration for the second parameter is constant.

We can also use constant with global data.

So remember global data is just a variable that's defined at the top

of a file. And global data is very risky. It really shouldn't be used because of maintenance

and you sometimes will alter pro program data when you don't mean to.

But however, when you have a global variable and it's a constant

that risk goes away quite a bit.

So it's perfectly reasonable to use global variables with the const qualifier.

And you should actually try to do it if it's read only

because this makes the global data uses definitely less risky.

If you're using global data across files, then you have to be very, very careful.

So if you use the external keyword, you have to be very careful with constant.

So one approach is to use defining declarations in one file

and reference declarations using the external keyword and the others, we know how to do that.

So for example, we could have one file here, we could have our

main.c file here and we could have some constants.

So say we have some global variables, constant double pi

and we have an array of constants.

We'll say we have another file. Let's create another file, and this is called other.c.

If we want to use the global constants elsewhere, we have to do something like this,

extern const double pi. We have make sure that we refer to as constants.

You have to follow that practice. In the first file, they're constants.

In the second file, they have to be defined as extern constant.

Now if you don't want to do this and sharing by files, you can actually put them in include file.

So we could have a header file here.

So here we have header.h.

And we can actually put all of our constants in here which would be a better approach.

So here we have static double,

static constant doubles pi and months.

We put them in the header, but we save them. And then if we want to use them in the main,

all we have to do is say include constant.h.

We know how to include header files. And so that's a better approach.

Okay, and in the header, you'll notice that we use the keyword static.

If you do not use the keyword static in the header file,

in file one and file two,

you'd have that would result in each file having a defining declaration of the same identifier.

So by making these static external, you actually give each file

a separate copy of the data.

So this would not work if the files are supposed to use the data to communicate with another.

So because the data is constant by using this constant keyword obviously,

and identical by having both files include the same header file, it's not a problem.

So the advantage of using this header file approach is that you do not have to remember

to use defining declarations in one file and the extern in the other file.

All the files simply include the same header file.

The disadvantage is that you do have some duplication of data.

And what you should have noticed throughout this lecture is const seems very, very familiar to define.

Remember, we talked about the defined preprocessor command,

they seem like they're very similar.

And I kind of mentioned earlier how they're a little bit different,

but let's take a look at how they are different.

So the relationship between these two

defined as a preprocessor directive constant is a keyword. We know that right off the bat.

Constant variables are actually variables like any other normal variable.

We can pass them around, we can typecast them

and any other thing that can be done with a normal variable we can do.

Define is not scope controlled, whereas constant is scope controlled.

So what that means is define can be used anywhere in the program

or in other files just by including that related header file.

A constant can be declared inside a function and it can only be used inside that function.

So there is scope related to constant.

Another advantage of using constant over define

is that the constant variable provides type checking by the compiler.

But because define is a preprocessor directive,

it happens before the compiler even gets invoked, so there's no type checking.

Also in the past, we have discussed situations when define cannot be replaced by constant.

I gave some examples in our define lecture where

define does not work in certain situations,

you have to use constant.

So here was a lecture on the advantages and the use of constant

with pointers and regular variables and so forth. Thank you.

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