Home > Backend Development > C++ > How to use cout in c++

How to use cout in c++

下次还敢
Release: 2024-04-28 19:27:18
Original
869 people have browsed it

The cout function in C is used to output data to the console or other output streams. The usage method is: cout << "output content" << endl;, where "output content" can be characters String, number, variable or expression, endl means line break. cout also supports custom formatted output, and you can use format specifiers to control the format of the output data, such as %d for integers, %f for floating point numbers, and %s for strings. In addition, cout can redirect output to a file or other output stream, and can also set the output precision.

How to use cout in c++

Usage of cout in C

cout is used in C programming language to output data to the console or Standard library functions for other output streams. It belongs to the iostream header file and needs to be included before use.

Usage:

The basic syntax of cout is as follows:

<code class="cpp">cout << "输出内容" << endl;</code>
Copy after login
  • "Output content" is the data to be output, which can be characters String, number, variable or expression.
  • endl is the newline character, which moves the cursor to the next line.

Example:

<code class="cpp">#include <iostream>

using namespace std;

int main() {
  cout << "你好,世界!" << endl;
  cout << "我的年龄是:" << 25 << endl;
  return 0;
}</code>
Copy after login

Output:

<code>你好,世界!
我的年龄是:25</code>
Copy after login

Custom formatted output:

cout Format specifiers can be used to control the format of the output data. The most commonly used specifiers are:

##% dInteger##%f%s
SpecifierDescription
Floating point number
String
Example:

<code class="cpp">#include <iostream>

using namespace std;

int main() {
  int age = 25;
  float height = 1.75;
  cout << "我的年龄是 %d,身高是 %f 米。" << endl;
  return 0;
}</p>Output:<p><strong><pre class="brush:php;toolbar:false"><code>我的年龄是 25,身高是 1.75 米。</code>
Copy after login

Other usage :

  • Redirect output:

    cout can be redirected to a file or other output stream, for example:

    <code class="cpp">ofstream myFile("output.txt");
    cout.rdbuf(myFile.rdbuf());</code>
    Copy after login

  • Set output precision:

    cout You can use the setprecision() method to set the output precision of floating point numbers. For example:

    <code class="cpp">cout << fixed << setprecision(2) << 3.14159;</code>
    Copy after login
    This will output: 3.14

Note:

cout always flushes its buffer , so the output will be displayed immediately.
  • If you want to output multiple items to the same line, you can use the << operator to connect them.

The above is the detailed content of How to use cout in c++. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template