By Peter G. AiTKEN, BRADLEY Jones. Sample Chapter is Provided Courtesy of Sams. Date: Mar 28, 2003.
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.
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.2 E-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 ............................. ..
Caution
COMPORS That Don't Support The C-99 Standard May Not Support long long and an unsigned long long value.
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. ISTEE, 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 sign. The key. The key. The key...................
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 type
1: / * sizeof.c-program to Tell the size of the c variable * /
2: / * Type in bytes * /
3:
4: #include
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 I% 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);
twenty two:
23: RETURN 0;
twenty four: }
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.
Do not 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 made Some Guarantees. There Arefi Things you can count on:
THE SIZE OF A Short Is Less Than or Equal to the size of a long 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 or equal to the size of a double.
Note
Table 3.2 listed the commitness ipiyword variable type. The following table (Table 3.3) Lists The full name of each of the data type.
As you can see from this Table, SHORT AND Long Types Are Really Just Variations on the int type. MOST Program 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 intVariable 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 Decland, 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.
Typedef Keyword
The Typedef Keyword IS Used to create a new name for an existing data type. In effect, typef creates a synonym. For example, the statement
Typedef int inteer;
Creates INTEGER AS A SYNONYM for int. You kiliables of type Int, AS in this Example:
Integer count;
Note that typedef does not create a new data type;. ". Structures" 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, 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 deflaration by useing an assocignment 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 deflaration 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;
.............
Do Understand The Number of Bytes That Variable Types Take for Your Computer.
Do Use Typedef to make your program more readable.
Do INITIALIZE VARIABLES WHEN You Declare Them WHENEVER POSSIBLE.
Don't Use a variable what 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 the iS ineffect.
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.