'printf' vs. 'cout' in C++

后端 未结 16 1569
梦如初夏
梦如初夏 2020-11-22 07:04

What is the difference between printf() and cout in C++?

相关标签:
16条回答
  • 2020-11-22 07:37

    With primitives, it probably doesn't matter entirely which one you use. I say where it gets usefulness is when you want to output complex objects.

    For example, if you have a class,

    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class Something
    {
    public:
            Something(int x, int y, int z) : a(x), b(y), c(z) { }
            int a;
            int b;
            int c;
    
            friend ostream& operator<<(ostream&, const Something&);
    };
    
    ostream& operator<<(ostream& o, const Something& s)
    {
            o << s.a << ", " << s.b << ", " << s.c;
            return o;
    }
    
    int main(void)
    {
            Something s(3, 2, 1);
    
            // output with printf
            printf("%i, %i, %i\n", s.a, s.b, s.c);
    
            // output with cout
            cout << s << endl;
    
            return 0;
    }
    

    Now the above might not seem all that great, but let's suppose you have to output this in multiple places in your code. Not only that, let's say you add a field "int d." With cout, you only have to change it in once place. However, with printf, you'd have to change it in possibly a lot of places and not only that, you have to remind yourself which ones to output.

    With that said, with cout, you can reduce a lot of times spent with maintenance of your code and not only that if you re-use the object "Something" in a new application, you don't really have to worry about output.

    0 讨论(0)
  • 2020-11-22 07:37

    I'm not a programmer, but I have been a human factors engineer. I feel a programming language should be easy to learn, understand and use, and this requires that it have a simple and consistent linguistic structure. Although all the languages is symbolic and thus, at its core, arbitrary, there are conventions and following them makes the language easier to learn and use.

    There are a vast number of functions in C++ and other languages written as function(parameter), a syntax that was originally used for functional relationships in mathematics in the pre-computer era. printf() follows this syntax and if the writers of C++ wanted to create any logically different method for reading and writing files they could have simply created a different function using a similar syntax.

    In Python we of course can print using the also fairly standard object.method syntax, i.e. variablename.print, since variables are objects, but in C++ they are not.

    I'm not fond of the cout syntax because the << operator does not follow any rules. It is a method or function, i.e. it takes a parameter and does something to it. However it is written as though it were a mathematical comparison operator. This is a poor approach from a human factors standpoint.

    0 讨论(0)
  • 2020-11-22 07:38

    From the C++ FAQ:

    [15.1] Why should I use <iostream> instead of the traditional <cstdio>?

    Increase type safety, reduce errors, allow extensibility, and provide inheritability.

    printf() is arguably not broken, and scanf() is perhaps livable despite being error prone, however both are limited with respect to what C++ I/O can do. C++ I/O (using << and >>) is, relative to C (using printf() and scanf()):

    • More type-safe: With <iostream>, the type of object being I/O'd is known statically by the compiler. In contrast, <cstdio> uses "%" fields to figure out the types dynamically.
    • Less error prone: With <iostream>, there are no redundant "%" tokens that have to be consistent with the actual objects being I/O'd. Removing redundancy removes a class of errors.
    • Extensible: The C++ <iostream> mechanism allows new user-defined types to be I/O'd without breaking existing code. Imagine the chaos if everyone was simultaneously adding new incompatible "%" fields to printf() and scanf()?!
    • Inheritable: The C++ <iostream> mechanism is built from real classes such as std::ostream and std::istream. Unlike <cstdio>'s FILE*, these are real classes and hence inheritable. This means you can have other user-defined things that look and act like streams, yet that do whatever strange and wonderful things you want. You automatically get to use the zillions of lines of I/O code written by users you don't even know, and they don't need to know about your "extended stream" class.

    On the other hand, printf is significantly faster, which may justify using it in preference to cout in very specific and limited cases. Always profile first. (See, for example, http://programming-designs.com/2009/02/c-speed-test-part-2-printf-vs-cout/)

    0 讨论(0)
  • 2020-11-22 07:41

    One is a function that prints to stdout. The other is an object that provides several member functions and overloads of operator<< that print to stdout. There are many more differences that I could enumerate, but I'm not sure what you are after.

    0 讨论(0)
提交回复
热议问题