Javatpoint Logo
Javatpoint Logo

Output operator in C++

The output operator in C++ is represented by the symbol and is used to output data to the standard output stream, which is often the console. It is frequently used in conjunction with the std::cout stream object to display data on the screen. Numerous platforms, including Windows, Linux, Unix, Mac, etc., support C++. The most fundamental approach to handling output in C++ is this one.

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:

Output:

Ram is a good boy.

Explanation:

  1. '#include iostream': This line adds the essential header file 'iostream' that gives C++ input and output stream capability. Use of cout is necessary.
  2. 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.
  3. int main(): The execution of your C++ program starts here, with the function int main().
  4. 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.
    1. 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.
    2. <<: 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:

Output:

Beauty in things exists in the mind which contemplates them. - David Hume

Explanation:

  1. '#include iostream': This line adds the essential header file 'iostream' that gives C++ input and output stream capability. Use of cout is necessary.
  2. 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.
  3. int main(): The execution of your C++ program starts here, with the function int main().
  4. 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.
    1. 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:
    2. 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.
    3. "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.
    4. The third component is str_1, which contains the contents of the str_1 character array, or "David Hume".
  5. 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.

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:

Output:

Please enter your village name: Atmakur
Your village name is: Atmakur

Explanation:

  1. #include iostream>: This line contains the iostream> header, which is required for operations like cin and cout that involve input and output.
  2. using namespace std;: Using the std namespace without having to prefix the symbol with std:: is possible due to this line.
  3. int main(): The main function is defined with the statement int main(), which serves as the program's entry point.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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 namecomplete 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 instance, the program output might be as follows if the user entered "Rivertown" as the name of the village:

The program lets the user enter a village name up to 30 characters long, and it outputs the entered village name.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA