Learn how to use C++ cout for standard output with clear examples. Understand cout syntax, printing text, variables, and formatted output in beginner-friendly C++ programs.
In C++, standard output is used to display information on the computer screen.
The standard output object in C++ is:
cout
cout is provided by the <iostream> library.
We commonly use cout to display:
coutThe basic syntax is:
cout << "Your message here";
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Hello World!
#include <iostream>#include <iostream>
Includes the input/output stream library.
It provides objects such as cout and cin.
coutcout
cout is the standard output object used to display information on the screen.
<< Insertion Operatorcout << "Hello World!";
The << symbol is called the insertion operator[1].
It sends the data on its right side to cout, which displays it on the screen.
Think of it as:
Data → cout → Screen
We can use cout to display text.
cout << "Hello World!";
Hello World!
Text must be written inside double quotation marks (" ").
cout can also display numbers.
cout << 100;
100
We do not use quotation marks when displaying a number.
Compare:
cout << 100;
Output:
100
Whereas:
cout << "100";
also displays:
100
However, the first is a number, while the second is text.
cout can be used to display the value stored in a variable.
int a = 10;
cout << a;
10
We do not put the variable name inside quotation marks.
int a = 10;
cout << "Value of a is: " << a;
Value of a is: 10
cout << "Value of a is: " << a;
There are two things being displayed:
"Value of a is: " → texta → value stored in the variableThe insertion operator << connects them.
We can use multiple << operators in one cout statement.
int age = 20;
float marks = 85.5;
cout << "Age: " << age << endl;
cout << "Marks: " << marks;
Age: 20
Marks: 85.5
We can use endl to move the cursor to the next line.
cout << "Line 1" << endl;
cout << "Line 2";
Line 1
Line 2
We can also use the newline escape sequence \n:
cout << "Line 1\n";
cout << "Line 2";
Both endl and \n can be used to move to the next line.
cout with Input and Calculationcout is often used together with cin.
cin → takes input from the user.cout → displays output to the user.Think of the process as:
User Input → cin → Variables → Calculation → cout → Screen
Write a C++ program that takes two floating-point numbers from the user, adds them, and displays the sum.
#include <iostream>
using namespace std;
int main() {
float num1, num2, sum;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
sum = num1 + num2;
cout << "The sum is: " << sum << endl;
return 0;
}
Enter first number: 12.5
Enter second number: 7.5
The sum is: 20
float variables: num1, num2, and sum.sum.cout to display the result.cout << "The sum is: " << sum << endl;
Here:
"The sum is: " → displays text.sum → displays the calculated value.endl → moves the cursor to the next line.Write a C++ program that takes the height and width of a rectangle from the user, calculates its area using:
Area = Height × Width
and displays the result.
#include <iostream>
using namespace std;
int main() {
float height, width, area;
cout << "Enter height of the rectangle: ";
cin >> height;
cout << "Enter width of the rectangle: ";
cin >> width;
area = height * width;
cout << "The area of the rectangle is: " << area << endl;
return 0;
}
Enter height of the rectangle: 5.5
Enter width of the rectangle: 4
The area of the rectangle is: 22
height, width, and area as float variables.area = height * width;
cout.std::coutWe can also use cout without writing:
using namespace std;
In that case, we write:
std::cout << "Hello World!";
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
Here, std::cout means that cout belongs to the standard (std) namespace.
For beginners, we commonly use:
using namespace std;
so that we can simply write:
cout
instead of:
std::cout
| Component | Purpose |
|---|---|
#include <iostream> |
Provides input/output features |
cout |
Displays output on the screen |
<< |
Insertion operator; sends data to cout |
endl |
Moves the cursor to the next line |
\n |
Moves the cursor to the next line |
cin |
Takes input from the user |
std::cout |
cout from the standard namespace |
coutUsed to display output.
cout << "Hello";
<<Called the insertion operator.
cout << value;
Write text inside double quotation marks:
cout << "Hello World!";
Do not use quotation marks around a variable:
int age = 20;
cout << age;
Use multiple << operators:
cout << "Age: " << age;
Use either:
cout << "Hello" << endl;
or:
cout << "Hello\n";
Write a C++ program that takes the student’s name, age, and marks as input and displays them using cout.
Write a C++ program that takes the prices of three items from the user, calculates the total, and displays the result.
Write a C++ program that takes the length and width of a rectangle and calculates:
Perimeter = 2 × (Length + Width)
Display the result using cout.
Write a C++ program that takes marks of three subjects, calculates the average, and displays the result.
cin → Input → User → Program
cout → Output → Program → Screen
cin takes data from the user, while cout displays data on t