Cin vs Scanf - C++ Input Comparison

  • Context: C/C++ 
  • Thread starter Thread starter benzun_1999
  • Start date Start date
  • Tags Tags
    C++ Comparison Input
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
9 replies · 21K views
benzun_1999
Messages
260
Reaction score
0
cin or scanf?

i use cin from iostream.h in c++ i also knowabout scanf in stdio.h but which is better? What is the difference betweeen the both?

-benny
 
Physics news on Phys.org
its a matter of which one your more comfortable with.
scanf requires you to know the format parameters(no spaces % & etc) whereas cin the format parameters are hidden and all you got to do is cin>>var. thus cin is nicer to look at. Personally though if you want to change parameters like precision i think it makes the code ugly.
ALso you can create functions based on scanf for easier display. UNfortunately I can't remember if C compilers allow for default values so i use C++ compiler and scanf.
 
(Almost) always cin.

Always prefer the updated versions of functions over the old C functions which are provided for compatibility. In the case of scanf, the danger occurs when passing "%s", where you risk buffer overflow. cin provides getline, which avoids the problem by requiring that you pass in the length of the buffer.

Changing precision using cin is more verbose, but in general using cin is preferable.
 
gnomedt said:
(Almost) always cin.

Always prefer the updated versions of functions over the old C functions which are provided for compatibility.

I agree: they didn't go to a new system just for kicks. Also, scanf requires passing pointers: classic bug injection site.

Cheers,
Tim
 
gnomedt said:
(cin provides getline, which avoids the problem by requiring that you pass in the length of the buffer.

Even better, use the standalone getline() which reads into a C++ string variable, Then you don't have to worry about the length of the buffer, because the string automatically expands as necessary!

Code:
#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string whatever;
    cout << "Give me a string, terminated with a newline: ";
    getline (cin, whatever);
    cout << "You entered \"" << whatever << "\"" << endl;
    return 0;
}

You can bang away on the keyboard almost forever before hitting the "enter" key, if you have enough memory. :cool:
 


benzun_1999 said:
i use cin from iostream.h in c++ i also knowabout scanf in stdio.h but which is better? What is the difference betweeen the both?

-benny

My friend,if you believe me i should say that "scanf()" and "printf()" are faster than "cin>> "and "count<<" Once I had a time limit when my code was submited ,after a long thought i changed "cin>> "and "count<< " by "scanf()" and "printf()" and my problem was solved, and if i remember well "scanf()" and "printf()" was 1sc faster then "cin" and "count".But to tell the truth scanf() has problems with strings.
 


mariam.kupa said:
My friend,if you believe me i should say that "scanf()" and "printf()" are faster than "cin>> "and "count<<" Once I had a time limit when my code was submited ,after a long thought i changed "cin>> "and "count<< " by "scanf()" and "printf()" and my problem was solved, and if i remember well "scanf()" and "printf()" was 1sc faster then "cin" and "count".But to tell the truth scanf() has problems with strings.

*shakes head in utter disbelief*
 


shoehorn said:
*shakes head in utter disbelief*


Sorry,my friend,but i didn't understand what do you mean? Am I wrong or not?
 


operator>> is overloadable to user-defined types, if you're into that. Otherwise, it's mostly a matter of aesthetic preference.

In the case of scanf, the danger occurs when passing "%s", where you risk buffer overflow.

Then don't take %s arguments with scanfs! Use fgets for those.

I agree: they didn't go to a new system just for kicks.

Welcome to software.