Author : Christopher Sawtell
Page : << Previous 5 Next >>
cunarder.displacement,
cunarder.length_of_water_line,
cunarder.number_of_passengers,
cunarder.number_of_crew
);
}
----------------------------------------------------------------------
I'd like to suggest that you compile the program demo1a.c and execute it.
$ cc demo1a.c
$ a.out
Name of Vessel: Queen Mary
Displacement: 97500000000.0 grammes
Water Line: 750.0 metres
Passengers: 3575
Crew: 4592
Which is the output of our totally trivial program to demonstrate
the use of structures.
Tip:
To avoid muddles in your mind and gross confusion in other minds
remember that you should ALWAYS declare a variable using a name which is
long enough to make it ABSOLUTELY obvious what you are talking about.
Storage Classes.
The little dissertation above about the storage of variables was
concerned with the sizes of the various types of data. There is
just the little matter of the position in memory of the variables'
storage.
'C' has been designed to maximise the the use of memory by allowing you
to re-cycle it automatically when you have finished with it.
A variable defined in this way is known as an 'automatic' one. Although
this is the default behaviour you are allowed to put the word 'auto' in
front of the word which states the variable's type in the definition.
It is quite a good idea to use this so that you can remind yourself
that this variable is, in fact, an automatic one. There are three other
storage allocation methods, 'static' and 'register', and 'const'.
The 'static' method places the variable in main storage for the whole
of the time your program is executing. In other words it kills the
're-cycling' mechanism. This also means that the value stored there
is also available all the time. The 'register' method is very machine
and implementation dependent, and also perhaps somewhat archaic in
that the optimiser phase of the compilation process does it all for
you. For the sake of completeness I'll explain. Computers have a small
number of places to store numbers which can be accessed very quickly.
These places are called the registers of the Central Processing Unit.
The 'register' variables are placed in these machine registers instead
of
stack or main memory. For program segments which are tiny loops the
speed
at which your program executes can be enhanced quite remarkably.
The optimiser compilation phase places as many of your variables into
registers as it can. However no machine can decide which of the
variables
should be placed in a register, and which may be left in memory, so if
your program has many variables and two or three should be register
ones
then you should specify which ones the compiler.
All this is dealt with at much greater detail later in the course.
Pointers.
'C' has the very useful ability to set up pointers. These are memory
cells which contain the address of a data element. The variable name is
preceeded by a '*' character. So, to reserve an element of type char
and
a pointer to an element of type char, one would say.
char c;
char *ch_p;
I always put the suffix '_p' on the end of all pointer variables
simply so that I can easily remember that they are in fact pointers.
There is also the companion unary operator '&' which yields the
address of the variable. So to initialize our pointer ch_p to point
at the char c, we have to say.
ch_p = &c;
Note very well that the process of indirection can procede to any
desired depth, However it is difficult for the puny brain of a normal
human to conceptualize and remember more that three levels! So be
careful
to provide a very detailed and precise commentry in your program if
you put more than two or three stars.
Getting data in and out of your programs.
As mentioned before 'C' is a small language and there are no intrinsic
operators to either convert between binary numbers and ascii
characters or to transfer information to and fro between the
computer's memory and the peripheral equipment, such as terminals or
disk stores.
This is all done using the i/o functions declared in the file stdio.h
which you should have examined earlier. Right now we are going to look
at the functions "printf" and "scanf". These two functions together
with their derivatives, perform i/o to the stdin and stdout files,
i/o to nominated files, and internal format conversions. This means
the conversion of data from ascii character strings to binary numbers
and vice versa completely within the computer's memory. It's more
efficient to set up a line of print inside memory and then to send the
whole line to the printer, terminal, or whatever, instead of
"squirting" the letters out in dribs and drabs!
Study of them will give you understanding of a very convenient way to
talk to the "outside world".
So, remembering that one of the most important things you learn in
computing is "where to look it up", lets do just that.
If you are using a computer which has the unix operating system,
find your copy of the "Programmer Reference Manual" and turn to the
page printf(3S), alternatively, if your computer is using some other
operating system, then refer to the section of the documentation which
describes the functions in the program library.
You will see something like this:-
NAME
Page : << Previous 5 Next >>