Cin vs Scanf - C++ Input Comparison

  • Context: C/C++ 
  • Thread starter Thread starter benzun_1999
  • Start date Start date
  • Tags Tags
    C++ Comparison Input
Click For Summary

Discussion Overview

The discussion centers around the comparison of input methods in C++, specifically between cin and scanf. Participants explore their differences, advantages, and disadvantages, touching on aspects such as safety, performance, and usability.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Exploratory

Main Points Raised

  • Some participants argue that cin is generally preferable due to its safety features, such as avoiding buffer overflow issues associated with scanf.
  • Others mention that scanf can be faster than cin and cout, particularly in time-sensitive situations, although they acknowledge that scanf has issues with string handling.
  • One participant highlights that cin allows for operator overloading for user-defined types, which can be an advantage in certain contexts.
  • Concerns are raised about the verbosity of changing precision with cin, while some find scanf more aesthetically pleasing despite its risks.
  • There is a suggestion to use fgets as an alternative to scanf for string input to mitigate risks.

Areas of Agreement / Disagreement

Participants express differing opinions on the advantages and disadvantages of cin versus scanf. There is no consensus on which method is definitively better, as preferences vary based on context and individual experience.

Contextual Notes

Some participants note that the choice between cin and scanf may depend on specific use cases, such as performance requirements or the need for safety in handling input.

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 "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?
 
  • #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
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
14
Views
4K
Replies
4
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 5 ·
Replies
5
Views
3K