C++ cout – Basic Output Operation in C++

Filed Under: C++
C Cout Object

Hey, readers! In this article, we will be focusing on C++ cout statement to perform the output operation in C++.

So, let us begin!!


Working of C++ cout

In C++, cout is an object of ostream class. It accordingly displays the output values on the console/screen. The cout object holds its cords with the standard C output stream stdout.

When we give a call to the cout object, it invokes the action of class ostream i.e. output stream and streams the data into a pipeline of bytes.

In c++, we fetch or display the data in the form of sequence of bytes known as Stream. Through output stream, the data is pulled from the main memory using the cout object and displayed on the screen.

Let us now focus on the structure of cout object in the upcoming section.


Syntax of cout in C++

Have a look at the below syntax!

#include<iostream>
cout<<"text";
OR
cout<<variable;

For the cout object to get called, it is necessary to import the iostream header file. The iostream class contains all the necessary functions to perform and invoke the objects such as cout, cin, etc.

Further, we use insertion operator(<<) along with the cout statement to display the values on the screen.

Having understood the syntax, let us now implement the concept through the below examples.


Implementing cout through examples

In this example, we have displayed a string through cout object and insertion operator(<<).

Example:

#include <iostream>
using namespace std; 
int main()
{
    cout<<"Welcome to JournalDev!!";
    return 0;
}

Output:

Welcome to JournalDev!!

In this example, we have declared and int data type variable and assigned a value to it. Further, we have displayed the value by using the variable name along with the cout object as shown–

Example:

#include <iostream>
using namespace std; 
int main()
{
    cout<<"Welcome to JournalDev!!"<<endl;
    int x = 10;
    cout<<"The value of x: "<<x;
    return 0;
}

Output:

Welcome to JournalDev!!
The value of x: 10

Conclusion

By this we have come to the end of this topic. Feel free to comment below, in case you come across any question.

Till then, Stay tuned @ C++ programming with JournalDev and Keep Learning!!


References

C++ cout object — Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages