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; }程式的執行結果: