Chapter 3 Section 3.10 Type of Expressions and Automatic Conversion CS 50 Hathairat Rattanasook
Types of Expressions and Automatic Conversions In C, every expression has an associated type. Operators and operands within the expression combine to determine the expression s type. Some type conversions are automatically performed in calculating the final value of expressions involving the arithmetic operators. CS 50 - Hathairat Rattanasook 1
Expressions with a relational operator An expression provides a value with a type int either 1 (true) or 0 (false). An expression contains relational operators which are!, &&,, ==,!=, <, <=, > and >=. CS 50 - Hathairat Rattanasook 2
A list of the dominating types from most to least dominating long doble double float unsigned long long unsigned int CS 50 - Hathairat Rattanasook 3
Exception For any expression involving only char, signed or unsigned char, short, or unsigned short, the dominating expression will be of type int or unsigned. For expressions consisting of types long int and unsigned int, the dominating type depends on the implementation. It may be either unsigned long int or long int. CS 50 - Hathairat Rattanasook 4
Example Int short char short + double will have type double + int will have type int + int will have type int + char will have type int A different compiler may represent a different size for each type. CS 50 - Hathairat Rattanasook 5
The actual rules for automatic conversions on the types in an arithmetic expression 1. The type char, short, unsigned char, and unsigned short are converted to type int if all the values of the original type can be represented in type int. If some values cannot be represented in type int, the conversion is to type unsiged int. CS 50 - Hathairat Rattanasook 6
The rules for automatic conversion cont. 2. After the initial conversions of types char, short, unsigned char, and unsigned short, expressions involving the unary operators -, --, and ++ will have a type matching the type of the operand. Example: -int, --int, and int++ are all type int -float, --float, and float++ are all type float -char, --char, and char++ are all type char CS 50 - Hathairat Rattanasook 7
The rules for automatic conversion cont. 3. If both the operands are of the same type, the expression will have that type. Example: int + int, int int, int * int and int / int are all type int float + float, float float, float * float and float/float are all type float CS 50 - Hathairat Rattanasook 8
Attention! int / int is type int What is the value of the results in the following program? #include <stdio.h> int main() { printf("result of 10/3 is %d\n", 10 / 3); printf("result of 5/2 is %d", 5 / 2); return 0; } CS 50 - Hathairat Rattanasook 9
Answer CS 50 - Hathairat Rattanasook 10
The rules for automatic conversion cont. 4. If and expression contains an operand of type long double, the other operand is converted to long double, before calculating the expression s value. The conversion is finished. Example: long double + int int will be converted to long double before calculating long double + char char will be converted to long double before calculating CS 50 - Hathairat Rattanasook 11
The rules for automatic conversion cont. 5. Otherwise, if there exits an operand of type double, the other operand is converted to type double before calculation. Example: double + int int will be converted to double double + short short will be converted to double double + char char will be converted to double double + long double double will be converted to long double ( previous rule) CS 50 - Hathairat Rattanasook 12
The rules for automatic conversion cont. 6. Similarly, if one operand is of type float, the other operand is converted to type float and the conversion terminates. 7. Of these, the type unsigned long dominates in that one operand of type unsigned long causes the other operand to be converted to that type. CS 50 - Hathairat Rattanasook 13
The rules for automatic conversion cont. 8. Operands of types long int and unsigned int in the same expression are converted to type long int if the range of values that can be represented by type unsigned int is a subset of the range that can be represented by type long. Otherwise, both values are converted to type unsigned long int. CS 50 - Hathairat Rattanasook 14
The rules for automatic conversion cont. 9. Otherwise, if one operand has type long, the other is converted to type long. 10. If one operand has type unsigned, the other operand is converted to type unsigned. 11.Otherwise, both operands are of type int. CS 50 - Hathairat Rattanasook 15
Chapter 3 Section 3.11 Forced Type Conversions CS 50 Hathairat Rattanasook
Forced Type Conversions Sometimes the default conversions do not give the desired resultant type. A type cast can be used in these situations. Any type in a C program can be cast to any other type. However, data might be lost. CS 50 - Hathairat Rattanasook 17
The type cast The cast consists of a pair of parentheses enclosing a type specify. It is a unary operator in C. Example: int number = 5; // The number variable is integer with value 5. (float)number; // The number is now type float (unsigned)number;// The number is now type unsigned (short)number; // The number is now type short CS 50 - Hathairat Rattanasook 18
Question? What is the output of the following program? #include <stdio.h> int main() { int ten = 10; int three = 3; } printf("1. 10 / 3 is %f\n", (double)(ten / three)); printf("2. 10 / 3 is %f", ((double)ten) / ((double)three)); return 0; CS 50 - Hathairat Rattanasook 19
Answer CS 50 - Hathairat Rattanasook 20