Hello, and welcome.
In this demonstration, I want to provide a solution to a quick little program that
will use the sizeof operator to tell us how big
our memory is for certain data types.
Let's bring up code blocks,
and we're going to do very familiar steps here with creating a project.
And we're going to get a meaningful name.
Let's call this project
sizeofexample.
So sizeofexample.
This is going to be pretty easy, really. It's just going to be
using printout statements to display certain data types.
So I'm just going to not even create any variables. I'm just going
to print out some data.
So here I'm going to say variables of type char
occupy percent.
And I was going to use
certain um data type. I think it was zf, but
let's try using zd. This is a nice format specifier,
was added in C99, I believe.
Zd and then all we have to do is use sizeof.
Sizeof and get it looks like a function, but it's not. And then say char for character.
And then I'm just going to copy this
and paste it because I'm going to do similar things.
I'm going to not do a char, but I'm going to do a short on the second one.
I'm going to do an int and a long,
long long,
double or let's do a float, float
double, let's try long double.
And then all we have to do is modify the sizeof parameters
to take the corresponding types.
We'll be using this as well when we talk about memory and pointers.
But this helps just to understand different data types.
And you'll see that you can actually pass in space long long
sizeof. That's probably why it's not a function because you can't have spaces in function parameters.
But remember the sizeof also takes variables, expressions,
all sorts of things.
And when it does that, it actually determines the sizeof at runtime as opposed to compile time
in long double.
Save that, build.
No errors.
It looks like it doesn't understand the z format.
It's because it might not be int. So instead let's change these to percent use
for unsigned.
All right now let's build.
All right, now we have no warnings. Typically when I build, you don't want to have any warnings as well.
Warnings in the compiler
are exactly what they say they're warning. So you'll still compile successfully,
but a warning usually indicates a problem.
So you want to clean up your warnings. And so I had a warning saying that a format specifier
wasn't allowed percent z., so I changed it, now I have no warnings.
Try to clean up your warnings as much as possible because the reason they're warnings because they can cause problems.
And one thing I did forget here that I just noticed is
it's formatted incorrectly because I didn't add my escape character for bet for line feeds.
So it makes it very difficult to read. So let's add our
backslash n after our percent u.
So now it says variables of type char occupy one.
Variables of type short occupy 2 bytes,
int 4 bytes, long 4 bytes, long long 8 bytes,
float 4 bytes, double 8 bytes, long double 12.
It's pretty neat. You run this on many different platforms and you'll see those things change.
So an easy program to write, but again demonstrating some neat concepts.
So you can go ahead, you can download this.
If you have any trouble, try running it on different platforms,
but this is all I wanted to demonstrate just
a little show of the different data type sizes, so on to the next section. Thank you.