When creating a program using C++ which symbol is used for the insertion operator?

In this tutorial, we will learn about the C++ cout object with the help of examples.

The cout object is used to display the output to the standard output device. It is defined in the iostream header file.

Example

#include <iostream>

using namespace std;

int main() {
  int a = 24;
	

// print variable cout << "Value of a is " << a;

return 0; } // Output: Value of a is 24


cout Syntax

The syntax of the cout object is:

cout << var_name;

Or

cout << "Some String";

Here,

  • << is the insertion operator
  • var_name is usually a variable, but can also be an array element or elements of containers like vectors, lists, maps, etc.

cout with Insertion Operator

The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output".

The cout object is used along with the insertion operator << in order to display a stream of characters. For example,

int var1 = 25, var2 = 50;

cout << var1;
cout << "Some String";
cout << var2;

The << operator can be used more than once with a combination of variables, strings, and manipulators (like endl):

cout << var1 << "Some String" << var2 << endl;

Example 1: cout with Insertion Operator

#include <iostream>
using namespace std;

int main() {
  int a,b;
  string str = "Hello Programmers";
	

// single insertion operator cout << "Enter 2 numbers - ";

cin >> a >> b; cout << str; cout << endl;

// multiple insertion operators cout << "Value of a is " << a << endl << "Value of b is " << b;

return 0; }

Output

Enter 2 numbers - 6
17
Hello Programmers
Value of a is 6
Value of b is 17

cout with Member Functions

The cout object can also be used with other member functions such as put(), write(), etc. Some of the commonly used member functions are:

  • cout.put(char &ch): Displays the character stored by ch.
  • cout.write(char *str, int n): Displays the first n character reading from str.
  • cout.setf(option): Sets a given option. Commonly used options are left, right, scientific, fixed, etc.
  • cout.unsetf(option): Unsets a given option.
  • cout.precision(int n): Sets the decimal precision to n while displaying floating-point values. Same as cout << setprecision(n).

Example 2: cout with Member Function

#include <iostream>

using namespace std;

int main() {
  string str = "Do not interrupt me";
  char ch = 'm';
	

// use cout with write() cout.write(str,6);

cout << endl;

// use cout with put() cout.put(ch);

return 0; }

Output

Do not
m

cout Prototype

The prototype of cout as defined in the iostream header file is:

extern ostream cout;

The cout object in C++ is an object of class ostream. It is associated with the standard C output stream stdout.

The cout object is ensured to be initialized during or before the first time an object of type ios_base::Init is constructed. After the cout object is constructed, it is tied to cin which means that any input operation on cin executes cout.flush().

I'm wondering if there is a standard name for the "<<" and ">>" operators? This is mostly in the context of teaching C++ and using those operators as part of stream input/output. If I need to read code or prompt for student responses (such as cout << "Hello";), I'm not sure how to verbalize those symbols. Is there a convention when reading them out loud?

Improved version of this question: How do you read the "<<" and ">>" symbols out loud?

asked Mar 11, 2017 at 3:06

When creating a program using C++ which symbol is used for the insertion operator?

7

When not overloaded, left-shift and right-shift and some people call them that even when used with streams, but insertion and extraction is a lot more common in that context. They are also sometimes informally called put to and get from. IIRC, Stroustrup favoured that last form.

answered Mar 11, 2017 at 3:10

Jon HannaJon Hanna

108k9 gold badges143 silver badges247 bronze badges

3

According to cplusplus.com's documentation:

This operator (<<) applied to an output stream is known as insertion operator.

...

And from the same website

This operator (>>) applied to an input stream is known as extraction operator.

...

answered Mar 11, 2017 at 3:11

nbronbro

14.5k27 gold badges104 silver badges189 bronze badges

In his book "The C++ Programming Language", C++11, bjarne stroustrup has called << "put to" and >> "get from".

Hope this helps

answered Mar 11, 2017 at 3:13

When creating a program using C++ which symbol is used for the insertion operator?

Son NguyenSon Nguyen

1703 silver badges6 bronze badges

<< is the insertion operator. Note when you write

cout << "Some text";

The arrows are pointing to the stream. You're inserting the text into the stream.

>> is the extraction operator. When you write

cin >> some_var;

You're extracting a value from the stream.

answered Mar 11, 2017 at 3:12

When creating a program using C++ which symbol is used for the insertion operator?

CarcigenicateCarcigenicate

41.2k9 gold badges66 silver badges108 bronze badges

2

Those are officially bitwise shift operators (e.g., 1 << 3 is 8), but they're often overloaded as stream insertion/stream extraction operators (as in the cout example you gave).

answered Mar 11, 2017 at 3:10

When creating a program using C++ which symbol is used for the insertion operator?

3

When creating a program using C++ which symbol is used for the insertion operator?

In C++, stream insertion operator “<<” is used for output and extraction operator “>>” is used for input. We must know the following things before we start overloading these operators. 2) These operators must be overloaded as a global function.

When creating an assignment statement which symbol is used for the assignment operator?

Assignment (=) The simple assignment operator ( = ) is used to assign a value to a variable. The assignment operation evaluates to the assigned value. Chaining the assignment operator is possible in order to assign a single value to multiple variables.

Which development component combines the object file with other machine codes necessary for your C++ program to run correctly?

A compiler takes the program code (source code) and converts the source code to a machine language module (called an object file). Another specialized program, called a linker, combines this object file with other previously compiled object files (in particular run-time modules) to create an executable file.

Which stream manipulator can be used to advance the cursor to the next line on the computer screen?

endl C++ Manipulator The word 'endl' in C++, a programming language, stands for end of line. Furthermore, the use of the endl C++ manipulators takes place to move the cursor to the beginning of the next line. Moreover, its working is similar to the '\n' escape sequence.