Last time I introduce types like int char and double and before you get into explaining more about types
I did something in the last lecture I did this really 10 and three this is what's called the comment
and you'll see a comment above here as well.
The name of my seat BP file variables the anti-common is ignored by the compiler.
It's really just a way to mark something for programmers so I thought.
Just explain that.
So no this gets compiled this is just for us.
So in this lecture I want to talk about more types and a little bit of why we have types and how their
sizes.
So I'm sure you've heard of binary before the ones and zeros and a bit you may have heard before as
well.
And that contains either the value 1 or 0 which means it's base to number.
So our typical number system is base 10 which goes from 0 to 0 to 9 right.
In this in binary is either 0 or what.
So if we had a base 10 numbers say 123 and we'd understand the base number like this one
one times 10 to that is power power to was
you times 10 to the power of one plus three
times.
Ten to the power of zero.
So meaning this is the ones so tenth power of zero just ones of time series three.
And then this would be the tens because it's one to the power one doesn't change change the tune at
all.
So just be two times 10 or 20 and then 10 to the power of two which would be 100 times one is hundred.
So all that added together is 123.
So but if we had a 4 bit number what's so one bit is either there or one.
So but if we had a forbit number say like this what would that be.
What is the Momi the base 10 representation of that number.
So this is not too difficult to do.
So you'd say 1 times 2 to the power of three right.
Right.
This because this would be a power of zero to the power one due to the power of two and two to the power
of three races in the very last one or the very low left most one plus zero times.
Tines to the power of two was zero times two power.
One was one times two power zero.
Right.
And all of that is equal to let's say eight Ciro.
Right.
Zero us one and just nine this number 1 0 1 in binary is really just our nine.
And this is an easy easy way to determine what is or how to actually make a
translate let's say a binary number to a base 10 or so all of this is really just to say that
if you want a well a byte that you may have heard of as well like a megabyte or kilobyte is really 8
bits so two.
So this is four bits and really we want a byte is 8 bits
and all our types are actually measured in bytes.
So how how big our types are is usually measured in bytes.
So the size I said before completely system dependent actually and we can actually figure out what those
sizes are if we use what's called the size of operator.
So let's find out what the sizes of the types that we use last time are.
So let's say see ouch and do the int size of an inch is and then we just say size of it.
And we'll see.
All our other types as well the size char is
ch
you look at double as well.
So if we run this
so we'll see.
So like I said they're all measured in bytes so the size of an int is 4 bytes.
So that's 32 bit and the size of a char interestingly is just one byte and float is the same as in it
it's 4 bytes and double is double a float.
So it's eight bytes.
So because of their sizes types only have a certain value range they can be.
So for example in in
an integer which can have the values are in the positive zero or negative.
Right.
It can only have the values of negative to shew the power
of 31 and it's 31 because 4 bytes is 32 bits.
But since we have negative values it has to be divided by two which would knock off.
So it should be 32 bits but since we have to divide what two we have to knock off one.
So it's only 30 power 31.
So it's you can go negative 2 to the power of 31 to 2 to the power of 31 minus 1 and it's minus 1 because
of zero.
You have to count zero as well.
So it's always minus 1.
So this is the main reason why we have different types or one of the main reasons.
Because they are different sizes in the computer's memory and we can change the value ranges by adding
what's called an unsigned
unsigned keyword say unsigned int x is zero right.
So unsigned means it can't be negative and must be positive.
And unsigned also doesn't change the size of the type at all.
It will only change the value range thus it will have the range from zero to two to power 30 to rate
as it's 32 bits or bytes minus one because of zero.
So this is now 30 to 32 instead of power 31.
It was up here because we're completely eliminating all negative numbers
so this is not the only reason why we have types in the language though.
And note that int and float are the same size.
But let's try.
Let's try this let's get rid of unsigned So let's say we have an X or it's called something better.
Now and float float in footwell won't give it 3.20 for a number and let's say we say interval equals
to fluvial
and then let's print out each value.
Now is
this not tell us.
CH So see how the info is 3 right.
It's not 3.1 4 this is because of how the computer will interpret the integers.
Integers have to be whole number.
But if we did something like bow equals six and then we kind of reverse this.
Now here like that.
And then we ran it.
Well some compilers actually say 6.00 actually are actually no sorry since this actually my fault book
is only a fluke.
Sorry.
So it classics.
But some some will actually output six point 0.
Instead Not just six.
So it actually interprets completely differently.
So
remember that even if they're the same size different types are interpreted differently.
The compiler often gives you some kind of warning or maybe even error sometimes if you just start assigning
types.
So here here we're assigning this into vowed to be the float for example.
So if you just start assigning types without being careful and thinking about doing might end up with
something you didn't want to happen.
So in the next lecture we'll start talking about arithmetic operators operations.