Types and Variables
Fundamental to any programning languages are the means of naming values. In most all programming languages, these variables represent data of different types. To see more completely how C handles these issues, you will need to read the following sections from the textbook.
- King: Sections 7.1-7.4, 4.1-4.3, pages 125-148, 53-62
In Scheme, a variable (or identifier or parameter) can represent any type of data, and the type of a variable may change from one call of a procedure to another. For example, consider the following Scheme procedure that adds all numbers on a list or its sublists:
(define sum-numbers (lambda (lst) (cond [(number? lst) lst] [(not (list? lst)) 0] [(null? lst) 0] [else (+ (sum-numbers (car lst)) (sum-numbers (cdr lst)))))))
sum-numbers is called:
(sum-numbers '((1) 2.0 four ((3/4) (five) 6)))
lst starts as a list, but various calls
lst various values, including sublists,
integers (e.g., 1, 6), real numbers (2.0), fractions (3/4), and
four, five). At the end of this
sum-numbers returns 9.75.
In C, the data type of each variable must be declared before the variable is used in a program (or procedure). Once declared, the data type cannot change within the procedure; values can change, but not the type of data.
When you start programming in C, you need to be familiar with the few primitive types of variables provided to manage data with. Often processing proceeds according to the type of data involved. However, in some processing situations, C allows for implicit and explicit type conversions between these storage classes. In the accompanying lab, you will perform different operations and examine the results.
C has four primitive types:
char c; /* a character */ int i; /* an integer */ float f; /* a real number (single-precision)*/ double d; /* a real number that needs twice the space as a float (double-precision) */
The following notes provide some background about each of these data types,
charis a character that you might type at the keyboard, such as
In C, a
charvalue is identified with single quotes, such as 'a', 'b', 'A', '8', ';', etc.
intis an integer (a whole number with no decimal point)
doublerepresent a real number (a number with a decimal point).
floattypically maintains about 7 digits of accuracy, and requires a relatively small amount of memory
doubletypically maintains about 16 digits of accuracy, and requires twice the amount of space required by a
As we shall discuss in several weeks, these types of data are represented in different ways within a computer.
- Each data type may take up a different amount storage.
- The amount of storage may vary from one machine to another.
Each data type has a limited range of values.
Details of these limits are given in a library
limits.h. (See pages 591 - 593 in the text book.)
For example, an
intusually has values between -2,147,483,648 (
limits.h) and 2,147,483,647 (
- Details of these limits are given in a library package,
C provides common arithmetic operations for both
doubles, with an extra capability for
-represent addition and subtraction
/represent multiplication and division
%represents integer remainder.
ints is particularly efficient and fast, and the result is always an
doubles may be somewhat slower, and the result is always a
Arithmetic involving an
doubleproceeds in two steps: first the
intis converted to a
double, and second the operation is applied to the two
doubles to obtain a
Incrementing and Decrementing Numbers
Suppose you have a counter variable
days that counts
down the number of days you have left for the summer. Every day that
passes, you need to decrement the number of days to get your
countdown going. You could do it like this:
days = days - 1;
This would mean that your are taking the number of days, subtracting
1 from it, and setting that as my new
However, a shortcut to saying that the new value of
days is the
old value of
days minus one may be written as follows.
days -= 1;
You can do a variety of operations just like this:
a += bmeans
a = a+b
a /= bmeans
a = a/b
a *= bmeans
a = a*b
In practice, 1 is the most common value to add or subtract from a
variable, and C provides two special ways to increment by 1. For a
++ais called a pre-increment operation: add 1 to a before anything else happens in the expression.
a++is called a post-increment operation: add 1 to a after anything else happens in the expression.
In C, a character is stored in a coded form—an integer,
typically between 0 and 255. (Historically, the code used numbers 0
to 127, but the extended code now uses codes over the full range 0 to
255.) Thus, a
char is considered to be an integer with
a restricted size. For the most part, we do not care what the
internal code is for an integer—conceptually, we just have a
character. Usually the underlying numerical code is the American Standard
Code for Information Interchange or ASCII. However, the C
) does not require that compilers use ASCII, and therefore the ASCII code should not
be relied upon when writing programs that you may want to compile and run on different computers.