Home >Backend Development >C++ >How to use cout in c language
cout is an object in C used to output data on the console. Its output methods include: using the insertion operator (<<) to output data. Call member functions such as put() to output a single character, write() to output a string or array, and flush() to force the output buffer to be flushed.
How to use cout in C
What is cout?
cout is an object in the C standard library used to output data on the console.
Usage method
There are two main ways to use cout to output data:
Use the insertion operator (<) to output data to the cout stream. For example:
<code class="cpp">cout << "Hello, world!" << endl; // 输出 "Hello, world!" 字符串</code>
cout object also provides several member functions for outputting data, for example:
Example
The following code demonstrates the use of the cout object:
#include
using namespace std;
int main() {
cout << "Enter your name: ";
string name;
getline(cin, name);
cout << "Hello, " << name << "!" << endl;
return 0;
}
Note: The
- cout stream buffers output internally and does not output data until the buffer is full or the flush() function is called.
- endl manipulator is used to break the line after output.
- << can also be used to output other data types, such as numbers and Boolean values.
The above is the detailed content of How to use cout in c language. For more information, please follow other related articles on the PHP Chinese website!