cout Statement and its Use in C++ Program
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 in C++ is used for output operations. It is part of the standard input/output stream library, which allows programs to send output to the standard output device, typically the console. The cout is defined in the <iostream> header file.
In simple terms, a console is a screen or interface where you can see messages and type commands to interact with a computer.
Let's see the use of cout statement in C++ programs.
C++ program to print the text Welcome to Dremendo on the screen.
#include <iostream>
using namespace std;
int main()
{
cout<<"Welcome to Dremendo";
return 0;
}
Welcome to Dremendo
In the above program, the statement using namespace std; is a way to tell the C++ compiler that we want to use everything from the standard library without needing to write std:: before each name.
For example, instead of writing std::cout to print something, we can just write cout. It makes our code shorter and easier to read.
The << is called the Insertion Operator, which is used with the cout statement to insert or write data into an output stream, such as the console.
C++ program to print Sunday, Monday and Tuesday on separate lines on the screen.
#include <iostream>
using namespace std;
int main()
{
cout<<"Sunday\n"; // insert new line using \n
cout<<"Monday"<<endl; // insert new line using <<endl
cout<<"Tuesday";
return 0;
}
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. Instead of \n, we can use <<endl to break the line. We used <<endl after the text Monday to break the line after printing Monday. We can use multiple \n or <<endl statement if we want to break more lines.
C++ program to print integer data type values on the screen.
#include <iostream>
using namespace std;
int main()
{
int a=5, b=26;
cout<<"Value of a = "<<a<<" Value of b = "<<b<<"\n\n";
cout<<"Value of a = "<<a<<endl;
cout<<"Value of b = "<<b<<endl;
return 0;
}
Value of a = 5 Value of b = 26 Value of a = 5 Value of b = 26
In the above program, we have printed two integer values in two different ways. First, in the same line with multiple spaces between them and second on separate lines.
C++ program to print float and double data type values on the screen.
#include <iostream>
#include <iomanip> // For setting precision
using namespace std;
int main()
{
float a=5.237;
double b=26.14253165;
cout<<"Value of a = "<<a<<" Value of b = "<<b<<"\n\n";
cout<<fixed<<setprecision(2); // Display the number with 2 digits after the decimal point
cout<<"Value of a = "<<a<<endl;
cout<<fixed<<setprecision(8); // Display the number with 8 digits after the decimal point
cout<<"Value of b = "<<b<<endl;
return 0;
}
Value of a = 5.237 Value of b = 26.1425 Value of a = 5.24 Value of b = 26.14253165
Just like the previous example this time, we have printed a float and a double type values on the screen.
Please note that we have included the <iomanip> header file in the above program to set the precision of decimal numbers. We used cout<<fixed<<setprecision(2); and cout<<fixed<<setprecision(8); to display the float and double values with 2 and 8 digits after the decimal point, respectively.
When we use fixed and setprecision with cout, we are telling the program to print floating-point numbers in fixed-point notation (not scientific notation) and display the number of digits after the decimal point as specified by setprecision.
If only fixed is used without setprecision, then the default precision is 6 digits after the decimal point.
If only setprecision is used without fixed, then it sets the total number of significant digits which includes both the digits before and after the decimal point.
In C++, the default precision for float and double is 6 significant digits, which includes both the digits before and after the decimal point. If the number has more digits or is very large or very small, it may be displayed in scientific (exponential) notation.
To print a specific number of digits after the decimal point and avoid scientific notation, use cout<<fixed<<setprecision(n);, where n is the number of digits you want to display after the decimal point.
C++ program to print a char data type value on the screen.
#include <iostream>
using namespace std;
int main()
{
char a='#';
cout<<a;
return 0;
}
#
This time we have printed a char data type value of the variable a on the screen.
Escape Sequence Character in C++
Escape sequence characters are those that does not represent itself when used inside a character constant 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 characters start with a backslash (\) followed by one or more characters that determine the interpretation of the escape sequence.
Common Escape Sequences
Here are some of the most common escape sequences in C++:
| Escape Sequence | Meaning | Description |
| \n | Newline | Move the cursor to the next line. |
| \t | Horizontal tab | Insert a horizontal tab space. |
| \\ | Backslash | Insert a single backslash. |
| \" | Double quote | Insert a double quote. |
| \' | Single quote | Insert a single quote. |
| \b | Backspace | Delete or overwrite the previous character by moving the cursor back one position. |
| \r | Carriage Return | Moves the cursor to the beginning of the line. It may overwrite the text that was there before. |
#include <iostream>
using namespace std;
int main()
{
// Using various escape sequences
cout<<"Hello World\n"; // Newline
cout<<"Hello\tWorld\n"; // Horizontal tab
cout<<"\\\n"; // Backslash
cout<<"\"Hello\"\n"; // Double quote
cout<<"\'Hello\'\n"; // Single quote
cout<<"Hell\bo World\b\n"; // Backspace
cout<<"Carriage return \rHello\n"; // Carriage return
return 0;
}
Hello World Hello World \ "Hello" 'Hello' Helo World Helloage return
On Windows terminal, the \b escape sequence moves the cursor back one space but does not delete the character. Any characters printed after the backspace overwrite previously written characters.
Test Your Knowledge
Attempt the questions in the exercise to check whether you have understood the lesson clearly.