Sams Teach Yourself C in 21 Days

(singke) #1
Looking at Table 3.2, you might notice that the variable types intandshortare identi-
cal. Why are two different types necessary? The intandshortvariable 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 shortand an intaren’t the same size.
Instead, a shortis 2 bytes, whereas an intis 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 intandshortinterchangeably.
No special keyword is needed to make an integer variable signed; integer variables are
signed by default. You can, however, include the signedkeyword if you wish. The key-
words 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.

LISTING3.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 ));

46 Day 3

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

Note


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

06 448201x-CH03 8/13/02 11:14 AM Page 46

Free download pdf