Home C++ cout tutorial in C++ with code examples

cout tutorial in C++ with code examples

In C++, cout is an object of the ostream class and is used to output data to the standard output device, typically the console.

The cout object is part of the iostream library, and it outputs data in a human-readable format.

Basic Syntax of cout

cout << expression;
  • The << operator is known as the insertion operator, and it directs the data to be displayed in the console.
  • cout can be chained with multiple << operators to output several pieces of data in a single line.

1. Basic Output Using cout

A simple example that uses cout to display a string.

#include <iostream>
using namespace std;

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

Explanation:

  • cout << “Hello, World!” << endl; displays “Hello, World!” in the console.
  • endl is used to add a newline at the end of the output.

Output:

Hello, World!

2. Printing Variables

You can display the value of variables using cout by chaining multiple << operators.

#include <iostream>
using namespace std;

int main() {
    int age = 25;
    double height = 5.9;

    cout << "Age: " << age << ", Height: " << height << endl;
    return 0;
}

Explanation:

  • cout << “Age: ” << age << “, Height: ” << height << endl; prints both age and height on the same line.
  • Chaining allows displaying multiple variables and strings in a single line.

Output:

Age: 25, Height: 5.9

3. Using cout with Different Data Types

cout can output data of different types, including int, float, double, char, and string.

#include <iostream>
using namespace std;

int main() {
    int age = 30;
    float weight = 68.5f;
    double height = 1.75;
    char initial = 'A';
    string name = "Alice";

    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
    cout << "Weight: " << weight << " kg" << endl;
    cout << "Height: " << height << " m" << endl;
    cout << "Initial: " << initial << endl;

    return 0;
}

Explanation:

  • cout outputs each data type in a format suitable for display.

Output:

Name: Alice
Age: 30
Weight: 68.5 kg
Height: 1.75 m
Initial: A

4. Formatting Output with cout and endl

Using endl and newlines (\n) for formatting the output in multiple lines.

#include <iostream>
using namespace std;

int main() {
    cout << "Line 1" << endl;
    cout << "Line 2\n";
    cout << "Line 3\n";
    return 0;
}

Explanation:

  • endl and \n both create newlines, but endl also flushes the output buffer.
  • \n is faster because it doesn’t flush the buffer.

Output:

Line 1
Line 2
Line 3

5. Displaying Mathematical Expressions with cout

cout can output the result of mathematical expressions directly.

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 5;

    cout << "Sum: " << (a + b) << endl;
    cout << "Difference: " << (a - b) << endl;
    cout << "Product: " << (a * b) << endl;
    cout << "Quotient: " << (a / b) << endl;

    return 0;
}

Explanation:

  • Mathematical expressions inside cout are evaluated before outputting the result.

Output:

Sum: 15
Difference: 5
Product: 50
Quotient: 2

6. Controlling Decimal Precision with cout

The <iomanip> library provides manipulators like setprecision to control the number of decimal places.

#include <iostream>
#include <iomanip> // Required for setprecision
using namespace std;

int main() {
    double pi = 3.14159265359;

    cout << "Default precision: " << pi << endl;
    cout << fixed << setprecision(2) << "Fixed precision (2): " << pi << endl;
    cout << fixed << setprecision(5) << "Fixed precision (5): " << pi << endl;

    return 0;
}

Explanation:

  • setprecision specifies the number of digits after the decimal point.
  • fixed is used to fix the precision after the decimal.

Output:

Default precision: 3.14159
Fixed precision (2): 3.14
Fixed precision (5): 3.14159

7. Aligning Output Using setw

The setw manipulator allows setting a width for the output, which is helpful for aligning columns.

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    cout << setw(10) << "Name" << setw(10) << "Age" << endl;
    cout << setw(10) << "Alice" << setw(10) << 30 << endl;
    cout << setw(10) << "Bob" << setw(10) << 25 << endl;
    cout << setw(10) << "Carol" << setw(10) << 35 << endl;

    return 0;
}

Explanation:

  • setw(10) sets a field width of 10 for each item, aligning the output in columns.

Output:

      Name       Age
     Alice        30
       Bob        25
     Carol        35

8. Combining cout with boolalpha for Boolean Values

Using boolalpha displays boolean values as true and false instead of 1 and 0.

#include <iostream>
using namespace std;

int main() {
    bool isActive = true;
    bool isAdmin = false;

    cout << "Is Active: " << boolalpha << isActive << endl;
    cout << "Is Admin: " << boolalpha << isAdmin << endl;

    return 0;
}

Explanation:

  • boolalpha changes the display of boolean values to true and false.

Output:

Is Active: true
Is Admin: false

9. Displaying Hexadecimal and Octal Values

cout supports different number bases using manipulators like hex, oct, and dec.

#include <iostream>
using namespace std;

int main() {
    int number = 255;

    cout << "Decimal: " << dec << number << endl;
    cout << "Hexadecimal: " << hex << number << endl;
    cout << "Octal: " << oct << number << endl;

    return 0;
}

Explanation:

  • dec, hex, and oct set the base for displaying numbers as decimal, hexadecimal, and octal, respectively.

Output:

Decimal: 255
Hexadecimal: ff
Octal: 377

10. Combining cout with Conditional Statements

Using cout within a conditional statement to display output based on conditions.

#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter an integer: ";
    cin >> number;

    if (number % 2 == 0) {
        cout << number << " is even." << endl;
    } else {
        cout << number << " is odd." << endl;
    }

    return 0;
}

Explanation:

  • cout displays whether the entered number is even or odd based on a conditional check.

Sample Output:

Enter an integer: 5
5 is odd.

Summary Table

Use Case Code Example Description
Basic Output cout << “Hello, World!”; Displays a simple message
Displaying Variables cout << “Value: ” << value; Displays the value of variables
Multiple Data Types cout << age << height << name; Outputs different data types
Newline and Formatting cout << “Text” << endl; or \n Adds a newline to output
Mathematical Expressions cout << (a + b); Outputs the result of expressions
Decimal Precision cout << fixed << setprecision(2) << pi; Sets the precision for floating-point numbers
Width Alignment cout << setw(10) << “Name”; Aligns columns of text
Boolean Formatting cout << boolalpha << true; Displays boolean values as true and false
Number Base Formatting cout << hex << 255; Outputs numbers in different bases
Conditional Output if (a > b) cout << “A is greater”; Uses cout with conditions

Complete Example

This example combines several cout manipulators to display a formatted table with decimal, hexadecimal, and binary representations of numbers.

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    cout << setw(10) << "Decimal" << setw(10) << "Hex" << setw(10) << "Octal" << endl;
    for (int i = 1; i <= 10; i++) {
        cout << setw(10) << dec << i;
        cout << setw(10) << hex << i;
        cout << setw(10) << oct << i << endl;
    }

    return 0;
}

Sample Output:

   Decimal       Hex     Octal
         1         1         1
         2         2         2
         3         3         3
         4         4         4
         5         5         5
         6         6         6
         7         7         7
         8         8        10
         9         9        11
        10         a        12

Key Takeaways

  • cout provides a flexible way to display data in C++ programs, supporting a wide variety of data types and formats.
  • Using manipulators like endl, setw, setprecision, hex, and boolalpha allows you to control and format output for readability.

You may also like