C Reference String Operation: strncpy()

The function strncpy() copies up to n characters of one string to another string.

Usage:

char * strncpy( char * target, const char *source, size_t count);

If the source has less characters then is asked for in count, then the remainder is filled with ‘\0’ characters.

Note: No null-character is implicitly appended at the end of target.

This means you have to add it yourself or the source has less characters then is asked for in count.

strncpy() source code example:


	#include<stdio.h>
	#include<string.h>

	main()
	{
		char line[100];
		strncpy(line, "hello",3);
		line[3]='\0';
		printf ("%s\n", line);
	}

Output of the example:


	hel

This entry was posted in C Reference string.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.