Hello, and welcome.
In this lecture, I'd like to talk about the concept
of variables and data types.
All right, so you should remember that a program needs to store the
instructions of its program and the data that it acts upon while your
computer is executing that program.
So I want to just reiterate to you the concept of memory.
We talked about this in an earlier lecture, but I'm kind of just
summarizing here why memory is important and what it is, because
it's directly related to variables.
So you should understand this concept of memory, understand
what the difference is between memory and a hard drive.
But we know that memory is used by programs.
When a program has to send instructions to the CPU, it's going
to use memory and when it also stores data, it's going to use memory and
a program will definitely do both of these in order to be useful.
This is called ram, random access memory.
Program store data and ram, and they will opt for variables and data.
They will also use hard drives.
The difference between storing data in ram versus a hard drive is when
you store it in ram, the data is going to go away when the computer is
turned off or when the program exits.
Now if you want to store persistent data, you would
store it on a hard drive.
Program still may use data on the hard drive but only usually for
external data, persistent data, data that's retrieved somewhere
all variables and all data in the program itself is stored in memory.
So memory is you can just kind of think of it as this
ordered sequence of boxes.
I was going to try to find a good illustration for this to help you.
We'll go over this in a lot more detail when we talk about
pointers because then you really, really have to understand and
visualize how memory is stored.
But if you think of memory as just it's an external hardware type and
it's just a little stick of memory that you can put in your motherboard.
And if you think of it visually, conceptually, it's
just a sequence of boxes.
Each box is full will be represented by the number 1
and when it's empty will be represented by the number zero.
And it stores binary digits.
There's zeros or 1s, which represent true or false values.
And each box is called a bit.
So bits in memory are then grouped into sets of eight, which is a byte.
So you've probably heard these terms bit and byte.
They're important when they're related to memory because it
determines the size of the data.
So each byte has been labeled with a number that represents an address.
If you have data stored somewhere in hardware, you
have to know how to access it.
And just like a street address where you live, you need to know
how to get access to that memory through some kind of label.
So all memory bits and bytes have an address.
So eight byte represents an address in memory and it
uniquely references that byte.
So that's how you can access the data and that's how you
kind of want to visualize it.
So again, memory consists of a large number of bits that are in
groups of eight called bytes and each byte has a unique address, and
your program will access that data usually through an identifier, but it
can also use hexadecimal addresses.
And when you have memory for a program, memories that use programs
and memories that are on your motherboard, it will increase the
efficiency of your programs if you have more memory and the memory
is faster, the buses are fast you can access the memory quicker.
So 16 gigs of ram, you probably heard terms like that, the higher amount
of ram means that programs can store more memory and access it quicker.
And remember, programs use memory because it's just faster.
You have to store something on a hard drive, it would
be much longer to access.
And remember, we are executing a billion instructions
per second by the CPU.
So that's a lot of data that you may need to access.
So how does this all relate?
Well, it directly relates to this concept of a variable.
And so a program is only going to be powerful if it can manipulate data.
It's going to send instructions to the computer, but it
needs to manipulate data.
So in order to understand the different data types that you
can use and how to create and name variables, you need to
understand this concept of memory.
And when you're writing a program for it to be powerful, you have to then
understand the concept of variables.
How to create them, name them and also this idea of a data type.
Constants are types of data that do not change.
They're always the same.
They retain their values throughout the life of the program.
So as long as that program is running, that data is always the same.
Variables are the exact opposite.
They're types of data that may change, and that's why it's called a variable.
So values can change throughout the lifetime of the program.
Different values for that variable.
And variables and constants are stored in memory with an address.
However, it would be really, really hard to access that
memory and memorize all the hexadecimal addresses.
So it's a lot easier to create variables that have meaningful names.
So you can give variable names that represent the memory locations
to represent the addresses.
And that way you can access them and modify that data much easier.
So for example, assume you want to store 2 values, 10 and 20, in your program.
And at a later stage, you want to use these 2 values.
What you would do with this data is you would first create
two variables with meaningful names.
Probably something like depending on what the 10 and the 20 represented,
but it might represent ages.
So you would say age 1 equals 10, age 2 equals 20.
You always want to use meaningful names.
And then you can store the value.
So you create the variables with names, then you assign data to
those variables, the 10 and the 20.
And I'm going to show you how to do that.
Then later in your program, you can access those variables by
their name to read the data.
And you can also modify them later in the program to change it
as long as it's not a constant.
So as far as naming variables, there are rules in the c programming
language for what you can name it.
In c, all variable names must begin with a letter or an underscore.
And then they can be followed by any combination of
letters, upper or lowercase, underscores or digits 0 to 9.
So for example, I could create a variable called Jason,
myFlag, i, J5x7 my underscore data, underscore another variable.
These are all variables that start with either a letter or an underscore
and then whatever's following them is fine as long as they're upper
case letters, underscores or digits.
Here are some examples of invalid variable names.
You would not be able to create variables with these names, the
compiler would give you an error.
If you said temp dollar sign value, the compiler would give
you an error because the dollar sign is not a valid character.
So you can't use the dollar sign.
My space flag that will give you an error because there's a space.
3Jason, that's an error because you start a variable with a
number, can't do that in c.
Remember, the compiler checks these rules, so they'll
tell you if you're wrong.
Int, int is actually an error too.
It looks okay because it starts with an alphanumeric character.
The problem here is it's a keyword.
All programming languages have certain words that have special
meaning that you cannot use for names.
And int is a specific data type for the c programming language, so you
cannot use that as a variable name.
So these are some examples of variable names that are not good.
I mentioned earlier that when you do create variable names, you want
to make sure they're meaningful.
We talked about comments already and the importance of comments
for self-documentation and understanding the code, meaningful
names are the exact same thing.
If you have meaningful names in your code, it can increase the
ability of the program, it's much easier to follow and it also helps
in the debug and documentation process, so use meaningful names.
And we'll be doing that throughout all the examples that we go through.
So once we name the variables and we have a variable, what about the data
that's associated with that variable.
That's referred to as a data type.
So some types of data in programs can be numbers, letters or words.
We just talked about how you can have 10 and 20 as values, and
you can also have alphanumeric or strings or characters.
So depending on that type of data needs a way to identify it so
that it can do certain things it really just affects the size of the
data in memory, how it's stored.
So a data type represents a type of the data, which you
can process using your program.
Examples of types of data I mentioned numbers, letters and words, those
are represented by specific keywords in the programming language.
So examples of keywords in c that represent data types
are ints, floats and doubles.
And we're going to talk about these specific data types and
what they mean in another lecture.
But data types are important because you have to specify the data of
the variable when you create it.
And the data types are going to correspond to the byte sizes
depending on the operating system the architecture your program.
So again, it all has to do with size and how this
stuff is stored in memory.
The program needs to know do I need to allocate 8 bytes, 4 bytes,
6 bytes and it all depends on the data that's being stored and the
way that the program knows how to do that is you specify a data type.
There's also this idea or maybe you've heard of this term
called primitive data types.
These are types that are not objects and they store all sorts of data.
In c, all we have is primitive data types because we have no objects.
So everything is a primitive data type in c.
Now if we want to actually start using variables in our program,
we first have to declare them.
Declaring a variable means that you specify the type of the variable
followed by the variable name.
So you use the data type followed by the name.
And this tells the compiler how a particular variable will be
used by the program and it will also understand how much data
to store in memory, whether it's 8 bytes, 4 bytes and so forth.
So for example, there's a reserved order keyword in c named int.
That's used to declare a data type of type integer.
And so if you wanted to create that in your program, you
first have to identify the data type the keyword which is int.
Then you'd have to have the name of your variable that's also
referred to as an identifier.
And then at the end, you'd have to have a semicolon because remember in
c all semicolons end all statements.
So the actual syntax is like type specify or data type
specify after with variable name followed by the semicolon.
You can also declare more than one variable on a single line by
using a comma or you can declare variables on separate lines.
It all depends, but there is syntax for comma separated variables.
And c requires you to specify all program variables be declared before.
They're used so declaration is basically just telling your
program this is what I have.
It's not telling you how you're using it.
It's just saying this is what I have in my program.
I have a variable named int.
I have a variable that's going to store a decimal
number, things like that.
So here's an example.
We have the type of the data which is int and then the
variable name and here's just x and then we have a semicolon.
Another example if you wanted to create multiple variables on the same
line, you can separate them by commas.
So in this example, we have 3 variables of type int named x y and z.
And this is called declaring variables.
We haven't assigned any data to the variables, we just said this
is the variable that exists and what will happen in the program
is the program will create those 3 variables in memory, but
there's no values for them yet.
You can assign a variable of value by using an equal operator, and we're
going to talk about this in more detail when we talk about operators.
But this is pretty simple, and you should understand it assigning
a variable to a value just use equals and then the actual
literal constant of the type.
So in this case, x is an integer a decimal, so we assign it 112,
and then we have the semicolon.
That's assigning a value.
In closely related to assigning a value is initializing the variable.
Initializing the variable is basically having an initial
value for that variable.
It's important to initialize your variables because if they
don't have any data any values associated with them, it could
cause problems in your program.
So you want to get in the habit of initialization.
And initialization can be done as part of the declaration,
doesn't have to be.
Basically, what happens is when you declare the variable and you say
hey this is my variable name, this is my type of variable, you can
then immediately have the equals operators and then the value of the
variable followed by a semicolon.
Like for example index equals 21.
In that case, you're declaring a variable named acts of type int
and you're also assigning it a value of 21 initializing a first
value, and that's initialization.
Another example into y equals 32, z equals 14.
We can do it on the same line, initialize 2 variables the same type,
int z equals 94 is okay to do by the compiler, but in this case, you're not
actually initializing both variables.
Only z will be initialized.
X is not initialized.
So z is going to be equal to 94, x is not going to have a value.
So that's important.
And we're going to be declaring initialized variables
all throughout our code.
So let me just go and we'll bring up a simple project in code
blocks and I can show you how you basically can declare these
variables, just like these examples.
So here we are in code blocks.
This should look familiar from our previous example programs.
We have some comments at the top from lines 1 through 6
through multi-line comments.
We have our include directive on line 8, saying we're going
to use standard library.
And then we have our entry point to the program, our
main routine on line 10.
And we're not doing anything in our program.
Now again, for a program to be interesting to do anything of
substance, you're going to want to create and use variables.
And there's all sorts of different ways.
Right now we don't really know all the data types so we can't really
create many different types of data.
We've really just had that int example, but just to reiterate
all you need to do is you have to specify the data type.
We know int so you type in int and actually with the intelligence
in this nice integrated develop environment it displays all the other
things that you can define with int.
It actually completes I.
If you were to type an f, you would see maybe other things
completed, like a flow is another data type that we'll talk about.
But again, to declare a variable, you just say the data type, a valid name.
Remember valid names start with alphanumeric characters
or underscores, can't start them with digits.
So here I could just say int Jason.
And then I have a semicolon to end the declaration.
And that's how you declare a variable.
Now if I want to initialize that variable which you should do,
during the declaration you just use the equals operator and you
specify the right type of data.
So int is a decimal, so I have to say a number.
I can't put a word in here because I didn't specify a word, I specified a
number and that would give a compiler.
But all you have to do is specify based on the type of
data and then a semicolon.
Remember, semicolons and the statements.
And that's initializing and declaring a variable on the same line.
You could also assign values to variables later in your program.
So I could just declare it and I could actually initialize it.
Let's say it's equal to 5 and then I could overwrite it in another line.
I could say Jason equals 8.
I want to change the variable because variables can change values.
Here we're assigning data using the equals operator.
And you'll notice that there's no data type on line 14.
All we have to do to access that variable is specify
the name to modify it.
We'll talk more about the equals operating when
we talk about operators.
But this is the basics for variables.
They represent data in your program, stored in memory accessed
via variables using these naming conventions and initialized and
declared in your program with data types and assigned values.
Thank you.