473,706 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The "<<" operator behind cout

Hey...
Can anybody tell me why "<<" operator is used behind cout?
Like:
cout<<"Welcome" ;
My one friend told me that it "shifts the bits"...But it's not very
clear to me...Can anybody tell?

Jan 8 '07 #1
8 1788
"Prateek" <pr***********@ gmail.comwrote in message
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .
Hey...
Can anybody tell me why "<<" operator is used behind cout?
Like:
cout<<"Welcome" ;
My one friend told me that it "shifts the bits"...But it's not very
clear to me...Can anybody tell?
Unique to C++ (isnt' used that way in C).
It is the operator<<
That's the only thing I can see it's called as. It would call the function
prototyped something like (this may be wrong in details, probably is)
ostream& operator<<( ostream& os, const char* )
Jan 8 '07 #2
"Prateek" <pr***********@ gmail.comwrote in message
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .
Hey...
Can anybody tell me why "<<" operator is used behind cout?
Like:
cout<<"Welcome" ;
My one friend told me that it "shifts the bits"...But it's not very
clear to me...Can anybody tell?
The default operator<< for integers will shift the bits, yes. But in C++
most operators are overloadable, which means anyone can introduce his own
meaning to the various operators when used on custom types. For
std::ostream, the operator<< is used to output a textual representation of
the right hand side argument to the stream.

- Sylvester
Jan 8 '07 #3

Prateek wrote:
Hey...
Can anybody tell me why "<<" operator is used behind cout?
Like:
cout<<"Welcome" ;
My one friend told me that it "shifts the bits"...But it's not very
clear to me...Can anybody tell?
In C the operator << is a bitwise shift operator.
But in C++ this operator is an overloaded operator that works as an
output operator.

Jan 8 '07 #4

Prateek wrote:
Hey...
Can anybody tell me why "<<" operator is used behind cout?
Like:
cout<<"Welcome" ;
My one friend told me that it "shifts the bits"...But it's not very
clear to me...Can anybody tell?
In C the operator << is a bitwise shift operator.
But in C++ this operator is an overloaded operator that works as an
output operator.

Jan 8 '07 #5
"Prateek" <pr***********@ gmail.comwrote:
>Hey...
Can anybody tell me why "<<" operator is used behind cout?
Like:
cout<<"Welcome ";
My one friend told me that it "shifts the bits"...But it's not very
clear to me...Can anybody tell?
The "<<" operator normally shifts bits. But cout is of type
std::ostream, and that class overloads the "<<" operator, redefining
it as "insertion" . The item on the right side of the operator
("Welcome", in your example) is inserted into the ostream. That causes
it to be written to whatever the ostream has been told to write to.
For the standard object "cout" that's the console.

--
Tim Slattery
Sl********@bls. gov
http://members.cox.net/slatteryt
Jan 8 '07 #6
"prabhu_ani c" <pr*********@gm ail.comwrote:
>In C the operator << is a bitwise shift operator.
But in C++ this operator is an overloaded operator that works as an
output operator.
In C++ the "<<" operator is also bitwise shift. *Any* operator can be
overloaded in C_++, std::ostream overloads this one (and others).

--
Tim Slattery
Sl********@bls. gov
http://members.cox.net/slatteryt
Jan 8 '07 #7
Tim Slattery wrote:
"prabhu_ani c" <pr*********@gm ail.comwrote:
>In C the operator << is a bitwise shift operator.
But in C++ this operator is an overloaded operator that works as an
output operator.

In C++ the "<<" operator is also bitwise shift. *Any* operator can be
overloaded in C_++, std::ostream overloads this one (and others).
*Almost* any...

http://www.parashift.com/c++-faq-lit....html#faq-13.5
Jan 8 '07 #8
Thanks to you all. I also asked this question to my school C++ teacher.
She said that "<<" take the thing behind bitwise(bit by bit, one bit at
a time), and that's why it is said that "it shifts the bits". According
to her, in cin>>, ">>" is also the same but it takes input. But I was
not satisfied by this answer. But after reading your replies, now I
think I'm quite clear about it. Thank You!!!

Jan 12 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
9029
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex' undeclared (first use this function)
16
2087
by: Michael | last post by:
just a quickie: as cout << endl; is to cout << "\n"; is there an equivalent for cout << "\t";
2
8860
by: Generic Usenet Account | last post by:
What exactly is the difference between the hex manipulator and the following statement: cout.setf(ios_base::hex)? According to Stroustrup, Third Edition, Section 21.4.4, "once set, a base is used until reset". For some reason, I interpreted this to mean that "<< hex" manipulator would cause the base to be set for only the current statement, while the cout.setf(ios_base::hex) statement would cause the base to be set for multiple...
4
1762
by: wangzhihuii | last post by:
Hi all, I'm really confused, can cout<<""; contribute anything to the routine ?!! my programm won't work properly without this trivial sentence. Sincerely vivian
12
6098
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
3
2156
by: key9 | last post by:
Hi all I am confuse of how to override operate "<<" Sample , I've got 2 class class Terminal { public:
1
1757
by: Sunny | last post by:
The way to overload operator << is : ostream& operator << (ostream& os, const Obj& obj); and this is a member function. My question is why do we need to provide a const reference of Obj as argument when it is a member function and its members accessible through this pointer ? Isnt cout << obj; equivalent to obj.operator<<(cout) ? Thanks
0
8697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9056
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7912
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4710
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2096
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.