0

Recently I've been watching "Intro to C++" videos, some of which do a better job of explaining certain concepts than others. However, there is something none of the videos have addressed and that is the meaning of the carrot brackets.

Ex:

int main(){  

    cout << "Hello World" << endl;
    return 0;

}

I'm only interested in the first line of the above function. If I'm understanding correctly, cout prints the variable or value that follows, while endl indicates the end of the line while representing "\n".

I've googled C++ operators and keywords, and this appears in the lists of neither, although << apparently also serves as a binary left shift operator...

To clarify I'm coming from a Python/Java background so I'd expect a comma or addition sign. I want to know if << is the equivalent of these or does something similar but different.

Thanks.

  • 1
    Google: operator overloading. – Yu Hao May 13 '16 at 19:21
  • << is overloaded to serve as console output operator too. – K.M. Shihab Uddin May 13 '16 at 19:22
  • 7
    Are those _really_ called _carrot_ (?!) brackets? – ForceBru May 13 '16 at 19:23
  • 1
    It's called the insertion operator. – erip May 13 '16 at 19:25
  • 3
    Do you mean "caret"? The `<` and `>` characters do resemble the *caret* character `^`. But I've never heard either term used in this context. The `<` and `>` characters are usually referred to as "less-than" and "greater-than", respectively. – Keith Thompson May 13 '16 at 19:25
  • 3
    @ForceBru I've only heard "angle brackets" ... – o11c May 13 '16 at 19:27
  • 3
    @ForceBru no, they're alligator mouths ;) – kmdreko May 13 '16 at 19:30
  • Interesting to see some behind the scenes discussion about the `<<` operator [here](http://www.drdobbs.com/cpp/a-personal-note-about-argument-dependent/232901443). It talks about ADL and namespaces, but the introduction might be helpful even if the rest is not relevant to this question. – wally May 13 '16 at 19:30
  • Honestly wasn't sure what they were called either. I saw carrot brackets in a couple places so I just used that. Made sense to me considering curly brackets and square brackets are just called by what they look like as well. – Karla Miletti May 13 '16 at 19:34
  • I think you mean "caret", not "carrot". But a caret is the ^ character. The < character is an angle bracket. – Dan Korn May 13 '16 at 19:37
  • I think `c++` choose [`<<`](http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/) because it emphasize the direction of the "flow" of data stream. Don't forget `cout` is a `ostream` object. – gdlmx May 13 '16 at 19:50
  • https://en.m.wikipedia.org/wiki/Guillemet – rici May 13 '16 at 20:44
  • Does this answer your question? [<< and >> in C++](https://stackoverflow.com/questions/5123674/and-in-c) – tripleee Oct 01 '21 at 09:28

3 Answers3

6

These are not "carrot brackets". << is left-bitshift operator and nothing more.

You can print stuff using << because C++ allows you to change how operators behave with specific classes. This is called operator overloading. << is overloaded for std::cout to behave as a printing function.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • Thanks! Is this the only operator that is overloaded in this way? – Karla Miletti May 13 '16 at 19:37
  • 1
    @KarlaMiletti As far as I know, for `cout` - yes. Some of the other classes have different custom operators. For example, you can use `>>` on `std::cin` to read stuff into variables. – HolyBlackCat May 13 '16 at 19:40
  • 1
    @KarlaMiletti No, it's not. `+` is overloaded for `std::string` to perform concatenation instead of addition. – Barmar May 13 '16 at 19:50
4

C++ allows operator overloading so the meaning of << changes depending on the types involved. cout is of type ostream which overloads << to write to the stream.

Here is a listing of many of them note that it isn't a complete list because you can have operators defined outside of the ostream class as in this example on MSDN of a Date.

Guvante
  • 18,775
  • 1
  • 33
  • 64
4

In the semantic context of streams, << is called the "stream insertion operator" and >> is called the "stream extraction operator. Syntactically, they are the same as the left and right shift operators that semantically operate on integers.

From the context of Java, you should be familiar with this with the + operator, which acts differently depending on whether its arguments are ints, floats, or strings (although C++ allows general overloading).

std::endl differs from '\n' in that it also flushes the stream (note that std::cout is line-buffered by default if it is going to a terminal).

Using a + for output means that a bunch of unnecessary temporary strings would be created, and overloading , (like && or ||) would be surprising since it would hide the builtin meaning.

If variadic templates had been available when C++ was in its early stages, it likely would not have used <<, but a member function.

o11c
  • 15,265
  • 4
  • 50
  • 75
  • Thanks for tying your answer back to Java, it was very helpful. Is "flushes the stream" memory management terminology? – Karla Miletti May 13 '16 at 19:42
  • 1
    @KarlaMiletti No. Streams are objects that cam be used to read and/or write things. `std::cout` is an example of an output stream. For performance reasons, data that you put into a stream is not being outputted to screen (or file, for file streams) immediately. Instead, it's being outputted in large blocks from time to time. Such output of block of data is called *flushing*. – HolyBlackCat May 13 '16 at 19:46