fork download
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int a = 100;
  6.  
  7. double b = static_cast<float>(a); // ok
  8. double b1 = (float)a; // c-cast a->float == ok, implicit static_cast here
  9.  
  10. // unsigned char const * c = a; // illegal, need casting
  11. //unsigned char const * c = static_cast<unsigned char const *>(a); // illegal cast, incompartible types
  12. unsigned char const * c = reinterpret_cast<unsigned char const *>(a); // ok
  13. unsigned char const * c1 = (unsigned char const *)(a); // c-cast 100->pointer == ok, implicit reinterpret_cast here
  14.  
  15. // unsigned char * d = c; // illegal, stripping const
  16. unsigned char * d = const_cast<unsigned char *>(c); // ok
  17. unsigned char * d1 = (unsigned char *)c; // c-cast const -> non-const == ok, implicit const_cast here
  18.  
  19.  
  20. printf("%d, %f, %x, %x", a, b, c, d);
  21. return 0;
  22. }
Success #stdin #stdout 0.02s 2724KB
stdin
Standard input is empty
stdout
100, 100.000000, 64, 64