Thread: printf() and cout>>

  1. #1
    Unregistered
    Guest

    printf() and cout>>

    What's the difference between printf() and cout in c++ all the tutorials i read use cout and i bought a book for c just c and it used printf what is the difference and which is better.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    Which is better? Depends who you ask from (I prefer printf()). I consider printf as something that gives more control to me. Second is that cout takes lots of space (as in typing it, I mean). I also find it frustrating to type those <<'s all the time. A major reason is that I have never really learned cout. Ask me how to display the hexadecimal value of char in cout and see me leaving the room...

    But if you want to get easier and not to think of the datatype you are handling perhaps cout is more to you.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Basic differences:

    printf is a function. It is available in C and C++.
    cout is an object having multiple overloaded operators << and as such is only available in C++.

    What is better is a matter of taste. printf is considerably faster, but printing text to the screen is nothing you would need state of the art speed for in my eyes. With printf, complex formatting is easier, while cout is more like a universal output: thow something at it, and it will be printed to the screen. Not the way you'd want it, but quick and perfect for debugging. From my experience, people who need something on screen without requirements for formatting or without intimate knowledge of datatypes (i.e. students assignment) use cout, while people with complex formatting specifications like printf better.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    1
    Just do what you like best.

    The << style might seem a bit complicated at first, but when using streams you'll find them very useful!


    btw:

    char c = 'A';
    std::cout << static_cast<int>(c) << " - " << std::hex << static_cast<int>(c);

    is a way to show dec and hex value of a char

  5. #5
    I like cout, because I don't understand what those different things following the '%' mean. That's just ignorance though.
    What will people say if they hear that I'm a Jesus freak?
    What will people do if they find that it's true?
    I don't really care if they label me a Jesus freak, there is no disguising the truth!

    Jesus Freak, D.C. Talk

    -gnu-ehacks

  6. #6
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    I prefer
    cout << "because its easier";
    and maybe it's because I have never even touched printf. And btw, in the title the cout arrows are the wrong way (they shold be like << this instead of >> this).


    I can't believe no on else noticed that.

Popular pages Recent additions subscribe to a feed