fork download
  1. #include <iostream>
  2.  
  3.  
  4. int main(){
  5.  
  6. // . The compiler applies implicit conversions
  7. // when types are different in
  8. // an expression
  9. // . Conversions are always done from the smallest
  10. // to the largest type in this case int is
  11. // transformed to double before the expression
  12. // is evaluated.Unless we are doing an assignment
  13.  
  14. double price { 45.6 };
  15. int units {10};
  16.  
  17. auto total_price = units * price; // units will be implicitly converted to double
  18.  
  19. std::cout << "Total price : " << total_price << std::endl;
  20. std::cout << "sizeof total_price : " << sizeof(total_price) << std::endl;
  21.  
  22.  
  23. //Implicit conversions in assignments
  24. // The assignment operation is going to cause an implicit
  25. // narrowing conversion , y is converted to int before assignment
  26. int x (45.44);
  27. double y ;
  28. y= x; // double to int
  29. std::cout << "The value of x is : " << y << std::endl; // 45
  30. std::cout << "sizeof x : " << sizeof(y) << std::endl;// 4
  31.  
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Total price : 456
sizeof total_price : 8
The value of x is : 45
sizeof x : 8