Storing data. Descriminating types. Declaring data.
A variable is a seqeuence of program code with a name (also called its identifier). A name or identifier in C can be anything from a single letter to a word. The name of a variable must begin with an alphabetic letter or the underscore _ character but the other characters in the name can be chosen from the following groups:
a .. z
(any letter from a to z)
A .. Z
(any letter from A to Z)
0 .. 9
(any digit from 0 to 9)
_
(the underscore character)
Some examples of valid variable names are: a total Out_of_Memory VAR integer etc...
In C variables do not only have names: they also have types. The type of a variable conveys to the the compiler what sort of data will be stored in it. In BASIC and in some older, largely obsolete languages, like PL/1, a special naming convention is used to determine the sort of data which can be held in particular variables. e.g. the dollar symbol $ is commonly used in BASIC to mean that a variable is a string and the percentage % symbol is used to indicate an integer. No such convention exists in C. Instead we specify the types of variables in their declarations. This serves two purposes:
It gives a compiler precise information about the amount of memory that will have to be given over to a variable when a program is finally run and what sort of arithmetic will have to be used on it (e.g. integer only or floating point or none).
It provides the compiler with a list of the variables in a convenient place so that it can cross check names and types for any errors.
There is a lot of different possible types in C. In fact it is possible for us to define our own, but there is no need to do this right away: there are some basic types which are provided by C ready for use. The names of these types are all reserved words in C and they are summarized as follows:
char
A single ASCII character
short
A short integer (usually 16-bits)
short int
A short integer
int
A standard integer (usually 32-bits)
long
A long integer
long int
A long integer (usually 32-bits, but increasingly 64 bits)
float
A floating point or real number (short)
long float
a long floating point number
double
A long floating point number
void
Discussed in a later chapter.
enum
Discussed in a later chapter.
volatile
Discussed in a later chapter.
There is some repetition in these words. In addition to the above, the word unsigned can also be placed in front of any of these types. Unsigned means that only positive or zero values can be used. (i.e. there is no minus sign). The advantage of using this kind of variable is that storing a minus sign takes up some memory, so that if no minus sign is present, larger numbers can be stored in the same kind of variable. The ANSI standard also allows the word signed to be placed in front of any of these types, so indicate the opposite of unsigned. On some systems variables are signed by default, whereas on others they are not.
Declarations:
Where to declare things:
Declarations and Initialization:
Types:
Choosing Variables:
Assigning variables to one another:
Types and The Cast Operator:
Storage class register static and extern:
Functions types:
Questionsdeclare:
Node:Declarations, Next:Where to declare things, Previous:Variables, Up:Variables
Declarations
To declare a variable in a C program one writes the type followed by a list of variable names which are to be treated as being that type: typename variablename1,..,..,variablenameN;
For example: int i,j; char ch; double x,y,z,fred; unsigned long int Name_of_Variable;
Failing to declare a variable is more risky than passing through customs and failing to declare your six tonnes of Swiss chocolate. A compiler is markedly more efficient than a customs officer: it will catch a missing declaration every time and will terminate a compiling session whilst complaining bitterly, often with a host of messages, one for each use of the undeclared variable.
Node:Where to declare things, Next:Declarations and Initialization, Previous:Declarations, Up:Variables
Where to declare things
There are two kinds of place in which declarations can be made, See Scope. For now it will do to simply state what these places are.
One place is outside all of the functions. That is, in the space between function definitions. (After the #include lines, for example.) Variables declared here are called global variables. There are also called static and external variables in special cases.) 2. #include
The other place where declarations can be made is following the opening brace, {}, of a block. Any block will do, as long as the declaration follows immediately after the opening brace. Variables of this kind only work inside their braces {} and are often called local variables. Another name for them is automatic variables. 14. main ()15. 16. { int a;17. float x,y,z;18. 19. /* statements */20. 21. }22.
or function () { int i; /* .... */ while (i < 10) { char ch; int g; /* ... */ } }
Node:Declarations and Initialization, Next:Types, Previous:Where to declare things, Up:Variables
Declarations and Initialization
When a variable is declared in C, the language allows a neat piece of syntax which means that variables can be declared and assigned a value in one go. This is no more efficient than doing it in two stages, but it is sometimes tidier. The following: int i = 0; char ch = 'a';
are equivalent to the more longwinded int i;char ch; i = 0;ch = 'a';
This is called initialization of the variables. C always allows the programmer to write declarations/initializers in this way, but it is not always desirable to do so. If there are just one or two declarations then this initialization method can make a program neat and tidy. If there are many, then it is better to initialize separately, as in the second case. A lot means when it starts to look as though there are too many. It makes no odds to the compiler, nor (ideally) to the final code whether the first or second method is used. It is only for tidiness that this is allowed.
Node:Types, Next:Choosing Variables, Previous:Declarations and Initialization, Up:Variables
Individual Types
char:
Example special chars:
integers:
Float:
Node:char, Next:Example special chars, Previous:Types, Up:Types
char
A character type is a variable which can store a single ASCII character. Groups of char form strings. In C single characters are written enclosed by single quotes, e.g. 'c'! (This is in contrast to strings of many characters which use double quotes, e.g. "string") For instance, if ch is the name of a character: char ch; ch = 'a';
would give ch the value of the character a. The same effect can also be achieved by writing: char ch = 'a';
A character can be any ASCII character, printable or not printable from values -128 to 127. (But only 0 to 127 are used.) Control characters i.e. non printable characters are put into programs by using a backslash \ and a special character or number. The characters and their meanings are:
\b
backspace BS
\f
form feed FF (also clear screen)
\n
new line NL (like pressing return)
\r
carriage return CR (cursor to start of line)
\t
horizontal tab HT
\v
vertical tab (not all versions)
\"
double quotes (not all versions)
\'
single quote character '
\\
backslash character \
\ddd
character ddd where ddd is an ASCII code given in octal or base 8, See Character Conversion Table.
\xddd
character ddd where ddd is an ASCII code given in hexadecimal or base 16, See Character Conversion Table.
Node:Example special chars, Next:integers, Previous:char, Up:Types
Listing/***************************************************//* *//* Special Characters *//* *//***************************************************/ #include
The output of this program is: Beep! (and the BELL sound )ch = 'a' <- Start of this line!!
and the text cursor is left where the arrow points. It is also possible to have the type: unsigned char
This admits ASCII values from 0 to 255, rather than -128 to 127.
Node:integers, Next:Float, Previous:Example special chars, Up:Types
Integers
Whole numbers
There are five integer types in C and they are called char, int, long, long long and short. The difference between these is the size of the integer which either can hold and the amount of storage required for them. The sizes of these objects depend on the operating system of the computer. Even different flavours of Unix can have varying sizes for these objects. Usually, the two to remember are int and short. int means a `normal' integer and short means a `short' one, not that that tells us much. On a typical 32 bit microcomputer the size of these integers is the following: Type Bits Possible Values short 16 -32768 to 32767unsigned short 16 0 to 65535 int 32 -2147483648 to 2147483647long 32 (ditto)unsigned int 32 0 to 4294967295long long 64 -9e18 to + 8e18
Increasingly though, 64 bit operating systems are appearing and long integers are 64 bits long. You should always check these values. Some mainframe operating systems are completely 64 bit, e.g. Unicos has no 32 bit values. Variables are declared in the usual way: int i,j; i = j = 0;
or short i=0,j=0;
Node:Float, Previous:integers, Up:Types
Floating Point
There are also long and short floating point numbers in C. All the mathematical functions which C can use require double or long float arguments so it is common to use the type float for storage only of small floating point numbers and to use double elsewhere. (This not always true since the C `cast' operator allows temporary conversions to be made.) On a typical 32 bit implementation the different types would be organized as follows: Type Bits Possible Values float 32 +/- 10E-37 to +/- 10E38double 64 +/- 10E-307 to +/- 10E308long float 32 (ditto)long double ???
Typical declarations: float x,y,z;x = 0.1;y = 2.456E5z = 0; double bignum,smallnum;bignum = 2.36E208;smallnum = 3.2E-300;
0 comments: on " "
Post a Comment