[cpp]
//p23
//將字符賦給整型變量
#include <iostream>
using namespace std;
int main()
{
int i,j;
i='A';
j='B';
cout<<i<<" "<<j<<endl;
return 0;
}
//p23
//將字符賦給整型變量
#include <iostream>
using namespace std;
int main()
{
int i,j;
i='A';
j='B';
cout<<i<<" "<<j<<endl;
return 0;
}運行結果:
[cpp]
//p24
//將小寫字母轉化為大寫字母
#include <iostream>
using namespace std;
int main()
{
char c1,c2;
c1='a';
c2='b';
c1=c1-32;
c2=c2-32;
cout<<c1<<" "<<c2<<endl;
return 0;
}
//p24
//將小寫字母轉化為大寫字母
#include <iostream>
using namespace std;
int main()
{
char c1,c2;
c1='a';
c2='b';
c1=c1-32;
c2=c2-32;
cout<<c1<<" "<<c2<<endl;
return 0;
}
運行結果:
[cpp]
//p25
//符號常量
#include <iostream>
using namespace std;
#define PRICE 30
int main()
{
int num,total;
num=10;
total=num*PRICE;
cout<<"total="<<total<<endl;
return 0;
}
//p25
//符號常量
#include <iostream>
using namespace std;
#define PRICE 30
int main()
{
int num,total;
num=10;
total=num*PRICE;
cout<<"total="<<total<<endl;
return 0;
}
運行結果:
[cpp]
//p34
//強制類型轉換
#include <iostream>
using namespace std;
int main()
{
float x;
int i;
x=3.6;
i=(int)x;
cout<<"x="<<x<<",i="<<i<<endl;
return 0;
}
//p34
//強制類型轉換
#include <iostream>
using namespace std;
int main()
{
float x;
int i;
x=3.6;
i=(int)x;
cout<<"x="<<x<<",i="<<i<<endl;
return 0;
}
運行結果:
[cpp]
//p35
//有符號數據傳送給無符號變量
#include <iostream>
using namespace std;
int main()
{
unsigned short a;
short int b=-1;
a=b;
cout<<"a="<<a<<endl;
return 0;
}
//p35
//有符號數據傳送給無符號變量
#include <iostream>
using namespace std;
int main()
{
unsigned short a;
short int b=-1;
a=b;
cout<<"a="<<a<<endl;
return 0;
}運行結果: