What is cout? It's a variable, yet it can print stuff on the screen. Also, << (Left Shift Operator) isn't like the one for the variable cout.

How does cout work? cout isn't even a macro / #define.

Recommended Answers

All 5 Replies

Yes, cout is a variable but of type ostream. To be more precise its an object of class ostream which has << as overloaded operators.

So whenever you use that operator on cout, it performs output to the screen.

Cout is the same as the C language stdout. Output goes to the console/terminal where the program was run, or to the file stdout was redirected to by the shell. The << operator is an input operator, and basically does nothing with cout/stdout. It is used with cin/stdin (standard input - usually the keyboard, or file if redirected by the shell). From the command line, the stty command uses << to read the status of the specified file/stream descriptor to determine what it's settings are, such as baud rate, etc. In any case, I think it is time you read some documentation/manuals/books on Unix/Linux I/O systems to better understand this stuff.

>>What is cout? It's a variable, yet it can print stuff on the screen. std::cout is an object of the class std::ostream (or a class derived from std::ostream ). It has an internal connection to the terminal output (or DOS output), which is the same as the C alternative stdout . Most systems implement the terminal input/output by a kind of file emulation, this is why the type of stdout is FILE* . The std::cout object also does buffering of the data that is outputted to it, for maximum efficiency in the memory access.

>>Also, << (Left Shift Operator) isn't like the one for the variable cout.

The basic semantics of the left-shift operator is to perform a binary bit-shift to the left. However, C++ allows for operator overloading for any user-defined class. This means that you can define your own function(s) to be called when a certain operator is used. In general, this feature is used to be able to create a custom class that can behave as a built-in type (with the same operators) and this usually implies that the semantics of the operators are preserved (a + sign is an addition, etc.). The developers of the C++ standard I/O library thought it would be a good idea to use the left-shift and right-shift operators to do input / output operations, this is ok because the original semantics of these operators is clearly not applicable in this case (you can't do a left bit-shift on an output stream). So, that's why the left-shift operator behaves differently for the std::cout object (like it does for any object of the class std::ostream ), that is, the operator causes an element to be outputted on the stream.

>>How does cout work?

That's not a question that should concern you right now. This site will give as much information as you need.

I think the reason that there are no Marco/#Difine stuff is because iostream is a library not a header file so it doesn't contain any real code... But from the talk it must be in code somewhere... As some extra information I thin the word 'cout' means "Charecter(s) Out"

I think the reason that there are no Marco/#Difine stuff is because iostream is a library not a header file so it doesn't contain any real code... But from the talk it must be in code somewhere... As some extra information I thin the word 'cout' means "Charecter(s) Out"

I think cout stands for Console Output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.