Type casting program in c++
Data type casting in c++ program
copy
#Beginner
In gcc c++ compiler using namespace std;
#include <iostream>
using namespace std;
int main()
{
cout << "float to int " << (int)1.35 << endl;
cout << "int to float " << (float)36 << endl;
cout << "char to int " << (int)'A' << endl;
cout << endl;
int int_num1 = 35;
float float_num1 = 30.9;
char char_letter = 'B';
int int_num2 = (int)float_num1;
float float_num2 = (float)int_num1;
int int_char = (int)char_letter;
cout << "float to int " << int_num2 << endl;
cout << "int to float" << float_num2 << endl;
cout << "char to int " << int_char << endl;
}
Output
float to int 1
int to float 36
char to int 65
float to int 30
int to float35
char to int 66
[Program finished]
OR
In gcc c++ compiler using namespace std;
#include <iostream>
using namespace std;
int main()
{
cout << "float to int " << static_cast<int>(1.35)<< endl;
cout << "int to float " << static_cast<float>(36)<< endl;
cout << "char to int " << static_cast<int>('A') << endl;
cout << endl;
int int_num1 = 35;
float float_num1 = 30.9;
char char_letter = 'B';
int int_num2 = static_cast<int>(float_num1);
float float_num2 = static_cast<int>(int_num1);
int int_char = static_cast<int>(char_letter);
cout << "float to int " << int_num2 << endl;
cout << "int to float " << float_num2 << endl;
cout << "char to int " << int_char << endl;
}
Output
float to int 1
int to float 36
char to int 65
float to int 30
int to float 35
char to int 66
[Program finished]
Comments
Post a Comment
Any doubt about programming or questions