All language subtitles for 10. Casting in C Programming Language

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 Download
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
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

So for that, we'll need code blocks.

And first of all, let's define two variables of type in.

Now one in two.

So end number one and number two.

And also, let's create a third variable that will simply store the result of the division and it will

be something like each resolved.

So once again, this variable result is of type end.

And it will simply be used to store the result of the division between number one and number two.

So now we can say that result will equal to now one over.

Number two, assuming that number two is not zero.

Right.

Assuming number two is not equal to zero.

All right.

And then we will just use the printer message to print out the resolve to the screen.

Will see resolved.

Resolved.

Equals to percentage the backslash can and then will go with resolved.

But before we build and ran this program, we kind of forgot something.

We forgot to initialize these variables.

So let's put it.

Now, one equals two.

Five.

And then two equals two.

Two.

Now, if we save and then we build and run it, we can see that the results should be gone.

That the result is actually two.

Right.

We divided five by two and the result was simply too.

So that's a K.

Let's try to figure out what will happen if we simply to change these result from integer to a floating

point.

For example, it's used double.

So now if we are to use double, will simply modify these percentages, these two percentage alef,

and then we will use the result as we use it previously.

So save it.

Build and run.

There you go.

Still, too.

But now it's two with a bunch of zeroes after words.

So why is that?

Why again, do we have the result of two when we've defined it to be of type double?

We defined the result variable to be of type double.

So we expected the result to be something like 2.5.

Right.

That division between number one and number two should give 2.5.

But still we got just two.

So what happened?

Let's try to find out.

The result variable is of type double.

And it basically does contain numbers of the time we saw on the screen, two point zero zero zero is

actually a floating point representation.

And if we are to take a closer look at this line and line eight, we can see that on the left side of

this assignment operation.

There is this result and it will simply hold the value represented as a floating point number.

But what interests us here is actually the right side of this assignment, number one over number two.

And this leads us to the first rule I'm about to tell you.

So the first rule I want you guys to know is that whenever you make any mathematical operation between

two numbers of the same type.

In this case, it's integer and integer.

Right.

So whenever you do any mathematical operation between two numbers of the same type, the result you

will get.

By default, you will be actually of the same particular type.

Right.

Which side?

Previously in the slide itself.

I discussed it in just a couple of moments.

So the result of these guys or this division is going to be also an integer because both number one

and number two are of type end.

So the result is also going to be of type in.

And please notice that the result itself on the right side before, before and I emphasize of these

before it is assigned to the result itself.

There is some mathematical operation going on here.

And the result itself before it is being assigned is an integer number.

So whenever you divide five by two, it is round to the lowest number A, which is in our case, is

simply too.

And now, once the right side has been calculated, has been found, that it's still only now we take

these two as an integer and we assign it to a result.

So the result will refer to this.

S2 is two point zero zero zero.

All right.

So it will be something like that.

It was two in the first place and then it became two point zero instead of being just something like

two point five, as you would have expected.

Right.

And I hope this one is clear for you and you got the point.

So let's proceed to the following question.

How can we divide five?

How can we divide five by two and still obtain two point five as the result?

And this leads us to the second rule.

And I call this rule an implicit.

Casting.

And the reason why it's called implicit is because this casting is being done behind the scenes.

And we don't explicitly specify that we want to make type conversion.

So the second rule, the implicit casting states that whenever you make mathematical operation between

an integer and a floating point number, the result of this operation will be always treated as it would

have been done between two floating points.

Meaning that the result would be of a floating point-I.

So, for example, if we take the number of three, which is an end and added 4.5, which is a free

floating point type, then the result would be also of a floating point type, which is seven point

five.

And it's a floating point type, though.

Three is I mean, the jaw.

Right.

So in addition to any plus and minus operations, this will also work for that, a vision.

So if we change even one of these numbers, number one or number two, let's leave.

One equals two.

Five is an integer.

And we'll just use double NUM to double num.

Two equals to two point zero, which is pretty much the same.

So in this case, if we are to run these code, we will see a different result.

Let's try to build and run it.

Build and run.

Come on.

So the result is actually two point five.

And leave aside these zeroes at the end.

And that's exactly what we expected.

Which is great.

So another option would be simply to use instead of now two as a double.

It's live it as an integer.

And number two equals two.

Two.

And just modifying one to be.

To be of type.

Double.

Double.

So now Y equals two five point zero.

And if you tried to build and run it, you will see pretty much the same result.

Two point five.

And that happens because it's sufficient for us that even if one of these numbers that participate in

this division operation will be of type of some floating point type.

Then also the result before it is even assigned to this result variable, the result itself will also

be treated as a floating point.

And that's the reason why we don't lose any data in these type conversion.

All right, guys.

Good, good, good.

But suppose that you began your program by setting up both number one and number two to be of type N,

something like what we've done previously, number one equals two five and one two equals two two.

And you don't want to change their tie for some reason.

I don't know.

Maybe the boss demands that these two variables should remain of type bar.

You still need to calculate these results somehow.

So for that, we will use a technique called Castellaneta.

We know that thanks to the second rule, we said that it's enough that at least one of the participants

in the mathematical operation would have been a floating point time.

So then the whole result would have been as a floating point.

And we also know that in this case, both number one and number two are of type in nature.

So basically what we have to do is simply to take at least one of these numbers.

Number one or number two, and cast them explicitly use casting into double, for example.

And it will allow the computer to treat that mathematical operation as it is being done between two

floating points number, which is exactly what we want to use.

Casting, for example, on number two in this case.

Let's define something like this one double.

So we simply use the parentheses inside of this parentheses.

We specify the type of casting that we want to make on these num do.

So, for example, we know that name two in such a case.

Let's remove this one.

We know that num two in this example equals two two.

And if we add this explicit casting before.

All right.

These casting will be applied on NUM two and it will simply change it from two to two point zero to

a floating point of representation.

So in this case, if we save it, then build and run this program, we'll see the expected 2.5 printed

out on the screen.

Although we used now one and num two as integers.

So I hope that makes sense to you guys.

And also you.

You are not required only to use casting gone num two.

You may also use these casting on the first num num one on the right here.

All right.

And if you are to run and build it, build and run into literacy, then the result is also the same.

It's 2.5 also here.

And all of that, thanks to the.

Second rule where we've defined that it's sufficient for at least one one of the participants to be

of type.

For example, of type double, then the whole result of this operation will also be of type double.

And then we can simply assign this result to the result variable on the left of the on this assignment

operation.

And also, please note there that the type of neither number one or number two after the casting operation

will never be changed.

Both number one and number two will still remain off type end.

They were just converted or better say, casted to be.

For example, here and just name one to be of type double.

Just for this operation.

Just for this line.

So hopefully these casting makes sense to you guys.

You could basically just also use here a double on both of them, both for number one and number two.

And it will not change their result at all because it will already be treating these these separation.

These mathematical division as an operation between two floating point numbers.

So simply saying you can basically cast whatever you need.

For example, you can cast a floating point to an integer.

And this can be done by something like that.

You specify in the parentheses the type that you want to cast a given value.

So let's say Amed.

And then you specify the floating point representation 5.1 and all of these expression.

All of this.

Everything we've written here can simply be treated as just fine.

This will simply be an integer representation of these 5.1.

And also you can do the same for seven point ninety five, let's say, seven point ninety five.

So if there are these casting operation, these casting two integer representation.

We will simply stay with the just the seven.

So I hope that's clear for you guys.

And to summarize everything up, casting is a very important topic.

You must be familiar with because you will encounter with a lot in our programming career.

And you must know how to tackle problems with casting, how to use them, when to use them, and basically

whether it should be done implicitly or explicitly or whatever you like.

And one of the examples is going to be shown in the next challenge where you will have to calculate

the average grade between three given grades from the user.

Because previously we didn't.

When we solved this exercise of the average grade, we didn't specify.

We didn't talk about anything related to casting.

Right.

Because an average grade will usually will not be in integer representation, but rather it will be

a floating point number so that just now we know how to handle it and how to to take care of all of

these casting.

And of course, guys, there is much more depth to these rules.

We've just seen and many new ones says that should be taken care of.

But that will be for another course because these topics are much more advanced and require the understanding

of lower levels.

So hopefully you'll find this information valuable for you for now.

And I will, of course, share my knowledge with you in the next advanced courses that I'm planning

to create just for you.

So thank you guys for watching.

And I'll see you in the next challenge where we will solve and practice our casting skills.

I'll see you there.

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