| City & Guilds 7261/224 | Coding and Programming in C II  |
| 1 | COMMENTS
In C,
For example: Comments should enlighten the reader, not restate the obvious; for example: ptr++; /* Increment pointer */is not helpful - all C programmers know that ++
increments. A more useful comment would be:
ptr++; /* Point to next character in string */This course requires (C&G Ref: 7261/224, Section 3.3) that comments are used appropriately. Too many comments are as bad as too few. Don't write code that has more comments than statements. Be succinct and pertinent. The examples in this course will show how to use comments appropriately. | |||||||||||||||||||||||||||||||||
| 1.1 | Programming Style
This course requires (C&G Ref: 7261/224, Section 3.1) that
indentation be used to emphasise the logical flow of your
programs. As a matter of style, three space indentation should be
used (indentation of 2 to 4 spaces inclusive has been found to be
the optimum for readability - sorry, that means using tabs is
out). Always prefix your programs with a comment which contains the
source file name, a colon, then a description of the paper and
question number. The next line should contain a comment with your
name on it. Prefix /* example.c: Lexical Components, Question 999 */ /* Martin O'Brien */ #include
This format will serve you well later in the course, as your source
file becomes full of different function definitions; and as your
diskette becomes full of various different source files. | |||||||||||||||||||||||||||||||||
| 2 | KEYWORDS
Table 1 shows the C keywords. These must be written in lower
case. Keywords marked with a grey background are new with the ANSI
standard.
| |||||||||||||||||||||||||||||||||
| 3 | IDENTIFIERS
One use for identifiers is to name data. An identifier is a
sequence of letters and digits. The first character must be a
letter; the underscore Identifiers may have any length; the ANSI standard states that the compiler must recognise the first 31 characters. The C language keywords listed in Table 1 cannot be used as identifiers.
Identifiers should be chosen so that they indicate the purpose of
the data. Typically, in C, | |||||||||||||||||||||||||||||||||
| 2 | Examples of Valid Identifiers
|
Which of the following identifiers are invalid, and why?
| 1 | a b c | 11 | Signed | ||
| 2 | am_i_valid | 12 | parameter_list | ||
| 3 | int | 13 | RATIO | ||
| 4 | INT | 14 | 3d_maze | ||
| 5 | Int | 15 | -val | ||
| 6 | INT-6 | 16 | ifs | ||
| 7 | 5ive | 17 | radiuS | ||
| 8 | _i | 18 | max height | ||
| 9 | ____ | 19 | max-height | ||
| 10 | hash_value | 20 | max_height |