C++ Primer Plus 第二章 开始学习C++
一、进入C++(C++基本概念)
1、main()函数
main()函数的结构如图所示:
具体实例:
#include <iostream> // a PREPROCESSOR directive
int main() // function header
{ // start of function body
using namespace std; // make definitions visible
cout << "Come up and C++ me some time."; // message
cout << endl; // start a new line
cout << "You will regret it!" << endl; // more output
// If the output window closes before you can read it,
// add the following code:
// cout << "Press any key to continue." <<endl;
// cin.get();
return 0; // terminate main()
} // end of function body
- 函数头
main()函数的函数头为:
int main()
main()函数会被启动代码调用,启动代码充当了操作系统于程序之间的桥梁,然后程序中的其他函数再被main()函数调用,实现程序的运行。因此,一个独立的程序中必须有main()函数。
函数头表明,main()函数返回值为int型,main()函数不接受任何参数。
- 返回语句(即结束函数)
main()函数的返回语句为:
return 0;
程序开发一般惯例:
return 0 代表程序正常退出
return 1代表程序异常退出
return -1::表示返回一个代数值,一般用在子函数结尾表示该函数失败
2、注释
//在C++中,注释使用双斜杠表示,注释不能跨行
//如果跨行需要重新使用双斜杠
/*斜杠加星号的注释也是可以的,可以跨行,但这样写可能造成注释起始和注释结束的配对问题*/
3、C++预处理器和iostream文件
C++程序在进行主编译前,会采用预处理器对以#开头的编译指令进行预编译。
未完待续