C String Function strncpy

Source: Internet
Author: User

Prototype: extern char *strncpy (char *dest, char *src, int n);

Usage: #include <string.h>

Function: Copy the first n bytes of the null-terminated string from SRC to the dest-point array.

Description

If the first n bytes of SRC contain no null characters, the result does not end with a null character.

If the length of SRC is less than n bytes, the dest is filled with null until the n bytes are copied.

The memory regions referred to in SRC and dest cannot overlap and dest must have enough space to hold the SRC string.

Returns a pointer to Dest.

Example:

// strncpy.c

#include <syslib.h>
#include <string.h>
main()
{
 char *s="Golden Global View";
 char *d="Hello, GGV Programmers";
 char *p=strdup(s);

 clrscr();
 textmode(0x00); // enable 6 lines mode

 strncpy(d,s,strlen(s));
 printf("%s\n",d);

 strncpy(p,s,strlen(d));
 printf("%s",p);

 getchar();
 return 0;
}

Related functions: memccpy,memcpy,stpcpy,strcpy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.