| by Arround The Web | No comments

What is cout in C++ Language

C++ is an extremely useful programming language that provides a wide range of features to work with input and output. Among these features, the cout is one of the most widely used ones. Users can print or output data in the console window.

Follow this article’s guidelines if you want to learn about the cout function in C++ programming language.

What is cout in C++ Language

cout is a combination of two words one is c which is called ‘character’ and the other is out which is known as ‘output’ in C++. It is a built-in function in C++ used for displaying text, numbers, floating points, and other characters on the output screen. It uses the built-in C++ “iostream” header file to display the characters on the output screen. Without declaring the header file, you won’t be able to use the cout function in your program.

The following is the syntax to use cout in C++ programming language.

cout << "data_to_print";

In the above syntax, “cout” is the object, << sign indicates insertion operator, while “data_to_print” is the user-defined data that a user wants to output on the console window.

The following are some C++ programming language examples of how to use cout.

Example 1

A simple example of displaying the message on the screen using cout in C++ is as follows:

#include <iostream>

using namespace std;

int main ()

{

cout << "Hi, how are you ";

}

In the above example, you will see cout is used in the main function with the “iostream” header file, and it will print the message enclosed in the quotation marks.

Output

Example 2

Let’s follow up with another example of using cout function to print values of two integers in C++.

#include <iostream>

using namespace std;

int main () {

  int x, y;

cout << "Enter two digits and add them:";

cin >> x >> y;

cout << "Value of the 1st digit is " << x << endl

<< "Value of 2nd digit is: " << y << endl

<< "Sum of both digits:"<<x+y << endl;

  return 0;

}

In the above code we initialize two variables and get these values from the user and then using a single cout we showed the two digits and their sum.

Output

Example 3

You can also use a different pattern of cout in C++, which is “std::cout” which works similarly to cout.

#include <iostream>

using namespace std;

inline int sq (int len){

  return len*len;

}

int main () {

std::cout << "Square length: "

<<sq (100) << std::endl;

  return 0;

}

In the above program, we made a function where we use inline int sq (int len) to return the length using len of the sq variable. In the main function, we call member functions and show results using std::cout.

Output

Conclusion

In C++ programming the cout is used to display data to the console. Its use is simple that requires only << sign followed by the data to print. It makes use of the iostream header file, which is a must for programs that output data in C++ programs. In the above guidelines, you will find several examples that will help you implement cout in C++ programming language.

Share Button

Source: linuxhint.com

Leave a Reply