0
0
C++programming~5 mins

Using cout for output in C++

Choose your learning style9 modes available
Introduction

We use cout to show messages or results on the screen. It helps us communicate what the program is doing.

To display a greeting message to the user.
To show the result of a calculation.
To print instructions or menus in a program.
To debug by showing variable values during program execution.
Syntax
C++
#include <iostream>

int main() {
    std::cout << "Your message here" << std::endl;
    return 0;
}

std::cout is used to send output to the screen.

<< is called the insertion operator and it sends data to std::cout.

Examples
Prints a simple greeting message.
C++
#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}
Prints text and a number together.
C++
#include <iostream>

int main() {
    int age = 25;
    std::cout << "Age: " << age << std::endl;
    return 0;
}
Prints two lines by chaining << operators.
C++
#include <iostream>

int main() {
    std::cout << "Line 1" << std::endl << "Line 2" << std::endl;
    return 0;
}
Sample Program

This program prints a welcome message and then shows a number.

C++
#include <iostream>

int main() {
    std::cout << "Welcome to the program!" << std::endl;
    int number = 10;
    std::cout << "The number is: " << number << std::endl;
    return 0;
}
OutputSuccess
Important Notes

Always include #include <iostream> to use cout.

std::endl moves the cursor to the next line after printing.

You can print many things in one line by chaining <<.

Summary

cout is used to print messages on the screen.

Use << to send data to cout.

End lines with std::endl to start a new line.