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 7942059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:51:08+00:00 2026-06-03T23:51:08+00:00

Since my shifting from C to C++ I have a question on STL’s formatting

  • 0

Since my shifting from C to C++ I have a question on STL’s formatting output. How ostreams tell one basic type from another?

In C with its printf and formatting strings it was pretty straightforward, but in C++ ostreams somehow distinguish basic types automatically. It puzzles me.

For example, in the following code,

int i;
float f;

std::cout << i << std::endl;
std::cout << f << std::endl;

how cout “knows” that i is an int and f is a float?

  • 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-06-03T23:51:10+00:00Added an answer on June 3, 2026 at 11:51 pm

    The compiler converts the operators to function calls. So that

    std::cout << i
    

    becomes

    operator<<(std::cout, i)
    

    Somewhere buried deep in the bowels of the standard library headers there are function declarations (functionally equivalent to):

    std::ostream& operator<<(std::ostream& o, int i);
    std::ostream& operator<<(std::ostream& o, double d);
    

    That is, operator<< is overloaded. When the function call is made, the compiler chooses the function overload which is the best match to the arguments passed in.

    In the case of std::cout << i, the int overload is chosen. In the case of std::cout<<d, the double overload is chosen.

    You can see function overloading in action fairly simply with a contrived example:

    #include <stdio.h>
    
    void print(int i) {printf("%d\n", i);}
    void print(double d) {printf("%f\n", d);}
    
    int main()
    {
       int j=5;
       double f=7.7;
    
       print(j);
       print(f);
    }
    

    Producing the output:

    5
    7.700000
    

    Try it for yourself: http://ideone.com/grlZl.

    Edit: As Jesse Good points out, the functions in question are member functions. So really we have:

    std::cout << i
    

    becomes

    std::cout.operator<<(i)
    

    And in the headers there are declarations (equivalent to):

    class ostream {
        ostream& operator<<(int i);
        ostream& operator<<(double d);
        ...
    };
    

    The same basic idea holds, however.

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

Sidebar

Related Questions

Since version 1.5 Subversion supports to have a local caching-proxy for the main Master-repository.
Since Eclipse 3.5 there seems to be no option to have drop a plugin
As some of you may notice this question is problem 16 from Project Euler
I have been doing Windows programming in .Net since last two years. Now I
I have used makecontext/swapcontext successfully for shifting the stack. However, when I try to
Since I started writing this question, I think I figured out the answers to
Since the getElementsByTagName() function is new (DOM-1?) I wanted another more reliable method to
Since I'm just learning C# with .NET, (I have more experience with the XNA
Since I have started learning about rendering to a texture I grew to understand
I made another post earlier about this subject but I've since changed my code

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.