Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Cin or scanf?

  1. Jul 20, 2006 #1
    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???

  2. jcsd
  3. Jul 20, 2006 #2
    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 gotta 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.
  4. Jul 21, 2006 #3
    (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.
  5. Jul 22, 2006 #4
    I agree: they didn't go to a new system just for kicks. Also, scanf requires passing pointers: classic bug injection site.

  6. Jul 28, 2006 #5
    use fgets(); :)
  7. Jul 28, 2006 #6


    User Avatar

    Staff: Mentor

    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 (Text):

    #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:
  8. Jan 27, 2009 #7
    Re: cin or scanf???

    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.
  9. Jan 27, 2009 #8
    Re: cin or scanf???

    *shakes head in utter disbelief*
  10. Jan 29, 2009 #9
    Re: cin or scanf???

    Sorry,my friend,but i didn't understand what do you mean? Am I wrong or not?
  11. Jan 29, 2009 #10
    Re: cin or scanf???

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

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

    Welcome to software.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook