Output in C++

The standard C++ library iostream provides three built-in ways to print into the console.

  1. cout
  2. clog
  3. cerr

Cout

It is used to print any kind of message or data on the console. Unlike C language here we don’t need to provide the type of data we want to print.

Cout Example

#include <iostream>

using namespace std;

int main() {

    cout << "Hello World";

    return 0;
}

Output

Hello World

Clog

Clog is much similar to cout but it is mainly used for logging.

Clog Example

#include <iostream>

using namespace std;

int main() {

    clog << "I, am an error!";

    return 0;
}

Output

I, am an error!

Cerr

Cerr also produces output like cout, but the main purpose of cerr is to print errors on the console.

Cerr Example

#include <iostream>

using namespace std;

int main() {

    cerr << "I, am an error!";

    return 0;
}

Output

I, am an error!