Sams Teach Yourself C in 21 Days

(singke) #1
Variables and Constants,” Table 3.1 presented the values typically stored in IBM-compat-
ible PCs. However, these values aren’t guaranteed.
The following rules apply to variable types:


  • A character (char) is the smallest data type. A character variable (type char) will
    be one byte.

  • A short variable (type short) will be smaller than or equal to an integer variable
    (typeint).

  • An integer variable (type int) will be smaller than or equal to the size of a long
    variable (type long).

  • An unsigned integer variable (type unsigned) is equal to the size of a signed inte-
    ger variable (type int).

  • A float variable (type float) will be less than or equal to the size of a double vari-
    able (type double).
    Listing D.2 presents a commonly used way to print the size of the variables based on the
    machine on which the program is compiled.


LISTINGD.2 Printing the size of the data types
1: /*==========================================================*
2: * Program: listD02.c *
3: * Book: Teach Yourself C in 21 Days *
4: * Purpose: This program prints the sizes of the variable *
5: * types of the machine the program is compiled on *
6: *==========================================================*/
7: #include <stdio.h>
8: int main(void)
9: {
10: printf( “\nVariable Type Sizes” );
11: printf( “\n=========================” );
12: printf( “\nchar %d”, sizeof(char) );
13: printf( “\nshort %d”, sizeof(short) );
14: printf( “\nint %d”, sizeof(int) );
15: printf( “\nfloat %d”, sizeof(float) );
16: printf( “\ndouble %d”, sizeof(double) );
17:
18: printf( “\n\nunsigned char %d”, sizeof(unsigned char) );
19: printf( “\nunsigned short %d”, sizeof(unsigned short) );
20: printf( “\nunsigned int %d\n”, sizeof(unsigned int) );
21:
22: return 0;
23: }

802 Appendix D

INPUT

47 448201x-APP D 8/13/02 11:17 AM Page 802

Free download pdf