Constant and Variable Types
Constant and Variable Types
Variables
A declaration begins with the type, followed by the name of one or more
variables. For example,
All of the integer types plus the char are called the integral types. float and
double are called the real types.
Variable Names
Every variable has a name and a value. The name identifies the variable, the
value stores data. There is a limitation on what these names can be. Every
variable name in C must start with a letter, the rest of the name can consist of
letters, numbers and underscore characters. C recognises upper and lower case
characters as being different. Finally, you cannot use any of C's keywords like
main, while, switch etc as variable names.
The rules governing variable names also apply to the names of functions. We
shall meet functions later on in the course.
Global Variables
Local variables are declared within the body of a function, and can only be
used within that function. This is usually no problem, since when another
function is called, all required data is passed to it as arguments. Alternatively, a
variable can be declared globally so it is available to all functions. Modern
programming practice recommends against the excessive use of global
variables. They can lead to poor program structure, and tend to clog up the
available name space.
A global variable declaration looks normal, but is located outside any of the
program's functions. This is usually done at the beginning of the program file,
but after preprocessor directives. The variable is not declared again in the body
of the functions which access it.
External Variables
Where a global variable is declared in one file, but used by functions from
another, then the variable is called an external variable in these functions, and
must be declared as such. The declaration must be preceeded by the word
extern. The declaration is required so the compiler can find the type of the
variable without having to search through several source files for the
declaration.
Global and external variables can be of any legal type. They can be initialised,
but the initialisation takes place when the program starts up, before entry to the
main function.
tatic Variables
Another class of local variable is the static type. A static can only be accessed
from the function in which it was declared, like a local variable. The static
variable is not destroyed on exit from the function, instead its value is
preserved, and becomes available again when the function is next called. Static
variables are declared as local variables, but the declaration is preceeded by the
word static.
Constants
Character constants are usually just the character enclosed in single quotes;
'a', 'b', 'c'. Some characters can't be represented in this way, so we use a 2
character sequence.
In addition, a required bit pattern can be specified using its octal equivalent.
Character constants are rarely used, since string constants are more convenient.
A string constant is surrounded by double quotes eg "Brian and Dennis". The
string is actually stored as an array of characters. The null character '\0' is
automatically placed at the end of such a string to act as a string terminator.
We will meet strings and characters again when we deal with the input / output
functions in more detail.
Arrays
We have already met single dimensioned arrays which are declared like this
int results[20];
Arrays can have more dimensions, in which case they might be declared as
int results_2d[20][5];
int results_3d[20][5][3];
Each index has its own set of square brackets.
Where an array is declared in the main function it will usually have details of
dimensions included. It is possible to use another type called a pointer in place
of an array. This means that dimensions are not fixed immediately, but space
can be allocated as required. This is an advanced technique which is only
required in certain specialised programs.
When passed as an argument to a function, the receiving function need not
know the size of the array. So for example if we have a function which sorts a
list (represented by an array) then the function will be able to sort lists of
different sizes. The drawback is that the function is unable to determine what
size the list is, so this information will have to be passed as an additional
argument.
return(total);
}