Questions tagged [cout]

std::cout is the global stream object provided by the C++ standard library for writing to the standard output stream.

For more general questions about std::ostream use the or tags.

Bjarne Stroustrup, the creator of C++, explains that cout is pronounced "see-out" and the "c" stands for "character". (wcout is used for wide character output to the standard output stream)

1820 questions
31
votes
3 answers

Why is '\n' preferred over "\n" for output streams?

In this answer we can read that: I suppose there's little difference between using '\n' or using "\n", but the latter is an array of (two) characters, which has to be printed character by character, for which a loop has to be set up, which is more…
Fureeish
  • 12,533
  • 4
  • 32
  • 62
31
votes
2 answers

Type of ternary expression

Can anyone explain the output of the following program: #include using namespace std; int main() { int test = 0; cout << "First character " << '1' << endl; cout << "Second character " << (test ? 3 : '1') << endl; return…
amitkumarusc
  • 862
  • 10
  • 24
30
votes
6 answers

How to disable cout output in the runtime?

I often use cout for debugging purpose in many different places in my code, and then I get frustrated and comment all of them manually. Is there a way to suppress cout output in the runtime? And more importantly, let's say I want to suppress all…
user3639557
  • 4,791
  • 6
  • 30
  • 55
30
votes
5 answers

How do I correctly organize output into columns?

The first thing that comes to my mind is to do a bunch of \t's, but that would cause words to be misaligned if any word is longer than any other word by a few characters. For example, I would like to have something like: Name Last Name …
wrongusername
  • 18,564
  • 40
  • 130
  • 214
29
votes
3 answers

cout << order of call to functions it prints?

the following code: myQueue.enqueue('a'); myQueue.enqueue('b'); cout << myQueue.dequeue() << myQueue.dequeue(); prints "ba" to the console while: myQueue.enqueue('a'); myQueue.enqueue('b'); cout << myQueue.dequeue(); cout <<…
finiteloop
  • 4,444
  • 8
  • 41
  • 64
27
votes
5 answers

How does C++ interpret a cout with a '+' in it?

I've been moving back and forth with Java/C++ so I messed up with my console output and accidentally wrote lines like: cout << "num" + numSamples << endl; cout << "max" + maxSampleValue << endl; Each of which gave me bits and pieces of other…
GuitarStrum
  • 713
  • 8
  • 24
26
votes
9 answers

Why does this output of the same expression from printf differ from cout?

I'm using Visual C++ 2012 and compiling from the command line the following files: #include int main() { printf("%.5f", 18/4+18%4); return 0; } Linking with MSVCRT.LIB rather than LIBCMT to avoid runtime error R6002. The value…
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
26
votes
6 answers

cout not printing unsigned char

I am working on below code: #include #include using namespace std; main() { unsigned char a; a=1; printf("%d", a); cout<
Hitesh Menghani
  • 977
  • 1
  • 6
  • 14
24
votes
3 answers

Why is the address of this volatile variable always at 1?

I wanted to inspect the address of my variable volatile int clock; cout << &clock; But it always says that x is at address 1. Am i doing something wrong??
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
24
votes
2 answers

What is the difference between std::cout and std::wcout?

In c++ what is the difference between std::cout and std::wcout? They both control output to a stream buffer or print stuff to the console, or are they just alike ?
Whizz Mirray
  • 380
  • 1
  • 3
  • 9
24
votes
5 answers

Is std::cout buffered?

Just reading an old but interesting article by "Scott Meyers" http://aristeia.com/Papers/C++ReportColumns/novdec95.pdf Basically it is about preferring to use '\n' over std::endl (which I agree with and have used the same augment for years). BUT…
Martin York
  • 257,169
  • 86
  • 333
  • 562
24
votes
4 answers

How to get console output in Visual Studio 2012 Unit Tests

I have a managed C++ unit test in VS 2012. The test runs fine and I can verify that a loop with multiple cout calls is executed. However when I look at the test explorer the test is marked as passed but there is no hyper link for the output as I am…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
24
votes
6 answers

Float formatting in C++

How do you format a float in C++ to output to two decimal places rounded up? I'm having no luck with setw and setprecision as my compiler just tells me they are not defined. cout << "Total : " << setw(2) << total << endl; total outputs: Total …
eveo
  • 2,797
  • 15
  • 61
  • 95
23
votes
5 answers

Insight into how things get printed onto the screen (cout,printf) and origin of really complex stuff that I can't seem to find on textbooks

I've always wondered this, and still haven't found the answer. Whenever we use "cout" or "printf" how exactly is that printed on the screen?. How does the text come out as it does...(probably quite a vague question here, ill work with whatever you…
silent
  • 2,836
  • 10
  • 47
  • 73
22
votes
4 answers

Why is std::cout not printing the correct value for my int8_t number?

I have something like: int8_t value; value = -27; std::cout << value << std::endl; When I run my program I get a wrong random value of outputted to the screen, but when I run the program in gdb and use p value it prints out -27, which is the…
Grammin
  • 11,808
  • 22
  • 80
  • 138