 # Storing Information: Variables and Constants in C

• Print
This chapter is from the book

## Numeric Variable Types

C provides several different types of numeric variables. You need different types of variables because different numeric values have varying memory storage requirements and differ in the ease with which certain mathematical operations can be performed on them. Small integers (for example, 1, 199, and –8) require less memory to store, and your computer can perform mathematical operations (addition, multiplication, and so on) with such numbers very quickly. In contrast, large integers and floating-point values (123,000,000, 3.14, or 0.000000871256, for example) require more storage space and more time for mathematical operations. By using the appropriate variable types, you ensure that your program runs as efficiently as possible.

C's numeric variables fall into the following two main categories:

• Integer variables hold values that have no fractional part (that is, whole numbers only). Integer variables come in two flavors: signed integer variables can hold positive or negative values, whereas unsigned integer variables can hold only positive values (and 0).

• Floating-point variables hold values that have a fractional part (that is, real numbers).

Within each of these categories are two or more specific variable types. These are summarized in Table 3.2, which also shows the amount of memory, in bytes, generally required to hold a single variable of each type.

#### Table 3.2 C's numeric data types

 Variable Type Keyword Bytes Required Range Character char 1 –128 to 127 Short integer short 2 –32767 to 32767 Integer int 4 –2,147,483,647 to 2,147,438,647 Long integer long 4 –2,147,483,647 to 2,147,438,647 Long long integer long long 8 –9,223,372,036,854,775,807 to 9,223,372,036,854,775,807 Unsigned character unsigned char 1 0 to 255 Unsigned short integer unsigned short 2 0 to 65535 Unsigned integer unsigned int 4 0 to 4,294,967,295 Unsigned long integer unsigned long 4 0 to 4,294,967,295 Unsigned long long integer unsigned long long 8 0 to 18,446,744,073,709,551,615 Single-precision floating-point float 4 1.2E–38 to 3.4E381 Double-precision floating-point double 8 2.2E–308 to 1.8E3082 1Approximate range; precision = 7 digits. 2Approximate range; precision = 19 digits.

NOTE

Approximate range means the highest and lowest values a given variable can hold. (Space limitations prohibit listing exact ranges for the values of these variables.) Precision is the accuracy with which a variable is stored. (For example, if you evaluate 1/3, the answer is 0.33333... with 3s going to infinity. A variable with a precision of 7 stores seven 3s.)

CAUTION

Compilers that don't support the C-99 standard may not support long long and an unsigned long long values.

Looking at Table 3.2, you might notice that the variable types int and short are identical. Why are two different types necessary? The int and short variable types are indeed identical on 32-bit Intel systems (PCs), but they might be different on other types of hardware. For example, on a VAX system, a short and an int aren't the same size. Instead, a short is 2 bytes, whereas an int is 4 bytes. Remember that C is a flexible, portable language, so it provides different keywords for the two types. If you're working on a PC, you can use int and short interchangeably.

No special keyword is needed to make an integer variable signed; integer variables are signed by default. You can, however, include the signed keyword if you wish. The keywords shown in Table 3.2 are used in variable declarations, which are discussed in the next section.

Listing 3.1 will help you determine the size of variables on your particular computer. Don't be surprised if your output doesn't match the output presented after the listing.

#### Listing 3.1 sizeof.c—A program that displays the size of variable types

```1:  /* sizeof.c—Program to tell the size of the C variable */
2:  /*      type in bytes */
3:
4:  #include <stdio.h>
5:
6:  int main(void)
7:  {
8:   printf( "\nA char   is %d bytes", sizeof( char ));
9:   printf( "\nAn int   is %d bytes", sizeof( int ));
10:   printf( "\nA short   is %d bytes", sizeof( short ));
11:   printf( "\nA long   is %d bytes", sizeof( long ));
12:   printf( "\nA long long is %d bytes\n", sizeof( long long));
13:   printf( "\nAn unsigned char is %d bytes", sizeof( unsigned char ));
14:   printf( "\nAn unsigned int  is %d bytes", sizeof( unsigned int ));
15:   printf( "\nAn unsigned short is %d bytes", sizeof( unsigned short ));
16:   printf( "\nAn unsigned long is %d bytes", sizeof( unsigned long ));
17:   printf( "\nAn unsigned long long is %d bytes\n",
18:                  sizeof( unsigned long long));
19:   printf( "\nA float   is %d bytes", sizeof( float ));
20:   printf( "\nA double  is %d bytes\n", sizeof( double ));
21:   printf( "\nA long double is %d bytes\n", sizeof( long double ));
22:
23:   return 0;
24:  }```
```A char   is 1 bytes
An int   is 4 bytes
A short   is 2 bytes
A long   is 4 bytes
A long long is 8 bytes

An unsigned char is 1 bytes
An unsigned int  is 4 bytes
An unsigned short is 2 bytes
An unsigned long is 4 bytes
An unsigned long long is 8 bytes

A float   is 4 bytes
A double  is 8 bytes
A long double is 12 bytes```

As the preceding output shows, Listing 3.1 tells you exactly how many bytes each variable type on your computer takes. If you're using a standard 32-bit PC, your numbers should match those in Table 3.2.

Don't worry about trying to understand all the individual components of the program. Although some items are new, such as sizeof, others should look familiar. Lines 1 and 2 are comments about the name of the program and a brief description. Line 4 includes the standard input/output header file to help print the information on-screen. This is a simple program, in that it contains only a single function, main() (lines 7 through 24). Lines 8 through 21 are the bulk of the program. Each of these lines prints a textual description with the size of each of the variable types, which is done using the sizeof operator. Line 23 of the program returns the value 0 to the operating system before ending the program.

Although I said the size of the data types can vary depending on your computer platform, C does make some guarantees. There are five things you can count on:

• The size of a char is one byte.

• The size of a short is less than or equal to the size of an int.

• The size of an int is less than or equal to the size of a long.

• The size of an unsigned is equal to the size of an int.

• The size of a float is less than or equal to the size of a double.

NOTE

Table 3.2 listed the common keyword used to identify the different variable types. The following table (Table 3.3) lists the full name of each of the data types.

As you can see from this table, short and long types are really just variations on the int type. Most programmers don't use the full name of the variable types, rather thy use the shorter version.

#### Table 3.3 Full names of data types

 Full name commonly used keyword char signed char short signed short int int signed int long signed long int long long signed long long int unsigned char unsigned char unsigned short unsigned short int unsigned int unsigned int unsigned long unsigned long int unsigned long long unsigned long long int

### Variable Declarations

Before you can use a variable in a C program, it must be declared. A variable declaration tells the compiler the name and type of a variable. The declaration may also initialize the variable to a specific value. If your program attempts to use a variable that hasn't been declared, the compiler generates an error message. A variable declaration has the following form:

`typename varname;`

typename specifies the variable type and must be one of the keywords listed in Table 3.2. varname is the variable name, which must follow the rules mentioned earlier. You can declare multiple variables of the same type on one line by separating the variable names with commas:

```int count, number, start;  /* three integer variables */
float percent, total;    /* two float variables */```

On Day 12, "Understanding Variable Scope," you'll learn that the location of variable declarations in the source code is important, because it affects the ways in which your program can use the variables. For now, you can place all the variable declarations together just before the start of the main() function.

### The typedef Keyword

The typedef keyword is used to create a new name for an existing data type. In effect, typedef creates a synonym. For example, the statement

`typedef int integer;`

creates integer as a synonym for int. You then can use integer to define variables of type int, as in this example:

`integer count;`

Note that typedef doesn't create a new data type; it only lets you use a different name for a predefined data type. The most common use of typedef concerns aggregate data types, as explained on Day 11, "Structures." An aggregate data type consists of a combination of data types presented today.

### Initializing Variables

When you declare a variable, you instruct the compiler to set aside storage space for the variable. However, the value stored in that space—the value of the variable—isn't defined. It might be zero, or it might be some random "garbage" value. Before using a variable, you should always initialize it to a known value. You can do this independently of the variable declaration by using an assignment statement, as in this example:

```int count;  /* Set aside storage space for count */
count = 0;  /* Store 0 in count */```

Note that this statement uses the equal sign (=), which is C's assignment operator and is discussed further on Day 4, "Statements, Expressions, and Operators." For now, you need to be aware that the equal sign in programming is not the same as the equal sign in algebra. If you write

`x = 12`

in an algebraic statement, you are stating a fact: "x equals 12." In C, however, it means something quite different. In C it means "Assign the value 12 to the variable named x."

You can also initialize a variable when it's declared. To do so, follow the variable name in the declaration statement with an equal sign and the desired initial value:

```int count = 0;
double percent = 0.01, taxrate = 28.5;```

The first statement declares a variable called count as an integer and initializes it to zero. The second statement declares two variables as doubles and initializes them. The first, percent, is initialized to 0.01. The second, taxrate, is initialized to 28.5.

Be careful not to initialize a variable with a value outside the allowed range. Here are two examples of out-of-range initializations:

```int weight = 100000;
unsigned int value = -2500;```

The C compiler may not catch such errors. Your program might compile and link, but you might get unexpected results when the program is run.

DO understand the number of bytes that variable types take for your computer.

DO use typedef to make your programs more readable.

DO initialize variables when you declare them whenever possible.

DON'T use a variable that hasn't been initialized. Results can be unpredictable.

DON'T use a float or double variable if you're only storing integers. Although they will work, using them is inefficient.

DON'T try to put numbers that are too big or too small into a variable if its type won't hold them.

DON'T put negative numbers into variables with an unsigned type.