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


1 INTRODUCTION

This paper discusses four kinds of constant: integer, floating, character and string.

2 INTEGER CONSTANTS

Integer constants can be expressed as decimal, octal or hexadecimal. A sequence of digits is taken as decimal unless it begins with 0 (zero), in which case it's taken as octal. If the digits are preceded by 0x or 0X, it's taken as hexadecimal.

Examples:
456 decimal constant
0710 octal constant
0x1C8 hexadecimal constant
0X1C8 hexadecimal constant

Integer constants may be prefixed with a unary minus or a unary plus, e.g. -356, +34. + is assumed if neither are present.

3 FLOATING CONSTANTS

The full form of a floating constant is:
(a) int-part.fract-partEexpon 1.496e11 m; mean distance to the Sun
(b) .fract-partEexpon .5101E15 m2; surface area of the Earth
(c) .fract-part .01745 pi/180
(d) int-part.Eexpon 10.e9 Giga (G)
(e) int-part. 27.
(f) int-partEexpon 10e9 Giga (G)
(g) int-part.fract-part 3.14159265 pi

4 CHARACTER CONSTANTS

A character constant is a character enclosed in single quotes, as in 'A'. The value of a character constant is the character's value in the machine's character set; this means that a character constant has a positive integer value. In ASCII, the character code for the character constant 'A' is 65, but this information is not useful.

The following escape sequences allow non-printable characters, and values in hexadecimal or octal, to be expressed.

Note that mmm is one to three octal digits (0...7); hh is one or more hexadecimal digits (0...9, a...f, A...F).

If the character following the \ is not one of those specified, the behaviour is undefined.

Escape Sequence Character Represented ASCII Mnemonic
\a Audible alert BEL
\b Backspace BS
\f Form feed FF
\n Newline NL (LF)
\r Carriage Return CR
\t Horizontal tab HT
\v Vertical tab
\\ Backslash
\' Single quote
\" Double quote
\mmm Octal number
\xhh Hexadecimal number
4.1 Examples

  • '\t' represents a horizontal tabulation.
  • '\x7F' is a character constant with the hexadecimal value 7F (decimal 127)
  • '\0' is a character constant used frequently in C. It has the value zero (ASCII NUL)
5 STRING CONSTANTS

A string constant (also known as string literal) is a sequence of zero or more characters enclosed in double quotes, for example

 "Hello, world"   "A"   ""
"" is an empty string. The escape sequences listed for character constants can be used in strings, for example, we may want to represent a double quote in a string constant:
      "He said \"Good morning.\" to me." 
will be displayed as: He said "Good morning." to me.

A string is stored as an array of characters. Each element contains the character code of the respective character. Null ('\0', that is, the value zero) is appended to the string by the compiler so the total length of the array is the number of characters in the string plus one.

The type of this is "array of characters," and its storage class is static. You do not need to understand the term 'static' in this course, but just be aware that the behaviour is undefined if you try to alter it.

6 #define Preprocessor Directive

#define allows you to name any type of constant embedded in your source code:

   #define   MAXPAGE   66
   #define   SPACE     ' '
This allows you to use the words MAXPAGE and SPACE in your code, instead of the rather meaningless 66 and difficult to see ' '. The preprocessor simply substitutes the symbols on the right for the symbolic constant. A symbolic constant must conform to the syntax of an identifier (see Lexical Components, Section 3) but it is C practice always to use upper case, and to place the definitions at the beginning of the source file, after any #include preprocessor directives. This tutorial will use symbolic constants in the example code when appropriate.


EXERCISE 1

1 For each of the following decide if it is a valid or invalid constant. If it's valid, specify its type; if it's invalid, explain why.
   (a) +9      (b) '/f'      (c) '"'      (d) "'"      (e) '0'
   (f) ' '     (g) 476       (h) "0"      (i) 0x7F     (j) 097   
   (k) 0       (l) 'mins'    (m) '\''     (n) '\0'     (o) "\""   
   (p) 200e3
 
2 Describe how the character constant 'A' and the string constant "A" are stored.

3 Identify each of the following as either a string constant or a character constant.
   (a) 'Q'   (b) "Q"   (c) "ABC"   (d) '\t'   (e) "23\t45"

4 What are the types of the following constants?
   (a) 0x17   (b) 027   (c) 0X17   (d) 23.   (e) 23.0

Back to C Home Page


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

Last update: 9 May 1999