Input and output are performed through cin and cout in C. Input uses cin >> to read data from standard input according to the specified data type. Output uses cout << to write data to standard output according to the specified data type.
Input and output statements in C
Get straight to the point:
Used in C cin
and cout
keywords for input and output.
Detailed answer:
Input (cin
function):
cin >>: Read data from standard input (usually the keyboard). - Data type: Specify the data type of the variable to read data from, such as
int
, float
or string
. Example:
int age;
cin >> age; // 从键盘读取年龄并将其存储在 age 变量中
Copy after loginOutput (cout
function):
cout <<
: Write data to standard output (usually a terminal or console). - Insertion operator (
<<
): Insert data into the output stream. - Data type: Specify the type of output data.
Example:
cout << "你的年龄是:" << age << "\n"; // 在终端上输出 "你的年龄是:" 和 age 的值,后面换行
Copy after loginNotes:
cin
and cout
are objects, so they can be redirected to files or network streams. cin
and cout
can be used with format specifiers to control the output format. - Input and output operations can be chained together to create complex operations.
Example (link input and output):
int x, y;
cout << "输入两个数字:" << flush; // 提示用户输入两个数字并刷新输出缓冲区
cin >> x >> y; // 读取两个数字
cout << "它们的和是:" << x + y << "\n"; // 输出数字的和
The above is the detailed content of How to write input and output statements in c++. For more information, please follow other related articles on the PHP Chinese website!