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 - Integer Constants (Page 3 of 8 )
Because you can have different kinds of integer variables, you might expect to have different kinds of integer constants, and you do. If you just write an integer value 100, for example, this will be of typeint. If you want to make sure it is typelong, you must append an upper- or lowercase letterLto the numeric value. So the integer 100 as alongvalue is written100L. Although it’s perfectly legal, a lowercase letter lis best avoided because it’s easily confused with the digit 1.
To declare and initialize the variableBig_Number, you could write this:
long Big_Number = 1287600L;
An integer value will also be typelongif it’s outside the range of typeint. Thus, if your compiler implementation uses 2 bytes to store typeintvalues, the values1000000and33000will be of typelongby default, because they won’t fit into 2 bytes.
You write negative integer constants with a minus sign, for example:
int decrease = -4; long below_sea_level = -100000L;
You can also write integer values in hexadecimal form—that is, to base 16. The digits in a hexadecimal number are the equivalent of decimal values 0 to 15, and they’re represented by 0 through 9 andAthroughF(orathroughf). Because there needs to be a way to distinguish between 9910and 9916, hexadecimal numbers are written with the prefix0x or0X. You would therefore write 9916in your program as0x99or as0X99.
Hexadecimal constants are most often used to specify bit patterns because each hexadecimal digit corresponds to 4 binary bits. The bitwise operators that you’ll see in the next chapter are usually used with hexadecimal constants that define masks. If you’re unfamiliar with hexadecimal numbers, you can find a detailed discussion of them in Appendix A.
Floating-point variablesare used to store floating-point numbers. Floating-point numbers hold values that are written with a decimal point, so you can represent fractional as well as integral values. The following are examples of floating-point values:
1.6 0.00008 7655.899
Because of the way floating-point numbers are represented, they hold only a fixed number of decimal digits; however, they can represent a very wide range of values—much wider than integer types. Floating-point numbers are often expressed as a decimal value multiplied by some power of 10. For example, each of the previous examples of floating-point numbers could be expressed as shown in Table 2-4.
Table 2-4. Expressing Floating-Point Numbers
With an Exponent 0.16✕101
Can Also Be Written in C As 0.16E1
The center column shows how the numbers in the left column could be represented with an exponent. This isn’t how you write them in C; it’s just an alternative way of representing the same value designed to link to the right column. The right column shows how the representation in the center column would be expressed in C. TheEin each of the numbers is for exponent, and you could equally well use a lowercasee. Of course, you can write each of these numbers in your program without an exponent, just as they appear in the left column, but for very large or very small numbers, the exponent form is useful. I’m sure you’d rather write0.5E-15than0.0000000000000005, wouldn’t you?