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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:08:41+00:00 2026-05-19T13:08:41+00:00

At the following regarding strncpy : http://www.cplusplus.com/reference/clibrary/cstring/strncpy/ , it mentions the following: No null-character

  • 0

At the following regarding strncpy: http://www.cplusplus.com/reference/clibrary/cstring/strncpy/, it mentions the following:

No null-character is implicitly appended to the end of destination, so destination will only be null-terminated if the length of the C string in source is less than num.

What is meant by this sentence?

  • 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-19T13:08:41+00:00Added an answer on May 19, 2026 at 1:08 pm

    It means that, for example, if your source string is 20 characters plus a null terminator and your strncpy specifies less than 21 characters, the target string will not have a null appended to it.

    It’s because of the way it works: strncpy guarantees that it will write exactly N bytes where N is the length value passed in.

    If the length of the source string (sans null byte) is less than that, it will pad the destination area with nulls. If it’s equal or greater, you won’t get a null added to the destination.

    That means it may not technically be a C string that you get. This can be solved with code like:

    char d[11];          // Have enough room for string and null.
    strncpy (d, s, 10);  // Copy up to 10 bytes of string, null the rest.
    d[10] = '\0';        // Then append null manually in case s was too long.
    

    You allocate 11 bytes (array indexes 0..10), copy up to 10 (indexes 0..9) then set the 11th (index 10) to null.

    Here’s a diagram showing the three possibilities for writing various-sized strings to a 10-character area with strncpy (d, s, 10) where . represents a null byte:

    s              d
    -------------  ----------
    Hello.         Hello.....
    Hello Fred.    Hello Fred
    Hello George.  Hello Geor
    

    Note that in the second and third case, no null byte is written so, if you treat d as a string, you’re likely to be disappointed in the outcome.

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

Sidebar

Related Questions

I was reading the following article: http://msdn.microsoft.com/en-us/magazine/cc817398.aspx Solving 11 Likely Problems In Your Multithreaded
I'm writing a small webapp in Grails and I have the following question regarding
Following my question regarding a .NET YAML Library ... as there doesn't seem to
Following on from my recent question regarding parsing XML files in Java I have
I was following along with the railscast regarding the restful_authentication plugin. He recommended running
I have a question regarding memory allocation order. In the following code I allocate
I have following questions regarding strings in C++: 1>> which is a better option(considering
I have the following question regarding Flex/AIR data grids: Can I access the value
I have following queries regarding java string pool: Is it a part of java
The Apple documentation gives the following warning regarding using View Controllers to manage part

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.