cout<<待输出项1<<待输出项2<<...;
“待输出项”可以是各种基本类型的变量、常量和表达式。
#include <iostream>
using namespace std;
int main(){
int n = 5;
double f = 3.9;
char c = 'a';
cout << "n=" << n << ",f=" << f << endl; //endl表示换行
cout << 123 << ", c=" << c << endl;
return 0;
}
程序的输出结果是:using namespace std;,则 cout 就会没有定义,除非写明std::cout,指明其来自名字空间 std。endl表示换行。cin>>变量1>>变量2>>...;
以下程序演示了 cin 的用法:
#include <iostream>
using namespace std;
int main(){
int nl, n2;
char s[20];
double f;
char c;
cin >> s >> nl >> n2 >> c >> f ;
cout << s <<"," << nl << "," << n2 << "," << c << "," << f <<endl;
return 0;
}
程序的运行结果:
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有