1.c++内置类型:基本类型和复合类型;
2.c++命名规则:
2.1.在名称中只能使用字母字符、数字和下划线(_);
2.2.名称的第一个字符不能是数字。
2.3.区分大写和小写字符。
2.4.不能将关键字作为名称。
3.头文件climits定义了符号常量来表示类型的限制。例如:INT_MAX表示类型int能够存储的最大值。
4.要创建无符号版本的基本整型,只需要使用关键字unsigned来修改声明。
例如:unsigned short change;
5.仅当有大型整型数组时,才有必要使用short。如果short比int小,则使用short可以节省内存。
6.在默认情况下,cout以十进制格式显示整数。不论这些整数在程序中是如何书写的。
进制间的转换关系
#include <iostream>
using namespace std;
int main()
{
using namespace std;
int chest = 42;
int waist = 42;
int inseam = 42;
cout << "chest=" << chest << "(decimal for 42)" << endl;
cout << hex;//hex就是一条信息,告诉cout采取何种行为。
cout << "waist=" << waist << "(hexadecimal for 42)" << endl;
cout << oct;
cout << "inseam=" << inseam << "(octal for 42)" << endl;
cout << oct;
return 0;
}
7.除非有理由存储为其他类型,(使用了特殊的后缀来表示特定的类型,或者值太大,不能存储为int)否则c++将整型常量存储为int类型。
8.cout.put()显示变量和一个字符常量。
9.signed char 和unsigned char
前者表示范围通常为0-255,后者表示范围为-128-127。
10.wcha_t
表示扩展字符集。是一种整数类型。
处理工具:wcin 和 wcout。可通过加上前缀L来指示宽字符常量和宽字符串。
#include <iostream>
using namespace std;
int main()
{
using namespace std;
wchar_t bob = L'p';
wcout << L"tall" << endl;
return 0;
}
11、bool 类型
布尔变量的值可以是true或者false。c++将非零值解释为true,将零解释为false。
12、const 关键字来修改变量声明和初始化。
conest type name = value;
conest int Months = 12;
优点:1.可以明确指定类型。2.可以使用c++的作用域规则将定义限制在特定的函数或文件中。3.可以将const用于更复杂的类型。
13、浮点类型:float ,double ,long double.
#include <iostream>
using namespace std;
int main()
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield);
//这种调用迫使输出使用定点表示法。以便更好的了解精度,防止程序把较大的值切换为为E表示法。并使程序显示到小数点后六位
float tub = 10.0 / 3.0;
double mint = 10.0 / 3.0;
const float millon = 1.0e6;
cout << "tub= " <<tub<< endl;
cout << "mint= " << mint << endl;
cout << "millon= " << millon << endl;
cout << "tub*millon= " << millon*tub << endl;
return 0;
}
14、类型转换
1.将一种算数类型的值赋给另一种算数类型的变量时,c++将对值进行转换。
#include <iostream>
using namespace std;
int main()
{
using namespace std;
cout.setf(ios_base::fixed, ios_base::floatfield);
float tree = 3;
int guess(3.9832);
int debt = 7.2E12;
cout << "tree=" << tree << endl;
cout << "guess=" << guess << endl;
cout << "debt=" << debt << endl;
return 0;
}
2.表达式中包含不同的类型时,c++将对值进行转换。
3.将参数传递给函数时,c++将对值进行转换。
4、表达式中的转换
4.1、如果short 比int 短,则unsigned short 类型将被转换为int ,如果两种类型长度相同,则unsigned short 类型将被转换为unsigned int,确保了对unsigned short 进行提升时不会损失数据。
4.2、当运算设计到两种类型时,较小的类型将被转换为较大的类型。
5、强制类型转换
15、auto 说明
在初始化声明中, 如果使用关键字auto,而不指定变量的类型,编译器将把变量的类型设置成于初始值相同。
例如:
auto n=100; // n将被设置为int 类型
练习题:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
using namespace std;
const int du = 60;
const int feng = 60;
int degrees=37, minutes=51, seconds=19;
double a;
cout << "Enter a latitude in degrees,minutes, and seconds" << endl;
cout << "First ,enter the degrees: " << degrees << endl;
cout << "Next, enter the minutes of arc: " << minutes << endl;
cout << "Finally,enter the se4conds of arc: " << seconds << endl;
cout << degrees << "degrees. " << minutes << "minutes, " << seconds << "seconds = ";
a = (seconds *1.0/ feng + minutes) *1.0/ du+degrees;//加上1.0之后就完成了直接整数除以整数得到的是小数。
cout <<fixed<<setprecision(4)<< a << "degrees" << endl;
return 0;
}