C strncpy function copy strings

Syntax

C strncpy function has the following format.

char *strncpy(char *str1, const char *str2, size_t count);

C strncpy function is from header file string.h.

Description

C strncpy function copies up to count characters from *str2 to *str1 and returns a pointer to str1.

str2 must be a pointer to a null-terminated string.

If *str2 has less than 'count' characters, nulls will be appended to *str1 until count characters have been copied. If *str2 is longer than count characters, the result *str1 will not be null terminated.

Example

Use C strncpy function to copy string.


#include<stdio.h>
#include<string.h>
//from   w  w  w. j av  a 2s.co  m
int main(void){
  char str1[128], str2[80];

  gets(str1);
  strncpy(str2, str1, 79);

  printf("%s",str2);
}

The code above generates the following result.





















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h