Type conversions for C/C++
Type conversions
Implicit type conversion
// Ex.1 print Implicit type conversion
#include <iostream>
using namespace std;
int main(){
int x = 1;
char y = 'A';
cout<<"x = "<< x <<", y = " << y<< endl;
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x+y ;
cout<<" x = "<<x<< endl;
// x is implicitly converted to float
float z = x+1.1;
cout <<" z = "<<z<< endl;
// z is implicitly converted to int
int a = z +1;
cout <<" a = "<<a<< endl;
// a is int of datatype
a = x/a;
cout <<" a = "<<a<< endl;
// a is implicitly converted to float
float b =a;
cout <<" b = "<<b<< endl;
// z is implicitly converted to char
char c = z;
cout <<" c = "<<c<< endl;
return 0;
}Output

Explicit type conversion
แหล่งอ้างอิง:
Last updated
Was this helpful?

