Skip to main content
main-content

Über dieses Buch

The programming language C occupies an unusual position midway between conventional high-level and assembly languages, allowing the programmer to combine the best features of both. This book is an introduction to the language itself, and to the special style of thinking that goes with it. Anyone wishing to learn C is likely to have some experience in a high-level language such as BASIC or Pascal, and it seems sensible to make use of that experience. We therefore assume some facility with conventional notation for computer arith­ metic, and simple notions (such as looping and branching) common to most high-level languages. However, that cannot be the whole story. One cannot learn to speak colloquial French by thinking in English and performing a routine translation. No more can one learn to program in colloquial C by thinking in BASIC and performing a routine translation. However, when learning French it is normal to assume familiarity with English, building on that in the early stages, thereby creating the confidence necessary to provide that mot juste to which nothing corresponding exists in English. Our approach to C is similar. In particular we do not introduce at the very beginning some of the features of C which eventually lead to more efficient and elegant code-for example, the ability to do several things, apparently at once. Initially, such constructs can be confusing. Once the reader has acquired some facility with the language it then becomes possible to bring these features into play in a natural manner.

Inhaltsverzeichnis

Frontmatter

Chapter 1. Compilers and Interpreters

Abstract
Any high-level language program must be converted to the native code (or machine language) of the processor on which it ultimately is to run. There are two common approaches to this problem (and a number of less common ones which need not concern us here). The first is to store the program, more or less as it is entered, in the computer’s main memory (this is known as source code). Then, when the program is executed, to take each line in turn, translate it into machine code and then run the machine code. When the translation of a line takes place, the resulting machine code overwrites that from the previous line, so that if a line of source code appears in a loop which is executed 200 times, it also must be translated 200 times. A translator which adopts this strategy is called an interpreter* and it’s the mechanism most familiar to anyone who has used a Commodore 64, Macintosh, IBM PC, or any of the other home micros (or personal computers as the upmarket salesmen prefer to call them).
Robin Jones, Ian Stewart

Chapter 2. The Skeleton of a C Program

Abstract
Let’s now turn our attention to what a C program looks like. All C programs consist of a series of functions. A function is somewhat like a BASIC subroutine. The primary difference is that a C function has a built-in mechanism for communicating values to the program which called it (and for accepting values from the calling program).
Robin Jones, Ian Stewart

Chapter 3. Loops and Control Constructs

Abstract
All modern computer languages provide a number of control structures that allow you to execute a piece of code once or repeatedly as long as some condition is true. In BASIC we have IF..THEN (and sometimes IF.. THEN.. ELSE) and FOR.. NEXT.
Robin Jones, Ian Stewart

Chapter 4. Arithmetic and Logic

Abstract
It seems a little late to introduce arithmetic now, when we’ve been using it with wild abandon for the last two chapters. Up to now, though, all the symbols I have used have had the same meanings in BASIC, except for the increment and decrement operators, which I introduced separately.
Robin Jones, Ian Stewart

Chapter 5. Strings, Arrays, and Pointers

Abstract
Thus far in our discussions, there have been two glaring omissions: any reference to string handling and any reference to arrays. In most BASICs, a string is held in an array, but the connection is not made explicit. In C, the relationship between strings and arrays is much more evident, so we can kill two birds with one stone.
Robin Jones, Ian Stewart

Chapter 6. Floats and Other Types

Abstract
So far, I’ve limited the variable types discussed to int and char. There are a number of others, but not all C compilers support all of them.
Robin Jones, Ian Stewart

Chapter 7. Input

Abstract
I’ve said nothing at all yet about how to get things into the machine in C, and ittle enough about getting stuff out again. I’ll remedy these omissions in the next two chapters.
Robin Jones, Ian Stewart

Chapter 8. Output

Abstract
We’ll now turn out attention to more ways of getting the machine to talk to us.
Robin Jones, Ian Stewart

Chapter 9. More About Control Constructs

Abstract
In Chapter 3, I introduced most of the control constructs C offers us. But there are others, and there also are some additional keywords that can modify the effect of the loops we’ve already met.
Robin Jones, Ian Stewart

Chapter 10. Recursion

Abstract
Among the first things for which children are berated when they are learning their mother tongue is any attempt at a circular definition. “A chair is a chair with...” “You can’t explain something by using the word you’re explaining!” they’re told firmly.
Robin Jones, Ian Stewart

Chapter 11. Structures

Abstract
Back in Chapter 6, I dropped some heavy hints about the usefulness of structures, and then annoyingly said no more. I’ll rectify that omission here.
Robin Jones, Ian Stewart

Chapter 12. File-handling

Abstract
Very early on in this book I remarked on the satisfyingly consistent nature of C compilers. Generally, sins are those of omission (float or typedef may not be implemented for instance) rather than those of juggling with the syntax or giving library functions nonstandard features.
Robin Jones, Ian Stewart

Chapter 13. Debugging

Abstract
Whenever you write computer programs, there are two Golden Rules to bear in mind:
  • Golden Rule 1. Anything that can go wrong, will.
  • Golden Rule 2. Anything that can’t go wrong, probably will too. In other words, cats miaow, dogs bark, politicians tell imaginative versions of the truth, and programmers make mistakes.
Robin Jones, Ian Stewart

Chapter 14. Rational Arithmetic

Abstract
Most modern high-level languages offer you a choice of integers or floating point arithmetic. Generally, so far as the programmer is concerned, that choice is dictated by the kinds of numbers he or she wants to play with; if you want fractional values, you’ve got to use float.
Robin Jones, Ian Stewart

Chapter 15. Implementing Turtle Graphics...

Abstract
Turtle Graphics were invented by Seymour Papert in the 1960’s and form the best known part of his introductory programming language, Logo. The idea is that lines can be drawn on the screen by an (imaginary) turtle which happens to be carrying a pen around with it. It can be commanded to head off in any given direction, travel a specific distance, lower the pen so that it leaves a track as it goes, or raise it so that no trace is left. In some implementations the turtle is a robot, and the name is more appropriate, but the side effects on carpets can be undesirable.
Robin Jones, Ian Stewart

Chapter 16. ... and Using Them

Abstract
Now comes the fun: mobilizing our faithful turtle in pursuit of art, science, and technological know-how. I’ve written this Chapter assuming the unsophisticated Integer Turtle Graphics is in use; but apart from changes to the type of variables and the standard initialization, the same programs will work with higher-precision Turtle Graphics.
Robin Jones, Ian Stewart

Chapter 17. Random Thoughts

Abstract
As any programmer knows, random numbers can be extremely useful animals, particularly in simulations and games programs. And, of course, it’s a simple matter to get hold of them; simply use a function called RND or rand or random which produces them between zero and one, or between limits you define, like rabbits out of a hat. Your C library almost certainly has at least one function that does the job for you.
Robin Jones, Ian Stewart

Backmatter

Weitere Informationen