Lecture 10
Data type Conversions
Int and float
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main (int argc, char *argv[])
{
int a=4,c;
float b=3.5,d;
d=a;
c=(int)b;
cout
<<" a value is :
"<< a << endl;
cout <<" b value is : "<< b << endl;
cout
<<" d value float type conversions : "<< d << endl;
cout
<<" c value int type conversions : "<< c << endl;
getch();
return 0;
}
Code view in dev compiler:
Compile the program Ctrl+F9
Run program Ctrl+F10
Char and int
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main (int argc, char *argv[])
{
int a=4,c;
char b='S',d;
d=a;
c=(int)b;
cout
<<" a value is :
"<< a << endl;
cout
<<" b variable character is
: "<< b << endl;
cout
<<" d value char type conversions : "<< d << endl;
cout
<<" Character conversions int type, c value is : "<< c
<< endl;
getch();
return 0;
}
Code view in dev compiler:
Compile the program Ctrl+F9
Run program Ctrl+F10
Char and long
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main (int argc, char *argv[])
{
long a=4,c;
char b='S',d;
d=a;
c=(long)b;
cout
<<" a value is :
"<< a << endl;
cout
<<" b variable character is
: "<< b << endl;
cout
<<" d value char type conversions : "<< d << endl;
cout
<<" Character conversions long type, c value is : "<< c
<< endl;
getch();
return 0;
}
Code view in dev compiler:
Compile the program Ctrl+F9
Run program Ctrl+F10
long and double
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main (int argc, char *argv[])
{
long a=4,c;
double b=3.5,d;
d=a;
c=(long)b;
cout
<<" a value is :
"<< a << endl;
cout
<<" b value is :
"<< b << endl;
cout
<<" d value double type conversions : "<< d <<
endl;
cout
<<" c value long type conversions : "<< c << endl;
getch();
return 0;
}
Code view in dev compiler:
Compile the program Ctrl+F9
Run program Ctrl+F10