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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:48:57+00:00 2026-05-26T12:48:57+00:00

The following code does not produce the expected output. Why? wchar_t* wchar_t_printf_return(wchar_t* formatstring, …){

  • 0

The following code does not produce the expected output. Why?


    wchar_t* wchar_t_printf_return(wchar_t* formatstring, ...){
        va_list argp;
        va_start(argp, formatstring);
        int templen = 256;
        templen = vsnwprintf(NULL, 0, formatstring, argp)+3;
        wchar_t *buffer = (wchar_t *) malloc ((templen+1)*sizeof(wchar_t));
        memset(buffer, 0, (templen+1)*sizeof(*buffer));
        int retval;
        while ((retval = vsnwprintf(buffer, templen, formatstring, argp)) == -1 || (retval >= (templen-1))){
            templen = templen << 1;
            buffer = (wchar_t *) realloc (buffer, (templen+1)*sizeof(wchar_t));
            va_end(argp);
            va_start(argp, formatstring);
        }
        va_end(argp);
        buffer[templen] = L'\0';
        return buffer;
    }

    int main(){
        int i;
        char *id = "2923BE84E16CD6AE529049F1F1BBE9EB";
        wchar_t *val = wchar_t_printf_return(L"'%s'", id);
        printf("%ls\n", val);
    }

EDIT: to state more specifically, the printf in main should wrap the id in two single quotes thereby outputting: ‘2923BE84E16CD6AE529049F1F1BBE9EB’. The purpose of the main here is to illustrate the bug in the function, no more. The function is supposed to be an alternate of the printf family functions which return the result in a newly allocated buffer instead of a preexisting one. This is being run in cygwin compiled natively via gcc-3 with the -mno-cygwin option (aka mingw). Sorry for the confusion!

  • 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-26T12:48:57+00:00Added an answer on May 26, 2026 at 12:48 pm

    The %s specifier changes meaning depending on whether you are using a printf or wprintf family function. When used with a wprintf family function, the %s specifier indicates a wide string, but you’re passing a narrow string. You need %hs to say “This is a narrow string.”

    (You seemed to be aware of this because you use %ls to print a wide string with a printf-family function, but you somehow forgot about it when going the other way.)

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

Sidebar

Related Questions

Why does the following code Graphics[Raster[{{Hue[1], Hue[1/3]}, {Hue[2/3], Hue[1/6]}}]] not produce any output? In
The following Common Lisp code does not produce the output I would expect it
The following code does not compile: //int a = ... int? b = (int?)
Why does the following code not produce three lines, all with similar gradients? <html>
Why does the following C# code not produce the equivalent of my Silverlight code?
Short and sweet: Why does the following block of Objective-C code not produce a
The following code does not produce an exception but instead passes the value 4
I've got the following code that does not produce an CA1804 warning (declared variable
The following code does not compile: public class GenericsTest { public static void main(String[]
The following Code does not compile Dim BasicGroups As String() = New String() {Node1,

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.