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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:35:41+00:00 2026-06-04T21:35:41+00:00

My understanding is that string is a member of the std namespace, so why

  • 0

My understanding is that string is a member of the std namespace, so why does the following occur?

#include <iostream>

int main()
{
    using namespace std;

    string myString = "Press ENTER to quit program!";
    cout << "Come up and C++ me some time." << endl;
    printf("Follow this command: %s", myString);
    cin.get();

    return 0;
}

enter image description here

Each time the program runs, myString prints a seemingly random string of 3 characters, such as in the output above.

  • 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-04T21:35:42+00:00Added an answer on June 4, 2026 at 9:35 pm

    C++23 Update

    We now finally have std::print as a way to use std::format for output directly:

    #include <print>
    #include <string>
    
    int main() {
        // ...
        std::print("Follow this command: {}", myString);
        // ...
    }
    

    This combines the best of both approaches.

    Original Answer

    It’s compiling because printf isn’t type safe, since it uses variable arguments in the C sense1. printf has no option for std::string, only a C-style string. Using something else in place of what it expects definitely won’t give you the results you want. It’s actually undefined behaviour, so anything at all could happen.

    The easiest way to fix this, since you’re using C++, is printing it normally with std::cout, since std::string supports that through operator overloading:

    std::cout << "Follow this command: " << myString;
    

    If, for some reason, you need to extract the C-style string, you can use the c_str() method of std::string to get a const char * that is null-terminated. Using your example:

    #include <iostream>
    #include <string>
    #include <stdio.h>
    
    int main()
    {
        using namespace std;
    
        string myString = "Press ENTER to quit program!";
        cout << "Come up and C++ me some time." << endl;
        printf("Follow this command: %s", myString.c_str()); //note the use of c_str
        cin.get();
    
        return 0;
    }
    

    If you want a function that is like printf, but type safe, look into variadic templates (C++11, supported on all major compilers as of MSVC12). You can find an example of one here. There’s nothing I know of implemented like that in the standard library, but there might be in Boost, specifically boost::format.


    [1]: This means that you can pass any number of arguments, but the function relies on you to tell it the number and types of those arguments. In the case of printf, that means a string with encoded type information like %d meaning int. If you lie about the type or number, the function has no standard way of knowing, although some compilers have the ability to check and give warnings when you lie.

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

Sidebar

Related Questions

int main(void) { std::string foo(foo); } My understanding is that the above code uses
I'm fairly new to C++, but my understanding is that a #include statement will
I have the following scenario that may warrant storing data in a conroller member
I have a struct: struct OutputStore { int myINT; string mySTRING; } If I
I am using h helper method in Rails to encode/escape a string that has
I have the following method: public IEnumerable<Foo> GetFoo(int x, string y) { return from
I have an ASPX MVC v3 applications that is using String sourceIp = Request.ServerVariables[REMOTE_ADDR];
It's my understanding that in Spring, all objects are treated by default as singletons.
Understanding that I should probably just dig into the source to come up with
It's my understanding that all three of these lines below should return an ARRAY

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.