Professional Documents
Culture Documents
Overview
Programs
As we saw in the previous chapter, the execution of a C program always starts in the function called main .
From here it calls more and more functions, to perform all the actions it requires.
Variables
Functions in C consist of manipulating variables. These are items of data which we give a name to.
Every variable in C has an explicit type. These types are declared by ourselves or built into the language. We
can declare a new variable by writing the name of its type, followed by its name, and optionally setting it to
some value using = . This declaration is a statement, and we terminate all statements in C with a semicolon
; .
int count;
Here are some descriptions and examples of some of the built in types.
long Integer that can hold larger values long age_of_universe = 13798000000;
Function Declarations
A function is a computation that manipulates variables, and optionally changes the state of the program. It
takes as input some variables and returns some single variable as output.
To declare a function we write the type of the variable it returns, the name of the function, and then in
parenthesis a list of the variables it takes as input, separated by commas. The contents of the function are
put inside curly brackets {} , and lists all of the statements the function executes, terminated by semicolons
; . A return statement is used to let the function finish and output a variable.
For example a function that takes two int variables called x and y and adds them together could look
like this.
We call functions by writing their name and putting the arguments to the function in parentheses, separated
by commas. For example to call the above function and store the result in a variable added we would write
the following.
Structure Declarations
Structures are used to declare new types. Structures are several variables bundled together into a single
package.
We can use structure to represent more complex data types. For example to represent a point in 2D space
we could create a structure called point that packs together two float (decimal) values called x and
y . To declare structures we can use the struct keyword in conjunction with the typedef keyword. Our
declaration would look like this.
typedef struct {
float x;
float y;
} point;
We should place this definition above any functions that wish to use it. This type is no different to the built in
types, and we can use it in all the same ways. To access an individual field we use a dot . , followed by the
name of the field, such as x .
point p;
p.x = 0.1;
p.y = 10.0;
Pointers
Strings
Conditionals
Conditional statements let the program perform some code only if certain conditions are met.
To perform code under some condition we use the if statement. This is written as if followed by some
condition in parentheses, followed by the code to execute in curly brackets. An if statement can be
followed by an optional else statement, followed by other statements in curly brackets. The code in these
brackets will be performed in the case the conditional is false.
We can test for multiple conditions using the logical operators || for or, and && for and.
Inside a conditional statement's parentheses any value that is not 0 will evaluate to true. This is important
to remember as many conditions use this to check things implicitly.
If we wished to check if an int called x was greater than 10 and less than 100 , we would write the
following.
Loops
Loops allow for some code to be repeated until some condition becomes false, or some counter elapses.
There are two main loops in C. The first is a while loop. This loop repeatedly executes a block of code until
some condition becomes false. It is written as while followed by some condition in parentheses, followed
by the code to execute in curly brackets. For example a loop that counts downward from 10 to 1 could
be written as follows.
int i = 10;
while (i > 0) {
puts("Loop Iteration");
i = i - 1;
}
The second kind of loop is a for loop. Rather than a condition, this loop requires three expressions
separated by semicolons ; . These are an initialiser, a condition and an incrementer. The initialiser is
performed before the loop starts. The condition is checked before each iteration of the loop. If it is false, the
loop is exited. The incrementer is performed at the end of each iteration of the loop. These loops are often
used for counting as they are more compact than the while loop.
For example to write a loop that counts up from 0 to 9 we might write the following. In this case the ++
operator increments the variable i .
Bonus Marks
› Declare a function that outputs Hello World! n number of times. Call this from main .
› What built in types are there other than the ones listed?
› What other conditional operators are there other than greater than > , and less than < ?
› What other mathematical operators are there other than add + , and subtract - ?
Navigation
‹ Installation • Contents • An Interactive Prompt ›