C programming notes for the students, Lecture notes of C programming

A full theory of C programming

Typology: Lecture notes

2018/2019

Uploaded on 02/25/2019

gubabeso
gubabeso 🇮🇳

4.5

(2)

2 documents

1 / 94

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to
C Programming
Rob Miles
Department of Computer Science
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e

Partial preview of the text

Download C programming notes for the students and more Lecture notes C programming in PDF only on Docsity!

Introduction to

C Programming

Rob Miles

Department of Computer Science

i

This document is © Rob Miles 2001 Department of Computer Science, The University of Hull All rights reserved. No reproduction, copy or transmission of this publication may be made without written

  • Computers Contents
    • An Introduction to Computers........................................................................................
      • Hardware and Software ....................................................................................
      • Data and Information .......................................................................................
      • Data Processing................................................................................................
  • Programming Languages
    • What is Programming? ..................................................................................................
      • From Problem to Program ................................................................................
    • Programming Languages ...............................................................................................
  • C
    • A look at C ....................................................................................................................
      • Making C Run..................................................................................................
      • Creating C Programs........................................................................................
      • What Comprises a C Program?.......................................................................
      • The Advantages of C ......................................................................................
      • The Disadvantages of C..................................................................................
  • A First C Program
    • The Program Example .................................................................................................
      • #include ........................................................................................................
      • <stdio.h>.......................................................................................................
      • void ...............................................................................................................
      • main ..............................................................................................................
      • ( void )...........................................................................................................
      • { .....................................................................................................................
      • float ...............................................................................................................
      • height, width, area, wood_length................................................................
      • ; .....................................................................................................................
      • scanf .............................................................................................................
      • ( .....................................................................................................................
      • "%f", ..............................................................................................................
      • &height..........................................................................................................
      • ) ; ...................................................................................................................
      • scanf ( "%f", &width ) ;.................................................................................
      • area = 2 * height * width ;............................................................................
      • wood_length = 2 * ( height + width ) * 3.25 ;.................................................
      • printf..............................................................................................................
      • ( "The area of glass needed is : %f metres.\n",.........................................
      • area ) ; ..........................................................................................................
      • printf ( "The length of wood needed is : %f feet.\n", wood_length ) ; ......
      • } .....................................................................................................................
      • Punctuation ....................................................................................................
  • Variables - Variables and Data ......................................................................................... ii
    • Types of Variables .......................................................................................................
    • Declaration ..................................................................................................................
      • int variables....................................................................................................
      • float variables ................................................................................................
      • char variables ................................................................................................
      • Missing Types ................................................................................................
    • Variable Declaration ....................................................................................................
    • Giving Values to Variables ..........................................................................................
      • Expressions ....................................................................................................
      • Types of Data in Expressions..........................................................................
    • Getting Values into the Program ..................................................................................
  • Writing a Program - Comments ......................................................................................................
    • Program Flow ..............................................................................................................
      • Conditional Execution - if...............................................................................
      • Conditions and Relational Operators ..............................................................
      • Combining Logical Operators.........................................................................
      • Lumping Code Together.................................................................................
      • Magic Numbers and #define..........................................................................
      • Loops .............................................................................................................
      • Breaking Out of Loops ...................................................................................
      • Going Back to the Top of a Loop ....................................................................
      • More Complicated Decisions ..........................................................................
      • Complete Glazing Program ............................................................................
      • Operator Shorthand........................................................................................
      • Statements and Values....................................................................................
      • Neater Printing...............................................................................................
  • Functions
    • Functions So Far ..........................................................................................................
      • Function Heading ...........................................................................................
      • Function Body ................................................................................................
      • return .............................................................................................................
      • Calling a Function..........................................................................................
      • Scope .............................................................................................................
      • Variables Local to Blocks ...............................................................................
      • Full Functions Example..................................................................................
    • Pointers........................................................................................................................
      • NULL Pointers ...............................................................................................
      • Pointers and Functions ...................................................................................
    • Static Variables............................................................................................................
  • Arrays
    • Why We Need Arrays ..................................................................................................
      • Sorting ...........................................................................................................
    • Array Types and Sizes..................................................................................................
      • More Than One Dimension ............................................................................
  • Switching
    • Making Multiple Decisions ..........................................................................................
  • Strings
    • How long is a piece of string?....................................................................................... iii
    • Putting Values into Strings ..........................................................................................
    • Using Strings ...............................................................................................................
    • The String Library .......................................................................................................
      • strcpy .............................................................................................................
      • strcmp ............................................................................................................
      • strlen..............................................................................................................
    • Reading and Printing Strings .......................................................................................
    • Bomb Proof Input.........................................................................................................
  • Structures
    • What is a Structure?.....................................................................................................
    • How Structures Work...................................................................................................
    • Pointers to structures....................................................................................................
    • Defining your own Types .............................................................................................
  • Files
    • When do we use Files? .................................................................................................
      • Streams and Files ...........................................................................................
    • fopen and fclose ...........................................................................................................
      • Mode String ...................................................................................................
    • File Functions ..............................................................................................................
      • fread and fwrite ..............................................................................................
    • The End of the File and Errors.....................................................................................
  • Memory
    • Fetching Memory.........................................................................................................
      • malloc ............................................................................................................
      • free.................................................................................................................
    • The heap......................................................................................................................
  • C and Large Programs
    • Building Large Programs in C .....................................................................................
      • The Compile and Link Process .......................................................................
      • Referring to External Items ............................................................................
      • The Make Program.........................................................................................
      • Projects ..........................................................................................................
    • The C Pre-Processor.....................................................................................................
      • The #include Directive ...................................................................................
      • Conditional Compilation ................................................................................
    • A Sample Project .........................................................................................................
      • The Problem...................................................................................................
      • The Data Structure .........................................................................................
      • Program Files.................................................................................................
      • The Most Important Bit!.................................................................................
  • Glossary of Terms
  • Index

iv

The author can be contacted at: The Department of Computer Science, The University of Hull, HULL, HU6 7RX [email protected]

Computers? 2

existence and it is comparatively easy to change. Software is the voice which says "Computer Running" in a Star Trek film.

We are going to use an operating system called MS- DOS. Later we will be using UNIX.

All computers are sold with some software. Without it they would just be a novel and highly expensive heating system. The software which comes with a computer is often called its Operating System. The Operating System makes the machine usable. It looks after all the information held on the computer and provides lots of commands to allow you to manage things. It also lets you run programs, ones you have written and ones from other people. You will have to learn to talk to an operating system so that you can create your C programs and get them to go.

Data and Information

People use the words data and information interchangeably. They seem to think that one means the other. I regard data and information as two different things: Data is the collection of ons and offs which computers store and manipulate. Information is the interpretation of the data by people to mean something. Strictly speaking computers process data, humans work on information. An example, the computer holds the bit pattern: 11111111 11111111 11111111 00000000 However you could regard this as meaning: "you are 256 pounds overdrawn at the bank" or "you are 256 feet below the surface of the ground" or "eight of the thirty two light switches are off" The transition from data to information is usually made when the human reads the output. So why am I being so pedantic? Because it is vital to remember that a computer does not "know" what the data it is processing actually means. As far as it is concerned data is just patterns of bits, it is you who gives meaning to these patterns. Remember this when you get a bank statement which says that you have £8,388,608!

Data Processing

Computers are data processors. Information is fed into them, they do something with it, and then generate further information. A computer program tells the computer what to do with the information coming in. A computer works on data in the same way that a sausage machine works on meat, something is put in one end, some processing is performed, and something comes out of the other end:

Data Computer Data

This makes a computer a very good "mistake amplifier", as well as a useful thing to blame.....

A program is unaware of the data it is processing in the same way that a sausage machine is unaware of what meat is. Put a bicycle into a sausage machine and it will try to make sausages out of it. Put duff data into a computer and it will do equally useless things. It is only us people who actually ascribe meaning to data (see above), as far a the computer is concerned it is just stuff coming in which has to be manipulated in some way.

Computers? 3

A computer program is just a sequence of instructions which tell a computer what to do with the data coming in, and what form the data sent out will have. Note that the data processing side of computers, which you might think is entirely reading and writing numbers, is much more than that, examples of typical data processing applications are: Digital Watch : A micro-computer in your watch is taking pulses from a crystal and requests from buttons, processing this data and producing a display which tells you the time. Car : A micro-computer in the engine is taking information from sensors telling it the current engine speed, road speed, oxygen content of the air, setting of the accelerator etc and producing voltages out which control the setting of the carburettor, timing of the spark etc, to optimise the performance of the engine. CD Player : A computer is taking a signal from the disk and converting it into the sound that you want to hear. At the same time it is keeping the laser head precisely positioned and also monitoring all the buttons in case you want to select another part of the disk. Note that some of these data processing applications are merely applying technology to existing devices to improve the way they work. However one, the CD player, could not be made to work without the built-in data processing ability. Most reasonably complex devices contain data processing components to optimise their performance and some exist only because we can build in intelligence. It is into this world that we, as software writers are moving. It is important to think of business of data processing as much more than working out the company payroll, reading in numbers and printing out results. These are the traditional uses of computers.

Note that this "raises the stakes" in that the consequences of software failing could be very damaging.

As engineers it is inevitable that a great deal of our time will be spent fitting data processing components into other devices to drive them. You will not press a switch to make something work, you will press a switch to tell a computer to make it work. These embedded systems will make computer users of everybody, and we will have to make sure that they are not even aware that there is a computer in there!

Programming Languages? 5

From Problem to Program

Programming is not about mathematics, it is about organisation.

The art of taking a problem and breaking it down into a set of instructions you can give a computer is the interesting part of programming. Unfortunately it is also the most difficult part of programming as well. If you think that learning to program is simply a matter of learning a programming language you are very wrong. In fact if you think that programming is simply a matter of coming up with a program which solves a problem you are equally wrong! There are many things you must consider when writing a program; not all of them are directly related to the problem in hand. I am going to start on the basis that you are writing your programs for a customer. He or she has problem and would like you to write a program to solve it. We shall assume that the customer knows even less about computers than we do! Initially we are not even going to talk about the programming language, type of computer or anything like that, we are simply going to make sure that we know what the customer wants. Coming up with a perfect solution to a problem the customer has not got is something which happens surprisingly often in the real world.

The worst thing you can say to a customer is "I can do that". Instead you should think "Is that what the customer wants?".

This is almost a kind of self discipline. Programmers pride themselves on their ability to come up with solutions, so as soon as they are given a problem they immediately start thinking of ways to solve it, this almost a reflex action. What you should do is think "Do I really understand what the problem is?". Before you solve a problem you should make sure that you have a watertight definition of what the problem is, which both you and the customer agree on. In the real world this is often called a Functional Design Specification or FDS. This tells you exactly what the customer wants. Both you and the customer sign it, and the bottom line is that if you provide a system which behaves according to the design specification the customer must pay you. Once you have got your design specification, then you can think about ways of solving the problem. You might think that this is not necessary if you are writing a program for yourself; there is no customer to satisfy. This is not true. Writing an FDS forces you to think about your problem at a very detailed level.

A Simple Problem

Consider the scenario; You are sitting in your favourite chair in the pub contemplating the universe when you are interrupted in your reverie by a friend of yours who sells double glazing for a living. He knows you are a programmer of sorts and would like your help in solving a problem which he has: He has just started making his own window units and is looking for a program which will do the costing of the materials for him. He wants to just enter the dimensions of the window and then get a print out of the cost to make the window, in terms of the amount of wood and glass required. "This looks like a nice little earner" you think, and once you have agreed a price you start work. The first thing you need to do is find out exactly what the customer wants you to do...

Specifying the Problem

When considering how to write the specification of a system there are three important things : ? What information flows into the system. ? What flows out of the system. ? What the system does with the information.

Programming Languages? 6

There are lots of ways of representing this information in the form of diagrams, for now we will stick with written text when specifying each of the stages:

Information going in

In the case of our immortal double glazing problem we can describe the information as: ? The width of a window. ? The height of the window.

Information coming out

The information that our customer wants to see is : ? the area of glass required for the window ? the length of wood required to build a frame.

What the program actually does

The program can derive the two values according to the following equations : glass area = width of window * height of window wood length = (width of window + height of window) * 2

Putting in more detail

We now have a fairly good understanding of what our program is going to do for us. Being sensible and far thinking people we do not stop here, we now have to worry about how our program will decide when the information coming in is actually valid. This must be done in conjunction with the customer, he or she must understand that if information is given which fits within the range specified, your program will regard the data as valid and act accordingly. In the case of the above we could therefore expand the definition of data coming in as : ? The width of the window, in metres and being a value between 0. Metres and 3.5 metres inclusive. ? The height of the window, in metres and being a value between 0.5 metres and 2.0 metres inclusive. Note that we have also added units to our description, this is very important - perhaps our customer buys wood from a supplier who sells by the foot, in which case our output description should read : ? The area of glass required for the window, in square metres. ? The length of wood required for the frame, given in feet using the conversion factor of 3.25 feet per metre.

Note that both you and the customer must understand the document!

Having written this all up in a form that both you and the customer can understand, we must then both sign the completed specification, and work can commence. In a real world you would now create a procedure which will allow you to prove that the program works, you could for example say : If I give the above program the inputs 2 metres high and 1 metre wide the program should print out : 2 square metres of glass and 9.75 feet of wood. The test procedure which is designed for a proper project should test out all possible states within the program, including the all important error conditions.

C? 8

Programming Languages

You might ask the question "Why do we need programming languages, why can we not use something like English?" There are two answers to this one:

  1. Computers are too stupid to understand English.
  2. English would make a lousy programming language.

Please note that this does not imply that tape worms would make good programmers!

To take the first point. We cannot make very clever computers at the moment. Computers are made clever by putting software into them, and there are limits to the size of program that we can create and the speed at which it can talk to us. At the moment, by using the most advanced software and hardware, we can make computers which are about as clever as a tape worm. Tape worms do not speak very good English, therefore we cannot make a computer which can understand English. The best we can do is get a computer to make sense of a very limited language which we use to tell it what to do.

Time Files like an Arrow. Fruit Flies like a Banana!

To take the second point. English as a language is packed full of ambiguities. It is very hard to express something in an unambiguous way using English, if you do not believe me, ask any lawyer! Programming languages get around both of these problems. They are simple enough to be made sense of by computer programs and they reduce ambiguity. There are very many different programming languages around, you will need to know more than one if you are to be a good programmer.

C

A look at C

There are literally hundreds of programming languages around, you will need to know at least 3!

We are going to learn a language called C. C is a very flexible and powerful programming language originally designed in the early 1970s. It is famous as the language the UNIX operating system was written in, and was specially designed for this. However its use has now spread way beyond that field and it is currently very popular. C is a professional language. So what do I mean by that? Consider the chain saw. If I, Rob Miles, want to use a chain saw I will hire one from a shop. As I am not an experienced chain saw user I would expect it to come with lots of built in safety features such as guards and automatic cut outs. These will make me much safer with the thing but will probably limit the usefulness of the tool, i.e. because of all the safety stuff I might not be able to cut down certain kinds of tree. If I was a real lumberjack I would go out and buy a professional chain saw which has no safety features whatsoever but can be used to cut down most anything. If I make a mistake with the professional tool I could quite easily lose my leg, something the amateur machine would not let happen.

C? 9

In programming terms this means is that C lacks some safety features provided by other programming languages. This makes the language much more flexible. However, if I do something stupid C will not stop me, so I have a much greater chance of crashing the computer with a C program than I do with a safer language. This is not something to worry about, you should always work on the basis that any computer will tolerate no errors on my part and anything that I do which is stupid will always cause a disaster!

Making C Run

You actually write the program using some form of text editor - which may be part of the compiling and linking system.

C is usually a compiled programming language. The computer cannot understand the language directly, so a program called a compiler converts the C into the machine code instructions which do the job. Actually getting a program to run is however a two stage process. First you show your program, often called the source , to the compiler. If the compiler gives it the thumbs up you then perform a process called linking. Linking is when all the various portions of your program are brought together to form something which can be run. You might ask "Why to we link things? - the compiler has created a machine code version of my program, can't I just run that?". The reason that we have the additional linking process is that it allows us to reuse standard pieces of code from a library. Many things your program will do are common to lots of other programs, for example you will want to read information from the keyboard and you will want to send information to the display. Rather than compile the program code which does this every time you compile your program, a much more efficient way is to put a compiled version of this code into a library. Your program just contains a reference to the particular function you want to use, the linker then loads the relevant part from the library when it creates your program.

It is possible to get the compiler to give you warnings in this case.

Note that a side effect of this is that if you refer to a function which does not exist, the compiler will not mind particularly - but the linker will not find the item in its library, and thus give you an error. Once the linker has finished you are left with a free standing file which is your program. If you run this your program gets control!

Creating C Programs

The actual business of constructing and compiling the depends on the computer you are using and the particular version of C. We will look at the business of producing your program in the laboratory section of this course. Initially it is best if we just work through your programs on paper. I reckon that you write programs best when you are not sitting at the computer, i.e. the best approach is to write (or at least map out) your solution on paper a long way away from the machine. Once you are sitting in front of the keyboard there is a great temptation to start pressing keys and typing something in which might work. This is not good technique. You will almost certainly end up with something which almost works, which you will then spend hours fiddling with to get it going. If you had sat down with a pencil and worked out the solution first you would probably get to a working system in around half the time. I am not impressed by hacking programmers who spend whole days at terminals fighting with enormous programs and debugging them into shape. I am impressed by someone who turns up, types in the program and makes it work first time!

A First C Program? 11

The Advantages of C

The good news about C is that you can write code which runs quickly, and your program is very "close to the hardware". By that I mean that you can access low level facilities in your computer quite easily, without the compiler or run time system stopping you from doing something potentially dangerous. The use of compiler directives to the pre-processor make it possible to produce a single version of a program which can be compiled on several different types of computer. In this sense C is said to be very portable. The function libraries are standard for all versions of C so they can be used on all systems.

The Disadvantages of C

The disadvantages of C fall neatly from the advantages. The biggest one is that you can write C programs which can fail in very catastrophic ways. These programs will appear totally valid as far as the compiler is concerned but will not work and may even cause your computer to stop. A more picky language would probably notice that you were doing something stupid in your program and allow you to find the error before it crashed your computer! However a more picky language would probably not allow you to write the program in the first place! Another disadvantage of C is that it allows you to write very terse code. You can express exactly what you want to do in very few statements. You might think that this is nice, because it makes your programs even more efficient, but it has the side effect of making them much harder to understand. At the time you write the code you know exactly what each part is supposed to do. If you come back to the program in several months you will need time to "get back inside it". If the code is written very tightly you will take much longer to do this, and other people may not be able to understand it at all! I write code which is not the most efficient possible, but is easy to understand. I am sacrificing program performance for ease of maintenance.

A First C Program

The Program Example

Perhaps the best way to start looking at C is to jump straight in with our first ever C program. Here it is:

A First C Program? 12

#include <stdio.h>

void main ( void ) { float height, width, area, wood_length ; scanf ( "%f", &height ) ; scanf ( "%f", &width ) ; area = 2 * height * width ; wood_length = 2 * ( h eight + width ) * 3.25 ; printf ( "The area of glass is : %f metres. \n", area ) ;

printf ( "The length of wood is : %f feet. \n", wood_length ) ; }

You should easily work out what it does, but what do all the various bits mean?

#include

The pre-processor is the part of the compiler which actually gets your program from the file.

This is a pre-processor directive. It is not part of our program, it is an instruction to the compiler to make it do something. It tells the C compiler to include the contents of a file, in this case the system file stdio.h. The compiler knows it is a system file, and therefore must be looked for in a special place, by the fact that the name is enclosed in <> characters. (see later)

<stdio.h>

All versions of C have exactly the same library functions.

This is the name of the standard library definition file for all STanDard Input Output. Your program will almost certainly want to send stuff to the screen and read things from the keyboard. stdio.h is the name of the file in which the functions that we want to use are defined. A function is simply a chunk of program that we want to use a lot, so we stuck it in a parcel and gave it a name. The function we want to use is called printf (see later). To use printf correctly C needs to know what it looks like, i.e. what things it can work on and what value it returns. The actual code which performs the printf will be tied in later by the linker. Note that without the definition of what printf looks like the compiler makes a guess when it sees the use of it. This can lead to the call failing when the program runs, a common cause of programs crashing.

The .h potion is the language extension, which denotes an include file.

The <> characters around the name tell C to look in the system area for the file stdio.h. If I had given the name " robsstuff.h " instead it would tell the compiler to look in the current directory. This means that I can set up libraries of my own routines and use them in my programs, a very useful feature.

void

Some C programs have the type of int so that the main function can return a value to the operating system which runs it. We are not going to do this.

Means literally this means nothing. In this case it is referring to the function whose name follows. It tells C that this function, which could return something interesting, (see printf ) in fact returns nothing of interest whatsoever. Why do this? Because we want to be able to handle the situation where I make a mistake and try to ascribe meaning to something which has none. If the C compiler has already been told that a given entity has no meaning it can detect my mistake and produce an error.

A First C Program? 14

refer to the above names, and the compiler will know that we are using that particular variable.

The semicolon marks the end of the list of variable names, and also the end of that declaration statement. All statements in C programs are separated by the ; character, this helps to keep the compiler on the right track. The ; character is actually very important. It tells the compiler where a given statement ends. If the compiler does not find one of these where it expects to see one it will produce an error. You can equate these characters with the sprocket holes in film, they keep everything synchronised.

scanf

If you have been used to other programming languages you might expect that the printing and reading functions are part of the language. In C this is not the case, instead they are defined as standard functions which are part of the language specification, but not part of the language itself. Any decent book on C must have a big section on how to use the standard functions and what they are. The standard input/output library contains a number of functions for formatted data transfer, the two we are going to use are scanf (scan formatted) and printf (print formatted).

A parameter is something for a function to work on.

This line is a call to the function scanf. The compiler knows that this is a function call by the fact that it is followed by a parameter list. (see later) What the compiler does at this point is parcel up the parameters you have given and then call scanf, passing the parameter information on. This function is expecting a particular sequence and type of parameters. The compiler knows what scanf looks like and the kind of information scanf uses is expecting because it has already seen the file stdio.h , which is where this function is defined. The compiler can now check that the way we have used scanf agrees with the definition. This allows valuable checking to be carried out. If the compiler had not seen this function defined before it would simply guess what it was supposed to do and probably produce a program which would not work properly.

This marks the start of the list of parameters to the scanf function. A parameter is something which a function operates on. All functions must have a parameter list, if they have no parameters the list is empty. Note that the parameters you give when you call a function must agree in number and type with those that the function expects, otherwise unpredictable things will happen. The system file stdio.h contains a description of what scanf should be given to work on. If what you supply does not agree with this the compiler will generate an error for you.

"%f",

This is the set of parameters to the call of scanf. scanf is short for scan formatted. The function is controlled by the format string, which is the first parameter to the function. The second and successive parameters are addresses into which scanf puts the values it has been told to fetch.

A First C Program? 15

The " character de fines the limits of the string.

In C a string is given as a sequence of characters enclosed in " characters. You will meet the concept of delimiters regularly in C. A delimiter is a particular character which is to be used to define the limits of something. C uses different delimiters in different places, the " character is used to delimit a string of text. When the compiler sees the first " it recognises that everything up until the next " is a sequence of characters which we wish to use. It therefore just assembles the string until it finds another ". The string must all appear on the same line, otherwise you will get an error. The scanf function has been told to look out for certain characters in the format string and treat them as special. Such a special character is %. The % character tells scanf that we are giving the format of a number which is to be processed. The letter after the % character tells scanf what kind of value is being fetched, f means floating point. This command will cause scanf to look for a floating point number on the input. If it finds one it is to put the value in the address given as the next parameter.

&height

In C a function cannot change the value of a parameter which is supplied to it. This is quite a departure from other languages, which let you do this. It does make life more complicated, when you want your function to be able to change the value of something. The way that you get the effect is by cheating. Instead of passing scanf the variable height we have instead supplied &height. The & is very important. When the compiler sees the & it regards this as an instruction to create a pointer to the given variable, and then pass this pointer to scanf. This means that when scanf runs it is given a a pointer to where the result is to be placed, which is OK.

The ) character marks the end of the list of parameters to scanf and the ; then end of this statement.

scanf ( "%f", &width ) ;

The function of this line is identical to the one above, except that the result is placed into the variable called width.

area = 2 * height * width ;

This is an assignment. The assignments are the bread and butter of programming. A good proportion of your programs will be instructions to assign new values to variables, as the various results are calculated. C uses the = character to make assignments happen. The first part of this statement is the name of a previously defined variable. This is followed by the = character which I call the gozzinta. I call it that because the value on the right gozzinta (goes into ) the variable on the left. When this program runs the expression is worked out and then the result is placed in the specified variable. In this case the expression works out the total amount of glass needed. This result is then placed in the area variable.