在c++中,输出是必不可少的,cout是很常用的输出。在<iostream>中,有许许多多的内置函数,像cout,cin等等。把一个东西显示到屏幕上是cout的作用,当然,不是只有cout可以输出,像printf也可以。
如果不加 using namespace std ,可以 std::cout << 1; ,这样也可以输出。
标准输出(cout)
#include <iostream>
using namespace std;
int main()
{
cout << 1 << endl;
return 0;
}
执行以上代码会输出:
1
如果输出计算也可以:
cout << 1 + 1 << endl;
cout 也可以写成 “cout << 1;”这样,后面的 << endl;是换行的意思,如果输出字符可以用\n。
输出字符(hello)
#include <iostream>
using namespace std;
int main()
{
cout << "hello" << endl;
return 0;
}
输出结果
hello
这是输出字符,想要换行后面加\n就可以了。
举例
1 输出数字
#include <iostream>
using namespace std;
int main()
{
cout << 1 << endl;
return 0;
}
2 输出字符
#include <iostream>
using namespace std;
int main()
{
cout << "lo" << endl;
return 0;
}
3 输出后换行
字符
#include <iostream>
using namespace std;
int main()
{
cout << "hello\n" << endl;
return 0;
}
数字
#include <iostream>
using namespace std;
int main()
{
cout << 1 << endl;
return 0;
}
感谢大家的观看。