| City & Guilds 7261/224 | Coding and Programming in C II  |
| 1 | INTRODUCTION
Two types of data can be used in C programs: constants and variables. This paper discusses variables. A variable is a named area of storage (also called object), and must have:
There are others, but they are beyond the scope of this course. For example, the value stored in a variable of type int
will be regarded as a whole number (no fractional part). Its
allowable range of operations will include addition, subtraction,
division and multiplication. Its memory requirements might be four
bytes. | ||||||||||
| 2 | BASIC VARIABLE TYPES
The three basic data types in C of interest to us in this course
are characters (char), integers (int) and
floating-point (float). | ||||||||||
| 2.1 | Char
A character variable stores character codes. It can hold any
member of the execution character set. The value actually held
depends on the character set being used. Don't assume it's ASCII. A
character variable occupies a byte of storage. The number of bits
in a byte is defined by the Standard C macro CHAR_BIT,
declared in <limits.h>. It's usually 8, but don't
count on it. The range of values of a character variable, and
whether it's signed or unsigned is implementation-dependent. If
plain char is signed then its range of values is
CHAR_MIN to CHAR_MAX inclusive. If it's
unsigned then its range is 0 to
UCHAR_MAX. These macros are defined in the header file
<limits.h>. | ||||||||||
| 2.2 | Int
An integer variable holds numbers with no fractional part. Printable character values stored in a variable of type char can be safely stored in an int: C guarantees that any printable character will have a positive value. Be careful when copying a char whose leftmost bit is 1 - it may be converted to a negative integer, or become positive. The memory requirements of an integer variable reflects the size
suggested by the host architecture. The range of values of an
integer variable is implementation-dependent, but at a minimium
must be -32767 to +32767. The actual limits for your implementation
will be specified by the macros INT_MIN and
INT_MAX, in <limits.h>. | ||||||||||
| 2.3 | Float
A floating-point variable holds numbers with a fractional
part. The range of values is implementation-dependent and specified
by the macros FLT_MIN and FLT_MAX defined in
<float.h>. | ||||||||||
| 3 | ARRAYS
A collection of elements of the same type is called an array. An
array is commonly used in C to implement a string, that is, an
array of characters. | ||||||||||
| 4 | DECLARATION OF VARIABLES
A declaration specifies the properties associated with the
variables listed. Declarations of variables, at present, should be
made after the opening brace of the function main. | ||||||||||
| 4.1 | Basic Variable Types
Basic variable types are created by declarations of the form: type-specifier identifier ;where type-specifier is one of the keywords char, int, or float. Examples: int i, count, nflag; char ch; float radius, area;Variables can be initialised at the point of declaration using the assignment operator =, which copies the value or constant on the right side to the variable on the left side; for example: int i, count = 0, nflag; float f = 3.7;declares i, count and nflag as integer variables, with count initalised to zero and the others containing garbage. A floating-point variable f is declared, initialised to 3.7. Variables initialised at the point of declaration can be difficult to see, so the above sequence could be re-written: int i, count, nflag; /* declare the variables */ float f; count = 0; /* initialise the variables */ f = 3.7; | ||||||||||
| 4.2 | Arrays
The format for the declaration of a one-dimensional array is: type-specifier identifier[constant-expression] ;where type-specifier is one of the keywords char, int, or float and constant-expression evaluates to an integer. Examples: int arr[4]; /* creates array arr[0]...arr[3] */ float f1[9], f2[5], f3[7]; char s[5]; /* holds a string of four characters + '\0' */To access an element use the form identifier[index] where index is any integer expression; for example: arr[0] = 15; f3[i] = f2[i*2]; f3[i+3] = f1[arr[1]]; s[0] = 'A';where all variables used on the right side of the assignment operator have been initialised prior to the actual assignment. | ||||||||||
| 4.2.1 | Notes
|