1.变量名
c++变量命名规则:
·在名称中只能使用字母字符、数字和下划线_
·名称的第一个字符不能是数字
·区分大写字符和小写字符
·不能将c++关键字用作名称
·c++对于名称长度没有限制,但有些平台有长度限制
·两个下划线或下划线和大写字母打头的名称被保留给实现使用(编译器及其使用的资源),一个下划线打头的名称被保留给实现,用作全局标识符。(__、_A、_名称不能使用)
2.整型
根据系统不同,整型标准如下所示:
·short至少16位
·int至少与short一样长
·long至少32位,且至少与int一样长
·long long至少64位,且至少与long一样长
下面用程序检测系统整型字节及最大取值:
//limits.cpp -- some integer limits整型字节及取值查询
#include<iostream>
#include<climits>
int main()
{
using namespace std;
int n_int = INT_MAX; //symbols defined in climits file标记函数在climits文件里
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
//sizeof operater yields size of type or of variable
//各类整型的字节输出
cout<<"int is "<<sizeof(n_int)<<" bytes.\n";
cout<<"short is "<<sizeof(n_short)<<" bytes.\n";
cout<<"long is "<<sizeof(n_long)<<" bytes.\n";
cout<<"long long is "<<sizeof(n_llong)<<" bytes.\n";
cout<<"\n";
cout<<"Maximum values:\n";
cout<<"int:"<<n_int<<"\n";
cout<<"short:"<<n_short<<"\n";
cout<<"long:"<<n_long<<"\n";
cout<<"long long:"<<n_llong<<"\n";
cout<<"Minimum int value:"<<INT_MIN<<"\n";
cout<<"Bits per byte = "<<CHAR_BIT<<endl;
system("pause");
return 0;
}
//64位Windows7系统
//int为32位
//short为16位
//long为32位
//llong为64位
无符号类型可增大变量能够存储的最大值
unsigned quarterback;
unsigned short change;
unsigned long gone;
unsigned long long lang_lang;
变量超过整型的取值范围,其值将为另一端的取值
short:-32678–32676
32676+1=-32678
3.整型字面值
十进制:第一位为1-9,例93.
八进制:第一位为0,第二位为1-7,例042.
十六进制:前两位为0x,a-f代表10-15
默认情况下,cout输出均为十进制格式,存储在计算机中,都被存储为二进制
下面为用cout改变各进制形式程序:
//hexoct -- display values in hex and octal 将值以十六进制和八进制形式显示
#include<iostream>
using namespace std;
int main()
{
int chest = 42; //胸围
int waist = 42; //腰围
int inseam = 42;//臀围
cout<<"Monsieur cuts a striking figure!\n"; //他有一身吸引人的身材
cout<<"chest = "<<chest<<" (decimal for 42)\n";
cout<<hex; //manipulator for changing number base 操作改变数字形式
cout<<"waist = "<<waist<<" (hexadecimal for 42)\n";
cout<<oct; //manipulator for changing number base 操作改变数字形式
cout<<"inseam = "<<inseam<<" (octal for 42)\n";
system("pause");
return 0;
}
4.char类型
cout输出char为字母,书写字符字面值,用单引号,例‘M’
下面用程序做演示:
//the char type and int type contrasted 转换char和int形式
#include<iostream>
using namespace std;
int main()
{
char ch = 'M'; //assign ASCII code for M to ch 将M的ASCII编码分配给ch
int i = ch;
cout<<"The ASCII code for "<<ch<<" is "<<i<<endl;
cout<<"Add one to the character code:\n";
ch = ch+1;
i=ch;
cout<<"The ASCII code for "<<ch<<" is "<<i<<endl;
//用cout.put()展示字符
cout<<"Displaying char ch using cout.put(ch):";
cout.put(ch);
cout.put('!');
cout<<endl<<"Done!\n";
system("pause");
return 0;
}
5.const限定符
const int Months = 12; //Months is symbolic constant for 12
设置Months常量为12,类型为int
如果在声明常量时没有提供值,则该常量的值将是不确定的,且无法修改
6.浮点数
浮点数的表示方法
1.常用的标准小数点表示法:
12.34、939001.32、8.0
2.E表示法:
2.52e+6 = 2.52e6 = 2.52*1000000 = 2520000
8.33e-4 = 8.33/10000 = 0.000833
e大小写均可,正负都可表示
3种浮点类型float、double、long double
例如山脉有14000英尺,有效位数为2位,占位符为3位
通常有效位数float至少32位,double至少48位,且不少于float,long double至少和double一样多,这三种类型有效位数可以一样多,一般float32位,double64位,long double为80/96/128位
7.C++算术运算符
初始化进行的转换程序:
//type changes on initialization 初始化格式转换
#include<iostream>
int main()
{
using namespace std;
cout.setf(ios_base::fixed,ios_base::floatfield);
float tree = 3; //int converted to float 转换为float
int quess(3.9832);
int debt = 7.2E12;
cout<<"tree = "<<tree<<endl;
cout<<"guess = "<<quess<<endl;
cout<<"debt = "<<debt<<endl;
system("pause");
return 0;
}
系统输出:
tree = 3.000000
guess = 3
debt = 2147483647
将浮点性转换为整型时,C++采取截取整数部分
int变量debt无法存储7.2E12,导致C++没有对结果进行定义的情况发生
总结
整型从最小到最大依次是:bool,char,signed char,unsigned char,short,unsigned short,int,unsigned int,long,unsigned long,long long,unsigned long long
类型char16_t和char32_t可以分别存储16和32位的字符编码。wchar_t可以存储系统扩展字符集中的任意成员
short至少为16位,而int至少与short一样长,long至少为32位,且至少为32位,且至少和int一样长
浮点类型分别为float,double,long double,确保float不比double长,double不比long double长。通常float使用32位内存,double使用64位,long double使用80-128位