Printing debug output

How to:

Here’s a snippet showing you how to print a simple debug message to the console.

#include <iostream>

int main() {
    int lifeTheUniverseAndEverything = 42;

    // Debug message
    std::cout << "Debug: The value of lifeTheUniverseAndEverything is " 
              << lifeTheUniverseAndEverything << std::endl;

    // Rest of the code goes here...

    return 0;

Sample Output:

Debug: The value of lifeTheUniverseAndEverything is 42

Deep Dive

Long ago, debug outputs were etched onto physical media. Not fun. Now, we just use std::cout and similar tools. std::cerr is there for errors, often used alongside std::cout. Why two different streams? It’s like having different chats for work and friends; it helps keep things organized. Fancy IDEs provide integrated debuggers, but sometimes a simple print statement does the trick without the fuss. Be warned, unnecessary prints slow things down; imagine someone narrating every step they take. Tidy up when you’re done.

See Also