Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1043377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:39:29+00:00 2026-05-16T15:39:29+00:00

Possible Duplicates: printf vs cout in C++ cin or printf?? I’ve always wondered about

  • 0

Possible Duplicates:
printf vs cout in C++
cin or printf??

I’ve always wondered about printf and cout.. which one is ultimately faster, and is it the most flexible as well (ie can print a range of variables, and output can be formatted)?

P.S.
I know this looks similar to 'printf' vs. 'cout' in C++ ,but i’m not really asking the same thing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:39:30+00:00Added an answer on May 16, 2026 at 3:39 pm

    Short Answer

    Faster : printf

    More flexible : cout

    Long answer

    When compared to the sprintf family, the C++ streams are supposed to be slower (by a factor 6 if I recall an item of Exceptional C++, by Herb Sutter). Still, most of the time, you won’t need this speed, but you need to be sure your code won’t be bugged.

    And it is easy to do something wrong with the printf family of functions, be it putting the wrong number of arguments, the wrong types, or even introduce potential security vulnerability (the %n specifier comes to mind) in your code.

    Unless really wanting it (and then, it’s called sabotage), it’s almost impossible to get it wrong with C++ streams. They handle seamlessly all known types (build-ins, std::strings, etc.), and it’s easy to extend it. For example, let’s say I have an object "Coordinate3D", and that I want to print out its data:

    #include <iostream>
    
    struct Coordinate3D
    {
        int x ;
        int y ;
        int z ;
    } ;
    
    std::ostream & operator << (std::ostream & p_stream
                              , const Coordinate3D & p_c)
    {
        return p_stream << "{ x : " << p_c.x
                       << " , y : " << p_c.y
                       << " , z : " << p_c.z << " }" ;
    }
    
    int main(int argc, char * argv[])
    {
        Coordinate3D A = {25,42,77} ;
        std::cout << A << std::endl ;
              // will print "{ x : 25 , y : 42 , z : 77 }"
        return 0 ;
    }
    

    The problem with the stream is that they are quite difficult to handle correctly when wanting to specify format of some data (padding spaces for numbers, for example), and that sometimes, you really really need to go fast. Then, either fall back to printf, or try some high-speed C++ alternatives (FastFormat comes to mind).

    Edit: Note that Thomas‘ series of tests show interesting results (which I reproduced right now on my computer), that is: cout and printf have similar performances when one avoids using std::endl (which flushes the output in addition to outputing a \n).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++: What is the printf() format spec for “float”? I am new
Possible Duplicate: Why does Mac's $find not have the option -printf? Not sure what
Possible Duplicate: php loop through associative arrays i have a variable which prints an
Possible Duplicates: Why is lock(this) {...} bad? In C# it is common to use
Possible Duplicates: What is the best approach for a Java developer to learn C++
Possible Duplicates: Formatting of if Statements Is there a best coding style for identations
Possible Duplicates: Is String.Format as efficient as StringBuilder C# String output: format or concat?
Possible Duplicates: Valid use of goto for error management in C? Examples of good
Possible Duplicates: function overloading in C Does C support overloading ? Can anyone explain
Possible Duplicate: What’s the “condition” in C interview question? void main() { if(CONDITION) printf(Hello

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.