Dremendo Tag Line

cout Statement and its Use in C++ Program

Input and Output in C++

In this lesson, we will understand what cout statement is and how it works in C++ programming with the help of some examples.

What is cout Statement

The cout statement is used in the C++ program to print text as well as the value of the variables on the screen.

Let's see some examples for more understanding.

Example 1

C++ program to print Hello World text on the screen.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    cout<<"Hello World";
    return 0;
}

Output

Hello World

Here you can see we have printed the text Hello World on the screen using cout<< statement. Here << is called the Insertion Operator which is used with cout statement to send output on the screen.

Example 2

C++ program to print Sunday, Monday and Tuesday on separate lines on the screen.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    cout<<"Sunday\n";
    cout<<"Monday"<<endl<<endl;
    cout<<"Tuesday";
    return 0;
}

Output

Sunday
Monday

Tuesday

You can see that we have used \n (escape sequence character for new line) to break the line so that Monday can be printed on the next line. In place of \n, we can use <<endl to break the line, as we have done in the above program on line number 9 we have used <<endl two times at the end of the line to break the line twice after printing Monday. You can use more than one \n or <<endl statement if you want to break more lines.

Example 3

C++ program to print integer data type values on the screen.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int a=5, b=26;
    cout<<"a="<<a<<" b="<<b<<"\n\n";
    cout<<"a="<<a<<"\n"<<"b="<<b;
    return 0;
}

Output

a=5 b=26

a=5
b=26

In the above program, we have printed two integer data type values in two different ways. First, in the same line with a space between them and second, on separate lines using \n. We have used << on line number 9 to print the outputs in a single line and after that break the line twice using \n.

Example 4

C++ program to print float data type values on the screen.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    float a=5.23, b=26.14;
    cout<<"a="<<a<<" b="<<b;
    return 0;
}

Output

a=5.23 b=26.14

In the above program, we have printed two float data type values with a space between them in the same line on the screen.

Example 5

C++ program to print double data type values on the screen.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    double a=18.413, b=57.273;
    cout<<"a="<<a<<" b="<<b;
    return 0;
}

Output

a=18.413 b=57.273

Just like the previous example this time, we have printed two double data type values with a space between them in the same line on the screen.

Example 6

C++ program to print a char data type value on the screen.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    char a='#';
    cout<<a;
    return 0;
}

Output

#

This time we have printed a char data type value of the variable a on the screen.

Escape Sequence Character in C++

An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.

In C++, all escape sequences consist of two or more characters, the first of which is the backslash, \ (called the "Escape character"); the remaining characters determine the interpretation of the escape sequence.

Escape Sequence Character Description
\n Insert a linefeed (new line)
\r Insert a carriage return
\f Insert a form feed
\b Insert a backspace
\t Insert a tab
\\ Insert a backslash
\" Insert a double quote
\' Insert a single quote
video-poster

Test Your Knowledge

Attempt the multiple choice quiz and practical questions to check if the lesson is adequately clear to you.

Test Your Knowledge