473,506 Members | 16,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why <<,>> operators are overloded to perform input and output?

5 New Member
Why <<(),>>() operators are overloded to perform input and output,why not any function is preferred, like scanf and printf in c
Feb 3 '11 #1
1 1373
horace1
1,510 Recognized Expert Top Contributor
a major problem with the printf() and scanf() functions is getting the conversion specification corresponding to the parameters correct in particular when first learning C. e.g. %d for int
Expand|Select|Wrap|Line Numbers
  1.  
  2.     int data
  3.     printf("data %d\n", data);
  4.  
when you have a long list of parameters of different types it is very easy to get the conversion specifications wrong and it prints garbage.
Scanf also requires the address of operator & for input of simple types
Expand|Select|Wrap|Line Numbers
  1.  
  2. scanf("%d", &data);
lots of students forget the &

C++ has the put to operator >> and get from operators <<
Expand|Select|Wrap|Line Numbers
  1.     int data;
  2.     cin >> data;
  3.     cout << "data " << data << endl;
  4.  
C++ looks at the paramter type (int in this case) and calls the correct version of the overloaded functions operator<<() and operator>>(). Makes life much simpler.

To avoid the problems associated with scanf() and printf() some C books provide libraries of IO functions like printInt() and readInt(). I don't like them prefering to teach C using the C++ << and >> operators for IO. There are pros (IO is easier) and cons, i.e. sooner or later students have to lean printf() and scanf()
Feb 3 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
1460
by: Christopher Benson-Manica | last post by:
I know I keep asking similar questions, but I really want to do this at least sort of right. Not to mention I got no on-group replies to my previous post :( I desperately want an interface that...
2
2427
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
3
2451
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
0
1101
by: Pedro Werneck | last post by:
Hi list I'm trying to implement a new type in a C extension and it must support some binary operators, like &, |, ^, << and >>. With &, | and ^, the method must receive another object of the...
5
5091
by: Ian Lazarus | last post by:
Hello, My question is whether it is possible to avoid assignment on the left hand side of an overloaded operator << expression, as in the code below. Without the assignment, the compiler...
17
3011
by: Peter Bromley | last post by:
The following code snippet does not seem to work correcly unsigned __int64 result = 0xFFFFFFFFFFFFFFFF; result = result << 64; Debugger.WriteLine(System::String::Format(S"{0:X16}",...
9
1670
by: vineoff | last post by:
Why do I have to declare my oper<< as friend in my class as follows: class A { public: friend std::ostream& operator<<(std::ostream& out, const A& a) { return out << "foo"; } };
3
11669
by: jparulan | last post by:
Hi All, I'm using SOAP3.0. I was able to successfully call a WSDL file and get a value properly. But when the WSDL changed to have a MULTIPLE <element name> it was failing. This code works...
8
1593
by: markengley | last post by:
Hi everyone hope you are all ok. Just learning C++ (as you can tell from the noob question Im about to ask). One symbol/function/assignment/whatever really bothers me which is the "<<" one. ...
4
5918
by: PulsarSL | last post by:
Hi I'm working on improving the "Simpletron" computer described in Deitel & Deitel's "C++ How to Program" 3rd edition. Check out http://www.lehigh.edu/~ejk0/smlspec.pas for a description if...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7105
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...
0
7371
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...
1
7023
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...
1
5037
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...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
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 ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.