Hello, and welcome.
In this lecture, I want to describe some of the data types that I
mentioned in our last lecture.
Data types are important because you have to provide this
when you declare a variable.
And they tell the actual compiler how much memory you want to create.
So it's specific to size as well.
So the compiler needs this information, but let's talk
about the ones that are supported by the c programming language.
Up until this point, we know that c has many different types of variables
that have different types of data.
We know that from creating and initializing variables.
You can have types to store integers, numbers essentially.
You can have types that store non-integer numeric varies,
things like um scientific notation or things like Booleans
which is true and false values.
And then you also have data that needs to be stored such
as characters, either a single character or many characters,
which is referred to as a string.
But we know that we have these different types of data.
Again, for a c program to be powerful, it's going to need to manipulate data
and it's going to need to manipulate all different types of data.
So some of the examples that c supports the basic data types.
There are many data types in c, but the basic ones that
we're going to talk about today in this lecture are the int.
And remember, these data types are special keywords.
So when you say int in a c program, the compiler is going to know
that oh this is a data type.
So we want to talk about int, float, double, char or
care and underscore bool.
These are the main ones that you're going to use and then
there's different variations off of those depending again
on the type of data you want to store and how big the data is.
So the difference between these types is the amount of memory.
That's all the data types in specifying the data types is about.
It's about the memory that needs to be allocated, and it varies depending
on what type of architecture you have.
But the memory and the amount is going to determine the range of
the values that can be stored.
So the amount of storage that is allocated to store particular type
of data is important to the compiler.
It all depends on the computer you're running.
And this is referred to as machine dependent.
So if I want to see program on a Linux system with different
hardware versus a windows system with certain hardware, an integer
may be 4 bytes in memory or maybe 8 bytes, again it depends.
So it's dependent on the machine you're running at how much memory
is allocated depending on the data that you want to store.
So you know an integer might take up 32 bits which is 4
bytes or it might take up 8.
So first let's talk about the int.
This is one of the most widely used data types.
It's a data type that can store integral values, values that do not
contain decimal places, whole numbers.
This is when you would identify this data type when you create
a variable int to stored number.
You can also put a minus sign preceding the data type, invert
in the variable and that indicates that the value is negative.
So if you have a minus sign proceeding the actual literal.
So what I'm talking about here is when you create the variable and
assign it data, if you put a negative number a value it's a negative number.
So ints can store negative numbers is basically what this is saying.
It's also a signed integer.
We'll talk more about what that means, but a sign integer just means that it
can be either positive, negative or 0.
And that's why you can put a minus in front of it.
If an integer is preceded by a 0 in the letter x.
So here we're talking about constants.
Here we're talking about when you assign data to an int.
I'm trying to describe to you the type of data that can be stored in here.
And so when you assign data, we know how to assign data to
variables because we learned that.
Earlier, we just define the data type and then we assign
it using the equals operator.
Now you can assign values to ints.
You can either assign numbers which is the most intuitive
and easy way to do it, like 15.
But you can also assign hexadecimal numbers.
The computer in the programming language allows that.
If you want to assign a hexadecimal number to a variable that stores an
integer, you just put the 0 and the x before, and that tells the compiler
that this is a hexadecimal number.
Hexadecimal numbers is base 16 so you can store letters a through f.
So hexadecimal looks like 0xFEFOD.
So you may see this occasionally in your program where somebody is
assigning a hexadecimal number, and you're probably thinking
your cell phone int only stirs a decimal or a whole number.
But it can also store different representations.
And so this is one with 0x preceding it.
The values 158 negative 10 and 0 are all valid examples of constants that
you can assign to integer variables.
There's no embedded spaces are permitted between the digits.
And if you try to assign a value larger than 999, you can't use commas.
So don't try to assign a value to a variable named x with
12, 000, you have to do 12000.
Float, another really important data type, one of the basic data types.
This is a variable that can store data for floating point
numbers, which means numbers that contain decimal places.
So values like 3.
125.8 and negative.001 are all valid examples of data that can be
assigned to variables of type float.
There are also floating point constants in c.
This can be expressed in scientific notation.
So for example, if you have 1.7e4, that's a floating point value and
it's expressed in the scientific notation and represents the
value 1.7 x 10 to the power of 4.
So that's 17 times 10000 which would be 170000.
A Double, very, very similar to the float.
It stores decimal numbers, decimal places.
The only difference is it's larger so you can store larger numbers.
So that would mean that it collects more data in memory.
So you want to use a double whenever the float is not
sufficient, it's too small.
So float I think stores like say say a float stores depending
on your architecture say a float stores eight bytes.
That means it can store values from like negative
32000 to a positive 32000.
If you try to represent that same data and memory using a double, you
can then store even higher numbers.
So you want to use basically twice as many significant digits
going long as long millions and millions and trillions whatever
much bigger than a float.
So most computers represent a double value using 64 bits, which is 8 bytes.
Floats are usually 4, doubles or a I might spoke there.
And that just means you can store more data.
And the way that you would create the variable is you would
say double keyword before the before naming the variable.
All floating point constants are taken as double values by the c compiler.
So that's by default.
If you specifically want to make something a float, you just add an f2
at the end of it when you assign it.
So to explicitly express a float constant, just put that f there 12.f.
Underscore bool.
This is a data type that can be used to store just the values 0 and 1.
You often hear this term Boolean.
Boolean expression in bool, it's all means true or false.
It's a yes or no 0 or 1.
So it's indicated for binary choices essentially.
And we're going to talk a lot about Booleans and Boolean expressions when
we talk about decision statements.
Boolean variables are used in programs that need indicate
a Boolean condition, a true or false type of answer.
So for example, a variable this type might be used to indicate
whether all the data has been read from a file, yes or no.
Has all the data been read from a file?
If it's 0, false, 1 yes, which we'll talk about now.
So 0 is used to indicate a false value, 1 indicates a true value.
So even though it's true or false values, the way that
the data is stored in memory is 0 or 1 as a numeric.
But their numerics mean that 0 means false, 1 equals true.
So if you were to print out Booleans, you would see values 0 and 1
printed out, not true and false.
But they mean true or false.
They only can have 2 values.
So here's an example with some data.
Let's go ahead and put this in code blocks and see how we how we do it.
So here again we have our basic program, and we have our main routine.
We know how to declare variables, where we have
to provide the data type.
Now we know how to declare more than one before we only declared an int.
Now we can actually declare something like a float.
You just use the float keyword, and your intellisense will help you.
And then you follow the naming conventions let's just say again Jason
and then we can assign it a value.
Floats consist of decimal places.
So we could say 23.22, and that contains a float and we assign it.
So we're initializing and creating a variable of type float that
contains value 23.33, and you notice the semicolon at the end there.
We can do the same for double.
We have to have come up with another name.
We could say Jason2.
We want to have meaningful names though we know that here it
would just be a larger number.
So it'd be like so many.
And we could also express this with scientific notation if we wanted.
We could put an e here plus 11.
You know but that big large number would not be able to be
stored in the float and that's why we would use a double.
We can create a bool.
And here just bool variable usually you want a camel case.
And then here, we could just say true even though it doesn't
necessarily contain that when you print it out or we could set it to 1.
And then you always want to compile and run your command.
The bool type also usually has been alias to other names, so
you don't always have to just use underscore bool, depends
on the compiler you're using.
This actually compiled fine.
It just says I'm using variables which gives warnings.
Remember, c has different standards.
The C89 standard means you have to use underscore bool like
we've learned, underscoring bull with a capital b and so forth.
With C99 standard, it allows you to use the true false
values in the bool data type.
So you don't have to use underscore bool and then create
a variable my Boolean and then set it equal to 1 or 0.
1 true, 0 false.
You don't have to do that with C99.
Underscore bool is only in the C89 standard.
So if you're using a C99 compiler, you can actually use the bool data type.
All you have to do is you have to say include and then you can
say include standard bool.h.
This is going to have a macro that allows you to use different data
type, the traditional bool data type.
So when you include standard bool.h, you can now say instead
of underscore bool, you can just say bool and it's going to
recognize that as a data type.
Instead of saying 1 or 0, you can say true or you can say bool, another
Boolean and you can say false.
So if you don't like using underscore bool for your data type, which,
again, is not what most languages use, they usually just use something
like bool or Boolean include standard bool.h and specify bool and then
you can use the true and false keywords in the bool data type.
So other data types, you may ask yourself the question well, hey,
why do I need more data types.
I pretty much talked about numbers, why do i need to represent
numbers in different ways.
So we still haven't talked about characters, which we're going
to talk about, and there's some other new other data types.
But as far as numerics goes, we've talked about numerics
being decimals or non-decimals int doubles and floats.
And you would say that probably is enough.
And typically the int is enough.
All you need is an ant.
You'll use an in quite a bit.
But c is a flexible language.
And as one of its advantages, there are many other data types where you
can represent other integer types.
And it gives some flexibility to the programmer.
Depending on the use case, depending on the data, it allows
it to be even more precise.
This is one of the ways that c can become even more efficient.
If you're if you have something very, very specific.
You can specify a specific data type as part as part as an
integral or an integer and it will make the program run quicker.
So it gives the programmer options, flexibility with
additional data types.
Integer types can vary in ranges an and whether
negative numbers can be used.
So some of the data types I'm going to talk about in terms of integers,
but these also apply to floats and doubles are some keywords that
you can assign to the int already, but it's not always required.
So you can basically have more specified versions of ints,
more specified versions of floats or doubles or you can use
these keywords by themselves.
So for example, you could say short int, that's a data type.
And I'm going to tell you what that means or long int or unsigned int
or short double or long double.
These are all additional data types with these 3 adjectives.
But you also don't need to use the int, float or doubles with them.
You could just say short or you could just say long or
just -- and that will provide specific data that you can store.
But this is all about being more precise and making
the program more efficient.
Again, you'll probably just use int, double and float for the most part.
But when you start getting one more advanced c, you're going to start
using short, long and unsigned along with the int, double float.
So for example type short int or just short by itself, like
I said you could use both.
It uses less storage than an int, so it can save space.
So if you're writing a program that needs a lot of memory and you need the
program to be efficient and not use a lot of memory, you can create shorts.
So allows that flexibility, it makes your code more efficient if you
use a short as opposed to an int.
You don't have to use it short, but it will use less memory.
And the same goes for longs.
They use more memory and it's actually larger data.
So a long end is going to use more storage than an int, allowing you to
store larger integer values or you say long float or you can just say long
by itself and that will be a larger integer value that you can store.
You might ask you ask yourself why not use a double because a
double remember has decimal spots.
So you may want to store an in that just a really
large int, you can use long.
So a type long long int another type or long long may
use more storage than a long.
And again, it all depends on the computer platform you're
using in the architecture.
You can assign values, use all these different types.
A lot of them have a different letters that you can append.
So for example, here if you're assigning a long long constant
value, you just put the capital l or lowercase l at the end of it and that
will assign that value as a long long.
Same goes if you're doing some of the shorts.
You can also apply suffixes at the end to make sure that
the compiler explicitly knows what you're dealing with.
So type specifiers can also be applied to doubles.
As I mentioned, these shorts and longs you can say long double.
And here, we create a long double called us deficit 2017.
You can for the long doubles.
You can use the capital L, as I mentioned or lowercase l same
way you would with the ints.
As far as the other data types besides short and long that we
talked about concerning unsigned, you can set the prefix unsigned
to int, doubles or floats, and this is used for only variables
that have non-negative values.
So remember that an int can store no-negative
numbers, you just say minus.
If you don't want int to store a negative number, you can
declare it as an unsigned int or just unsigned, and that will
make sure that all everything assigned to that is positive.
Now that's how it works in theory.
Some compilers don't enforce it.
But if you were to create an unsigned int and you tried to assign a negative
number, the compiler should complain.
So unsigned encounter for example.
And again, this allows the actual the energy variable to be extended
to only allow positive values.
So it's making your program more efficient by using all of these
different data types, these more specified data types for these.
Also there's the signed data type that can be applied to
ints, floats and doubles or shorts signed, short signed int.
It can be used with any of the types to make it more explicit the intent.
Signed shorts short int, shorts, they're all the same.
Signed means that it can be negative and positive.
So these are just the basic data types that I wanted to talk about.
These are the ones you're going to use most the time along with
a couple more that I'm talking about in the next lecture.
You usually don't have to get into using shorts and unsigns
and longs unless you are more advance and you're trying to
make your program more efficient.
But all these data types, they're provided right before the variable
name when you declare the variable.
And then when you assign data to these variables of these different types,
you can use suffixes like capital l or lowercase l to have explicit intent.
Thank you.