函数定义
注意:函数定义不能嵌套在另一个函数定义中。每个函数定义都是独立的(也就是说,在main()
函数中也不能定义其他函数)
关于return 0;
由于main()
函数的原型为:int main();
,所以最后要返回一个整数值,但这个值不返回给程序其他部分,而是返回给操作系统(也叫退出值,约定为0则意味着程序运行成功)
using编译指令的位置
一般我都习惯写在#include <iostream>
的下面一行,让下面的所有函数都可以访问std;但是我看书中,只让需要访问std的函数访问它会更好。于是编译指令可以写在具体使用到的函数里面。如下:
#include <iostream>
int stonetolb(int);
int main()
{
using namespace std; //命名空间写在函数内
int stone;
cout << "Enter the weight in stone: ";
cin >> stone;
int pounds = stonetolb(stone);
cout << stone << "stone=" << pounds << "pounds" << endl;
return 0;
}
int stonetolb(int sts)
{
return 14 * sts;
}
由于stonetolb()
函数并没有使用cout
等,所以不需要访问std,将编译指令直接写在main()
函数里面即可。
另外,也可以不写,用std::cout
来代替,但是本人写不习惯
整型
- short至少16位
- int至少和short一样长
- long至少32位,且至少与int一样长
- long long至少64位,且至少跟long一样长
确定类型的宽度
宽度:存储整数时使用的内存量
#include <iostream>
#include <climits>
int main()
{
using namespace std;
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
cout << "int is " << sizeof(int) << " bytes." << endl;
cout << "short is " << sizeof(n_short) << " bytes." << endl;
cout << "long is " << sizeof n_long << " bytes." << endl;
cout << "long long is " << sizeof n_llong << " bytes." << endl;
cout << endl;
cout << "MAximum values:" << endl;
cout << "int:" << n_int << endl;
cout << "short:" << n_short << endl;
cout << "long:" << n_long << endl;
cout << "long long:" << n_llong << endl << endl;
cout << "Minimum int value=" << INT_MIN << endl;
cout << "Bits per byte=" << CHAR_BIT << endl;
return 0;
}
使用sizeof()
来返回类型或变量的长度,参数为类型时,将类型放在括号内;参数为变量时,将变量放在括号内或空格+变量名均可。
climits头文件中定义了关于整型的限制信息,例如INT_MAX表示int的最大取值等等~,输出如下:
一种特殊的初始化方法
int emus{7};
int rheas = {12}
大括号内也可以不包含任何东西,这种情况下变量被初始化为0:
int rocs = {}
整型变量超越限制的表现
可以把整型变量(不管是有符号还是无符号)想象成一个圆形,圆形的一圈裹满了它可取的值(逆时针增大)。这样一来,如果超越了限制,其值将变成范围另一端的取值
上图以short为例,无符号时,当当前值为0,若此时再减去1,超过下限,会变成+65535;有符号时,当当前值为32767,若此时再加上1,超过上限,会变成-32768.
进制前缀
C++可以以三种方式来书写整数:十进制(无前缀即可)、八进制(0为前缀)、十六进制(0x为前缀)
默认情况下cout以十进制显示整数:
#include <iostream>
int main()
{
using namespace std;
int chest = 42;
int waist = 0x42;
int inseam = 042;
cout << "Monsieur cuts a striking figure!\n";
cout << "chest=" << chest << "(42 in decimal)\n";
cout << "waist=" << waist << "(0x42 in hex)\n";
cout << "inseam=" << inseam << "(042 in octal)\n";
return 0;
}
如图,不管以什么进制表示数据,cout默认输出都是以十进制表示;
当然,也可以使用控制符dec、hex和oct分别指示cout以十进制、十六进制和八进制显示数据:
#include <iostream>
int main()
{
using namespace std;
int chest = 42;
int waist = 42;
int inseam = 42;
int temp = 16;
cout << "Monsieur cuts a striking figure!\n";
cout << "chest=" << chest << "(decimal for 42)\n";
cout << hex;
cout << "waist=" << waist << "(hexadecimal for 42)\n";
cout << "没有改变控制符,输出的还是" << temp << "十六进制\n";
cout << oct;
cout << "inseam=" << inseam << "(octal for 42)\n";
return 0;
}
注意:在修改格式前,原来的格式一直有效。
编译器确定常量类型
- 根据后缀;
- l或L表示long常量
- u或U表示unsigned int常量
- ul(任意顺序和大小写)表示unsigned long常量
- ll或LL表示long long类型(C++11)
- ull或ULL等表示unsigned long long常量(C++11)
- 根据长度:
- 对于十进制,采取下面几种类型能存储该数的最小类型来表示:int、long、long long
- 对于八进制和十六进制, 采取下面几种类型能存储该数的最小类型来表示:int、unsigned int、long、unsigned long、long long、unsigned long long
关于char
cahr类型的变量中存储的是对应字符的ASCII码。输入时,cin
将键盘输入的字符转换为对应的ASCII码;输出时,cout将ASCII码转换为对应的字符;
可以把char类型看做比short更小的整型
成员函数cout.put()
只能通过类的特定对象来使用成员函数,且必须用句点(成员运算符)将对象名和成员函数名连接起来。
(至于为什么需要这个函数,我觉得大概了解一下就可以了。
大概就是之前的时候C++会把字符常量存储为int类型,用cout<<输出只能输出ASCII码而不能输出字符)
例如:cout<<'$'
这个会输出ASCII码而不是字符!!
wchar_t宽字符类型
字符集无法用8位表示,可以使用wchar_t类型。wcin和wcout可用于处理wcahr_t类型~
wchar_t bob = L'P';
wcout << L"tall"<<endl;
前缀L表示宽字符常量和宽字符串。