tailieunhanh - A Complete Guide to Programming in C++ part 17
A Complete Guide to Programming in C++ part 17. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | chapter Converting Arithmetic Types This chapter introduces implicit type conversions which are performed in C whenever different arithmetic types occur in expressions. Additionally an operator for explicit type conversion is introduced. 139 140 CHAPTER 8 CONVERTING ARITHMETIC TYPES IMPLICIT TYPE CONVERSIONS Integer promotions bool char signed char unsigned char ------ int short Type hierarchy long double double float unsigned long long unsigned int not-existent if int equals long int Example short size 512 double res x res size 10 x short - int - double int IMPLICIT TYPE CONVERSIONS 141 C allows you to mix arithmetic types in a single expression in other words the operands of an operator can belong to different types. The compiler automatically performs implicit type conversion where a common type which allows the operation in question to be performed is assigned for the values of both operands. You can generally assume that the smaller type will be converted to the larger type. The assignment operator is an exception to this rule and will be discussed separately. The result of an arithmetic operation belongs to the common type used to perform the calculation. However comparison expressions will be bool types no matter what type of operands are involved. Integer Promotion Integer promotion is first performed for any expression bool char signed char unsigned char and short are converted to int unsigned short is also converted to int if the int type is greater than short and to unsigned int in all other cases. This type conversion is performed so as to preserve the original values. The boolean value false is converted to 0 and true is converted to 1. Thus C will always use int type values or greater when performing calculations. Given a char variable c the values of c and a in the expression Example c a will be converted to int before being compared. Usual Arithmetic Type Conversions If operands of different arithmetic types still occur after integer promotion .
đang nạp các trang xem trước