declare

C Programming Tutorial 21 - Int, Float, and Double Data Types



Sharing buttons:

welcome back everyone to caleb the video

make it to this video we are going to be

talking about numeric data types now I

know we've talked about numeric data

types a little bit in fact I know I've

already taught you about int and also

double the other one that I wanted to

talk about is float if you haven't heard

of float it's very similar to double

with just a little bit of a difference

there are some other numeric data types

but they're basically just variations on

these three data types so it's very

important you get these three down and

then you can kind of just modify them a

little bit to figure out all the other

data types the very first one int it

stands for

integer and an integer is a whole number

so for example seven up seven six seven

eight nine ten those are all whole

numbers even like negative numbers

negative four negative three zero all of

those now the next to these you could

classify as floating-point numbers it

seems kind of odd because you see it has

this word float in it and it makes you

think it only applies to the float

datatype but actually both the double

and the float data types are considered

floating-point essentially what a

floating-point number is is anything

with a fractional part so for example

five point four six point eight three

point zero so this video is going to try

to familiarize you with all three of

these and also talk about some tricks

and tips and all that good stuff so what

I'm going to do is I'm going to open a

file we'll just create a new one we'll

name it

numeric data types dot C so we're in our

file and the three data types again just

so you have them here for reference are

int double and float so let's say you

wanted to store in a variable the number

of dogs you own which data type are you

going to want to use well you're

probably going to want to use int that's

because I highly doubt you have a

fractional part of a dog as a pet if so

that's that's is messed up so you could

do something like this int dogs

equals six so that one's pretty easy

when I

you want to have whole numbers or things

that can't be divided you will want to

use an int but when do you use double or

float they're pretty similar so it's

kind of hard to tell the difference well

if you're not sure err on the side of

using double double is considered the

more standard more accepted better

datatype of the two now there are

certain situations when to use float I

think it would be more clear if I

actually explained the difference let's

make a section for a floating point the

difference has to do with precision

think of precision is how much data the

thing can hold so if you have a value

nine nine nine nine nine point nine nine

nine nine nine nine nine this thing is

super precise then again if you have a

value like four I mean that's that's not

precise at all is this is four so the

difference between the two is that

double has double the precision that

means if you need to store a lot of

information you're probably going to

want to use the double data type let's

clear this junk out and then I will go

through an example showing you the

difference very clearly let's say we

want to print some values we need to

know the conversion characters for all

of these data types well the first is

percent I for an integer and we want

these in a string and then the second is

a percent F for floating point values

there's a little tip here I'm going to

talk about this more later but if you

pass in a float to this it becomes

double the function will actually

convert the float value to a double that

is another example of why we want to use

doubles more often is because they are

more standard there is a time when you

would want to use a float though and

that is if you're very very very low on

memory

that's because doubles take up twice as

much space so if you're on a very very

small device with very limited memory

you might want to use floats but for

basically every other computer

application you're going to want to use

doubles the fact that this becomes a

double also shows us why there's only

one conversion character no matter

whether we pass in a float or double

it's going to be outputted exactly

the same so we only need % F we don't

need % F for doubles and then % F for

floats it's the same so let's get rid of

some of these comments and now I'm going

to show you where floating point values

have their weaknesses so let's put a

comma here and let's pass in some values

I'm actually going to pass in a flow and

a double so let's add another % F and a

new line will pass in 1 1 point 1 1 1 1

and then we'll pass in 1 point 1 1 1 1

with a capital F which is to say hey

this is specifically a float let's save

this and run it and see the output

alright you can see that the output for

the integer is what we expected 1 and

the flow and the double output exactly

the same in this situation whether you

use float or double doesn't really

matter the output is exactly the same

but there are times when there are

differences for example let's go back

into our code and let's change the

numbers we're printing out let's change

this to a thousand one hundred and

eleven point one one one one oh now you

see some differences here the integers

just like we expected the double is what

we wanted but the flow is not what we

wanted this problem has to do with how

numbers are stored on computers they are

stored in base 2 essentially they are

stored in binary and in binary certain

numbers cannot be represented exactly

this problem of being in base 2 with the

combination of using a float variable

gives us a number that is not exactly

what we are hoping for if we kept adding

data to our numbers eventually the

double variable is going to mess up too

so the moral of the story is that you

can't trust floating-point numbers for

everything integers are a little bit

more trustworthy if you put in 1 you

should expect one to come out that's all

I got to say in this video guys and as

always my last minute words on whether

to use float or double is if you're

unsure prepare for trouble

make it double and that's all I got for

you guys see you in the next video

you