home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
languages
/
c
/
c_tutor
/
CHAP4_TXT
< prev
next >
Wrap
Text File
|
1987-11-21
|
32KB
|
718 lines
Chapter 4 - Assignment & Logical compares
Throughout this chapter, references are given to
various ranges of variables. Your compiler may use a
different range for some of the variables since the proposed
ANSI standard does not define specific limits for all data
types. Consult the documentation for your compiler for the
exact range for each of the variable types.
INTEGER ASSIGNMENT STATEMENTS
Load the file INTASIGN.C and display it for an example
of assignment statements. Three variables are defined for
use in the program and the rest of the program is merely a
series of illustrations of various assignments. The first
two lines of the assignment statements assign numerical
values to "a" and "b", and the next five lines illustrate
the five basic arithmetic functions and how to use them.
The fifth is the modulo operator and gives the remainder if
the two variables were divided. It can only be applied to
"int" or "char" type variables, and of course "int"
extensions such as "long", "short", etc. Following these,
there are two lines illustrating how to combine some of the
variables in some complex math expressions. All of the
above examples should require no comment except to say that
none of the equations are meant to be particularly useful
except as illustrations. The "char" type variable will be
defined in the description of the next example program.
The expressions in lines 17 and 18 are perfectly
acceptable as given, but we will see later in this chapter
that there is another way to write these for more compact
code.
This leaves us with the last two lines which may appear
to you as being very strange. The C compiler scans the
assignment statement from right to left, (which may seem a
bit odd since we do not read that way), resulting in a very
useful construct, namely the one given here. The compiler
finds the value 20, assigns it to "c", then continues to the
left finding that the latest result of a calculation should
be assigned to "b". Thinking that the latest calculation
resulted in a 20, it assigns it to "b" also, and continues
the leftward scan assigning the value 20 to "a" also. This
is a very useful construct when you are initializing a group
of variables. The last statement illustrates that it is
possible to actually do some calculations to arrive at the
value which will be assigned to all three variables. In
fact, the rightmost expression can contain variables, even
"a", "b", & "c".
The program has no output, so compiling and executing
this program will be very uninteresting. Since you have
Page 18
Chapter 4 - Assignment & Logical compares
already learned how to display some integer results using
the "printf" function, it would be to your advantage to add
some output statements to this program to see if the various
statements do what you think they should do.
This would be a good time for a preliminary definition
of a rule to be followed in C. The data definitions are
always given before any executable statements in any program
block. This is why the variables are defined first in this
program and in every C program. If you try to define a new
variable after executing some statements, your compiler will
issue an error.
ADDITIONAL DATA TYPES
Loading and editing MORTYPES.C will illustrate how some
additional data types can be used. Once again we have
defined a few integer type variables which you should be
fairly familiar with by now, but we have added two new
types, the "char", and the "float".
The "char" type of data is nearly the same as the
integer except that it can only be assigned numerical values
between -128 to 127 on most implementations of C, since it
is stored in only one byte of memory. The "char" type of
data is usually used for ASCII data, more commonly known as
text. The text you are reading was originally written on a
computer with a word processor that stored the words in the
computer one character per byte. In contrast, the integer
data type is stored in two bytes of computer memory on
nearly all microcomputers.
DATA TYPE MIXING
It would be profitable at this time to discuss the way
C handles the two types "char" and "int". Most functions in
C that are designed to operate with integer type variables
will work equally well with character type variables because
they are a form of an integer variable. Those functions,
when called on to use a "char" type variable, will actually
promote the "char" data into integer data before using it.
For this reason, it is possible to mix "char" and "int" type
variables in nearly any way you desire. The compiler will
not get confused, but you might. It is good not to rely on
this too much, but to carefully use only the proper types of
data where they should be used.
The second new data type is the "float" type of data,
commonly called floating point data. This is a data type
which usually has a very large range, a large number of
significant digits, and a large number of computer words are
Page 19
Chapter 4 - Assignment & Logical compares
required to store it. The "float" data type has a decimal
point associated with it and has an allowable range of from
3.4E-38 to 3.4E+38 when using most C compilers on
microcomputers, and is composed of about 7 significant
digits.
HOW TO USE THE NEW DATA TYPES
The first three lines of the program assign values to
all nine of the defined variables so we can manipulate some
of the data between the different types.
Since, as mentioned above, a "char" data type is in
reality an "integer" data type, no special considerations
need be taken to promote a "char" to an "int", and a "char"
type data field can be assigned to an "int" variable. When
going the other way, there is no standard, so you may simply
get garbage if the value of the integer variable is outside
the range of the "char" type variable. It will translate
correctly if the value is within the range of -128 to 127.
The third line illustrates the simplicity of
translating an integer into a "float", simply assign it the
new value and the system will do the proper conversion.
When going the other way however, there is an added
complication. Since there may be a fractional part of the
floating point number, the system must decide what to do
with it. By definitions , it will truncate it.
This program produces no output, and we haven't covered
a way to print out "char" and "float" type variables, so you
can't really get in to this program and play with the
results, but the next program will cover this for you.