Site hosted by Angelfire.com: Build your free website today!
 City & Guilds 7261/224 Coding and Programming in C II 

Lexical Components

1 COMMENTS

In C, /* begins a comment and */ ends it; it can span several lines. Comments do not nest.

For example: /* This is a comment */

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 main with a comment describing the purpose of the program. Here's an example:

   /* example.c: Lexical Components, Question 999  */
   /* Martin O'Brien                               */
   #include 

   /* main: display greeting on screen */
   int main(void)
   { 
      printf("Hello everybody.\n");
      return 0;
   }  /* end function 'main' */


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.
Table 1: C Keywords
auto const double float int short struct unsigned
break continue else for long signed switch void
case default enum goto register sizeof typedef volatile
char do extern if return static union while


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 _ counts as a letter. Upper and lower case letters are regarded as different.  Though legal, use of underscores to start identifier names should be avoided.  See this link for much more information (well above the requirements for City & Guilds).

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, c and ch are used to identify variables of type char; i is used to indicate an int type datum, s is used to indicate a datum of type string. There short identifiers are used as local, temporary identifiers, and not for data included in a Data Dictionary, where longer and more meanigful names would be used.

2 Examples of Valid Identifiers

c, valid_message, and DO are examples of valid identifiers; the underscore is useful for separating words because space cannot be used. DO is not a keyword because upper and lower case are treated differently. Note that C identifiers are normally written in lower case, but upper case is valid. Upper case is normally reserved for symbolic constants created using the #define preprocessor directive.



EXERCISE 1

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


Back to C Home Page


Martin O'Brien <martin.o_brien@which.net>

Last update: 9 May 1999