• Home
  • 2.1 Printing Output Using std::cout

2.1 Printing Output Using std::cout

View Categories

2.1 Printing Output Using std::cout

< 1 min read

Table of Contents

The standard output stream is used to display text and values on the console. In C++, the object std::cout represents the standard output stream provided by the <iostream> library.

This example demonstrates how to print text, variables, and multiple values using the stream insertion operator.

Source Code #

#include <iostream>

int main()
{
    int marks = 95;

    std::cout << "Programming in C++\n";
    std::cout << "Marks: " << marks << '\n';
    std::cout << "Status: Passed\n";

    return 0;
}

Output #

Programming in C++
Marks: 95
Status: Passed

Explanation #

  • #include <iostream> includes the standard input/output stream library.
  • std::cout is the standard output stream object used to write data to the console.
  • The insertion operator (<<) sends data to the output stream.
  • String literals enclosed in double quotes are written to the console exactly as they appear.
  • Variables can be inserted into the output stream without using format specifiers.
  • Multiple insertion operators can be used in a single statement to print text and variables together.
  • '\n' inserts a newline character after the output.
  • return 0; terminates the program successfully.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *