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.

Introduction

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)))))))

When sum-numbers is called:

(sum-numbers '((1) 2.0 four ((3/4) (five) 6)))

the parameter lst starts as a list, but various calls will give lst various values, including sublists, integers (e.g., 1, 6), real numbers (2.0), fractions (3/4), and symbols (e.g., four, five). At the end of this example, 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.

Primitive Types

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,

As we shall discuss in several weeks, these types of data are represented in different ways within a computer.

Computations with ints and doubles

C provides common arithmetic operations for both ints and doubles, with an extra capability for ints.

Basic rules:

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 days value.

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:

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 variable a,

Characters

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 standard (cf. Section 5.2 ) 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.