Skip to main content
Top

1993 | Book | 3. edition

A Book on C

Authors: B. A. E. Meekings, T. P. Kudrycki, M. D. Soren

Publisher: Macmillan Education UK

Book Series : Computer Science Series

insite
SEARCH

Table of Contents

Frontmatter
Introduction
Abstract
Programming is communication. In attempting to teach a programming language we are trying to provide the learner with a means of communication, a means of expressing himself or herself. At first sight it will appear that the communication will be one way, between the program writer and the machine on which his or her program is processed. This view is too simplistic, for the communication occurs on a number of different levels.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
1. Program Structure
Abstract
In the introduction we attempted to show that programming must be undertaken in a disciplined and organised manner. If the resulting program is to display the benefit of this approach then the programmer must be thoroughly familiar with the program structure dictated by the programming language that he, or she, is to use.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
2. Functions
Abstract
As we have seen in the previous chapter, functions offer an easy way to construct a modular program. Since they are such an essential part of good C programming we shall introduce their facilities at an early stage to encourage familiarity with their use.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
3. Output and Input
Abstract
Our use of the output function printf so far been straightforward. We have seen that, as well as printing text strings, it can easily be made to convert the internal form of our data items into a suitable form for printing. The general form of the printf function call can be expressed as (The square brackets enclose an item that is optional.) The control string may contain characters to be printed, special control characters preceded by backslash, and conversion specifiers.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
4. Decisions
Abstract
A programming language that only offered the possibility of moving from one instruction to the next instruction in sequence would be extremely limiting. To be useful, we must be provided with the facility to choose different courses of action under different circumstances. There are two distinct ways that this may be done in C. We can use either the conditional statement or the switch statement.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
5. Loops
Abstract
The conditional statements of the previous chapter freed our programs from the straitjacket of the sequential execution of instructions without branching, but it is the ability to loop, or repeat the execution of one or more instructions, that brings power to programming. It brings economy too, for a modest number of programming language statements can be responsible for a significant amount of computing time.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
6. Operators
Abstract
In preceding chapters we have used identifiers with type char, int, and float. In addition, data types void, short, long, double, signed, unsigned and long double are provided in standard C. Most of these data types are also provided in most non-standard compilers. We suggest that you look at the implementation notes for C on your system to discover what is on offer.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
7. Arrays
Abstract
In the examples used so far each data item that we wished to manipulate has been given a name, or identifier. Each identifier has associated with it a type, and a storage class. This association is made explicit through the declaration. But so far any identifier has represented a numeric value of one type or another, or a character. Consider again example 4.3 in which we produced a grade for a given mark. If we now change the specification of the problem, to ask that we produce the number of times that each grade was achieved, the statements in example 7.1 could appear in a suitable loop.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
8. More Data Types
Abstract
In our discussion so far, all data types of identifiers have been simple: they consist of one elementary type. The elementary types are:
(char)
characters
(int)
integers
(float)
floating point
Chars and ints can be either signed or unsigned, and ints and floats can have modifiers short or long. A “long float” is referred to as a “double.” Unless otherwise explicitly stated in a declaration, the default type is int.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
9. Pointers Revisited
Abstract
Our use of pointers so far has been largely restricted to the processing of character strings. In this chapter we will explore much more imaginative uses of this very powerful feature of C. In particular, we will need pointers to simplify the handling of the data structures that are typical of more complex programs.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
10. The C Preprocessor
Abstract
We have already introduced the C preprocessor directives #include and #define for file inclusion and symbol definition capabilities. In this chapter, we expand the discussion to include all other capabilities of the preprocessor such as the #undef directive, the use of the conditional compilation directives #if, #ifdef, #ifndef, #else, #elif and #endif, string concatenation and token pasting. In addition, parameters for the #define directive are introduced to yield a more powerful macro facility. Some of these facilities have been defined recently in the ANSI standard and may not be available on your implementation.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
11. Programming Style
Abstract
Programming in any language is a skill acquired largely by experience and by observing the example of others. The way in which your programs are presented is a matter for personal taste. It is often a tradeoff between brevity and intelligibility. Although programming ‘style’ is often considered to be unquantifiable and assessable only in subjective terms, we have made an attempt, in an appendix, to identify those features of program layout and organisation that tend to make it more visually appealing and more easily comprehensible.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
12. The Standard C Libraries
Abstract
The definition of standard C specifies a library which must be present in any conforming implementation. The functions supplied in the library are declared in several header files, divided according to the type of services provided by the functions. Many of the functions defined by the standard are supplied even in older implementations and most of them are probably present on your system, even though the original C definition did not specify any of them. The header files declaring the library functions also specify macros and variables which may be used by the applications to accomplish various tasks as described below. Additionally, the headers may define macros with the same names as the corresponding library functions, provided that the macros can safely be used in any place and with any set of arguments with which the corresponding function can be called. In particular, it must be guaranteed that macro arguments are evaluated only once. The applications can always ensure that a true library function rather than a macro is called by using the #undef preprocessor directive. It can also be done locally by enclosing the function name in parentheses. For syntactic reasons, the closing parenthesis will prevent the interpretation of the identifier as a macro.
B. A. E. Meekings, T. P. Kudrycki, M. D. Soren
Backmatter
Metadata
Title
A Book on C
Authors
B. A. E. Meekings
T. P. Kudrycki
M. D. Soren
Copyright Year
1993
Publisher
Macmillan Education UK
Electronic ISBN
978-1-349-12804-4
Print ISBN
978-0-333-56919-1
DOI
https://doi.org/10.1007/978-1-349-12804-4