+ All Categories

C baics

Date post: 07-Apr-2015
Category:
Upload: dharish
View: 56 times
Download: 0 times
Share this document with a friend
189
http://www2.its.strath.ac.uk/courses/c/subsection3_4_2.html Next: Weight Conversion Table Up: A Quick Overview Previous: A Very Simple A Weight Conversion Program #include <stdio.h> #define KILOS_PER_POUND .45359 main() { int pounds; printf(" US lbs UK st. lbs INT Kg\n"); for(pounds=10; pounds < 250; pounds+=10) { int stones = pounds / 14; int uklbs = pounds % 14; float kilos = pounds * KILOS_PER_POUND; printf(" %d %d %d %f\n", pounds, stones, uklbs, kilos); } } Again notice that the only function is called main. int pounds; Creates a variable of integer type called pounds. float kilos; Creates a floating point variable (real number) called kilos. #define KILOS_PER_POUND .45359 defines a constant called KILOS_PER_POUND. This definition will be true throughout the program. It is customary to use capital letters for the names of constants, since they are implemented by the C preprocessor. for(pounds=10; pounds < 250; pounds+=10) This is the start of the loop. All statements enclosed in the following curly brackets will be repeated. The loop definition contains three parts separated by semi-colons. The first is used to initialise variables when the loop is entered. The second is a check, when it proves false, the loop is exited. The third is a statement used to modify loop counters on each loop iteration after the first. http://www2.its.strath.ac.uk/courses/c/subsection3_4_2.html (1 of 2) [6/23/2010 11:20:17 AM]
Transcript
Page 1: C baics

http://www2.its.strath.ac.uk/courses/c/subsection3_4_2.html

Next: Weight Conversion Table Up: A Quick Overview Previous: A Very Simple

A Weight Conversion Program

#include <stdio.h>#define KILOS_PER_POUND .45359main(){ int pounds;

printf(" US lbs UK st. lbs INT Kg\n");

for(pounds=10; pounds < 250; pounds+=10) { int stones = pounds / 14; int uklbs = pounds % 14; float kilos = pounds * KILOS_PER_POUND; printf(" %d %d %d %f\n", pounds, stones, uklbs, kilos); }}

Again notice that the only function is called main.

int pounds; Creates a variable of integer type called pounds.

float kilos; Creates a floating point variable (real number) called kilos.

#define KILOS_PER_POUND .45359 defines a constant called KILOS_PER_POUND. This definition will be true throughout the program. It is customary to use capital letters for the names of constants, since they are implemented by the C preprocessor.

for(pounds=10; pounds < 250; pounds+=10) This is the start of the loop. All statements enclosed in the following curly brackets will be repeated. The loop definition contains three parts separated by semi-colons.

● The first is used to initialise variables when the loop is entered. ● The second is a check, when it proves false, the loop is exited. ● The third is a statement used to modify loop counters on each loop iteration after the first.

http://www2.its.strath.ac.uk/courses/c/subsection3_4_2.html (1 of 2) [6/23/2010 11:20:17 AM]

Page 2: C baics

http://www2.its.strath.ac.uk/courses/c/subsection3_4_2.html

The effect of pounds += 10 is to add 10 to the value of the variable pounds. This is a shorthand way of writing pounds = pounds + 10.

The printf statement now contains the symbols %d and %f. These are instructions to print out a decimal (integer) or floating (real) value. The values to be printed are listed after the closing quote of the printf statement. Note also that the printf statement has been split over 2 lines so it can fit onto our page. The computer can recognise this because all C statements end with a semicolon.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_4_2.html (2 of 2) [6/23/2010 11:20:17 AM]

Page 3: C baics

Weight Conversion Table Using a Function

Next: Weight Conversion with Up: A Quick Overview Previous: A Weight Conversion

Weight Conversion Table Using a Function

The previous program could be better structured by defining a function to convert the weights and print out their values. It will then look like this.

#include <stdio.h>

void print_converted(int pounds)/* Convert U.S. Weight to Imperial and International Units. Print the results */{ int stones = pounds / 14; int uklbs = pounds % 14; float kilos_per_pound = 0.45359; float kilos = pounds * kilos_per_pound;

printf(" %3d %2d %2d %6.2f\n", pounds, stones, uklbs, kilos);}

main(){ int us_pounds;

printf(" US lbs UK st. lbs INT Kg\n");

for(us_pounds=10; us_pounds < 250; us_pounds+=10) print_converted(us_pounds);}

void print_converted(int pounds) is the beginning of the function definition. The line within the loop reading print_converted(us_pounds) is a call to that function. When execution of the main function reaches that call, print_converted is executed, after which control returns to main.

The text enclosed by symbols /* and */ is a comment. These are C's way of separating plain text comments from the body of the program. It is usually good practice to have a short comment to explain the purpose of each function.

Defining a function has made this program larger, but what have we gained? The structure has been

http://www2.its.strath.ac.uk/courses/c/subsection3_4_3.html (1 of 2) [6/23/2010 11:20:18 AM]

Page 4: C baics

Weight Conversion Table Using a Function

improved. This may make little difference to the readability of such a small program. In a larger program, such structuring makes the program shorter, easier to read, and simplifies future maintenance of the program. Another benefit of defining a function, is that the function can easily be re-used as part of another program.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_4_3.html (2 of 2) [6/23/2010 11:20:18 AM]

Page 5: C baics

Weight Conversion with a Prompt

Next: Weight Conversion with Up: A Quick Overview Previous: Weight Conversion Table

Weight Conversion with a Prompt

To illustrate this, our next program will re-use the function from 2.3. The program is similar to the last one, but instead of printing a table of weights, the user enters a weight, and this value is converted. Such a program requires user input. This can be done in two ways, both of which will be shown.

The simpler implementation is to prompt for a weight, and then read the user's keyboard input. This would be done as follows

#include <stdio.h>

void print_converted(int pounds)/* Convert U.S. Weight to Imperial and International Units. Print the results */{ int stones = pounds / 14; int uklbs = pounds % 14; float kilos_per_pound = 0.45359; float kilos = pounds * kilos_per_pound;

printf(" %3d %2d %2d %6.2f\n", pounds, stones, uklbs, kilos);}

main(){ int us_pounds;

printf("Give an integer weight in Pounds : "); scanf("%d", &us_pounds);

printf(" US lbs UK st. lbs INT Kg\n"); print_converted(us_pounds);}

A printf statement is used to prompt for input. scanf is an equivalent input statement, note that the variable to be read us_pounds is written as &us_pounds here. This is very important and it will be explained later.

http://www2.its.strath.ac.uk/courses/c/subsection3_4_4.html (1 of 2) [6/23/2010 11:20:18 AM]

Page 6: C baics

Weight Conversion with a Prompt

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_4_4.html (2 of 2) [6/23/2010 11:20:18 AM]

Page 7: C baics

http://www2.its.strath.ac.uk/courses/c/subsection3_4_5.html

Next: Fibonacci Series Using Up: A Quick Overview Previous: Weight Conversion with

Weight Conversion with Command Line Argument

In this example, the number to be converted is supplied as part of the command to run the program. This can be a useful way of supplying a limited number of names or numbers to a program. We can run the program by typing program 1200 to convert 1200 lbs.

#include <stdio.h>

void print_converted(int pounds)/* Convert U.S. Weight to Imperial and International Units. Print the results */{ int stones = pounds / 14; int uklbs = pounds % 14; float kilos_per_pound = 0.45359; float kilos = pounds * kilos_per_pound;

printf(" %3d %2d %2d %6.2f\n", pounds, stones, uklbs, kilos);}

main(int argc,char *argv[]){ int pounds;

if(argc != 2) { printf("Usage: convert weight_in_pounds\n"); exit(1); }

sscanf(argv[1], "%d", &pounds); /* Convert String to int */

printf(" US lbs UK st. lbs INT Kg\n"); print_converted(pounds);}

The main function definition has changed so that it takes two arguments, argc is a count of the number of arguments, and argv is an array of strings containing each of the arguments. The system creates these when the program is run.

http://www2.its.strath.ac.uk/courses/c/subsection3_4_5.html (1 of 2) [6/23/2010 11:20:19 AM]

Page 8: C baics

http://www2.its.strath.ac.uk/courses/c/subsection3_4_5.html

Some other new concepts are introduced here.

if(argc != 2) Checks that the typed command has two elements, the command name and the weight in pounds.

exit(1); Leave the program. The argument 1 is a way of telling the operating system that an error has occurred. A 0 would be used for a successful exit.

sscanf(argv[1], "%d", &pounds) Converts a string like "100" into an integer value stored in pounds. The argument is stored in argv[1] as a string. sscanf works like scanf, but reads from a string instead of from the terminal.

The rest of the program is the same as the previous one.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_4_5.html (2 of 2) [6/23/2010 11:20:19 AM]

Page 9: C baics

http://www2.its.strath.ac.uk/courses/c/subsection3_4_6.html

Next: Using C with Up: A Quick Overview Previous: Weight Conversion with

Fibonacci Series Using an Array

We have now used a variety the features of C. This final example will introduce the array. The program prints out a table of Fibonacci numbers. These are defined by a series in which any element is the sum of the previous two elements. This program stores the series in an array, and after calculating it, prints the numbers out as a table.

#include <stdio.h>

main(){ int fib[24]; int i;

fib[0] = 0; fib[1] = 1;

for(i = 2; i < 24; i++) fib[i] = fib[i-1] + fib[i-2];

for (i = 0; i < 24; i++) printf("%3d %6d\n", i, fib[i]);}

One new construct has been introduced here.

int fib[24]; This defines an array called fib of 24 integers. In C, the array index is bounded by square brackets (this avoids confusion with a function call). Also, the first element of the array has index zero, and for this 24 element array, the last element is index 23.

Following this brief scan through the language, we shall introduce the components of C in rather more detail.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_4_6.html [6/23/2010 11:20:19 AM]

Page 10: C baics

http://www2.its.strath.ac.uk/courses/c/section3_5.html

Next: Writing the Program Up: C Programming Previous: Fibonacci Series Using

Using C with UNIXA little knowledge is necessary before you can write and compile programs on the UNIX system. Every programmer goes through the same three step cycle.

1. Writing the program into a file 2. Compiling the program 3. Running the program

During program development, the programmer may repeat this cycle many times, refining, testing and debugging a program until a satisfactory result is achieved. The UNIX commands for each step are discussed below.

● Writing the Program ● Compiling the Program

❍ The C Compiler (cc) ❍ Make, a Program Builder ❍ Improved Type Checking Using Lint

● Running the Program

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_5.html [6/23/2010 11:20:20 AM]

Page 11: C baics

Writing the Program

Next: Compiling the Program Up: Using C with Previous: Using C with

Writing the Program

UNIX expects you to store your program in a file whose name ends in .c This identifies it as a C program. The easiest way to enter your text is using a text editor like vi, emacs or xedit. To edit a file called testprog.c using vi type

vi testprog.c

The editor is also used to make subsequent changes to the program.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_5_1.html [6/23/2010 11:20:21 AM]

Page 12: C baics

Compiling the Program

Next: The C Compiler Up: Using C with Previous: Writing the Program

Compiling the Program

There are a number of ways to achieve this, though all of them eventually rely on the compiler (called cc on our system).

● The C Compiler (cc) ● Make, a Program Builder ● Improved Type Checking Using Lint

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_5_2.html [6/23/2010 11:20:21 AM]

Page 13: C baics

The C Compiler (cc)

Next: Makea Program Up: Compiling the Program Previous: Compiling the Program

The C Compiler (cc)

The simplest method is to type

cc testprog.c

This will try to compile testprog.c, and, if successful, will produce a runnable file called a.out. If you want to give the runnable file a better name you can type

cc testprog.c -o testprog

This will compile testprog.c, creating runnable file testprog.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_5_2_1.html [6/23/2010 11:20:21 AM]

Page 14: C baics

Make, a Program Builder

Next: Improved Type Checking Up: Compiling the Program Previous: The C Compiler

Make, a Program Builder

UNIX also includes a very useful program called make. Make allows very complicated programs to be compiled quickly, by reference to a configuration file (usually called Makefile). If your C program is a single file, you can usually use make by simply typing

make testprog

This will compile testprog.c and put the executable code in testprog.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_5_2_2.html [6/23/2010 11:20:22 AM]

Page 15: C baics

Improved Type Checking Using Lint

Next: Running the Program Up: Compiling the Program Previous: Makea Program

Improved Type Checking Using Lint

The C compiler is rather liberal about type checking function arguments, it doesn't check bounds of array indices. There is a stricter checker called lint which won't generate any runnable code. It is a good idea to use lint to check your programs before they are completed. This is done by typing

lint testprog.c

Lint is very good at detecting errors which cause programs to crash at run time. However, lint is very fussy, and generally produces a long list of messages about minor problems with the program. Many of these will be quite harmless. Experience will teach you to distinguish the important messages from those which can be ignored.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_5_2_3.html [6/23/2010 11:20:22 AM]

Page 16: C baics

Running the Program

Next: Constant and Variable Up: Using C with Previous: Improved Type Checking

Running the Program

To run a program under UNIX you simply type in the filename. So to run program testprog, you would type

testprog

or if this fails to work, you could type

./testprog

You will see your prompt again after the program is done.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_5_3.html [6/23/2010 11:20:23 AM]

Page 17: C baics

Constant and Variable Types

Next: Variables Up: C Programming Previous: Running the Program

Constant and Variable Types

● Variables ● Variable Names ● Global Variables

❍ External Variables ● Static Variables ● Constants ● Arrays

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_6.html [6/23/2010 11:20:23 AM]

Page 18: C baics

Variables

Next: Variable Names Up: Constant and Variable Previous: Constant and Variable

Variables

In C, a variable must be declared before it can be used. Variables can be declared at the start of any block of code, but most are found at the start of each function. Most local variables are created when the function is called, and are destroyed on return from that function.

A declaration begins with the type, followed by the name of one or more variables. For example,

int high, low, results[20];

Declarations can be spread out, allowing space for an explanatory comment. Variables can also be initialised when they are declared, this is done by adding an equals sign and the required value after the declaration.

int high = 250; /* Maximum Temperature */int low = -40; /* Minimum Temperature */int results[20]; /* Series of temperature readings */

C provides a wide range of types. The most common are

There are also several variants on these types.

All of the integer types plus the char are called the integral types. float and double are called the real

http://www2.its.strath.ac.uk/courses/c/subsection3_6_1.html (1 of 2) [6/23/2010 11:20:24 AM]

Page 19: C baics

Variables

types.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_6_1.html (2 of 2) [6/23/2010 11:20:24 AM]

Page 20: C baics

Variable Names

Next: Global Variables Up: Constant and Variable Previous: Variables

Variable Names

Every variable has a name and a value. The name identifies the variable, the value stores data. There is a limitation on what these names can be. Every variable name in C must start with a letter, the rest of the name can consist of letters, numbers and underscore characters. C recognises upper and lower case characters as being different. Finally, you cannot use any of C's keywords like main, while, switch etc as variable names.

Examples of legal variable names include

x result outfile bestyetx1 x2 out_file best_yetpower impetus gamma hi_score

It is conventional to avoid the use of capital letters in variable names. These are used for names of constants. Some old implementations of C only use the first 8 characters of a variable name. Most modern ones don't apply this limit though.

The rules governing variable names also apply to the names of functions. We shall meet functions later on in the course.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_6_2.html [6/23/2010 11:20:25 AM]

Page 21: C baics

Global Variables

Next: External Variables Up: Constant and Variable Previous: Variable Names

Global Variables

Local variables are declared within the body of a function, and can only be used within that function. This is usually no problem, since when another function is called, all required data is passed to it as arguments. Alternatively, a variable can be declared globally so it is available to all functions. Modern programming practice recommends against the excessive use of global variables. They can lead to poor program structure, and tend to clog up the available name space.

A global variable declaration looks normal, but is located outside any of the program's functions. This is usually done at the beginning of the program file, but after preprocessor directives. The variable is not declared again in the body of the functions which access it.

● External Variables

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_6_3.html [6/23/2010 11:20:26 AM]

Page 22: C baics

External Variables

Next: Static Variables Up: Global Variables Previous: Global Variables

External Variables

Where a global variable is declared in one file, but used by functions from another, then the variable is called an external variable in these functions, and must be declared as such. The declaration must be preceeded by the word extern. The declaration is required so the compiler can find the type of the variable without having to search through several source files for the declaration.

Global and external variables can be of any legal type. They can be initialised, but the initialisation takes place when the program starts up, before entry to the main function.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_6_3_1.html [6/23/2010 11:20:26 AM]

Page 23: C baics

Static Variables

Next: Constants Up: Constant and Variable Previous: External Variables

Static Variables

Another class of local variable is the static type. A static can only be accessed from the function in which it was declared, like a local variable. The static variable is not destroyed on exit from the function, instead its value is preserved, and becomes available again when the function is next called. Static variables are declared as local variables, but the declaration is preceeded by the word static.

static int counter;

Static variables can be initialised as normal, the initialisation is performed once only, when the program starts up.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_6_4.html [6/23/2010 11:20:27 AM]

Page 24: C baics

Constants

Next: Arrays Up: Constant and Variable Previous: Static Variables

Constants

A C constant is usually just the written version of a number. For example 1, 0, 5.73, 12.5e9. We can specify our constants in octal or hexadecimal, or force them to be treated as long integers.

● Octal constants are written with a leading zero - 015. ● Hexadecimal constants are written with a leading 0x - 0x1ae. ● Long constants are written with a trailing L - 890L.

Character constants are usually just the character enclosed in single quotes; 'a', 'b', 'c'. Some characters can't be represented in this way, so we use a 2 character sequence.

In addition, a required bit pattern can be specified using its octal equivalent.

'\044' produces bit pattern 00100100.

Character constants are rarely used, since string constants are more convenient. A string constant is surrounded by double quotes eg "Brian and Dennis". The string is actually stored as an array of characters. The null character '\0' is automatically placed at the end of such a string to act as a string terminator.

A character is a different type to a single character string. This is important.

We will meet strings and characters again when we deal with the input / output functions in more detail.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_6_5.html [6/23/2010 11:20:28 AM]

Page 25: C baics

Arrays

Next: Expressions and Operators Up: Constant and Variable Previous: Constants

Arrays

An array is a collection of variables of the same type. Individual array elements are identified by an integer index. In C the index begins at zero and is always written inside square brackets.

We have already met single dimensioned arrays which are declared like this

int results[20];

Arrays can have more dimensions, in which case they might be declared as

int results_2d[20][5];int results_3d[20][5][3];

Each index has its own set of square brackets.

Where an array is declared in the main function it will usually have details of dimensions included. It is possible to use another type called a pointer in place of an array. This means that dimensions are not fixed immediately, but space can be allocated as required. This is an advanced technique which is only required in certain specialised programs.

When passed as an argument to a function, the receiving function need not know the size of the array. So for example if we have a function which sorts a list (represented by an array) then the function will be able to sort lists of different sizes. The drawback is that the function is unable to determine what size the list is, so this information will have to be passed as an additional argument.

As an example, here is a simple function to add up all of the integers in a single dimensioned array.

int add_array(int array[], int size){ int i; int total = 0;

for(i = 0; i < size; i++) total += array[i];

return(total);

http://www2.its.strath.ac.uk/courses/c/subsection3_6_6.html (1 of 2) [6/23/2010 11:20:28 AM]

Page 26: C baics

Arrays

}

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_6_6.html (2 of 2) [6/23/2010 11:20:28 AM]

Page 27: C baics

Expressions and Operators

Next: Assignment Statement Up: C Programming Previous: Arrays

Expressions and OperatorsOne reason for the power of C is its wide range of useful operators. An operator is a function which is applied to values to give a result. You should be familiar with operators such as +, -, /.

Arithmetic operators are the most common. Other operators are used for comparison of values, combination of logical states, and manipulation of individual binary digits. The binary operators are rather low level for so are not covered here.

Operators and values are combined to form expressions. The values produced by these expressions can be stored in variables, or used as a part of even larger expressions.

● Assignment Statement ● Arithmetic operators ● Type conversion ● Comparison ● Logical Connectors ● Summary

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_7.html [6/23/2010 11:20:29 AM]

Page 28: C baics

Assignment Statement

Next: Arithmetic operators Up: Expressions and Operators Previous: Expressions and Operators

Assignment Statement

The easiest example of an expression is in the assignment statement. An expression is evaluated, and the result is saved in a variable. A simple example might look like

y = (m * x) + c

This assignment will save the value of the expression in variable y.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_7_1.html [6/23/2010 11:20:29 AM]

Page 29: C baics

Arithmetic operators

Next: Type conversion Up: Expressions and Operators Previous: Assignment Statement

Arithmetic operators

Here are the most common arithmetic operators

*, / and % will be performed before + or - in any expression. Brackets can be used to force a different order of evaluation to this. Where division is performed between two integers, the result will be an integer, with remainder discarded. Modulo reduction is only meaningful between integers. If a program is ever required to divide a number by zero, this will cause an error, usually causing the program to crash.

Here are some arithmetic expressions used within assignment statements.

velocity = distance / time;

force = mass * acceleration;

count = count + 1;

C has some operators which allow abbreviation of certain types of arithmetic assignment statements.

These operations are usually very efficient. They can be combined with another expression.

http://www2.its.strath.ac.uk/courses/c/subsection3_7_2.html (1 of 2) [6/23/2010 11:20:31 AM]

Page 30: C baics

Arithmetic operators

Versions where the operator occurs before the variable name change the value of the variable before evaluating the expression, so

These can cause confusion if you try to do too many things on one command line. You are recommended to restrict your use of ++ and - to ensure that your programs stay readable.

Another shorthand notation is listed below

These are simple to read and use.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_7_2.html (2 of 2) [6/23/2010 11:20:31 AM]

Page 31: C baics

Type conversion

Next: Comparison Up: Expressions and Operators Previous: Arithmetic operators

Type conversion

You can mix the types of values in your arithmetic expressions. char types will be treated as int. Otherwise where types of different size are involved, the result will usually be of the larger size, so a float and a double would produce a double result. Where integer and real types meet, the result will be a double.

There is usually no trouble in assigning a value to a variable of different type. The value will be preserved as expected except where;

● The variable is too small to hold the value. In this case it will be corrupted (this is bad). ● The variable is an integer type and is being assigned a real value. The value is rounded down.

This is often done deliberately by the programmer.

Values passed as function arguments must be of the correct type. The function has no way of determining the type passed to it, so automatic conversion cannot take place. This can lead to corrupt results. The solution is to use a method called casting which temporarily disguises a value as a different type.

eg. The function sqrt finds the square root of a double.

int i = 256;int root

root = sqrt( (double) i);

The cast is made by putting the bracketed name of the required type just before the value. (double) in this example. The result of sqrt( (double) i); is also a double, but this is automatically converted to an int on assignment to root.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_7_3.html [6/23/2010 11:20:31 AM]

Page 32: C baics

Comparison

Next: Logical Connectors Up: Expressions and Operators Previous: Type conversion

Comparison

C has no special type to represent logical or boolean values. It improvises by using any of the integral types char, int, short, long, unsigned, with a value of 0 representing false and any other value representing true. It is rare for logical values to be stored in variables. They are usually generated as required by comparing two numeric values. This is where the comparison operators are used, they compare two numeric values and produce a logical result.

Note that == is used in comparisons and = is used in assignments. Comparison operators are used in expressions like the ones below.

x == y

i > 10

a + b != c

In the last example, all arithmetic is done before any comparison is made.

These comparisons are most frequently used to control an if statement or a for or a while loop. These will be introduced in a later chapter.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_7_4.html [6/23/2010 11:20:32 AM]

Page 33: C baics

Logical Connectors

Next: Summary Up: Expressions and Operators Previous: Comparison

Logical Connectors

These are the usual And, Or and Not operators.

They are frequently used to combine relational operators, for example

x < 20 && x >= 10

In C these logical connectives employ a technique known as lazy evaluation. They evaluate their left hand operand, and then only evaluate the right hand one if this is required. Clearly false && anything is always false, true || anything is always true. In such cases the second test is not evaluated.

Not operates on a single logical value, its effect is to reverse its state. Here is an example of its use.

if ( ! acceptable ) printf("Not Acceptable !!\n");

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_7_5.html [6/23/2010 11:20:33 AM]

Page 34: C baics

Summary

Next: Control Statements Up: Expressions and Operators Previous: Logical Connectors

Summary

Three types of expression have been introduced here;

● Arithmetic expressions are simple, but watch out for subtle type conversions. The shorthand notations may save you a lot of typing.

● Comparison takes two numbers and produces a logical result. Comparisons are usually found controlling if statements or loops.

● Logical connectors allow several comparisons to be combined into a single test. Lazy evaluation can improve the efficiency of the program by reducing the amount of calculation required.

C also provides bit manipulation operators. These are too specialised for the scope of this course.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_7_6.html [6/23/2010 11:20:33 AM]

Page 35: C baics

Control Statements

Next: The if else Up: C Programming Previous: Summary

Control StatementsA program consists of a number of statements which are usually executed in sequence. Programs can be much more powerful if we can control the order in which statements are run.

Statements fall into three general types;

● Assignment, where values, usually the results of calculations, are stored in variables. ● Input / Output, data is read in or printed out. ● Control, the program makes a decision about what to do next.

This section will discuss the use of control statements in C. We will show how they can be used to write powerful programs by;

● Repeating important sections of the program. ● Selecting between optional sections of a program.

● The if else Statement ● The switch Statement ● Loops ● The while Loop ● The do while Loop ● The for Loop ● The break Statement ● The continue Statement ● The goto Statement

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_8.html [6/23/2010 11:20:34 AM]

Page 36: C baics

The if else Statement

Next: The switch Statement Up: Control Statements Previous: Control Statements

The if else Statement

This is used to decide whether to do something at a special point, or to decide between two courses of action.

The following test decides whether a student has passed an exam with a pass mark of 45

if (result >= 45) printf("Pass\n");else printf("Fail\n");

It is possible to use the if part without the else.

if (temperature < 0) print("Frozen\n");

Each version consists of a test, (this is the bracketed statement following the if). If the test is true then the next statement is obeyed. If is is false then the statement following the else is obeyed if present. After this, the rest of the program continues as normal.

If we wish to have more than one statement following the if or the else, they should be grouped together between curly brackets. Such a grouping is called a compound statement or a block.

if (result >= 45){ printf("Passed\n"); printf("Congratulations\n")}else{ printf("Failed\n"); printf("Good luck in the resits\n");}

Sometimes we wish to make a multi-way decision based on several conditions. The most general way of doing this is by using the else if variant on the if statement. This works by cascading several

http://www2.its.strath.ac.uk/courses/c/subsection3_8_1.html (1 of 2) [6/23/2010 11:20:34 AM]

Page 37: C baics

The if else Statement

comparisons. As soon as one of these gives a true result, the following statement or block is executed, and no further comparisons are performed. In the following example we are awarding grades depending on the exam result.

if (result >= 75) printf("Passed: Grade A\n");else if (result >= 60) printf("Passed: Grade B\n");else if (result >= 45) printf("Passed: Grade C\n");else printf("Failed\n");

In this example, all comparisons test a single variable called result. In other cases, each test may involve a different variable or some combination of tests. The same pattern can be used with more or fewer else if's, and the final lone else may be left out. It is up to the programmer to devise the correct structure for each programming problem.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_8_1.html (2 of 2) [6/23/2010 11:20:34 AM]

Page 38: C baics

The switch Statement

Next: Loops Up: Control Statements Previous: The if else

The switch Statement

This is another form of the multi way decision. It is well structured, but can only be used in certain cases where;

● Only one variable is tested, all branches must depend on the value of that variable. The variable must be an integral type. (int, long, short or char).

● Each possible value of the variable can control a single branch. A final, catch all, default branch may optionally be used to trap all unspecified cases.

Hopefully an example will clarify things. This is a function which converts an integer into a vague description. It is useful where we are only concerned in measuring a quantity when it is quite small.

estimate(number)int number;/* Estimate a number as none, one, two, several, many */{ switch(number) { case 0 : printf("None\n"); break; case 1 : printf("One\n"); break; case 2 : printf("Two\n"); break; case 3 : case 4 : case 5 : printf("Several\n"); break; default : printf("Many\n"); break; }}

http://www2.its.strath.ac.uk/courses/c/subsection3_8_2.html (1 of 2) [6/23/2010 11:20:35 AM]

Page 39: C baics

The switch Statement

Each interesting case is listed with a corresponding action. The break statement prevents any further statements from being executed by leaving the switch. Since case 3 and case 4 have no following break, they continue on allowing the same action for several values of number.

Both if and switch constructs allow the programmer to make a selection from a number of possible actions.

The other main type of control statement is the loop. Loops allow a statement, or block of statements, to be repeated. Computers are very good at repeating simple tasks many times, the loop is C's way of achieving this.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_8_2.html (2 of 2) [6/23/2010 11:20:35 AM]

Page 40: C baics

Loops

Next: The while Loop Up: Control Statements Previous: The switch Statement

Loops

C gives you a choice of three types of loop, while, do while and for.

● The while loop keeps repeating an action until an associated test returns false. This is useful where the programmer does not know in advance how many times the loop will be traversed.

● The do while loops is similar, but the test occurs after the loop body is executed. This ensures that the loop body is run at least once.

● The for loop is frequently used, usually where the loop will be traversed a fixed number of times. It is very flexible, and novice programmers should take care not to abuse the power it offers.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_8_3.html [6/23/2010 11:20:36 AM]

Page 41: C baics

The while Loop

Next: The do while Up: Control Statements Previous: Loops

The while Loop

The while loop repeats a statement until the test at the top proves false.

As an example, here is a function to return the length of a string. Remember that the string is represented as an array of characters terminated by a null character '\0'.

int string_length(char string[]){ int i = 0;

while (string[i] != '\0') i++;

return(i);}

The string is passed to the function as an argument. The size of the array is not specified, the function will work for a string of any size.

The while loop is used to look at the characters in the string one at a time until the null character is found. Then the loop is exited and the index of the null is returned. While the character isn't null, the index is incremented and the test is repeated.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_8_4.html [6/23/2010 11:20:36 AM]

Page 42: C baics

The do while Loop

Next: The for Loop Up: Control Statements Previous: The while Loop

The do while Loop

This is very similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The test then verifies the data, and loops back to read again if it was unacceptable.

do{ printf("Enter 1 for yes, 0 for no :"); scanf("%d", &input_value);} while (input_value != 1 && input_value != 0)

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_8_5.html [6/23/2010 11:20:36 AM]

Page 43: C baics

The for Loop

Next: The break Statement Up: Control Statements Previous: The do while

The for Loop

The for loop works well where the number of iterations of the loop is known before the loop is entered. The head of the loop consists of three parts separated by semicolons.

● The first is run before the loop is entered. This is usually the initialisation of the loop variable. ● The second is a test, the loop is exited when this returns false. ● The third is a statement to be run every time the loop body is completed. This is usually an

increment of the loop counter.

The example is a function which calculates the average of the numbers stored in an array. The function takes the array and the number of elements as arguments.

float average(float array[], int count){ float total = 0.0; int i;

for(i = 0; i < count; i++) total += array[i];

return(total / count);}

The for loop ensures that the correct number of array elements are added up before calculating the average.

The three statements at the head of a for loop usually do just one thing each, however any of them can be left blank. A blank first or last statement will mean no initialisation or running increment. A blank comparison statement will always be treated as true. This will cause the loop to run indefinitely unless interrupted by some other means. This might be a return or a break statement.

It is also possible to squeeze several statements into the first or third position, separating them with commas. This allows a loop with more than one controlling variable. The example below illustrates the definition of such a loop, with variables hi and lo starting at 100 and 0 respectively and converging.

for (hi = 100, lo = 0; hi >= lo; hi--, lo++)

http://www2.its.strath.ac.uk/courses/c/subsection3_8_6.html (1 of 2) [6/23/2010 11:20:37 AM]

Page 44: C baics

The for Loop

The for loop is extremely flexible and allows many types of program behaviour to be specified simply and quickly.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_8_6.html (2 of 2) [6/23/2010 11:20:37 AM]

Page 45: C baics

The break Statement

Next: The continue Statement Up: Control Statements Previous: The for Loop

The break Statement

We have already met break in the discussion of the switch statement. It is used to exit from a loop or a switch, control passing to the first statement beyond the loop or a switch.

With loops, break can be used to force an early exit from the loop, or to implement a loop with a test to exit in the middle of the loop body. A break within a loop should always be protected within an if statement which provides the test to control the exit condition.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_8_7.html [6/23/2010 11:20:37 AM]

Page 46: C baics

The continue Statement

Next: The goto Statement Up: Control Statements Previous: The break Statement

The continue Statement

This is similar to break but is encountered less frequently. It only works within loops where its effect is to force an immediate jump to the loop control statement.

● In a while loop, jump to the test statement. ● In a do while loop, jump to the test statement. ● In a for loop, jump to the test, and perform the iteration.

Like a break, continue should be protected by an if statement. You are unlikely to use it very often.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_8_8.html [6/23/2010 11:20:38 AM]

Page 47: C baics

The goto Statement

Next: Functions in C Up: Control Statements Previous: The continue Statement

The goto Statement

C has a goto statement which permits unstructured jumps to be made. Its use is not recommended, so we'll not teach it here. Consult your textbook for details of its use.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_8_9.html [6/23/2010 11:20:39 AM]

Page 48: C baics

Functions in C

Next: Scope of Function Up: C Programming Previous: The goto Statement

Functions in CAlmost all programming languages have some equivalent of the function. You may have met them under the alternative names subroutine or procedure.

Some languages distinguish between functions which return variables and those which don't. C assumes that every function will return a value. If the programmer wants a return value, this is achieved using the return statement. If no return value is required, none should be used when calling the function.

Here is a function which raises a double to the power of an unsigned, and returns the result.

double power(double val, unsigned pow){ double ret_val = 1.0; unsigned i;

for(i = 0; i < pow; i++) ret_val *= val;

return(ret_val);}

The function follows a simple algorithm, multiplying the value by itself pow times. A for loop is used to control the number of multiplications, and variable ret_val stores the value to be returned. Careful

programming has ensured that the boundary condition is correct too. ie .

Let us examine the details of this function.

double power(double val, unsigned pow) This line begins the function definition. It tells us the type of the return value, the name of the function, and a list of arguments used by the function. The arguments and their types are enclosed in brackets, each pair separated by commas.

The body of the function is bounded by a set of curly brackets. Any variables declared here will be treated as local unless specifically declared as static or extern types.

http://www2.its.strath.ac.uk/courses/c/section3_9.html (1 of 3) [6/23/2010 11:20:39 AM]

Page 49: C baics

Functions in C

return(ret_val); On reaching a return statement, control of the program returns to the calling function. The bracketed value is the value which is returned from the function. If the final closing curly bracket is reached before any return value, then the function will return automatically, any return value will then be meaningless.

The example function can be called by a line in another function which looks like this

result = power(val, pow);

This calls the function power assigning the return value to variable result.

Here is an example of a function which does not return a value.

void error_line(int line){ fprintf(stderr, "Error in input data: line %d\n", line);}

The definition uses type void which is optional. It shows that no return value is used. Otherwise the function is much the same as the previous example, except that there is no return statement. Some void type functions might use return, but only to force an early exit from the function, and not to return any value. This is rather like using break to jump out of a loop.

This function also demonstrates a new feature.

fprintf(stderr, "Error in input data: line %d\n", line); This is a variant on the printf statement, fprintf sends its output into a file. In this case, the file is stderr. stderr is a special UNIX file which serves as the channel for error messages. It is usually connected to the console of the computer system, so this is a good way to display error messages from your programs. Messages sent to stderr will appear on screen even if the normal output of the program has been redirected to a file or a printer.

The function would be called as follows

error_line(line_number);

● Scope of Function Variables ● Modifying Function Arguments ● Pointers in C ● Arrays and Pointers

http://www2.its.strath.ac.uk/courses/c/section3_9.html (2 of 3) [6/23/2010 11:20:39 AM]

Page 50: C baics

Functions in C

● Recursive Functions

Next: Scope of Function Up: C Programming Previous: The goto Statement

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_9.html (3 of 3) [6/23/2010 11:20:39 AM]

Page 51: C baics

Scope of Function Variables

Next: Modifying Function Arguments Up: Functions in C Previous: Functions in C

Scope of Function Variables

Only a limited amount of information is available within each function. Variables declared within the calling function can't be accessed unless they are passed to the called function as arguments. The only other contact a function might have with the outside world is through global variables.

Local variables are declared within a function. They are created anew each time the function is called, and destroyed on return from the function. Values passed to the function as arguments can also be treated like local variables.

Static variables are slightly different, they don't die on return from the function. Instead their last value is retained, and it becomes available when the function is called again.

Global variables don't die on return from a function. Their value is retained, and is available to any other function which accesses them.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_9_1.html [6/23/2010 11:20:40 AM]

Page 52: C baics

Modifying Function Arguments

Next: Pointers in C Up: Functions in C Previous: Scope of Function

Modifying Function Arguments

Some functions work by modifying the values of their arguments. This may be done to pass more than one value back to the calling routine, or because the return value is already being used in some way. C requires special arrangements for arguments whose values will be changed.

You can treat the arguments of a function as variables, however direct manipulation of these arguments won't change the values of the arguments in the calling function. The value passed to the function is a copy of the calling value. This value is stored like a local variable, it disappears on return from the function.

There is a way to change the values of variables declared outside the function. It is done by passing the addresses of variables to the function. These addresses, or pointers, behave a bit like integer types, except that only a limited number of arithmetic operators can be applied to them. They are declared differently to normal types, and we are rarely interested in the value of a pointer. It is what lies at the address which the pointer references which interests us.

To get back to our original function, we pass it the address of a variable whose value we wish to change. The function must now be written to use the value at that address (or at the end of the pointer). On return from the function, the desired value will have changed. We manipulate the actual value using a copy of the pointer.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_9_2.html [6/23/2010 11:20:40 AM]

Page 53: C baics

Pointers in C

Next: Arrays and Pointers Up: Functions in C Previous: Modifying Function Arguments

Pointers in C

Pointers are not exclusive to functions, but this seems a good place to introduce the pointer type.

Imagine that we have an int called i. Its address could be represented by the symbol &i. If the pointer is to be stored as a variable, it should be stored like this.

int *pi = &i;

int * is the notation for a pointer to an int. & is the operator which returns the address of its argument. When it is used, as in &i we say it is referencing i.

The opposite operator, which gives the value at the end of the pointer is *. An example of use, known as de-referencing pi, would be

i = *pi;

Take care not to confuse the many uses of the * sign; Multiplication, pointer declaration and pointer de-referencing.

This is a very confusing subject, so let us illustrate it with an example. The following function fiddle takes two arguments, x is an int while y is a pointer to int. It changes both values.

fiddle(int x, int *y){ printf(" Starting fiddle: x = %d, y = %d\n", x, *y); x ++; (*y)++; printf("Finishing fiddle: x = %d, y = %d\n", x, *y);}

since y is a pointer, we must de-reference it before incrementing its value.

A very simple program to call this function might be as follows.

main()

http://www2.its.strath.ac.uk/courses/c/subsection3_9_3.html (1 of 2) [6/23/2010 11:20:41 AM]

Page 54: C baics

Pointers in C

{ int i = 0; int j = 0;

printf(" Starting main : i = %d, j = %d\n", i, j); printf("Calling fiddle now\n");. fiddle(i, &j); printf("Returned from fiddle\n"); printf("Finishing main : i = %d, j = %d\n", i, j);}

Note here how a pointer to int is created using the & operator within the call fiddle(i, &j);.

The result of running the program will look like this.

Starting main : i = 0 ,j = 0Calling fiddle now Starting fiddle: x = 0, y = 0Finishing fiddle: x = 1, y = 1Returned from fiddleFinishing main : i = 0, j = 1

After the return from fiddle the value of i is unchanged while j, which was passed as a pointer, has changed.

To summarise, if you wish to use arguments to modify the value of variables from a function, these arguments must be passed as pointers, and de-referenced within the function.

Where the value of an argument isn't modified, the value can be passed without any worries about pointers.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_9_3.html (2 of 2) [6/23/2010 11:20:41 AM]

Page 55: C baics

http://www2.its.strath.ac.uk/courses/c/subsection3_9_4.html

Next: Recursive Functions Up: Functions in C Previous: Pointers in C

Arrays and Pointers

To fully understand the workings of C you must know that pointers and arrays are related.

An array is actually a pointer to the 0th element of the array. Dereferencing the array name will give the 0th element. This gives us a range of equivalent notations for array access. In the following examples, arr is an array.

There are some differences between arrays and pointers. The array is treated as a constant in the function where it is declared. This means that we can modify the values in the array, but not the array itself, so statements like arr ++ are illegal, but arr[n] ++ is legal.

Since an array is like a pointer, we can pass an array to a function, and modify elements of that array without having to worry about referencing and de-referencing. Since the array is implemented as a hidden pointer, all the difficult stuff gets done automatically.

A function which expects to be passed an array can declare that parameter in one of two ways.

Either of these definitions is independent of the size of the array being passed. This is met most frequently in the case of character strings, which are implemented as an array of type char. This could be declared as char string[]; but is most frequently written as char *string; In the same way, the argument vector argv is an array of strings which can be supplied to function main. It can be declared as one of the following.

Don't panic if you find pointers confusing. While you will inevitably meet pointers in the form of strings,

http://www2.its.strath.ac.uk/courses/c/subsection3_9_4.html (1 of 2) [6/23/2010 11:20:42 AM]

Page 56: C baics

http://www2.its.strath.ac.uk/courses/c/subsection3_9_4.html

or as variable arguments for functions, they need not be used in most other simple types of programs.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_9_4.html (2 of 2) [6/23/2010 11:20:42 AM]

Page 57: C baics

Recursive Functions

Next: Input and Output Up: Functions in C Previous: Arrays and Pointers

Recursive Functions

A recursive function is one which calls itself. This is another complicated idea which you are unlikely to meet frequently. We shall provide some examples to illustrate recursive functions.

Recursive functions are useful in evaluating certain types of mathematical function. You may also encounter certain dynamic data structures such as linked lists or binary trees. Recursion is a very useful way of creating and accessing these structures.

Here is a recursive version of the Fibonacci function. We saw a non recursive version of this earlier.

int fib(int num)/* Fibonacci value of a number */{ switch(num) { case 0: return(0); break; case 1: return(1); break; default: /* Including recursive calls */ return(fib(num - 1) + fib(num - 2)); break; }}

We met another function earlier called power. Here is an alternative recursive version.

double power(double val, unsigned pow){ if(pow == 0) /* pow(x, 0) returns 1 */ return(1.0); else return(power(val, pow - 1) * val);}

http://www2.its.strath.ac.uk/courses/c/subsection3_9_5.html (1 of 2) [6/23/2010 11:20:43 AM]

Page 58: C baics

Recursive Functions

Notice that each of these definitions incorporate a test. Where an input value gives a trivial result, it is returned directly, otherwise the function calls itself, passing a changed version of the input values. Care must be taken to define functions which will not call themselves indefinitely, otherwise your program will never finish.

The definition of fib is interesting, because it calls itself twice when recursion is used. Consider the effect on program performance of such a function calculating the fibonacci function of a moderate size number.

If such a function is to be called many times, it is likely to have an adverse effect on program performance.

Don't be frightened by the apparent complexity of recursion. Recursive functions are sometimes the simplest answer to a calculation. However there is always an alternative non-recursive solution available too. This will normally involve the use of a loop, and may lack the elegance of the recursive solution.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_9_5.html (2 of 2) [6/23/2010 11:20:43 AM]

Page 59: C baics

Input and Output

Next: The Standard Input Up: C Programming Previous: Recursive Functions

Input and OutputInput and output are covered in some detail. C allows quite precise control of these. This section discusses input and output from keyboard and screen.

The same mechanisms can be used to read or write data from and to files. It is also possible to treat character strings in a similar way, constructing or analysing them and storing results in variables. These variants of the basic input and output commands are discussed in the next section

● The Standard Input Output File ● Character Input / Output

❍ getchar ❍ putchar

● Formatted Input / Output ❍ printf ❍ scanf

● Whole Lines of Input and Output ❍ gets ❍ puts

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_10.html [6/23/2010 11:20:44 AM]

Page 60: C baics

The Standard Input Output File

Next: Character Input / Up: Input and Output Previous: Input and Output

The Standard Input Output File

UNIX supplies a standard package for performing input and output to files or the terminal. This contains most of the functions which will be introduced in this section, along with definitions of the datatypes required to use them. To use these facilities, your program must include these definitions by adding the line This is done by adding the line

#include <stdio.h>

near the start of the program file.

If you do not do this, the compiler may complain about undefined functions or datatypes.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_10_1.html [6/23/2010 11:20:45 AM]

Page 61: C baics

Character Input / Output

Next: getchar Up: Input and Output Previous: The Standard Input

Character Input / Output

This is the lowest level of input and output. It provides very precise control, but is usually too fiddly to be useful. Most computers perform buffering of input and output. This means that they'll not start reading any input until the return key is pressed, and they'll not print characters on the terminal until there is a whole line to be printed.

● getchar ● putchar

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_10_2.html [6/23/2010 11:20:45 AM]

Page 62: C baics

getchar

Next: putchar Up: Character Input / Previous: Character Input /

getchar

getchar returns the next character of keyboard input as an int. If there is an error then EOF (end of file) is returned instead. It is therefore usual to compare this value against EOF before using it. If the return value is stored in a char, it will never be equal to EOF, so error conditions will not be handled correctly.

As an example, here is a program to count the number of characters read until an EOF is encountered. EOF can be generated by typing Control - d.

#include <stdio.h>

main() { int ch, i = 0;

while((ch = getchar()) != EOF) i ++;

printf("%d\n", i); }

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_10_2_1.html [6/23/2010 11:20:46 AM]

Page 63: C baics

putchar

Next: Formatted Input / Up: Character Input / Previous: getchar

putchar

putchar puts its character argument on the standard output (usually the screen).

The following example program converts any typed input into capital letters. To do this it applies the function toupper from the character conversion library ctype.h to each character in turn.

#include <ctype.h> /* For definition of toupper */ #include <stdio.h> /* For definition of getchar, putchar, EOF */

main() { int ch;

while((ch = getchar()) != EOF) putchar(toupper(ch)); }

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_10_2_2.html [6/23/2010 11:20:46 AM]

Page 64: C baics

Formatted Input / Output

Next: printf Up: Input and Output Previous: putchar

Formatted Input / Output

We have met these functions earlier in the course. They are closest to the facilities offered by Pascal or Fortran, and usually the easiest to use for input and output. The versions offered under C are a little more detailed, offering precise control of layout.

● printf ● scanf

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_10_3.html [6/23/2010 11:20:47 AM]

Page 65: C baics

printf

Next: scanf Up: Formatted Input / Previous: Formatted Input /

printf

This offers more structured output than putchar. Its arguments are, in order; a control string, which controls what gets printed, followed by a list of values to be substituted for entries in the control string.

There are several more types available. For full details type

man printf

on your UNIX system.

It is also possible to insert numbers into the control string to control field widths for values to be displayed. For example %6d would print a decimal value in a field 6 spaces wide, %8.2f would print a real value in a field 8 spaces wide with room to show 2 decimal places. Display is left justified by default, but can be right justified by putting a - before the format information, for example %-6d, a decimal integer right justified in a 6 space field.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_10_3_1.html [6/23/2010 11:20:48 AM]

Page 66: C baics

scanf

Next: Whole Lines of Up: Formatted Input / Previous: printf

scanf

scanf allows formatted reading of data from the keyboard. Like printf it has a control string, followed by the list of items to be read. However scanf wants to know the address of the items to be read, since it is a function which will change that value. Therefore the names of variables are preceeded by the & sign. Character strings are an exception to this. Since a string is already a character pointer, we give the names of string variables unmodified by a leading &.

Control string entries which match values to be read are preceeded by the percentage sign in a similar way to their printf equivalents.

Type man scanf for details of all options on your system.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_10_3_2.html [6/23/2010 11:20:48 AM]

Page 67: C baics

Whole Lines of Input and Output

Next: gets Up: Input and Output Previous: scanf

Whole Lines of Input and Output

Where we are not too interested in the format of our data, or perhaps we cannot predict its format in advance, we can read and write whole lines as character strings. This approach allows us to read in a line of input, and then use various string handling functions to analyse it at our leisure.

● gets ● puts

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_10_4.html [6/23/2010 11:20:49 AM]

Page 68: C baics

gets

Next: puts Up: Whole Lines of Previous: Whole Lines of

gets

gets reads a whole line of input into a string until a newline or EOF is encountered. It is critical to ensure that the string is large enough to hold any expected input lines.

When all input is finished, NULL as defined in stdio.h is returned.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_10_4_1.html [6/23/2010 11:20:49 AM]

Page 69: C baics

puts

Next: Handling Files in Up: Whole Lines of Previous: gets

puts

puts writes a string to the output, and follows it with a newline character.

Example: Program which uses gets and puts to double space typed input.

#include <stdio.h>

main(){ char line[256]; /* Define string sufficiently large to store a line of input */

while(gets(line) != NULL) /* Read line */ { puts(line); /* Print line */ printf("\n"); /* Print blank line */ }}

Note that putchar, printf and puts can be freely used together. So can getchar, scanf and gets.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_10_4_2.html [6/23/2010 11:20:50 AM]

Page 70: C baics

Handling Files in C

Next: UNIX File Redirection Up: C Programming Previous: puts

Handling Files in CThis section describes the use of C's input / output facilities for reading and writing files. There is also a brief description of string handling functions here.

The functions are all variants on the forms of input / output which were introduced in the previous section.

● UNIX File Redirection ● C File Handling - File Pointers

❍ Opening a file pointer using fopen ❍ Standard file pointers in UNIX ❍ Closing a file using fclose

● Input and Output using file pointers ❍ Character Input and Output with Files ❍ Formatted Input Output with File Pointers ❍ Formatted Input Output with Strings ❍ Whole Line Input and Output using File Pointers

● Special Characters ❍ NULL, The Null Pointer or Character ❍ EOF, The End of File Marker

● Other String Handling Functions ● Conclusion

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_11.html [6/23/2010 11:20:50 AM]

Page 71: C baics

UNIX File Redirection

Next: C File Handling Up: Handling Files in Previous: Handling Files in

UNIX File Redirection

UNIX has a facility called redirection which allows a program to access a single input file and a single output file very easily. The program is written to read from the keyboard and write to the terminal screen as normal.

To run prog1 but read data from file infile instead of the keyboard, you would type

prog1 < infile

To run prog1 and write data to outfile instead of the screen, you would type

prog1 > outfile

Both can also be combined as in

prog1 < infile > outfile

Redirection is simple, and allows a single program to read or write data to or from files or the screen and keyboard.

Some programs need to access several files for input or output, redirection cannot do this. In such cases you will have to use C's file handling facilities.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_11_1.html [6/23/2010 11:20:51 AM]

Page 72: C baics

C File Handling - File Pointers

Next: Opening a file Up: Handling Files in Previous: UNIX File Redirection

C File Handling - File Pointers

C communicates with files using a new datatype called a file pointer. This type is defined within stdio.h, and written as FILE *. A file pointer called output_file is declared in a statement like

FILE *output_file;

● Opening a file pointer using fopen ● Standard file pointers in UNIX ● Closing a file using fclose

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_11_2.html [6/23/2010 11:20:51 AM]

Page 73: C baics

Opening a file pointer using fopen

Next: Standard file pointers Up: C File Handling Previous: C File Handling

Opening a file pointer using fopen

Your program must open a file before it can access it. This is done using the fopen function, which returns the required file pointer. If the file cannot be opened for any reason then the value NULL will be returned. You will usually use fopen as follows

if ((output_file = fopen("output_file", "w")) == NULL) fprintf(stderr, "Cannot open %s\n", "output_file");

fopen takes two arguments, both are strings, the first is the name of the file to be opened, the second is an access character, which is usually one of:

As usual, use the man command for further details by typing man fopen.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_11_2_1.html [6/23/2010 11:20:52 AM]

Page 74: C baics

Standard file pointers in UNIX

Next: Closing a file Up: C File Handling Previous: Opening a file

Standard file pointers in UNIX

UNIX systems provide three file descriptors which are automatically open to all C programs. These are

Since these files are already open, there is no need to use fopen on them.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_11_2_2.html [6/23/2010 11:20:53 AM]

Page 75: C baics

Closing a file using fclose

Next: Input and Output Up: C File Handling Previous: Standard file pointers

Closing a file using fclose

The fclose command can be used to disconnect a file pointer from a file. This is usually done so that the pointer can be used to access a different file. Systems have a limit on the number of files which can be open simultaneously, so it is a good idea to close a file when you have finished using it.

This would be done using a statement like

fclose(output_file);

If files are still open when a program exits, the system will close them for you. However it is usually better to close the files properly.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_11_2_3.html [6/23/2010 11:20:53 AM]

Page 76: C baics

Input and Output using file pointers

Next: Character Input and Up: Handling Files in Previous: Closing a file

Input and Output using file pointers

Having opened a file pointer, you will wish to use it for either input or output. C supplies a set of functions to allow you to do this. All are very similar to input and output functions that you have already met.

● Character Input and Output with Files ● Formatted Input Output with File Pointers ● Formatted Input Output with Strings ● Whole Line Input and Output using File Pointers

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_11_3.html [6/23/2010 11:20:54 AM]

Page 77: C baics

Character Input and Output with Files

Next: Formatted Input Output Up: Input and Output Previous: Input and Output

Character Input and Output with Files

This is done using equivalents of getchar and putchar which are called getc and putc. Each takes an extra argument, which identifies the file pointer to be used for input or output.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_11_3_1.html [6/23/2010 11:20:55 AM]

Page 78: C baics

Formatted Input Output with File Pointers

Next: Formatted Input Output Up: Input and Output Previous: Character Input and

Formatted Input Output with File Pointers

Similarly there are equivalents to the functions printf and scanf which read or write data to files. These are called fprintf and fscanf. You have already seen fprintf being used to write data to stderr.

The functions are used in the same way, except that the fprintf and fscanf take the file pointer as an additional first argument.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_11_3_2.html [6/23/2010 11:20:55 AM]

Page 79: C baics

Formatted Input Output with Strings

Next: Whole Line Input Up: Input and Output Previous: Formatted Input Output

Formatted Input Output with Strings

These are the third set of the printf and scanf families. They are called sprintf and sscanf.

sprintf puts formatted data into a string which must have sufficient space allocated to hold it. This can be done by declaring it as an array of char. The data is formatted according to a control string of the same form as that for p rintf.

sscanf takes data from a string and stores it in other variables as specified by the control string. This is done in the same way that scanf reads input data into variables. sscanf is very useful for converting strings into numeric v values.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_11_3_3.html [6/23/2010 11:20:56 AM]

Page 80: C baics

Whole Line Input and Output using File Pointers

Next: Special Characters Up: Input and Output Previous: Formatted Input Output

Whole Line Input and Output using File Pointers

Predictably, equivalents to gets and puts exist called fgets and fputs. The programmer should be careful in using them, since they are incompatible with gets and puts. gets requires the programmer to specify the maximum number of characters to be read. fgets and fputs retain the trailing newline character on the line they read or write, wheras gets and puts discard the newline.

When transferring data from files to standard input / output channels, the simplest way to avoid incompatibility with the newline is to use fgets and fputs for files and standard channels too.

For Example, read a line from the keyboard using

fgets(data_string, 80, stdin);

and write a line to the screen using

fputs(data_string, stdout);

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_11_3_4.html [6/23/2010 11:20:56 AM]

Page 81: C baics

Special Characters

Next: NULLThe Null Up: Handling Files in Previous: Whole Line Input

Special Characters

C makes use of some 'invisible' characters which have already been mentioned. However a fuller description seems appropriate here.

● NULL, The Null Pointer or Character ● EOF, The End of File Marker

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_11_4.html [6/23/2010 11:20:57 AM]

Page 82: C baics

NULL, The Null Pointer or Character

Next: EOFThe End Up: Special Characters Previous: Special Characters

NULL, The Null Pointer or Character

NULL is a character or pointer value. If a pointer, then the pointer variable does not reference any object (i.e. a pointer to nothing). It is usual for functions which return pointers to return NULL if they failed in some way. The return value can be tested. See the section on fopen for an example of this.

NULL is returned by read commands of the gets family when they try to read beyond the end of an input file.

Where it is used as a character, NULL is commonly written as '\0'. It is the string termination character which is automatically appended to any strings in your C program. You usually need not bother about this final \0', since it is handled automatically. However it sometimes makes a useful target to terminate a string search. There is an example of this in the string_length function example in the section on Functions in C.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_11_4_1.html [6/23/2010 11:20:57 AM]

Page 83: C baics

EOF, The End of File Marker

Next: Other String Handling Up: Special Characters Previous: NULLThe Null

EOF, The End of File Marker

EOF is a character which indicates the end of a file. It is returned by read commands of the getc and scanf families when they try to read beyond the end of a file.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_11_4_2.html [6/23/2010 11:20:58 AM]

Page 84: C baics

Other String Handling Functions

Next: Conclusion Up: Handling Files in Previous: EOFThe End

Other String Handling Functions

As well as sprintf and sscanf, the UNIX system has a number of other string handling functions within its libraries. A number of the most useful ones are contained in the <strings.h> file, and are made available by putting the line

#include <strings.h>

near to the head of your program file.

A couple of the functions are described below.

A full list of these functions can be seen using the man command by typing

man 3 strings

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_11_5.html [6/23/2010 11:20:59 AM]

Page 85: C baics

Conclusion

Next: Structures in C Up: Handling Files in Previous: Other String Handling

Conclusion

The variety of different types of input and output, using standard input or output, files or character strings make C a very powerful language. The addition of character input and output make it highly suitable for applications where the format of data must be controlled very precisely.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_11_6.html [6/23/2010 11:20:59 AM]

Page 86: C baics

Structures in C

Next: Defining a Structure Up: C Programming Previous: Conclusion

Structures in CA structure is a collection of variables under a single name. These variables can be of different types, and each has a name which is used to select it from the structure. A structure is a convenient way of grouping several pieces of related information together.

A structure can be defined as a new named type, thus extending the number of available types. It can use other structures, arrays or pointers as some of its members, though this can get complicated unless you are careful.

● Defining a Structure ● Accessing Members of a Structure ● Structures as Function Arguments ● Further Uses of Structures

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_12.html [6/23/2010 11:21:00 AM]

Page 87: C baics

Defining a Structure

Next: Accessing Members of Up: Structures in C Previous: Structures in C

Defining a Structure

A structure type is usually defined near to the start of a file using a typedef statement. typedef defines and names a new type, allowing its use throughout the program. typedefs usually occur just after the #define and #include statements in a file.

Here is an example structure definition.

typedef struct { char name[64]; char course[128]; int age; int year; } student;

This defines a new type student variables of type student can be declared as follows.

student st_rec;

Notice how similar this is to declaring an int or float.

The variable name is st_rec, it has members called name, course, age and year.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_12_1.html [6/23/2010 11:21:00 AM]

Page 88: C baics

Accessing Members of a Structure

Next: Structures as Function Up: Structures in C Previous: Defining a Structure

Accessing Members of a Structure

Each member of a structure can be used just like a normal variable, but its name will be a bit longer. To return to the examples above, member name of structure st_rec will behave just like a normal array of char, however we refer to it by the name

st_rec.name

Here the dot is an operator which selects a member from a structure.

Where we have a pointer to a structure we could dereference the pointer and then use dot as a member selector. This method is a little clumsy to type. Since selecting a member from a structure pointer happens frequently, it has its own operator -> which acts as follows. Assume that st_ptr is a pointer to a structure of type student We would refer to the name member as

st_ptr -> name

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_12_2.html [6/23/2010 11:21:01 AM]

Page 89: C baics

Structures as Function Arguments

Next: Further Uses of Up: Structures in C Previous: Accessing Members of

Structures as Function Arguments

A structure can be passed as a function argument just like any other variable. This raises a few practical issues.

Where we wish to modify the value of members of the structure, we must pass a pointer to that structure. This is just like passing a pointer to an int type argument whose value we wish to change.

If we are only interested in one member of a structure, it is probably simpler to just pass that member. This will make for a simpler function, which is easier to re-use. Of course if we wish to change the value of that member, we should pass a pointer to it.

When a structure is passed as an argument, each member of the structure is copied. This can prove expensive where structures are large or functions are called frequently. Passing and working with pointers to large structures may be more efficient in such cases.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_12_3.html [6/23/2010 11:21:01 AM]

Page 90: C baics

Further Uses of Structures

Next: The C Preprocessor Up: Structures in C Previous: Structures as Function

Further Uses of Structures

As we have seen, a structure is a good way of storing related data together. It is also a good way of representing certain types of information. Complex numbers in mathematics inhabit a two dimensional plane (stretching in real and imaginary directions). These could easily be represented here by

typedef struct { double real; double imag; } complex;

doubles have been used for each field because their range is greater than floats and because the majority of mathematical library functions deal with doubles by default.

In a similar way, structures could be used to hold the locations of points in multi-dimensional space. Mathematicians and engineers might see a storage efficient implementation for sparse arrays here.

Apart from holding data, structures can be used as members of other structures. Arrays of structures are possible, and are a good way of storing lists of data with regular fields, such as databases.

Another possibility is a structure whose fields include pointers to its own type. These can be used to build chains (programmers call these linked lists), trees or other connected structures. These are rather daunting to the new programmer, so we won't deal with them here.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_12_4.html [6/23/2010 11:21:01 AM]

Page 91: C baics

The C Preprocessor

Next: Using #define to Up: C Programming Previous: Further Uses of

The C PreprocessorThe C preprocessor is a tool which filters your source code before it is compiled. The preprocessor allows constants to be named using the #define notation. The preprocessor provides several other facilities which will be described here. It is particularly useful for selecting machine dependent pieces of code for different computer types, allowing a single program to be compiled and run on several different computers.

The C preprocessor isn't restricted to use with C programs, and programmers who use other languages may also find it useful, however it is tuned to recognise features of the C language like comments and strings, so its use may be restricted in other circu mstances.

The preprocessor is called cpp, however it is called automatically by the compiler so you will not need to call it while programming in C.

● Using #define to Implement Constants ● Using #define to Create Functional Macros ● Reading in Other Files using #include ● Conditional selection of code using #ifdef

❍ Using #ifdef for Different Computer Types ❍ Using #ifdef to Temporarily Remove Program Statements

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_13.html [6/23/2010 11:21:02 AM]

Page 92: C baics

Using #define to Implement Constants

Next: Using #define to Up: The C Preprocessor Previous: The C Preprocessor

Using #define to Implement Constants

We have already met this facility, in its simplest form it allows us to define textual substitutions as follows.

#define MAXSIZE 256

This will lead to the value 256 being substituted for each occurrence of the word MAXSIZE in the file.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_13_1.html [6/23/2010 11:21:02 AM]

Page 93: C baics

Using #define to Create Functional Macros

Next: Reading in Other Up: The C Preprocessor Previous: Using #define to

Using #define to Create Functional Macros

#define can also be given arguments which are used in its replacement. The definitions are then called macros. Macros work rather like functions, but with the following minor differences.

● Since macros are implemented as a textual substitution, there is no effect on program performance (as with functions).

● Recursive macros are generally not a good idea. ● Macros don't care about the type of their arguments. Hence macros are a good choice where we

might want to operate on reals, integers or a mixture of the two. Programmers sometimes call such type flexibility polymorphism.

● Macros are generally fairly small.

Macros are full of traps for the unwary programmer. In particular the textual substitution means that arithmetic expressions are liable to be corrupted by the order of evaluation rules.

Here is an example of a macro which won't work.

#define DOUBLE(x) x+x

Now if we have a statement

a = DOUBLE(b) * c;

This will be expanded to

a = b+b * c;

And since * has a higher priority than +, the compiler will treat it as.

a = b + (b * c);

The problem can be solved using a more robust definition of DOUBLE

#define DOUBLE(x) (x+x)

http://www2.its.strath.ac.uk/courses/c/subsection3_13_2.html (1 of 2) [6/23/2010 11:21:03 AM]

Page 94: C baics

Using #define to Create Functional Macros

Here the brackets around the definition force the expression to be evaluated before any surrounding operators are applied. This should make the macro more reliable.

In general it is better to write a C function than risk using a macro.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_13_2.html (2 of 2) [6/23/2010 11:21:03 AM]

Page 95: C baics

Reading in Other Files using #include

Next: Conditional selection of Up: The C Preprocessor Previous: Using #define to

Reading in Other Files using #include

The preprocessor directive #include is an instruction to read in the entire contents of another file at that point. This is generally used to read in header files for library functions. Header files contain details of functions and types used within the library. They must be included before the program can make use of the library functions.

Library header file names are enclosed in angle brackets, < >. These tell the preprocessor to look for the header file in the standard location for library definitions. This is /usr/include for most UNIX systems.

For example

#include <stdio.h>

Another use for #include for the programmer is where multi-file programs are being written. Certain information is required at the beginning of each program file. This can be put into a file called globals.h and included in each program file. Local header file names are usually enclosed by double quotes, " ". It is conventional to give header files a name which ends in .h to distinguish them from other types of file.

Our globals.h file would be included by the following line.

#include "globals.h"

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_13_3.html [6/23/2010 11:21:03 AM]

Page 96: C baics

Conditional selection of code using #ifdef

Next: Using #ifdef for Up: The C Preprocessor Previous: Reading in Other

Conditional selection of code using #ifdef

The preprocessor has a conditional statement similar to C's if else. It can be used to selectively include statements in a program. This is often used where two different computer types implement a feature in different ways. It allows the programmer to produce a program which will run on either type.

The keywords for conditional selection are; #ifdef, #else and #endif.

#ifdef takes a name as an argument, and returns true if the the name has a current definition. The name may be defined using a #define, the -d option of the compiler, or certain names which are automatically defined by the UNIX environment.

#else is optional and ends the block beginning with #ifdef. It is used to create a 2 way optional selection.

#endif ends the block started by #ifdef or #else.

Where the #ifdef is true, statements between it and a following #else or #endif are included in the program. Where it is false, and there is a following #else, statements between the #else and the following #endif are included.

This is best illustrated by an example.

● Using #ifdef for Different Computer Types ● Using #ifdef to Temporarily Remove Program Statements

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_13_4.html [6/23/2010 11:21:04 AM]

Page 97: C baics

Using #ifdef for Different Computer Types

Next: Using #ifdef to Up: Conditional selection of Previous: Conditional selection of

Using #ifdef for Different Computer Types

Conditional selection is rarely performed using #defined values. A simple application using machine dependent values is illustrated below.

#include <stdio.h>

main(){#ifdef vax printf("This is a VAX\n");#endif#ifdef sun printf("This is a SUN\n");#endif}

sun is defined automatically on SUN computers. vax is defined automatically on VAX computers.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_13_4_1.html [6/23/2010 11:21:04 AM]

Page 98: C baics

Using #ifdef to Temporarily Remove Program Statements

Next: Programs with Several Up: Conditional selection of Previous: Using #ifdef for

Using #ifdef to Temporarily Remove Program Statements

#ifdef also provides a useful means of temporarily `blanking out' lines of a program. The lines in question are preceeded by #ifdef NEVER and followed by #endif. Of course you should ensure that the name NEVER isn't defined anywhere.

The preprocessor has several other useful facilities. If you are interested in these you can read more by typing

man cpp

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_13_4_2.html [6/23/2010 11:21:05 AM]

Page 99: C baics

Programs with Several Files

Next: Advantages of Using Up: C Programming Previous: Using #ifdef to

Programs with Several FilesWhen writing a large program, you may find it convenient to split it several source files. This has several advantages, but makes compilation more complicated.

This section will discuss advantages and disadvantages of using several files in a program, and advise you on how to divide a program between several files, should you wish to do so.

● Advantages of Using Several Files ● How to Divide a Program between Several Files ● Organisation of Data in each File ● Compiling Multi-File Programs

❍ Separate Compilation ● Using make with Multi-File Programs

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_14.html [6/23/2010 11:21:05 AM]

Page 100: C baics

Advantages of Using Several Files

Next: How to Divide Up: Programs with Several Previous: Programs with Several

Advantages of Using Several Files

The main advantages of spreading a program across several files are:

● Teams of programmers can work on programs. Each programmer works on a different file.

● An object oriented style can be used. Each file defines a particular type of object as a datatype and operations on that object as functions. The implementation of the object can be kept private from the rest of the program. This makes for well structured programs which are easy to maintain.

● Files can contain all functions from a related group. For Example all matrix operations. These can then be accessed like a function library.

● Well implemented objects or function definitions can be re-used in other programs, reducing development time.

● In very large programs each major function can occupy a file to itself. Any lower level functions used to implement them can be kept in the same file. Then programmers who call the major function need not be distracted by all the lower level work.

● When changes are made to a file, only that file need be re-compiled to rebuild the program. The UNIX make facility is very useful for rebuilding multi-file programs in this way.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_14_1.html [6/23/2010 11:21:06 AM]

Page 101: C baics

How to Divide a Program between Several Files

Next: Organisation of Data Up: Programs with Several Previous: Advantages of Using

How to Divide a Program between Several Files

Where a function is spread over several files, each file will contain one or more functions. One file will include main while the others will contain functions which are called by others. These other files can be treated as a library of functions.

Programmers usually start designing a program by dividing the problem into easily managed sections. Each of these sections might be implemented as one or more functions. All functions from each section will usually live in a single file.

Where objects are implemented as data structures, it is usual to to keep all functions which access that object in the same file. The advantages of this are;

● The object can easily be re-used in other programs. ● All related functions are stored together. ● Later changes to the object require only one file to be modified.

Where the file contains the definition of an object, or functions which return values, there is a further restriction on calling these functions from another file. Unless functions in another file are told about the object or function definitions, they will be unable to compile them correctly.

The best solution to this problem is to write a header file for each of the C files. This will have the same name as the C file, but ending in .h. The header file contains definitions of all the functions used in the C file.

Whenever a function in another file calls a function from our C file, it can define the function by making a #include of the appropriate .h file.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_14_2.html [6/23/2010 11:21:06 AM]

Page 102: C baics

Organisation of Data in each File

Next: Compiling Multi-File Programs Up: Programs with Several Previous: How to Divide

Organisation of Data in each File

Any file must have its data organised in a certain order. This will typically be:

1. A preamble consisting of #defined constants, #included header files and typedefs of important datatypes.

2. Declaration of global and external variables. Global variables may also be initialised here. 3. One or more functions.

The order of items is important, since every object must be defined before it can be used. Functions which return values must be defined before they are called. This definition might be one of the following:

● Where the function is defined and called in the same file, a full declaration of the function can be placed ahead of any call to the function.

● If the function is called from a file where it is not defined, a prototype should appear before the call to the function.

A function defined as

float find_max(float a, float b, float c) { /* etc ... ... */

would have a prototype of

float find_max(float a, float b, float c);

The prototype may occur among the global variables at the start of the source file. Alternatively it may be declared in a header file which is read in using a #include.

It is important to remember that all C objects should be declared before use.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_14_3.html [6/23/2010 11:21:07 AM]

Page 103: C baics

Compiling Multi-File Programs

Next: Separate Compilation Up: Programs with Several Previous: Organisation of Data

Compiling Multi-File Programs

This process is rather more involved than compiling a single file program. Imagine a program in three files prog.c, containing main(), func1.c and func2.c. The simplest method of compilation (to produce a runnable file called a.out) would be

cc prog.c func1.c func2.c

If we wanted to call the runnable file prog we would have to type

cc prog.c func1.c func2.c -o prog

In these examples, each of the .c files is compiled, and then they are automatically linked together using a program called the loader ld.

● Separate Compilation

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_14_4.html [6/23/2010 11:21:07 AM]

Page 104: C baics

Separate Compilation

Next: Using make with Up: Compiling Multi-File Programs Previous: Compiling Multi-File Programs

Separate Compilation

We can also compile each C file separately using the cc -c option. This produces an object file with the same name, but ending in .o. After this, the object files are linked using the linker. This would require the four following commands for our current example.

cc -c prog.c cc -c func1.c cc -c func2.c ld prog.o func1.o func2.o -o prog

Each file is compiled before the object files are linked to give a runnable file.

The advantage of separate compilation is that only files which have been edited since the last compilation need to be re-compiled when re-building the program. For very large programs this can save a lot of time.

The make utility is designed to automate this re-building process. It checks the times of modification of files, and selectively re-compiles as required. It is an excellent tool for maintaining multi-file programs. Its use is recommended when building multi-file programs.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_14_4_1.html [6/23/2010 11:21:08 AM]

Page 105: C baics

Using make with Multi-File Programs

Next: UNIX Library Functions Up: Programs with Several Previous: Separate Compilation

Using make with Multi-File Programs

We have already used make to build single file programs. It was really designed to help build large multi-file programs. Its use will be described here.

Make knows about `dependencies' in program building. For example;

● We can get prog.o by running cc -c prog.c. ● This need only be done if prog.c changed more recently than prog.o.

make is usually used with a configuration file called Makefile which describes the structure of the program. This includes the name of the runnable file, and the object files to be linked to create it. Here is a sample Makefile for our current example

# Sample Makefile for prog # # prog is built from prog.c func1.c func2.c #

# Object files (Ending in .o, # these are compiled from .c files by make) OBJS = prog.o func1.o func2.o

# Prog is generated from the object files prog: $(OBJS) $(CC) $(CFLAGS) -o prog $(OBJS) # ^^^ This space must be a TAB. # Above line is an instruction to link object files

This looks cluttered, but ignore the comments (lines starting with #) andthere are just 3 lines.

When make is run, Makefile is searched for a list of dependencies. The compiler is involved to create .o files where needed. The link statement is then used to create the runnable file.

make re-builds the whole program with a minimum of re-compilation, and ensures that all parts of the program are up to date. It has many other features, some of which are very complicated.

http://www2.its.strath.ac.uk/courses/c/subsection3_14_5.html (1 of 2) [6/23/2010 11:21:08 AM]

Page 106: C baics

Using make with Multi-File Programs

For a full description of all of these features, look at the manual page for make by typing

man make

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_14_5.html (2 of 2) [6/23/2010 11:21:08 AM]

Page 107: C baics

UNIX Library Functions

Next: Finding Information about Up: C Programming Previous: Using make with

UNIX Library FunctionsThe UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others are very specialised in their application.

Wise programmers will check whether a library function is available to perform a task before writing their own version. This will reduce program development time. The library functions have been tested, so they are more likely to be correct than any function which the programmer might write. This will save time when debugging the program.

● Finding Information about Library Functions ● Use of Library Functions ● Some Useful Library Functions

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_15.html [6/23/2010 11:21:09 AM]

Page 108: C baics

http://www2.its.strath.ac.uk/courses/c/subsection3_15_1.html

Next: Use of Library Up: UNIX Library Functions Previous: UNIX Library Functions

Finding Information about Library Functions

The UNIX manual has an entry for all available functions. Function documentation is stored in section 3 of the manual, and there are many other useful system calls in section 2. If you already know the name of the function you want, you can read the page by typing (to find about strcat).

man 3 strcat

If you don't know the name of the function, a full list is included in the introductory page for section 3 of the manual. To read this, type

man 3 intro

There are approximately 700 functions described here. This number tends to increase with each upgrade of the system.

On any manual page, the SYNOPSIS section will include information on the use of the function. For example

#include <time.h>

char *ctime(time_t *clock)

This means that you must have

#include <time.h>

in your file before you call ctime. And that function ctime takes a pointer to type time_t as an argument, and returns a string (char *). time_t will probably be defined in the same manual page.

The DESCRIPTION section will then give a short description of what the function does. For example

ctime() converts a long integer, pointed to by clock, to a 26-character string of the form produced by asctime().

http://www2.its.strath.ac.uk/courses/c/subsection3_15_1.html (1 of 2) [6/23/2010 11:21:09 AM]

Page 109: C baics

http://www2.its.strath.ac.uk/courses/c/subsection3_15_1.html

Further related reading is suggested in the SEE ALSO section.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_15_1.html (2 of 2) [6/23/2010 11:21:09 AM]

Page 110: C baics

Use of Library Functions

Next: Some Useful Library Up: UNIX Library Functions Previous: Finding Information about

Use of Library Functions

To use a function, ensure that you have made the required #includes in your C file. Then the function can be called as though you had defined it yourself.

It is important to ensure that your arguments have the expected types, otherwise the function will probably produce strange results. lint is quite good at checking such things.

Some libraries require extra options before the compiler can support their use. For example, to compile a program including functions from the math.h library the command might be

cc mathprog.c -o mathprog -lm

The final -lm is an instruction to link the maths library with the program. The manual page for each function will usually inform you if any special compiler flags are required.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_15_2.html [6/23/2010 11:21:10 AM]

Page 111: C baics

Some Useful Library Functions

Next: Precedence of C Up: UNIX Library Functions Previous: Use of Library

Some Useful Library Functions

The following functions may be useful to you. Each manual page typically describes several functions, so if you see something similar to what you want, try looking in that manual page.

To get a full summary type

man 3 intro

at your terminal.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_15_3.html [6/23/2010 11:21:11 AM]

Page 112: C baics

Precedence of C operators

Next: Special Characters Up: C Programming Previous: Some Useful Library

Precedence of C operatorsThe following table shows the precedence of operators in C. Where a statement involves the use of several operators, those with the lowest number in the table will be applied first.

Some of these operators have not been described in this course, consult a textbook if you want details about them.

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_16.html [6/23/2010 11:21:11 AM]

Page 113: C baics

Special Characters

Next: Formatted Input and Up: C Programming Previous: Precedence of C

Special CharactersThe following special patterns are used to represent a single character in C programs. The leading backslash in the single quotes indicates that more information is to follow.

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_17.html [6/23/2010 11:21:12 AM]

Page 114: C baics

Formatted Input and Output Function Types

Next: Some Recommended Books Up: C Programming Previous: Special Characters

Formatted Input and Output Function TypesThe following format strings can be used to specify data to be printed or read using the printf or scanf functions, or any of their variants; fprintf, sprintf, fscanf or sscanf .

Note that though all can be used with the printf variants, some are not available for scanf.

[email protected] Jan 17 11:40:37 GMT 1995

http://www2.its.strath.ac.uk/courses/c/section3_18.html [6/23/2010 11:21:13 AM]

Page 115: C baics

Some Recommended Books

Next: C Language Keywords Up: C Programming Previous: Formatted Input and

Some Recommended BooksApart from these excellent notes, there are many C books to choose from. Try to browse a few in your library or bookshop before buying one. Consider the following when selecting a book:

Price: Can you afford it?

Style: Is the book aimed at your standard of programming skill? Do you understand the points which the author is trying to make?

Accuracy: The book should support the ANSI C standard. Check whether it mentions ANSI, and also the date of the "last revision" (Computer technology changes rapidly, printed textbooks don't change at all).

These are two books which I have found very useful.

The C Programming Language By: Brian W. Kernighan, Dennis M. Ritchie Publishers: Prentice Hall

This book, by the authors of C and the original UNIX system, gives a full and economical description of the language. It is not cluttered with trivial examples, so will not suit the novice programmer. This book is regarded by many as the `standard' work on the C language.

A Book on C By: Al Kelley, Ira Pohl Publishers: Benjamin Cummings

This book gives a good description of C and the UNIX environment. Its style is more tutorial than Kernighan and Ritchie, so it would make a better purchase for the less experienced programmer. Newcomers to programming will find the plentiful examples helpful.

http://www2.its.strath.ac.uk/courses/c/section3_19.html (1 of 2) [6/23/2010 11:21:14 AM]

Page 116: C baics

Some Recommended Books

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_19.html (2 of 2) [6/23/2010 11:21:14 AM]

Page 117: C baics

C Language Keywords

Next: Usable SUN Systems Up: C Programming Previous: Some Recommended Books

C Language KeywordsThe following names are reserved by the C language. Their meaning is already defined, and they cannot be re-defined to mean anything else.

Other than these names, you can choose any names of reasonable length for variables, functions etc. The names must begin with a letter or underscore (letters are better), and then can contain letters, numbers and underscores.

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_20.html [6/23/2010 11:21:16 AM]

Page 118: C baics

Usable SUN Systems

Next: About this document Up: C Programming Previous: C Language Keywords

Usable SUN SystemsThis section is no longer relevant.

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_21.html [6/23/2010 11:21:16 AM]

Page 119: C baics

http://www2.its.strath.ac.uk/courses/c/section3_22.html

About this document ...

Up: C Programming Previous: Usable SUN Systems

About this document ... This document was originally generated using the LaTeX2HTML translator Version 0.5.3 (Wed Jan 26 1994) Copyright © 1993, Nikos Drakos, Computer Based Learning Unit, University of Leeds.

The command line arguments were: latex2html ccourse.tex.

HTML documentation modified and tweaked to correct errors and tidy up presentation, IT Services, 2001.

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_22.html [6/23/2010 11:21:19 AM]

Page 120: C baics

http://www2.its.strath.ac.uk/courses/c/ccourse.html

index.html

http://www2.its.strath.ac.uk/courses/c/ccourse.html [6/23/2010 11:21:19 AM]

Page 121: C baics

http://www2.its.strath.ac.uk/courses/c/tableofcontents3_1.html

Next: Copyright Notice and Up: C Programming Previous: C Programming

Contents

● Contents ● Copyright Notice and Credits ● Introduction

❍ About C ❍ C and UNIX ❍ This Course ❍ Dialects of C

■ Common C ■ ANSI C

● A Quick Overview of C ❍ A Very Simple Program ❍ A Weight Conversion Program ❍ Weight Conversion Table Using a Function ❍ Weight Conversion with a Prompt ❍ Weight Conversion with Command Line Argument ❍ Fibonacci Series Using an Array

● Using C with UNIX ❍ Writing the Program ❍ Compiling the Program

■ The C Compiler (cc) ■ Make, a Program Builder ■ Improved Type Checking Using Lint

❍ Running the Program ● Constant and Variable Types

❍ Variables ❍ Variable Names ❍ Global Variables

■ External Variables ❍ Static Variables ❍ Constants ❍ Arrays

http://www2.its.strath.ac.uk/courses/c/tableofcontents3_1.html (1 of 4) [6/23/2010 11:21:20 AM]

Page 122: C baics

http://www2.its.strath.ac.uk/courses/c/tableofcontents3_1.html

● Expressions and Operators ❍ Assignment Statement ❍ Arithmetic operators ❍ Type conversion ❍ Comparison ❍ Logical Connectors ❍ Summary

● Control Statements ❍ The if else Statement ❍ The switch Statement ❍ Loops ❍ The while Loop ❍ The do while Loop ❍ The for Loop ❍ The break Statement ❍ The continue Statement ❍ The goto Statement

● Functions in C ❍ Scope of Function Variables ❍ Modifying Function Arguments ❍ Pointers in C ❍ Arrays and Pointers ❍ Recursive Functions

● Input and Output ❍ The Standard Input Output File ❍ Character Input / Output

■ getchar ■ putchar

❍ Formatted Input / Output ■ printf ■ scanf

❍ Whole Lines of Input and Output ■ gets ■ puts

● Handling Files in C ❍ UNIX File Redirection ❍ C File Handling - File Pointers

■ Opening a file pointer using fopen

http://www2.its.strath.ac.uk/courses/c/tableofcontents3_1.html (2 of 4) [6/23/2010 11:21:20 AM]

Page 123: C baics

http://www2.its.strath.ac.uk/courses/c/tableofcontents3_1.html

■ Standard file pointers in UNIX ■ Closing a file using fclose

❍ Input and Output using file pointers ■ Character Input and Output with Files ■ Formatted Input Output with File Pointers ■ Formatted Input Output with Strings ■ Whole Line Input and Output using File Pointers

❍ Special Characters ■ NULL, The Null Pointer or Character ■ EOF, The End of File Marker

❍ Other String Handling Functions ❍ Conclusion

● Structures in C ❍ Defining a Structure ❍ Accessing Members of a Structure ❍ Structures as Function Arguments ❍ Further Uses of Structures

● The C Preprocessor ❍ Using #define to Implement Constants ❍ Using #define to Create Functional Macros ❍ Reading in Other Files using #include ❍ Conditional selection of code using #ifdef

■ Using #ifdef for Different Computer Types ■ Using #ifdef to Temporarily Remove Program Statements

● Programs with Several Files ❍ Advantages of Using Several Files ❍ How to Divide a Program between Several Files ❍ Organisation of Data in each File ❍ Compiling Multi-File Programs

■ Separate Compilation ❍ Using make with Multi-File Programs

● UNIX Library Functions ❍ Finding Information about Library Functions ❍ Use of Library Functions ❍ Some Useful Library Functions

● Precedence of C operators ● Special Characters ● Formatted Input and Output Function Types

http://www2.its.strath.ac.uk/courses/c/tableofcontents3_1.html (3 of 4) [6/23/2010 11:21:20 AM]

Page 124: C baics

http://www2.its.strath.ac.uk/courses/c/tableofcontents3_1.html

● Some Recommended Books ● C Language Keywords ● Usable SUN Systems ● About this document ...

January 1995

http://www2.its.strath.ac.uk/courses/c/tableofcontents3_1.html (4 of 4) [6/23/2010 11:21:20 AM]

Page 125: C baics

http://www2.its.strath.ac.uk/courses/c/section3_2.html

Next: Introduction Up: C Programming Previous: Contents

Copyright Notice and Credits© The University of Strathclyde, Glasgow, Scotland.

Permission to copy is granted provided that these credits remain intact.

These notes were written by Steve Holmes of the University of Strathclyde Computer Centre. They originally formed the basis of the Computer Centre's C programming course. Steve no longer works for the University of Strathclyde, and we are unable to answer queries relating to this course. You are welcome to make links to it however, but please bear in mind that it was written for students within the University and so some parts may not be relevant to external readers.

This page has nothing to do with C programming. The rest of this document does, read on and good luck.

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_2.html [6/23/2010 11:21:20 AM]

Page 126: C baics

Introduction

Next: About C Up: C Programming Previous: Copyright Notice and

Introduction

● About C ● C and UNIX ● This Course ● Dialects of C

❍ Common C ❍ ANSI C

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_3.html [6/23/2010 11:21:21 AM]

Page 127: C baics

About C

Next: C and UNIX Up: Introduction Previous: Introduction

About C

As a programming language, C is rather like Pascal or Fortran. Values are stored in variables. Programs are structured by defining and calling functions. Program flow is controlled using loops, if statements and function calls. Input and output can be directed to the terminal or to files. Related data can be stored together in arrays or structures.

Of the three languages, C allows the most precise control of input and output. C is also rather more terse than Fortran or Pascal. This can result in short efficient programs, where the programmer has made wise use of C's range of powerful operators. It also allows the programmer to produce programs which are impossible to understand.

Programmers who are familiar with the use of pointers (or indirect addressing, to use the correct term) will welcome the ease of use compared with some other languages. Undisciplined use of pointers can lead to errors which are very hard to trace. This course only deals with the simplest applications of pointers.

It is hoped that newcomers will find C a useful and friendly language. Care must be taken in using C. Many of the extra facilities which it offers can lead to extra types of programming error. You will have to learn to deal with these to successfully make the transition to being a C programmer.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_3_1.html [6/23/2010 11:21:21 AM]

Page 128: C baics

C and UNIX

Next: This Course Up: Introduction Previous: About C

C and UNIX

This course teaches C under the UNIX operating system. C programs will look similar under any other system (such as VMS or DOS), some other features will differ from system to system. In particular the method of compiling a program to produce a file of runnable code will be different on each system.

The UNIX system is itself written in C. In fact C was invented specifically to implement UNIX. All of the UNIX commands which you type, plus the other system facilities such as password checking, lineprinter queues or magnetic tape controllers are written in C.

In the course of the development of UNIX, hundreds of functions were written to give access to various facets of the system. These functions are available to the programmer in libraries. By writing in C and using the UNIX system libraries, very powerful system programs can be created. These libraries are less easy to access using other programming languages. C is therefore the natural language for writing UNIX system programs.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_3_2.html [6/23/2010 11:21:22 AM]

Page 129: C baics

This Course

Next: Dialects of C Up: Introduction Previous: C and UNIX

This Course

The course aims to introduce programmers to the C language. Previous programming experience is assumed, so we can quickly progress to looking at the features of C and their uses. Students with little programming experience will need to do some homework in order to keep up with the lectures.

Teaching will emphasise the use of supervised practical sessions, giving the student hands on programming experience. The student will collect a number of working practical programs which will be useful reference material for the future.

The notes will include examples and explanation as far as possible. We will try to avoid involved discussion of the syntax of the language. This subject is exhaustively covered in a range of books which are available from bookshops or the University Library.

We aim to introduce C in a structured manner, beginning with the simpler aspects of the language, and working up to more complex issues. Simple aspects will be dealt with rather quickly in order to leave more time for the more powerful features.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_3_3.html [6/23/2010 11:21:22 AM]

Page 130: C baics

Dialects of C

Next: Common C Up: Introduction Previous: This Course

Dialects of C

● Common C ● ANSI C

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_3_4.html [6/23/2010 11:21:23 AM]

Page 131: C baics

Common C

Next: ANSI C Up: Dialects of C Previous: Dialects of C

Common C

Until recently there was one dominant form of the C language. This was the native UNIX form, which for historical reasons is known as either Bell Labs C, after the most popular compiler, or K. &R. C, after the authors of the most popular textbook on the language. It is now often called "Classic C"

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_3_4_1.html [6/23/2010 11:21:23 AM]

Page 132: C baics

ANSI C

Next: A Quick Overview Up: Dialects of C Previous: Common C

ANSI C

The American National Standards Institute defined a standard for C, eliminating much uncertainty about the exact syntax of the language. This newcomer, called ANSI C, proclaims itself the standard version of the language. As such it will inevitably overtake, and eventually replace common C.

ANSI C does incorporate a few improvements over the old common C. The main difference is in the grammar of the language. The form of function declarations has been changed making them rather more like Pascal procedures.

This course introduces ANSI C since it is supported by the SUN workstation compilers. Most C programming texts are now available in ANSI editions.

January 1995

http://www2.its.strath.ac.uk/courses/c/subsubsection3_3_4_2.html [6/23/2010 11:21:24 AM]

Page 133: C baics

A Quick Overview of C

Next: A Very Simple Up: C Programming Previous: ANSI C

A Quick Overview of CIt is usual to start programming courses with a simple example program. This course is no exception.

● A Very Simple Program ● A Weight Conversion Program ● Weight Conversion Table Using a Function ● Weight Conversion with a Prompt ● Weight Conversion with Command Line Argument ● Fibonacci Series Using an Array

January 1995

http://www2.its.strath.ac.uk/courses/c/section3_4.html [6/23/2010 11:21:24 AM]

Page 134: C baics

A Very Simple Program

Next: A Weight Conversion Up: A Quick Overview Previous: A Quick Overview

A Very Simple Program

This program which will print out the message This is a C program

#include <stdio.h>

main(){ printf("This is a C program\n");}

Though the program is very simple, a few points are worthy of note.

Every C program contains a function called main. This is the start point of the program.

#include <stdio.h> allows the program to interact with the screen, keyboard and filesystem of your computer. You will find it at the beginning of almost every C program.

main() declares the start of the function, while the two curly brackets show the start and finish of the function. Curly brackets in C are used to group statements together as in a function, or in the body of a loop. Such a grouping is known as a compound statement or a block.

printf("This is a C program\n"); prints the words on the screen. The text to be printed is enclosed in double quotes. The \n at the end of the text tells the program to print a newline as part of the output.

Most C programs are in lower case letters. You will usually find upper case letters used in preprocessor definitions (which will be discussed later) or inside quotes as parts of character strings. C is case sensitive, that is, it recognises a lower case letter and it's upper case equivalent as being different.

While useful for teaching, such a simple program has few practical uses. Let us consider something rather more practical. The following program will print a conversion table for weight in pounds (U.S.A. Measurement) to pounds and stones (Imperial Measurement) or Kilograms (International).

http://www2.its.strath.ac.uk/courses/c/subsection3_4_1.html (1 of 2) [6/23/2010 11:21:25 AM]

Page 135: C baics

A Very Simple Program

January 1995

http://www2.its.strath.ac.uk/courses/c/subsection3_4_1.html (2 of 2) [6/23/2010 11:21:25 AM]

Page 136: C baics

IT Services | Copyright Information

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

Copyright Information

All material within the http://www.its.strath.ac.uk/

domain is published by IT Services on behalf of the The University of Strathclyde, Glasgow, UK. Unless specified otherwise, all material is © The University of Strathclyde.

Permission to reproduce material from our site is granted as long as full credit is given. We suggest the use of the following paragraph:

© The University of Strathclyde, Glasgow, UK. All rights reserved. Reproduced with kind permission.

with a link to either http://www.strath.ac.uk/ or

http://www.its.strath.ac.uk/ as appropriate.

We would appreciate notification of your use of any of our material. Contact IT Services Webteam.

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/doc/copyright.html [6/23/2010 11:21:27 AM]

Page 137: C baics

IT Services | Webteam

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

IT Services Webteam

The IT Services web pages are the responsibility of a number of people. To contact the team, you should contact IT Services Helpdesk in the first instance.

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/webteam/ [6/23/2010 11:21:28 AM]

Page 138: C baics

IT Services

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

IT Services Helpdesks

Helpdesk Home

Contacts

Services

FAQ

Glossary of Terms

Search the

'Knowledge Base'

IT Services operates a number of Helpdesks which offer a number of services including user account processing,

applications help, fault reporting, IP address allocations, e-mail account queries, and other general computing or network queries.

We try to provide as much information as possible online to address common queries and requests for information, however obviously we can't present every possible answer. If you can't find the answer to your query online, contact the appropriate Helpdesk.

We recommend you contact us by e-mail where possible; a prompt response is guaranteed. If you prefer to speak to someone directly then phone or visit, but please be aware that at busy times an answerphones may be operational, or there may be a queue at the desk.

Each of the Helpdesks has its own area of expertise. If you're unsure which one to contact, try the general academic Helpdesk in the Curran desk first, and we'll redirect you if necessary.

If you want to buy consumables (floppy disks, printer cartridges, stationery, etc), then you need IT Services

Sales.

.

.

Useful links | Search ITS pages | Helpdesk | Back |

http://www2.its.strath.ac.uk/helpdesk/index.html (1 of 2) [6/23/2010 11:21:29 AM]

Page 139: C baics

IT Services

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/helpdesk/index.html (2 of 2) [6/23/2010 11:21:29 AM]

Page 140: C baics

IT Services

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

Helpdesk Contacts

Helpdesk Home

Contacts

Services

FAQ

Glossary of Terms

Search the

'Knowledge Base'

Curran HelpdeskFor general academic computing enquiries and matters relating to user registration.Location: Level three of the Andersonian Library in the Curran Building, straight ahead as you come through the security point.Email: [email protected]

Staffed: 9am to 7:45pm Monday to FridayTelephone: (0141) 548 4000 (option 1)Fax: (0141) 548 4509

McCance HelpdeskFor matters relating to university administrative staff and services.Location: level one of the McCance Building, in room 1.38 directly opposite the Cash Office.Email: [email protected]

Staffed: 8:30am to 5pm Monday to FridayTelephone: (0141) 548 4000 (option 3)Fax: (0141) 548 4164

Jordanhill HelpdeskFor enquiries at the Faculty of Education.Location: in the Technical Support Centre, room 117a on level one of the David Stow Building, to the right of the main entrance.Email: [email protected]

Staffed: 8:30am to 5pm Monday to FridayTelephone: (0141) 950 3278Fax: (0141) 950 3239

http://www2.its.strath.ac.uk/helpdesk/contacts.html (1 of 2) [6/23/2010 11:21:30 AM]

Page 141: C baics

IT Services

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/helpdesk/contacts.html (2 of 2) [6/23/2010 11:21:30 AM]

Page 142: C baics

Helpdesk Services

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

Helpdesk Services

Helpdesk Home

Contacts

Services

FAQ

Glossary of Terms

Search the

'Knowledge Base'

A number of services are available at the IT Services Helpdesk. Here is a brief overview of some of them.

User RegistrationAll users must be registered to use services provided centrally by IT Services. Registration involves proving your identity (usually by means of a student or staff ID card), agreeing to the terms and conditions of the use of computing facilities, and being allocated a number of usernumber/password pairs. Registration is always required before being able to use the central IT Services computing labs and email services, and registration may also be required for other services.

IP address allocationsAll computing equipment to be used on the network must have an IP address registered. For departmental equipment, the departmental Computer Officer will normally co-ordinate IP registrations. A number of details will be required about the equipment in order to register it. PCs in the Halls of Residence will have an IP address automatically assigned when they are first used by DHCP.

Network point installationsRequests for new data points on University premises should be made to the Helpdesk. You will need to provide full details on where the points are to go, and the budget code to charge the work to.

Fault ReportingAll faults relating to the network or to other

http://www2.its.strath.ac.uk/helpdesk/services.html (1 of 3) [6/23/2010 11:21:31 AM]

Page 143: C baics

Helpdesk Services

centrally-provided computing facilities should be reported to the Helpdesk. In departments, faults should normally be reported by the departmental Computer Officer where it is likely to be a general problem. This helps to prevent the same fault being reported by a number of people (and thus freeing up Helpdesk staff to pursue investigation of the fault), and allows the Computer Officer the opportunity to suggest specific advice to affected parties, or gather further information that may enable the IT Services staff to identify and rectify the fault.

Software help and distributionStaff on the Helpdesk have experience in a number of common software applications, and may be able to offer assistance with problems. Some software media is available for loan to staff or students for installation on University computers under certain licence agreements, and the Helpdesk would be the first point of contact for obtaining this.

Application formsApplication forms for various additional services can be obtained from the Helpdesk. Examples include the "WWW Provider Application Form" which is required for members of staff and students requiring an allocation of space on the University web server for their department/society, etc.

General computing queriesHelpdesk staff may be able to offer assistance with any general computing queries you may have, or may be able to direct you to someone who can. Because in the course of their duties they speak to many people, they have extensive experience of the expertise of various organisations, groups, and staff around the campus.

Please note that sometimes, especially in the case of faults with centrally provided systems, the Helpdesk may not be able to provide an immediate solution. Details of

http://www2.its.strath.ac.uk/helpdesk/services.html (2 of 3) [6/23/2010 11:21:31 AM]

Page 144: C baics

Helpdesk Services

the problem may be taken and it will then be passed to an appropriate support group. A member of the support group (or the Helpdesk again) may then get in touch to request more information, or to give information on when the problem can be fixed. Please be patient after reporting a fault; some can be difficult to resolve and may take some time. All faults are logged and tracked in the Helpdesk database system.

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/helpdesk/services.html (3 of 3) [6/23/2010 11:21:31 AM]

Page 145: C baics

IT Services

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

Helpdesk FAQ

Helpdesk Home

Contacts

Services

FAQ

Glossary of Terms

Search the

'Knowledge Base'

Where are the I.T. Help Desks?

Where are the labs?

What software is available in the labs?

Where (and how) do I get a usernumber and email

address?

How do I logon to the PCs?

How do I change my password?

How do I print in the labs?

Where can I buy a printing / photocopying card?

Where can I use a scanner?

How do I access email in the labs?

Where else can I access my email?

How do I let people know that I won’t be reading

my email ?

How do I save files onto floppy / zip drive / my

home directory?

What is the capacity of a floppy / zip drive / my

home directory?

How do I delete unwanted files from floppy / zip

drive / my home directory?

What if I delete a file by mistake?

How do I get access to labs outwith normal hours?

How do I get access and logon to Pegasus?

Where can I plug my laptop onto LAN?

How do I get access to (restricted) I.T. resources

from off campus?

How do I access the facilities from my room in the

residences?

How do I configure my PC / Laptop to use the

http://www2.its.strath.ac.uk/helpdesk/faq.html (1 of 10) [6/23/2010 11:21:32 AM]

Page 146: C baics

IT Services

proxy service?

Where can I get access to the Radio Network ?

How do I FTP a file (to my homepage)?

Where are the I.T. Help Desks?There are three I.T. Help Desks with responsibilities as follows:

Curran Help Desk – for general student and academic computing enquiries, based in level 3 of the Andersonian Library in the Curran Building.e-mail: [email protected] or phone ext 4000 option

1.Opening hours: 09:00 – 19:45 Monday – Friday (Term Time)

McCance Help Desk – for matters relating to University administration, based in the McCance Building.e-mail: [email protected] or phone ext 4000 option

3.Opening hours: 08:30 – 17:00 Monday – Friday.

Jordanhill Help Desk – for Education Faculty enquiries, based in Room 28a, Level 1 of the Stow Building.e-mail: [email protected] or phone Ext 3278 (77

prefix outwith Jordanhill campus)Opening: 08:30 – 17:00 Monday – Friday.See www.its.strath.ac.uk/helpdesk for more detail.

Where are the labs? I.T. Services has labs distributed throughout the campus; a detailed listing of locations of all centrally managed labs and software available in them can be viewed at:http://www.strath.ac.uk/IT/TeachingSupport/software/

What software is available in the labs?All of the I.T. Services Labs have the following core

http://www2.its.strath.ac.uk/helpdesk/faq.html (2 of 10) [6/23/2010 11:21:32 AM]

Page 147: C baics

IT Services

software: Microsoft Office, including Word, Excel, Access and PowerPoint.Internet Explorer browser. Web based mail for students – http://nemo.strath.ac.uk

This list is only a small subset of the software available; a detailed list of software available in all of the centrally managed labs can be viewed at: http://www.strath.ac.uk/IT/TeachingSupport/software/

Where (and how) do I get a usernumber and email address? You must be a registered student of the University, or a member of the teaching staff, in order to be granted access to the University's I.T. facilities. New students will receive details of their usernumber, password and email address in a security slip in the registration pack that is mailed by Registry, accounts only being activated when the main registration process is complete. Any late registering students omitted from the registry mailing process can obtain their user account, password and email address from the Curran Help Desk, Andersonian Library, level 3 (John Anderson campus) or Jordanhill Helpdesk, Room 28a, David Stow Building, (Jordanhill campus).

How do I logon to the PCs? Before you can log on to the lab PCs you need to have a usernumber and password. (see above).You logon to the PCs by firstly pressing the CTRL, ALT and DEL keys simultaneously. This will bring up the Windows logon box.

Username: Type your usernumber, which was issued to you by IT Services.Password: Type your password. This will appear on the screen as asterisks - *********.N.B. Your password is case sensitive.

http://www2.its.strath.ac.uk/helpdesk/faq.html (3 of 10) [6/23/2010 11:21:32 AM]

Page 148: C baics

IT Services

How do I change my password? After having logged on to the PC using your existing password, hold down the CTRL, ALT, and DEL keys simultaneously, this will launch the Windows XP Security dialog box, which has six buttons. Click on the 'Change Password' button. You should now see the Change Password dialog box, pre-populated with your usernumber and the domain (DS) and three blank password fields that need to be completed in order to change your password, Old Password, New Password and Confirm New Password.Use the TAB key or the mouse to move between the fields, entering initially the existing password and then the new password twice. Click on OK when complete.When changing password, remember that it is good practice to make password minimum length of 8, a mix of upper and lower case characters, and also to include some numbers.N.B. Your PC password is case sensitive. It takes approx 15-20 minutes to update to our servers, so it is good practice to change your password at the end of your login session.

How do I print in the labs? In the PC Labs we use a printing system called “Uniprint” to manage printing. In order to print you will require an emos card (printing card). Each time that a printer is used unit(s) will be deducted from the emos card: One unit per page is deducted for each black & white print and two units per page is deducted for each colour print. Five units per page will be deducted for each acetate printed.When you select print from the lab PC, simply choose “Uniprint laser print” (black & white print) or “Uniprint colour printer” (colour print), then click OK and follow the instructions on the screen.Each lab has uniprint stations designated for collecting printouts. To obtain your print, take your emos card to the print station and follow the instructions on the screen.

http://www2.its.strath.ac.uk/helpdesk/faq.html (4 of 10) [6/23/2010 11:21:32 AM]

Page 149: C baics

IT Services

Where can I buy a printing / photocopying card?You can buy printing / photocopying cards from I.T. Sales which is located in the Andersonian Library, level 3, on the right hand side. Cards can also be purchased from the library desk, and from vending machines on the 4th Floor of the Curran Building and in Andersonian Library, level 3.Prices are 50 units for £3 / 200 units for £10

Where can I use a scanner?There are scanners available in the following IT Services labs:Architecture Building G18Andersonian Library Level 3Curran Building R5.83Livingstone Tower L2.14Royal College 3.36James Weir M114

How do I access email in the labs?Email is available through a web interface. For most students, this will be NEMO, and is accessed as follows : Log on to a PC, launch Internet Explorer and go to the URL https://nemo.strath.ac.uk Type ds\username and

password in the appropriate boxes and click on the ‘Log on’ button. You will now have access to your Inbox.Some students may be on the legacy Cool/Mail service, which is accessed as follows: Log on to a PC, launch Internet Explorer and go to the URL http://cool.strath.ac.uk Type username and password in

the appropriate boxes and click on the ‘Log in’ button.When you first login, Cool/Mail may display a number of useful Mail Maintenance Operations it can carry out for you on your email. Check the box for any operation(s) you want to perform and click on the Perform Maintenance Operations button. To skip these operations, click on Skip Maintenance.You will now have access to your Inbox.More information on these services will be available from

http://www2.its.strath.ac.uk/helpdesk/faq.html (5 of 10) [6/23/2010 11:21:32 AM]

Page 150: C baics

IT Services

the helpdesk.

Where else can I access my email?You can access your email from anywhere that you have access to the Web.

How do I let people know that I won’t be reading my email ? If you are unable to read your email for some time, you might like to make people aware of this fact. This is achived by configuring your email account to send a "vacation" or “Out of Office” message, which is sent automatically to people when you receive an email from them.1. Log in to NEMO as described previously2. Select Options3. Select the “I'm currently out of the office” radio button4. Type an approptiate message for your correspondents in the dialog boxn.b. When you log in to NEMO it will ask if you want to turn the Out of Office Assistant off.

How do I save files onto floppy / zip drive / my home directory? Create your file (in Word / Excel etc) then by clicking on File at the top left hand corner of the screen you will then see a drop down menu from which you should choose Save As – then clicking on the down arrow on the Save in dialog lists the disk drives available.To save to the floppy drive, click on 3½ “ Floppy (A:), enter a file name – then click on Save.To save to the zip drive, click on Removable Disk (E:), enter a file name – then click on Save.To save to your Home Directory, click on Usernumber (on alcatraz ... (H)), enter a file name – then click on Save.

What is the capacity of a floppy / zip drive / my home directory?The capacity of a floppy disk is 1.44Mb.

http://www2.its.strath.ac.uk/helpdesk/faq.html (6 of 10) [6/23/2010 11:21:32 AM]

Page 151: C baics

IT Services

The capacity of a zip disk is either 100Mb or 250Mb.The capacity of your home directory on PCs is 50Mb.

How do I delete unwanted files from floppy / zip drive / my home directory? Double click on the My Computer icon on desktop; this will bring up a list of all drives on the PC. Double clicking on 3½ Floppy (A:) will list all of the files that are on the floppy disk. Highlight file or files that have to be deleted, then press the delete key.To delete files from zip drive – as above, but select Removable Disk (E:) To delete files from home directory – as above, but selecting usernumber on 'alcatraz ...' (H:)

What if I delete a file by mistake?Backups of the filestore are run daily; this backs up your Home Directory (H: drive), but doesn't back up floppy disks or zip disks. A copy of the file you have deleted can be restored to the previous day. The procedure for restore requests is to fill in a restore form with all your details, this form is available at the I.T. Helpdesk.

How do I get access to labs outwith normal hours?You need a RED CARD, which you get from your own department. Normally the departmental secretary or your supervisor keeps them. The card should be filled out, signed by the Head of Department or your supervisor then countersigned by I.T. Helpdesk staff. You will need show your registration card before the Card will be countersigned by I.T. Helpdesk red card can be signed. The RED CARD gives your access 24/7, except when the University is closed (Bank Holidays).

How do I get access and logon to Pegasus? Launch Internet Explorer (or Netscape Navigator), and type http://pegasus.strath.ac.uk in the Address box.You should now see the login prompt, enter your usernumber and DS password (remember that passwords are case sensitive), and click on Login. When you have

http://www2.its.strath.ac.uk/helpdesk/faq.html (7 of 10) [6/23/2010 11:21:32 AM]

Page 152: C baics

IT Services

successfully logged in you should see various links to get access to announcements, timetable, exam results etc.

Where can I plug my laptop onto LAN?There are 50 points in Level 3 of the Andersonian Library where you can plug in your laptop. Your laptop must be configured for DHCP access (booklet at Helpdesk)

How do I get access to (restricted) I.T. resources from off campus?Off-campus access to some resources can be obtained through a Virtual Private Network (VPN) connection.See http://www.its.strath.ac.uk/vpn/ for more details

How do I access the facilities from my room in the residences?In order to enhance the availability of computing facilities to students, a network has been installed in all the University's Halls Of Residence. This allows students to connect their own equipment to the campus-wide computer network via data points in their rooms.The URL below links to pages containing information on how to set up equipment and software to take full advantage of the facilities offered by the residences network.http://www.resnet.strath.ac.uk/

How do I configure my PC / Laptop to use the proxy service? DHCP Configuration :1. Start / Settings / Network and Dial Up Connections2. Select Local Area Connection3. Select TCP/IP Properties4. Select Obtain an IP Address automatically.5. Click OK twice to return to Settings.

Proxy Configuration :1. Launch Internet Explorer2. Select Tools from the menu bar.3. Select Internet Options

http://www2.its.strath.ac.uk/helpdesk/faq.html (8 of 10) [6/23/2010 11:21:32 AM]

Page 153: C baics

IT Services

4. Select the Connections Tab.5. Select ‘LAN Settings’ 6. Ensure that ‘Automatically detect settings’ is UNchecked7. Ensure that ‘Use proxy server’ is UNchecked.8. Check ‘Use automatic configuration script’9. In the ‘Address’ box type ‘http://www-config.strath.ac.uk/proxy.config’ 10. Select OK11. Select OK

You can also find these instructions at http://www.its.strath.ac.uk/proxy/

Where can I get access to the Radio Network ?John Anderson CampusAnderson - L3&L4 teaching clusterAndersonian libraryCollins - court/senateColville - L4 open areaCurran - IT Services, level 4Hills - L5 teaching clusterLord Hope - Several areas in LawMcCance - L3 open area/conservatory/lecture theatresSGBS - most open areas (pending)Student Union - priory barStenhouse - LawWeir - some L1, L2, L4 areas

Jordanhill CampusCrawfurd - most teaching areasDavid Stow building:FoyerBoardroomHelpdeskRoom 28Room 44 - 47Sir Henry Wood - library

How do I update my personal web home page?

http://www2.its.strath.ac.uk/helpdesk/faq.html (9 of 10) [6/23/2010 11:21:32 AM]

Page 154: C baics

IT Services

You have a personal web home page, which can be updated by modifying files in your H:\My Webspace folder. For more information, see http://www.its.strath.ac.uk/web/personalpages.html

Back to Top

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/helpdesk/faq.html (10 of 10) [6/23/2010 11:21:32 AM]

Page 155: C baics

IT Services

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

Helpdesk Glossary Of Computing Terms

Helpdesk Home

Contacts

Services

FAQ

Glossary of Terms

Search the

'Knowledge Base'

A

Acrobat Reader - A program from Adobe that lets you capture a document and then view it in its original format and appearance. To view an Acrobat document, which is called a PDF (Portable Document Format) file, you need Acrobat Reader. The Reader is available in all I.T. Services PC labs and can be downloaded free from Adobe, via the Internet : http://www.adobe.com/products/acrobat/

Active Directory - Active Directory is Microsoft's trademarked directory service, an integral part of the Windows 2000 architecture. Like other directory services, such as Novell Directory Services (NDS), Active Directory is a centralized and standardized system that automates network management of user data, security, and distributed resources, and enables interoperation with other directories. Active Directory is designed especially for distributed networking environments.

Active Server Page - An Active Server Page (ASP) is an HTML page that includes one or more scripts (small embedded programs) that are processed on a Web server before the page is sent to the user.

Application software - Application software (sometimes shortened to application) is any program designed to perform a specific function directly for the user or, in some cases, for another application program.. Typical types of application software include, Word processing,

http://www2.its.strath.ac.uk/helpdesk/glossary.html (1 of 13) [6/23/2010 11:21:34 AM]

Page 156: C baics

IT Services

Electronic spreadsheet, Database, Email reader, Web browser, Desktop publishing, Graphics software and so on.

ASCII - An acronym that stands for American Standard Code for Information Interchange. If you save your document as an ASCII file (sometimes called a plain text file) it can be opened in most word processing packages. Authentication - The verification of the identity of a person or process.

Back To Top

B

Backup - The process of securing data by copying your disks, in order to secure your data, in the event that the disks get lost or damaged.

Browser - An application program that provides a way to look at and interact with all the information on the World Wide Web. The most popular browsers are currently Netscape Navigator and Microsoft Internet Explorer, and these are both available in all I.T. Services PC labs.

Byte - Usually denotes 8 bits, which the computer treats as a single unit. A bit is the smallest unit of data in computing, with a value of either 0 or 1.

Back To Top

C

Cache - A cache is a place to store something temporarily. The files you automatically request by looking at a Web page are stored on your hard disk in a cache subdirectory under the directory for your browser (for example, Internet Explorer). When you return to a page you've recently looked at, the browser can get it from the cache rather than the original server, saving you time and the network the burden of some additional traffic.

http://www2.its.strath.ac.uk/helpdesk/glossary.html (2 of 13) [6/23/2010 11:21:34 AM]

Page 157: C baics

IT Services

Carbon copy - In e-mail, a carbon copy (CC) is a copy of a note sent to an addressee other than the main addressee. A blind carbon copy (BCC) is a copy sent to an addressee that is not visible to the main and carbon copy addressees.

CD-ROM - An acronym that stands for Compact Disk Read Only Memory. CD-ROMs usually contain massive amounts of information available for viewing, saving or printing.

Click - Press and release the mouse button to activate or select a command or option within an application.

Clip art - A library of cartoons and pictures that are available for you to use and copy.

Crash - This means that the computer or the network has suddenly stopped responding.

Configure - Set up hardware/software appropriately to function in a particular environment or setting.

Cursor - The flashing vertical line or block that appears on your screen, indicating that the next character you type in will appear there.

Cut & Paste - To move text or graphics from one place in your document and place it elsewhere.

Back To Top

D

Database - A collection of information or data that is organised, usually for referencing e.g. a group of names, addresses, phone numbers or student details, etc.

Desktop - Is the background screen on the computer. It consists of pictures, called icons, that show cabinets, files, folders, and various types of documents.

.doc - This is the file extension that Word automatically

http://www2.its.strath.ac.uk/helpdesk/glossary.html (3 of 13) [6/23/2010 11:21:34 AM]

Page 158: C baics

IT Services

puts on the end of your file names, this way both you and Word knows what sort of file it is.

DOS - Stands for Disk Operating System. It is a command based operating system.

Downloading / Uploading - Downloading is the transmission of a file from one computer system to another. From the Internet user's point-of-view, to download a file is to request it from another computer (or from a Web page on another computer) and to receive it. Uploading is transmission in the other direction: from one, usually smaller computer to another computer. From an Internet user's point-of-view, uploading is sending a file to a computer that is set up to receive it.

Back To Top

E

Electronic databases - They are a method of storing large amounts of information. Sometimes they are in CD-Rom format, which the University loads onto a server so they can be accessed from a linked computer terminal. On other occasions they can be in Web format, where you actually link via the World Wide Web to a server outside the University.

Email - Electronic Mail enables you to communicate electronically. To use email you need to use a computer that is on a Network.

Error Message - A message that appears on your screen, informing you that either the computer is not working properly or that you have made a mistake. Usually there are explanations of the error messages in the Help systems.

Ethernet - Is a standard for connecting computers into a local area network (LAN).

Back To Top

http://www2.its.strath.ac.uk/helpdesk/glossary.html (4 of 13) [6/23/2010 11:21:34 AM]

Page 159: C baics

IT Services

F

FAQ's - An acronym for Frequently Asked(or Answered) Questions.

File - Information stored by the computer onto disks. File types can be identified by the extension of the file name. For example .doc for Word, .xls for Excel .txt for Text, .jpg or .gif for image files, etc.

Firewall - A system designed to prevent unauthorized access to or from a private network. Firewalls are frequently used to prevent unauthorized Internet users from accessing private networks connected to the Internet, especially intranets.

Floppy Disk - A floppy disk is small portable magnetic diskette in a plastic holder used to store information. This is also called "3½ Floppy (A:/)" .

Floppy Drive - A slot usually found on the front of a computer for reading floppy disks.

Font - A font is the overall design for a set of characters, which includes a specific typeface and size. In computing terminology, `typeface' and `font' are often used interchangeably. Courier is a typeface; Courier 24-point bold is a font.

Format - This is done to prepare the floppy (or hard) disk ready for use by the computer. Different computers have different formats. Format can also be used when describing the various commands which allows you to change the appearance of your document, i.e.; Font, Bold, Styles, and Underline.

FTP (File Transfer Protocol) - File Transfer Protocol (FTP), a standard Internet protocol, is the simplest way to exchange files between computers on the Internet. FTP is an application protocol that uses the Internet's TCP/IP protocols. FTP is commonly used to transfer Web page files from their creator to the computer that acts as

http://www2.its.strath.ac.uk/helpdesk/glossary.html (5 of 13) [6/23/2010 11:21:34 AM]

Page 160: C baics

IT Services

their server for everyone on the Internet. It's also commonly used to download programs and other files to your computer from other servers.

Back To Top

G

.gif - [Derived from Graphics Interchange Format] A GIF is a type of image file. Like Jpeg it allows you to compress and store images for transfer over the Internet. Gif compression works best for non-photo images.

Gigabyte - A measurement of storage capacity. One gigabyte is equal to approximately 1 thousand megabytes (Exact figure is 1,024 megabytes). Gigabyte is often abbreviated as G or GB.

Back To Top

H

Hard Disk - Is a fixed disk inside the computer and is used to store vast amounts of data on it. On a PC it is often referred to as the C: drive.

Hardware - The physical components of any computer item that you can touch e.g. a printer.

HTTP (Hypertext Transfer Protocol) - The Hypertext Transfer Protocol (HTTP) is the set of rules for exchanging files (text, graphic images, sound, video, and other multimedia files) on the World Wide Web.

Back To Top

I

Icon - A small graphic which when clicked, using a mouse, launches an application program. Within an application icons are grouped together as 'toolbars' to provide quick access to particular functions.

http://www2.its.strath.ac.uk/helpdesk/glossary.html (6 of 13) [6/23/2010 11:21:34 AM]

Page 161: C baics

IT Services

i.d. or username - The identifier by which you are known to the computer.

Index - The searchable catalogue of documents created by search engine software. Also called "catalog."

Internet - An Internet is generally defined as a 'network of networks'. The word, Internet itself, spelt with a capital 'I' can be seen as the system, which links millions of computers throughout the World. Thus allowing them to communicate with each other.

Intranet - A network based on TCP/IP protocols (an internet) belonging to an organization, usually a corporation, accessible only by the organization's members, employees, or others with authorization.

IP Address - An IP address is a 32-bit number that identifies each sender or receiver of information that is sent in packets across the network. An IP address has two parts: the identifier of a particular network on the Internet and an identifier of the particular device (which can be a server or a workstation) within that network.

ISP (Internet Service Provider) - A company that provides individuals and other companies access to the Internet and other related services such as Web site building and virtual hosting. An ISP has the equipment and the telecommunication line access required to have a point-of-presence on the Internet for the geographic area served.

Back To Top

J

JPEG (.jpg) - is a standard type of image file. It is a very common way to compress and store images for transfer over the Internet. Often used to compress photographic images.

K

http://www2.its.strath.ac.uk/helpdesk/glossary.html (7 of 13) [6/23/2010 11:21:34 AM]

Page 162: C baics

IT Services

Kilobyte - A measurement of storage capacity. One kilobyte is equal to approximately 1 thousand bytes (Exact figure is 1,024 bytes). In computer literature, kilobyte is usually abbreviated as K or Kb.

L

LAN (Local Area Network) - Is a short-distance network used to link a group of computers together within a building.

Login/Log in - To connect to a network or computer.

Logout/Log out - To disconnect or end a session on a computer or a network.

Back To Top

M

Mac address (Media Access Control address) - Is an identity code, which is built into a network card. The IEEE (Institute of Electrical and Electronics Engineers) committee assigns blocks of addresses to a network-card manufacturer. This procedure ensures that no more than one network card share the same Mac address.Megabyte - A megabyte is equal to approximately one million bytes (Exact figure is 1,048,576 bytes). Megabyte is frequently abbreviated as M or MB. Bytes are the means of measuring the amounts of space a computer can use. A High Density floppy disk can hold 1.44MB of information, which is just over 500 pages in a Word document.

Menu Bar - A strip across the top of the screen containing a number of menus in a row. You can choose the menus by using the mouse or the keyboard.

Menu - A menu displays a list of commands. Most menus are located on the menu bar at the top of the Word window.

http://www2.its.strath.ac.uk/helpdesk/glossary.html (8 of 13) [6/23/2010 11:21:34 AM]

Page 163: C baics

IT Services

Modem - (modulator-demodulator). - This device is used by computers to communicate to other remote computers via the telephone lines.

Mouse - A mouse sits on your desk and is guided by either a wheel or light sensor. Moving the mouse around and clicking its buttons will move you in and out of programs without the need for your keyboard.

Multimedia - This is software that combines graphics, audio and video to make us a media presentation.

Back To Top

N

Network - A network of computers refers to computers that are able to communicate with each-other generally along wires. Networks can be 'local area' (LAN) or 'wide area' (WAN).

Network Interface Card - A network interface card (NIC) is a computer circuit board or card that is installed in a computer so that it can be connected to a network. Network interface cards provide a dedicated, full-time connection to the network.

NDS (Novell Directory Services) - A popular software product for managing access to computer resources and keeping track of the users of a network, such as a company's intranet, from a single point of administration. Using NDS, a network administrator can set up and control a database of users and manage them using a directory with an easy-to-use graphical user interface (GUI). Users of computers at remote locations can be added, updated, and managed centrally. Applications can be distributed electronically and maintained centrally.

O

Operating System - An operating system (sometimes abbreviated as "OS") is the program that manages all the other programs in a computer. These programs are called

http://www2.its.strath.ac.uk/helpdesk/glossary.html (9 of 13) [6/23/2010 11:21:34 AM]

Page 164: C baics

IT Services

applications or application programs. The application programs make use of the operating system by making requests for services.

Back To Top

P

Password - A password is a secret combination of characters that are either assigned to you or you can choose that give you access to the computer or the network. It is very important that you do not tell anyone else your password.

Protocol - In information technology, a protocol is the special set of rules that end points in a telecommunication connection use when they communicate. Protocols are often described in an industry or international standard.

Prompt - The little flashing line or block that appears on your screen, indicating that the computer is ready for your command or response. Usually you have to press Enter after you have typed.

Proxy Server - In an enterprise that uses the Internet, a proxy server is a server that acts as an intermediary between a workstation user and the Internet so that the enterprise can ensure security, administrative control, and caching service.

R

Remote - A remote computer is one that is connected to your network from a distant location like the other end of the campus, or in another town.

Rich Text Format (RTF) - This is a file format that enables you to save text files in your word processor, with formatting information, such as fonts and margins. It was developed by Microsoft to enable documents to be transferred between application programs. Most word processors can process Rich Text files. Additionally the

http://www2.its.strath.ac.uk/helpdesk/glossary.html (10 of 13) [6/23/2010 11:21:34 AM]

Page 165: C baics

IT Services

size of the file will be considerably smaller than a normal word-processed file.

Back To Top

S

Scanner - A device that can electronically read text or pictures and save them in a format where they can be used in other packages such as Word.

Scroll Bar - These are horizontal or vertical strips that allow you to view up, down, left and right of windows.

Search Engine - On the Internet, a search engine is a co-ordinated set of programs which searches an index and returns matches to a specified keyword. Google and Yahoo are examples of search engines.

Server - In general, a server is a computer program that provides services to other computer programs in the same or other computers. The computer that a server program runs in is also frequently referred to as a server (though it may contain a number of server and client programs).

SMTP (Simple Mail Transfer Protocol) - A TCP/IP protocol used in sending and receiving e-mail. However, since it's limited in its ability to queue messages at the receiving end, it's usually used with one of two other protocols, POP3 or Internet Message Access Protocol, that let the user save messages in a server mailbox and download them periodically from the server.

Software - Is an application package, which enables you to perform certain tasks, which manipulates any number of variables, hardware or data. Microsoft Word is a software program that enables word processing facilities.

Back To Top

T

http://www2.its.strath.ac.uk/helpdesk/glossary.html (11 of 13) [6/23/2010 11:21:34 AM]

Page 166: C baics

IT Services

Telnet - An Internet tool that allows users to connect to a remote computer.

Toolbar - In the graphical user interface (GUI), a toolbar is a horizontal row or vertical column of selectable images (buttons) that carry out certain frequently used actions when clicked. For example, saving or printing a document. Word processing, spreadsheet, and many other types of application programs come with one or more toolbars as part of their user interface.

U

Unix - Unix is one of the most flexible and powerful operating systems available. Unix is not a single operating system like Windows 2000, but a class of similar operating systems. There are dozens of different implementations of Unix.

Username - A name to identify yourself to a computer or network - you will need this when you login.

V

VAX - This is a large (mainframe) computer, which allows a number of users to use it at the same time.

VMS - (Virtual Memory System) This is the operating system used by the VAX.

VPN - A virtual private network (VPN) is a private data network that makes use of the public telecommunication infrastructure, maintaining privacy through the use of a tunnelling protocol and security procedures. It can be contrasted with a system of owned or leased lines that can only be used by one company.

Back To Top

W

Windows - Windows is a software package that provides the easy way to use, graphical user interface.

http://www2.its.strath.ac.uk/helpdesk/glossary.html (12 of 13) [6/23/2010 11:21:34 AM]

Page 167: C baics

IT Services

Web-site (Website) - is a related collection of World Wide Web (WWW) files that includes a beginning file called a home page.

World Wide Web (WWW) - Is a system of pages composed of graphics, sound, text and user input linked together via the Internet. It is part of, but by no means the only part of, the Internet.

X

.xls - the Excel file extension.

ZZip Disk - is a sophisticated floppy disk which stores up to 250 Mb of information. This is a much greater capacity than an ordinary 3.5 floppy disk of 1.4 Mb. You need a Zip Drive to be able to use a Zip disk.

Zip Drive - A high-capacity floppy disk drive developed by Iomega Corporation. Like the traditional floppy drive its works in much the same way.

Back To Top

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/helpdesk/glossary.html (13 of 13) [6/23/2010 11:21:34 AM]

Page 168: C baics

IT Services

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

Helpdesk Knowledge Base

Helpdesk Home

Contacts

Services

FAQ

Glossary of Terms

Search the

'Knowledge Base'

Searching the Knowledgebase

The knowledgebase contains details of known problems / issues / queries in relation to the desktop software in use at the University. To access the knowledgebase, click on the link below, and then on the search icon (looks like a magnifying glass). Type the required search expression in the Expression or Natural Language dialog box, the different types of search are outlined below.

Search the 'Knowledge Base' here...

Basic search expressionsA query expression is a mixture of words and operators used by the knowledgebase tosearch through the knowledge collection.All of the following query expressions are entered in the Expression box andsearch through the KnowledgeBase collection :

• Single words and word lists• AND, OR and NOT operators• Wildcard searching• Other useful searchesBy default, search expressions are not case sensitive.

Single words and word listsYou can type single-words in the Expression box, for example: Print. Knowledgebase searches for the word you type and its stemmed variations, in this case print, prints, printed, printer, and printing.In a similar manner, you can type word lists: print, paper, cartridge.

http://www2.its.strath.ac.uk/helpdesk/kb.html (1 of 4) [6/23/2010 11:21:35 AM]

Page 169: C baics

IT Services

Knowledgebase searches through your collection and retrieves calls that contain the words, print, paper or cartridge or their stemmed variations.

Calls that include a larger number of the words in the search expression are given a higher score on the results window and appear nearer the top.

AND, OR and NOT operatorsKnowledgebase provides the Boolean operators AND, OR and NOT. Searches involving these operators should be typed in in the Expression box.NOT Excludes problems or calls that contain the following word. For example, to search for problems or calls that are not relevant to phones, type:

NOT phones

AND The AND operator searches for problems or calls that contain all of thewords you specify. For example, to search for all problems or calls thatare relevant to phones and chargers, type:

phones AND chargers

which retrieves all problems or calls that contain both search words.

Search ExpressionsThe operators are in UPPER CASE in the examples above, to highlight their use. In Knowledgebase, you can type the operators in either upper or lower case. The precedence order of the operators is NOT, AND, OR. You can also use brackets (and) to indicate precedence. For example, if you type:

NOT (PC OR LAN) AND printer

Knowledgebase finds all problems or calls that do not

http://www2.its.strath.ac.uk/helpdesk/kb.html (2 of 4) [6/23/2010 11:21:35 AM]

Page 170: C baics

IT Services

refer to PCs or LANs, but do refer to printers.

Wildcard searchingYou can use wildcard characters in searches to represent variable portions ofsearch strings. The following wildcard characters can be used:

? - specifies one of any alphanumeric character, as in ?an, which finds can, ran, and so on.

* - specifies zero or more of any alphanumeric character, as in har*, which finds hardware, hardly, and so on.If you use a wildcard search that finds too many matched, a warning dialog box appears. Type a new, more precise search expression in the Search Expression dialog box, then click Search Again.

Natural language searchingThis advanced search option enables you to type free text to search for calls orproblems that have similar content to the free text. For example, you can type intothe Natural Language box:

No dialling tone when user tries to make a call from his mobile phone.

Knowledgebase parses the query to remove words such as the, and and, and performssimilarity and synonym searching on the remaining words. Problems or calls containing the words crash, crashing, crashes are found, as are problems or callscontaining the words missing and gone. The score indicates how well each problem or call matches this query. Problems or calls with a high score contain more components of the search expression than those with a low score.

Calls that include a larger number of the words in the

http://www2.its.strath.ac.uk/helpdesk/kb.html (3 of 4) [6/23/2010 11:21:35 AM]

Page 171: C baics

IT Services

search expression are given a higher score on the results window and appear nearer the top.

SEE ALSO FAQS ....

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/helpdesk/kb.html (4 of 4) [6/23/2010 11:21:35 AM]

Page 172: C baics

IT Services Sales

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

IT Services Sales

Information Technology Services Sales supplies computer consumables and various items of software to staff and students of the University. The price lists below have the most popular items that we stock. For other items please contact us.

● Inkjet Cartridges

● LaserJet Cartridges

● Paper and Transparencies

● Floppy/Zip Disks, CDs

● Data Cartridges

● Miscellaneous

● Stationery (student sales only)

A Requisition Form (PDF document) is available for

departmental use. Please print it off, complete, and fax back to us at the number below. All departmental orders must now be on the Requisition Form.

IT Sales also supplies and orders software on behalf of

members of the University, including software under site licences and special deals.

Staffed by Anne Porter and Ann Howarth, IT Sales are located at:

IT Services SalesAndersonian Library101 St James RoadGlasgow G4 0NT

http://www2.its.strath.ac.uk/sales/ (1 of 2) [6/23/2010 11:21:36 AM]

Page 173: C baics

IT Services Sales

Telephone: (0141) 548 3569Fax: (0141) 548 4486Email: [email protected].

We are open Monday to Thursday between 09:00 and 16:30, and Friday 09:00 to 16:00.

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/sales/ (2 of 2) [6/23/2010 11:21:36 AM]

Page 174: C baics

IT Services Sales: Inkjet Cartridges

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

Inkjet Cartridges

All prices are in UK pounds sterling.

ProductStockCode

Price (£)

Hewlett Packard

HP 51604A Black Thinkjet Cartridge 034007 11.65

HP51626A (No 26) Black Cartridge 034011 19.11

HP51625A (No 25) Colour cartridge 034012 19.99

HPC4844A (No 10) Black cartridge 034013 20.80

HP51640A (No 40) Black Cartridge 034024 18.80

HP51633M (No 33) Black Cartridge 034029 20.82

HP51629A (No 29) Black Cartridge 034034 16.35

HP51649A (No 49) Colour Cartridge 034035 19.80

HP51641A (No 41) Colour Cartridge 034036 16.82

HP51645A (No 45) Black Cartridge 034037 15.96

HPC1823D (No 23) Colour Cartridge 034042 18.45

HPC6614D (No 20) Black Cartridge 034060 18.47

HPC6578D (No 78D) Colour Cartridge 19ml

034061 18.45

HPC6578A (No 78A) Colour Cartridge 38ml

034152 33.87

HPC6615D (No 15) Black Cartridge 034062 15.63

HPC6625A (No 17) Colour Cartridge 034063 17.17

HPC4836A (No 11) Cyan Cartridge 034066 20.38

HPC4837A (No 11) Magenta Cartridge 034067 20.38

HPC4838A (No 11)Yellow Cartridge 034068 20.38

HPC5011A (No 14) Black Cartridge 034100 11.80

http://www2.its.strath.ac.uk/sales/inkjet.html (1 of 7) [6/23/2010 11:21:37 AM]

Page 175: C baics

IT Services Sales: Inkjet Cartridges

HPC5010A (No 14) Colour Cartridge 034101 15.66

HPC6656A (No 56) Black Cartridge 034107 11.03

HPC6657A (No 57) Colour Cartridge 034108 18.42

HPC6658A (No 58) Photo Cartridge 034131 15.29

HPC8727A (No 27) Black Cartridge 034118 10.23

HPC8728A (No 28) Colour Cartridge 034119 11.88

HPC9351AE (No 21) Black cartridge 034141 10.20

HPC9352AE (No 22) Colour cartrdige 034142 11.28

HPC8767 (No 339) Black cartridge 034138 17.19

HPC9504EE (No 339) Black (pack of 2) cartridges

034184 47.60

HPC9363 (No 344) colour cartridge 034139 20.47

HPC9505EE (No 344) Colour (pack of 2) cartridges

034253 40.77

HP No 348 colour cartridge 034254 19.28

HPC8765 (No 338) black cartridge 034160 11.44

HPC9364E (No 337) Black Cartdridge 034207 17.04

HPC8766EE (No 343) colour cartridge 034159 14.23

HPC8721E (No 363) black cartridge 034169 16.05

HPC8719E (No 363) Black high yield cart

034196 25.58

HPC8771E (No 363) cyan cartridge 034170 10.92

HPC8772E (No 363) magenta cartridge

034171 8.72

HPC8773E (No 363) yellow cartridge 034172 10.99

HPC8774E (No 363) light cyan cartridge

034173 13.46

HPC9396A (No 88) large black cartridge

034177 27.12

HPC9385A (No 88) Black cartridge 20.5ml

034185 21.27

HPC9386A (No 88) Cyan cartridge 034186 15.66

HPC9387A (No 88) Magenta cartridge 034187 15.34

HPC9388A (No 88) Yellow cartridge 034188 15.72

http://www2.its.strath.ac.uk/sales/inkjet.html (2 of 7) [6/23/2010 11:21:37 AM]

Page 176: C baics

IT Services Sales: Inkjet Cartridges

Epson

Epson Stylus 790/870/1270 (T007) Black Cartridge

034001 7.12

Epson Stylus 1270/1290 (T009) Colour Cartridge

034002 12.91

Epson (SO20093/187) consolidated Black branded

034039 14.14

Epson 740/800 (SO20089/189) Black Consolidated

034043 11.52

Epson (SO20108/191) Colour Consolidated Branded

034044 16.13

Epson 900 (T003) Black Cartridge 034051 4.54

Epson 900/900G (T005) Colour Cartridge

034052 5.25

Epson Photo 750/700 (SO20193/110) Consolidated colour

034053 13.06

Epson Stylus 880 (TO19) Black Cartridge

034064 3.73

Epson Stylus 880 (TO20) Colour Cartridge branded

034065 6.02

Epson Stylus 680 (TO17) Black Cartridge

034069 15.94

Epson Stylus 680 (TO18) Colour Cartridge

034071 10.08

Epson Stylus 480/C20/C40 (TO13) Black Cartridge

034072 11.04

Epson Stylus 480/C20/C40 (TO14) Colour (Branded)

034073 8.39

Epson Stylus 790/870 (T008) Photo Colour Cartridge

034074 9.47

Epson Stylus C60 (TO28) Black Cartridge Branded

034084 9.90

Epson Stylus C60 (TO29) Colour Cartridge

034085 13.61

Epson Stylus 810 (TO26) Black cartridge

034086 14.44

http://www2.its.strath.ac.uk/sales/inkjet.html (3 of 7) [6/23/2010 11:21:37 AM]

Page 177: C baics

IT Services Sales: Inkjet Cartridges

Epson Stylus 810 (TO27) Colour cartridge Branded

034087 11.69

Epson Stylus C70 (T032140) Black cartridge

034109 14.99

Epson Stylus C70 (T032240) Cyan cartridge (branded)

034110 12.13

Epson Stylus C70 (T032340) Magenta cartridge

034111 8.04

Epson stylus C70 (T032440) Yellow cartridge

034112 8.04

Epson Stylus C42 (T036) Black cartridge branded

034113 9.09

Epson Stylus C42 (T037) Colour cartridge branded

034114 12.72

Epson Stylus C62 (T040) Black Cartridge

034116 17.52

Epson Stylus C62 (T041) Colour Cartridge

034117 14.64

Epson Stylus 2100 T034140 Black Cartridge

034120 16.50

Epson Stylus C82 T042240 Cyan 034121 7.56

Epson Stylus C82 T042340 Magenta 034122 7.33

Epson Stylus C82 T042440 Yellow (branded)

034123 12.13

Epson Stylus C64 T044140 Black (branded)

034126 9.91

Epson Stylus C64 T044240 Cyan branded

034127 7.11

Epson Stylus C64 T044340 Magenta branded

034128 7.47

Epson Stylus C64 T044440 Yellow branded

034129 7.47

Epson TO45240 Cyan cartridge 034174 8.30

Epson TO45340 Magenta cartridge 034175 8.30

Epson TO45440 Yellow cartridge 034176 8.30

http://www2.its.strath.ac.uk/sales/inkjet.html (4 of 7) [6/23/2010 11:21:37 AM]

Page 178: C baics

IT Services Sales: Inkjet Cartridges

Epson Photo R800 T054140 Black 034197 15.22

Epson Photo R800 T054340 Magenta 034198 15.14

Epson Photo R800 T054440 Yellow 034199 15.14

Epson Photo R800 T054740 Red 034200 15.14

Epson Photo R800 T054840 Black 034201 15.22

Epson Photo R800 T054940 Blue 034202 15.14

Epson Photo R800 T054040 Black 034203 8.18

Epson Photo R800 T054240 Cyan 034205 15.02

Epson T0611 Black Cartridge 034168 7.47

Epson T061240 Cyan Cartridge 034181 8.30

Epson T061340 magenta Cartridge 034182 8.30

Epson T061440 yellow cartridge 034183 8.30

Epson R200/300 T048140 Black cartridge

034161 17.01

Epson R200/300 T048240 Cyan cartridge

034162 17.01

Epson R200/300 T048340 Magenta cartridge

034163 17.08

Epson R200/300 T048440 Yellow cartridge

034164 17.08

Epson R200/300 T048540 Light cyan cartridge

034165 17.08

Epson R200/300 T048640 cartridge 034166 17.08

Lexmark

Lexmark Z31/Z51 (12A1975) Black Cartridge

034056 15.51

Lexmark Z51 (15M0120)Colour Cartridge

034057 18.26

Lexmark Z31/Z51 (12A1990) Colour Cartridge

034058 19.65

Lexmark Z22/Z32 (17G0050) Black Cartridge

034081 14.55

Lexmark Z22/Z32 (17G0060) Colour Cartridge

034082 13.83

http://www2.its.strath.ac.uk/sales/inkjet.html (5 of 7) [6/23/2010 11:21:37 AM]

Page 179: C baics

IT Services Sales: Inkjet Cartridges

Lexmark Z11 (12A1980) Colour Cartridge

034083 29.26

Lexmark Z13 (10N0016) Black Cartridge

034088 14.55

Lexmark Z13 (10N0026) Colour Cartridge

034089 15.84

Lexmark Z13 (10N0227) Colour Cartridge

034130 19.76

Lexmark 10N0217 (No17) Black Cartridge

034132 10.85

Lexmark 17G0648 (No 48) Black Cartridge

034135 14.85

Lexmark 18L0032 (No 82) Black Cartridge

034154 20.09

Lexmark 18L0042 (No 83) Colour Cartridge

034155 29.15

Lexmark No 1 Black/colour cart 034204 18.60

Lexmark 18C0032E (No 32) Black cartridge

034193 24.68

Lexmark 18C0033E (No 33) Colour cartridge

034194 21.81

Lexmark No 31+33 twin pack (black/colour

034258 55.16

Canon

Canon BC-01/Stylewriter II Black Cartridge

034009 14.42

Canon BJC2000 Black Cartridge (BCI21)

034005 4.72

Canon BJC2000 Colour Cartridge (BCI21)

034006 10.79

Canon BJC2000 Colour Cartridge (BC-21e) Branded

034006 29.11

Canon BC-05 Colour Cartridge (Branded)

034003 24.54

Canon BC30E Black Cartridge 034096 35.50

http://www2.its.strath.ac.uk/sales/inkjet.html (6 of 7) [6/23/2010 11:21:37 AM]

Page 180: C baics

IT Services Sales: Inkjet Cartridges

Canon BCI-15 Black cartridge (2 pack) branded

034124 16.84

Canon BCI-15 Colour cartridge (2 pack) branded

034125 26.22

Canon BCI-10 black Cartridge 034134 16.09

Canon BCI-10 Colour Cartridge 034140 19.73

Canon BCI-24 Black cartridge branded

034136 3.86

Canon BCI-24 Colour cartridge branded

034137 12.51

Canon BCI-3E/BJC 3000 Black cartridge

034206 6.59

Canon PIXMA 1600 black cartridge branded

034208 15.40

Canon PIXMA 1600 colour cartridge branded

034209 18.80

Canon BCI-3eBK Black cartridge branded

034189 8.43

Canon BCI-6C cyan cartridge branded 034190 6.78

Canon BCI-6M magenta cartridge branded

034191 6.78

Canon BCI-6Y yellow cartridge branded

034192 6.78

Samsung

Samsung M55 Black cartridge 034252 46.24

Brother

Brother LC900 magenta cartridge 034255 11.62

Brother LC900 yellow cartridge 034256 11.62

Brother LC900 Cyan cartridge 034257 11.62

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/sales/inkjet.html (7 of 7) [6/23/2010 11:21:37 AM]

Page 181: C baics

IT Services Sales: Laserjet Cartridges

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

Laserjet Cartridges

All prices are in UK pounds sterling.

ProductStockCode

Price (£)

Hewlett Packard

HP 92295A series II Toner (Reviva) 035003 31.90

HP C4092A 1100 Toner 035004 26.96

HP C4096A 2100 Toner (Lexmark) 035008 34.02

HP 92275A IIP Toner 035023 57.76

HP 92298X IV Toner (lexmark) 035029 36.14

HP C4129X 5000N Toner 035034 92.36

HP C4127X 4000 Toner (Lexmark) 035050 43.28

HP 92274A 4L Toner (Reviva) 035053 50.37

HP C3903A 5P/6P Toner 035057 41.02

HP C3900A 4V Toner 035060 49.68

HP 3906A 5L/6L Toner 035061 32.41

HP 8500 C4149A Black Toner 035072 69.09

HP 8500 C4150A Cyan Toner 035073 107.32

HP 8500 C4151A Magenta Toner 035074 107.32

HP 8500 C4152A Yellow Toner 035075 107.32

HP 8500 C4153A Drum Unit 035076 95.34

HP 4500 C4191A Black Toner 035026 54.53

HP 4500 C4192A Cyan Toner 035042 79.13

HP 4500 C4193A Magenta Toner 035043 79.13

HP 4500 C4194A Yellow Toner 035044 79.13

HP 4500 C4195A Drum Unit 035077 41.33

HP 4500 C4196A Transfer Kit 035027 126.50

http://www2.its.strath.ac.uk/sales/laserjet.html (1 of 4) [6/23/2010 11:21:39 AM]

Page 182: C baics

IT Services Sales: Laserjet Cartridges

HP 4500 C4198A Fuser kit 035141 148.51

HP 4100 C8061X Toner 035025 58.18

HP 1200 C7115X Toner 035078 34.97

HP 1300 Q2613A Toner 035084 33.91

HP 4600 HPC9720A Black Toner 035086 76.78

HP 4600 HPC9721A Cyan Toner 035087 111.88

HP 4600 HPC9722A Yellow Toner 035088 111.88

HP 4600 HPC9723A Magenta Toner 035089 111.88

HP 4600 HPC9724A Drum Unit 035116 230.31

HP 2300 HPQ2610A Black toner 035031 54.71

HP 4200 Q1338A Black Toner 035091 69.81

HP 8150 HPC4182X Black Toner 035092 101.17

HP 1010 Q2612A Black Toner 035093 34.62

HP 1150 Q2624A Black Toner 035094 50.39

HP 2500 HPC9700A Black Toner 035095 39.79

HP 2500 HPC9701A Cyan Toner 035099 64.11

HP 2500 HPC9702A Yellow Toner 035100 51.62

HP 2500 HPC9703A Magenta Toner 035101 64.11

HP 2500 HPC9704A Drum Unit 035096 110.80

HP 4300 HPQ1339A Black Toner 035098 92.81

HP 4300 Fuser unit 035147 299.09

HP 4300 Maint kit Q2437A 035118 354.20

HP 5500 HPC9730A Black Toner 035103 111.04

HP 5500 HPC9731A Cyan Toner 035104 187.48

HP 5500 HPC9732A Yellow Toner 035105 187.48

HP 5500 HPC9733A Magenta Toner 035106 187.48

HP 2400 HPQ6511A Black Toner 035107 139.29

HP 2420 HPQ6511X Toner 035127 118.61

HP1160 Q5949A Black Toner 035128 39.46

HP 1320 HPQ5949X Black Toner 035108 64.75

HP 2550 HPQ3960A Black Toner 035109 39.79

HP 2550 HPQ3961A Cyan Toner 4k 035110 51.62

HP 2550 HPQ3962A Yellow Toner 4k 035111 51.62

http://www2.its.strath.ac.uk/sales/laserjet.html (2 of 4) [6/23/2010 11:21:39 AM]

Page 183: C baics

IT Services Sales: Laserjet Cartridges

HP 2550 HPQ3963A Magenta Toner 4k

035112 51.62

HP 2550 HPQ3964A Drum Unit 035114 101.54

HP 2550 HPQ3971A Cyan Toner 2k 035131 45.42

HP 2550 HPQ3972A Yellow Toner 2k 035132 45.42

HP 2550 HPQ3973A magenta Toner 2k

035133 45.42

HP 4250 Q5942X Toner 035115 208.45

HP 4250 Q5942A Toner 035117 94.63

HP 3500 Q2670A Black Toner 035120 63.52

HP 3500 Q2681A Cyan Toner 035121 88.07

HP 3500 Q2682A Yellow Toner 035122 88.07

HP 3500 Q2683A Magenta Toner 035123 88.07

HP 3500 Q2671A Cyan Toner 035124 91.40

HP 3500 Q2672A Yellow Toner 035125 91.40

HP 3500 Q2673A Magenta Toner 035126 91.40

HP P3005 Q7551X toner 035148 158.12

HP 9000 C8543X Toner 035136 204.92

HP 3600/3800 Q6470A toner 035139 88.12

HPQ6460A toner 035142 93.72

HPQ7581A toner 035143 122.20

HPQ7553A toner 035144 65.15

HPQ7582A toner 035145 119.86

HPQ7583A toner 035146 119.86

Brother

Brother HL760 TN-200 Toner 035005 17.00

Brother HL760 Drum Unit 035006 150.25

Brother HL820/TN-300 Toner 035009 17.00

Brother HL820/DR-300 Drum 035011 113.78

Brother HL630/TN-100 Toner 035068 33.69

Brother HL1250/TN-6600 Toner 035015 50.05

Brother HL1250/DR-6000 Drum 035017 93.27

Brother HL1650/70 TN-7600 Toner 035019 50.05

http://www2.its.strath.ac.uk/sales/laserjet.html (3 of 4) [6/23/2010 11:21:39 AM]

Page 184: C baics

IT Services Sales: Laserjet Cartridges

Brother TN-8000 Toner 035016 19.69

Brother DR-8000 Drum 035097 158.13

Brother HL5100 TN-3060 Toner 035102 41.84

Brother HL2030 TN-2000 Toner 035134 61.20

Brother DR-7000 Drum Unit 035140 90.72

Apple

Apple 8500/M5893G Toner 035067 252.37

Lexmark

Lexmark 11A4096 Drum Unit 035018 220.00

Lexmark 11A4097 Toner 035014 79.06

Lexmark Optra S Toner 035066 126.60

Lexmark Optra E312 035083 93.12

Lexmark E210 Toner 035085 64.91

Lexmark E320 Toner 035028 123.06

Lexmark E321 12A7400 Toner 035090 57.97

Kyocera

Kyocera TK17 toner 035022 44.19

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/sales/laserjet.html (4 of 4) [6/23/2010 11:21:39 AM]

Page 185: C baics

IT Services Sales: Paper and Transparencies

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

Paper and Transparancies

All prices are in UK pounds sterling.

ProductStockCode

Price (£)

Transparencies

Inkjet Transparencies Colour (box 50) 009033 8.50

Laser Transparencies Mono (box 50) 009034 8.50

Laser Transparencies Colour (box 50) 009035 8.50

Paper

Photo Glossy Paper (box 50) 009036 8.50

A1 sheet 009017 0.20

A2 sheet 009018 0.10

A3 sheet 009005 0.05

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/sales/paper.html [6/23/2010 11:21:40 AM]

Page 186: C baics

IT Services Sales: Floppy/Zip Disks, CDs

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

Floppy/Zip Disks, CDs

All prices are in UK pounds sterling.

ProductStockCode

Price (£)

LS-120 Super Disks 014019 3.11

Single formatted disk 014029 0.20

3 1/2 DS HD Unformatted box 10 014023 3.84

3 1/2 DS HD Formatted (card box of 10)

014027 1.35

Recordable CDs 700mb 014024 0.30

Re-writeable CDs 650mb 014021 0.60

DVD+R 4.7GB 014033 0.40

DVD+RW 4.7GB 014034 1.00

DVD-R 4.78GB 014035 0.40

DVD-RW 4.78GB 014036 1.00

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/sales/disks.html [6/23/2010 11:21:41 AM]

Page 187: C baics

IT Services Sales: Data Cartridges

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

Data Cartridges

All prices are in UK pounds sterling.

ProductStockCode

Price (£)

Compactape TK85K-01 DLT 3 011001 20.41

Compactape TK88K-01 DLT 4 011002 17.32

DLT Cleaning Cartridges 011003 25.44

Super DLT Cleaning cartridge 011005 48.13

Travan 20/40GB Tape 011011 36.44

Dat Tape DDS-2 120M 8GB 011044 3.44

Dat Tape DDS-3 125M 24GB 011046 2.68

Dat tape DDS-4 150M 40GB 011047 6.16

4mm Dat Tape Cleaner 011038 4.12

Super DLT 160/320 Tapes 011004 66.69

Super DLT 600 Tapes 011006 66.00

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/sales/data.html [6/23/2010 11:21:42 AM]

Page 188: C baics

IT Services Sales: Miscellaneous

IT Services

Home | IT Services | University | Navigation | Search ITS pages | Helpdesk | Back |

Miscellaneous

All prices are in UK pounds sterling.

ProductStockCode

Price (£)

Printer Cards

Emos Card 50 pages 009097 3.00

Emos Card 200 pages 009098 10.00

Cables

Parallel Printer Cable 077004 3.00

Ethernet Cable 077006 2.00

USB Printer cable 077020 2.00

PC power cable 077012 6.00

Ethernet Cards

10/100M PCI Ethernet Adaptor 077008 7.00

10/100M PCMCIA Ethernet Adaptor 077009 19.00

Network Card Wireless for Laptop 077010 19.00

Storage

3 1/2 Lockable Tray (100 capacity) 019014 10.00

3 1/2 Disk Protector (1 capacity) 019001 0.50

Cleaning

Screen,Keyboard Cleaning Kit 019019 5.80

Wet & Dry Wipes (Box 20) 019020 1.40

Batteries

Duracell MN1500 "AA" (4 pack) 019018 2.00

Duracell MN2400 "AAA" (4 pack) 019017 2.00

Duracell MN1604 9V each 019025 2.00

Laptop Bags

http://www2.its.strath.ac.uk/sales/misc.html (1 of 2) [6/23/2010 11:21:43 AM]

Page 189: C baics

IT Services Sales: Miscellaneous

Targus Notepac Black 019028 20.00

Backpack 019029 15.00

Laptop Sleeve (14" 15" 15.4") 019031 8.50

Memory Sticks

USB Memory Stick 256mb 077021 10.00

USB Memory Stick 512mb 077022 15.00

USB Memory Stick 1GB 077018 20.00

USB memory stick 2GB 077017 30.00

Misc

Optical Mouse 077014 4.00

Earphones 077015 2.70

Headphones with Mic 077016 5.00

AC Adaptor+cable (laptop) 077011 60.00

Software available for download through PEGASUS to Staff and Students

For Staff go to Administration tab

For Students go to Additional Services tab

Useful links | Search ITS pages | Helpdesk | Back |

Last modified: NaNth undefined NaN

© IT Services, University of Strathclyde

http://www2.its.strath.ac.uk/sales/misc.html (2 of 2) [6/23/2010 11:21:43 AM]


Recommended