Datasheet
55
Appendix C. The C Language
C.1. Introduction
This appendix will not turn you into an expert C programmer, nor is it even a decent tutorial. it will,
however, give you a little bit to get started with.
C is actually quite a simple language. There are only a handful of keywords and precious few rules.
Indeed, this lack of rules does tend to be difficult for folks coming from older languages such as Basic or
FORTRAN.
In this course, we won't be making use of a lot of elaborate code. Embedded
1
applications by their nature
tend to be simple. Even someone unfamiliar with programming should have little problem following the
code.
There are a few things about C that take a little getting used to. The following few paragraphs outline the
most obvious:
All whitespace is created equal
In C, a space, tab and newline are all called whitespace. Any combination of these characters is also
whitespace. Thus, a tab is the same as a space, as is three newlines followed by a space, or fourteen
tabs. They are all equivalent to a single space. One result is that the end of a line has nothing whatsoever
to do with the end of a statement. Statements may cross line boundaries with impunity. The exception is
a string literal. String literals are not allowed to cross line boundaries. However, there are ways of writing
newlines in literal strings.
Everything is case sensitive
Identifiers, keywords, anything C cares about is case sensitive. Thus, A has nothing to do with a, if is a
keyword while IF is not.
C will not try to out-guess you
In many languages, the compiler will prevent you from doing really stupid things. Not so in C. If you
wrote it, the compiler assumes you meant it, no matter how silly it may be. One fairly obvious place
where people can go wrong is in arrays. If you declare an array of, say, ten integers, and then access
the hundredth element of that array, C will assume that is exactly what you meant, and merrily return
whatever is in memory where the hundredth element of that array would have been, had it actually been
1
The Embedded Systems Glossary[Barr] provides the following definition for an embedded system, focusing largely on the
application: "embedded system n. A combination of computer hardware and software, and perhaps additional mechanical or other
parts, designed to perform a dedicated function. In some cases, embedded systems are part of a larger system or product, as in the
case of an antilock braking system in a car."
Wikipedia[WP1] focuses more on the hardware: "An embedded system is a computer system designed for specific control functions
within a larger system, often with real-time computing constraints. It is embedded as part of a complete device often including
hardware and mechanical parts. By contrast, a general-purpose computer, such as a personal computer (PC), is designed to be
flexible and to meet a wide range of end-user needs. Embedded systems control many devices in common use today."