| 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:
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:
| |||||||||||||||||||||||||||||||||||||||
| 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.
| |||||||||||||||||||||||||||||||||||||||
| 4.1 | Examples
| |||||||||||||||||||||||||||||||||||||||
| 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 ( 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 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.
| |||||||||||||||||||||||||||||||||||||||
| 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 |