Chapter 1 Introduction¶
The first C++ program
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World! I am " << 18 << " Today!" << endl;
return 0;
}
iostream:输入输出核心头文件using namespace std:使用标准命名空间,可省略std::前缀cout:标准输出流,<<为输出运算符endl:换行并刷新输出缓冲区(单纯换行可使用\n,效率更高)
Read Input
#include <iostream>
using namespace std;
int main(){
int number;
cout << "Enter a decimal number: ";
cin >> number;
cout << "The number you entered is " << number << "." << endl;
return 0;
}
cin:标准输入流,按空格/回车分割输入数据,>>为输入运算符
编译运行(g++)
# 编译:g++ 源文件.cpp -o 可执行文件
g++ a.cpp -o a
# 运行(Windows)
./a.exe