Output Operator In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Output Operator In C++

Output Operator In C++

BLUF: Mastering Output Operator In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Output Operator In C++

C++ is renowned for its efficiency. Learn how Output Operator In C++ enables low-level control and high-performance computing in the tutorial below.

The ```

Please enter your village name: Atmakur

Your village name is: Atmakur

Example


The monitor, or output , is printed on the computer's copy machine frequently. A member of the iostream class , the predefined object cout is an instance. The cout function is used in conjunction with the insertion operator, which is represented by the symbols "<<" (two "less than" signs) for operations on formatted output.

In C++, the output operator () is a potent tool for formatting and displaying data to files and the console. It is an essential component of the input/output (I/O) system in C++ and can be utilized with different data types.

### 1. The C++ program used to demonstrate how to use the cout object is shown below:

__PRESERVE_0__

Output:

__PRESERVE_1__

Explanation:

- '#include iostream': This line adds the essential header file 'iostream' that gives C++ input and output stream capability. Use of cout is necessary.

- using namespace std;: Using symbols from the std (standard) namespace without prefixing them with std:: is possible with the help of this line, which is a using directive. Bypassing the need to use std:: in this code, you can use cout .

- int main(): The execution of your C++ program starts here, with the function int main().

- cout<< "Ram is a good boy.";: Ram is a good boy , says the cout. In this case, the normal output stream is represented by the command cout . It is used to transmit information to the standard output, which is usually the console, in this case, the string " Ram is a good boy. cout: The I/O stream library (iostream>) is a component of the C++ Standard Library. The output is sent to the console or terminal, which serves as the standard output device. <<: In C++, the output operator is represented by the symbol. It is utilized to put data into the cout stream. In this instance, it is used to insert the string "Ram is a good boy. " into the cout stream.

- cout: The I/O stream library (iostream>) is a component of the C++ Standard Library. The output is sent to the console or terminal, which serves as the standard output device.

- <<: In C++, the output operator is represented by the symbol. It is utilized to put data into the cout stream. In this instance, it is used to insert the string "Ram is a good boy. " into the cout stream.

"Ram is a good boy." will be outputted on the console after this line of code has been run. After that, it returns 0 to indicate that the program has successfully run.

The code is a straightforward C++ program that uses cout to print the words "Ram is a good boy." to the console. When using cout, you can use it without the std:: prefix by using the namespace std; directive .

### 2. The C++ program used to demonstrate a manipulator for the cout object is shown below:

__PRESERVE_2__

Output:

__PRESERVE_3__

Explanation:

- '#include iostream': This line adds the essential header file 'iostream' that gives C++ input and output stream capability. Use of cout is necessary.

- using namespace std;: Using symbols from the std (standard) namespace without prefixing them with std:: is possible with the help of this line, which is a using directive. Bypassing the need to use std:: in this code, you can use coutdirectly.cout.

- int main(): The execution of your C++ program starts here, with the function int main() .

- char str_1[] = "David Hume";: This line creates a character array named str_1 and initializes it with the word "David Hume". The array str_1 will contain a string with a null termination since the char data type is utilized to represent individual characters in this scenario. cout "Beauty in things exists in the mind which contemplates them. - " str_1;: This line outputs a concatenated string by using the cout stream. Here's how it operates: cout: The cout object is a component of the I/O stream library (iostream>) of the C++ Standard Library. It is used to transmit output to the console or terminal, which is often the standard output device. "Beauty in things exists in the mind which contemplates them", reads the first portion of the sentence, which is a string literal. - ", a second string literal, makes up the second component. The third component is str_1 , which contains the contents of the str_1 character array, or "David Hume" .

- return 0;: The main function and the program come to a conclusion with the line return 0;. The operating system receives an integer value of 0 as a result. When the main function returns 0 in C++, the program has normally run successfully. Different values might be utilized to represent errors or other criteria.

- cout "Beauty in things exists in the mind which contemplates them. - " str_1;: This line outputs a concatenated string by using the cout stream. Here's how it operates:

- cout: The cout object is a component of the I/O stream library (iostream>) of the C++ Standard Library. It is used to transmit output to the console or terminal, which is often the standard output device.

- "Beauty in things exists in the mind which contemplates them", reads the first portion of the sentence, which is a string literal. - ", a second string literal, makes up the second component.

- The third component is str_1 , which contains the contents of the str_1 character array, or "David Hume" .

3. In this example, the user will be prompted for the name of his or her city, and after the user enters their city, the city name will be stored in the name variable. After that, the output string will be printed by the console. The program for the same is shown below:

include <iostream>

using namespace std;

int main

{

//The village_name variable has a 30-character maximum.

char village_name[30];

//Print the result after requesting the village name from the user.

cout<< "Please enter your village name: ";

//Obtain user input, then store it in the village_name variable.

cin>>village_name;

//print the output string with the user's data.

cout<< "Your village name is: "

<<village_name<<endl;

return 0;

}

Example


Output:

Please enter your village name: Atmakur

Your village name is: Atmakur

Example


Explanation:

- #include iostream>: This line contains the iostream> header , which is required for operations like cin and cout that involve input and output.

- using namespace std;: Using the std namespace without having to prefix the symbol with std:: is possible due to this line.

- int main(): The main function is defined with the statement int main(), which serves as the program's entry point.

- char village_name[30];: This line declares a character array with the name village_name that can hold up to 30 characters . It is used to store the village name that the user enters. The character array's memory is allocated at this line.

- cout<< "Please enter your village name: ";: The user is prompted to enter their village name in this line's prompt message, which is shown using the cout command . The cursor stays on the same line for user input because the message doesn't contain a newline character and is followed by a space.

- cin>>village_name;: The user's input is read using the cin command , which reads from the standard input stream. Village_name character array is used to store the input. Whenever it comes across whitespace (such as a space or the Enter key ), the >> operator , which is used for input, will cease reading the letters.

- cout "Your village name is:" village_name; endl;: In this line, the user-inputted village name is shown together with a message using the cout programming language. Concatenating the strings involves using the operator. The program output is placed on a new line after the village name is displayed due to the use of the endl character to introduce a newline.

- return 0;: The line return 0 ; denotes that the main function and the program are finished. It provides the operating system with an integer value of 0, which usually denotes a successful execution.

- It will operate as follows when you execute this program: The user is prompted to provide their village name , which is displayed on the screen. The user will enter a village name complete with no spaces. The village_name character array will be used by the program to store the input. An additional message on a new line confirms the village name that was supplied.

- The user is prompted to provide their village name , which is displayed on the screen.

- The user will enter a village name complete with no spaces.

- The village_name character array will be used by the program to store the input.

- An additional message on a new line confirms the village name that was supplied.

For example, the output of the program could resemble the following if the user inputted "Rivertown" as the village name:

Please enter your village name: Rivertown

Your village name is: Rivertown

Example


The software allows the user to input a village name with a maximum length of 30 characters, and then displays the village name that was entered.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience