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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:52:22+00:00 2026-05-22T16:52:22+00:00

When using standard char* strings, the snprintf and vsnprintf functions will return the length

  • 0

When using standard char* strings, the snprintf and vsnprintf functions will return the length of the output string, even if that string was truncated due to overflow.* It seems like the ISO C committee didn’t like this functionality when they added swprintf and vswprintf, which return -1 on overflow.

Does anyone know of a function that will provide this length? I don’t know the size of the potential strings. I might be asking too much, but.. I’d rather not:

  • allocate a huge static temp buffer
  • iteratively allocate and free memory until i’ve found a size that fits
  • add an additional library dependency
  • write my own format string parser

*I realize MSVC doesn’t do this, and instead provides the scprintf and vscprintf functions, but I’m looking for other compilers, mainly GCC.

  • 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-22T16:52:23+00:00Added an answer on May 22, 2026 at 4:52 pm

    My best suggestion to you would be not to use wchar_t strings at all, especially if you’re not writing Windows-oriented code. In case that’s not an option, here are some other ideas:

    1. If your format string does not contain non-ASCII characters itself, what about first calling vsnprintf with the same set of arguments to get the length in bytes, then use that as a safe upper bound for the length in wchar_t characters (if there are few or non-ASCII characters, the bound will be tight).

    2. If you’re okay with introducing a dependency on a POSIX function (which is likely to be added to C1x), use open_wmemstream and fwprintf.

    3. Just iterate allocating larger buffers, but do it smart: increase the size geometrically at each step, e.g. 127, 255, 511, 1023, 2047, … I like this pattern better than whole powers of 2 because it’s easy to avoid dangerous case where allocation might succeed for SIZE_MAX/2+1 but then wrap to 0 at the next iteration.

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

Sidebar

Related Questions

Using standard mysql functions is there a way to write a query that will
Is there a standard C function that allows you to build strings using format
Using MSVC2010... I have a struct that wraps a std::string, with standard move ctor's
I have a flash message in Django that is built using a standard char
In my ASP MVC application I'm using standard SQL (rather that Linq to SQL
I have an application running under WebLogic that is using standard forms authentication. The
What is the standard way to copy two structs that contain char arrays? Here
I am trying to write server that will communicate with any standard client that
I replaced the Standard allocator with an allocator that will phone home about how
not using the C standard library do this in C++ ? Convert string to

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.