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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:02:37+00:00 2026-06-13T11:02:37+00:00

Is snprintf always null terminating the destination buffer? In other words, is this sufficient:

  • 0

Is snprintf always null terminating the destination buffer?

In other words, is this sufficient:

char dst[10];

snprintf(dst, sizeof (dst), "blah %s", somestr);

or do you have to do like this, if somestr is long enough?

char dst[10];

somestr[sizeof (dst) - 1] = '\0';
snprintf(dst, sizeof (dst) - 1, "blah %s", somestr);

I am interested both in what the standard says and what some popular libc might do which is not standard behavior.

  • 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-13T11:02:37+00:00Added an answer on June 13, 2026 at 11:02 am

    As the other answers establish: It should:

    snprintf … Writes the results to a character string buffer. (…)
    will be terminated with a null character, unless buf_size is zero.

    So all you have to take care is that you don’t pass an zero-size buffer to it, because (obviously) it cannot write a zero to “nowhere”.


    However, beware that Microsoft’s library does not have a function called snprintf but instead historically only had a function called _snprintf (note leading underscore) which does not append a terminating null. Here’s the docs (VS 2012, ~~ VS 2013):

    http://msdn.microsoft.com/en-us/library/2ts7cx93%28v=vs.110%29.aspx

    Return Value

    Let len be the length of the formatted data string (not including the
    terminating null). len and count are in bytes for _snprintf, wide
    characters for _snwprintf.

    • If len < count, then len characters are stored in buffer, a
      null-terminator is appended, and len is returned.

    • If len = count, then len characters are stored in buffer, no
      null-terminator is appended, and len is returned.

    • If len > count, then count characters are stored in buffer, no
      null-terminator is appended, and a negative value is returned.

    (…)

    Visual Studio 2015 (VC14) apparently introduced the conforming snprintf function, but the legacy one with the leading underscore and the non null-terminating behavior is still there:

    The snprintf function truncates the output when len is greater
    than or equal to count, by placing a null-terminator at
    buffer[count-1]. (…)

    For all functions other than snprintf, if len = count, len
    characters are stored in buffer, no null-terminator is appended,
    (…)

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

Sidebar

Related Questions

i always like to use other sanitazing methods (and prepared statments) but i am
With int snprintf(char *str, size_t size, const char *format, ...); I can write specific
I have this code: string get_md5sum(unsigned char* md) { char buf[MD5_DIGEST_LENGTH + MD5_DIGEST_LENGTH]; char
_gnu_cxx::snprintf has not been declared when I include string in my cpp. This error
As I'm writing JavaScript I'm always missing some fairly basic language features that JavaScript
I'm starting a new project shortly and going to use coding standards. I've always
I have this little script, which should ping the IPs in the $hosts_to_ping array.
This piece of code is part of a large source code but the bug
What is C# analog of C snprintf()? In C code here we use snprintf(buf,
I've got this class I found for converting timestamps to X Time Ago. It

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.