If you're a beginning programmer and want to get more deeply into programming with variables, you've come to the right place. This article, the second of three parts, is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).
First Steps in (C) Programming, continued - Variables and Memory (Page 2 of 8 )
So far you’ve only looked at integer variables without considering how much space they take up in memory. Each time you declare a variable, the computer allocates a space in memory big enough to store that particular type of variable. Every variable of a particular type will always occupy the same amount of memory—the same number of bytes—but different types of variables require different amounts of memory to be allocated.
NOTE The amount of memory occupied by variables of a given type will always be the same on a particular machine. However, in some instances a variable of a given type on one computer may occupy more memory than it does on another.
You saw at the beginning of this chapter how your computer’s memory is organized into bytes. Each variable will occupy some number of bytes in memory, so how many bytes are needed to store an integer? Well, 1 byte can store an integer value from –128 to +127. This would be enough for the integer values that you’ve seen so far, but what if you wanted to store a count of the average number of stitches in a pair of knee-length socks? One byte wouldn’t be anywhere near enough. Consequently, not only do you have variables of different types in C that store different types of numbers, one of which happens to be integers, but you also have several varieties of integer variables to provide for different ranges of integers to be stored.
As I describe each type of variable in the following sections, I’ll include a table containing the range of values that can be stored and the memory the variable will occupy. I’ll summarize all these in a complete table of all the variable types at the end of this chapter.
Integer Variable Types
You have three basic flavors of integer variables that you can declare. Each type is specified by a different keyword, as shown in Table 2-3.
Table 2-3. Keywords for Integer Variable Types
Number of Bytes
Range of Values
2 or 4 (depending on your computer)
–32,768 to +32,767 or –2,147,438,648 to +2,147,438,647
–32,768 to +32,767
–2,147,438,648 to +2,147,438,647
The typesshortandlongare really abbreviations for typesshort intandlong int, but they’re almost always written in their abbreviated forms. Table 2-3 reflects the typical size of each type of integer variable, although the amount of memory occupied by variables of these types depends on the particular compiler you’re using. The only specific requirement is that typelongwon’t occupy less memory than typeint, and typeintwon’t occupy less memory than typeshort. Outside of that, the compiler-writer has complete freedom to make the best use of the hardware arithmetic capabilities of the machine on which the compiler is executing.
Variables of typeintshould have the size most suited to the computer on which the code is executing. For example, consider the following statement:
int cookies = 0;
This statement declares a variable that will occupy 2 bytes on some machines and 4 bytes on others. You may also find that two different compilers on the same machine implement typeintwith different sizes. This variation may seem a little strange, but theinttype is intended to correspond to the size of integer that the computer has been designed to deal with most efficiently, and this can vary not only between different types of machine, but also with the same machine architecture as the chip technology evolves over time. Ultimately, it’s the compiler that determines what you get. Although at one time many C compilers for the PC createdintvariables as 2 bytes, with more recent C compilers on a PC, variables of typeintoccupy 4 bytes. This is because all modern processors move data around at least 4 bytes at a time. If your compiler is of an older vintage, it may still use 2 bytes for typeint, even though 4 bytes would now be better on the hardware you’re using.
NOTE The sizes of all these types are compiler dependent. The ANSI standard for the C language requires only that the size ofshortvariables should be less than or equal to the size of typeint, which in turn should be less than or equal to the size of typelong.
If you use typeshort, you’ll probably get 2-byte variables. The previous declaration could have been written as follows:
short cookies = 0;
Because the keywordshortis actually an abbreviation forshort int, you could write this as follows:
short int cookies = 0;
This is exactly the same as the previous statement. When you write justshortin a variable declaration, theintis implied. Most people prefer to use this form—it’s perfectly clear and it saves a bit of typing.
NOTE Even though typeshortand typeintmay occupy the same amount of memory on some machines, they’re still different types.
If you need integers with a bigger range—to store the average number of hamburgers sold in one day, for instance—you can use the keyword long:
Typelongdefines an integer variable with a length of 4 bytes, which provides for a range of values from –2,147,438,648 to +2,147,438,647. As noted earlier, you can writelong intif you wish instead oflong, because it amounts to the same thing.