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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:52:00+00:00 2026-06-12T13:52:00+00:00

Possible Duplicate: Why should you use strncpy instead of strcpy? I’m reading a book

  • 0

Possible Duplicate:
Why should you use strncpy instead of strcpy?

I’m reading a book about computers/cryptographic etc. And often the writer use thing such as strncpy(dest, src, strlen(src)); instead of strcpy(dest, src); it makes no much sense for me.. Well, I’m a professional programmer.

The question is: It make really a real difference? real applications use something like this?

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

    The author is almost certainly abusing the strn* functions. Unfortunately, there is almost never a good reason to use strn* functions, since they don’t actually do what you want.

    Let’s take a look at strcpy and strncpy:

    char *strcpy(char *dest, const char *src);
    
    char *strncpy(char *dest, const char *src, size_t n);
    

    The strcpy function copies src into dest, including a trailing \0. It is only rarely useful:

    1. Unless you know the source string fits in the destination buffer, you have a buffer overflow. Buffer overflows are security hazards, they can crash your program, and cause it to behave incorrectly.

    2. If you do know the size of the source string and destination buffer, you might as well use memcpy.

    By comparison, the strncpy copies at most n bytes of src into dest and then pads the rest with \0. It is only rarely useful:

    1. Unless you know the source string is smaller than the destination buffer, you cannot be certain that the resulting buffer will be nul-terminated. This can cause errors elsewhere in the program, if you assume the result is nul-terminated.

    2. If you do know the source string is smaller, again, you might as well use memcpy.

    3. You can simply terminate the string after you call strncpy, but are you sure that you want to silently truncate the result? I’d imagine most of the time, you’d rather have an error message.

    Why do these functions exist?

    The strcpy function is occasionally handy, but it is mostly a relic of an era where people did not care very much about validating input. Feeding a string that is too large to a program would crash it, and the advice was “don’t do that”.

    The strncpy function is useful when you want to transmit data in fixed-size fields, and you don’t want to put garbage in the remainder of the field. This is mostly a relic of an era where people used fixed-size fields.

    So you will rarely see strcat or strncpy in modern C software.

    A worse problem

    However, your example combines the worst of both worlds. Let’s examine this piece of source code:

    strncpy(dest, src, strlen(src));
    

    This copies src into dest, without a \0 terminator and without bounds checking. It combines the worst aspect of strcpy (no bounds checking) with the worst aspect of strncpy (no terminator). If you see code like this, run away.

    How to work with strings

    Good C code typically uses one of a few options for working with strings:

    1. Use fixed buffers and snprintf, as long as you don’t mind fixed buffers.

    2. Use bounded string functions like strlcpy and strlcat. These are BSD extensions.

    3. Use a custom string point which tracks string lengths, and write your own functions using memcpy and malloc.

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

Sidebar

Related Questions

Possible Duplicate: When should I use Inline vs. External Javascript? Often Javascript needs to
Possible Duplicate: Why should I use ‘li’ instead of ‘div’? I'm figuring out how
Possible Duplicate: Should I use != or <> for not equal in TSQL? Behavior
Possible Duplicate: Why should I use templating system in PHP? I was just curious
Possible Duplicate: When should one use final? When should Java programmers prefer to use
Possible Duplicate: Should I use Java's String.format() if performance is important? I was wondering
Possible Duplicate: Should I use uint in C# for values that can’t be negative?
Possible Duplicate: When should I use GET or POST method? What’s the difference between
Possible Duplicate: Should I prefer to use literal syntax or constructors for creating dictionaries
Possible Duplicate: Understanding Enums in Java Why should we use enums rather Java constants?

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.