C/C++ Cin vs Scanf - C++ Input Comparison

Click For Summary
The discussion centers on the comparison between C++'s cin and C's scanf for input handling. Users express a preference for cin due to its ease of use, as it abstracts format parameters and provides a cleaner syntax. However, scanf is noted for its speed advantages in certain scenarios, particularly under time constraints, although it poses risks such as buffer overflow when using "%s". The conversation highlights that cin's getline function offers a safer alternative for string input, automatically managing buffer sizes. Additionally, there are mentions of the ability to overload operators with cin, which can enhance functionality for user-defined types. Overall, while cin is generally favored for its safety and readability, scanf may be chosen for performance in specific cases.
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
 
Technology 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
 
use fgets(); :)
 
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 "cout<<" Once I had a time limit when my code was submited ,after a long thought i changed "cin>> "and "cout<< " by "scanf()" and "printf()" and my problem was solved, and if i remember well "scanf()" and "printf()" was 1sc faster then "cin" and "cout".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 "cout<<" Once I had a time limit when my code was submited ,after a long thought i changed "cin>> "and "cout<< " by "scanf()" and "printf()" and my problem was solved, and if i remember well "scanf()" and "printf()" was 1sc faster then "cin" and "cout".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?
 
  • #10


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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
14
Views
3K
Replies
4
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
9K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 5 ·
Replies
5
Views
3K