[C++] Basics of Standard Output with cout | How to Display Text and Numbers on Screen

目次

Introduction

In learning C++, displaying results on the console (black screen) to check program behavior and variable contents is the most basic operation. This standard output function is provided by std::cout (short for character output), defined in the <iostream> header.

Output to the screen is performed by “flowing” the data you want to display (strings, numbers, etc.) into cout using the stream insertion operator (<<).

In this article, I will explain the basic output methods for various data types using cout.

Sample Code Using cout

This code outputs variables of various types, such as strings, integers, floating-point numbers, boolean values, and pointers, to the screen using cout.

C++

#include <iostream>
#include <string>

// Using "using namespace std;" allows you to omit "std::" every time
using namespace std;

int main() {
    // 1. Prepare variables of various types
    string message = "Hello, C++";
    char initial = 'A';
    int score = 100;
    double pi = 3.14159;
    bool is_active = true;
    int* p_score = &score; // Pointer pointing to the address of the score variable

    // 2. Output each value using the << operator
    cout << "--- Basic Output ---" << endl;
    cout << message << endl; // string
    cout << initial << endl; // char
    cout << "Your score: " << score << " points" << endl; // Concatenation of string literal and int
    cout << "Pi: " << pi << endl; // double
    cout << "Status (bool): " << boolalpha << is_active << endl; // bool (displayed as true)
    cout << "Pointer address: " << p_score << endl; // pointer

    return 0;
}
  • boolalpha: This is a manipulator that outputs bool values as the strings “true/false” instead of “1/0”.

Code Explanation

C++

cout << "String" << variable << endl;

This is the most basic usage of cout.

  • cout: An object representing standard output (usually the console screen).
  • <<: Stream insertion operator. It means “inserting (flowing)” the data on the right side into the stream (cout) on the left side.
  • Chaining: Since the << operator is designed to return cout itself, you can concatenate multiple data items and output them in one line, like cout << a << b << c;.

How to Insert a Newline: \n vs std::endl

There are mainly two ways to insert a newline.

  • ‘\n’ or “\n”: New line character. It simply inserts a new line.
  • std::endl: Inserts a new line and also flushes the output buffer (forcibly writes to the output destination).

Usually, there is not much difference between the two, but in situations where performance is critical, \n is slightly faster because it does not perform unnecessary flushing.

Summary

In this article, I explained the basic usage of C++ standard output std::cout.

  • Include the <iostream> header.
  • Output data to the std::cout object using the << operator.
  • Various data types (int, double, string, bool, etc.) can be output directly.
  • Insert a new line with std::endl or \n.

cout is the simplest and most essential tool for checking program execution results and variable values during debugging.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次