Input and Output - Introduction
Before we can write our first program, we need to know something about how
we provide (input) data to computer programs and
how data is returned (output) by them.
|
A part of every C language system (compiler and linker)
is a set of libraries which provide functions for commonly used
operations such as input and output.
|
The ANSI C standard specifies the names and operation of a basic set
of input and output operations.
Programs using these may be expected to run in some sensible way on most computers.
This very important property
of programs called portability.
|
We can view a simple C program as something which takes its input, performs some operations on it, and produces some output - rather like a pipe with an input end (into which we push some input data) and an output end (from which we will receive some data produced by the program).
On most computers, the simplest input device is your computer's keyboard and this is called standard input or stdin (Unless you do something to change the source of standard input data - you will learn about this later). The simplest output device is the computer's screen and this is called standard output or stdout.
The keyboard as a source of input has no clear 'start' or 'end':
you can start typing any time and continue for ever -
well at least until lunch or dinner (whichever comes first!).
Similarly, you can set a program running on the computer which will
continue to output onto the screen for ever
(or until you need the computer to complete your next assignment).
Such continuous data is often called a stream.
In order to get some output from our program onto the computer's screen,
we need some methods on the input and output data streams.
For output, we can use:
|
fputc(c,s) |
write a single character, c, to the output stream, s |
| fprintf(s,format,a,b,c, ..) |
write the values of variables a,b,c,.. to the stream, s, formatted according to the format string format. |
fprintf(stdout,format,a,b,c,..) is so commonly used -
for output to the standard output stream, stdout,
that there is a special abbreviation for it
printf(format,a,b,c,...)
similarly,
putchar(c)
is an abbreviation for fputc(s,stdout).
We will study the full power of fprintf and
printf later,
but for the moment, let's look at a few examples in simple programs:
/* import stream specification */
#include <stdio.h>
/* import Rectangle specification */
#include "Rectangle.h"
/* Every C program starts with a function called main */
main() {
/* Declare some variables */
Rectangle r;
double h;
/* Construct a rectangle and assign it to r */
r = ConsRectangle( 30.0, 40.0 );
/* Extract and print its height */
h = Height( r );
printf("Height is %f\n", h );
/* Change its height */
SetHeight( r, 50.0 );
/* Extract and print the new height */
h = Height( r );
printf("Height is now %f\n", h );
}
Notes
- Characters in between a pair of /*
and */ symbols are comments.
Comments are ignored by the compiler and
have no effect on the running of the program.
They are inserted simply to help you understand the program.
Note that, as in all good programs, this program has many comments.
- The first statements in the program import specifications of classes
which we wish to use in this program.
In C, these are just ordinary text files (prepared with any suitable text editor) which are included in this program's text before it is compiled.
By convention, specification files have a ".h" extension to their filenames.
(They are often called header files.)
- Every C program is actually a function called main.
So somewhere in your program you must define a function
main -
otherwise the linker (the significance of the terms compiler and linker will be explained later) will complain.
- The body of a C function is enclosed in braces - { }.
- A group of statements enclosed in braces is called a block.
At the beginning of the block, all the variables needed within that block are declared in
declaration statements
which give the name of the class (double, Rectangle
)
and a list of variable names.
- All statements are terminated by a semi-colon.
- In programs, character strings are surrounded by double quote marks (").
Thus "Height is %f\n" is a string.
- printf prints out the characters in its format string
until it finds a % followed by a character
which indicates the way a variable will be formatted for output:
%f is used for real variables,
%d is used for integers,
%c for characters,
%s for strings, ...
When a % is encountered,
the next argument in the input list is formatted
according to the character following the %,
then output is continued until the end of the string or another % is found.
Another % causes the next argument to be formatted and output.
- Certain non-printing characters are represented
by a back-slash and another character,
eg \n for line feed,
\r for return,
\t for tab.
Organisation of programs
Good programs are organised into a number of sections -
each of which is stored in a separate file -
along OO design principles.
Each class is contained in two files:
| File | File name suffix | Contents |
| a specification (or header) file |
.h | Name of class; formal specifications of methods of the class |
a body or implementation file |
.c| Attributes of class; actual code of methods |
|
C allows separate compilation so that a class' specification and body files may be written separately from any program in which they may be used and compiled so that they can be checked for syntactical correctness of the code
Compilation, libraries, etc
|
Producing a program is a three step process:
- Using any suitable text editor,
type out your program code and save it in a series of files,
eg Rectangle.c, Rectangle.h, testrect.c.
- A compiler translates the source into binary machine language (a form of code which the machine can understand and execute). The output of this stage is usually called an object file. Typical compilers will produce files with extensions like .o or .obj from this phase.
(There may be a number of intermediate phases, eg assembly language, but most modern compilers will perform all these intermediate stages at once for you.)
Object files normally contain a number of unresolved references
which would prevent the object from being executed:
these are resolved by the linker.
- A linker combines one or objects into an executable.
The output of this phase can be executed by the target machine - usually by simply typing its name (eg on Unix or DOS systems) or double clicking on the icon (Macintosh or Windows systems).
|
|
On a Unix system, once you have used a text editor to prepare a source file, called, say, x.c. Then you compile it by typing:
cc -c x.c
cc is the name of the compiler (cc = C compiler!); the -c is an option which tells the compiler to stop after it has produced the object and x.c is the name of your text file.
If x.c compiles with no errors, cc will produce a file x.o.
To link objects x.o, y.o and z.o into an executable file, invoke cc again:
cc -o x x.o y.o z.o
This tells cc to produce an executable named x from the three objects.
The option -o is followed by the name of the executable to be produced.
Any name could be used,
but a common convention is to use the same name as the name of the source file containing the
main() function.
On DOS systems, the convention would be to use x.exe as the executable's name.
To compile the code for the Rectangle class, one would use:
cc -c Rectangle.c
which will produce an object Rectangle.o.
Similarly, if your first test program was in a file called test1.c, you would compile it with:
cc -c test1.c
producing an object file test1.o.
These are linked together to produce an executable called test1 by the command:
cc -o test1 test1.o Rectangle.o
You can then run the executable, test1, by simply typing
test1
The link command can have as many object files in it as you like.
(The compile command can also compile multiple source files at a time, but it's more usual to compile source files one by one until all the errors are removed - since each would represent a class if you follow the design strategy being taught in this course - before proceeding to work on another class.)
- portability
- the ability of your computer program to run in essentially the
same way on more than one computer
- stream
- a continuous stream of data either into or out of a computer
- stdin
- the standard input stream - usually the computer's keyboard
(but can be changed to another source of data)
- stdout
- the standard output stream - usually the computer's terminal screen
(but can be changed to another source of data)
- function
- functions in computer programs perform calculations on their
arguments - they are analogous to mathematical functions. We will see
a more formal definition later.
- block
- a block is a syntactic unit of a computer program - in C,
blocks are enclosed in braces { }.
- declaration statements
- declaration statements define labels for objects (or variables)
which will be manipulated in a program block
- compiler
- a program which translates programs in text files into
an intermediate form (an object file) which will be processed by
a linker
- linker
- a program which links together object files created by
a compiler into an executable program
- executable
- a file containing instructions which can be read and
executed directly by a computer:
executable files are not normally humanly readable -
they are patterns of binary bits which have been translated
by the compiler from the original source file (in C or some other
language) to a form understood by the computer
© John Morris, 1997