Sunday, September 1, 2013

Type conversion

C++ makes many type conversions automatically
  1.  C++ converts values when you assign a value of one arithmetic type to a variable of another arithmetic type.  
  2. C++ converts values when you combine mixed types in expressions
  3. C++ converts values when you combine mixed types in expression

Conversion on Assignment

 Numeric value of one type to a variable of another type, value is converted to the type of the receiving variable
  • short s=30;
  • long t = s; //s is converted to long and assigned to t
  • The program takes the value of thirty (typically a 16-bit value) and expands it to a long value (typically a 32-bit value) upon making this assignment.The expansion creates a new value to place into long; the contents of thirty are unaltered.

 Widening

  • Assigning a value to a type with a greater range usually poses no problem
  • Ex:  assigning a short value to a long variable doesn't change the value; it just gives the value a few more bytes 

Narrowing

Assigning a large long  to a float variable results in the loss of some precision. 

Bool

Nonzero(+ve or ve) value  assigned to boolean variable is converted to true

Conversions that take place when you arithmetically combine different types

When an operation involves two types, the smaller is converted to the larger

 


 


 

No comments:

Post a Comment