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

  • Home
  • SEARCH
  • 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 7754271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:15:53+00:00 2026-06-01T12:15:53+00:00

When using printf to format a double-byte string into a single-byte string: printf(%ls\n, Ls:\\яшертыHello);

  • 0

When using printf to format a double-byte string into a single-byte string:

printf("%ls\n", L"s:\\яшертыHello");   // %ls for a wide string (%s varies meaning depending on the project's unicode settings).

Clearly, some characters can’t be represented as ascii characters, so sometimes I have seen behaviour where double-byte characters get turned into a ‘?’ mark character. But, this seems to depend on the particular characters. For the printf above, the output is:

s:\

I was hoping I might get something like:

s:\??????Hello

I’m afraid I’ve lost the example, but I think for one string when it encountered unicode characters, replaced the first one with a ‘?’ and then gave up on the rest.

So, my question is, what’s supposed to happen when you format a wide string into a single-byte string. Documentation here: http://msdn.microsoft.com/en-us/library/hf4y5e3w.aspx says “Characters are displayed up to the first null character”. But, I’m not seeing that. Is this a bug in printf, or is the behaviour I’m seeing documented somewhere, if so, where.

Thanks for your help.

UPDATE

Thanks for the answers from people giving me alternatives to using printf. I am going to change to an alternative, but I’m really interested out of curiosity why does printf not have reliable documented behaviour. It appears almost as if the implementer of it went out of their way to make this not work.

  • 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-01T12:15:54+00:00Added an answer on June 1, 2026 at 12:15 pm

    I expect your code to work — and it works here on Linux — but it is locale dependent. That means you have to set up the locale and your locale must support the character set used. Here is my test program:

    #include <locale.h>
    #include <stdio.h>
    
    int main()
    {
        int c;
        char* l = setlocale(LC_ALL, "");
        if (l == NULL) {
            printf("Locale not set\n");
        } else {
            printf("Locale set to %s\n", l);
        }
        printf("%ls\n", L"s:\\яшертыHello");
        return 0;
    }
    

    and here is an execution trace:

    $ env LC_ALL=en_US.utf8 ./a.out
    Locale set to en_US.utf8
    s:\яшертыHello
    

    If it says that the locale isn’t set or is set to “C”, it is normal that you don’t get the result you expect.

    Edit: see the answers to this question for the equivalent of en_US.utf8 for Windows.

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

Sidebar

Related Questions

I'm using String.format() in Java trying to emulate the printf() control channel available in
Using .NET string formatting you can plug the same value into a format string
When using printf for string, I got : string key = 123; printf(Value is
I'm trying to format a number using printf but can't find the correct mask.
I've recently read this article on using printf and scanf in assembly: Meaning of
How do I format output by using printf. suppose i want to print following
Using the code print('{0} is not'.format('That that is not')) in Python 3.1.1, I get
How can I output colored text using printf on both Mac OS X and
One can remove all calls to printf() using #define printf . What if I
I would like to use the syntax that printf uses, using the %d, %s

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.