- Programs work fine with immediate values,
but often times you don't know what a value is.
Say the value comes from input, the user types it,
or it's read from a file or the Internet.
To store such values you need
a specific cubby-hole or container.
In programming that container is known as a variable.
In this movie I introduce you
to the concept of the variable.
It's a container into which you can store values,
values that are unknown or values that can change.
You'll see how to declare a variable in the C language
and how a variable is used in the code.
A variable is a container for a value.
Because C deals with different types of values
a variable must be declared as a specific type
and it's given a name, a name that's used in the code.
In C the variable type matches the variable's content,
the type of value being stored.
Integer variables can hold only
integer values, or whole numbers.
Float variables hold floating-point values,
which are very large numbers, very small numbers,
or numbers with a fractional part like 2.1.
Character variables hold single values such as the letter X.
The C language lacks a string variable type.
Instead a character array is used.
I'll explain how that works in a later movie.
Int, float, and char are all C language keywords.
Additional keywords are also used to declare variables.
These include double, long, short, signed, and unsigned.
These are all C language keywords and
they're used to declare types of variables.
This code declares an integer variable at line five.
First comes the keyword int which is
used to declare an integer variable.
Next comes the variable name, age.
As this is a statement it ends with a semicolon.
Variable names can include numbers,
letters, and some symbols.
They must begin with a letter or an underline can be used.
The name must be unique, with no two variables
having the same name, nor should variables be
the same name as functions or C language keywords.
At line seven the variable age is assigned the value 30.
In C values or equations go on the right side of
the assignment operator, the equals sign.
The value or results of the equation is then
assigned to the variable on the left.
Because 30 is an integer value it fits nicely
into the integer variable age.
Finally, the statement at line nine displays
the variable's value by using the printf function.
The %d placeholder is used and the variable age
is specified as the second argument.
Build and run this code.
The whole idea behind a variable
is that its value can change.
Edit the source code so that the value of the
age variable is changed to 34.
Make this modification at line seven.
Save the modified code, build and run.
The program's output reflects the new value.
Assign a new value to variable in the code by
adding two lines after line eight.
First, assign the value 50 to the age variable, age = 50;
Second, type another printf function,
printf("This course's author is over %d years old\n", age);
Save this change, build and run.
See how the age variable is used twice
to hold two different integer values?
On your own you can change this code once more.
This time you can assign your own age to the age variable
and simply add a printf statement to say how old you are.
You can do this on your own later.
Math can also be performed using variables.
Open exercise variable2.
In line seven the value 32 is
assigned to the integer variable age.
Edit the value to reflect your own age in years.
The printf function at line eight uses two statements,
a string and an integer variable.
The string is an immediate value.
Change the name Dan to your own name,
unless your name is also Dan.
You don't need to guess how many months you have
because that value is calculated as the argument
in the printf function at line nine.
Save, build and run.
Of course the months value displayed is an approximation,
unless today is your birthday.
In that case, happy birthday.
The int is really one variable type.
Another common type is the char or character variable.
Open exercise variable3.
Here three character variables are declared, X, Y, and Z.
You can declare multiple variables
of the same type on a single line.
Each variable name must be separated by a comma.
Lines seven through nine assign the
variables characters, single quotes are used.
Then the values are displayed with
the printf function at line 11.
Build this code to run the program.
Just as you can change integer variables,
you can change character variables, you can even do math.
Edit line eight to read Y = X + 1;
and line nine to read Z = Y + 1;
Save, build and run.
The output is the same.
Instead of assigning immediate values to variables
Y and Z, you did a little character math adding one
to the value of character A gives you a B
and adding one to B gives you a C.
The final variable type I'd like to introduce is the float.
It holds very large values, very small values,
or any value with a fractional part.
Open exercise variable4.c
The float variable pi is declared at line five.
At line seven a calculation is made
and the result is assigned to variable pi.
The calculation uses floating-point values.
By adding a .0 to 22 and seven the compiler assumes
that you need floating-point values and treats them as such.
Otherwise, integers would be used
and the result would be wrong.
So when you use a floating-point whole
number remember to add the .0.
Line eight sends the result to standard output.
The %f placeholder is used to represent
floating-point values in a printf statement.
Build and run the code.
The value generated for pi is accurate down to the
hundreds place, which is okay for quite a few things
in antiquity, but not acceptable for modern calculations.