Type Casting and Conversion

Type casting allows you to convert one data type to another. C++ supports both implicit and explicit casting.

  • Implicit: Automatically done by the compiler.
  • Explicit: Manually done using cast syntax.
int a = 10;
double b = a; // Implicit

double x = 5.6;
int y = (int)x; // Explicit
cout << y; // Outputs: 5

Use explicit casting to avoid unexpected behavior during data conversion.

← PrevNext →