C and Object-oriented Programming

Chapter 6 - Working with Data

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Data Type Conversions
In the programs so far, the variables and numbers used in any particular statement were all of the same type—for example, int or float. You can write statements that perform operations involving variables of different types. These operations are called mixed-mode operations. In contrast to some other programming languages, C and C++ perform automatic conversions from one type to another. As you progress through the book, additional types will be introduced, and mixing of those types will be discussed.
Data of different types is stored differently in memory. Suppose that the number 10 is being stored. Its representation will depend upon its type. That is, the pattern of zeros and ones in memory will be different when 10 is stored as an integer than when it is stored as a floating-point number.
Suppose that the following operation is executed, where both fresult and fvalue are of type float, and the variable ivalue is of type int:
fresult = fvalue * ivalue;
The statement is therefore a mixed-mode operation. When the statement is executed, the value of ivalue will be converted into a floating-point number before the multiplication takes place. The compiler recognizes that a mixed-mode operation is occurring, and therefore generates code to perform the following operations. The integer value assigned to ivalue is read from memory. This value is then converted to the corresponding floating-point value, which is multiplied by the real value assigned to fvalue, and the resulting floating-point value is assigned to fresult. In other words, the compiler performs the conversion automatically. Note that the value assigned to ivalue is unchanged by this process and remains of type int.
You have seen that in mixed-mode operations involving a value of type int and another value of type float, the value of type int is converted into a value of type float for calculation. This is done without changing the stored integral value during the conversion process. Now let’s consider mixed-mode operations between two different types of variables.
Actually, before doing this, you need to know that there is in fact a hierarchy of conversions, in that the object of lower priority is temporarily converted to the type of higher priority for the performance of the calculation. The hierarchy of conversions takes the following structure, from highest priority to lowest:
double
float
long
int
short
For example, the type double has a higher priority than the type int. When a type is converted to one that has more significant digits, the value of the number and its accuracy are unchanged.
Look at what happens when a conversion from type float to type int takes place. Suppose that the variables ivalue1 and ivalue2 have been defined to be of type int, while fvalue and fresult have been defined to be of type float. Consider the following sequence of statements:
ivalue1 = 3;
ivalue2 = 4;
fvalue = 7.0;
fresult = fvalue + ivalue1/ivalue2;
The statement ivalue1/ivalue2 is not a mixed-mode operation; instead, it represents the division of two integers, and its result is zero since the fractional part (0.75, in this case) is discarded when integer division is performed. Therefore, the value stored in fresult is 7.0.
What if ivalue2 had been defined to be of type float? In this case, fresult would have been assigned the floating-point value 7.75, since the statement ivalue1/ivalue2 would be a mixed-mode operation. Under these circumstances, the value of ivalue1 is temporarily converted to the floating-point value 3.0, and the result of the division is 0.75. When that is added to fvalue, the result is 7.75.
It is important to know that the type of the value to the left of the assignment statement determines the type of the result of the operation. For example, suppose that fx and fy have been declared to be of type float and iresult has been declared to be of type int. Consider the following statements:
fx = 7.0;
fy = 2.0;
iresult = 4.0 + fx/fy
The result of executing the statement fx/fy is 3.5; when this is added to 4.0, the floating-point value generated is 7.5. However, this value cannot be assigned to iresult because iresult is of type int. The number 7.5 is therefore converted into an integer. When this is done, the fraction part is truncated. The resulting whole number is converted from a floating-point representation to an integer representation, and the value assigned to iresult is the integer number 7.
Explicit Type Conversions Using the Cast Operator
You have seen that the C and C++ compiler automatically changes the format of a variable in mixed-mode operations using different data types. However, there are circumstances where, although automatic conversion is not performed, type conversion would be desirable. For those occasions, you must specifically designate that a change of type is to be made. These explicit specifications also clarify to other programmers the statements involved. The C language provides several procedures that allow you to designate that type conversion must occur.
One of these procedures is called the cast operator. Whenever you want to temporarily change the format of a variable, you simply precede the variable’s identifier with the parenthesized type you want it converted to. For example, if ivalue1 and ivalue2 were defined to be of type int and fvalue and fresult have been defined to be of type float, the following three statements would perform the same operation:
fresult = fvalue + (float)ivalue1/ivalue2;
fresult = fvalue + ivalue1/(float)ivalue2;
fresult = fvalue + (float)ivalue1/(float)ivalue2;
All three statements perform a floating-point conversion and division of the variables ivalue1 and ivalue2. Because of the usual rules of mixed-mode arithmetic discussed earlier, if either variable is cast to type float, a floating-point division occurs. The third statement explicitly highlights the operation to be performed.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net