|
An Introduction To C - Lesson 2:
Lessons:
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
[ 7 ]
[ 8 ]
[ 9 ]
Lesson Requirements
A C compiler, GCC is well worth a look and available from gcc.gnu.org
Lesson Summary
This is going to be big so you might want to do it in parts. This Lesson describes much of the "backbone" of the C language.
Types
Types define what sort of data a variable can hold; C supports several types but we will only consider two to begin with.
|
int
|
A integer number e.g. 0, 1, -4, 7, 11931 |
|
char
|
A character e.g. 'a', 'b', 'c', 'r', 'z' |
Variables
In our programs we will often want to store some form of data (information). We have two ways of doing this -- use a constant or use a variable. Constants are, by their definition, constant, meaning when we write the program we set their value and it never changes.
Typical data we would use a constant for is the number of seconds in a minute. However we might want to store data that we will want to change, such as the number of seconds the program has been running. We use variables in this case. Variables are declared as:
<type> <variable_name>;
For example:
int num_of_seconds;
This creates a variable called num_of_seconds, of type int (integer).
Variable names can be anything begining with an alphabetical character and
continuing in any combination of alphanumeric characters. You can't use a
restricted name as a variable (eg a function or keyword). A good programming
procedure is to use meaningful variable names instead of just random names. This
means it is much easier to read your code, or for other people to read it.
Loop Constructs
There are several different loops, but we shall start with the For loop to begin with. For loops are defined as:
for (start condition; boolean test;
action)
{
...
code
...
}
The start condition is a typically an assignment operation such as:
i = 1;
which sets the variable i to have the value of 1. The next statement is a
boolean condition such as:
i < 5;
A boolean value is either TRUE or FALSE. In this case the variable i can
either store a value less than 5 or not. Finally we have the action statement --
typically we use For loops to perform a piece of code a set number of times. The
action statement allows us to determine the "step size".
At this point, let's consider some of the answers you may have come up with
for Lesson 1's
Exercise. You might have come up with:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello this is my first program.\nThis is line
number 2.\nAnd this is line number 3.\n");
}
or possibly:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello this is my first program.\n");
printf("This is line number 2.\n");
printf("And this is line number 3.\n");
}
Both of these are correct answers. But let's consider that the Exercise asked
you to print 1000 line numbers. This is where loops come in handy, and here's an
example to do that:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for(i = 1; i =< 1000; i
= i + 1)
{
printf("This is line number: %d\n", i);
}
}
Ok theres some news things in this. Firstly the "%d"
has been introduced in theprintf
statement. Furtheremore, there is a comma followed by the variable i -- ",
i".
You might wonder why this variable is called "i", when previously
it was stated that variables should be named meaninfully. However, we should
note here that "i" is an acceptable variable name in this case because
it is traditionally used as a loop counter.
Also, let us note that we have now passed two parameters to the printf()
function -- the string, and the variable.
Now let's look at the While loop. As explained earlier, the While loop lets
us execute a piece of code while a boolean expression is true. For example:
while I am alive
breathe
In C, While loops typically take the form:
while (boolean_expression)
{
...
code-body
...
}
Let's go back to the 1000 line number problem, and this time use a While loop
to solve it:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
i = 1;
while(i < 1000)
{
printf("This is line number: %d\n", i);
i = i + 1;
}
}
Now let's break this new code down:
(i < 1000)
This is a boolean expression (i.e. it is either TRUE or FALSE) which evaluates the the expression "is i less than 1000?". This brings us to an interesting point -- it is very common to either repeat the loop too few
times, or worse too many times when using while loops. So be very careful with them. Can you see the deliberate error above?
Note that we are initally setting i to be 1. But the loop is only going to go up to 999 because the while statement checks if the i variable is smaller than 1000.
Conditional Statements
Let's take a break from Loops and move on to conditions. The "if" condition has this form:
if (boolean_expression) then
do-this-code
There is also an Else statement we can use:
if (boolean_expression) then
do-this-code
else
do-something-else
Note that C does not use "then" in it's language. Here is an
example:
int Max(int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
This function returns the greater of two integer values.
We've covered quite a lot in this Lesson, and don't be worried if you don't
grasp all of it (or any!). We will be revisiting Loops and Conditions in the
future. If you feel in the mood, here are some exercises to do:
Exercises
1. Write a function or program to determine the maximum of 3 different numbers.
2. Write a program to print out all the numbers from 1000 down to 1.
|