输入输出
C++ 使用 iostream 头文件进行标准输入输出。
#include <iostream>
using namespace std;
int main(){
cout << "hello world" << endl;
return 0;
}
与 C 语言相比,C++ 的打印方式有所不同。首先包含头文件 <iostream>,不再需要 .h 后缀。std 是 C++ 标准库的命名空间。cout 是标准输出对象,对应控制台输出;cin 是标准输入对象,对应键盘输入。endl 表示换行并刷新缓冲区。
相对于 C 语言的 scanf 和 printf,cout 和 cin 可以自动识别变量类型。
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
int x = 0;
// 并没有指定按什么格式打印
cout << x << endl;
// 指定以整型的方式打印
printf("%d\n",x);
return 0;
}
<< 是流插入运算符,>> 是流提取运算符。cout << ... 可理解为数据流入了 cout 对象中。
缺省参数
缺省参数是在声明和定义函数时为参数指定的默认值。调用函数时,如果没有指定实参就采用形参的缺省值,否则使用实参。
#include <iostream>
using namespace std;
{
cout << a << endl;
}
{
();
();
;
}








