- Now that we covered some ground
in terms of what types of variables
to use in what instance,
let's focus a little bit on integer types.
As you've seen, there are a lot of them.
One of the main things to know
about integer types,
other than there are a lot of them,
is how much memory do they take,
and what to they range.
This way, you can make a clear determination
whether one type is more appropriate or another.
For example, if all you care about
is numbers between, for example,
minus 128 to 127, then the signed char
is the most appropriate option.
However, if your range expands beyond that,
you may want to go to a full int.
But the way char is not just short for character,
it is actually an integer type.
Another example, are larger integers,
for example, the short, which is larger
than the char from the previous slide.
32 thousand on the right side
all the way down to negative minus 32 thousand.
The unsigned long, goes from zero
to over four billion, and is one
of the larger available.
If you know what type of problem you're going to solve,
if you know what type of numbers
your program is going to generate
or need to handle, then you can match
the memory requirement to the problem at hand.
This way you can optimize memory usage,
save time, and make the program run
more efficiently and faster.
Let's look at the code that will give
even more information about
we can do when we talk about variables.
This program shows you
in very simple way, the ability of C
to display the size of each variable,
using the function that's already built
in the C language called size off.
All you have to do,
is simply use the (mumbles) of the function
and give this an input,
the type you're looking for.
What the program will do
will tell you the exact amount of memory
that this variable size or data type
is going to take.
So let's run it.
Okay, so, as you saw in the previous table,
this is now possible in code.
For example, you can see the storage size
for an int, the fourth line,
is four bytes, the storage size
for a long variable, is eight bytes.
This means that your program
may run unchanged, if you use long
or int as your integers,
however, it will take twice as much memory.
This is especially true, if you have
a lot of data, you save a lot of data,
you store a lot of data,
and you process a lot of data.
In short, the wrong choice of a data type
may cause your program to run
up to eight times as fast,
if you compare the unsigned long
with a char, or, even in the case
of a short, which is two bytes,
versus a long, which is four times more memory.
So be very careful, use memory carefully,
use memory efficiently.
Older generations of programmers
always complain that they don't
have enough memory, the computers were too slow,
but guess what?
They were a little bit better
than us today, because they knew how
to program with constraint.
Today's programmers just assume that every
generation of computers that's coming,
it's going to have more memory,
they're going to be faster,
so why worry about saving and cutting corners.
But it's not the case, because as memory gets cheaper
and faster, and there's more of it,
programs get more sophisticated,
and people always want to do more.
So there's never been a situation
where you had too much resources.
On the contrary, we're still complaining
that we don't have enough memory.
If you match the memory careful,
you can mitigate the problem.
You cannot eliminate it,
but at least you can do the best you can.