C++ Cout Format

In C++, the cout object is an object of the iostream class. It is specified in the iostream header file. It is linked to the typical output device which is generally a console. For the output to be exhibited on a screen, the cout is utilized with the stream insertion operator (<<).

The syntax of cout is as follows:

cout << variable;
cout << “string”;

Now, using this syntax, we perform the various illustrations in this article.

Using Cout to Display the Character String

In this illustration, we demonstrate the easiest and simplest way to utilize the cout object. We first include the header files and then utilize the cout object inside the main() function to display a string of characters on the console.

#include <iostream>
using namespace std;
int main()
    cout << "This is a cout format guide.";
    return 0;

The program begins with the inclusion of the required library which is “iostream”. Then, we use the “namespace std” library. This library allows us to use all the functions, variables, and classes that are available under the standard library namespace. The main() function starts and we use the cout object with the insertion operator within its braces. Then, we specify the text that we need to display between the double quotation marks as “This is a cout format guide.” The return “0” means that the program ran successfully.

The following snapshot shows that the provided text is displayed on the console:

Using Cout to Print the Integer Value that Is Stored in a Variable

Another way to use the cout in C++ to display the output is to initialize a variable first. Then, provide it to the cout object to print it on the console. In this example, we initialize an integer type variable with “a” value. Then, it is displayed on the console using the count object.

#include <iostream>
using namespace std;
int main()
  int s = 1000;
  cout << "The value of s is " << s;
  return 0;

First, we include the "iostream” header file. Then, the std namespace library is used to make the execution of the error-free code. The main() function is initialized and we initialize a variable “s” inside the main() function with int datatype and assign it with the value of 1000.

Now, to display the value of this variable, we use the cout object with the insertion operator and then specify the variable name. We can also display a text string with the cout object by writing it in between the double quotation marks after the insertion operator. The cout simply prints whatever is defined between the double quotation marks as it is on the console.

Here, we define it as "The value of s is" followed by the insertion operator. Then, we define the name of the variable whose value has to be displayed. Lastly, put a semicolon(;) to determine the end of a statement.

The output image shows the provided text string with the value which is stored in variable “s”:

Using Cout to Display the Sum of Two Numbers

Apart from displaying the values on the console, the cout object can also be used to perform arithmetical operations. We can simply specify the operation in the count statement and the calculated output is displayed on the console.

#include <iostream>
using namespace std;
int main() {
    int p;
    int r;
    cout << "Enter first number:";
    cin >> p;
    cout << "Enter second number:";
    cin >> r;
    cout << "The first number is: " <<p<<endl;
    cout << "The second number is:" <<r<<endl;
    cout << "The sum of p and r is:"<<p+r<<endl;  
    return 0;

After including the required libraries, we enter the main() function of the program. Here, we declare two variables – “p” and “r” – both having the integer datatype. Now, we take the input values for both these variables from the user, one by one. A cout object is used to display the text “Enter first number:” on the screen. Then on the next line, we use the cin object which takes the input from the user and stores the user-provided number in the “p” variable.

After that, we display another text “Enter second number:” with the use of cout. Cin takes the input value for the “r” variable.

As you can see in the snapshot, the user enters the first number and hits “Enter”. The control asks the user to input a second value. Here, the first number that is entered by the user is 20 and the second number is 7.

We then exhibit both the number on the console. To do that, we simply add a text and the variable name to the cout object with the use of the insertion operator. At the end of each cout statement, we use the “endl” function which immediately adds a new line after the following code.

Now, to count the sum of these two numbers, we write the athematic operator “+” between both the variables “p” and “r” along with the “The sum of p and r is:” statement. As the program is executed, the cout object calculates the sum of values that are stored in these variables and displays it on the console with the specified text.

The following image shows both the entered values and the sum of these two values:

Using Cout to Display the Character and Integer Variables in a Single Line

We can display multiple variables with a single cout statement. Here is the program to implement this technique:

#include <iostream>
using namespace std;
int main()
    string Name = "Harry";
    string Country = "America";
    int Age = 28;
    cout << "My Name is " << Name <<" , my age is "<< Age << " and I live in " << Country << endl;
    return 0;

In this program, we initialize two strings and one int value. The first string is “Name” which is initialized as “Harry”. The second string is “Country” which is given with the value of “America”. Lastly, we have the int variable “Age” with the value of “28”. Now, we use the cout object with the insertion operator to display all these values in a single line.

First, we write the cout object followed by the insertion operator. Then, with the help of double quotation marks, we defined the “My name is” text. Then, the variable that stores the name is added after the insertion operator, “Name”. By following the same pattern, we add the other two variables with the specified text in this cout statement. Finally, the execution of the program gives us a single-line output.

The output statement includes all three variables with the text and is displayed on the console:


The C++ cout format is defined and explained in this article. We provided you with the syntax to use this object. Various illustrations are demonstrated practically with a thorough exaplanation of their details. We first showed you the easiest and simplest format to employ a cout statement. Also, we carried out an example in calculating the sum of two variables in cout. Each example is elaborated on the different formats that are used for the cout object in C++.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.