04-12-2012, 05:47 PM
Programming in C
Programming in C.pdf (Size: 191.97 KB / Downloads: 21)
Introduction
About this Userguide
This userguide was written to complement the OUCS course Programming in C, but
also to serve as a reference to the essential aspects of C. It aims to introduce the C
language to those already familiar with programming in another language (as covered
on Introduction to programming in Pascal or Introduction to programming in Visual
Basic courses). It will show how fundamental programming structures are
implemented using the syntax of C.
This guide and the OUCS course will teach ANSI C (see [5]), but where it differs
significantly from Kernighan and Ritchie (K&R) C, the K&R method may have been
included. It is useful to see the K&R variants, as it is possible that you will encounter
K&R code.
Exercises are included in Chapter 18, along with a list of which exercises follow each
chapter. A set of solutions to these examples can be found in the OUCS user guide l9.5
Programming in C - Exercise Solutions [10] and are available on the World Wide Web
from the URL.
Why use C?
C (and its object oriented version, C++) is one of the most widely used third generation
programming languages. Its power and flexibility ensure it is still the leading choice for
almost all areas of application, especially in the software development environment.
Many applications are written in C or C++, including the compilers for other
programming languages. It is the language many operating systems are written in
including Unix, DOS and Windows . It continues to adapt to new uses, the latest being
Java, which is used for programming Internet applications.
C has many strengths, it is flexible and portable, it can produce fast, compact code, it
provides the programmer with objects to create and manipulate complex structures (e.g
classes in C++) and low level routines to control hardware (e.g input and output ports
and operating system interrupts). It is also one of the few languages to have an
international standard, ANSI C [5]. The background and advantages of C are covered
in more detail in Chapter 15 (see page 65).
Variable Names
Variable and function names (or identifiers) consist of a letter followed by any
combination of letters and digits. Underscore (_) is treated as a letter and it is possible
have identifiers that begin with an underscore. However, identifiers beginning with an
underscore followed by either another underscore or an upper case letter are reserved,
and identifiers beginning with an underscore followed by a lower case letter are
reserved for file scope identifiers. Therefore, use of underscore as the first letter of an
identifier is not advisable.
Upper and lower case letters are distinct, so fred and Fred are different
identifiers. ANSI C states that the first 31 characters of an identifier are significant
except for objects with external linkage. External variables are those declared outside
of any function, all functions are external; all external objects (variables and functions)
have external linkage unless they are declared to be static (see 7.2).
Parsing Rules ‡
The C compiler parses a C program by splitting up the program into its constituent
words and operators. Each separate entity is called a token. The C compiler uses a
simple rule when deciding where to split the C source. The compiler bites off as large a
chunk of your program as possible which will form a legal C token. This mechanism is
called greedy lexical analysis. Possible ambiguities when using the increment
operators can be eliminated if the greedy rule is followed.
Literal Constants
We need some method of writing literal values in our C programs. The most obvious is
the ability to write integer values in decimal form (to base 10); characters, digits and
punctuation; floating point numbers with decimal places and strings. C also allows the
writing of hexadecimal and octal constants.