cout vs printf - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

cout vs printf

Not sure if they do the same thing, but what are merrits of cout vs. printf, or cin vs. scanf. Is there any reason to use one and not the other, and if so, is there a rule of thumb for when to use which?



Thanks


Comments

  • : Not sure if they do the same thing, but what are merrits of cout vs. printf, or cin vs. scanf. Is there any reason to use one and not the other, and if so, is there a rule of thumb for when to use which?

    :

    : Thanks

    :



    Really it's just your preference.



    I prefer printf to cout, but I prefer cin to scanf.



    I'd personally recommend using cin, and it's up to you if you want to use printf or cout. One thing though, cout won't work in a C program, because it's a class, but that's not really an issue.



    Anyways, the reason I choose printf to cout is it's less, or the same amount of typing as cout, usually less, and it's quite a bit easier to have you comma's and periods and stuff, so you don't end up with "The score is35" as opposed to "The score is 35.". Though, printf takes a little more stuff to remember, the most common ones (probably the only ones you'll ever use) are easy to remember and logical. Also, printf is easier to type, you don't have to keep making those annoying shift-left's and opening and closing quotes, the only thing you do have to worry about is %'s, which isn't too bad.



    Here's an example where I consider printf superior to cout (or if you want to get technical cout.operator<<()).<br>


    cout<<"The score is "<<score<<".
    ";<br>
    printf("The score is %d.",score);



    or



    cout<<"The average of "<<score1<<", "<<score2<<", and "<<score2<<" is "<<average<<".
    ";<br>
    printf("The average of %d, %d, and %d is %d.
    ",score1,score2,score3,average);


  • : Not sure if they do the same thing, but what are merrits of cout vs. printf, or cin vs. scanf. Is there any reason to use one and not the other, and if so, is there a rule of thumb for when to use which?

    :

    : Thanks

    :



    Really it's just your preference.



    I prefer printf to cout, but I prefer cin to scanf.



    I'd personally recommend using cin, and it's up to you if you want to use printf or cout. One thing though, cout won't work in a C program, because it's a class, but that's not really an issue.



    Anyways, the reason I choose printf to cout is it's less, or the same amount of typing as cout, usually less, and it's quite a bit easier to have you comma's and periods and stuff, so you don't end up with "The score is35" as opposed to "The score is 35.". Though, printf takes a little more stuff to remember, the most common ones (probably the only ones you'll ever use) are easy to remember and logical. Also, printf is easier to type, you don't have to keep making those annoying shift-left's and opening and closing quotes, the only thing you do have to worry about is %'s, which isn't too bad.



    Here's an example where I consider printf superior to cout (or if you want to get technical cout.operator<<()).



    cout<<"The score is "<<score<<".
    ";

    printf("The score is %d.",score);



    or



    cout<<"The average of "<<score1<<", "<<score2<<", and "<<score2<<" is "<<average<<".
    ";

    printf("The average of %d, %d, and %d is %d.
    ",score1,score2,score3,average);



    PS printf is a LOT nicer to type in HTML than cout is.


  • Just remember that printf and a scanf are in a file (stdio.h) that is about 1/40'th the size of iostream.h (In number of lines in ASM).



    Printf and scanf are both compatible with C also, so that is another plus. Not to mention, Borland has built in color changing functions using cprintf and cscanf (both in conio.h), so it is a good thing to know how to use the (c/s/f)printf and (c/s/f)scanf family.




  • : Just remember that printf and a scanf are in a file (stdio.h) that is about 1/40'th the size of iostream.h (In number of lines in ASM).

    :

    : Printf and scanf are both compatible with C also, so that is another plus. Not to mention, Borland has built in color changing functions using cprintf and cscanf (both in conio.h), so it is a good thing to know how to use the (c/s/f)printf and (c/s/f)scanf family.



    To tell you the truth, in any place besides the Turbo C++ highschool classroom, cout and cin are rarely used from what I've seen.



    I find that printf() and scanf() are much more powerful than their iostream counterparts, and much easier to use.



    For instance in order to replicate this line in C:



    int weight = 100;

    float height = 5.02124, width = 3.2032;

    char *name = "Joey";



    printf ("Hello, my name is %s. I weigh %i kilograms, am %.2f feet tall and am %.3 feet wide.
    ", name, weight, height, width);



    You'd have to do this:





    cout << "Hello, my name is " << name << ". I weigh " << weight << " kilograms and am ";<br>


    cout.precision (2);



    cout << height << " feet tall and am ";<br>


    cout.precision (3);



    cout << width << "feet wide.";<br>


    Now granted I probably haven't made the code perfect, but you get the idea. Personally I don't like using << to send text out.<br>


    -Xotor-


  • Same here. I don't know when I started using printf and scanf, but I won't go back to iostream now that I've seen how useful they are.



    sprintf is probably the most usefule function in stdio.h, I couldn't handle not being able to use it easily ;)




  • : Not sure if they do the same thing, but what are merrits of

    : cout vs. printf, or cin vs. scanf. Is there any reason to

    : use one and not the other, and if so, is there a rule of

    : thumb for when to use which?

    :

    : Thanks

    :



    cin and cout are only available in C++, so if you're writing C, you don't have much choice. :-)



    Since C++ is a superset of C, you can use printf() and scanf(), but most hardcore C++ people seem to look down on their use. One reason is the combination of a useful standard library of classes and the fact that operators can be overloaded in C++, resulting in code that can be much more elegant than when writing the equivalent code in C. For example, the following C++ code reads a line of arbitrary length from standard input, automagically handling memory allocation:



        #include <string>

        ...

        string mystr;

        ...

        cin >> mystr;



    Doing the equivalent in C takes a pretty good bit of work: you have to be able to (correctly) handle the calls to malloc() and realloc(), understand pointer manipulation, remember to free() the allocated memory, safeguard against memory leaks, etc.



    Another plus for cin and cout is that you don't have to worry about getting a format string right, e.g. in printf(), using %d when you meant to use %f or the sort of ugly way to handle things like size_t, whose actual type is not necessarily the same on all compilers. Also, stream stuff isn't limited to the things that printf() and scanf() can handle. You can overload the insertion (<<) and extraction (>>) operators to handle most any kind of data type, so you can do stuff like "cout << some_object" and the Right Thing will happen.



    Plain old scanf() can be a pain to use safely, and isn't exactly recommended for use in either C or C++.



    I've known C for a pretty good while, and I've only just started getting into C++, but I can already see why C++ people favor the stream method of I/O rather than printf() and scanf().



    A general rule of thumb: If you're using C, you can't use the cin/cout stuff at all and will have to use printf() and scanf(), although using fgets() and sscanf() instead of scanf() is recommended. If you're using C++, use the cin/cout stuff unless you have a compelling reason not to.





    (.piliq.)




Sign In or Register to comment.